2011-08-28 15:17:58 +02:00
|
|
|
/*
|
2014-04-01 07:36:42 +02:00
|
|
|
* Copyright (c) 2010-2014 OTClient <https://github.com/edubart/otclient>
|
2011-08-28 15:17:58 +02:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2011-12-01 23:25:32 +01:00
|
|
|
#include "configmanager.h"
|
2011-08-16 15:06:16 +02:00
|
|
|
|
2011-12-01 23:25:32 +01:00
|
|
|
ConfigManager g_configs;
|
2011-08-16 15:06:16 +02:00
|
|
|
|
2014-04-01 13:30:17 +02:00
|
|
|
void ConfigManager::init()
|
2012-01-15 16:13:22 +01:00
|
|
|
{
|
2014-03-31 18:43:31 +02:00
|
|
|
m_settings = ConfigPtr(new Config());
|
2012-01-15 16:13:22 +01:00
|
|
|
}
|
|
|
|
|
2014-04-01 13:30:17 +02:00
|
|
|
void ConfigManager::terminate()
|
|
|
|
{
|
|
|
|
if(m_settings) {
|
|
|
|
// ensure settings are saved
|
|
|
|
m_settings->save();
|
|
|
|
|
|
|
|
m_settings->unload();
|
|
|
|
m_settings = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(ConfigPtr config : m_configs) {
|
|
|
|
config->unload();
|
|
|
|
config = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_configs.clear();
|
|
|
|
}
|
|
|
|
|
2014-03-31 18:43:31 +02:00
|
|
|
ConfigPtr ConfigManager::getSettings()
|
2011-08-16 15:06:16 +02:00
|
|
|
{
|
2014-03-31 18:43:31 +02:00
|
|
|
return m_settings;
|
2011-08-16 15:06:16 +02:00
|
|
|
}
|
|
|
|
|
2014-03-31 18:43:31 +02:00
|
|
|
ConfigPtr ConfigManager::get(const std::string& file)
|
2011-08-16 15:06:16 +02:00
|
|
|
{
|
2014-03-31 18:43:31 +02:00
|
|
|
for(const ConfigPtr config : m_configs) {
|
|
|
|
if(config->getFileName() == file) {
|
|
|
|
return config;
|
|
|
|
}
|
2012-01-15 16:13:22 +01:00
|
|
|
}
|
2014-03-31 18:43:31 +02:00
|
|
|
return nullptr;
|
2012-01-15 16:13:22 +01:00
|
|
|
}
|
|
|
|
|
2014-03-31 18:43:31 +02:00
|
|
|
ConfigPtr ConfigManager::loadSettings(const std::string file)
|
2012-01-15 16:13:22 +01:00
|
|
|
{
|
2014-03-31 18:43:31 +02:00
|
|
|
if(file.empty()) {
|
|
|
|
g_logger.error("Must provide a configuration file to load.");
|
2011-08-16 15:06:16 +02:00
|
|
|
}
|
2014-03-31 18:43:31 +02:00
|
|
|
else {
|
|
|
|
if(m_settings->load(file)) {
|
|
|
|
return m_settings;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
2012-02-02 00:37:40 +01:00
|
|
|
}
|
|
|
|
|
2014-04-01 14:54:13 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-03-31 18:43:31 +02:00
|
|
|
ConfigPtr ConfigManager::load(const std::string& file)
|
2012-02-02 00:37:40 +01:00
|
|
|
{
|
2014-03-31 18:43:31 +02:00
|
|
|
if(file.empty()) {
|
|
|
|
g_logger.error("Must provide a configuration file to load.");
|
2014-04-01 14:54:13 +02:00
|
|
|
return nullptr;
|
2014-03-31 18:43:31 +02:00
|
|
|
}
|
|
|
|
else {
|
2014-04-01 14:54:13 +02:00
|
|
|
ConfigPtr config = get(file);
|
|
|
|
if(!config) {
|
|
|
|
config = ConfigPtr(new Config());
|
|
|
|
|
|
|
|
if(config->load(file)) {
|
|
|
|
m_configs.push_back(config);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// cannot load config
|
|
|
|
config = nullptr;
|
|
|
|
}
|
2014-03-31 18:43:31 +02:00
|
|
|
}
|
2014-04-01 14:54:13 +02:00
|
|
|
return config;
|
2014-03-31 18:43:31 +02:00
|
|
|
}
|
2012-02-02 00:37:40 +01:00
|
|
|
}
|
|
|
|
|
2014-03-31 18:43:31 +02:00
|
|
|
bool ConfigManager::unload(const std::string& file)
|
2012-02-02 00:37:40 +01:00
|
|
|
{
|
2014-03-31 18:43:31 +02:00
|
|
|
ConfigPtr config = get(file);
|
|
|
|
if(config) {
|
|
|
|
config->unload();
|
2014-04-06 05:29:02 +02:00
|
|
|
remove(config);
|
2014-04-01 13:30:17 +02:00
|
|
|
config = nullptr;
|
2014-03-31 18:43:31 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-02-02 00:37:40 +01:00
|
|
|
}
|
2014-04-01 15:34:12 +02:00
|
|
|
|
|
|
|
void ConfigManager::remove(const ConfigPtr config)
|
|
|
|
{
|
|
|
|
m_configs.remove(config);
|
|
|
|
}
|