123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- /*
- * VCMI_Lib.cpp, part of VCMI engine
- *
- * Authors: listed in file AUTHORS in main folder
- *
- * License: GNU General Public License v2.0 or later
- * Full text of license available in license.txt file, in main folder
- *
- */
- #include "StdInc.h"
- #include "VCMI_Lib.h"
- #include "CArtHandler.h"
- #include "CBonusTypeHandler.h"
- #include "CCreatureHandler.h"
- #include "CHeroHandler.h"
- #include "CTownHandler.h"
- #include "CConfigHandler.h"
- #include "RoadHandler.h"
- #include "RiverHandler.h"
- #include "TerrainHandler.h"
- #include "CBuildingHandler.h"
- #include "spells/CSpellHandler.h"
- #include "spells/effects/Registry.h"
- #include "CSkillHandler.h"
- #include "CGeneralTextHandler.h"
- #include "modding/CModHandler.h"
- #include "modding/CModInfo.h"
- #include "modding/IdentifierStorage.h"
- #include "modding/CModVersion.h"
- #include "IGameEventsReceiver.h"
- #include "CStopWatch.h"
- #include "VCMIDirs.h"
- #include "filesystem/Filesystem.h"
- #include "CConsoleHandler.h"
- #include "rmg/CRmgTemplateStorage.h"
- #include "mapObjectConstructors/CObjectClassesHandler.h"
- #include "mapObjects/CObjectHandler.h"
- #include "mapping/CMapEditManager.h"
- #include "ScriptHandler.h"
- #include "BattleFieldHandler.h"
- #include "ObstacleHandler.h"
- #include "GameSettings.h"
- VCMI_LIB_NAMESPACE_BEGIN
- LibClasses * VLC = nullptr;
- DLL_LINKAGE void preinitDLL(CConsoleHandler * Console, bool onlyEssential, bool extractArchives)
- {
- console = Console;
- VLC = new LibClasses();
- VLC->loadFilesystem(extractArchives);
- settings.init();
- VLC->loadModFilesystem(onlyEssential);
- }
- DLL_LINKAGE void loadDLLClasses(bool onlyEssential)
- {
- VLC->init(onlyEssential);
- }
- const ArtifactService * LibClasses::artifacts() const
- {
- return arth;
- }
- const CreatureService * LibClasses::creatures() const
- {
- return creh;
- }
- const FactionService * LibClasses::factions() const
- {
- return townh;
- }
- const HeroClassService * LibClasses::heroClasses() const
- {
- return &heroh->classes;
- }
- const HeroTypeService * LibClasses::heroTypes() const
- {
- return heroh;
- }
- #if SCRIPTING_ENABLED
- const scripting::Service * LibClasses::scripts() const
- {
- return scriptHandler;
- }
- #endif
- const spells::Service * LibClasses::spells() const
- {
- return spellh;
- }
- const SkillService * LibClasses::skills() const
- {
- return skillh;
- }
- const IBonusTypeHandler * LibClasses::getBth() const
- {
- return bth;
- }
- const CIdentifierStorage * LibClasses::identifiers() const
- {
- return identifiersHandler;
- }
- const spells::effects::Registry * LibClasses::spellEffects() const
- {
- return spells::effects::GlobalRegistry::get();
- }
- spells::effects::Registry * LibClasses::spellEffects()
- {
- return spells::effects::GlobalRegistry::get();
- }
- const BattleFieldService * LibClasses::battlefields() const
- {
- return battlefieldsHandler;
- }
- const ObstacleService * LibClasses::obstacles() const
- {
- return obstacleHandler;
- }
- const IGameSettings * LibClasses::settings() const
- {
- return settingsHandler;
- }
- void LibClasses::updateEntity(Metatype metatype, int32_t index, const JsonNode & data)
- {
- switch(metatype)
- {
- case Metatype::ARTIFACT:
- arth->updateEntity(index, data);
- break;
- case Metatype::CREATURE:
- creh->updateEntity(index, data);
- break;
- case Metatype::FACTION:
- townh->updateEntity(index, data);
- break;
- case Metatype::HERO_CLASS:
- heroh->classes.updateEntity(index, data);
- break;
- case Metatype::HERO_TYPE:
- heroh->updateEntity(index, data);
- break;
- case Metatype::SKILL:
- skillh->updateEntity(index, data);
- break;
- case Metatype::SPELL:
- spellh->updateEntity(index, data);
- break;
- default:
- logGlobal->error("Invalid Metatype id %d", static_cast<int32_t>(metatype));
- break;
- }
- }
- void LibClasses::loadFilesystem(bool extractArchives)
- {
- CStopWatch loadTime;
- CResourceHandler::initialize();
- logGlobal->info("\tInitialization: %d ms", loadTime.getDiff());
- CResourceHandler::load("config/filesystem.json", extractArchives);
- logGlobal->info("\tData loading: %d ms", loadTime.getDiff());
- }
- void LibClasses::loadModFilesystem(bool onlyEssential)
- {
- CStopWatch loadTime;
- modh = new CModHandler();
- identifiersHandler = new CIdentifierStorage();
- modh->loadMods(onlyEssential);
- logGlobal->info("\tMod handler: %d ms", loadTime.getDiff());
- modh->loadModFilesystems();
- logGlobal->info("\tMod filesystems: %d ms", loadTime.getDiff());
- }
- static void logHandlerLoaded(const std::string & name, CStopWatch & timer)
- {
- logGlobal->info("\t\t %s handler: %d ms", name, timer.getDiff());
- }
- template <class Handler> void createHandler(Handler *&handler, const std::string &name, CStopWatch &timer)
- {
- handler = new Handler();
- logHandlerLoaded(name, timer);
- }
- void LibClasses::init(bool onlyEssential)
- {
- CStopWatch pomtime;
- CStopWatch totalTime;
- createHandler(settingsHandler, "Game Settings", pomtime);
- modh->initializeConfig();
- createHandler(generaltexth, "General text", pomtime);
- createHandler(bth, "Bonus type", pomtime);
- createHandler(roadTypeHandler, "Road", pomtime);
- createHandler(riverTypeHandler, "River", pomtime);
- createHandler(terrainTypeHandler, "Terrain", pomtime);
- createHandler(heroh, "Hero", pomtime);
- createHandler(arth, "Artifact", pomtime);
- createHandler(creh, "Creature", pomtime);
- createHandler(townh, "Town", pomtime);
- createHandler(objh, "Object", pomtime);
- createHandler(objtypeh, "Object types information", pomtime);
- createHandler(spellh, "Spell", pomtime);
- createHandler(skillh, "Skill", pomtime);
- createHandler(terviewh, "Terrain view pattern", pomtime);
- createHandler(tplh, "Template", pomtime); //templates need already resolved identifiers (refactor?)
- #if SCRIPTING_ENABLED
- createHandler(scriptHandler, "Script", pomtime);
- #endif
- createHandler(battlefieldsHandler, "Battlefields", pomtime);
- createHandler(obstacleHandler, "Obstacles", pomtime);
- logGlobal->info("\tInitializing handlers: %d ms", totalTime.getDiff());
- modh->load();
- modh->afterLoad(onlyEssential);
- }
- void LibClasses::clear()
- {
- delete heroh;
- delete arth;
- delete creh;
- delete townh;
- delete objh;
- delete objtypeh;
- delete spellh;
- delete skillh;
- delete modh;
- delete bth;
- delete tplh;
- delete terviewh;
- #if SCRIPTING_ENABLED
- delete scriptHandler;
- #endif
- delete battlefieldsHandler;
- delete generaltexth;
- delete identifiersHandler;
- makeNull();
- }
- void LibClasses::makeNull()
- {
- generaltexth = nullptr;
- heroh = nullptr;
- arth = nullptr;
- creh = nullptr;
- townh = nullptr;
- objh = nullptr;
- objtypeh = nullptr;
- spellh = nullptr;
- skillh = nullptr;
- modh = nullptr;
- bth = nullptr;
- tplh = nullptr;
- terviewh = nullptr;
- #if SCRIPTING_ENABLED
- scriptHandler = nullptr;
- #endif
- battlefieldsHandler = nullptr;
- identifiersHandler = nullptr;
- }
- LibClasses::LibClasses()
- {
- //init pointers to handlers
- makeNull();
- }
- void LibClasses::callWhenDeserializing()
- {
- //FIXME: check if any of these are needed
- //generaltexth = new CGeneralTextHandler();
- //generaltexth->load();
- //arth->load(true);
- //modh->recreateHandlers();
- //modh->loadConfigFromFile ("defaultMods"); //TODO: remember last saved config
- }
- #if SCRIPTING_ENABLED
- void LibClasses::scriptsLoaded()
- {
- scriptHandler->performRegistration(this);
- }
- #endif
- LibClasses::~LibClasses()
- {
- clear();
- }
- std::shared_ptr<CContentHandler> LibClasses::getContent() const
- {
- return modh->content;
- }
- void LibClasses::setContent(std::shared_ptr<CContentHandler> content)
- {
- modh->content = std::move(content);
- }
- VCMI_LIB_NAMESPACE_END
|