This commit is contained in:
FinnHornhoover 2020-08-24 02:35:28 +03:00
commit bd4b04649f
23 changed files with 151 additions and 86 deletions

3
.gitignore vendored
View File

@ -8,4 +8,5 @@ tags
CMakeFiles/
CMakeCache.txt
build/
.vs/
.vs/
.idea/

View File

@ -1,6 +1,6 @@
CXX=clang++
# -w suppresses all warnings (the part that's commented out helps me find memory leaks, it ruins performance though!)
CXXFLAGS=-std=c++17 -O3 -DPROTOCOL_VERSION=$(PROTOCOL_VERSION) #-g3 -fsanitize=address
CXXFLAGS=-Wall -std=c++17 -O3 -DPROTOCOL_VERSION=$(PROTOCOL_VERSION) #-g3 -fsanitize=address
LDFLAGS=-lpthread
# specifies the name of our exectuable
SERVER=bin/fusion
@ -11,7 +11,7 @@ PROTOCOL_VERSION?=104
# Windows-specific
WIN_CXX=x86_64-w64-mingw32-g++
WIN_CXXFLAGS=-std=c++17 -O3 -DPROTOCOL_VERSION=$(PROTOCOL_VERSION) #-g3 -fsanitize=address
WIN_CXXFLAGS=-Wall -std=c++17 -O3 -DPROTOCOL_VERSION=$(PROTOCOL_VERSION) #-g3 -fsanitize=address
WIN_LDFLAGS=-static -lws2_32 -lwsock32
WIN_SERVER=bin/winfusion.exe

View File

@ -1,5 +1,9 @@
# should the server print every packet it receives?
verbose=false
# verbosity level
# 0 = mostly silence
# 1 = debug prints and unknown packets
# 2 = print all packets except LIVE_CHECK and movement
# 3 = print all packets
verbosity=1
# Login Server configuration
[login]

View File

