ContentTypeHandler.cpp 10 KB

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