ContentTypeHandler.cpp 9.0 KB

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