Browse Source

Fix serialization to convert NationID enum to/from strings

Co-authored-by: djeada <[email protected]>
copilot-swe-agent[bot] 1 month ago
parent
commit
a273093cda
1 changed files with 11 additions and 2 deletions
  1. 11 2
      game/core/serialization.cpp

+ 11 - 2
game/core/serialization.cpp

@@ -1,6 +1,7 @@
 #include "serialization.h"
 #include "../map/terrain.h"
 #include "../map/terrain_service.h"
+#include "../systems/nation_id.h"
 #include "../units/spawn_type.h"
 #include "../units/troop_type.h"
 #include "component.h"
@@ -118,7 +119,7 @@ auto Serialization::serializeEntity(const Entity *entity) -> QJsonObject {
     unit_obj["unit_type"] = QString::fromStdString(
         Game::Units::spawn_typeToString(unit->spawn_type));
     unit_obj["owner_id"] = unit->owner_id;
-    unit_obj["nation_id"] = QString::fromStdString(unit->nation_id);
+    unit_obj["nation_id"] = Game::Systems::nationIDToQString(unit->nation_id);
     entity_obj["unit"] = unit_obj;
   }
 
@@ -305,7 +306,15 @@ void Serialization::deserializeEntity(Entity *entity, const QJsonObject &json) {
 
     unit->owner_id = unit_obj["owner_id"].toInt(0);
     if (unit_obj.contains("nation_id")) {
-      unit->nation_id = unit_obj["nation_id"].toString().toStdString();
+      const QString nation_str = unit_obj["nation_id"].toString();
+      Game::Systems::NationID nation_id;
+      if (Game::Systems::tryParseNationID(nation_str, nation_id)) {
+        unit->nation_id = nation_id;
+      } else {
+        qWarning() << "Unknown nation ID in save file:" << nation_str
+                   << "- using default";
+        unit->nation_id = Game::Systems::NationID::KingdomOfIron;
+      }
     }
   }