CModHandler.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. /*
  8. * CModHandler.h, part of VCMI engine
  9. *
  10. * Authors: listed in file AUTHORS in main folder
  11. *
  12. * License: GNU General Public License v2.0 or later
  13. * Full text of license available in license.txt file, in main folder
  14. *
  15. */
  16. class CArtHandler;
  17. class CHeroHandler;
  18. class CCreatureHandler;
  19. class CSpellHandler;
  20. class CBuildingHandler;
  21. class CObjectHandler;
  22. class CDefObjInfoHandler;
  23. class CTownHandler;
  24. class CGeneralTextHandler;
  25. class ResourceLocator;
  26. CModHandler::CModHandler()
  27. {
  28. VLC->modh = this;
  29. loadConfigFromFile ("defaultMods");
  30. findAvailableMods();
  31. //CResourceHandler::loadModsFilesystems(); //scan for all mods
  32. //TODO: mod filesystem is already initialized at LibClasses launch
  33. //TODO: load default (last?) config
  34. }
  35. void CModHandler::loadConfigFromFile (std::string name)
  36. {
  37. const JsonNode config(ResourceID("config/" + name + ".json"));
  38. const JsonNode & hardcodedFeatures = config["hardcodedFeatures"];
  39. settings.CREEP_SIZE = hardcodedFeatures["CREEP_SIZE"].Float();
  40. settings.WEEKLY_GROWTH = hardcodedFeatures["WEEKLY_GROWTH_PERCENT"].Float();
  41. settings.NEUTRAL_STACK_EXP = hardcodedFeatures["NEUTRAL_STACK_EXP_DAILY"].Float();
  42. settings.MAX_BUILDING_PER_TURN = hardcodedFeatures["MAX_BUILDING_PER_TURN"].Float();
  43. settings.DWELLINGS_ACCUMULATE_CREATURES = hardcodedFeatures["DWELLINGS_ACCUMULATE_CREATURES"].Bool();
  44. settings.ALL_CREATURES_GET_DOUBLE_MONTHS = hardcodedFeatures["ALL_CREATURES_GET_DOUBLE_MONTHS"].Bool();
  45. const JsonNode & gameModules = config["modules"];
  46. modules.STACK_EXP = gameModules["STACK_EXPERIENCE"].Bool();
  47. modules.STACK_ARTIFACT = gameModules["STACK_ARTIFACTS"].Bool();
  48. modules.COMMANDERS = gameModules["COMMANDERS"].Bool();
  49. modules.MITHRIL = gameModules["MITHRIL"].Bool();
  50. //TODO: load only mods from the list
  51. }
  52. void CModHandler::saveConfigToFile (std::string name)
  53. {
  54. //JsonNode savedConf = config;
  55. //JsonNode schema(ResourceID("config/defaultSettings.json"));
  56. //savedConf.Struct().erase("session");
  57. //savedConf.minimize(schema);
  58. CResourceHandler::get()->createResource("config/" + name +".json");
  59. std::ofstream file(CResourceHandler::get()->getResourceName(ResourceID("config/" + name +".json")), std::ofstream::trunc);
  60. //file << savedConf;
  61. }
  62. void CModHandler::findAvailableMods()
  63. {
  64. //TODO: read mods from Mods/ folder
  65. auto & configList = CResourceHandler::get()->getResourcesWithName (ResourceID("CONFIG/mod.json"));
  66. BOOST_FOREACH(auto & entry, configList)
  67. {
  68. auto stream = entry.getLoader()->load (entry.getResourceName());
  69. std::unique_ptr<ui8[]> textData (new ui8[stream->getSize()]);
  70. stream->read (textData.get(), stream->getSize());
  71. tlog3 << "\t\tFound mod file: " << entry.getResourceName() << "\n";
  72. allMods[allMods.size()].config.reset(new JsonNode((char*)textData.get(), stream->getSize()));
  73. }
  74. }
  75. void CModHandler::loadActiveMods()
  76. {
  77. BOOST_FOREACH(auto & mod, allMods)
  78. {
  79. const JsonNode & config = *mod.second.config;
  80. VLC->townh->load(config["factions"]);
  81. VLC->creh->load(config["creatures"]);
  82. }
  83. VLC->creh->buildBonusTreeForTiers(); //do that after all new creatures are loaded
  84. }
  85. void CModHandler::reload()
  86. {
  87. {
  88. assert(!VLC->dobjinfo->gobjs[Obj::MONSTER].empty()); //make sure that at least some def info was found
  89. const CGDefInfo * baseInfo = VLC->dobjinfo->gobjs[Obj::MONSTER].begin()->second;
  90. BOOST_FOREACH(auto & crea, VLC->creh->creatures)
  91. {
  92. if (!vstd::contains(VLC->dobjinfo->gobjs[Obj::MONSTER], crea->idNumber)) // no obj info for this type
  93. {
  94. CGDefInfo * info = new CGDefInfo(*baseInfo);
  95. info->subid = crea->idNumber;
  96. info->name = crea->advMapDef;
  97. VLC->dobjinfo->gobjs[Obj::MONSTER][crea->idNumber] = info;
  98. }
  99. }
  100. }
  101. {
  102. assert(!VLC->dobjinfo->gobjs[Obj::TOWN].empty()); //make sure that at least some def info was found
  103. const CGDefInfo * baseInfo = VLC->dobjinfo->gobjs[Obj::TOWN].begin()->second;
  104. auto & townInfos = VLC->dobjinfo->gobjs[Obj::TOWN];
  105. BOOST_FOREACH(auto & town, VLC->townh->towns)
  106. {
  107. auto & cientInfo = town.second.clientInfo;
  108. if (!vstd::contains(VLC->dobjinfo->gobjs[Obj::TOWN], town.first)) // no obj info for this type
  109. {
  110. CGDefInfo * info = new CGDefInfo(*baseInfo);
  111. info->subid = town.first;
  112. townInfos[town.first] = info;
  113. }
  114. townInfos[town.first]->name = cientInfo.advMapCastle;
  115. VLC->dobjinfo->villages[town.first] = new CGDefInfo(*townInfos[town.first]);
  116. VLC->dobjinfo->villages[town.first]->name = cientInfo.advMapVillage;
  117. VLC->dobjinfo->capitols[town.first] = new CGDefInfo(*townInfos[town.first]);
  118. VLC->dobjinfo->capitols[town.first]->name = cientInfo.advMapCapitol;
  119. }
  120. }
  121. }