MapFormatJson.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335
  1. /*
  2. * MapFormatJson.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "MapFormatJson.h"
  12. #include "../filesystem/CInputStream.h"
  13. #include "../filesystem/COutputStream.h"
  14. #include "../json/JsonWriter.h"
  15. #include "CMap.h"
  16. #include "MapFormat.h"
  17. #include "../GameLibrary.h"
  18. #include "../RiverHandler.h"
  19. #include "../RoadHandler.h"
  20. #include "../TerrainHandler.h"
  21. #include "../entities/artifact/CArtHandler.h"
  22. #include "../entities/faction/CTownHandler.h"
  23. #include "../entities/hero/CHeroHandler.h"
  24. #include "../mapObjectConstructors/AObjectTypeHandler.h"
  25. #include "../mapObjectConstructors/CObjectClassesHandler.h"
  26. #include "../mapObjects/ObjectTemplate.h"
  27. #include "../mapObjects/CGHeroInstance.h"
  28. #include "../mapObjects/CGTownInstance.h"
  29. #include "../mapObjects/MiscObjects.h"
  30. #include "../modding/ModScope.h"
  31. #include "../modding/ModUtility.h"
  32. #include "../spells/CSpellHandler.h"
  33. #include "../CSkillHandler.h"
  34. #include "../constants/StringConstants.h"
  35. #include "../serializer/JsonDeserializer.h"
  36. #include "../serializer/JsonSerializer.h"
  37. #include "../texts/Languages.h"
  38. VCMI_LIB_NAMESPACE_BEGIN
  39. class MapObjectResolver: public IInstanceResolver
  40. {
  41. public:
  42. MapObjectResolver(const CMapFormatJson * owner_);
  43. si32 decode (const std::string & identifier) const override;
  44. std::string encode(si32 identifier) const override;
  45. private:
  46. const CMapFormatJson * owner;
  47. };
  48. MapObjectResolver::MapObjectResolver(const CMapFormatJson * owner_):
  49. owner(owner_)
  50. {
  51. }
  52. si32 MapObjectResolver::decode(const std::string & identifier) const
  53. {
  54. //always decode as ObjectInstanceID
  55. auto it = owner->map->instanceNames.find(identifier);
  56. if(it != owner->map->instanceNames.end())
  57. {
  58. return (*it).second->id.getNum();
  59. }
  60. else
  61. {
  62. logGlobal->error("Object not found: %s", identifier);
  63. return -1;
  64. }
  65. }
  66. std::string MapObjectResolver::encode(si32 identifier) const
  67. {
  68. ObjectInstanceID id(identifier);
  69. auto object = owner->map->getObject(id);
  70. if(!object)
  71. {
  72. logGlobal->error("Cannot get object with id %d", id.getNum());
  73. return "";
  74. }
  75. return object->instanceName;
  76. }
  77. namespace HeaderDetail
  78. {
  79. static const std::vector<std::string> difficultyMap =
  80. {
  81. "EASY",
  82. "NORMAL",
  83. "HARD",
  84. "EXPERT",
  85. "IMPOSSIBLE"
  86. };
  87. enum class ECanPlay
  88. {
  89. NONE = 0,
  90. PLAYER_OR_AI = 1,
  91. PLAYER_ONLY = 2,
  92. AI_ONLY = 3
  93. };
  94. static const std::vector<std::string> canPlayMap =
  95. {
  96. "",
  97. "PlayerOrAI",
  98. "PlayerOnly",
  99. "AIOnly"
  100. };
  101. }
  102. namespace TriggeredEventsDetail
  103. {
  104. static const std::array conditionNames =
  105. {
  106. "haveArtifact", "haveCreatures", "haveResources", "haveBuilding",
  107. "control", "destroy", "transport", "daysPassed",
  108. "isHuman", "daysWithoutTown", "standardWin", "constValue"
  109. };
  110. static const std::array typeNames = { "victory", "defeat" };
  111. static EventCondition JsonToCondition(const JsonNode & node)
  112. {
  113. EventCondition event;
  114. const auto & conditionName = node.Vector()[0].String();
  115. auto pos = vstd::find_pos(conditionNames, conditionName);
  116. event.condition = static_cast<EventCondition::EWinLoseType>(pos);
  117. if (node.Vector().size() > 1)
  118. {
  119. const JsonNode & data = node.Vector()[1];
  120. event.objectInstanceName = data["object"].String();
  121. event.value = data["value"].Integer();
  122. switch (event.condition)
  123. {
  124. case EventCondition::HAVE_ARTIFACT:
  125. case EventCondition::TRANSPORT:
  126. if (data["type"].isNumber()) // compatibility
  127. event.objectType = ArtifactID(data["type"].Integer());
  128. else
  129. event.objectType = ArtifactID(ArtifactID::decode(data["type"].String()));
  130. break;
  131. case EventCondition::HAVE_CREATURES:
  132. if (data["type"].isNumber()) // compatibility
  133. event.objectType = CreatureID(data["type"].Integer());
  134. else
  135. event.objectType = CreatureID(CreatureID::decode(data["type"].String()));
  136. break;
  137. case EventCondition::HAVE_RESOURCES:
  138. if (data["type"].isNumber()) // compatibility
  139. event.objectType = GameResID(data["type"].Integer());
  140. else
  141. event.objectType = GameResID(GameResID::decode(data["type"].String()));
  142. break;
  143. case EventCondition::HAVE_BUILDING:
  144. if (data["type"].isNumber()) // compatibility
  145. event.objectType = BuildingID(data["type"].Integer());
  146. else
  147. event.objectType = BuildingID(BuildingID::decode(data["type"].String()));
  148. break;
  149. case EventCondition::CONTROL:
  150. case EventCondition::DESTROY:
  151. if (data["type"].isNumber()) // compatibility
  152. event.objectType = MapObjectID(data["type"].Integer());
  153. else
  154. event.objectType = MapObjectID(MapObjectID::decode(data["type"].String()));
  155. break;
  156. }
  157. if (!data["position"].isNull())
  158. {
  159. const auto & position = data["position"].Vector();
  160. event.position.x = static_cast<si32>(position.at(0).Float());
  161. event.position.y = static_cast<si32>(position.at(1).Float());
  162. event.position.z = static_cast<si32>(position.at(2).Float());
  163. }
  164. }
  165. return event;
  166. }
  167. static JsonNode ConditionToJson(const EventCondition & event)
  168. {
  169. JsonNode json;
  170. JsonVector & asVector = json.Vector();
  171. JsonNode condition;
  172. condition.String() = conditionNames.at(event.condition);
  173. asVector.push_back(condition);
  174. JsonNode data;
  175. if(!event.objectInstanceName.empty())
  176. data["object"].String() = event.objectInstanceName;
  177. data["type"].String() = event.objectType.toString();
  178. data["value"].Integer() = event.value;
  179. if(event.position != int3(-1, -1, -1))
  180. {
  181. auto & position = data["position"].Vector();
  182. position.resize(3);
  183. position[0].Float() = event.position.x;
  184. position[1].Float() = event.position.y;
  185. position[2].Float() = event.position.z;
  186. }
  187. if(!data.isNull())
  188. asVector.push_back(data);
  189. return json;
  190. }
  191. }//namespace TriggeredEventsDetail
  192. namespace TerrainDetail
  193. {
  194. static const std::array<char, 4> flipCodes =
  195. {
  196. '_', '-', '|', '+'
  197. };
  198. }
  199. ///CMapFormatJson
  200. const int CMapFormatJson::VERSION_MAJOR = 2;
  201. const int CMapFormatJson::VERSION_MINOR = 0;
  202. const std::string CMapFormatJson::HEADER_FILE_NAME = "header.json";
  203. const std::string CMapFormatJson::OBJECTS_FILE_NAME = "objects.json";
  204. const std::string CMapFormatJson::TERRAIN_FILE_NAMES[2] = {"surface_terrain.json", "underground_terrain.json"};
  205. CMapFormatJson::CMapFormatJson():
  206. fileVersionMajor(0), fileVersionMinor(0),
  207. mapObjectResolver(std::make_unique<MapObjectResolver>(this)),
  208. map(nullptr), mapHeader(nullptr)
  209. {
  210. }
  211. TerrainId CMapFormatJson::getTerrainByCode(const std::string & code)
  212. {
  213. for(const auto & object : LIBRARY->terrainTypeHandler->objects)
  214. {
  215. if(object->shortIdentifier == code)
  216. return object->getId();
  217. }
  218. return TerrainId::NONE;
  219. }
  220. RiverId CMapFormatJson::getRiverByCode(const std::string & code)
  221. {
  222. for(const auto & object : LIBRARY->riverTypeHandler->objects)
  223. {
  224. if (object->shortIdentifier == code)
  225. return object->getId();
  226. }
  227. return RiverId::NO_RIVER;
  228. }
  229. RoadId CMapFormatJson::getRoadByCode(const std::string & code)
  230. {
  231. for(const auto & object : LIBRARY->roadTypeHandler->objects)
  232. {
  233. if (object->shortIdentifier == code)
  234. return object->getId();
  235. }
  236. return RoadId::NO_ROAD;
  237. }
  238. void CMapFormatJson::serializeAllowedFactions(JsonSerializeFormat & handler, std::set<FactionID> & value) const
  239. {
  240. std::set<FactionID> temp;
  241. if(handler.saving)
  242. {
  243. for(auto const factionID : LIBRARY->townh->getDefaultAllowed())
  244. if(vstd::contains(value, factionID))
  245. temp.insert(factionID);
  246. }
  247. handler.serializeLIC("allowedFactions", &FactionID::decode, &FactionID::encode, LIBRARY->townh->getDefaultAllowed(), temp);
  248. if(!handler.saving)
  249. value = temp;
  250. }
  251. void CMapFormatJson::serializeHeader(JsonSerializeFormat & handler)
  252. {
  253. handler.serializeStruct("name", mapHeader->name);
  254. handler.serializeStruct("description", mapHeader->description);
  255. handler.serializeStruct("author", mapHeader->author);
  256. handler.serializeStruct("authorContact", mapHeader->authorContact);
  257. handler.serializeStruct("mapVersion", mapHeader->mapVersion);
  258. handler.serializeInt("creationDateTime", mapHeader->creationDateTime, 0);
  259. handler.serializeInt("heroLevelLimit", mapHeader->levelLimit, 0);
  260. //todo: support arbitrary percentage
  261. handler.serializeEnum("difficulty", mapHeader->difficulty, HeaderDetail::difficultyMap);
  262. serializePlayerInfo(handler);
  263. handler.serializeLIC("allowedHeroes", &HeroTypeID::decode, &HeroTypeID::encode, LIBRARY->heroh->getDefaultAllowed(), mapHeader->allowedHeroes);
  264. handler.serializeStruct("victoryMessage", mapHeader->victoryMessage);
  265. handler.serializeInt("victoryIconIndex", mapHeader->victoryIconIndex);
  266. handler.serializeStruct("defeatMessage", mapHeader->defeatMessage);
  267. handler.serializeInt("defeatIconIndex", mapHeader->defeatIconIndex);
  268. handler.serializeIdArray("reservedCampaignHeroes", mapHeader->reservedCampaignHeroes);
  269. }
  270. void CMapFormatJson::serializePlayerInfo(JsonSerializeFormat & handler)
  271. {
  272. auto playersData = handler.enterStruct("players");
  273. for(int player = 0; player < PlayerColor::PLAYER_LIMIT_I; player++)
  274. {
  275. PlayerInfo & info = mapHeader->players[player];
  276. if(handler.saving)
  277. {
  278. if(!info.canAnyonePlay())
  279. continue;
  280. }
  281. auto playerData = handler.enterStruct(GameConstants::PLAYER_COLOR_NAMES[player]);
  282. if(!handler.saving)
  283. {
  284. if(handler.getCurrent().isNull())
  285. {
  286. info.canComputerPlay = false;
  287. info.canHumanPlay = false;
  288. continue;
  289. }
  290. }
  291. serializeAllowedFactions(handler, info.allowedFactions);
  292. HeaderDetail::ECanPlay canPlay = HeaderDetail::ECanPlay::NONE;
  293. if(handler.saving)
  294. {
  295. if(info.canComputerPlay)
  296. {
  297. canPlay = info.canHumanPlay ? HeaderDetail::ECanPlay::PLAYER_OR_AI : HeaderDetail::ECanPlay::AI_ONLY;
  298. }
  299. else
  300. {
  301. canPlay = info.canHumanPlay ? HeaderDetail::ECanPlay::PLAYER_ONLY : HeaderDetail::ECanPlay::NONE;
  302. }
  303. }
  304. handler.serializeEnum("canPlay", canPlay, HeaderDetail::canPlayMap);
  305. if(!handler.saving)
  306. {
  307. switch(canPlay)
  308. {
  309. case HeaderDetail::ECanPlay::PLAYER_OR_AI:
  310. info.canComputerPlay = true;
  311. info.canHumanPlay = true;
  312. break;
  313. case HeaderDetail::ECanPlay::PLAYER_ONLY:
  314. info.canComputerPlay = false;
  315. info.canHumanPlay = true;
  316. break;
  317. case HeaderDetail::ECanPlay::AI_ONLY:
  318. info.canComputerPlay = true;
  319. info.canHumanPlay = false;
  320. break;
  321. default:
  322. info.canComputerPlay = false;
  323. info.canHumanPlay = false;
  324. break;
  325. }
  326. }
  327. //saving whole structure only if position is valid
  328. if(!handler.saving || info.posOfMainTown.isValid())
  329. {
  330. auto mainTown = handler.enterStruct("mainTown");
  331. handler.serializeBool("generateHero", info.generateHeroAtMainTown);
  332. handler.serializeInt("x", info.posOfMainTown.x, -1);
  333. handler.serializeInt("y", info.posOfMainTown.y, -1);
  334. handler.serializeInt("l", info.posOfMainTown.z, -1);
  335. }
  336. if(!handler.saving)
  337. {
  338. info.hasMainTown = info.posOfMainTown.isValid();
  339. }
  340. handler.serializeString("mainHero", info.mainHeroInstance);//must be before "heroes"
  341. //heroes
  342. if(handler.saving)
  343. {
  344. //ignoring heroesNames and saving from actual map objects
  345. //TODO: optimize
  346. for(auto & hero : map->getObjects<CGHeroInstance>())
  347. {
  348. if((hero->getOwner()) == PlayerColor(player))
  349. {
  350. auto heroes = handler.enterStruct("heroes");
  351. if(hero)
  352. {
  353. auto heroData = handler.enterStruct(hero->instanceName);
  354. heroData->serializeString("name", hero->nameCustomTextId);
  355. if(hero->ID == Obj::HERO)
  356. {
  357. std::string temp;
  358. if(hero->getHeroTypeID().hasValue())
  359. temp = hero->getHeroType()->getJsonKey();
  360. handler.serializeString("type", temp);
  361. }
  362. }
  363. }
  364. }
  365. }
  366. else
  367. {
  368. info.heroesNames.clear();
  369. auto heroes = handler.enterStruct("heroes");
  370. for(const auto & hero : handler.getCurrent().Struct())
  371. {
  372. const JsonNode & data = hero.second;
  373. const std::string instanceName = hero.first;
  374. SHeroName hname;
  375. hname.heroId = HeroTypeID::NONE;
  376. std::string rawId = data["type"].String();
  377. if(!rawId.empty())
  378. hname.heroId = HeroTypeID(HeroTypeID::decode(rawId));
  379. hname.heroName = data["name"].String();
  380. if(instanceName == info.mainHeroInstance)
  381. {
  382. //this is main hero
  383. info.mainCustomHeroNameTextId = hname.heroName;
  384. info.hasRandomHero = (hname.heroId == HeroTypeID::NONE);
  385. info.mainCustomHeroId = hname.heroId;
  386. info.mainCustomHeroPortrait = HeroTypeID::NONE;
  387. //todo:mainHeroPortrait
  388. }
  389. info.heroesNames.push_back(hname);
  390. }
  391. }
  392. handler.serializeBool("randomFaction", info.isFactionRandom);
  393. }
  394. }
  395. void CMapFormatJson::readTeams(JsonDeserializer & handler)
  396. {
  397. auto teams = handler.enterArray("teams");
  398. const JsonNode & src = teams->getCurrent();
  399. if(src.getType() != JsonNode::JsonType::DATA_VECTOR)
  400. {
  401. // No alliances
  402. if(src.getType() != JsonNode::JsonType::DATA_NULL)
  403. logGlobal->error("Invalid teams field type");
  404. mapHeader->howManyTeams = 0;
  405. for(auto & player : mapHeader->players)
  406. if(player.canAnyonePlay())
  407. player.team = TeamID(mapHeader->howManyTeams++);
  408. }
  409. else
  410. {
  411. const JsonVector & srcVector = src.Vector();
  412. mapHeader->howManyTeams = static_cast<ui8>(srcVector.size());
  413. for(int team = 0; team < mapHeader->howManyTeams; team++)
  414. for(const JsonNode & playerData : srcVector[team].Vector())
  415. {
  416. PlayerColor player = PlayerColor(vstd::find_pos(GameConstants::PLAYER_COLOR_NAMES, playerData.String()));
  417. if(player.isValidPlayer())
  418. if(mapHeader->players[player.getNum()].canAnyonePlay())
  419. mapHeader->players[player.getNum()].team = TeamID(team);
  420. }
  421. for(PlayerInfo & player : mapHeader->players)
  422. if(player.canAnyonePlay() && player.team == TeamID::NO_TEAM)
  423. player.team = TeamID(mapHeader->howManyTeams++);
  424. }
  425. }
  426. void CMapFormatJson::writeTeams(JsonSerializer & handler)
  427. {
  428. std::vector<std::set<PlayerColor>> teamsData;
  429. teamsData.resize(mapHeader->howManyTeams);
  430. //get raw data
  431. for(int idx = 0; idx < mapHeader->players.size(); idx++)
  432. {
  433. const PlayerInfo & player = mapHeader->players.at(idx);
  434. int team = player.team.getNum();
  435. if(vstd::iswithin(team, 0, mapHeader->howManyTeams-1) && player.canAnyonePlay())
  436. teamsData.at(team).insert(PlayerColor(idx));
  437. }
  438. //remove single-member teams
  439. vstd::erase_if(teamsData, [](std::set<PlayerColor> & elem) -> bool
  440. {
  441. return elem.size() <= 1;
  442. });
  443. if(!teamsData.empty())
  444. {
  445. JsonNode dest;
  446. //construct output
  447. dest.setType(JsonNode::JsonType::DATA_VECTOR);
  448. for(const std::set<PlayerColor> & teamData : teamsData)
  449. {
  450. JsonNode team;
  451. for(const PlayerColor & player : teamData)
  452. team.Vector().emplace_back(GameConstants::PLAYER_COLOR_NAMES[player.getNum()]);
  453. dest.Vector().push_back(std::move(team));
  454. }
  455. handler.serializeRaw("teams", dest, std::nullopt);
  456. }
  457. }
  458. void CMapFormatJson::readTriggeredEvents(JsonDeserializer & handler)
  459. {
  460. const JsonNode & input = handler.getCurrent();
  461. mapHeader->triggeredEvents.clear();
  462. for(const auto & entry : input["triggeredEvents"].Struct())
  463. {
  464. TriggeredEvent event;
  465. event.identifier = entry.first;
  466. readTriggeredEvent(event, entry.second);
  467. mapHeader->triggeredEvents.push_back(event);
  468. }
  469. }
  470. void CMapFormatJson::readTriggeredEvent(TriggeredEvent & event, const JsonNode & source) const
  471. {
  472. using namespace TriggeredEventsDetail;
  473. event.onFulfill.jsonDeserialize(source["message"]);
  474. event.description.jsonDeserialize(source["description"]);
  475. event.effect.type = vstd::find_pos(typeNames, source["effect"]["type"].String());
  476. event.effect.toOtherMessage.jsonDeserialize(source["effect"]["messageToSend"]);
  477. event.trigger = EventExpression(source["condition"], JsonToCondition); // logical expression
  478. }
  479. void CMapFormatJson::writeTriggeredEvents(JsonSerializer & handler)
  480. {
  481. JsonNode triggeredEvents;
  482. for(const auto & event : mapHeader->triggeredEvents)
  483. writeTriggeredEvent(event, triggeredEvents[event.identifier]);
  484. handler.serializeRaw("triggeredEvents", triggeredEvents, std::nullopt);
  485. }
  486. void CMapFormatJson::writeTriggeredEvent(const TriggeredEvent & event, JsonNode & dest) const
  487. {
  488. using namespace TriggeredEventsDetail;
  489. if(!event.onFulfill.empty())
  490. event.onFulfill.jsonSerialize(dest["message"]);
  491. if(!event.description.empty())
  492. event.description.jsonSerialize(dest["description"]);
  493. dest["effect"]["type"].String() = typeNames.at(static_cast<size_t>(event.effect.type));
  494. if(!event.effect.toOtherMessage.empty())
  495. event.description.jsonSerialize(dest["effect"]["messageToSend"]);
  496. dest["condition"] = event.trigger.toJson(ConditionToJson);
  497. }
  498. void CMapFormatJson::readDisposedHeroes(JsonSerializeFormat & handler)
  499. {
  500. auto definitions = handler.enterStruct("predefinedHeroes");//DisposedHeroes are part of predefinedHeroes in VCMI map format
  501. const JsonNode & data = handler.getCurrent();
  502. for(const auto & entry : data.Struct())
  503. {
  504. HeroTypeID type(HeroTypeID::decode(entry.first));
  505. std::set<PlayerColor> mask;
  506. for(const JsonNode & playerData : entry.second["availableFor"].Vector())
  507. {
  508. PlayerColor player = PlayerColor(vstd::find_pos(GameConstants::PLAYER_COLOR_NAMES, playerData.String()));
  509. if(player.isValidPlayer())
  510. mask.insert(player);
  511. }
  512. if(!mask.empty() && mask.size() != PlayerColor::PLAYER_LIMIT_I && type.getNum() >= 0)
  513. {
  514. DisposedHero hero;
  515. hero.heroId = type;
  516. hero.players = mask;
  517. //name and portrait are not used
  518. mapHeader->disposedHeroes.push_back(hero);
  519. }
  520. }
  521. }
  522. void CMapFormatJson::writeDisposedHeroes(JsonSerializeFormat & handler)
  523. {
  524. if(mapHeader->disposedHeroes.empty())
  525. return;
  526. auto definitions = handler.enterStruct("predefinedHeroes");//DisposedHeroes are part of predefinedHeroes in VCMI map format
  527. for(DisposedHero & hero : mapHeader->disposedHeroes)
  528. {
  529. std::string type = HeroTypeID::encode(hero.heroId.getNum());
  530. auto definition = definitions->enterStruct(type);
  531. JsonNode players;
  532. definition->serializeIdArray("availableFor", hero.players);
  533. }
  534. }
  535. void CMapFormatJson::serializeRumors(JsonSerializeFormat & handler)
  536. {
  537. auto rumors = handler.enterArray("rumors");
  538. rumors.serializeStruct(map->rumors);
  539. }
  540. void CMapFormatJson::serializeTimedEvents(JsonSerializeFormat & handler)
  541. {
  542. auto events = handler.enterArray("events");
  543. std::vector<CMapEvent> temp(map->events.begin(), map->events.end());
  544. events.serializeStruct(temp);
  545. map->events.assign(temp.begin(), temp.end());
  546. }
  547. void CMapFormatJson::serializePredefinedHeroes(JsonSerializeFormat & handler)
  548. {
  549. if(handler.saving)
  550. {
  551. auto heroPool = map->getHeroesInPool();
  552. if(!heroPool.empty())
  553. {
  554. auto predefinedHeroes = handler.enterStruct("predefinedHeroes");
  555. for(auto & heroID : heroPool)
  556. {
  557. auto heroPtr = map->tryGetFromHeroPool(heroID);
  558. auto predefinedHero = handler.enterStruct(heroPtr->getHeroTypeName());
  559. heroPtr->serializeJsonDefinition(handler);
  560. }
  561. }
  562. }
  563. else
  564. {
  565. auto predefinedHeroes = handler.enterStruct("predefinedHeroes");
  566. const JsonNode & data = handler.getCurrent();
  567. for(const auto & p : data.Struct())
  568. {
  569. auto predefinedHero = handler.enterStruct(p.first);
  570. auto hero = std::make_shared<CGHeroInstance>(map->cb);
  571. hero->ID = Obj::HERO;
  572. hero->setHeroTypeName(p.first);
  573. hero->serializeJsonDefinition(handler);
  574. map->addToHeroPool(hero);
  575. }
  576. }
  577. }
  578. void CMapFormatJson::serializeOptions(JsonSerializeFormat & handler)
  579. {
  580. serializeRumors(handler);
  581. serializeTimedEvents(handler);
  582. serializePredefinedHeroes(handler);
  583. handler.serializeLIC("allowedAbilities", &SecondarySkill::decode, &SecondarySkill::encode, LIBRARY->skillh->getDefaultAllowed(), map->allowedAbilities);
  584. handler.serializeLIC("allowedArtifacts", &ArtifactID::decode, &ArtifactID::encode, LIBRARY->arth->getDefaultAllowed(), map->allowedArtifact);
  585. handler.serializeLIC("allowedSpells", &SpellID::decode, &SpellID::encode, LIBRARY->spellh->getDefaultAllowed(), map->allowedSpells);
  586. //todo:events
  587. }
  588. void CMapFormatJson::readOptions(JsonDeserializer & handler)
  589. {
  590. readDisposedHeroes(handler);
  591. serializeOptions(handler);
  592. }
  593. void CMapFormatJson::writeOptions(JsonSerializer & handler)
  594. {
  595. writeDisposedHeroes(handler);
  596. serializeOptions(handler);
  597. }
  598. ///CMapPatcher
  599. CMapPatcher::CMapPatcher(const JsonNode & stream): input(stream)
  600. {
  601. //todo: update map patches and change this
  602. fileVersionMajor = 0;
  603. fileVersionMinor = 0;
  604. }
  605. void CMapPatcher::patchMapHeader(std::unique_ptr<CMapHeader> & header)
  606. {
  607. map = nullptr;
  608. mapHeader = header.get();
  609. if (!input.isNull())
  610. readPatchData();
  611. }
  612. void CMapPatcher::readPatchData()
  613. {
  614. JsonDeserializer handler(mapObjectResolver.get(), input);
  615. readTriggeredEvents(handler);
  616. handler.serializeInt("defeatIconIndex", mapHeader->defeatIconIndex);
  617. handler.serializeInt("victoryIconIndex", mapHeader->victoryIconIndex);
  618. handler.serializeStruct("victoryString", mapHeader->victoryMessage);
  619. handler.serializeStruct("defeatString", mapHeader->defeatMessage);
  620. }
  621. ///CMapLoaderJson
  622. CMapLoaderJson::CMapLoaderJson(CInputStream * stream)
  623. : buffer(stream)
  624. , ioApi(new CProxyROIOApi(buffer))
  625. , loader("", "_", ioApi)
  626. {
  627. }
  628. std::unique_ptr<CMap> CMapLoaderJson::loadMap(IGameCallback * cb)
  629. {
  630. LOG_TRACE(logGlobal);
  631. auto result = std::make_unique<CMap>(cb);
  632. map = result.get();
  633. mapHeader = map;
  634. readMap();
  635. return result;
  636. }
  637. std::unique_ptr<CMapHeader> CMapLoaderJson::loadMapHeader()
  638. {
  639. LOG_TRACE(logGlobal);
  640. map = nullptr;
  641. auto result = std::make_unique<CMapHeader>();
  642. mapHeader = result.get();
  643. readHeader(false);
  644. return result;
  645. }
  646. bool CMapLoaderJson::isExistArchive(const std::string & archiveFilename)
  647. {
  648. return loader.existsResource(JsonPath::builtin(archiveFilename));
  649. }
  650. JsonNode CMapLoaderJson::getFromArchive(const std::string & archiveFilename)
  651. {
  652. JsonPath resource = JsonPath::builtin(archiveFilename);
  653. if(!loader.existsResource(resource))
  654. throw std::runtime_error(archiveFilename+" not found");
  655. auto data = loader.load(resource)->readAll();
  656. JsonNode res(reinterpret_cast<const std::byte*>(data.first.get()), data.second, archiveFilename);
  657. return res;
  658. }
  659. void CMapLoaderJson::readMap()
  660. {
  661. LOG_TRACE(logGlobal);
  662. readHeader(true);
  663. map->initTerrain();
  664. readTerrain();
  665. readObjects();
  666. map->calculateGuardingGreaturePositions();
  667. }
  668. void CMapLoaderJson::readHeader(const bool complete)
  669. {
  670. //do not use map field here, use only mapHeader
  671. JsonNode header = getFromArchive(HEADER_FILE_NAME);
  672. fileVersionMajor = static_cast<int>(header["versionMajor"].Integer());
  673. if(fileVersionMajor > VERSION_MAJOR)
  674. {
  675. logGlobal->error("Unsupported map format version: %d", fileVersionMajor);
  676. throw std::runtime_error("Unsupported map format version");
  677. }
  678. fileVersionMinor = static_cast<int>(header["versionMinor"].Integer());
  679. if(fileVersionMinor > VERSION_MINOR)
  680. {
  681. logGlobal->warn("Too new map format revision: %d. This map should work but some of map features may be ignored.", fileVersionMinor);
  682. }
  683. JsonDeserializer handler(mapObjectResolver.get(), header);
  684. mapHeader->version = EMapFormat::VCMI;//todo: new version field
  685. //loading mods
  686. mapHeader->mods = ModVerificationInfo::jsonDeserializeList(header["mods"]);
  687. //todo: multilevel map load support
  688. {
  689. auto levels = handler.enterStruct("mapLevels");
  690. {
  691. auto surface = handler.enterStruct("surface");
  692. handler.serializeInt("height", mapHeader->height);
  693. handler.serializeInt("width", mapHeader->width);
  694. }
  695. {
  696. auto underground = handler.enterStruct("underground");
  697. mapHeader->twoLevel = !underground->getCurrent().isNull();
  698. }
  699. }
  700. serializeHeader(handler);
  701. readTriggeredEvents(handler);
  702. readTeams(handler);
  703. //TODO: check mods
  704. if(complete)
  705. readOptions(handler);
  706. readTranslations();
  707. }
  708. void CMapLoaderJson::readTerrainTile(const std::string & src, TerrainTile & tile)
  709. {
  710. try
  711. {
  712. using namespace TerrainDetail;
  713. {//terrain type
  714. const std::string typeCode = src.substr(0, 2);
  715. tile.terrainType = getTerrainByCode(typeCode);
  716. }
  717. int startPos = 2; //0+typeCode fixed length
  718. {//terrain view
  719. int pos = startPos;
  720. while (isdigit(src.at(pos)))
  721. pos++;
  722. int len = pos - startPos;
  723. if (len <= 0)
  724. throw std::runtime_error("Invalid terrain view in " + src);
  725. const std::string rawCode = src.substr(startPos, len);
  726. tile.terView = atoi(rawCode.c_str());
  727. startPos += len;
  728. }
  729. {//terrain flip
  730. int terrainFlip = vstd::find_pos(flipCodes, src.at(startPos++));
  731. if (terrainFlip < 0)
  732. throw std::runtime_error("Invalid terrain flip in " + src);
  733. else
  734. tile.extTileFlags = terrainFlip;
  735. }
  736. if (startPos >= src.size())
  737. return;
  738. bool hasRoad = true;
  739. {//road type
  740. const std::string typeCode = src.substr(startPos, 2);
  741. startPos += 2;
  742. tile.roadType = getRoadByCode(typeCode);
  743. if(!tile.roadType) //it's not a road, it's a river
  744. {
  745. tile.roadType = Road::NO_ROAD;
  746. tile.riverType = getRiverByCode(typeCode);
  747. hasRoad = false;
  748. if(!tile.riverType)
  749. {
  750. throw std::runtime_error("Invalid river type in " + src);
  751. }
  752. }
  753. }
  754. if (hasRoad)
  755. {//road dir
  756. int pos = startPos;
  757. while (isdigit(src.at(pos)))
  758. pos++;
  759. int len = pos - startPos;
  760. if (len <= 0)
  761. throw std::runtime_error("Invalid road dir in " + src);
  762. const std::string rawCode = src.substr(startPos, len);
  763. tile.roadDir = atoi(rawCode.c_str());
  764. startPos += len;
  765. }
  766. if (hasRoad)
  767. {//road flip
  768. int flip = vstd::find_pos(flipCodes, src.at(startPos++));
  769. if (flip < 0)
  770. throw std::runtime_error("Invalid road flip in " + src);
  771. else
  772. tile.extTileFlags |= (flip << 4);
  773. }
  774. if (startPos >= src.size())
  775. return;
  776. if (hasRoad)
  777. {//river type
  778. const std::string typeCode = src.substr(startPos, 2);
  779. startPos += 2;
  780. tile.riverType = getRiverByCode(typeCode);
  781. }
  782. {//river dir
  783. int pos = startPos;
  784. while (isdigit(src.at(pos)))
  785. pos++;
  786. int len = pos - startPos;
  787. if (len <= 0)
  788. throw std::runtime_error("Invalid river dir in " + src);
  789. const std::string rawCode = src.substr(startPos, len);
  790. tile.riverDir = atoi(rawCode.c_str());
  791. startPos += len;
  792. }
  793. {//river flip
  794. int flip = vstd::find_pos(flipCodes, src.at(startPos++));
  795. if (flip < 0)
  796. throw std::runtime_error("Invalid road flip in " + src);
  797. else
  798. tile.extTileFlags |= (flip << 2);
  799. }
  800. }
  801. catch (const std::exception &)
  802. {
  803. logGlobal->error("Failed to read terrain tile: %s");
  804. }
  805. }
  806. void CMapLoaderJson::readTerrainLevel(const JsonNode & src, const int index)
  807. {
  808. int3 pos(0, 0, index);
  809. const JsonVector & rows = src.Vector();
  810. if(rows.size() != map->height)
  811. throw std::runtime_error("Invalid terrain data");
  812. for(pos.y = 0; pos.y < map->height; pos.y++)
  813. {
  814. const JsonVector & tiles = rows[pos.y].Vector();
  815. if(tiles.size() != map->width)
  816. throw std::runtime_error("Invalid terrain data");
  817. for(pos.x = 0; pos.x < map->width; pos.x++)
  818. readTerrainTile(tiles[pos.x].String(), map->getTile(pos));
  819. }
  820. }
  821. void CMapLoaderJson::readTerrain()
  822. {
  823. {
  824. const JsonNode surface = getFromArchive(TERRAIN_FILE_NAMES[0]);
  825. readTerrainLevel(surface, 0);
  826. }
  827. if(map->twoLevel)
  828. {
  829. const JsonNode underground = getFromArchive(TERRAIN_FILE_NAMES[1]);
  830. readTerrainLevel(underground, 1);
  831. }
  832. }
  833. CMapLoaderJson::MapObjectLoader::MapObjectLoader(CMapLoaderJson * _owner, JsonMap::value_type & json):
  834. owner(_owner), instance(nullptr), id(-1), jsonKey(json.first), configuration(json.second)
  835. {
  836. }
  837. void CMapLoaderJson::MapObjectLoader::construct()
  838. {
  839. //TODO:consider move to ObjectTypeHandler
  840. //find type handler
  841. std::string typeName = configuration["type"].String();
  842. std::string subtypeName = configuration["subtype"].String();
  843. if(typeName.empty())
  844. {
  845. logGlobal->error("Object type missing");
  846. logGlobal->debug(configuration.toString());
  847. return;
  848. }
  849. int3 pos;
  850. pos.x = static_cast<si32>(configuration["x"].Float());
  851. pos.y = static_cast<si32>(configuration["y"].Float());
  852. pos.z = static_cast<si32>(configuration["l"].Float());
  853. //special case for grail
  854. if(typeName == "grail")
  855. {
  856. owner->map->grailPos = pos;
  857. owner->map->grailRadius = static_cast<int>(configuration["options"]["grailRadius"].Float());
  858. return;
  859. }
  860. else if(subtypeName.empty())
  861. {
  862. logGlobal->error("Object subtype missing");
  863. logGlobal->debug(configuration.toString());
  864. return;
  865. }
  866. auto handler = LIBRARY->objtypeh->getHandlerFor( ModScope::scopeMap(), typeName, subtypeName);
  867. auto appearance = std::make_shared<ObjectTemplate>();
  868. appearance->id = Obj(handler->getIndex());
  869. appearance->subid = handler->getSubIndex();
  870. appearance->readJson(configuration["template"], false);
  871. // Will be destroyed soon and replaced with shared template
  872. instance = handler->create(owner->map->cb, appearance);
  873. instance->instanceName = jsonKey;
  874. instance->setAnchorPos(pos);
  875. owner->map->addNewObject(instance);
  876. }
  877. void CMapLoaderJson::MapObjectLoader::configure()
  878. {
  879. if(nullptr == instance)
  880. return;
  881. JsonDeserializer handler(owner->mapObjectResolver.get(), configuration);
  882. instance->serializeJson(handler);
  883. //artifact instance serialization requires access to Map object, handle it here for now
  884. //todo: find better solution for artifact instance serialization
  885. if(auto art = std::dynamic_pointer_cast<CGArtifact>(instance))
  886. {
  887. ArtifactID artID = ArtifactID::NONE;
  888. SpellID spellID = SpellID::NONE;
  889. if(art->ID == Obj::SPELL_SCROLL)
  890. {
  891. auto spellIdentifier = configuration["options"]["spell"].String();
  892. auto rawId = LIBRARY->identifiers()->getIdentifier(ModScope::scopeBuiltin(), "spell", spellIdentifier);
  893. if(rawId)
  894. spellID = rawId.value();
  895. else
  896. spellID = 0;
  897. artID = ArtifactID::SPELL_SCROLL;
  898. }
  899. else if(art->ID == Obj::ARTIFACT)
  900. {
  901. //specific artifact
  902. artID = art->getArtifactType();
  903. }
  904. art->setArtifactInstance(owner->map->createArtifact(artID, spellID.getNum()));
  905. }
  906. if(auto hero = std::dynamic_pointer_cast<CGHeroInstance>(instance))
  907. {
  908. auto o = handler.enterStruct("options");
  909. hero->serializeJsonArtifacts(handler, "artifacts", owner->map);
  910. }
  911. }
  912. void CMapLoaderJson::readObjects()
  913. {
  914. LOG_TRACE(logGlobal);
  915. std::vector<std::unique_ptr<MapObjectLoader>> loaders;//todo: optimize MapObjectLoader memory layout
  916. JsonNode data = getFromArchive(OBJECTS_FILE_NAME);
  917. //get raw data
  918. for(auto & p : data.Struct())
  919. loaders.push_back(std::make_unique<MapObjectLoader>(this, p));
  920. for(auto & ptr : loaders)
  921. ptr->construct();
  922. //configure objects after all objects are constructed so we may resolve internal IDs even to actual pointers OTF
  923. for(auto & ptr : loaders)
  924. ptr->configure();
  925. std::set<HeroTypeID> debugHeroesOnMap;
  926. for (auto const & hero : map->getObjects<CGHeroInstance>())
  927. {
  928. if(hero->ID != Obj::HERO && hero->ID != Obj::PRISON)
  929. continue;
  930. if (debugHeroesOnMap.count(hero->getHeroTypeID()))
  931. logGlobal->error("Hero is already on the map at %s", hero->visitablePos().toString());
  932. debugHeroesOnMap.insert(hero->getHeroTypeID());
  933. }
  934. }
  935. void CMapLoaderJson::readTranslations()
  936. {
  937. std::list<Languages::Options> languages{Languages::getLanguageList().begin(), Languages::getLanguageList().end()};
  938. for(auto & language : Languages::getLanguageList())
  939. {
  940. if(isExistArchive(language.identifier + ".json"))
  941. mapHeader->translations.Struct()[language.identifier] = getFromArchive(language.identifier + ".json");
  942. }
  943. mapHeader->registerMapStrings();
  944. }
  945. ///CMapSaverJson
  946. CMapSaverJson::CMapSaverJson(CInputOutputStream * stream)
  947. : buffer(stream)
  948. , ioApi(new CProxyIOApi(buffer))
  949. , saver(ioApi, "_")
  950. {
  951. fileVersionMajor = VERSION_MAJOR;
  952. fileVersionMinor = VERSION_MINOR;
  953. }
  954. //must be instantiated in .cpp file for access to complete types of all member fields
  955. CMapSaverJson::~CMapSaverJson() = default;
  956. void CMapSaverJson::addToArchive(const JsonNode & data, const std::string & filename)
  957. {
  958. std::ostringstream out;
  959. JsonWriter writer(out, false);
  960. writer.writeNode(data);
  961. out.flush();
  962. {
  963. auto s = out.str();
  964. std::unique_ptr<COutputStream> stream = saver.addFile(filename);
  965. if(stream->write(reinterpret_cast<const ui8 *>(s.c_str()), s.size()) != s.size())
  966. throw std::runtime_error("CMapSaverJson::saveHeader() zip compression failed.");
  967. }
  968. }
  969. void CMapSaverJson::saveMap(const std::unique_ptr<CMap>& map)
  970. {
  971. this->map = map.get();
  972. this->mapHeader = this->map;
  973. writeHeader();
  974. writeTerrain();
  975. writeObjects();
  976. }
  977. void CMapSaverJson::writeHeader()
  978. {
  979. logGlobal->trace("Saving header");
  980. JsonNode header;
  981. JsonSerializer handler(mapObjectResolver.get(), header);
  982. header["versionMajor"].Float() = VERSION_MAJOR;
  983. header["versionMinor"].Float() = VERSION_MINOR;
  984. //write mods
  985. header["mods"] = ModVerificationInfo::jsonSerializeList(mapHeader->mods);
  986. //todo: multilevel map save support
  987. JsonNode & levels = header["mapLevels"];
  988. levels["surface"]["height"].Float() = mapHeader->height;
  989. levels["surface"]["width"].Float() = mapHeader->width;
  990. levels["surface"]["index"].Float() = 0;
  991. if(mapHeader->twoLevel)
  992. {
  993. levels["underground"]["height"].Float() = mapHeader->height;
  994. levels["underground"]["width"].Float() = mapHeader->width;
  995. levels["underground"]["index"].Float() = 1;
  996. }
  997. serializeHeader(handler);
  998. writeTriggeredEvents(handler);
  999. writeTeams(handler);
  1000. writeOptions(handler);
  1001. writeTranslations();
  1002. addToArchive(header, HEADER_FILE_NAME);
  1003. }
  1004. std::string CMapSaverJson::writeTerrainTile(const TerrainTile & tile)
  1005. {
  1006. using namespace TerrainDetail;
  1007. std::ostringstream out;
  1008. out.setf(std::ios::dec, std::ios::basefield);
  1009. out.unsetf(std::ios::showbase);
  1010. out << tile.getTerrain()->shortIdentifier << static_cast<int>(tile.terView) << flipCodes[tile.extTileFlags % 4];
  1011. if(tile.hasRoad())
  1012. out << tile.getRoad()->shortIdentifier << static_cast<int>(tile.roadDir) << flipCodes[(tile.extTileFlags >> 4) % 4];
  1013. if(tile.hasRiver())
  1014. out << tile.getRiver()->shortIdentifier << static_cast<int>(tile.riverDir) << flipCodes[(tile.extTileFlags >> 2) % 4];
  1015. return out.str();
  1016. }
  1017. JsonNode CMapSaverJson::writeTerrainLevel(const int index)
  1018. {
  1019. JsonNode data;
  1020. int3 pos(0,0,index);
  1021. data.Vector().resize(map->height);
  1022. for(pos.y = 0; pos.y < map->height; pos.y++)
  1023. {
  1024. JsonNode & row = data.Vector()[pos.y];
  1025. row.Vector().resize(map->width);
  1026. for(pos.x = 0; pos.x < map->width; pos.x++)
  1027. row.Vector()[pos.x].String() = writeTerrainTile(map->getTile(pos));
  1028. }
  1029. return data;
  1030. }
  1031. void CMapSaverJson::writeTerrain()
  1032. {
  1033. logGlobal->trace("Saving terrain");
  1034. //todo: multilevel map save support
  1035. JsonNode surface = writeTerrainLevel(0);
  1036. addToArchive(surface, TERRAIN_FILE_NAMES[0]);
  1037. if(map->twoLevel)
  1038. {
  1039. JsonNode underground = writeTerrainLevel(1);
  1040. addToArchive(underground, TERRAIN_FILE_NAMES[1]);
  1041. }
  1042. }
  1043. void CMapSaverJson::writeObjects()
  1044. {
  1045. logGlobal->trace("Saving objects");
  1046. JsonNode data;
  1047. JsonSerializer handler(mapObjectResolver.get(), data);
  1048. for(const auto & obj : map->getObjects())
  1049. {
  1050. //logGlobal->trace("\t%s", obj->instanceName);
  1051. auto temp = handler.enterStruct(obj->instanceName);
  1052. obj->serializeJson(handler);
  1053. }
  1054. if(map->grailPos.isValid())
  1055. {
  1056. JsonNode grail;
  1057. grail["type"].String() = "grail";
  1058. grail["x"].Float() = map->grailPos.x;
  1059. grail["y"].Float() = map->grailPos.y;
  1060. grail["l"].Float() = map->grailPos.z;
  1061. grail["options"]["radius"].Float() = map->grailRadius;
  1062. data["grail"] = grail;
  1063. }
  1064. //cleanup empty options
  1065. for(auto & p : data.Struct())
  1066. {
  1067. JsonNode & obj = p.second;
  1068. if(obj["options"].Struct().empty())
  1069. obj.Struct().erase("options");
  1070. }
  1071. addToArchive(data, OBJECTS_FILE_NAME);
  1072. }
  1073. void CMapSaverJson::writeTranslations()
  1074. {
  1075. for(auto & s : mapHeader->translations.Struct())
  1076. {
  1077. auto & language = s.first;
  1078. if(Languages::getLanguageOptions(language).identifier.empty())
  1079. {
  1080. logGlobal->error("Serializing of unsupported language %s is not permitted", language);
  1081. continue;
  1082. }
  1083. logGlobal->trace("Saving translations, language: %s", language);
  1084. addToArchive(s.second, language + ".json");
  1085. }
  1086. }
  1087. VCMI_LIB_NAMESPACE_END