VCMI_Lib.cpp 6.5 KB

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