From bbd6c5b532134a4bab9600683b85084ef73e64e8 Mon Sep 17 00:00:00 2001 From: CPunch Date: Fri, 21 Aug 2020 21:03:12 -0500 Subject: [PATCH 1/5] moved header libraries --- Makefile | 3 ++- src/NPCManager.cpp | 2 +- src/{ => contrib}/INIReader.hpp | 0 src/{ => contrib}/JSON.hpp | 0 src/settings.cpp | 2 +- 5 files changed, 4 insertions(+), 3 deletions(-) rename src/{ => contrib}/INIReader.hpp (100%) rename src/{ => contrib}/JSON.hpp (100%) diff --git a/Makefile b/Makefile index be6ed7a..d8eef65 100644 --- a/Makefile +++ b/Makefile @@ -39,7 +39,8 @@ HDR=\ src/CNShardServer.hpp\ src/CNShared.hpp\ src/CNStructs.hpp\ - src/INIReader.hpp\ + src/contrib/INIReader.hpp\ + src/contrib/JSON.hpp\ src/NanoManager.hpp\ src/ItemManager.hpp\ src/NPCManager.hpp\ diff --git a/src/NPCManager.cpp b/src/NPCManager.cpp index 8ac6a0d..42206dd 100644 --- a/src/NPCManager.cpp +++ b/src/NPCManager.cpp @@ -6,7 +6,7 @@ #include #include -#include "JSON.hpp" +#include "contrib/JSON.hpp" std::map NPCManager::NPCs; diff --git a/src/INIReader.hpp b/src/contrib/INIReader.hpp similarity index 100% rename from src/INIReader.hpp rename to src/contrib/INIReader.hpp diff --git a/src/JSON.hpp b/src/contrib/JSON.hpp similarity index 100% rename from src/JSON.hpp rename to src/contrib/JSON.hpp diff --git a/src/settings.cpp b/src/settings.cpp index a4fcb47..59dff6d 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -1,6 +1,6 @@ #include #include "settings.hpp" -#include "INIReader.hpp" +#include "contrib/INIReader.hpp" // defaults :) int settings::LOGINPORT = 8001; From cff382a8cec04ea26cd3a45d466c6a2d4f30c3b3 Mon Sep 17 00:00:00 2001 From: CPunch Date: Fri, 21 Aug 2020 21:32:22 -0500 Subject: [PATCH 2/5] sets a limit for sendData() --- src/CNProtocol.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/CNProtocol.cpp b/src/CNProtocol.cpp index ad724d6..f7df999 100644 --- a/src/CNProtocol.cpp +++ b/src/CNProtocol.cpp @@ -73,12 +73,15 @@ CNSocket::CNSocket(SOCKET s, PacketHandler ph): sock(s), pHandler(ph) { bool CNSocket::sendData(uint8_t* data, int size) { int sentBytes = 0; + int maxTries = 10; while (sentBytes < size) { int sent = send(sock, (buffer_t*)(data + sentBytes), size - sentBytes, 0); // no flags defined if (SOCKETERROR(sent)) { - if (errno == 11) + if (errno == 11 && maxTries > 0) { + maxTries--; continue; // try again + } std::cout << "[FATAL] SOCKET ERROR: " << errno << std::endl; return false; // error occured while sending bytes } From f71e1349c14a1afb776040e955f1682aeb512986 Mon Sep 17 00:00:00 2001 From: CPunch Date: Fri, 21 Aug 2020 22:11:04 -0500 Subject: [PATCH 3/5] temp fix for U16toU8 edgecase --- src/CNStructs.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/CNStructs.cpp b/src/CNStructs.cpp index 2885f36..b580cc3 100644 --- a/src/CNStructs.cpp +++ b/src/CNStructs.cpp @@ -1,8 +1,12 @@ #include "CNStructs.hpp" std::string U16toU8(char16_t* src) { - std::wstring_convert,char16_t> convert; - return convert.to_bytes(src); + try { + std::wstring_convert,char16_t> convert; + return convert.to_bytes(src); + } catch(std::exception e) { + return ""; + } } // returns number of char16_t that was written at des From 78c493b4610195ae5d0c6d9bb0257f80eceda63e Mon Sep 17 00:00:00 2001 From: CPunch Date: Fri, 21 Aug 2020 23:37:09 -0500 Subject: [PATCH 4/5] switched default ip in config.ini --- config.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.ini b/config.ini index b73ff3b..c479216 100644 --- a/config.ini +++ b/config.ini @@ -10,7 +10,7 @@ randomcharacters=true [shard] port=8002 # you'll want to change this one -ip=192.168.1.183 +ip=127.0.0.1 # distance at which other players and NPCs become visible view=20000 # little message players see when they enter the game From 5f65a84b022446ac66a8ba8c0c60d5d749a3bafa Mon Sep 17 00:00:00 2001 From: FinnHornhoover <30576665+FinnHornhoover@users.noreply.github.com> Date: Sat, 22 Aug 2020 09:46:52 +0300 Subject: [PATCH 5/5] Fix unhandled exception in NPCManager (#24) * fixed PROTOCOL_VERSION not being defined * handle exceptions in NPCManager init --- src/NPCManager.cpp | 39 ++++++++++++++------------------------- 1 file changed, 14 insertions(+), 25 deletions(-) diff --git a/src/NPCManager.cpp b/src/NPCManager.cpp index 42206dd..52cc07e 100644 --- a/src/NPCManager.cpp +++ b/src/NPCManager.cpp @@ -10,37 +10,26 @@ std::map NPCManager::NPCs; -#define CHECKNPC(x) if (NPC.value().find(x) == NPC.value().end()) { \ - std::cout << "[WARN] Malformed NPC.json file! failed to find " << x << std::endl; \ - return; \ - } - void NPCManager::init() { // load NPCs from NPCs.json into our NPC manager - std::ifstream inFile("NPCs.json"); - nlohmann::json jsonData; + try { + std::ifstream inFile("NPCs.json"); + nlohmann::json jsonData; - // read file into jsonData - inFile >> jsonData; + // read file into jsonData + inFile >> jsonData; - for (auto& NPC : jsonData.items()) { - std::string name = NPC.key(); + for (auto& npc : jsonData) { + BaseNPC tmp(npc["x"], npc["y"], npc["z"], npc["id"]); + NPCManager::NPCs[tmp.appearanceData.iNPC_ID] = tmp; + } - // sanity checks - CHECKNPC("id") - CHECKNPC("x") - CHECKNPC("y") - CHECKNPC("z") - - int x = (int)(jsonData[NPC.key()]["x"]); - int y = (int)(jsonData[NPC.key()]["y"]); - int z = (int)(jsonData[NPC.key()]["z"]); - BaseNPC tmp(x, y, z, jsonData[NPC.key()]["id"]); - NPCManager::NPCs[tmp.appearanceData.iNPC_ID] = tmp; + std::cout << "populated " << NPCs.size() << " NPCs" << std::endl; + } + catch (const std::exception& err) { + std::cerr << "[WARN] Malformed NPC.json file! Reason:" << std::endl << err.what() << std::endl; } - - std::cout << "populated " << NPCs.size() << " NPCs" << std::endl; } #undef CHECKNPC @@ -49,7 +38,7 @@ void NPCManager::updatePlayerNPCS(CNSocket* sock, PlayerView& view) { std::list yesView; std::list noView; - for (auto pair : NPCs) { + for (auto& pair : NPCs) { int diffX = abs(view.plr.x - pair.second.appearanceData.iX); int diffY = abs(view.plr.y - pair.second.appearanceData.iY);