[Zone State] Wrap all serialization/deserialization in try/catch (#4726)

This commit is contained in:
Chris Miles 2025-03-01 16:46:08 -06:00 committed by GitHub
parent 3611b49f68
commit eb7118754b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -48,12 +48,18 @@ struct LootStateData {
inline void LoadLootStateData(Zone *zone, NPC *npc, const std::string &loot_data) inline void LoadLootStateData(Zone *zone, NPC *npc, const std::string &loot_data)
{ {
LootStateData l{}; LootStateData l{};
try {
std::stringstream ss; std::stringstream ss;
{ {
ss << loot_data; ss << loot_data;
cereal::JSONInputArchive ar(ss); cereal::JSONInputArchive ar(ss);
l.serialize(ar); l.serialize(ar);
} }
} catch (const std::exception &e) {
LogZoneState("Failed to load loot state data for NPC [{}] [{}]", npc->GetNPCTypeID(), e.what());
return;
}
npc->AddLootCash(l.copper, l.silver, l.gold, l.platinum); npc->AddLootCash(l.copper, l.silver, l.gold, l.platinum);
@ -75,6 +81,7 @@ inline void LoadLootStateData(Zone *zone, NPC *npc, const std::string &loot_data
} }
LootdropEntriesRepository::LootdropEntries lootdrop_entry; LootdropEntriesRepository::LootdropEntries lootdrop_entry;
for (auto &le: entries) { for (auto &le: entries) {
if (e.item_id == le.item_id) { if (e.item_id == le.item_id) {
lootdrop_entry = le; lootdrop_entry = le;
@ -106,6 +113,7 @@ inline std::string GetLootSerialized(NPC *npc)
); );
} }
try {
std::stringstream ss; std::stringstream ss;
{ {
cereal::JSONOutputArchiveSingleLine ar(ss); cereal::JSONOutputArchiveSingleLine ar(ss);
@ -113,6 +121,12 @@ inline std::string GetLootSerialized(NPC *npc)
} }
return ss.str(); return ss.str();
} catch (const std::exception &e) {
LogZoneState("Failed to serialize loot data for NPC [{}] [{}]", npc->GetNPCTypeID(), e.what());
return "";
}
return "";
} }
inline std::string GetLootSerialized(Corpse *c) inline std::string GetLootSerialized(Corpse *c)
@ -134,6 +148,7 @@ inline std::string GetLootSerialized(Corpse *c)
); );
} }
try {
std::stringstream ss; std::stringstream ss;
{ {
cereal::JSONOutputArchiveSingleLine ar(ss); cereal::JSONOutputArchiveSingleLine ar(ss);
@ -141,6 +156,12 @@ inline std::string GetLootSerialized(Corpse *c)
} }
return ss.str(); return ss.str();
} catch (const std::exception &e) {
LogZoneState("Failed to serialize loot data for Corpse [{}] [{}]", c->GetID(), e.what());
return "";
}
return "";
} }
inline void LoadNPCEntityVariables(NPC *n, const std::string &entity_variables) inline void LoadNPCEntityVariables(NPC *n, const std::string &entity_variables)
@ -383,13 +404,18 @@ inline void SaveNPCState(NPC *n, ZoneStateSpawnsRepository::ZoneStateSpawns &s)
variables[k] = n->GetEntityVariable(k); variables[k] = n->GetEntityVariable(k);
} }
try {
std::ostringstream os; std::ostringstream os;
{ {
cereal::JSONOutputArchiveSingleLine archive(os); cereal::JSONOutputArchiveSingleLine archive(os);
archive(variables); archive(variables);
} }
s.entity_variables = os.str(); s.entity_variables = os.str();
}
catch (const std::exception &e) {
LogZoneState("Failed to serialize entity variables for NPC [{}] [{}]", n->GetNPCTypeID(), e.what());
return;
}
// buffs // buffs
auto buffs = n->GetBuffs(); auto buffs = n->GetBuffs();
@ -406,19 +432,18 @@ inline void SaveNPCState(NPC *n, ZoneStateSpawnsRepository::ZoneStateSpawns &s)
} }
try { try {
os = std::ostringstream(); std::ostringstream os = std::ostringstream();
{ {
cereal::JSONOutputArchiveSingleLine archive(os); cereal::JSONOutputArchiveSingleLine archive(os);
archive(cereal::make_nvp("buffs", valid_buffs)); archive(cereal::make_nvp("buffs", valid_buffs));
} }
s.buffs = os.str();
} }
catch (const std::exception &e) { catch (const std::exception &e) {
LogZoneState("Failed to serialize buffs for NPC [{}] [{}]", n->GetNPCTypeID(), e.what()); LogZoneState("Failed to serialize buffs for NPC [{}] [{}]", n->GetNPCTypeID(), e.what());
return; return;
} }
s.buffs = os.str();
// rest // rest
s.npc_id = n->GetNPCTypeID(); s.npc_id = n->GetNPCTypeID();
s.loot_data = GetLootSerialized(n); s.loot_data = GetLootSerialized(n);