VCMI_Lib.cpp 7.5 KB

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