VCMI_Lib.cpp 6.9 KB

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