CModHandler.cpp 7.9 KB

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