ContentTypeHandler.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 "../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. modInfo.modData[entry.first].swap(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. logMod->warn("Mod %s is attempting to inject object %s into mod %s! This may not be supported in future versions!", data.meta, name, modName);
  92. if (vstd::contains(data.Struct(), "index") && !data["index"].isNull())
  93. {
  94. if (modName != "core")
  95. logMod->warn("Mod %s is attempting to load original data! This should be reserved for built-in mod.", modName);
  96. // try to add H3 object data
  97. size_t index = static_cast<size_t>(data["index"].Float());
  98. if(originalData.size() > index)
  99. {
  100. logMod->trace("found original data in loadMod(%s) at index %d", name, index);
  101. JsonUtils::merge(originalData[index], data);
  102. std::swap(originalData[index], data);
  103. originalData[index].clear(); // do not use same data twice (same ID)
  104. }
  105. else
  106. {
  107. logMod->trace("no original data in loadMod(%s) at index %d", name, index);
  108. }
  109. performValidate(data, name);
  110. handler->loadObject(modName, name, data, index);
  111. }
  112. else
  113. {
  114. // normal new object
  115. logMod->trace("no index in loadMod(%s)", name);
  116. performValidate(data,name);
  117. handler->loadObject(modName, name, data);
  118. }
  119. }
  120. return result;
  121. }
  122. void ContentTypeHandler::loadCustom()
  123. {
  124. handler->loadCustom();
  125. }
  126. void ContentTypeHandler::afterLoadFinalization()
  127. {
  128. handler->afterLoadFinalization();
  129. }
  130. void CContentHandler::init()
  131. {
  132. handlers.insert(std::make_pair("heroClasses", ContentTypeHandler(&VLC->heroh->classes, "heroClass")));
  133. handlers.insert(std::make_pair("artifacts", ContentTypeHandler(VLC->arth, "artifact")));
  134. handlers.insert(std::make_pair("creatures", ContentTypeHandler(VLC->creh, "creature")));
  135. handlers.insert(std::make_pair("factions", ContentTypeHandler(VLC->townh, "faction")));
  136. handlers.insert(std::make_pair("objects", ContentTypeHandler(VLC->objtypeh, "object")));
  137. handlers.insert(std::make_pair("heroes", ContentTypeHandler(VLC->heroh, "hero")));
  138. handlers.insert(std::make_pair("spells", ContentTypeHandler(VLC->spellh, "spell")));
  139. handlers.insert(std::make_pair("skills", ContentTypeHandler(VLC->skillh, "skill")));
  140. handlers.insert(std::make_pair("templates", ContentTypeHandler(VLC->tplh, "template")));
  141. #if SCRIPTING_ENABLED
  142. handlers.insert(std::make_pair("scripts", ContentTypeHandler(VLC->scriptHandler, "script")));
  143. #endif
  144. handlers.insert(std::make_pair("battlefields", ContentTypeHandler(VLC->battlefieldsHandler, "battlefield")));
  145. handlers.insert(std::make_pair("terrains", ContentTypeHandler(VLC->terrainTypeHandler, "terrain")));
  146. handlers.insert(std::make_pair("rivers", ContentTypeHandler(VLC->riverTypeHandler, "river")));
  147. handlers.insert(std::make_pair("roads", ContentTypeHandler(VLC->roadTypeHandler, "road")));
  148. handlers.insert(std::make_pair("obstacles", ContentTypeHandler(VLC->obstacleHandler, "obstacle")));
  149. //TODO: any other types of moddables?
  150. }
  151. bool CContentHandler::preloadModData(const std::string & modName, JsonNode modConfig, bool validate)
  152. {
  153. bool result = true;
  154. for(auto & handler : handlers)
  155. {
  156. result &= handler.second.preloadModData(modName, modConfig[handler.first].convertTo<std::vector<std::string> >(), validate);
  157. }
  158. return result;
  159. }
  160. bool CContentHandler::loadMod(const std::string & modName, bool validate)
  161. {
  162. bool result = true;
  163. for(auto & handler : handlers)
  164. {
  165. result &= handler.second.loadMod(modName, validate);
  166. }
  167. return result;
  168. }
  169. void CContentHandler::loadCustom()
  170. {
  171. for(auto & handler : handlers)
  172. {
  173. handler.second.loadCustom();
  174. }
  175. }
  176. void CContentHandler::afterLoadFinalization()
  177. {
  178. for(auto & handler : handlers)
  179. {
  180. handler.second.afterLoadFinalization();
  181. }
  182. }
  183. void CContentHandler::preloadData(CModInfo & mod)
  184. {
  185. bool validate = (mod.validation != CModInfo::PASSED);
  186. // print message in format [<8-symbols checksum>] <modname>
  187. logMod->info("\t\t[%08x]%s", mod.checksum, mod.name);
  188. if (validate && mod.identifier != ModScope::scopeBuiltin())
  189. {
  190. if (!JsonUtils::validate(mod.config, "vcmi:mod", mod.identifier))
  191. mod.validation = CModInfo::FAILED;
  192. }
  193. if (!preloadModData(mod.identifier, mod.config, validate))
  194. mod.validation = CModInfo::FAILED;
  195. }
  196. void CContentHandler::load(CModInfo & mod)
  197. {
  198. bool validate = (mod.validation != CModInfo::PASSED);
  199. if (!loadMod(mod.identifier, validate))
  200. mod.validation = CModInfo::FAILED;
  201. if (validate)
  202. {
  203. if (mod.validation != CModInfo::FAILED)
  204. logMod->info("\t\t[DONE] %s", mod.name);
  205. else
  206. logMod->error("\t\t[FAIL] %s", mod.name);
  207. }
  208. else
  209. logMod->info("\t\t[SKIP] %s", mod.name);
  210. }
  211. const ContentTypeHandler & CContentHandler::operator[](const std::string & name) const
  212. {
  213. return handlers.at(name);
  214. }
  215. VCMI_LIB_NAMESPACE_END