adapt to new Ghoul version (p vs absPath macro)

added configurationmanager script
This commit is contained in:
Alexander Bock
2014-01-16 14:56:50 +01:00
parent 006bd56ae6
commit 7a4983f2c9
15 changed files with 281 additions and 95 deletions

2
.gitignore vendored
View File

@@ -1 +1,3 @@
bin/
build/
ext/SGCT

View File

@@ -4,6 +4,7 @@
<Window fullScreen="false">
<!-- 16:9 aspect ratio -->
<Size x="640" y="360" />
<Pos x="1000" y="50.0" />
<Viewport>
<Pos x="0.0" y="0.0" />
<Size x="1.0" y="1.0" />

View File

@@ -1,6 +1,5 @@
-- fofo
{
a = 0,
b = 1,
c = 2
setting1 = 1,
setting2 = 2,
t = {s = 1, t = 2, u = 3}
}

5
scripts/config2.lua Normal file
View File

@@ -0,0 +1,5 @@
{
setting2 = 4,
setting3 = 1,
t = { s = 2, v = 0}
}

View File

@@ -0,0 +1,66 @@
config = {}
function merge(t1, t2)
for k, v in pairs(t2) do
if (type(v) == "table") and (type(t1[k] or false) == "table") then
merge(t1[k], t2[k])
else
t1[k] = v
end
end
return t1
end
--[[
function printTable(t, i)
i = i or ""
for k,v in pairs(t) do
if (type(v) == "table") then
print(i .. k)
printTable(v, i .. " ")
else
print(i ..k .. ' , ' .. v)
end
end
end
]]--
function loadConfiguration(file)
io.input(file)
contents = io.read("*all")
source = "return " .. contents
settings = assert(load(source))()
merge(config, settings or {})
end
function getValue(key, t)
t = t or config -- default value of 'config'
pos = key:find('[.]')
if (not pos) then
return t[key]
else
newKey = t[key:sub(0, pos - 1)]
if (not newKey) then
return nil
else
return getValue(key:sub(pos + 1), newKey)
end
end
end
function setValue(key, v, t)
t = t or config -- default value of 'config'
pos = key:find('[.]')
if (not pos) then
t[key] = v
return true
else
newKey = t[key:sub(0, pos - 1)]
if (not newKey) then
return false
else
return setValue(key:sub(pos + 1), v, newKey)
end
end
end

View File

@@ -1,27 +0,0 @@
--config = assert(loadfile("../../scripts/config.lua"))()
io.input("../../scripts/config.lua")
file = io.read("*all")
config = assert(load("return " .. file))()
--t = {a=1, b="foo", c= 0.1 }
--print(_G)
-- for key,value in pairs(_G) do print(key,value) end
-- a = {}
-- local x = 20
-- for i=1,10 do
-- local y = 0
-- a[i] = function () y=y+1; return x+y end
-- end
-- for key,value in pairs(a) do print(key,value()) end
-- m = getmetatable(x)
--m = getfenv(m)
-- print(m)
--for key,value in pairs(m) do print(key,value) end
-- for key,value in pairs(debug.getregistry()) do print(key,value) end
-- print (debug.getregistry())
-- print(debug)

View File

