VCMI_Lib.cpp 7.1 KB

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