ContentTypeHandler.cpp 10 KB

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