From 252e9f0d197108e25ee7b04a6984ea4e5cc6d05e Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 10 Apr 2024 19:24:06 +0300 Subject: [PATCH] engine initialization minor refactor + fix --- src/engine.cpp | 80 ++++++++++++++++++++++++++------------------------ src/engine.h | 4 +++ 2 files changed, 46 insertions(+), 38 deletions(-) diff --git a/src/engine.cpp b/src/engine.cpp index 78e60277..13fc1051 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -2,18 +2,13 @@ #define GLEW_STATIC -#include "assets/Assets.h" #include "assets/AssetsLoader.h" #include "audio/audio.h" #include "coders/GLSLExtension.h" #include "coders/json.h" #include "coders/png.h" -#include "content/Content.h" #include "content/ContentLoader.h" -#include "content/ContentPack.h" -#include "content/PacksManager.h" #include "core_defs.h" -#include "files/engine_paths.h" #include "files/files.h" #include "frontend/locale/langs.h" #include "frontend/menu/menu.h" @@ -91,20 +86,18 @@ Engine::Engine(EngineSettings& settings, EnginePaths* paths) std::vector roots {resdir}; resPaths = std::make_unique(resdir, roots); - assets = std::make_unique(); + try { + loadAssets(); + } catch (std::runtime_error& err) { + logger.error() << "fatal error occurred while loading assets"; - AssetsLoader loader(assets.get(), resPaths.get()); - AssetsLoader::addDefaults(loader, nullptr); - - Shader::preprocessor->setPaths(resPaths.get()); - while (loader.hasNext()) { - if (!loader.loadNext()) { - assets.reset(); - scripting::close(); - Window::terminate(); - throw initialize_error("could not to load assets"); - } + assets.reset(); + scripting::close(); + audio::close(); + Window::terminate(); + throw initialize_error(err.what()); } + gui = std::make_unique(); if (settings.ui.language == "auto") { settings.ui.language = langs::locale_by_envlocale( @@ -210,6 +203,36 @@ Engine::~Engine() { logger.info() << "engine finished"; } +PacksManager Engine::createPacksManager(const fs::path& worldFolder) { + PacksManager manager; + manager.setSources({ + worldFolder/fs::path("content"), + paths->getUserfiles()/fs::path("content"), + paths->getResources()/fs::path("content") + }); + return manager; +} + +void Engine::loadAssets() { + logger.info() << "loading assets"; + Shader::preprocessor->setPaths(resPaths.get()); + + auto new_assets = std::make_unique(); + AssetsLoader loader(new_assets.get(), resPaths.get()); + AssetsLoader::addDefaults(loader, content.get()); + while (loader.hasNext()) { + if (!loader.loadNext()) { + new_assets.reset(); + throw std::runtime_error("could not to load assets"); + } + } + if (assets) { + assets->extend(*new_assets); + } else { + assets.reset(new_assets.release()); + } +} + // TODO: refactor this void Engine::loadContent() { auto resdir = paths->getResources(); @@ -221,12 +244,7 @@ void Engine::loadContent() { for (auto& pack : contentPacks) { names.push_back(pack.id); } - PacksManager manager; - manager.setSources({ - paths->getWorldFolder()/fs::path("content"), - paths->getUserfiles()/fs::path("content"), - paths->getResources()/fs::path("content") - }); + PacksManager manager = createPacksManager(paths->getWorldFolder()); manager.scan(); names = manager.assembly(names); contentPacks = manager.getAll(names); @@ -238,25 +256,11 @@ void Engine::loadContent() { ContentLoader loader(&pack); loader.load(contentBuilder); } - content.reset(contentBuilder.build()); resPaths = std::make_unique(resdir, resRoots); langs::setup(resdir, langs::current->getId(), contentPacks); - - logger.info() << "loading assets"; - - auto new_assets = std::make_unique(); - Shader::preprocessor->setPaths(resPaths.get()); - AssetsLoader loader(new_assets.get(), resPaths.get()); - AssetsLoader::addDefaults(loader, content.get()); - while (loader.hasNext()) { - if (!loader.loadNext()) { - new_assets.reset(); - throw std::runtime_error("could not to load assets"); - } - } - assets->extend(*new_assets); + loadAssets(); onAssetsLoaded(); } diff --git a/src/engine.h b/src/engine.h index dc210475..400b081d 100644 --- a/src/engine.h +++ b/src/engine.h @@ -8,6 +8,7 @@ #include "assets/Assets.h" #include "content/Content.h" #include "content/ContentPack.h" +#include "content/PacksManager.h" #include "files/engine_paths.h" #include "files/settings_io.h" @@ -59,6 +60,7 @@ class Engine { void updateHotkeys(); void renderFrame(Batch2D& batch); void processPostRunnables(); + void loadAssets(); public: Engine(EngineSettings& settings, EnginePaths* paths); ~Engine(); @@ -121,6 +123,8 @@ public: /// @brief Enqueue function call to the end of current frame in draw thread void postRunnable(runnable callback); + PacksManager createPacksManager(const fs::path& worldFolder); + SettingsHandler& getSettingsHandler(); };