升级之后的配置文件如下所示:
[asset]
BinaryRootFolder=.
AssetFolder=asset
SchemaFolder=schema
BigIconFile=resource/PiccoloEditorBigIcon.png
SmallIconFile=resource/PiccoloEditorSmallIcon.png
FontFile=resource/PiccoloEditorFont.TTF
DefaultWorld=asset/world/hello.world.json
GlobalRenderingRes=asset/global/rendering.global.json
GlobalParticleRes=asset/global/particle.global.json
JoltAssetFolder=jolt-asset
[key]
Jump=Space
WalkForward=W
WalkBackward=S
WalkLeft=A
WalkRight=D
稍微修改后就可以支持更多配置种类了。代码如下,主要通过添加一个状态来决定当前的解析函数:
#pragma once
#include <filesystem>
#include <unordered_map>
namespace Piccolo
{
struct EngineInitParams;
class ConfigManager
{
public:
void initialize(const std::filesystem::path& config_file_path);
const std::filesystem::path& getRootFolder() const;
const std::filesystem::path& getAssetFolder() const;
const std::filesystem::path& getSchemaFolder() const;
const std::filesystem::path& getEditorBigIconPath() const;
const std::filesystem::path& getEditorSmallIconPath() const;
const std::filesystem::path& getEditorFontPath() const;
#ifdef ENABLE_PHYSICS_DEBUG_RENDERER
const std::filesystem::path& getJoltPhysicsAssetFolder() const;
#endif
const std::string& getDefaultWorldUrl() const;
const std::string& getGlobalRenderingResUrl() const;
const std::string& getGlobalParticleResUrl() const;
private:
void setAssetConfig(const std::string& name,const std::string& value);
void setKeyConfig(const std::string& name,const std::string& value);
private:
const std::string ASSET_CONFIG = "[asset]";
const std::string KEY_CONFIG = "[key]";
std::string m_current_config = ASSET_CONFIG;
std::filesystem::path m_config_file_path;
std::filesystem::path m_root_folder;
std::filesystem::path m_asset_folder;
std::filesystem::path m_schema_folder;
std::filesystem::path m_editor_big_icon_path;
std::filesystem::path m_editor_small_icon_path;
std::filesystem::path m_editor_font_path;
#ifdef ENABLE_PHYSICS_DEBUG_RENDERER
std::filesystem::path m_jolt_physics_asset_folder;
#endif
std::string m_default_world_url;
std::string m_global_rendering_res_url;
std::string m_global_particle_res_url;
std::unordered_map<std::string, std::string> m_key_binding;
};
} // namespace Piccolo
#include "runtime/resource/config_manager/config_manager.h"
#include "runtime/engine.h"
#include <filesystem>
#include <fstream>
#include <string>
namespace Piccolo
{
void ConfigManager::initialize(const std::filesystem::path& config_file_path)
{
// read configs
m_config_file_path = config_file_path;
std::ifstream config_file(config_file_path);
std::string config_line;
while (std::getline(config_file, config_line))
{
size_t asset_config_pos = config_line.find(ASSET_CONFIG);
if (asset_config_pos >= 0 && asset_config_pos < config_line.length())
{
m_current_config = ASSET_CONFIG;
continue;
}
size_t key_config_pos = config_line.find(KEY_CONFIG);
if (key_config_pos >= 0 && key_config_pos < config_line.length())
{
m_current_config = KEY_CONFIG;
continue;
}
size_t seperate_pos = config_line.find_first_of('=');
if (seperate_pos > 0 && seperate_pos < (config_line.length() - 1))
{
std::string name = config_line.substr(0, seperate_pos);
std::string value = config_line.substr(seperate_pos + 1, config_line.length() - seperate_pos - 1);
if (m_current_config == ASSET_CONFIG)
{
setAssetConfig(name, value);
}
else if (m_current_config == KEY_CONFIG)
{
setKeyConfig(name, value);
}
}
}
}
void ConfigManager::setAssetConfig(const std::string& name, const std::string& value)
{
if (name == "BinaryRootFolder")
{
m_root_folder = m_config_file_path.parent_path() / value;
}
else if (name == "AssetFolder")
{
m_asset_folder = m_root_folder / value;
}
else if (name == "SchemaFolder")
{
m_schema_folder = m_root_folder / value;
}
else if (name == "DefaultWorld")
{
m_default_world_url = value;
}
else if (name == "BigIconFile")
{
m_editor_big_icon_path = m_root_folder / value;
}
else if (name == "SmallIconFile")
{
m_editor_small_icon_path = m_root_folder / value;
}
else if (name == "FontFile")
{
m_editor_font_path = m_root_folder / value;
}
else if (name == "GlobalRenderingRes")
{
m_global_rendering_res_url = value;
}
else if (name == "GlobalParticleRes")
{
m_global_particle_res_url = value;
}
#ifdef ENABLE_PHYSICS_DEBUG_RENDERER
else if (name == "JoltAssetFolder")
{
m_jolt_physics_asset_folder = m_root_folder / value;
}
#endif
}
void ConfigManager::setKeyConfig(const std::string& name, const std::string& value) { m_key_binding.emplace(name, value); }
const std::filesystem::path& ConfigManager::getRootFolder() const { return m_root_folder; }
const std::filesystem::path& ConfigManager::getAssetFolder() const { return m_asset_folder; }
const std::filesystem::path& ConfigManager::getSchemaFolder() const { return m_schema_folder; }
const std::filesystem::path& ConfigManager::getEditorBigIconPath() const { return m_editor_big_icon_path; }
const std::filesystem::path& ConfigManager::getEditorSmallIconPath() const { return m_editor_small_icon_path; }
const std::filesystem::path& ConfigManager::getEditorFontPath() const { return m_editor_font_path; }
const std::string& ConfigManager::getDefaultWorldUrl() const { return m_default_world_url; }
const std::string& ConfigManager::getGlobalRenderingResUrl() const { return m_global_rendering_res_url; }
const std::string& ConfigManager::getGlobalParticleResUrl() const { return m_global_particle_res_url; }
#ifdef ENABLE_PHYSICS_DEBUG_RENDERER
const std::filesystem::path& ConfigManager::getJoltPhysicsAssetFolder() const { return m_jolt_physics_asset_folder; }
#endif
} // namespace Piccolo