From 0eeeb8efd6a12c1bef8331d375094f768a4f0ea2 Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Sat, 13 Sep 2014 19:25:33 +0200 Subject: [PATCH] More work on fixing script access to time --- ext/ghoul | 2 +- scripts/default_startup.lua | 4 ++-- src/engine/openspaceengine.cpp | 5 ----- src/rendering/renderengine.cpp | 1 - src/scripting/scriptengine.cpp | 39 ++++++++++++++++++++++++++++------ src/util/time.cpp | 2 +- 6 files changed, 36 insertions(+), 17 deletions(-) diff --git a/ext/ghoul b/ext/ghoul index 6de5adc0c5..7906906863 160000 --- a/ext/ghoul +++ b/ext/ghoul @@ -1 +1 @@ -Subproject commit 6de5adc0c56dcb1069ff08e353d2e20ab2befcf7 +Subproject commit 79069068634b5a582d7fed49eb857dcc490471d3 diff --git a/scripts/default_startup.lua b/scripts/default_startup.lua index 92698abacc..e064f06acd 100644 --- a/scripts/default_startup.lua +++ b/scripts/default_startup.lua @@ -1,3 +1,3 @@ --openspace.setPropertyValue('Earth.renderable.colorTexture', '${OPENSPACE_DATA}/modules/mars/textures/mars.png') -openspace.setTime(1000000) -openspace.setDeltaTime(100) \ No newline at end of file +openspace.time.setTime(1000000) +openspace.time.setDeltaTime(100) \ No newline at end of file diff --git a/src/engine/openspaceengine.cpp b/src/engine/openspaceengine.cpp index aa019039a8..01ec011aa2 100644 --- a/src/engine/openspaceengine.cpp +++ b/src/engine/openspaceengine.cpp @@ -337,11 +337,6 @@ bool OpenSpaceEngine::initialize() // Register Lua script functions scriptEngine().addLibrary(Time::luaLibrary()); - //scripting::ScriptEngine::LuaLibrary timeLibrary = { - // "time", - //} - - // Load scenegraph SceneGraph* sceneGraph = new SceneGraph; _renderEngine->setSceneGraph(sceneGraph); diff --git a/src/rendering/renderengine.cpp b/src/rendering/renderengine.cpp index 85199e29f6..d86928517d 100644 --- a/src/rendering/renderengine.cpp +++ b/src/rendering/renderengine.cpp @@ -205,7 +205,6 @@ void RenderEngine::render() const PowerScaledScalar pssl = (position - origin).length(); const std::string time = Time::ref().currentTimeUTC().c_str(); - LINFO(time); Freetype::print( sgct_text::FontManager::instance()->getFont("SGCTFont", FONT_SIZE), FONT_SIZE, FONT_SIZE * 18, "Date: %s", time.c_str() diff --git a/src/scripting/scriptengine.cpp b/src/scripting/scriptengine.cpp index 6d90fdb3da..1799dd55e7 100644 --- a/src/scripting/scriptengine.cpp +++ b/src/scripting/scriptengine.cpp @@ -30,6 +30,7 @@ #include #include +#include #include namespace openspace { @@ -88,22 +89,34 @@ bool ScriptEngine::addLibrary(const ScriptEngine::LuaLibrary& library) { return false; } + //ghoul::lua::logStack(_state); lua_getglobal(_state, _openspaceLibraryName.c_str()); + //ghoul::lua::logStack(_state); if (library.name.empty()) { + //ghoul::lua::logStack(_state); addLibraryFunctions(library, true); + //ghoul::lua::logStack(_state); + lua_pop(_state, 1); + //ghoul::lua::logStack(_state); } else { const bool allowed = isLibraryNameAllowed(library.name); if (!allowed) return false; + //ghoul::lua::logStack(_state); + lua_pushstring(_state, library.name.c_str()); + //ghoul::lua::logStack(_state); lua_newtable(_state); + //ghoul::lua::logStack(_state); addLibraryFunctions(library, false); lua_settable(_state, _setTableOffset); - + //ghoul::lua::logStack(_state); + _registeredLibraries.insert(ghoul::hashCRC32(library.name)); } + return true; } @@ -199,8 +212,8 @@ bool ScriptEngine::isLibraryNameAllowed(const std::string& name) LERROR("Library name '" << name << "' specifies a thread"); break; } - - lua_pop(_state, 1); + + lua_pop(_state, 2); return result; } @@ -208,16 +221,23 @@ void ScriptEngine::addLibraryFunctions(const LuaLibrary& library, bool replace) { for (std::pair p : library.functions) { if (!replace) { + ghoul::lua::logStack(_state); lua_getfield(_state, -1, p.first.c_str()); + ghoul::lua::logStack(_state); const bool isNil = lua_isnil(_state, -1); if (!isNil) { LERROR("Function name '" << p.first << "' was already assigned"); return; } + lua_pop(_state, 1); } + //ghoul::lua::logStack(_state); lua_pushstring(_state, p.first.c_str()); + //ghoul::lua::logStack(_state); lua_pushcfunction(_state, p.second); + //ghoul::lua::logStack(_state); lua_settable(_state, _setTableOffset); + //ghoul::lua::logStack(_state); } } @@ -236,10 +256,15 @@ void ScriptEngine::addBaseLibrary() { } void ScriptEngine::remapPrintFunction() { - lua_getglobal(_state, _luaGlobalNamespace.c_str()); - lua_pushstring(_state, _printFunctionName.c_str()); - lua_pushcfunction(_state, _printFunctionReplacement); - lua_settable(_state, _setTableOffset); + //ghoul::lua::logStack(_state); + // lua_getglobal(_state, _luaGlobalNamespace.c_str()); + //ghoul::lua::logStack(_state); + // lua_pushstring(_state, _printFunctionName.c_str()); + //ghoul::lua::logStack(_state); + // lua_pushcfunction(_state, _printFunctionReplacement); + //ghoul::lua::logStack(_state); + // lua_settable(_state, _setTableOffset); + //ghoul::lua::logStack(_state); } } // namespace scripting diff --git a/src/util/time.cpp b/src/util/time.cpp index 01bc1c0837..441586d77b 100644 --- a/src/util/time.cpp +++ b/src/util/time.cpp @@ -136,7 +136,7 @@ std::string Time::currentTimeUTC() const { scripting::ScriptEngine::LuaLibrary Time::luaLibrary() { scripting::ScriptEngine::LuaLibrary timeLibrary = { - "", + "time", { {"setDeltaTime", &time_setDeltaTime}, {"deltaTime", &time_deltaTime},