VCMI_Lib.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * VCMI_Lib.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 "VCMI_Lib.h"
  12. #include "CArtHandler.h"
  13. #include "CBonusTypeHandler.h"
  14. #include "CCreatureHandler.h"
  15. #include "CHeroHandler.h"
  16. #include "CTownHandler.h"
  17. #include "CConfigHandler.h"
  18. #include "RoadHandler.h"
  19. #include "RiverHandler.h"
  20. #include "TerrainHandler.h"
  21. #include "CBuildingHandler.h"
  22. #include "spells/CSpellHandler.h"
  23. #include "spells/effects/Registry.h"
  24. #include "CSkillHandler.h"
  25. #include "CGeneralTextHandler.h"
  26. #include "modding/CModHandler.h"
  27. #include "modding/CModInfo.h"
  28. #include "modding/IdentifierStorage.h"
  29. #include "modding/CModVersion.h"
  30. #include "IGameEventsReceiver.h"
  31. #include "CStopWatch.h"
  32. #include "VCMIDirs.h"
  33. #include "filesystem/Filesystem.h"
  34. #include "CConsoleHandler.h"
  35. #include "rmg/CRmgTemplateStorage.h"
  36. #include "mapObjectConstructors/CObjectClassesHandler.h"
  37. #include "mapObjects/CObjectHandler.h"
  38. #include "mapping/CMapEditManager.h"
  39. #include "ScriptHandler.h"
  40. #include "BattleFieldHandler.h"
  41. #include "ObstacleHandler.h"
  42. #include "GameSettings.h"
  43. VCMI_LIB_NAMESPACE_BEGIN
  44. LibClasses * VLC = nullptr;
  45. DLL_LINKAGE void preinitDLL(CConsoleHandler * Console, bool onlyEssential, bool extractArchives)
  46. {
  47. console = Console;
  48. VLC = new LibClasses();
  49. VLC->loadFilesystem(extractArchives);
  50. settings.init();
  51. VLC->loadModFilesystem(onlyEssential);
  52. }
  53. DLL_LINKAGE void loadDLLClasses(bool onlyEssential)
  54. {
  55. VLC->init(onlyEssential);
  56. }
  57. const ArtifactService * LibClasses::artifacts() const
  58. {
  59. return arth;
  60. }
  61. const CreatureService * LibClasses::creatures() const
  62. {
  63. return creh;
  64. }
  65. const FactionService * LibClasses::factions() const
  66. {
  67. return townh;
  68. }
  69. const HeroClassService * LibClasses::heroClasses() const
  70. {
  71. return &heroh->classes;
  72. }
  73. const HeroTypeService * LibClasses::heroTypes() const
  74. {
  75. return heroh;
  76. }
  77. #if SCRIPTING_ENABLED
  78. const scripting::Service * LibClasses::scripts() const
  79. {
  80. return scriptHandler;
  81. }
  82. #endif
  83. const spells::Service * LibClasses::spells() const
  84. {
  85. return spellh;
  86. }
  87. const SkillService * LibClasses::skills() const
  88. {
  89. return skillh;
  90. }
  91. const IBonusTypeHandler * LibClasses::getBth() const
  92. {
  93. return bth;
  94. }
  95. const CIdentifierStorage * LibClasses::identifiers() const
  96. {
  97. return &modh->getIdentifiers();
  98. }
  99. const spells::effects::Registry * LibClasses::spellEffects() const
  100. {
  101. return spells::effects::GlobalRegistry::get();
  102. }
  103. spells::effects::Registry * LibClasses::spellEffects()
  104. {
  105. return spells::effects::GlobalRegistry::get();
  106. }
  107. const BattleFieldService * LibClasses::battlefields() const
  108. {
  109. return battlefieldsHandler;
  110. }
  111. const ObstacleService * LibClasses::obstacles() const
  112. {
  113. return obstacleHandler;
  114. }
  115. const IGameSettings * LibClasses::settings() const
  116. {
  117. return settingsHandler;
  118. }
  119. void LibClasses::updateEntity(Metatype metatype, int32_t index, const JsonNode & data)
  120. {
  121. switch(metatype)
  122. {
  123. case Metatype::ARTIFACT:
  124. arth->updateEntity(index, data);
  125. break;
  126. case Metatype::CREATURE:
  127. creh->updateEntity(index, data);
  128. break;
  129. case Metatype::FACTION:
  130. townh->updateEntity(index, data);
  131. break;
  132. case Metatype::HERO_CLASS:
  133. heroh->classes.updateEntity(index, data);
  134. break;
  135. case Metatype::HERO_TYPE:
  136. heroh->updateEntity(index, data);
  137. break;
  138. case Metatype::SKILL:
  139. skillh->updateEntity(index, data);
  140. break;
  141. case Metatype::SPELL:
  142. spellh->updateEntity(index, data);
  143. break;
  144. default:
  145. logGlobal->error("Invalid Metatype id %d", static_cast<int32_t>(metatype));
  146. break;
  147. }
  148. }
  149. void LibClasses::loadFilesystem(bool extractArchives)
  150. {
  151. CStopWatch loadTime;
  152. CResourceHandler::initialize();
  153. logGlobal->info("\tInitialization: %d ms", loadTime.getDiff());
  154. CResourceHandler::load("config/filesystem.json", extractArchives);
  155. logGlobal->info("\tData loading: %d ms", loadTime.getDiff());
  156. }
  157. void LibClasses::loadModFilesystem(bool onlyEssential)
  158. {
  159. CStopWatch loadTime;
  160. modh = new CModHandler();
  161. modh->loadMods(onlyEssential);
  162. logGlobal->info("\tMod handler: %d ms", loadTime.getDiff());
  163. modh->loadModFilesystems();
  164. logGlobal->info("\tMod filesystems: %d ms", loadTime.getDiff());
  165. }
  166. static void logHandlerLoaded(const std::string & name, CStopWatch & timer)
  167. {
  168. logGlobal->info("\t\t %s handler: %d ms", name, timer.getDiff());
  169. }
  170. template <class Handler> void createHandler(Handler *&handler, const std::string &name, CStopWatch &timer)
  171. {
  172. handler = new Handler();
  173. logHandlerLoaded(name, timer);
  174. }
  175. void LibClasses::init(bool onlyEssential)
  176. {
  177. CStopWatch pomtime;
  178. CStopWatch totalTime;
  179. createHandler(settingsHandler, "Game Settings", pomtime);
  180. modh->initializeConfig();
  181. createHandler(generaltexth, "General text", pomtime);
  182. createHandler(bth, "Bonus type", pomtime);
  183. createHandler(roadTypeHandler, "Road", pomtime);
  184. createHandler(riverTypeHandler, "River", pomtime);
  185. createHandler(terrainTypeHandler, "Terrain", pomtime);
  186. createHandler(heroh, "Hero", pomtime);
  187. createHandler(arth, "Artifact", pomtime);
  188. createHandler(creh, "Creature", pomtime);
  189. createHandler(townh, "Town", pomtime);
  190. createHandler(objh, "Object", pomtime);
  191. createHandler(objtypeh, "Object types information", pomtime);
  192. createHandler(spellh, "Spell", pomtime);
  193. createHandler(skillh, "Skill", pomtime);
  194. createHandler(terviewh, "Terrain view pattern", pomtime);
  195. createHandler(tplh, "Template", pomtime); //templates need already resolved identifiers (refactor?)
  196. #if SCRIPTING_ENABLED
  197. createHandler(scriptHandler, "Script", pomtime);
  198. #endif
  199. createHandler(battlefieldsHandler, "Battlefields", pomtime);
  200. createHandler(obstacleHandler, "Obstacles", pomtime);
  201. logGlobal->info("\tInitializing handlers: %d ms", totalTime.getDiff());
  202. modh->load();
  203. modh->afterLoad(onlyEssential);
  204. //FIXME: make sure that everything is ok after game restart
  205. //TODO: This should be done every time mod config changes
  206. }
  207. void LibClasses::clear()
  208. {
  209. delete heroh;
  210. delete arth;
  211. delete creh;
  212. delete townh;
  213. delete objh;
  214. delete objtypeh;
  215. delete spellh;
  216. delete skillh;
  217. delete modh;
  218. delete bth;
  219. delete tplh;
  220. delete terviewh;
  221. #if SCRIPTING_ENABLED
  222. delete scriptHandler;
  223. #endif
  224. delete battlefieldsHandler;
  225. delete generaltexth;
  226. makeNull();
  227. }
  228. void LibClasses::makeNull()
  229. {
  230. generaltexth = nullptr;
  231. heroh = nullptr;
  232. arth = nullptr;
  233. creh = nullptr;
  234. townh = nullptr;
  235. objh = nullptr;
  236. objtypeh = nullptr;
  237. spellh = nullptr;
  238. skillh = nullptr;
  239. modh = nullptr;
  240. bth = nullptr;
  241. tplh = nullptr;
  242. terviewh = nullptr;
  243. #if SCRIPTING_ENABLED
  244. scriptHandler = nullptr;
  245. #endif
  246. battlefieldsHandler = nullptr;
  247. }
  248. LibClasses::LibClasses()
  249. {
  250. //init pointers to handlers
  251. makeNull();
  252. }
  253. void LibClasses::callWhenDeserializing()
  254. {
  255. //FIXME: check if any of these are needed
  256. //generaltexth = new CGeneralTextHandler();
  257. //generaltexth->load();
  258. //arth->load(true);
  259. //modh->recreateHandlers();
  260. //modh->loadConfigFromFile ("defaultMods"); //TODO: remember last saved config
  261. }
  262. #if SCRIPTING_ENABLED
  263. void LibClasses::scriptsLoaded()
  264. {
  265. scriptHandler->performRegistration(this);
  266. }
  267. #endif
  268. LibClasses::~LibClasses()
  269. {
  270. clear();
  271. }
  272. std::shared_ptr<CContentHandler> LibClasses::getContent() const
  273. {
  274. return modh->content;
  275. }
  276. void LibClasses::setContent(std::shared_ptr<CContentHandler> content)
  277. {
  278. modh->content = std::move(content);
  279. }
  280. VCMI_LIB_NAMESPACE_END