Can now create new configs on the fly using: local config = g_configs.create(file)

This commit is contained in:
BenDol 2014-04-02 01:54:13 +13:00
parent 5204e3ac03
commit 2d84fae2c8
4 changed files with 30 additions and 7 deletions

View File

@ -27,7 +27,7 @@ g_resources.setupUserWriteDir(g_app.getCompactName())
-- search all packages -- search all packages
g_resources.searchAndAddPackages('/', '.otpkg', true) g_resources.searchAndAddPackages('/', '.otpkg', true)
-- load configurations -- load settings
g_configs.loadSettings("/config.otml") g_configs.loadSettings("/config.otml")
g_modules.discoverModules() g_modules.discoverModules()

View File

@ -59,7 +59,6 @@ ConfigPtr ConfigManager::get(const std::string& file)
return config; return config;
} }
} }
g_logger.error(stdext::format("Unable to find configuration for '%s' ", file));
return nullptr; return nullptr;
} }
@ -76,19 +75,41 @@ ConfigPtr ConfigManager::loadSettings(const std::string file)
return nullptr; return nullptr;
} }
ConfigPtr ConfigManager::create(const std::string& file)
{
ConfigPtr config = load(file);
if(!config) {
config = ConfigPtr(new Config());
config->load(file);
config->save();
m_configs.push_back(config);
}
return config;
}
ConfigPtr ConfigManager::load(const std::string& file) ConfigPtr ConfigManager::load(const std::string& file)
{ {
if(file.empty()) { if(file.empty()) {
g_logger.error("Must provide a configuration file to load."); g_logger.error("Must provide a configuration file to load.");
return nullptr;
} }
else { else {
ConfigPtr config = ConfigPtr(new Config()); ConfigPtr config = get(file);
if(config->load(file)) { if(!config) {
m_configs.push_back(config); config = ConfigPtr(new Config());
return config;
if(config->load(file)) {
m_configs.push_back(config);
}
else {
// cannot load config
config = nullptr;
}
} }
return config;
} }
return nullptr;
} }
bool ConfigManager::unload(const std::string& file) bool ConfigManager::unload(const std::string& file)

View File

@ -35,6 +35,7 @@ public:
ConfigPtr getSettings(); ConfigPtr getSettings();
ConfigPtr get(const std::string& file); ConfigPtr get(const std::string& file);
ConfigPtr create(const std::string& file);
ConfigPtr loadSettings(const std::string file); ConfigPtr loadSettings(const std::string file);
ConfigPtr load(const std::string& file); ConfigPtr load(const std::string& file);
bool unload(const std::string& file); bool unload(const std::string& file);

View File

@ -141,6 +141,7 @@ void Application::registerLuaFunctions()
g_lua.bindSingletonFunction("g_configs", "loadSettings", &ConfigManager::loadSettings, &g_configs); g_lua.bindSingletonFunction("g_configs", "loadSettings", &ConfigManager::loadSettings, &g_configs);
g_lua.bindSingletonFunction("g_configs", "load", &ConfigManager::load, &g_configs); g_lua.bindSingletonFunction("g_configs", "load", &ConfigManager::load, &g_configs);
g_lua.bindSingletonFunction("g_configs", "unload", &ConfigManager::unload, &g_configs); g_lua.bindSingletonFunction("g_configs", "unload", &ConfigManager::unload, &g_configs);
g_lua.bindSingletonFunction("g_configs", "create", &ConfigManager::create, &g_configs);
// Logger // Logger
g_lua.registerSingletonClass("g_logger"); g_lua.registerSingletonClass("g_logger");