ContentTypeHandler.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 "../CGeneralTextHandler.h"
  19. #include "../CHeroHandler.h"
  20. #include "../CSkillHandler.h"
  21. #include "../CStopWatch.h"
  22. #include "../CTownHandler.h"
  23. #include "../GameSettings.h"
  24. #include "../IHandlerBase.h"
  25. #include "../Languages.h"
  26. #include "../ObstacleHandler.h"
  27. #include "../RiverHandler.h"
  28. #include "../RoadHandler.h"
  29. #include "../ScriptHandler.h"
  30. #include "../constants/StringConstants.h"
  31. #include "../TerrainHandler.h"
  32. #include "../mapObjectConstructors/CObjectClassesHandler.h"
  33. #include "../rmg/CRmgTemplateStorage.h"
  34. #include "../spells/CSpellHandler.h"
  35. VCMI_LIB_NAMESPACE_BEGIN
  36. ContentTypeHandler::ContentTypeHandler(IHandlerBase * handler, const std::string & objectName):
  37. handler(handler),
  38. objectName(objectName),
  39. originalData(handler->loadLegacyData())
  40. {
  41. for(auto & node : originalData)
  42. {
  43. node.setMeta(ModScope::scopeBuiltin());
  44. }
  45. }
  46. bool ContentTypeHandler::preloadModData(const std::string & modName, const std::vector<std::string> & fileList, bool validate)
  47. {
  48. bool result = false;
  49. JsonNode data = JsonUtils::assembleFromFiles(fileList, result);
  50. data.setMeta(modName);
  51. ModInfo & modInfo = modData[modName];
  52. for(auto entry : data.Struct())
  53. {
  54. size_t colon = entry.first.find(':');
  55. if (colon == std::string::npos)
  56. {
  57. // normal object, local to this mod
  58. std::swap(modInfo.modData[entry.first], entry.second);
  59. }
  60. else
  61. {
  62. std::string remoteName = entry.first.substr(0, colon);
  63. std::string objectName = entry.first.substr(colon + 1);
  64. // patching this mod? Send warning and continue - this situation can be handled normally
  65. if (remoteName == modName)
  66. logMod->warn("Redundant namespace definition for %s", objectName);
  67. logMod->trace("Patching object %s (%s) from %s", objectName, remoteName, modName);
  68. JsonNode & remoteConf = modData[remoteName].patches[objectName];
  69. JsonUtils::merge(remoteConf, entry.second);
  70. }
  71. }
  72. return result;
  73. }
  74. bool ContentTypeHandler::loadMod(const std::string & modName, bool validate)
  75. {
  76. ModInfo & modInfo = modData[modName];
  77. bool result = true;
  78. auto performValidate = [&,this](JsonNode & data, const std::string & name){
  79. handler->beforeValidate(data);
  80. if (validate)
  81. result &= JsonUtils::validate(data, "vcmi:" + objectName, name);
  82. };
  83. // apply patches
  84. if (!modInfo.patches.isNull())
  85. JsonUtils::merge(modInfo.modData, modInfo.patches);
  86. for(auto & entry : modInfo.modData.Struct())
  87. {
  88. const std::string & name = entry.first;
  89. JsonNode & data = entry.second;
  90. if (data.meta != modName)
  91. {
  92. // in this scenario, entire object record comes from another mod
  93. // normally, this is used to "patch" object from another mod (which is legal)
  94. // however in this case there is no object to patch. This might happen in such cases:
  95. // - another mod attempts to add object into this mod (technically can be supported, but might lead to weird edge cases)
  96. // - another mod attempts to edit object from this mod that no longer exist - DANGER since such patch likely has very incomplete data
  97. // so emit warning and skip such case
  98. logMod->warn("Mod %s attempts to edit object %s from mod %s but no such object exist!", data.meta, name, modName);
  99. continue;
  100. }
  101. if (vstd::contains(data.Struct(), "index") && !data["index"].isNull())
  102. {
  103. if (modName != "core")
  104. logMod->warn("Mod %s is attempting to load original data! This should be reserved for built-in mod.", modName);
  105. // try to add H3 object data
  106. size_t index = static_cast<size_t>(data["index"].Float());
  107. if(originalData.size() > index)
  108. {
  109. logMod->trace("found original data in loadMod(%s) at index %d", name, index);
  110. JsonUtils::merge(originalData[index], data);
  111. std::swap(originalData[index], data);
  112. originalData[index].clear(); // do not use same data twice (same ID)
  113. }
  114. else
  115. {
  116. logMod->trace("no original data in loadMod(%s) at index %d", name, index);
  117. }
  118. performValidate(data, name);
  119. handler->loadObject(modName, name, data, index);
  120. }
  121. else
  122. {
  123. // normal new object
  124. logMod->trace("no index in loadMod(%s)", name);
  125. performValidate(data,name);
  126. handler->loadObject(modName, name, data);
  127. }
  128. }
  129. return result;
  130. }
  131. void ContentTypeHandler::loadCustom()
  132. {
  133. handler->loadCustom();
  134. }
  135. void ContentTypeHandler::afterLoadFinalization()
  136. {
  137. handler->afterLoadFinalization();
  138. }
  139. void CContentHandler::init()
  140. {
  141. handlers.insert(std::make_pair("heroClasses", ContentTypeHandler(&VLC->heroh->classes, "heroClass")));
  142. handlers.insert(std::make_pair("artifacts", ContentTypeHandler(VLC->arth, "artifact")));
  143. handlers.insert(std::make_pair("creatures", ContentTypeHandler(VLC->creh, "creature")));
  144. handlers.insert(std::make_pair("factions", ContentTypeHandler(VLC->townh, "faction")));
  145. handlers.insert(std::make_pair("objects", ContentTypeHandler(VLC->objtypeh, "object")));
  146. handlers.insert(std::make_pair("heroes", ContentTypeHandler(VLC->heroh, "hero")));
  147. handlers.insert(std::make_pair("spells", ContentTypeHandler(VLC->spellh, "spell")));
  148. handlers.insert(std::make_pair("skills", ContentTypeHandler(VLC->skillh, "skill")));
  149. handlers.insert(std::make_pair("templates", ContentTypeHandler(VLC->tplh, "template")));
  150. #if SCRIPTING_ENABLED
  151. handlers.insert(std::make_pair("scripts", ContentTypeHandler(VLC->scriptHandler, "script")));
  152. #endif
  153. handlers.insert(std::make_pair("battlefields", ContentTypeHandler(VLC->battlefieldsHandler, "battlefield")));
  154. handlers.insert(std::make_pair("terrains", ContentTypeHandler(VLC->terrainTypeHandler, "terrain")));
  155. handlers.insert(std::make_pair("rivers", ContentTypeHandler(VLC->riverTypeHandler, "river")));
  156. handlers.insert(std::make_pair("roads", ContentTypeHandler(VLC->roadTypeHandler, "road")));
  157. handlers.insert(std::make_pair("obstacles", ContentTypeHandler(VLC->obstacleHandler, "obstacle")));
  158. //TODO: any other types of moddables?
  159. }
  160. bool CContentHandler::preloadModData(const std::string & modName, JsonNode modConfig, bool validate)
  161. {
  162. bool result = true;
  163. for(auto & handler : handlers)
  164. {
  165. result &= handler.second.preloadModData(modName, modConfig[handler.first].convertTo<std::vector<std::string> >(), validate);
  166. }
  167. return result;
  168. }
  169. bool CContentHandler::loadMod(const std::string & modName, bool validate)
  170. {
  171. bool result = true;
  172. for(auto & handler : handlers)
  173. {
  174. result &= handler.second.loadMod(modName, validate);
  175. }
  176. return result;
  177. }
  178. void CContentHandler::loadCustom()
  179. {
  180. for(auto & handler : handlers)
  181. {
  182. handler.second.loadCustom();
  183. }
  184. }
  185. void CContentHandler::afterLoadFinalization()
  186. {
  187. for(auto & handler : handlers)
  188. {
  189. handler.second.afterLoadFinalization();
  190. }
  191. }
  192. void CContentHandler::preloadData(CModInfo & mod)
  193. {
  194. bool validate = (mod.validation != CModInfo::PASSED);
  195. // print message in format [<8-symbols checksum>] <modname>
  196. auto & info = mod.getVerificationInfo();
  197. logMod->info("\t\t[%08x]%s", info.checksum, info.name);
  198. if (validate && mod.identifier != ModScope::scopeBuiltin())
  199. {
  200. if (!JsonUtils::validate(mod.config, "vcmi:mod", mod.identifier))
  201. mod.validation = CModInfo::FAILED;
  202. }
  203. if (!preloadModData(mod.identifier, mod.config, validate))
  204. mod.validation = CModInfo::FAILED;
  205. }
  206. void CContentHandler::load(CModInfo & mod)
  207. {
  208. bool validate = (mod.validation != CModInfo::PASSED);
  209. if (!loadMod(mod.identifier, validate))
  210. mod.validation = CModInfo::FAILED;
  211. if (validate)
  212. {
  213. if (mod.validation != CModInfo::FAILED)
  214. logMod->info("\t\t[DONE] %s", mod.getVerificationInfo().name);
  215. else
  216. logMod->error("\t\t[FAIL] %s", mod.getVerificationInfo().name);
  217. }
  218. else
  219. logMod->info("\t\t[SKIP] %s", mod.getVerificationInfo().name);
  220. }
  221. const ContentTypeHandler & CContentHandler::operator[](const std::string & name) const
  222. {
  223. return handlers.at(name);
  224. }
  225. VCMI_LIB_NAMESPACE_END