ContentTypeHandler.cpp 11 KB

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