engine initialization minor refactor + fix

This commit is contained in:
MihailRis
2024-04-10 19:24:06 +03:00
parent d5d0ba8959
commit 252e9f0d19
2 changed files with 46 additions and 38 deletions
+42 -38
View File
@@ -2,18 +2,13 @@
#define GLEW_STATIC #define GLEW_STATIC
#include "assets/Assets.h"
#include "assets/AssetsLoader.h" #include "assets/AssetsLoader.h"
#include "audio/audio.h" #include "audio/audio.h"
#include "coders/GLSLExtension.h" #include "coders/GLSLExtension.h"
#include "coders/json.h" #include "coders/json.h"
#include "coders/png.h" #include "coders/png.h"
#include "content/Content.h"
#include "content/ContentLoader.h" #include "content/ContentLoader.h"
#include "content/ContentPack.h"
#include "content/PacksManager.h"
#include "core_defs.h" #include "core_defs.h"
#include "files/engine_paths.h"
#include "files/files.h" #include "files/files.h"
#include "frontend/locale/langs.h" #include "frontend/locale/langs.h"
#include "frontend/menu/menu.h" #include "frontend/menu/menu.h"
@@ -91,20 +86,18 @@ Engine::Engine(EngineSettings& settings, EnginePaths* paths)
std::vector<fs::path> roots {resdir}; std::vector<fs::path> roots {resdir};
resPaths = std::make_unique<ResPaths>(resdir, roots); resPaths = std::make_unique<ResPaths>(resdir, roots);
assets = std::make_unique<Assets>(); try {
loadAssets();
} catch (std::runtime_error& err) {
logger.error() << "fatal error occurred while loading assets";
AssetsLoader loader(assets.get(), resPaths.get()); assets.reset();
AssetsLoader::addDefaults(loader, nullptr); scripting::close();
audio::close();
Shader::preprocessor->setPaths(resPaths.get()); Window::terminate();
while (loader.hasNext()) { throw initialize_error(err.what());
if (!loader.loadNext()) {
assets.reset();
scripting::close();
Window::terminate();
throw initialize_error("could not to load assets");
}
} }
gui = std::make_unique<gui::GUI>(); gui = std::make_unique<gui::GUI>();
if (settings.ui.language == "auto") { if (settings.ui.language == "auto") {
settings.ui.language = langs::locale_by_envlocale( settings.ui.language = langs::locale_by_envlocale(
@@ -210,6 +203,36 @@ Engine::~Engine() {
logger.info() << "engine finished"; 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<Assets>();
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 // TODO: refactor this
void Engine::loadContent() { void Engine::loadContent() {
auto resdir = paths->getResources(); auto resdir = paths->getResources();
@@ -221,12 +244,7 @@ void Engine::loadContent() {
for (auto& pack : contentPacks) { for (auto& pack : contentPacks) {
names.push_back(pack.id); names.push_back(pack.id);
} }
PacksManager manager; PacksManager manager = createPacksManager(paths->getWorldFolder());
manager.setSources({
paths->getWorldFolder()/fs::path("content"),
paths->getUserfiles()/fs::path("content"),
paths->getResources()/fs::path("content")
});
manager.scan(); manager.scan();
names = manager.assembly(names); names = manager.assembly(names);
contentPacks = manager.getAll(names); contentPacks = manager.getAll(names);
@@ -238,25 +256,11 @@ void Engine::loadContent() {
ContentLoader loader(&pack); ContentLoader loader(&pack);
loader.load(contentBuilder); loader.load(contentBuilder);
} }
content.reset(contentBuilder.build()); content.reset(contentBuilder.build());
resPaths = std::make_unique<ResPaths>(resdir, resRoots); resPaths = std::make_unique<ResPaths>(resdir, resRoots);
langs::setup(resdir, langs::current->getId(), contentPacks); langs::setup(resdir, langs::current->getId(), contentPacks);
loadAssets();
logger.info() << "loading assets";
auto new_assets = std::make_unique<Assets>();
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);
onAssetsLoaded(); onAssetsLoaded();
} }
+4
View File
@@ -8,6 +8,7 @@
#include "assets/Assets.h" #include "assets/Assets.h"
#include "content/Content.h" #include "content/Content.h"
#include "content/ContentPack.h" #include "content/ContentPack.h"
#include "content/PacksManager.h"
#include "files/engine_paths.h" #include "files/engine_paths.h"
#include "files/settings_io.h" #include "files/settings_io.h"
@@ -59,6 +60,7 @@ class Engine {
void updateHotkeys(); void updateHotkeys();
void renderFrame(Batch2D& batch); void renderFrame(Batch2D& batch);
void processPostRunnables(); void processPostRunnables();
void loadAssets();
public: public:
Engine(EngineSettings& settings, EnginePaths* paths); Engine(EngineSettings& settings, EnginePaths* paths);
~Engine(); ~Engine();
@@ -121,6 +123,8 @@ public:
/// @brief Enqueue function call to the end of current frame in draw thread /// @brief Enqueue function call to the end of current frame in draw thread
void postRunnable(runnable callback); void postRunnable(runnable callback);
PacksManager createPacksManager(const fs::path& worldFolder);
SettingsHandler& getSettingsHandler(); SettingsHandler& getSettingsHandler();
}; };