VCMI_Lib.cpp 7.0 KB

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