ContentTypeHandler.cpp 9.1 KB

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