ContentTypeHandler.cpp 11 KB

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