From 21d280147cb56d8f6c01d732fde395066de42495 Mon Sep 17 00:00:00 2001 From: gsemaj Date: Mon, 18 Dec 2023 12:37:55 -0500 Subject: [PATCH] Shard crash fixes --- src/Entities.cpp | 5 +++-- src/Groups.cpp | 18 ++++++++++++++++++ src/Missions.cpp | 3 +++ src/Nanos.cpp | 2 +- 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/Entities.cpp b/src/Entities.cpp index bd97981..63a4a64 100644 --- a/src/Entities.cpp +++ b/src/Entities.cpp @@ -16,8 +16,9 @@ EntityRef::EntityRef(CNSocket *s) { EntityRef::EntityRef(int32_t i) { id = i; - assert(NPCManager::NPCs.find(id) != NPCManager::NPCs.end()); - kind = NPCManager::NPCs[id]->kind; + kind = EntityKind::INVALID; + if (NPCManager::NPCs.find(id) != NPCManager::NPCs.end()) + kind = NPCManager::NPCs[id]->kind; } bool EntityRef::isValid() const { diff --git a/src/Groups.cpp b/src/Groups.cpp index ac58d6e..8d948b8 100644 --- a/src/Groups.cpp +++ b/src/Groups.cpp @@ -64,6 +64,9 @@ static void attachGroupData(std::vector& pcs, std::vector& } void Groups::addToGroup(Group* group, EntityRef member) { + if (group == nullptr) + return; + if (member.kind == EntityKind::PLAYER) { Player* plr = PlayerManager::getPlayer(member.sock); plr->group = group; @@ -109,6 +112,9 @@ void Groups::addToGroup(Group* group, EntityRef member) { } bool Groups::removeFromGroup(Group* group, EntityRef member) { + if (group == nullptr) + return false; + if (member.kind == EntityKind::PLAYER) { Player* plr = PlayerManager::getPlayer(member.sock); plr->group = nullptr; // no dangling pointers here muahaahahah @@ -168,6 +174,9 @@ bool Groups::removeFromGroup(Group* group, EntityRef member) { } void Groups::disbandGroup(Group* group) { + if (group == nullptr) + return; + // remove everyone from the group!! bool done = false; while(!done) { @@ -252,6 +261,9 @@ static void leaveGroup(CNSocket* sock, CNPacketData* data) { } void Groups::sendToGroup(Group* group, void* buf, uint32_t type, size_t size) { + if (group == nullptr) + return; + auto players = group->filter(EntityKind::PLAYER); for (EntityRef ref : players) { ref.sock->sendPacket(buf, type, size); @@ -259,6 +271,9 @@ void Groups::sendToGroup(Group* group, void* buf, uint32_t type, size_t size) { } void Groups::sendToGroup(Group* group, EntityRef excluded, void* buf, uint32_t type, size_t size) { + if (group == nullptr) + return; + auto players = group->filter(EntityKind::PLAYER); for (EntityRef ref : players) { if(ref != excluded) ref.sock->sendPacket(buf, type, size); @@ -294,6 +309,9 @@ void Groups::groupTickInfo(CNSocket* sock) { void Groups::groupKick(Group* group, EntityRef ref) { + if (group == nullptr) + return; + // if you are the group leader, destroy your own group and kick everybody if (group->members[0] == ref) { disbandGroup(group); diff --git a/src/Missions.cpp b/src/Missions.cpp index b13699a..7bbdba5 100644 --- a/src/Missions.cpp +++ b/src/Missions.cpp @@ -386,6 +386,9 @@ static void taskStart(CNSocket* sock, CNPacketData* data) { static void taskEnd(CNSocket* sock, CNPacketData* data) { sP_CL2FE_REQ_PC_TASK_END* missionData = (sP_CL2FE_REQ_PC_TASK_END*)data->buf; + if (Missions::Tasks.find(missionData->iTaskNum) == Missions::Tasks.end()) + return; + TaskData* task = Missions::Tasks[missionData->iTaskNum]; // handle timed mission failure diff --git a/src/Nanos.cpp b/src/Nanos.cpp index 8fa9629..73082be 100644 --- a/src/Nanos.cpp +++ b/src/Nanos.cpp @@ -110,7 +110,7 @@ void Nanos::summonNano(CNSocket *sock, int slot, bool silent) { } static void setNanoSkill(CNSocket* sock, sP_CL2FE_REQ_NANO_TUNE* skill) { - if (skill->iNanoID >= NANO_COUNT) + if (skill == nullptr || skill->iNanoID >= NANO_COUNT || skill->iNanoID < 0) return; Player *plr = PlayerManager::getPlayer(sock);