diff --git a/init.lua b/init.lua index 5ef3a990..fcd95c53 100644 --- a/init.lua +++ b/init.lua @@ -28,7 +28,7 @@ g_resources.setupUserWriteDir(g_app.getCompactName()) g_resources.searchAndAddPackages('/', '.otpkg', true) -- load configurations -g_configs.load("/config.otml") +g_configs.loadSettings("/config.otml") g_modules.discoverModules() diff --git a/modules/client/client.lua b/modules/client/client.lua index 97ac544c..eb7a81af 100644 --- a/modules/client/client.lua +++ b/modules/client/client.lua @@ -91,9 +91,9 @@ function init() g_keyboard.bindKeyDown('Ctrl+Shift+R', reloadScripts) -- generate machine uuid, this is a security measure for storing passwords - if not g_crypt.setMachineUUID(g_configs.get('uuid')) then - g_configs.set('uuid', g_crypt.getMachineUUID()) - g_configs.save() + if not g_crypt.setMachineUUID(g_settings.get('uuid')) then + g_settings.set('uuid', g_crypt.getMachineUUID()) + g_settings.save() end end diff --git a/modules/client_modulemanager/modulemanager.lua b/modules/client_modulemanager/modulemanager.lua index cf1c2097..1826b181 100644 --- a/modules/client_modulemanager/modulemanager.lua +++ b/modules/client_modulemanager/modulemanager.lua @@ -14,7 +14,8 @@ function init() g_keyboard.bindKeyPress('Up', function() moduleList:focusPreviousChild(KeyboardFocusReason) end, moduleManagerWindow) g_keyboard.bindKeyPress('Down', function() moduleList:focusNextChild(KeyboardFocusReason) end, moduleManagerWindow) - moduleManagerButton = modules.client_topmenu.addLeftButton('moduleManagerButton', tr('Module Manager'), '/images/topbuttons/modulemanager', toggle) + moduleManagerButton = modules.client_topmenu.addLeftButton('moduleManagerButton', + tr('Module Manager'), '/images/topbuttons/modulemanager', toggle) -- refresh modules only after all modules are loaded addEvent(listModules) diff --git a/modules/corelib/settings.lua b/modules/corelib/settings.lua index 9abc7c3a..34fa74bf 100644 --- a/modules/corelib/settings.lua +++ b/modules/corelib/settings.lua @@ -1,14 +1,5 @@ -- @docclass -g_settings = {} - -g_settings.exists = g_configs.exists -g_settings.setNode = g_configs.setNode -g_settings.mergeNode = g_configs.mergeNode -g_settings.getNode = g_configs.getNode -g_settings.remove = g_configs.remove -g_settings.setList = g_configs.setList -g_settings.getList = g_configs.getList -g_settings.save = g_configs.save +g_settings = g_configs.getSettings() local function convertSettingValue(value) if type(value) == 'table' then @@ -31,7 +22,7 @@ local function convertSettingValue(value) end function g_settings.set(key, value) - g_configs.set(key, convertSettingValue(value)) + g_settings.set(key, convertSettingValue(value)) end function g_settings.setDefault(key, value) @@ -41,10 +32,11 @@ function g_settings.setDefault(key, value) end function g_settings.get(key, default) + print(g_settings.exists) if not g_settings.exists(key) and default ~= nil then g_settings.set(key, default) end - return g_configs.get(key) + return g_settings.get(key) end function g_settings.getString(key, default) diff --git a/modules/corelib/util.lua b/modules/corelib/util.lua index a827b696..ad59811f 100644 --- a/modules/corelib/util.lua +++ b/modules/corelib/util.lua @@ -284,6 +284,10 @@ function numbertoboolean(number) end end +function postostring(pos) + return pos.x .. " " .. pos.y .. " " .. pos.z +end + function signalcall(param, ...) if type(param) == 'function' then local status, ret = pcall(param, ...) diff --git a/src/framework/CMakeLists.txt b/src/framework/CMakeLists.txt index 924dad91..17af48ae 100644 --- a/src/framework/CMakeLists.txt +++ b/src/framework/CMakeLists.txt @@ -71,6 +71,8 @@ set(framework_SOURCES ${framework_SOURCES} ${CMAKE_CURRENT_LIST_DIR}/core/binarytree.h ${CMAKE_CURRENT_LIST_DIR}/core/clock.cpp ${CMAKE_CURRENT_LIST_DIR}/core/clock.h + ${CMAKE_CURRENT_LIST_DIR}/core/config.cpp + ${CMAKE_CURRENT_LIST_DIR}/core/config.h ${CMAKE_CURRENT_LIST_DIR}/core/configmanager.cpp ${CMAKE_CURRENT_LIST_DIR}/core/configmanager.h ${CMAKE_CURRENT_LIST_DIR}/core/declarations.h diff --git a/src/framework/core/application.cpp b/src/framework/core/application.cpp index 6c0016a6..56585cf8 100644 --- a/src/framework/core/application.cpp +++ b/src/framework/core/application.cpp @@ -126,7 +126,10 @@ void Application::terminate() #endif // save configurations - g_configs.save(); + ConfigPtr settings = g_configs.getSettings(); + if(settings) { + settings->save(); + } // release resources g_resources.terminate(); diff --git a/src/framework/core/config.cpp b/src/framework/core/config.cpp new file mode 100644 index 00000000..511995ff --- /dev/null +++ b/src/framework/core/config.cpp @@ -0,0 +1,156 @@ +/* + * Copyright (c) 2010-2013 OTClient + * + * 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. + */ + +#include "config.h" +#include "resourcemanager.h" + +#include + +Config::Config() +{ + m_confsDoc = OTMLDocument::create(); +} + +bool Config::load(const std::string& file) +{ + m_fileName = file; + + if(!g_resources.fileExists(file)) + return false; + + try { + OTMLDocumentPtr confsDoc = OTMLDocument::parse(file); + if(confsDoc) + m_confsDoc = confsDoc; + return true; + } catch(stdext::exception& e) { + g_logger.error(stdext::format("Unable to parse configuration file '%s': ", e.what())); + return false; + } +} + +bool Config::unload() +{ + if(isLoaded()) { + m_confsDoc = nullptr; + m_fileName = ""; + return true; + } + return false; +} + +bool Config::save() +{ + if(m_fileName.length() == 0) + return false; + return m_confsDoc->save(m_fileName); +} + +void Config::clear() +{ + m_confsDoc->clear(); +} + +void Config::set(const std::string& key, const std::string& value) +{ + if(key == "") { + remove(key); + return; + } + + OTMLNodePtr child = OTMLNode::create(key, value); + m_confsDoc->addChild(child); +} + +void Config::setList(const std::string& key, const std::vector& list) +{ + remove(key); + + if(list.size() == 0) + return; + + OTMLNodePtr child = OTMLNode::create(key, true); + for(const std::string& value : list) + child->writeIn(value); + m_confsDoc->addChild(child); +} + +bool Config::exists(const std::string key) +{ + return m_confsDoc->hasChildAt(key); +} + +std::string Config::get(const std::string& key) +{ + OTMLNodePtr child = m_confsDoc->get(key); + if(child) + return child->value(); + else + return ""; +} + +std::vector Config::getList(const std::string& key) +{ + std::vector list; + OTMLNodePtr child = m_confsDoc->get(key); + if(child) { + for(const OTMLNodePtr& subchild : child->children()) + list.push_back(subchild->value()); + } + return list; +} + +void Config::remove(const std::string& key) +{ + OTMLNodePtr child = m_confsDoc->get(key); + if(child) + m_confsDoc->removeChild(child); +} + +void Config::setNode(const std::string& key, const OTMLNodePtr& node) +{ + remove(key); + mergeNode(key, node); +} + +void Config::mergeNode(const std::string& key, const OTMLNodePtr& node) +{ + OTMLNodePtr clone = node->clone(); + node->setTag(key); + node->setUnique(true); + m_confsDoc->addChild(node); +} + +OTMLNodePtr Config::getNode(const std::string& key) +{ + return m_confsDoc->get(key); +} + +bool Config::isLoaded() +{ + return !m_fileName.empty(); +} + +std::string Config::getFileName() +{ + return m_fileName; +} diff --git a/src/framework/core/config.h b/src/framework/core/config.h new file mode 100644 index 00000000..f16120e0 --- /dev/null +++ b/src/framework/core/config.h @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2010-2013 OTClient + * + * 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. + */ + +#ifndef CONFIG_H +#define CONFIG_H + +#include "declarations.h" + +#include +#include + +// @bindclass +class Config : public LuaObject +{ +public: + Config(); + + bool load(const std::string& file); + bool unload(); + bool save(); + void clear(); + + void set(const std::string& key, const std::string& value); + void setList(const std::string& key, const std::vector& list); + std::string get(const std::string& key); + std::vector getList(const std::string& key); + + void setNode(const std::string& key, const OTMLNodePtr& node); + void mergeNode(const std::string& key, const OTMLNodePtr& node); + OTMLNodePtr getNode(const std::string& key); + + bool exists(const std::string key); + void remove(const std::string& key); + + std::string getFileName(); + bool isLoaded(); + + // @dontbind + ConfigPtr asConfig() { return static_self_cast(); } + +private: + std::string m_fileName = ""; + OTMLDocumentPtr m_confsDoc; +}; + +#endif diff --git a/src/framework/core/configmanager.cpp b/src/framework/core/configmanager.cpp index f0ec36e8..0d8bcdad 100644 --- a/src/framework/core/configmanager.cpp +++ b/src/framework/core/configmanager.cpp @@ -21,118 +21,65 @@ */ #include "configmanager.h" -#include "resourcemanager.h" - -#include ConfigManager g_configs; ConfigManager::ConfigManager() { - m_confsDoc = OTMLDocument::create(); + m_settings = ConfigPtr(new Config()); } -bool ConfigManager::load(const std::string& file) +ConfigPtr ConfigManager::getSettings() { - m_fileName = file; + return m_settings; +} - if(!g_resources.fileExists(file)) - return false; +ConfigPtr ConfigManager::get(const std::string& file) +{ + for(const ConfigPtr config : m_configs) { + if(config->getFileName() == file) { + return config; + } + } + g_logger.error(stdext::format("Unable to find configuration for '%s' ", file)); + return nullptr; +} - try { - OTMLDocumentPtr confsDoc = OTMLDocument::parse(file); - if(confsDoc) - m_confsDoc = confsDoc; +ConfigPtr ConfigManager::loadSettings(const std::string file) +{ + if(file.empty()) { + g_logger.error("Must provide a configuration file to load."); + } + else { + if(m_settings->load(file)) { + return m_settings; + } + } + return nullptr; +} + +ConfigPtr ConfigManager::load(const std::string& file) +{ + if(file.empty()) { + g_logger.error("Must provide a configuration file to load."); + } + else { + ConfigPtr config = ConfigPtr(new Config()); + if(config->load(file)) { + m_configs.push_back(config); + return config; + } + } + return nullptr; +} + +bool ConfigManager::unload(const std::string& file) +{ + ConfigPtr config = get(file); + if(config) { + config->unload(); + m_configs.remove(config); return true; - } catch(stdext::exception& e) { - g_logger.error(stdext::format("Unable to parse configuration file '%s': ", e.what())); - return false; } -} - -bool ConfigManager::save() -{ - if(m_fileName.length() == 0) - return false; - return m_confsDoc->save(m_fileName); -} - -void ConfigManager::clear() -{ - m_confsDoc->clear(); -} - -void ConfigManager::set(const std::string& key, const std::string& value) -{ - if(key == "") { - remove(key); - return; - } - - OTMLNodePtr child = OTMLNode::create(key, value); - m_confsDoc->addChild(child); -} - -void ConfigManager::setList(const std::string& key, const std::vector& list) -{ - remove(key); - - if(list.size() == 0) - return; - - OTMLNodePtr child = OTMLNode::create(key, true); - for(const std::string& value : list) - child->writeIn(value); - m_confsDoc->addChild(child); -} - -bool ConfigManager::exists(const std::string& key) -{ - return m_confsDoc->hasChildAt(key); -} - -std::string ConfigManager::get(const std::string& key) -{ - OTMLNodePtr child = m_confsDoc->get(key); - if(child) - return child->value(); - else - return ""; -} - -std::vector ConfigManager::getList(const std::string& key) -{ - std::vector list; - OTMLNodePtr child = m_confsDoc->get(key); - if(child) { - for(const OTMLNodePtr& subchild : child->children()) - list.push_back(subchild->value()); - } - return list; -} - -void ConfigManager::remove(const std::string& key) -{ - OTMLNodePtr child = m_confsDoc->get(key); - if(child) - m_confsDoc->removeChild(child); -} - -void ConfigManager::setNode(const std::string& key, const OTMLNodePtr& node) -{ - remove(key); - mergeNode(key, node); -} - -void ConfigManager::mergeNode(const std::string& key, const OTMLNodePtr& node) -{ - OTMLNodePtr clone = node->clone(); - node->setTag(key); - node->setUnique(true); - m_confsDoc->addChild(node); -} - -OTMLNodePtr ConfigManager::getNode(const std::string& key) -{ - return m_confsDoc->get(key); + return false; } diff --git a/src/framework/core/configmanager.h b/src/framework/core/configmanager.h index 2f5cb70e..39b82010 100644 --- a/src/framework/core/configmanager.h +++ b/src/framework/core/configmanager.h @@ -23,8 +23,7 @@ #ifndef CONFIGMANAGER_H #define CONFIGMANAGER_H -#include "declarations.h" -#include +#include "config.h" // @bindsingleton g_configs class ConfigManager @@ -32,25 +31,18 @@ class ConfigManager public: ConfigManager(); - bool load(const std::string& file); - bool save(); - void clear(); + ConfigPtr getSettings(); + ConfigPtr get(const std::string& file); - void set(const std::string& key, const std::string& value); - void setList(const std::string& key, const std::vector& list); - std::string get(const std::string& key); - std::vector getList(const std::string& key); + ConfigPtr loadSettings(const std::string file); + ConfigPtr load(const std::string& file); + bool unload(const std::string& file); - void setNode(const std::string& key, const OTMLNodePtr& node); - void mergeNode(const std::string& key, const OTMLNodePtr& node); - OTMLNodePtr getNode(const std::string& key); - - bool exists(const std::string& key); - void remove(const std::string& key); +protected: + ConfigPtr m_settings; private: - std::string m_fileName; - OTMLDocumentPtr m_confsDoc; + std::list m_configs; }; extern ConfigManager g_configs; diff --git a/src/framework/core/declarations.h b/src/framework/core/declarations.h index 5f943623..4aa80cf9 100644 --- a/src/framework/core/declarations.h +++ b/src/framework/core/declarations.h @@ -25,9 +25,11 @@ #include +class ConfigManager; class ModuleManager; class ResourceManager; class Module; +class Config; class Event; class ScheduledEvent; class FileStream; @@ -35,6 +37,7 @@ class BinaryTree; class OutputBinaryTree; typedef stdext::shared_object_ptr ModulePtr; +typedef stdext::shared_object_ptr ConfigPtr; typedef stdext::shared_object_ptr EventPtr; typedef stdext::shared_object_ptr ScheduledEventPtr; typedef stdext::shared_object_ptr FileStreamPtr; diff --git a/src/framework/luafunctions.cpp b/src/framework/luafunctions.cpp index 08cd53f8..2ac5fd2a 100644 --- a/src/framework/luafunctions.cpp +++ b/src/framework/luafunctions.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -135,17 +136,11 @@ void Application::registerLuaFunctions() // ConfigManager g_lua.registerSingletonClass("g_configs"); - g_lua.bindSingletonFunction("g_configs", "load", &ConfigManager::load, &g_configs); - g_lua.bindSingletonFunction("g_configs", "save", &ConfigManager::save, &g_configs); - g_lua.bindSingletonFunction("g_configs", "set", &ConfigManager::set, &g_configs); - g_lua.bindSingletonFunction("g_configs", "setList", &ConfigManager::setList, &g_configs); + g_lua.bindSingletonFunction("g_configs", "getSettings", &ConfigManager::getSettings, &g_configs); g_lua.bindSingletonFunction("g_configs", "get", &ConfigManager::get, &g_configs); - g_lua.bindSingletonFunction("g_configs", "getList", &ConfigManager::getList, &g_configs); - g_lua.bindSingletonFunction("g_configs", "exists", &ConfigManager::exists, &g_configs); - g_lua.bindSingletonFunction("g_configs", "remove", &ConfigManager::remove, &g_configs); - g_lua.bindSingletonFunction("g_configs", "setNode", &ConfigManager::setNode, &g_configs); - g_lua.bindSingletonFunction("g_configs", "mergeNode", &ConfigManager::mergeNode, &g_configs); - g_lua.bindSingletonFunction("g_configs", "getNode", &ConfigManager::getNode, &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", "unload", &ConfigManager::unload, &g_configs); // Logger g_lua.registerSingletonClass("g_logger"); @@ -195,6 +190,19 @@ void Application::registerLuaFunctions() g_lua.bindSingletonFunction("g_resources", "isFileType", &ResourceManager::isFileType, &g_resources); g_lua.bindSingletonFunction("g_resources", "getFileTime", &ResourceManager::getFileTime, &g_resources); + // Config + g_lua.registerClass(); + g_lua.bindClassMemberFunction("save", &Config::save); + g_lua.bindClassMemberFunction("set", &Config::set); + g_lua.bindClassMemberFunction("setList", &Config::setList); + g_lua.bindClassMemberFunction("get", &Config::get); + g_lua.bindClassMemberFunction("getList", &Config::getList); + g_lua.bindClassMemberFunction("exists", &Config::exists); + g_lua.bindClassMemberFunction("remove", &Config::remove); + g_lua.bindClassMemberFunction("setNode", &Config::setNode); + g_lua.bindClassMemberFunction("getNode", &Config::getNode); + g_lua.bindClassMemberFunction("mergeNode", &Config::mergeNode); + // Module g_lua.registerClass(); g_lua.bindClassMemberFunction("load", &Module::load);