@@ -26,15 +26,14 @@
#include <ghoul/filesystem/filesystem>
#include <ghoul/logging/logging>
#include <ghoul/lua/ghoul_lua.h>
#include <assert.h>
#include <fstream>
namespace {
const std::string _loggerCat = "ConfigurationManager";
lua_State* _state = nullptr;
const char* _configurationScript = "${SCRIPTS}/configurationmanager.lua";
const char* _configurationTable = "config";
}
using namespace ghoul::lua;
@@ -42,81 +41,86 @@ using namespace ghoul::lua;
namespace openspace {
ConfigurationManager::ConfigurationManager() {}
ConfigurationManager::ConfigurationManager()
: _state(nullptr)
{}
ConfigurationManager::~ConfigurationManager() {
if (_state != nullptr) {
LWARNING("ConfigurationManager was not deinitialized");
deinitialize();
}
}
void ConfigurationManager::initialize() {
bool ConfigurationManager::initialize() {
// TODO custom assert (ticket #5)
assert(_state == nullptr);
LDEBUG("Create Lua state");
_state = luaL_newstate();
if (_state == nullptr) {
LFATAL("Error creating new Lua state: Memory allocation error");
return false;
}
LDEBUG("Open libraries");
luaL_openlibs(_state);
LDEBUG("Loading configuration script '" << _configurationScript << "'");
const int status = luaL_loadfile(_state, absPath(_configurationScript).c_str());
if (status != LUA_OK) {
LFATAL("Error loading configuration script: " << lua_tostring(_state, -1));
deinitialize();
return false;
}
LDEBUG("Executing configuration script");
if (lua_pcall(_state, 0, LUA_MULTRET, 0)) {
LFATAL("Error executing configuration script: " << lua_tostring(_state, -1));
deinitialize();
return false;
}
// Sanity checks
lua_getglobal(_state, "loadConfiguration");
if (!lua_isfunction(_state, -1)) {
LFATAL("Could not find function 'loadConfiguration' in script");
deinitialize();
return false;
}
lua_pop(_state, 1);
lua_getglobal(_state, _configurationTable);
if (!lua_istable(_state, -1)) {
LERROR("Table '" << _configurationTable << "' not found in script");
deinitialize();
return false;
}
lua_pop(_state, 1);
return true;
}
void ConfigurationManager::deinitialize() {
// TODO custom assert (ticket #5)
assert(_state != nullptr);
lua_close(_state);
_state = nullptr;
}
void ConfigurationManager::loadConfiguration(const std::string& filename,
const std::string& position)
{
// TODO custom assert (ticket #5)
assert(_state != nullptr);
// load 'filename', prepent 'return', execute string, modify stack and execute
// separate script to apply the table of changes
std::ifstream file(filename);
if (!file.good()) {
LERROR("Error opening file '" << filename << "': Error code:" <<
std::endl << file.rdstate());
const std::string& absFilename = absPath(filename);
lua_getglobal(_state, "loadConfiguration");
lua_pushstring(_state, absFilename.c_str());
const int status = lua_pcall(_state, 1, 0, 0);
if (status != LUA_OK) {
LERROR("Error loading configuration: " << lua_tostring(_state, -1));
return;
}
// load script code from 'filename' and prepend "return"
std::stringstream buffer;
buffer << file.rdbuf();
std::string scriptText = "return " + buffer.str();
const int loadResult = luaL_loadstring(_state, scriptText.c_str());
if (loadResult != 0) {
LERROR("Error loading script in file '" << filename << "'. Error:" <<
std::endl << lua_tostring(_state, -1));
return;
}
LINFO("before");
lua_logStack(_state);
const int execResult = lua_pcall(_state, 0, LUA_MULTRET, 0);
if (execResult != 0) {
LERROR("Error executing script in file '" << filename << "'. Error:" <<
std::endl << lua_tostring(_state, -1));
return;
}
LINFO("after");
lua_logStack(_state);
lua_logTable(_state);
//lua_p
const int load2Result = luaL_loadfile(_state, p("${SCRIPTS}/script.lua").c_str());
if (load2Result != 0) {
LERROR("Error loading script in file '" << filename << "'. Error:" <<
std::endl << lua_tostring(_state, -1));
return;
}
LINFO("afterloading");
lua_logStack(_state);
}
template <class T>
@@ -133,5 +137,64 @@ bool ConfigurationManager::setValue(const std::string& key, const T& value) {
return false;
}
bool getFullValue(lua_State* state, const std::string& key, LUA_INTEGER& value) {
assert(state != nullptr);
lua_getglobal(state, "getValue");
lua_pushstring(state, key.c_str());
lua_pcall(state, 1, 1, NULL);
//lua_getglobal(state, _configurationTable);
//lua_getfield(state, -1, key.c_str());
//if (lua_isnil(state, -1)) {
// LWARNING("Key '" << key << "' not found");
// return false;
//}
//if (!lua_isnumber(state, -1)) {
// LWARNING("Value of key '" << key <<"' is not a number");
// return false;
//}
//value = lua_tointeger(state, -1);
//lua_pop(state, lua_gettop(state));
return true;
}
bool setFullValue(lua_State* state, const std::string& key, const LUA_INTEGER& value) {
assert(state != nullptr);
lua_getglobal(state, _configurationTable);
lua_pushinteger(state, value);
lua_setfield(state, -2, key.c_str());
lua_pop(state, 1);
return true;
}
// int
template <>
bool ConfigurationManager::setValue(const std::string& key, const int& value) {
return setFullValue(_state, key, value);
}
template <>
bool ConfigurationManager::getValue(const std::string& key, int& value) {
lua_getglobal(_state, "getValue");
lua_pushstring(_state, key.c_str());
lua_pcall(_state, 1, 1, NULL);
if (lua_isnil(_state, -1)) {
lua_pop(_state, 1);
return false;
} else {
value = lua_tointeger(_state, -1);
lua_pop(_state, 1);
return true;
}
//lua_getglobal(_state, _configurationTable);
//LUA_INTEGER val;
//const bool result = getFullValue(_state, key, val);
//if (result)
// value = static_cast<int>(val);
//return result;
}
} // namespace openspace

View File

@@ -25,8 +25,11 @@
#ifndef __CONFIGURATIONMANAGER_H__
#define __CONFIGURATIONMANAGER_H__
#include <ghoul/lua/ghoul_lua.h>
#include <string>
struct lua_State;
namespace openspace {
class ConfigurationManager {
@@ -34,7 +37,7 @@ public:
ConfigurationManager();
~ConfigurationManager();
void initialize();
bool initialize();
void deinitialize();
void loadConfiguration(const std::string& filename, const std::string& position = "");
@@ -44,8 +47,13 @@ public:
template <class T>
bool getValue(const std::string& key, T& value);
//private:
lua_State* _state;
};
} // namespace openspace
#include "configurationmanager.inl"
#endif // __CONFIGURATIONMANAGER_H__

View File

@@ -0,0 +1,29 @@
/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014 *
* *
* 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 <ghoul/logging/logmanager.h>
namespace openspace {
} // namespace openspace

View File

@@ -160,7 +160,7 @@ int main(int argc, char **argv) {
openspace::OpenSpaceEngine::create(argc, argv);
char* cmd = "-config";
const std::string pathStr = p("${BASE_PATH}/config/single_sbs_stereo.xml");
const std::string pathStr = absPath("${BASE_PATH}/config/single.xml");
char* path = _strdup(pathStr.c_str());
char** newArgv = new char*[3];
int newArgc = 3;

View File

@@ -75,14 +75,13 @@ OpenSpaceEngine& OpenSpaceEngine::ref() {
void OpenSpaceEngine::create(int& argc, char**& argv) {
// TODO add parsing of configuration file, configuration file loading
LogManager::initialize(LogManager::LogLevel::Info);
LogManager::initialize(LogManager::LogLevel::Debug);
LogMgr.addLog(new ConsoleLog);
ghoul::filesystem::FileSystem::initialize();
FileSys.registerPathToken("${BASE_PATH}", "../..");
FileSys.registerPathToken("${BASE_PATH}", "../../..");
FileSys.registerPathToken("${SCRIPTS}", "${BASE_PATH}/scripts");
// TODO custom assert (ticket #5)
assert(_engine == nullptr);
_engine = new OpenSpaceEngine;
@@ -99,7 +98,47 @@ void OpenSpaceEngine::destroy() {
bool OpenSpaceEngine::initialize() {
_configurationManager->initialize();
_configurationManager->loadConfiguration(p("${SCRIPTS}/config.lua"));
_configurationManager->loadConfiguration("${SCRIPTS}/config.lua");
_configurationManager->loadConfiguration("${SCRIPTS}/config2.lua");
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
ghoul::lua::lua_logStack(_configurationManager->_state);
int v;
LINFO("setting2");
LARGE_INTEGER t1, t2;
QueryPerformanceCounter(&t1);
bool success = _configurationManager->getValue("setting2", v);
QueryPerformanceCounter(&t2);
LINFO("Get: " << ((t2.QuadPart - t1.QuadPart) / double(freq.QuadPart)) * 1000* 1000);
LINFO("Value:" << v);
QueryPerformanceCounter(&t1);
_configurationManager->setValue("setting2", 5);
QueryPerformanceCounter(&t2);
LINFO("Set: " << ((t2.QuadPart - t1.QuadPart) / double(freq.QuadPart)) * 1000* 1000);
success = _configurationManager->getValue("setting2", v);
LINFO("Value:" << v);
//_configurationManager->setValue("setting2", 5);
ghoul::lua::lua_logStack(_configurationManager->_state);
//success = _configurationManager->getValue("setting2", v);
LINFO("t.s");
QueryPerformanceCounter(&t1);
success = _configurationManager->getValue("t.s", v);
QueryPerformanceCounter(&t2);
LINFO(((t2.QuadPart - t1.QuadPart) / double(freq.QuadPart)) * 1000 * 1000);
ghoul::lua::lua_logStack(_configurationManager->_state);
LINFO("table.te.s.foo");
QueryPerformanceCounter(&t1);
success = _configurationManager->getValue("table.te.s.foo", v);
QueryPerformanceCounter(&t2);
LINFO(((t2.QuadPart - t1.QuadPart) / double(freq.QuadPart)) * 1000 * 1000);
ghoul::lua::lua_logStack(_configurationManager->_state);
Time::init();
Spice::init();
@@ -176,6 +215,7 @@ void OpenSpaceEngine::mouseScrollWheelCallback(int pos) {
_interactionHandler->mouseScrollWheelCallback(pos);
}
} // namespace openspace
/*

View File

@@ -40,7 +40,7 @@ void SceneGraph::init() {
std::string _loggerCat = "SceneGraph::init";
SceneGraphLoader *loader = new SceneGraphLoader(&nodes_, &shaders_);
root_ = loader->loadSceneGraph(p("${BASE_PATH}/modules"));
root_ = loader->loadSceneGraph(absPath("${BASE_PATH}/modules"));
update();
pss bs = root_->calculateBoundingSphere();
}

View File

@@ -68,7 +68,7 @@ void Spice::loadDefaultKernels() {
assert(this_);
// load
loadKernel(p("${BASE_PATH}/data/spice/de430_1850-2150.bsp"));
loadKernel(absPath("${BASE_PATH}/data/spice/de430_1850-2150.bsp"));
//Summary for: de430_1850-2150.bsp
//Bodies: MERCURY BARYCENTER (1) SATURN BARYCENTER (6) MERCURY (199)
// VENUS BARYCENTER (2) URANUS BARYCENTER (7) VENUS (299)
@@ -79,7 +79,7 @@ void Spice::loadDefaultKernels() {
// ----------------------------- -----------------------------
// 1849 DEC 26 00:00:00.000 2150 JAN 22 00:00:00.000
loadKernel(p("${BASE_PATH}/data/spice/pck00010.tpc"));
loadKernel(absPath("${BASE_PATH}/data/spice/pck00010.tpc"));
}
bool Spice::loadKernel(const std::string &path) {

View File

@@ -18,7 +18,7 @@ Time::Time() {
time_ = 0.0;
// load spice time kernel
furnsh_c (p("${BASE_PATH}/data/spice/naif0010.tls").c_str());
furnsh_c (absPath("${BASE_PATH}/data/spice/naif0010.tls").c_str());
// convert UTC to ET
str2et_c ( "2006 JAN 31 01:00", &time_ );