CModHandler.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #include "StdInc.h"
  2. #include "CModHandler.h"
  3. #include "CDefObjInfoHandler.h"
  4. #include "JsonNode.h"
  5. #include "Filesystem/CResourceLoader.h"
  6. #include "Filesystem/ISimpleResourceLoader.h"
  7. #include "CCreatureHandler.h"
  8. #include "CArtHandler.h"
  9. #include "CTownHandler.h"
  10. #include "CHeroHandler.h"
  11. /*
  12. * CModHandler.h, part of VCMI engine
  13. *
  14. * Authors: listed in file AUTHORS in main folder
  15. *
  16. * License: GNU General Public License v2.0 or later
  17. * Full text of license available in license.txt file, in main folder
  18. *
  19. */
  20. class CArtHandler;
  21. class CHeroHandler;
  22. class CCreatureHandler;
  23. class CSpellHandler;
  24. class CBuildingHandler;
  25. class CObjectHandler;
  26. class CDefObjInfoHandler;
  27. class CTownHandler;
  28. class CGeneralTextHandler;
  29. class ResourceLocator;
  30. void CIdentifierStorage::requestIdentifier(std::string name, const boost::function<void(si32)> & callback)
  31. {
  32. auto iter = registeredObjects.find(name);
  33. if (iter != registeredObjects.end())
  34. callback(iter->second); //already registered - trigger callback immediately
  35. else
  36. missingObjects[name].push_back(callback); // queue callback
  37. }
  38. void CIdentifierStorage::registerObject(std::string name, si32 identifier)
  39. {
  40. // do not allow to register same object twice
  41. assert(registeredObjects.find(name) == registeredObjects.end());
  42. registeredObjects[name] = identifier;
  43. auto iter = missingObjects.find(name);
  44. if (iter != missingObjects.end())
  45. {
  46. //call all awaiting callbacks
  47. BOOST_FOREACH(auto function, iter->second)
  48. {
  49. function(identifier);
  50. }
  51. missingObjects.erase(iter);
  52. }
  53. }
  54. void CIdentifierStorage::finalize() const
  55. {
  56. // print list of missing objects and crash
  57. // in future should try to do some cleanup (like returning all id's as 0)
  58. BOOST_FOREACH(auto object, missingObjects)
  59. {
  60. tlog1 << "Error: object " << object.first << " was not found!\n";
  61. }
  62. assert(missingObjects.empty());
  63. }
  64. CModHandler::CModHandler()
  65. {
  66. VLC->modh = this;
  67. loadConfigFromFile ("defaultMods");
  68. }
  69. void CModHandler::loadConfigFromFile (std::string name)
  70. {
  71. const JsonNode config(ResourceID("config/" + name + ".json"));
  72. const JsonNode & hardcodedFeatures = config["hardcodedFeatures"];
  73. settings.CREEP_SIZE = hardcodedFeatures["CREEP_SIZE"].Float();
  74. settings.WEEKLY_GROWTH = hardcodedFeatures["WEEKLY_GROWTH_PERCENT"].Float();
  75. settings.NEUTRAL_STACK_EXP = hardcodedFeatures["NEUTRAL_STACK_EXP_DAILY"].Float();
  76. settings.MAX_BUILDING_PER_TURN = hardcodedFeatures["MAX_BUILDING_PER_TURN"].Float();
  77. settings.DWELLINGS_ACCUMULATE_CREATURES = hardcodedFeatures["DWELLINGS_ACCUMULATE_CREATURES"].Bool();
  78. settings.ALL_CREATURES_GET_DOUBLE_MONTHS = hardcodedFeatures["ALL_CREATURES_GET_DOUBLE_MONTHS"].Bool();
  79. const JsonNode & gameModules = config["modules"];
  80. modules.STACK_EXP = gameModules["STACK_EXPERIENCE"].Bool();
  81. modules.STACK_ARTIFACT = gameModules["STACK_ARTIFACTS"].Bool();
  82. modules.COMMANDERS = gameModules["COMMANDERS"].Bool();
  83. modules.MITHRIL = gameModules["MITHRIL"].Bool();
  84. }
  85. void CModHandler::initialize(std::vector<std::string> availableMods)
  86. {
  87. BOOST_FOREACH(std::string &name, availableMods)
  88. {
  89. std::string modFileName = "mods/" + name + "/mod.json";
  90. if (CResourceHandler::get()->existsResource(ResourceID(modFileName)))
  91. {
  92. const JsonNode config = JsonNode(ResourceID(modFileName));
  93. if (!config.isNull())
  94. {
  95. allMods[name].identifier = name;
  96. allMods[name].name = config["name"].String();
  97. allMods[name].description = config["description"].String();
  98. allMods[name].loadPriority = config["priority"].Float();
  99. activeMods.push_back(name);
  100. tlog1 << "\t\tMod ";
  101. tlog2 << allMods[name].name;
  102. tlog1 << " enabled\n";
  103. }
  104. }
  105. else
  106. tlog1 << "\t\t Directory " << name << " does not contains VCMI mod\n";
  107. }
  108. std::sort(activeMods.begin(), activeMods.end(), [&](std::string a, std::string b)
  109. {
  110. return allMods[a].loadPriority < allMods[b].loadPriority;
  111. });
  112. }
  113. std::vector<std::string> CModHandler::getActiveMods()
  114. {
  115. return activeMods;
  116. }
  117. template<typename Handler>
  118. void handleData(Handler handler, const JsonNode & config)
  119. {
  120. handler->load(JsonUtils::assembleFromFiles(config.convertTo<std::vector<std::string> >()));
  121. }
  122. void CModHandler::loadActiveMods()
  123. {
  124. BOOST_FOREACH(std::string & modName, activeMods)
  125. {
  126. std::string modFileName = "mods/" + modName + "/mod.json";
  127. const JsonNode config = JsonNode(ResourceID(modFileName));
  128. handleData(VLC->townh, config["factions"]);
  129. handleData(VLC->creh, config["creatures"]);
  130. handleData(VLC->arth, config["artifacts"]);
  131. handleData(&VLC->heroh->classes, config["heroClasses"]);
  132. handleData(VLC->heroh, config["heroes"]);
  133. }
  134. VLC->creh->buildBonusTreeForTiers(); //do that after all new creatures are loaded
  135. identifiers.finalize();
  136. }
  137. void CModHandler::reload()
  138. {
  139. {
  140. //recreate adventure map defs
  141. assert(!VLC->dobjinfo->gobjs[Obj::MONSTER].empty()); //make sure that at least some def info was found
  142. const CGDefInfo * baseInfo = VLC->dobjinfo->gobjs[Obj::MONSTER].begin()->second;
  143. BOOST_FOREACH(auto & crea, VLC->creh->creatures)
  144. {
  145. if (!vstd::contains(VLC->dobjinfo->gobjs[Obj::MONSTER], crea->idNumber)) // no obj info for this type
  146. {
  147. CGDefInfo * info = new CGDefInfo(*baseInfo);
  148. info->subid = crea->idNumber;
  149. info->name = crea->advMapDef;
  150. VLC->dobjinfo->gobjs[Obj::MONSTER][crea->idNumber] = info;
  151. }
  152. }
  153. }
  154. {
  155. assert(!VLC->dobjinfo->gobjs[Obj::ARTIFACT].empty());
  156. const CGDefInfo * baseInfo = VLC->dobjinfo->gobjs[Obj::ARTIFACT].begin()->second;
  157. BOOST_FOREACH(auto & art, VLC->arth->artifacts)
  158. {
  159. if (!vstd::contains(VLC->dobjinfo->gobjs[Obj::ARTIFACT], art->id)) // no obj info for this type
  160. {
  161. CGDefInfo * info = new CGDefInfo(*baseInfo);
  162. info->subid = art->id;
  163. info->name = art->advMapDef;
  164. VLC->dobjinfo->gobjs[Obj::ARTIFACT][art->id] = info;
  165. }
  166. }
  167. }
  168. {
  169. assert(!VLC->dobjinfo->gobjs[Obj::TOWN].empty()); //make sure that at least some def info was found
  170. const CGDefInfo * baseInfo = VLC->dobjinfo->gobjs[Obj::TOWN].begin()->second;
  171. auto & townInfos = VLC->dobjinfo->gobjs[Obj::TOWN];
  172. BOOST_FOREACH(auto & town, VLC->townh->towns)
  173. {
  174. auto & cientInfo = town.second.clientInfo;
  175. if (!vstd::contains(VLC->dobjinfo->gobjs[Obj::TOWN], town.first)) // no obj info for this type
  176. {
  177. CGDefInfo * info = new CGDefInfo(*baseInfo);
  178. info->subid = town.first;
  179. townInfos[town.first] = info;
  180. }
  181. townInfos[town.first]->name = cientInfo.advMapCastle;
  182. VLC->dobjinfo->villages[town.first] = new CGDefInfo(*townInfos[town.first]);
  183. VLC->dobjinfo->villages[town.first]->name = cientInfo.advMapVillage;
  184. VLC->dobjinfo->capitols[town.first] = new CGDefInfo(*townInfos[town.first]);
  185. VLC->dobjinfo->capitols[town.first]->name = cientInfo.advMapCapitol;
  186. }
  187. }
  188. }