ContentTypeHandler.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 "../IGameSettings.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 & entityName):
  39. handler(handler),
  40. entityName(entityName),
  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. if (!remoteConf.isNull())
  72. JsonUtils::detectConflicts(conflictList, remoteConf, entry.second, objectName);
  73. JsonUtils::merge(remoteConf, entry.second);
  74. }
  75. }
  76. return result;
  77. }
  78. bool ContentTypeHandler::loadMod(const std::string & modName, bool validate)
  79. {
  80. ModInfo & modInfo = modData[modName];
  81. bool result = true;
  82. auto performValidate = [&,this](JsonNode & data, const std::string & name){
  83. handler->beforeValidate(data);
  84. if (validate)
  85. result &= JsonUtils::validate(data, "vcmi:" + entityName, name);
  86. };
  87. // apply patches
  88. if (!modInfo.patches.isNull())
  89. JsonUtils::merge(modInfo.modData, modInfo.patches);
  90. for(auto & entry : modInfo.modData.Struct())
  91. {
  92. const std::string & name = entry.first;
  93. JsonNode & data = entry.second;
  94. if (data.getModScope() != modName)
  95. {
  96. // in this scenario, entire object record comes from another mod
  97. // normally, this is used to "patch" object from another mod (which is legal)
  98. // however in this case there is no object to patch. This might happen in such cases:
  99. // - another mod attempts to add object into this mod (technically can be supported, but might lead to weird edge cases)
  100. // - another mod attempts to edit object from this mod that no longer exist - DANGER since such patch likely has very incomplete data
  101. // so emit warning and skip such case
  102. logMod->warn("Mod '%s' attempts to edit object '%s' of type '%s' from mod '%s' but no such object exist!", data.getModScope(), name, entityName, modName);
  103. continue;
  104. }
  105. bool hasIndex = vstd::contains(data.Struct(), "index") && !data["index"].isNull();
  106. if (hasIndex && modName != "core")
  107. logMod->error("Mod %s is attempting to load original data! This option is reserved for built-in mod.", modName);
  108. if (hasIndex && modName == "core")
  109. {
  110. // try to add H3 object data
  111. size_t index = static_cast<size_t>(data["index"].Float());
  112. if(originalData.size() > index)
  113. {
  114. logMod->trace("found original data in loadMod(%s) at index %d", name, index);
  115. JsonUtils::merge(originalData[index], data);
  116. std::swap(originalData[index], data);
  117. originalData[index].clear(); // do not use same data twice (same ID)
  118. }
  119. else
  120. {
  121. logMod->trace("no original data in loadMod(%s) at index %d", name, index);
  122. }
  123. performValidate(data, name);
  124. handler->loadObject(modName, name, data, index);
  125. }
  126. else
  127. {
  128. // normal new object
  129. logMod->trace("no index in loadMod(%s)", name);
  130. performValidate(data,name);
  131. handler->loadObject(modName, name, data);
  132. }
  133. }
  134. return result;
  135. }
  136. void ContentTypeHandler::loadCustom()
  137. {
  138. handler->loadCustom();
  139. }
  140. void ContentTypeHandler::afterLoadFinalization()
  141. {
  142. for (auto const & data : modData)
  143. {
  144. if (data.second.modData.isNull())
  145. {
  146. for (auto node : data.second.patches.Struct())
  147. 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);
  148. }
  149. for(auto & otherMod : modData)
  150. {
  151. if (otherMod.first == data.first)
  152. continue;
  153. if (otherMod.second.modData.isNull())
  154. continue;
  155. for(auto & otherObject : otherMod.second.modData.Struct())
  156. {
  157. if (data.second.modData.Struct().count(otherObject.first))
  158. {
  159. logMod->warn("Mod '%s' have added object with name '%s' that is also available in mod '%s'", data.first, otherObject.first, otherMod.first);
  160. 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);
  161. }
  162. }
  163. }
  164. }
  165. for (const auto& [conflictPath, conflictModData] : conflictList.Struct())
  166. {
  167. std::set<std::string> conflictingMods;
  168. std::set<std::string> resolvedConflicts;
  169. for (auto const & conflictModData : conflictModData.Struct())
  170. conflictingMods.insert(conflictModData.first);
  171. for (auto const & modID : conflictingMods)
  172. resolvedConflicts.merge(VLC->modh->getModDependencies(modID));
  173. vstd::erase_if(conflictingMods, [&resolvedConflicts](const std::string & entry){ return resolvedConflicts.count(entry);});
  174. if (conflictingMods.size() < 2)
  175. continue; // all conflicts were resolved - either via compatibility patch (mod that depends on 2 conflicting mods) or simple mod that depends on another one
  176. bool allEqual = true;
  177. for (auto const & modID : conflictingMods)
  178. {
  179. if (conflictModData[modID] != conflictModData[*conflictingMods.begin()])
  180. {
  181. allEqual = false;
  182. break;
  183. }
  184. }
  185. if (allEqual)
  186. continue; // conflict still present, but all mods use the same value for conflicting entry - permit it
  187. logMod->warn("Potential confict in '%s'", conflictPath);
  188. for (auto const & modID : conflictingMods)
  189. logMod->warn("Mod '%s' - value set to %s", modID, conflictModData[modID].toCompactString());
  190. }
  191. handler->afterLoadFinalization();
  192. }
  193. void CContentHandler::init()
  194. {
  195. handlers.insert(std::make_pair("heroClasses", ContentTypeHandler(VLC->heroclassesh.get(), "heroClass")));
  196. handlers.insert(std::make_pair("artifacts", ContentTypeHandler(VLC->arth.get(), "artifact")));
  197. handlers.insert(std::make_pair("creatures", ContentTypeHandler(VLC->creh.get(), "creature")));
  198. handlers.insert(std::make_pair("factions", ContentTypeHandler(VLC->townh.get(), "faction")));
  199. handlers.insert(std::make_pair("objects", ContentTypeHandler(VLC->objtypeh.get(), "object")));
  200. handlers.insert(std::make_pair("heroes", ContentTypeHandler(VLC->heroh.get(), "hero")));
  201. handlers.insert(std::make_pair("spells", ContentTypeHandler(VLC->spellh.get(), "spell")));
  202. handlers.insert(std::make_pair("skills", ContentTypeHandler(VLC->skillh.get(), "skill")));
  203. handlers.insert(std::make_pair("templates", ContentTypeHandler(VLC->tplh.get(), "template")));
  204. #if SCRIPTING_ENABLED
  205. handlers.insert(std::make_pair("scripts", ContentTypeHandler(VLC->scriptHandler.get(), "script")));
  206. #endif
  207. handlers.insert(std::make_pair("battlefields", ContentTypeHandler(VLC->battlefieldsHandler.get(), "battlefield")));
  208. handlers.insert(std::make_pair("terrains", ContentTypeHandler(VLC->terrainTypeHandler.get(), "terrain")));
  209. handlers.insert(std::make_pair("rivers", ContentTypeHandler(VLC->riverTypeHandler.get(), "river")));
  210. handlers.insert(std::make_pair("roads", ContentTypeHandler(VLC->roadTypeHandler.get(), "road")));
  211. handlers.insert(std::make_pair("obstacles", ContentTypeHandler(VLC->obstacleHandler.get(), "obstacle")));
  212. handlers.insert(std::make_pair("biomes", ContentTypeHandler(VLC->biomeHandler.get(), "biome")));
  213. }
  214. bool CContentHandler::preloadModData(const std::string & modName, JsonNode modConfig, bool validate)
  215. {
  216. bool result = true;
  217. for(auto & handler : handlers)
  218. {
  219. result &= handler.second.preloadModData(modName, modConfig[handler.first].convertTo<std::vector<std::string> >(), validate);
  220. }
  221. return result;
  222. }
  223. bool CContentHandler::loadMod(const std::string & modName, bool validate)
  224. {
  225. bool result = true;
  226. for(auto & handler : handlers)
  227. {
  228. result &= handler.second.loadMod(modName, validate);
  229. }
  230. return result;
  231. }
  232. void CContentHandler::loadCustom()
  233. {
  234. for(auto & handler : handlers)
  235. {
  236. handler.second.loadCustom();
  237. }
  238. }
  239. void CContentHandler::afterLoadFinalization()
  240. {
  241. for(auto & handler : handlers)
  242. {
  243. handler.second.afterLoadFinalization();
  244. }
  245. }
  246. void CContentHandler::preloadData(CModInfo & mod)
  247. {
  248. bool validate = (mod.validation != CModInfo::PASSED);
  249. // print message in format [<8-symbols checksum>] <modname>
  250. auto & info = mod.getVerificationInfo();
  251. logMod->info("\t\t[%08x]%s", info.checksum, info.name);
  252. if (validate && mod.identifier != ModScope::scopeBuiltin())
  253. {
  254. if (!JsonUtils::validate(mod.config, "vcmi:mod", mod.identifier))
  255. mod.validation = CModInfo::FAILED;
  256. }
  257. if (!preloadModData(mod.identifier, mod.config, validate))
  258. mod.validation = CModInfo::FAILED;
  259. }
  260. void CContentHandler::load(CModInfo & mod)
  261. {
  262. bool validate = (mod.validation != CModInfo::PASSED);
  263. if (!loadMod(mod.identifier, validate))
  264. mod.validation = CModInfo::FAILED;
  265. if (validate)
  266. {
  267. if (mod.validation != CModInfo::FAILED)
  268. logMod->info("\t\t[DONE] %s", mod.getVerificationInfo().name);
  269. else
  270. logMod->error("\t\t[FAIL] %s", mod.getVerificationInfo().name);
  271. }
  272. else
  273. logMod->info("\t\t[SKIP] %s", mod.getVerificationInfo().name);
  274. }
  275. const ContentTypeHandler & CContentHandler::operator[](const std::string & name) const
  276. {
  277. return handlers.at(name);
  278. }
  279. VCMI_LIB_NAMESPACE_END