// // Created by sebastian on 08.02.26. // #include "AssetManager.h" #include #include #include #include #include #include "json.hpp" #include "OBJLoader.h" #include "spdlog/spdlog.h" namespace fs = std::filesystem; using namespace nlohmann; std::shared_ptr AssetManager::loadModel(const std::string &name, const std::string &objPath, const std::string &texturePath, Loader &loader) { if (models.contains(name)) { return models[name]; } auto model = OBJLoader::loadModel(objPath, texturePath, loader); models[name] = model; return model; } std::shared_ptr AssetManager::getModel(const std::string &name) { assert(models.contains(name) && "Model not found!"); return models.at(name); } void AssetManager::insertGeneratedModel(const std::string &name, std::shared_ptr model) { assert(!models.contains(name) && "Model already exists!"); models[name] = std::move(model); } void AssetManager::loadAsset(const std::string &assetPath, Loader &loader) { json assetConfiguration = readJsonFile(assetPath); ModelStages modelStages; std::string baseDir = std::filesystem::path(assetPath).parent_path().string(); for (const auto& modelStage : assetConfiguration["modelStages"]) { auto name = modelStage["name"].get(); auto objPath = modelStage["filename"].get(); const auto conditionKey = modelStage["conditionKey"].get(); const float min = modelStage["minValue"].get(); const float max = modelStage["maxValue"].get(); std::filesystem::path fullObjPath = std::filesystem::path(baseDir) / std::filesystem::path(objPath); const std::shared_ptr model = loadModel(name, fullObjPath, "", loader); const auto condition = ModelStageCondition(conditionKey, min, max); modelStages.addModelStage(ModelStageConfiguration(model, name, condition)); } modelsWithStages[assetConfiguration["name"].get()] = std::make_shared(modelStages); } std::shared_ptr AssetManager::getModelStages(const std::string &modelName) { if (modelsWithStages.contains(modelName)) { return modelsWithStages[modelName]; } return nullptr; } std::shared_ptr AssetManager::getModelStageConfiguration(const std::string &modelName, const std::string &stageName) { if (const auto modelStages = getModelStages(modelName)) { return std::make_shared(modelStages->getModelStageConfiguration(stageName)); } return nullptr; } std::shared_ptr AssetManager::loadTexture(const std::string& name, const std::string &texturePath, Loader &loader) { if (textures.contains(name)) { return textures[name]; } auto texture = std::make_shared(loader.loadTextureFromFile(texturePath)); textures[name] = texture; return texture; } std::shared_ptr AssetManager::getTexture(const std::string &name) { assert(textures.contains(name) && "Texture not found!"); return textures.at(name); } std::shared_ptr AssetManager::getUiTheme(const std::string &themeName) { assert(uiThemes.contains(themeName) && "Theme not found!"); return uiThemes.at(themeName); } std::shared_ptr AssetManager::loadUiTheme(const std::string &themeName, const std::string &themePath) { if (!uiThemes.contains(themeName)) { json themeConfiguration = readJsonFile(themePath); if (themeConfiguration.contains("font")) { auto fontData = themeConfiguration["font"]; if (fontData.contains("path") && fontData.contains("sizes")) { auto fontSizes = fontData["sizes"]; if (fontSizes.contains("small") && fontSizes.contains("medium") && fontSizes.contains("large")) { std::string fontPath = fontData["path"].get(); auto theme = std::make_shared( std::make_unique(fontPath, fontSizes["small"]), std::make_unique(fontPath, fontSizes["medium"]), std::make_unique(fontPath, fontSizes["large"]) ); uiThemes[themeName] = theme; return theme; } else { std::cerr << "Required Font sizes not found in theme configuration!" << std::endl; return nullptr; } } else { std::cerr << "Required Font path not found in theme configuration!" << std::endl; return nullptr; } } else { std::cerr << "Required Font configuration not found in theme configuration!" << std::endl; return nullptr; } } else { return uiThemes[themeName]; } } void AssetManager::insertLoadedTexture(const std::string &name, const Texture2D &texture) { if (textures.contains(name)) { spdlog::warn("Texture '{}' already exists, skipping insert", name); return; } textures[name] = std::make_shared(texture.id); spdlog::debug("Texture '{}' inserted successfully", name); } void AssetManager::insertTexturedModel(const std::string &name, const TexturedModel &textured_model) { if (models.contains(name)) { spdlog::warn("Model '{}' already exists, skipping insert", name); return; } models[name] = std::make_shared(textured_model); spdlog::debug("Model '{}' inserted successfully", name); } void AssetManager::insertModelStages(const std::string &name, ModelStages modelStages) { if (modelsWithStages.contains(name)) { spdlog::warn("Asset '{}' already has stages, skipping insert", name); return; } modelsWithStages[name] = std::make_shared(modelStages); } json AssetManager::readJsonFile(const std::string &path) { if (fs::exists(path)) { std::ifstream filestream(path); json j; filestream >> j; return j; } return json::object(); }