VCMI_Lib.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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(generaltexth, "General text", pomtime);
  168. createHandler(heroh, "Hero", pomtime);
  169. createHandler(arth, "Artifact", pomtime);
  170. createHandler(creh, "Creature", pomtime);
  171. createHandler(townh, "Town", pomtime);
  172. createHandler(objh, "Object", pomtime);
  173. createHandler(objtypeh, "Object types information", pomtime);
  174. createHandler(spellh, "Spell", pomtime);
  175. createHandler(skillh, "Skill", pomtime);
  176. createHandler(terviewh, "Terrain view pattern", pomtime);
  177. createHandler(tplh, "Template", pomtime); //templates need already resolved identifiers (refactor?)
  178. #if SCRIPTING_ENABLED
  179. createHandler(scriptHandler, "Script", pomtime);
  180. #endif
  181. createHandler(battlefieldsHandler, "Battlefields", pomtime);
  182. createHandler(obstacleHandler, "Obstacles", pomtime);
  183. logGlobal->info("\tInitializing handlers: %d ms", totalTime.getDiff());
  184. modh->load();
  185. modh->afterLoad(onlyEssential);
  186. //FIXME: make sure that everything is ok after game restart
  187. //TODO: This should be done every time mod config changes
  188. }
  189. void LibClasses::clear()
  190. {
  191. delete generaltexth;
  192. delete heroh;
  193. delete arth;
  194. delete creh;
  195. delete townh;
  196. delete objh;
  197. delete objtypeh;
  198. delete spellh;
  199. delete skillh;
  200. delete modh;
  201. delete bth;
  202. delete tplh;
  203. delete terviewh;
  204. #if SCRIPTING_ENABLED
  205. delete scriptHandler;
  206. #endif
  207. delete battlefieldsHandler;
  208. makeNull();
  209. }
  210. void LibClasses::makeNull()
  211. {
  212. generaltexth = nullptr;
  213. heroh = nullptr;
  214. arth = nullptr;
  215. creh = nullptr;
  216. townh = nullptr;
  217. objh = nullptr;
  218. objtypeh = nullptr;
  219. spellh = nullptr;
  220. skillh = nullptr;
  221. modh = nullptr;
  222. bth = nullptr;
  223. tplh = nullptr;
  224. terviewh = nullptr;
  225. #if SCRIPTING_ENABLED
  226. scriptHandler = nullptr;
  227. #endif
  228. battlefieldsHandler = nullptr;
  229. }
  230. LibClasses::LibClasses()
  231. {
  232. IS_AI_ENABLED = false;
  233. //init pointers to handlers
  234. makeNull();
  235. }
  236. void LibClasses::callWhenDeserializing()
  237. {
  238. //FIXME: check if any of these are needed
  239. //generaltexth = new CGeneralTextHandler();
  240. //generaltexth->load();
  241. //arth->load(true);
  242. //modh->recreateHandlers();
  243. //modh->loadConfigFromFile ("defaultMods"); //TODO: remember last saved config
  244. }
  245. #if SCRIPTING_ENABLED
  246. void LibClasses::scriptsLoaded()
  247. {
  248. scriptHandler->performRegistration(this);
  249. }
  250. #endif
  251. LibClasses::~LibClasses()
  252. {
  253. clear();
  254. }
  255. std::shared_ptr<CContentHandler> LibClasses::getContent() const
  256. {
  257. return modh->content;
  258. }
  259. void LibClasses::setContent(std::shared_ptr<CContentHandler> content)
  260. {
  261. modh->content = content;
  262. }