Started implementing ability to load custom config files (currently unstable).

Feel free to help out with this if you like :)
This commit is contained in:
BenDol 2014-04-01 05:43:31 +13:00
parent f4de000646
commit 61059e66ec
13 changed files with 320 additions and 147 deletions

View File

@ -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()

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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, ...)

View File

@ -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

View File

@ -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();

View File

@ -0,0 +1,156 @@
/*
* Copyright (c) 2010-2013 OTClient <https://github.com/edubart/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 <framework/otml/otml.h>
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<std::string>& 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<std::string> Config::getList(const std::string& key)
{
std::vector<std::string> 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;
}

View File

@ -0,0 +1,65 @@
/*
* Copyright (c) 2010-2013 OTClient <https://github.com/edubart/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 <framework/luaengine/luaobject.h>
#include <framework/otml/declarations.h>
// @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<std::string>& list);
std::string get(const std::string& key);
std::vector<std::string> 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<Config>(); }
private:
std::string m_fileName = "";
OTMLDocumentPtr m_confsDoc;
};
#endif

View File

@ -21,118 +21,65 @@
*/
#include "configmanager.h"
#include "resourcemanager.h"
#include <framework/otml/otml.h>
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<std::string>& 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<std::string> ConfigManager::getList(const std::string& key)
{
std::vector<std::string> 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);
}

View File

@ -23,8 +23,7 @@
#ifndef CONFIGMANAGER_H
#define CONFIGMANAGER_H
#include "declarations.h"
#include <framework/otml/declarations.h>
#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<std::string>& list);
std::string get(const std::string& key);
std::vector<std::string> 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<ConfigPtr> m_configs;
};
extern ConfigManager g_configs;

View File

@ -25,9 +25,11 @@
#include <framework/global.h>
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<Module> ModulePtr;
typedef stdext::shared_object_ptr<Config> ConfigPtr;
typedef stdext::shared_object_ptr<Event> EventPtr;
typedef stdext::shared_object_ptr<ScheduledEvent> ScheduledEventPtr;
typedef stdext::shared_object_ptr<FileStream> FileStreamPtr;

View File

@ -24,6 +24,7 @@
#include <framework/luaengine/luainterface.h>
#include <framework/core/eventdispatcher.h>
#include <framework/core/configmanager.h>
#include <framework/core/config.h>
#include <framework/otml/otml.h>
#include <framework/core/modulemanager.h>
#include <framework/core/module.h>
@ -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<Config>();
g_lua.bindClassMemberFunction<Config>("save", &Config::save);
g_lua.bindClassMemberFunction<Config>("set", &Config::set);
g_lua.bindClassMemberFunction<Config>("setList", &Config::setList);
g_lua.bindClassMemberFunction<Config>("get", &Config::get);
g_lua.bindClassMemberFunction<Config>("getList", &Config::getList);
g_lua.bindClassMemberFunction<Config>("exists", &Config::exists);
g_lua.bindClassMemberFunction<Config>("remove", &Config::remove);
g_lua.bindClassMemberFunction<Config>("setNode", &Config::setNode);
g_lua.bindClassMemberFunction<Config>("getNode", &Config::getNode);
g_lua.bindClassMemberFunction<Config>("mergeNode", &Config::mergeNode);
// Module
g_lua.registerClass<Module>();
g_lua.bindClassMemberFunction<Module>("load", &Module::load);