1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-07-17 01:32:21 +02:00

Simplify library initialization

This commit is contained in:
Ivan Savenko
2025-03-03 17:20:34 +00:00
parent f7305fd1c2
commit 222b73bbcd
8 changed files with 65 additions and 83 deletions

View File

@ -78,10 +78,11 @@ static CBasicLogConfigurator *logConfig;
static void init() static void init()
{ {
CStopWatch tmh;
try try
{ {
loadDLLClasses(); CStopWatch tmh;
LIBRARY->initializeLibrary();
logGlobal->info("Initializing VCMI_Lib: %d ms", tmh.getDiff());
} }
catch (const DataLoadingException & e) catch (const DataLoadingException & e)
{ {
@ -89,8 +90,6 @@ static void init()
return; return;
} }
logGlobal->info("Initializing VCMI_Lib: %d ms", tmh.getDiff());
// Debug code to load all maps on start // Debug code to load all maps on start
//ClientCommandManager commandController; //ClientCommandManager commandController;
//commandController.processCommand("translate maps", false); //commandController.processCommand("translate maps", false);
@ -241,7 +240,8 @@ int main(int argc, char * argv[])
// Init filesystem and settings // Init filesystem and settings
try try
{ {
preinitDLL(false); LIBRARY = new GameLibrary;
LIBRARY->initializeFilesystem(false);
} }
catch (const DataLoadingException & e) catch (const DataLoadingException & e)
{ {

View File

@ -46,20 +46,7 @@ VCMI_LIB_NAMESPACE_BEGIN
GameLibrary * LIBRARY = nullptr; GameLibrary * LIBRARY = nullptr;
DLL_LINKAGE void preinitDLL(bool extractArchives)
{
LIBRARY = new GameLibrary();
LIBRARY->loadFilesystem(extractArchives);
settings.init("config/settings.json", "vcmi:settings");
persistentStorage.init("config/persistentStorage.json", "");
LIBRARY->loadModFilesystem();
}
DLL_LINKAGE void loadDLLClasses(bool onlyEssential)
{
LIBRARY->init(onlyEssential);
}
const ArtifactService * GameLibrary::artifacts() const const ArtifactService * GameLibrary::artifacts() const
{ {
@ -160,12 +147,21 @@ void GameLibrary::loadModFilesystem()
logGlobal->info("\tMod filesystems: %d ms", loadTime.getDiff()); logGlobal->info("\tMod filesystems: %d ms", loadTime.getDiff());
} }
template <class Handler> void createHandler(std::shared_ptr<Handler> & handler) template <class Handler>
void createHandler(std::unique_ptr<Handler> & handler)
{ {
handler = std::make_shared<Handler>(); handler = std::make_unique<Handler>();
} }
void GameLibrary::init(bool onlyEssential) void GameLibrary::initializeFilesystem(bool extractArchives)
{
loadFilesystem(extractArchives);
settings.init("config/settings.json", "vcmi:settings");
persistentStorage.init("config/persistentStorage.json", "");
loadModFilesystem();
}
void GameLibrary::initializeLibrary()
{ {
createHandler(settingsHandler); createHandler(settingsHandler);
modh->initializeConfig(); modh->initializeConfig();
@ -194,7 +190,7 @@ void GameLibrary::init(bool onlyEssential)
createHandler(obstacleHandler); createHandler(obstacleHandler);
modh->load(); modh->load();
modh->afterLoad(onlyEssential); modh->afterLoad();
} }
#if SCRIPTING_ENABLED #if SCRIPTING_ENABLED
@ -207,14 +203,4 @@ void GameLibrary::scriptsLoaded()
GameLibrary::GameLibrary() = default; GameLibrary::GameLibrary() = default;
GameLibrary::~GameLibrary() = default; GameLibrary::~GameLibrary() = default;
std::shared_ptr<CContentHandler> GameLibrary::getContent() const
{
return modh->content;
}
void GameLibrary::setContent(std::shared_ptr<CContentHandler> content)
{
modh->content = std::move(content);
}
VCMI_LIB_NAMESPACE_END VCMI_LIB_NAMESPACE_END

View File

@ -51,10 +51,6 @@ namespace scripting
/// Loads and constructs several handlers /// Loads and constructs several handlers
class DLL_LINKAGE GameLibrary final : public Services class DLL_LINKAGE GameLibrary final : public Services
{ {
std::shared_ptr<CContentHandler> getContent() const;
void setContent(std::shared_ptr<CContentHandler> content);
public: public:
const ArtifactService * artifacts() const override; const ArtifactService * artifacts() const override;
const CreatureService * creatures() const override; const CreatureService * creatures() const override;
@ -76,38 +72,44 @@ public:
const IBonusTypeHandler * getBth() const; //deprecated const IBonusTypeHandler * getBth() const; //deprecated
const CIdentifierStorage * identifiers() const; const CIdentifierStorage * identifiers() const;
std::shared_ptr<CArtHandler> arth; std::unique_ptr<CArtHandler> arth;
std::shared_ptr<CBonusTypeHandler> bth; std::unique_ptr<CBonusTypeHandler> bth;
std::shared_ptr<CHeroHandler> heroh; std::unique_ptr<CHeroHandler> heroh;
std::shared_ptr<CHeroClassHandler> heroclassesh; std::unique_ptr<CHeroClassHandler> heroclassesh;
std::shared_ptr<CCreatureHandler> creh; std::unique_ptr<CCreatureHandler> creh;
std::shared_ptr<CSpellHandler> spellh; std::unique_ptr<CSpellHandler> spellh;
std::shared_ptr<CSkillHandler> skillh; std::unique_ptr<CSkillHandler> skillh;
// TODO: Remove ObjectHandler altogether? // TODO: Remove ObjectHandler altogether?
std::shared_ptr<CObjectHandler> objh; std::unique_ptr<CObjectHandler> objh;
std::shared_ptr<CObjectClassesHandler> objtypeh; std::unique_ptr<CObjectClassesHandler> objtypeh;
std::shared_ptr<CTownHandler> townh; std::unique_ptr<CTownHandler> townh;
std::shared_ptr<CGeneralTextHandler> generaltexth; std::unique_ptr<CGeneralTextHandler> generaltexth;
std::shared_ptr<CModHandler> modh; std::unique_ptr<CModHandler> modh;
std::shared_ptr<TerrainTypeHandler> terrainTypeHandler; std::unique_ptr<TerrainTypeHandler> terrainTypeHandler;
std::shared_ptr<RoadTypeHandler> roadTypeHandler; std::unique_ptr<RoadTypeHandler> roadTypeHandler;
std::shared_ptr<RiverTypeHandler> riverTypeHandler; std::unique_ptr<RiverTypeHandler> riverTypeHandler;
std::shared_ptr<CIdentifierStorage> identifiersHandler; std::unique_ptr<CIdentifierStorage> identifiersHandler;
std::shared_ptr<CTerrainViewPatternConfig> terviewh; std::unique_ptr<CTerrainViewPatternConfig> terviewh;
std::shared_ptr<CRmgTemplateStorage> tplh; std::unique_ptr<CRmgTemplateStorage> tplh;
std::shared_ptr<BattleFieldHandler> battlefieldsHandler; std::unique_ptr<BattleFieldHandler> battlefieldsHandler;
std::shared_ptr<ObstacleHandler> obstacleHandler; std::unique_ptr<ObstacleHandler> obstacleHandler;
std::shared_ptr<GameSettings> settingsHandler; std::unique_ptr<GameSettings> settingsHandler;
std::shared_ptr<ObstacleSetHandler> biomeHandler; std::unique_ptr<ObstacleSetHandler> biomeHandler;
#if SCRIPTING_ENABLED #if SCRIPTING_ENABLED
std::shared_ptr<scripting::ScriptHandler> scriptHandler; std::unique_ptr<scripting::ScriptHandler> scriptHandler;
#endif #endif
GameLibrary(); //c-tor, loads .lods and NULLs handlers GameLibrary();
~GameLibrary(); ~GameLibrary();
void init(bool onlyEssential); //uses standard config file
/// initializes settings and filesystem
void initializeFilesystem(bool extractArchives);
/// Loads all game entities
void initializeLibrary();
private:
// basic initialization. should be called before init(). Can also extract original H3 archives // basic initialization. should be called before init(). Can also extract original H3 archives
void loadFilesystem(bool extractArchives); void loadFilesystem(bool extractArchives);
void loadModFilesystem(); void loadModFilesystem();
@ -119,8 +121,4 @@ public:
extern DLL_LINKAGE GameLibrary * LIBRARY; extern DLL_LINKAGE GameLibrary * LIBRARY;
DLL_LINKAGE void preinitDLL(bool extractArchives);
DLL_LINKAGE void loadDLLClasses(bool onlyEssential = false);
VCMI_LIB_NAMESPACE_END VCMI_LIB_NAMESPACE_END

View File

@ -309,7 +309,7 @@ void CModHandler::load()
logMod->info("\tAll game content loaded"); logMod->info("\tAll game content loaded");
} }
void CModHandler::afterLoad(bool onlyEssential) void CModHandler::afterLoad()
{ {
JsonNode modSettings; JsonNode modSettings;
for (const auto & modEntry : getActiveMods()) for (const auto & modEntry : getActiveMods())

View File

@ -62,7 +62,7 @@ public:
/// load content from all available mods /// load content from all available mods
void load(); void load();
void afterLoad(bool onlyEssential); void afterLoad();
CModHandler(); CModHandler();
~CModHandler(); ~CModHandler();

View File

@ -69,16 +69,6 @@ QPixmap pixmapFromJson(const QJsonValue &val)
return p; return p;
} }
void init()
{
loadDLLClasses();
Settings config = settings.write["session"]["editor"];
config->Bool() = true;
logGlobal->info("Initializing VCMI_Lib");
}
void MainWindow::loadUserSettings() void MainWindow::loadUserSettings()
{ {
//load window settings //load window settings
@ -190,7 +180,8 @@ MainWindow::MainWindow(QWidget* parent) :
logGlobal->info("The log file will be saved to %s", logPath); logGlobal->info("The log file will be saved to %s", logPath);
//init //init
preinitDLL(extractionOptions.extractArchives); LIBRARY = new GameLibrary();
LIBRARY->initializeFilesystem(extractionOptions.extractArchives);
// Initialize logging based on settings // Initialize logging based on settings
logConfig->configure(); logConfig->configure();
@ -250,7 +241,12 @@ MainWindow::MainWindow(QWidget* parent) :
loadUserSettings(); //For example window size loadUserSettings(); //For example window size
setTitle(); setTitle();
init(); LIBRARY->initializeLibrary();
Settings config = settings.write["session"]["editor"];
config->Bool() = true;
logGlobal->info("Initializing VCMI_Lib");
graphics = new Graphics(); // should be before curh->init() graphics = new Graphics(); // should be before curh->init()
graphics->load();//must be after Content loading but should be in main thread graphics->load();//must be after Content loading but should be in main thread

View File

@ -78,10 +78,11 @@ int main(int argc, const char * argv[])
boost::program_options::variables_map opts; boost::program_options::variables_map opts;
handleCommandOptions(argc, argv, opts); handleCommandOptions(argc, argv, opts);
preinitDLL(false); LIBRARY = new GameLibrary;
LIBRARY->initializeFilesystem(false);
logConfigurator.configure(); logConfigurator.configure();
loadDLLClasses(); LIBRARY->initializeLibrary();
std::srand(static_cast<uint32_t>(time(nullptr))); std::srand(static_cast<uint32_t>(time(nullptr)));
{ {

View File

@ -22,8 +22,9 @@
void CVcmiTestConfig::SetUp() void CVcmiTestConfig::SetUp()
{ {
preinitDLL(true); LIBRARY = new GameLibrary;
loadDLLClasses(true); LIBRARY->initializeFilesystem(false);
LIBRARY->initializeLibrary();
/* TEST_DATA_DIR may be wrong, if yes below test don't run, /* TEST_DATA_DIR may be wrong, if yes below test don't run,
find your test data folder in your build and change TEST_DATA_DIR for it*/ find your test data folder in your build and change TEST_DATA_DIR for it*/