VCMI_Lib.cpp 6.5 KB

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