ContentTypeHandler.cpp 11 KB

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