MapFormatJson.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 "CMap.h"
  15. #include "../CModHandler.h"
  16. #include "../VCMI_Lib.h"
  17. static const std::string conditionNames[] = {
  18. "haveArtifact", "haveCreatures", "haveResources", "haveBuilding",
  19. "control", "destroy", "transport", "daysPassed",
  20. "isHuman", "daysWithoutTown", "standardWin", "constValue"
  21. };
  22. static const std::string typeNames[] = { "victory", "defeat" };
  23. static EventCondition JsonToCondition(const JsonNode & node)
  24. {
  25. EventCondition event;
  26. event.condition = EventCondition::EWinLoseType(vstd::find_pos(conditionNames, node.Vector()[0].String()));
  27. if (node.Vector().size() > 1)
  28. {
  29. const JsonNode & data = node.Vector()[1];
  30. if (data["type"].getType() == JsonNode::DATA_STRING)
  31. event.objectType = VLC->modh->identifiers.getIdentifier(data["type"]).get();
  32. if (data["type"].getType() == JsonNode::DATA_FLOAT)
  33. event.objectType = data["type"].Float();
  34. if (!data["value"].isNull())
  35. event.value = data["value"].Float();
  36. if (!data["position"].isNull())
  37. {
  38. event.position.x = data["position"].Vector()[0].Float();
  39. event.position.y = data["position"].Vector()[1].Float();
  40. event.position.z = data["position"].Vector()[2].Float();
  41. }
  42. }
  43. return event;
  44. }
  45. ///CMapFormatJson
  46. const int CMapFormatJson::VERSION_MAJOR = 1;
  47. const int CMapFormatJson::VERSION_MINOR = 0;
  48. const std::string CMapFormatJson::HEADER_FILE_NAME = "header.json";
  49. void CMapFormatJson::readTriggeredEvents(const JsonNode & input)
  50. {
  51. mapHeader->victoryMessage = input["victoryString"].String();
  52. mapHeader->victoryIconIndex = input["victoryIconIndex"].Float();
  53. mapHeader->defeatMessage = input["defeatString"].String();
  54. mapHeader->defeatIconIndex = input["defeatIconIndex"].Float();
  55. mapHeader->triggeredEvents.clear();
  56. for (auto & entry : input["triggeredEvents"].Struct())
  57. {
  58. TriggeredEvent event;
  59. event.identifier = entry.first;
  60. readTriggeredEvent(event, entry.second);
  61. mapHeader->triggeredEvents.push_back(event);
  62. }
  63. }
  64. void CMapFormatJson::readTriggeredEvent(TriggeredEvent & event, const JsonNode & source)
  65. {
  66. event.onFulfill = source["message"].String();
  67. event.description = source["description"].String();
  68. event.effect.type = vstd::find_pos(typeNames, source["effect"]["type"].String());
  69. event.effect.toOtherMessage = source["effect"]["messageToSend"].String();
  70. event.trigger = EventExpression(source["condition"], JsonToCondition); // logical expression
  71. }
  72. ///CMapPatcher
  73. CMapPatcher::CMapPatcher(JsonNode stream):
  74. input(stream)
  75. {
  76. }
  77. void CMapPatcher::patchMapHeader(std::unique_ptr<CMapHeader> & header)
  78. {
  79. header.swap(mapHeader);
  80. if (!input.isNull())
  81. readPatchData();
  82. header.swap(mapHeader);
  83. }
  84. void CMapPatcher::readPatchData()
  85. {
  86. readTriggeredEvents(input);
  87. }
  88. ///CMapFormatZip
  89. CMapFormatZip::CMapFormatZip(CInputOutputStream * stream):
  90. buffer(stream),
  91. ioApi(new CProxyIOApi(buffer))
  92. {
  93. }
  94. ///CMapLoaderJson
  95. CMapLoaderJson::CMapLoaderJson(CInputOutputStream * stream):
  96. CMapFormatZip(stream),
  97. loader("", "_", ioApi)
  98. {
  99. }
  100. std::unique_ptr<CMap> CMapLoaderJson::loadMap()
  101. {
  102. map = new CMap();
  103. mapHeader = std::unique_ptr<CMapHeader>(dynamic_cast<CMapHeader *>(map));
  104. readMap();
  105. return std::unique_ptr<CMap>(dynamic_cast<CMap *>(mapHeader.release()));
  106. }
  107. std::unique_ptr<CMapHeader> CMapLoaderJson::loadMapHeader()
  108. {
  109. mapHeader.reset(new CMapHeader);
  110. readHeader();
  111. return std::move(mapHeader);
  112. }
  113. /*
  114. //This code can be used to write map header to console or file in its Json representation
  115. JsonNode out;
  116. JsonNode data;
  117. data["victoryString"].String() = mapHeader->victoryMessage;
  118. data["defeatString"].String() = mapHeader->defeatMessage;
  119. data["victoryIconIndex"].Float() = mapHeader->victoryIconIndex;
  120. data["defeatIconIndex"].Float() = mapHeader->defeatIconIndex;
  121. for (const TriggeredEvent & entry : mapHeader->triggeredEvents)
  122. {
  123. JsonNode event;
  124. event["message"].String() = entry.onFulfill;
  125. event["effect"]["messageToSend"].String() = entry.effect.toOtherMessage;
  126. event["effect"]["type"].String() = typeNames[entry.effect.type];
  127. event["condition"] = entry.trigger.toJson(eventToJson);
  128. data["triggeredEvents"][entry.identifier] = event;
  129. }
  130. out[mapHeader->name] = data;
  131. logGlobal->errorStream() << out;
  132. JsonNode eventToJson(const EventCondition & cond)
  133. {
  134. JsonNode ret;
  135. ret.Vector().resize(2);
  136. ret.Vector()[0].String() = conditionNames[size_t(cond.condition)];
  137. JsonNode & data = ret.Vector()[1];
  138. data["type"].Float() = cond.objectType;
  139. data["value"].Float() = cond.value;
  140. data["position"].Vector().resize(3);
  141. data["position"].Vector()[0].Float() = cond.position.x;
  142. data["position"].Vector()[1].Float() = cond.position.y;
  143. data["position"].Vector()[2].Float() = cond.position.z;
  144. return ret;
  145. }
  146. */
  147. void CMapLoaderJson::readMap()
  148. {
  149. readHeader();
  150. map->initTerrain();
  151. //TODO:readMap
  152. }
  153. void CMapLoaderJson::readHeader()
  154. {
  155. //do not use map field here, use only mapHeader
  156. ResourceID headerID(HEADER_FILE_NAME, EResType::TEXT);
  157. if(!loader.existsResource(headerID))
  158. throw new std::runtime_error(HEADER_FILE_NAME+" not found");
  159. auto headerData = loader.load(headerID)->readAll();
  160. const JsonNode header(reinterpret_cast<char*>(headerData.first.get()), headerData.second);
  161. //TODO: read such data like map name & size
  162. //mapHeader->version = ??? //todo: new version field
  163. //todo: multilevel map load support
  164. const JsonNode levels = header["mapLevels"];
  165. mapHeader->height = levels["surface"]["height"].Float();
  166. mapHeader->width = levels["surface"]["width"].Float();
  167. mapHeader->twoLevel = !levels["underground"].isNull();
  168. mapHeader->name = header["name"].String();
  169. mapHeader->description = header["description"].String();
  170. //todo: support arbitrary percentage
  171. static const std::map<std::string, ui8> difficultyMap =
  172. {
  173. {"EASY", 0},
  174. {"NORMAL", 1},
  175. {"HARD", 2},
  176. {"EXPERT", 3},
  177. {"IMPOSSIBLE", 4}
  178. };
  179. mapHeader->difficulty = difficultyMap.at(header["difficulty"].String());
  180. mapHeader->levelLimit = header["levelLimit"].Float();
  181. // std::vector<bool> allowedHeroes;
  182. // std::vector<ui16> placeholdedHeroes;
  183. readTriggeredEvents(header);
  184. readPlayerInfo();
  185. //TODO: readHeader
  186. }
  187. void CMapLoaderJson::readPlayerInfo()
  188. {
  189. //ui8 howManyTeams;
  190. //TODO: readPlayerInfo
  191. }
  192. ///CMapSaverJson
  193. CMapSaverJson::CMapSaverJson(CInputOutputStream * stream):
  194. CMapFormatZip(stream),
  195. saver(ioApi, "_")
  196. {
  197. }
  198. CMapSaverJson::~CMapSaverJson()
  199. {
  200. }
  201. void CMapSaverJson::saveMap(const std::unique_ptr<CMap>& map)
  202. {
  203. //TODO: saveMap
  204. this->map = map.get();
  205. saveHeader();
  206. }
  207. void CMapSaverJson::saveHeader()
  208. {
  209. JsonNode header;
  210. header["versionMajor"].Float() = VERSION_MAJOR;
  211. header["versionMinor"].Float() = VERSION_MINOR;
  212. //todo: multilevel map save support
  213. JsonNode levels = header["mapLevels"];
  214. levels["surface"]["height"].Float() = map->height;
  215. levels["surface"]["width"].Float() = map->width;
  216. levels["surface"]["index"].Float() = 0;
  217. if(map->twoLevel)
  218. {
  219. levels["underground"]["height"].Float() = map->height;
  220. levels["underground"]["width"].Float() = map->width;
  221. levels["underground"]["index"].Float() = 1;
  222. }
  223. header["name"].String() = map->name;
  224. header["description"].String() = map->description;
  225. //todo: support arbitrary percentage
  226. static const std::map<ui8, std::string> difficultyMap =
  227. {
  228. {0, "EASY"},
  229. {1, "NORMAL"},
  230. {2, "HARD"},
  231. {3, "EXPERT"},
  232. {4, "IMPOSSIBLE"}
  233. };
  234. header["difficulty"].String() = difficultyMap.at(map->difficulty);
  235. header["levelLimit"].Float() = map->levelLimit;
  236. //todo: allowedHeroes;
  237. //todo: placeholdedHeroes;
  238. std::ostringstream out;
  239. out << header;
  240. out.flush();
  241. {
  242. auto s = out.str();
  243. std::unique_ptr<COutputStream> stream = saver.addFile(HEADER_FILE_NAME);
  244. if (stream->write((const ui8*)s.c_str(), s.size()) != s.size())
  245. throw new std::runtime_error("CMapSaverJson::saveHeader() zip compression failed.");
  246. }
  247. }