@ -17,8 +17,7 @@ CNLoginServer::CNLoginServer(uint16_t p) {
}
void CNLoginServer::handlePacket(CNSocket* sock, CNPacketData* data) {
if (settings::VERBOSE)
std::cout << "OpenFusion: received " << Defines::p2str(CL2LS, data->type) << " (" << data->type << ")" << std::endl;
printPacket(data, CL2LS);
switch (data->type) {
case P_CL2LS_REQ_LOGIN: {
@ -27,7 +26,6 @@ void CNLoginServer::handlePacket(CNSocket* sock, CNPacketData* data) {
sP_CL2LS_REQ_LOGIN* login = (sP_CL2LS_REQ_LOGIN*)data->buf;
INITSTRUCT(sP_LS2CL_REP_LOGIN_SUCC, resp);
uint64_t cachedKey = sock->getEKey(); // so we can still send the resp packet with the correct key
int charCount = 2; // send 4 randomly generated characters for now
DEBUGLOG(
@ -281,7 +279,8 @@ void CNLoginServer::handlePacket(CNSocket* sock, CNPacketData* data) {
break;
}
default:
std::cerr << "OpenFusion: LOGIN UNIMPLM ERR. PacketType: " << Defines::p2str(CL2LS, data->type) << " (" << data->type << ")" << std::endl;
if (settings::VERBOSITY)
std::cerr << "OpenFusion: LOGIN UNIMPLM ERR. PacketType: " << Defines::p2str(CL2LS, data->type) << " (" << data->type << ")" << std::endl;
break;
}
}

View File

@ -1,5 +1,4 @@
#ifndef _CNLS_HPP
#define _CNLS_HPP
#pragma once
#include "CNProtocol.hpp"
#include "Defines.hpp"
@ -24,5 +23,3 @@ public:
void newConnection(CNSocket* cns);
void killConnection(CNSocket* cns);
};
#endif

View File

@ -63,7 +63,7 @@ int CNSocketEncryption::decryptData(uint8_t* buffer, uint8_t* key, int size) {
// ========================================================[[ CNPacketData ]]========================================================
CNPacketData::CNPacketData(void* b, uint32_t t, int l): buf(b), type(t), size(l) {}
CNPacketData::CNPacketData(void* b, uint32_t t, int l): buf(b), size(l), type(t) {}
// ========================================================[[ CNSocket ]]========================================================
@ -78,7 +78,7 @@ bool CNSocket::sendData(uint8_t* data, int size) {
while (sentBytes < size) {
int sent = send(sock, (buffer_t*)(data + sentBytes), size - sentBytes, 0); // no flags defined
if (SOCKETERROR(sent)) {
if (errno == 11 && maxTries > 0) {
if (errno == EAGAIN && maxTries > 0) {
maxTries--;
continue; // try again
}
@ -181,6 +181,10 @@ void CNSocket::step() {
// we'll just leave bufferIndex at 0 since we already have the packet size, it's safe to overwrite those bytes
activelyReading = true;
} else if (errno != EAGAIN) {
// serious socket issue, disconnect connection
kill();
return;
}
}
@ -189,6 +193,11 @@ void CNSocket::step() {
int recved = recv(sock, (buffer_t*)(readBuffer + readBufferIndex), readSize - readBufferIndex, 0);
if (!SOCKETERROR(recved))
readBufferIndex += recved;
else if (errno != EAGAIN) {
// serious socket issue, disconnect connection
kill();
return;
}
}
if (activelyReading && readBufferIndex - readSize <= 0) {
@ -352,6 +361,28 @@ void CNServer::kill() {
connections.clear();
}
void CNServer::printPacket(CNPacketData *data, int type) {
if (settings::VERBOSITY < 2)
return;
if (settings::VERBOSITY < 3) switch (data->type) {
case P_CL2LS_REP_LIVE_CHECK:
case P_CL2FE_REP_LIVE_CHECK:
case P_CL2FE_REQ_PC_MOVE:
case P_CL2FE_REQ_PC_JUMP:
case P_CL2FE_REQ_PC_SLOPE:
case P_CL2FE_REQ_PC_MOVEPLATFORM:
case P_CL2FE_REQ_PC_MOVETRANSPORTATION:
case P_CL2FE_REQ_PC_ZIPLINE:
case P_CL2FE_REQ_PC_JUMPPAD:
case P_CL2FE_REQ_PC_LAUNCHER:
case P_CL2FE_REQ_PC_STOP:
return;
}
std::cout << "OpenFusion: received " << Defines::p2str(type, data->type) << " (" << data->type << ")" << std::endl;
}
void CNServer::newConnection(CNSocket* cns) {} // stubbed
void CNServer::killConnection(CNSocket* cns) {} // stubbed
void CNServer::onTimer() {} // stubbed

View File

@ -1,8 +1,7 @@
#ifndef _CNP_HPP
#define _CNP_HPP
#pragma once
#define MAX_PACKETSIZE 8192
#define DEBUGLOG(x) x
#define DEBUGLOG(x) if (settings::VERBOSITY) {x};
#include <iostream>
#include <stdio.h>
@ -39,6 +38,9 @@
#include <list>
#include <queue>
#include "Defines.hpp"
#include "settings.hpp"
#if defined(__MINGW32__) && !defined(_GLIBCXX_HAS_GTHREADS)
#include "mingw/mingw.mutex.h"
#else
@ -148,9 +150,8 @@ public:
void start();
void kill();
static void printPacket(CNPacketData *data, int type);
virtual void newConnection(CNSocket* cns);
virtual void killConnection(CNSocket* cns);
virtual void onTimer(); // called every 2 seconds
};
#endif

View File

@ -18,13 +18,11 @@ CNShardServer::CNShardServer(uint16_t p) {
}
void CNShardServer::handlePacket(CNSocket* sock, CNPacketData* data) {
if (settings::VERBOSE)
std::cout << "OpenFusion: received " << Defines::p2str(CL2FE, data->type) << " (" << data->type << ")" << std::endl;
printPacket(data, CL2FE);
if (ShardPackets.find(data->type) != ShardPackets.end())
ShardPackets[data->type](sock, data);
else
else if (settings::VERBOSITY)
std::cerr << "OpenFusion: SHARD UNIMPLM ERR. PacketType: " << Defines::p2str(CL2FE, data->type) << " (" << data->type << ")" << std::endl;
}

View File

@ -1,5 +1,4 @@
#ifndef _CNSS_HPP
#define _CNSS_HPP
#pragma once
#include "CNProtocol.hpp"
#include "Defines.hpp"
@ -21,5 +20,3 @@ public:
void killConnection(CNSocket* cns);
void onTimer();
};
#endif

View File

@ -3,8 +3,7 @@
There's some data shared between the Login Server and the Shard Server. Of course all of this needs to be thread-safe. No mucking about on this one!
*/
#ifndef _CNSD_HPP
#define _CNSD_HPP
#pragma once
#include <map>
#include <string>
@ -19,5 +18,3 @@ namespace CNSharedData {
Player getPlayer(int64_t sk);
void erasePlayer(int64_t sk);
}
#endif

View File

@ -2,8 +2,7 @@
CNStructs.hpp - defines some basic structs & useful methods for packets used by FusionFall based on the version defined
*/
#ifndef _CNS_HPP
#define _CNS_HPP
#pragma once
#ifdef _MSC_VER
// codecvt_* is deprecated in C++17 and MSVC will throw an annoying warning because of that.
@ -45,5 +44,3 @@ uint64_t getTime();
#else
#error Invalid PROTOCOL_VERSION
#endif
#endif

View File

@ -27,7 +27,6 @@ void ChatManager::chatHandler(CNSocket* sock, CNPacketData* data) {
otherSock->sendPacket((void*)&resp, P_FE2CL_REP_SEND_FREECHAT_MESSAGE_SUCC, sizeof(sP_FE2CL_REP_SEND_FREECHAT_MESSAGE_SUCC));
}
}
void ChatManager::menuChatHandler(CNSocket* sock, CNPacketData* data) {
if (data->size != sizeof(sP_CL2FE_REQ_SEND_MENUCHAT_MESSAGE))
return; // malformed packet
@ -37,7 +36,7 @@ void ChatManager::menuChatHandler(CNSocket* sock, CNPacketData* data) {
// send to client
INITSTRUCT(sP_FE2CL_REP_SEND_MENUCHAT_MESSAGE_SUCC, resp);
memcpy(resp.szFreeChat, chat->szFreeChat, sizeof(chat->szFreeChat));
resp.iPC_ID = plr.plr.iID;
resp.iPC_ID = PlayerManager::players[sock].plr.iID;
resp.iEmoteCode = chat->iEmoteCode;
sock->sendPacket((void*)&resp, P_FE2CL_REP_SEND_MENUCHAT_MESSAGE_SUCC, sizeof(sP_FE2CL_REP_SEND_MENUCHAT_MESSAGE_SUCC));

View File

@ -1,5 +1,4 @@
#ifndef _CM_HPP
#define _CM_HPP
#pragma once
#include "CNShardServer.hpp"
@ -10,5 +9,3 @@ namespace ChatManager {
void emoteHandler(CNSocket* sock, CNPacketData* data);
void menuChatHandler(CNSocket* sock, CNPacketData* data);
}
#endif

View File

@ -1,5 +1,4 @@
#ifndef _IM_HPP
#define _IM_HPP
#pragma once
#include "CNShardServer.hpp"
@ -9,5 +8,3 @@ namespace ItemManager {
void itemDeleteHandler(CNSocket* sock, CNPacketData* data);
void itemGMGiveHandler(CNSocket* sock, CNPacketData* data);
}
#endif

View File

@ -9,30 +9,49 @@
#include "contrib/JSON.hpp"
std::map<int32_t, BaseNPC> NPCManager::NPCs;
std::map<int32_t, WarpLocation> NPCManager::Warps;
void NPCManager::init() {
// load NPCs from NPCs.json into our NPC manager
try {
std::ifstream inFile("NPCs.json");
nlohmann::json jsonData;
nlohmann::json npcData;
// read file into jsonData
inFile >> jsonData;
// read file into json
inFile >> npcData;
for (auto& npc : jsonData) {
BaseNPC tmp(npc["x"], npc["y"], npc["z"], npc["id"]);
NPCManager::NPCs[tmp.appearanceData.iNPC_ID] = tmp;
for (nlohmann::json::iterator npc = npcData.begin(); npc != npcData.end(); npc++) {
BaseNPC tmp(npc.value()["x"], npc.value()["y"], npc.value()["z"], npc.value()["id"]);
NPCs[tmp.appearanceData.iNPC_ID] = tmp;
}
std::cout << "populated " << NPCs.size() << " NPCs" << std::endl;
std::cout << "[INFO] populated " << NPCs.size() << " NPCs" << std::endl;
} catch (const std::exception& err) {
std::cerr << "[WARN] Malformed NPCs.json file! Reason:" << err.what() << std::endl;
}
catch (const std::exception& err) {
std::cerr << "[WARN] Malformed NPC.json file! Reason:" << std::endl << err.what() << std::endl;
}
}
#undef CHECKNPC
try {
std::ifstream infile("warps.json");
nlohmann::json warpData;
// read file into json
infile >> warpData;
for (nlohmann::json::iterator warp = warpData.begin(); warp != warpData.end(); warp++) {
WarpLocation warpLoc = {warp.value()["m_iToX"], warp.value()["m_iToY"], warp.value()["m_iToZ"]};
int warpID = atoi(warp.key().c_str());
Warps[warpID] = warpLoc;
}
std::cout << "[INFO] populated " << Warps.size() << " Warps" << std::endl;
} catch (const std::exception& err) {
std::cerr << "[WARN] Malformed warps.json file! Reason:" << err.what() << std::endl;
}
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_WARP_USE_NPC, npcWarpManager);
}
void NPCManager::updatePlayerNPCS(CNSocket* sock, PlayerView& view) {
std::list<int32_t> yesView;
@ -81,4 +100,25 @@ void NPCManager::updatePlayerNPCS(CNSocket* sock, PlayerView& view) {
}
PlayerManager::players[sock].viewableNPCs = view.viewableNPCs;
}
}
void NPCManager::npcWarpManager(CNSocket* sock, CNPacketData* data)
{
if (data->size != sizeof(sP_CL2FE_REQ_PC_WARP_USE_NPC))
return; // malformed packet
sP_CL2FE_REQ_PC_WARP_USE_NPC* warpNpc = (sP_CL2FE_REQ_PC_WARP_USE_NPC*)data->buf;
// sanity check
if (Warps.find(warpNpc->iWarpID) == Warps.end())
return;
// send to client
INITSTRUCT(sP_FE2CL_REP_PC_WARP_USE_NPC_SUCC, resp);
resp.iX = Warps[warpNpc->iWarpID].x;
resp.iY = Warps[warpNpc->iWarpID].y;
resp.iZ = Warps[warpNpc->iWarpID].z;
sock->sendPacket((void*)&resp, P_FE2CL_REP_PC_WARP_USE_NPC_SUCC, sizeof(sP_FE2CL_REP_PC_WARP_USE_NPC_SUCC));
}

View File

@ -1,5 +1,4 @@
#ifndef _NPCMANAGER_HPP
#define _NPCMANAGER_HPP
#pragma once
#include "CNProtocol.hpp"
#include "PlayerManager.hpp"
@ -7,11 +6,16 @@
#include <map>
struct WarpLocation {
int x, y, z;
};
namespace NPCManager {
extern std::map<int32_t, BaseNPC> NPCs;
extern std::map<int32_t, WarpLocation> Warps;
void init();
void updatePlayerNPCS(CNSocket* sock, PlayerView& plr);
}
void npcWarpManager(CNSocket* sock, CNPacketData* data);
#endif
}

View File

@ -93,14 +93,13 @@ void NanoManager::nanoSummonHandler(CNSocket* sock, CNPacketData* data) {
sNano nano = plr.plr.Nanos[nanoId];
// Send to other players
for (CNSocket *s : PlayerManager::players[sock].viewable) {
INITSTRUCT(sP_FE2CL_NANO_ACTIVE, pkt);
INITSTRUCT(sP_FE2CL_NANO_ACTIVE, pkt1);
pkt.iPC_ID = plr.plr.iID;
pkt.Nano = nano;
pkt1.iPC_ID = plr.plr.iID;
pkt1.Nano = nano;
s->sendPacket((void*)&pkt, P_FE2CL_NANO_ACTIVE, sizeof(sP_FE2CL_NANO_ACTIVE));
}
for (CNSocket *s : PlayerManager::players[sock].viewable)
s->sendPacket((void*)&pkt1, P_FE2CL_NANO_ACTIVE, sizeof(sP_FE2CL_NANO_ACTIVE));
// update player
plr.plr.nano = nanoId;
@ -184,8 +183,8 @@ void NanoManager::setNanoSkill(CNSocket* sock, int16_t nanoId, int16_t skillId)
std::cout << U16toU8(plr.PCStyle.szFirstName) << U16toU8(plr.PCStyle.szLastName) << " set skill id " << skillId << " for nano: " << nanoId << std::endl;
)
// Update the player
PlayerManager::updatePlayer(sock, plr);
// Update the player
PlayerManager::updatePlayer(sock, plr);
}
void NanoManager::resetNanoSkill(CNSocket* sock, int16_t nanoId) {

View File

@ -1,5 +1,4 @@
#ifndef _NM_HPP
#define _NM_HPP
#pragma once
#include "CNShardServer.hpp"
@ -17,5 +16,3 @@ namespace NanoManager {
void setNanoSkill(CNSocket* sock, int16_t nanoId, int16_t skillId);
void resetNanoSkill(CNSocket* sock, int16_t nanoId);
}
#endif

View File

@ -151,6 +151,20 @@ void PlayerManager::updatePlayerPosition(CNSocket* sock, int X, int Y, int Z) {
NPCManager::updatePlayerNPCS(sock, players[sock]);
}
std::list<CNSocket*> PlayerManager::getNearbyPlayers(int x, int y, int dist) {
std::list<CNSocket*> plrs;
for (auto pair : players) {
int diffX = abs(pair.second.plr.x - x);
int diffY = abs(pair.second.plr.x - x);
if (diffX < dist && diffY < dist)
plrs.push_back(pair.first);
}
return plrs;
}
void PlayerManager::enterPlayer(CNSocket* sock, CNPacketData* data) {
if (data->size != sizeof(sP_CL2FE_REQ_PC_ENTER))
return; // ignore the malformed packet

View File

@ -1,5 +1,4 @@
#ifndef _PM_HPP
#define _PM_HPP
#pragma once
#include "Player.hpp"
#include "CNProtocol.hpp"
@ -27,6 +26,7 @@ namespace PlayerManager {
void updatePlayer(CNSocket* key, Player plr);
void updatePlayerPosition(CNSocket* sock, int X, int Y, int Z);
std::list<CNSocket*> getNearbyPlayers(int X, int Y, int dist);
void enterPlayer(CNSocket* sock, CNPacketData* data);
void loadPlayer(CNSocket* sock, CNPacketData* data);
@ -43,5 +43,3 @@ namespace PlayerManager {
void heartbeatPlayer(CNSocket* sock, CNPacketData* data);
void exitGame(CNSocket* sock, CNPacketData* data);
}
#endif

View File

@ -3,7 +3,7 @@
#include "contrib/INIReader.hpp"
// defaults :)
bool settings::VERBOSE = false;
int settings::VERBOSITY = 1;
int settings::LOGINPORT = 8001;
bool settings::LOGINRANDCHARACTERS = false;
@ -32,7 +32,7 @@ void settings::init() {
return;
}
VERBOSE = reader.GetBoolean("", "verbose", VERBOSE);
VERBOSITY = reader.GetInteger("", "verbosity", VERBOSITY);
LOGINPORT = reader.GetInteger("login", "port", LOGINPORT);
LOGINRANDCHARACTERS = reader.GetBoolean("login", "randomcharacters", LOGINRANDCHARACTERS);
SHARDPORT = reader.GetInteger("shard", "port", SHARDPORT);

View File

@ -1,8 +1,7 @@
#ifndef _SETT_HPP
#define _SETT_HPP
#pragma once
namespace settings {
extern bool VERBOSE;
extern int VERBOSITY;
extern int LOGINPORT;
extern bool LOGINRANDCHARACTERS;
extern int SHARDPORT;
@ -17,5 +16,3 @@ namespace settings {
void init();
}
#endif

1
warps.json Normal file

File diff suppressed because one or more lines are too long