VCMI_Lib.cpp 7.2 KB

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