ContentTypeHandler.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * ContentTypeHandler.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 "ContentTypeHandler.h"
  12. #include "CModHandler.h"
  13. #include "CModInfo.h"
  14. #include "ModScope.h"
  15. #include "../BattleFieldHandler.h"
  16. #include "../CArtHandler.h"
  17. #include "../CCreatureHandler.h"
  18. #include "../entities/faction/CTownHandler.h"
  19. #include "../texts/CGeneralTextHandler.h"
  20. #include "../CHeroHandler.h"
  21. #include "../CSkillHandler.h"
  22. #include "../CStopWatch.h"
  23. #include "../GameSettings.h"
  24. #include "../IHandlerBase.h"
  25. #include "../ObstacleHandler.h"
  26. #include "../mapObjects/ObstacleSetHandler.h"
  27. #include "../RiverHandler.h"
  28. #include "../RoadHandler.h"
  29. #include "../ScriptHandler.h"
  30. #include "../constants/StringConstants.h"
  31. #include "../TerrainHandler.h"
  32. #include "../json/JsonUtils.h"
  33. #include "../mapObjectConstructors/CObjectClassesHandler.h"
  34. #include "../rmg/CRmgTemplateStorage.h"
  35. #include "../spells/CSpellHandler.h"
  36. #include "../VCMI_Lib.h"
  37. VCMI_LIB_NAMESPACE_BEGIN
  38. ContentTypeHandler::ContentTypeHandler(IHandlerBase * handler, const std::string & objectName):
  39. handler(handler),
  40. objectName(objectName),
  41. originalData(handler->loadLegacyData())
  42. {
  43. for(auto & node : originalData)
  44. {
  45. node.setModScope(ModScope::scopeBuiltin());
  46. }
  47. }
  48. bool ContentTypeHandler::preloadModData(const std::string & modName, const std::vector<std::string> & fileList, bool validate)
  49. {
  50. bool result = false;
  51. JsonNode data = JsonUtils::assembleFromFiles(fileList, result);
  52. data.setModScope(modName);
  53. ModInfo & modInfo = modData[modName];
  54. for(auto entry : data.Struct())
  55. {
  56. size_t colon = entry.first.find(':');
  57. if (colon == std::string::npos)
  58. {
  59. // normal object, local to this mod
  60. std::swap(modInfo.modData[entry.first], entry.second);
  61. }
  62. else
  63. {
  64. std::string remoteName = entry.first.substr(0, colon);
  65. std::string objectName = entry.first.substr(colon + 1);
  66. // patching this mod? Send warning and continue - this situation can be handled normally
  67. if (remoteName == modName)
  68. logMod->warn("Redundant namespace definition for %s", objectName);
  69. logMod->trace("Patching object %s (%s) from %s", objectName, remoteName, modName);
  70. JsonNode & remoteConf = modData[remoteName].patches[objectName];
  71. JsonUtils::merge(remoteConf, entry.second);
  72. }
  73. }
  74. return result;
  75. }
  76. bool ContentTypeHandler::loadMod(const std::string & modName, bool validate)
  77. {
  78. ModInfo & modInfo = modData[modName];
  79. bool result = true;
  80. auto performValidate = [&,this](JsonNode & data, const std::string & name){
  81. handler->beforeValidate(data);
  82. if (validate)
  83. result &= JsonUtils::validate(data, "vcmi:" + objectName, name);
  84. };
  85. // apply patches
  86. if (!modInfo.patches.isNull())
  87. JsonUtils::merge(modInfo.modData, modInfo.patches);
  88. for(auto & entry : modInfo.modData.Struct())
  89. {
  90. const std::string & name = entry.first;
  91. JsonNode & data = entry.second;
  92. if (data.getModScope() != modName)
  93. {
  94. // in this scenario, entire object record comes from another mod
  95. // normally, this is used to "patch" object from another mod (which is legal)
  96. // however in this case there is no object to patch. This might happen in such cases:
  97. // - another mod attempts to add object into this mod (technically can be supported, but might lead to weird edge cases)
  98. // - another mod attempts to edit object from this mod that no longer exist - DANGER since such patch likely has very incomplete data
  99. // so emit warning and skip such case
  100. logMod->warn("Mod '%s' attempts to edit object '%s' of type '%s' from mod '%s' but no such object exist!", data.getModScope(), name, objectName, modName);
  101. continue;
  102. }
  103. bool hasIndex = vstd::contains(data.Struct(), "index") && !data["index"].isNull();
  104. if (hasIndex && modName != "core")
  105. logMod->error("Mod %s is attempting to load original data! This option is reserved for built-in mod.", modName);
  106. if (hasIndex && modName == "core")
  107. {
  108. // try to add H3 object data
  109. size_t index = static_cast<size_t>(data["index"].Float());
  110. if(originalData.size() > index)
  111. {
  112. logMod->trace("found original data in loadMod(%s) at index %d", name, index);
  113. JsonUtils::merge(originalData[index], data);
  114. std::swap(originalData[index], data);
  115. originalData[index].clear(); // do not use same data twice (same ID)
  116. }
  117. else
  118. {
  119. logMod->trace("no original data in loadMod(%s) at index %d", name, index);
  120. }
  121. performValidate(data, name);
  122. handler->loadObject(modName, name, data, index);
  123. }
  124. else
  125. {
  126. // normal new object
  127. logMod->trace("no index in loadMod(%s)", name);
  128. performValidate(data,name);
  129. handler->loadObject(modName, name, data);
  130. }
  131. }
  132. return result;
  133. }
  134. void ContentTypeHandler::loadCustom()
  135. {
  136. handler->loadCustom();
  137. }
  138. void ContentTypeHandler::afterLoadFinalization()
  139. {
  140. for (auto const & data : modData)
  141. {
  142. if (data.second.modData.isNull())
  143. {
  144. for (auto node : data.second.patches.Struct())
  145. logMod->warn("Mod '%s' have added patch for object '%s' from mod '%s', but this mod was not loaded or has no new objects.", node.second.getModScope(), node.first, data.first);
  146. }
  147. for(auto & otherMod : modData)
  148. {
  149. if (otherMod.first == data.first)
  150. continue;
  151. if (otherMod.second.modData.isNull())
  152. continue;
  153. for(auto & otherObject : otherMod.second.modData.Struct())
  154. {
  155. if (data.second.modData.Struct().count(otherObject.first))
  156. {
  157. logMod->warn("Mod '%s' have added object with name '%s' that is also available in mod '%s'", data.first, otherObject.first, otherMod.first);
  158. logMod->warn("Two objects with same name were loaded. Please use form '%s:%s' if mod '%s' needs to modify this object instead", otherMod.first, otherObject.first, data.first);
  159. }
  160. }
  161. }
  162. }
  163. handler->afterLoadFinalization();
  164. }
  165. void CContentHandler::init()
  166. {
  167. handlers.insert(std::make_pair("heroClasses", ContentTypeHandler(VLC->heroclassesh.get(), "heroClass")));
  168. handlers.insert(std::make_pair("artifacts", ContentTypeHandler(VLC->arth.get(), "artifact")));
  169. handlers.insert(std::make_pair("creatures", ContentTypeHandler(VLC->creh.get(), "creature")));
  170. handlers.insert(std::make_pair("factions", ContentTypeHandler(VLC->townh.get(), "faction")));
  171. handlers.insert(std::make_pair("objects", ContentTypeHandler(VLC->objtypeh.get(), "object")));
  172. handlers.insert(std::make_pair("heroes", ContentTypeHandler(VLC->heroh.get(), "hero")));
  173. handlers.insert(std::make_pair("spells", ContentTypeHandler(VLC->spellh.get(), "spell")));
  174. handlers.insert(std::make_pair("skills", ContentTypeHandler(VLC->skillh.get(), "skill")));
  175. handlers.insert(std::make_pair("templates", ContentTypeHandler(VLC->tplh.get(), "template")));
  176. #if SCRIPTING_ENABLED
  177. handlers.insert(std::make_pair("scripts", ContentTypeHandler(VLC->scriptHandler.get(), "script")));
  178. #endif
  179. handlers.insert(std::make_pair("battlefields", ContentTypeHandler(VLC->battlefieldsHandler.get(), "battlefield")));
  180. handlers.insert(std::make_pair("terrains", ContentTypeHandler(VLC->terrainTypeHandler.get(), "terrain")));
  181. handlers.insert(std::make_pair("rivers", ContentTypeHandler(VLC->riverTypeHandler.get(), "river")));
  182. handlers.insert(std::make_pair("roads", ContentTypeHandler(VLC->roadTypeHandler.get(), "road")));
  183. handlers.insert(std::make_pair("obstacles", ContentTypeHandler(VLC->obstacleHandler.get(), "obstacle")));
  184. handlers.insert(std::make_pair("biomes", ContentTypeHandler(VLC->biomeHandler.get(), "biome")));
  185. }
  186. bool CContentHandler::preloadModData(const std::string & modName, JsonNode modConfig, bool validate)
  187. {
  188. bool result = true;
  189. for(auto & handler : handlers)
  190. {
  191. result &= handler.second.preloadModData(modName, modConfig[handler.first].convertTo<std::vector<std::string> >(), validate);
  192. }
  193. return result;
  194. }
  195. bool CContentHandler::loadMod(const std::string & modName, bool validate)
  196. {
  197. bool result = true;
  198. for(auto & handler : handlers)
  199. {
  200. result &= handler.second.loadMod(modName, validate);
  201. }
  202. return result;
  203. }
  204. void CContentHandler::loadCustom()
  205. {
  206. for(auto & handler : handlers)
  207. {
  208. handler.second.loadCustom();
  209. }
  210. }
  211. void CContentHandler::afterLoadFinalization()
  212. {
  213. for(auto & handler : handlers)
  214. {
  215. handler.second.afterLoadFinalization();
  216. }
  217. }
  218. void CContentHandler::preloadData(CModInfo & mod)
  219. {
  220. bool validate = (mod.validation != CModInfo::PASSED);
  221. // print message in format [<8-symbols checksum>] <modname>
  222. auto & info = mod.getVerificationInfo();
  223. logMod->info("\t\t[%08x]%s", info.checksum, info.name);
  224. if (validate && mod.identifier != ModScope::scopeBuiltin())
  225. {
  226. if (!JsonUtils::validate(mod.config, "vcmi:mod", mod.identifier))
  227. mod.validation = CModInfo::FAILED;
  228. }
  229. if (!preloadModData(mod.identifier, mod.config, validate))
  230. mod.validation = CModInfo::FAILED;
  231. }
  232. void CContentHandler::load(CModInfo & mod)
  233. {
  234. bool validate = (mod.validation != CModInfo::PASSED);
  235. if (!loadMod(mod.identifier, validate))
  236. mod.validation = CModInfo::FAILED;
  237. if (validate)
  238. {
  239. if (mod.validation != CModInfo::FAILED)
  240. logMod->info("\t\t[DONE] %s", mod.getVerificationInfo().name);
  241. else
  242. logMod->error("\t\t[FAIL] %s", mod.getVerificationInfo().name);
  243. }
  244. else
  245. logMod->info("\t\t[SKIP] %s", mod.getVerificationInfo().name);
  246. }
  247. const ContentTypeHandler & CContentHandler::operator[](const std::string & name) const
  248. {
  249. return handlers.at(name);
  250. }
  251. VCMI_LIB_NAMESPACE_END