[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

@ -47,12 +47,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{};
std::stringstream ss;
{ try {
ss << loot_data; std::stringstream ss;
cereal::JSONInputArchive ar(ss); {
l.serialize(ar); ss << loot_data;
cereal::JSONInputArchive ar(ss);
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,7 +81,8 @@ 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;
break; break;
@ -106,13 +113,20 @@ inline std::string GetLootSerialized(NPC *npc)
); );
} }
std::stringstream ss; try {
{ std::stringstream ss;
cereal::JSONOutputArchiveSingleLine ar(ss); {
ls.serialize(ar); cereal::JSONOutputArchiveSingleLine ar(ss);
ls.serialize(ar);
}
return ss.str();
} catch (const std::exception &e) {
LogZoneState("Failed to serialize loot data for NPC [{}] [{}]", npc->GetNPCTypeID(), e.what());
return "";
} }
return ss.str(); return "";
} }
inline std::string GetLootSerialized(Corpse *c) inline std::string GetLootSerialized(Corpse *c)
@ -134,13 +148,20 @@ inline std::string GetLootSerialized(Corpse *c)
); );
} }
std::stringstream ss; try {
{ std::stringstream ss;
cereal::JSONOutputArchiveSingleLine ar(ss); {
ls.serialize(ar); cereal::JSONOutputArchiveSingleLine ar(ss);
ls.serialize(ar);
}
return ss.str();
} catch (const std::exception &e) {
LogZoneState("Failed to serialize loot data for Corpse [{}] [{}]", c->GetID(), e.what());
return "";
} }
return ss.str(); 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);
} }
std::ostringstream os; try {
{ std::ostringstream os;
cereal::JSONOutputArchiveSingleLine archive(os); {
archive(variables); cereal::JSONOutputArchiveSingleLine archive(os);
archive(variables);
}
s.entity_variables = os.str();
}
catch (const std::exception &e) {
LogZoneState("Failed to serialize entity variables for NPC [{}] [{}]", n->GetNPCTypeID(), e.what());
return;
} }
s.entity_variables = os.str();
// 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);