ContentTypeHandler.cpp 9.3 KB

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