VCMI_Lib.cpp 7.2 KB

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