Fix unhandled exception in NPCManager (#24)

* fixed PROTOCOL_VERSION not being defined

* handle exceptions in NPCManager init
This commit is contained in:
FinnHornhoover 2020-08-22 09:46:52 +03:00 committed by GitHub
parent 78c493b461
commit 5f65a84b02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -10,37 +10,26 @@
std::map<int32_t, BaseNPC> NPCManager::NPCs; std::map<int32_t, BaseNPC> 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() { void NPCManager::init() {
// load NPCs from NPCs.json into our NPC manager // load NPCs from NPCs.json into our NPC manager
std::ifstream inFile("NPCs.json"); try {
nlohmann::json jsonData; std::ifstream inFile("NPCs.json");
nlohmann::json jsonData;
// read file into jsonData // read file into jsonData
inFile >> jsonData; inFile >> jsonData;
for (auto& NPC : jsonData.items()) { for (auto& npc : jsonData) {
std::string name = NPC.key(); BaseNPC tmp(npc["x"], npc["y"], npc["z"], npc["id"]);
NPCManager::NPCs[tmp.appearanceData.iNPC_ID] = tmp;
}
// sanity checks std::cout << "populated " << NPCs.size() << " NPCs" << std::endl;
CHECKNPC("id") }
CHECKNPC("x") catch (const std::exception& err) {
CHECKNPC("y") std::cerr << "[WARN] Malformed NPC.json file! Reason:" << std::endl << err.what() << std::endl;
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;
} }
#undef CHECKNPC #undef CHECKNPC
@ -49,7 +38,7 @@ void NPCManager::updatePlayerNPCS(CNSocket* sock, PlayerView& view) {
std::list<int32_t> yesView; std::list<int32_t> yesView;
std::list<int32_t> noView; std::list<int32_t> noView;
for (auto pair : NPCs) { for (auto& pair : NPCs) {
int diffX = abs(view.plr.x - pair.second.appearanceData.iX); int diffX = abs(view.plr.x - pair.second.appearanceData.iX);
int diffY = abs(view.plr.y - pair.second.appearanceData.iY); int diffY = abs(view.plr.y - pair.second.appearanceData.iY);