diff --git a/include/openspace/scripting/scriptengine.h b/include/openspace/scripting/scriptengine.h index f9c3b3bac0..5e804ac092 100644 --- a/include/openspace/scripting/scriptengine.h +++ b/include/openspace/scripting/scriptengine.h @@ -28,6 +28,7 @@ #include #include +#include /** * \defgroup LuaScripts Lua Scripts @@ -89,9 +90,16 @@ public: void preSynchronization(); void queueScript(const std::string &script); + + std::vector cachedScripts(); std::vector allLuaFunctions() const; + //parallel functions + bool parseLibraryAndFunctionNames(std::string &library, std::string &function, const std::string &script); + bool shouldScriptBeSent(const std::string &library, const std::string &function); + void cacheScript(const std::string &library, const std::string &function, const std::string &script); + private: bool registerLuaLibrary(lua_State* state, const LuaLibrary& library); void addLibraryFunctions(lua_State* state, const LuaLibrary& library, bool replace); @@ -100,8 +108,6 @@ private: void addBaseLibrary(); void remapPrintFunction(); - - bool findLibraryAndFunction(std::string &library, std::string &function, const std::string &script); lua_State* _state; std::set _registeredLibraries; @@ -112,6 +118,10 @@ private: std::vector _receivedScripts; std::string _currentSyncedScript; + //parallel variables + std::map> _cachedScripts; + std::mutex _cachedScriptsMutex; + }; } // namespace scripting diff --git a/src/scripting/scriptengine.cpp b/src/scripting/scriptengine.cpp index f69c5140e4..f55b7d2449 100644 --- a/src/scripting/scriptengine.cpp +++ b/src/scripting/scriptengine.cpp @@ -159,40 +159,95 @@ bool ScriptEngine::runScript(const std::string& script) { if (OsEng.parallelConnection()->isHost()){ std::string lib, func; - if (findLibraryAndFunction(lib, func, script)){ - - LuaLibrary *library = nullptr; - std::set::const_iterator libit; - for (libit = _registeredLibraries.cbegin(); - libit != _registeredLibraries.cend(); - ++libit){ - if (libit->name.compare(lib) == 0){ - break; - } - } - - std::vector::const_iterator funcit; - //library was found - if (libit != _registeredLibraries.cend()){ - for (funcit = libit->functions.cbegin(); - funcit != libit->functions.cend(); - ++funcit){ - //function was found! - if (funcit->name.compare(func) == 0){ - //is the function of a type that should be shared via parallel connection? - if (funcit->parallelShared){ - OsEng.parallelConnection()->sendScript(script); - } - } - } - } + if (parseLibraryAndFunctionNames(lib, func, script) && shouldScriptBeSent(lib, func)){ + OsEng.parallelConnection()->sendScript(script); + cacheScript(lib, func, script); } } return true; } + + +bool ScriptEngine::runScriptFile(const std::string& filename) { + if (filename.empty()) { + LWARNING("Filename was empty"); + return false; + } + if (!FileSys.fileExists(filename)) { + LERROR("Script with name '" << filename << "' did not exist"); + return false; + } + + int status = luaL_loadfile(_state, filename.c_str()); + if (status != LUA_OK) { + LERROR("Error loading script: '" << lua_tostring(_state, -1) << "'"); + return false; + } + + LDEBUG("Executing script '" << filename << "'"); + if (lua_pcall(_state, 0, LUA_MULTRET, 0)) { + LERROR("Error executing script: " << lua_tostring(_state, -1)); + return false; + } -bool ScriptEngine::findLibraryAndFunction(std::string &library, std::string &function, const std::string &script){ + return true; +} + +bool ScriptEngine::shouldScriptBeSent(const std::string &library, const std::string &function){ + + std::set::const_iterator libit; + for (libit = _registeredLibraries.cbegin(); + libit != _registeredLibraries.cend(); + ++libit){ + if (libit->name.compare(library) == 0){ + break; + } + } + + std::vector::const_iterator funcit; + //library was found + if (libit != _registeredLibraries.cend()){ + for (funcit = libit->functions.cbegin(); + funcit != libit->functions.cend(); + ++funcit){ + //function was found! + if (funcit->name.compare(function) == 0){ + //is the function of a type that should be shared via parallel connection? + return funcit->parallelShared; + } + } + } + + return false; +} + +void ScriptEngine::cacheScript(const std::string &library, const std::string &function, const std::string &script){ + _cachedScriptsMutex.lock(); + _cachedScripts[library][function] = script; + _cachedScriptsMutex.unlock(); +} + +std::vector ScriptEngine::cachedScripts(){ + _cachedScriptsMutex.lock(); + + std::vector retVal; + std::map>::const_iterator outerIt; + std::map::const_iterator innerIt; + for(outerIt = _cachedScripts.cbegin(); + outerIt != _cachedScripts.cend(); + ++outerIt){ + for(innerIt = outerIt->second.cbegin(); + innerIt != outerIt->second.cend(); + ++innerIt){ + + } + } + + _cachedScriptsMutex.unlock(); +} + +bool ScriptEngine::parseLibraryAndFunctionNames(std::string &library, std::string &function, const std::string &script){ //"deconstruct the script to find library and function name //assuming a script looks like: "openspace.library.function()" @@ -234,31 +289,6 @@ bool ScriptEngine::findLibraryAndFunction(std::string &library, std::string &fun return !function.empty(); } -bool ScriptEngine::runScriptFile(const std::string& filename) { - if (filename.empty()) { - LWARNING("Filename was empty"); - return false; - } - if (!FileSys.fileExists(filename)) { - LERROR("Script with name '" << filename << "' did not exist"); - return false; - } - - int status = luaL_loadfile(_state, filename.c_str()); - if (status != LUA_OK) { - LERROR("Error loading script: '" << lua_tostring(_state, -1) << "'"); - return false; - } - - LDEBUG("Executing script '" << filename << "'"); - if (lua_pcall(_state, 0, LUA_MULTRET, 0)) { - LERROR("Error executing script: " << lua_tostring(_state, -1)); - return false; - } - - return true; -} - bool ScriptEngine::hasLibrary(const std::string& name) { auto it = std::find_if(