From 351a246f3a08e6fbd41cf351182f5da5024e0ee9 Mon Sep 17 00:00:00 2001 From: Emil Axelsson Date: Thu, 6 Jul 2017 22:30:57 +0200 Subject: [PATCH] Add support for c closures in lua --- include/openspace/scripting/lualibrary.h | 2 ++ src/engine/moduleengine.cpp | 1 + src/engine/openspaceengine.cpp | 6 ++++++ src/engine/wrapper/windowwrapper.cpp | 1 + src/interaction/interactionhandler.cpp | 8 ++++++++ src/mission/missionmanager.cpp | 2 ++ src/network/parallelconnection.cpp | 4 ++++ src/rendering/renderengine.cpp | 6 ++++++ src/scene/scene.cpp | 7 +++++++ src/scripting/scriptengine.cpp | 17 ++++++++++++++++- src/scripting/scriptscheduler.cpp | 3 +++ src/scripting/systemcapabilitiesbinding.cpp | 20 ++++++++++++++++++++ src/util/spicemanager.cpp | 2 ++ src/util/time.cpp | 8 ++++++++ 14 files changed, 86 insertions(+), 1 deletion(-) diff --git a/include/openspace/scripting/lualibrary.h b/include/openspace/scripting/lualibrary.h index e49adc1026..f970e4fe86 100644 --- a/include/openspace/scripting/lualibrary.h +++ b/include/openspace/scripting/lualibrary.h @@ -48,6 +48,8 @@ struct LuaLibrary { std::string name; /// The function pointer that is executed if the function is called lua_CFunction function; + /// A vector of light userdata to be passed into the function + std::vector userdata; /// A text describing the arugments to this function std::string argumentText; /// A help text describing what the function does/ diff --git a/src/engine/moduleengine.cpp b/src/engine/moduleengine.cpp index 0fb80fda46..829f6a9c97 100644 --- a/src/engine/moduleengine.cpp +++ b/src/engine/moduleengine.cpp @@ -104,6 +104,7 @@ scripting::LuaLibrary ModuleEngine::luaLibrary() { { "isLoaded", &luascriptfunctions::isLoaded, + {}, "string", "Checks whether a specific module is loaded" } diff --git a/src/engine/openspaceengine.cpp b/src/engine/openspaceengine.cpp index 25acac1fce..b46b92bafb 100644 --- a/src/engine/openspaceengine.cpp +++ b/src/engine/openspaceengine.cpp @@ -1258,6 +1258,7 @@ scripting::LuaLibrary OpenSpaceEngine::luaLibrary() { { "toggleShutdown", &luascriptfunctions::toggleShutdown, + {}, "", "Toggles the shutdown mode that will close the application after the count" "down timer is reached" @@ -1265,30 +1266,35 @@ scripting::LuaLibrary OpenSpaceEngine::luaLibrary() { { "writeDocumentation", &luascriptfunctions::writeDocumentation, + {}, "", "Writes out documentation files" }, { "downloadFile", &luascriptfunctions::downloadFile, + {}, "", "Downloads a file from Lua scope" }, { "addVirtualProperty", &luascriptfunctions::addVirtualProperty, + {}, "type, name, identifier, [value, minimumValue, maximumValue]", "Adds a virtual property that will set a group of properties" }, { "removeVirtualProperty", &luascriptfunctions::removeVirtualProperty, + {}, "string", "Removes a previously added virtual property" }, { "removeAllVirtualProperties", &luascriptfunctions::removeAllVirtualProperties, + {}, "", "Remove all registered virtual properties" } diff --git a/src/engine/wrapper/windowwrapper.cpp b/src/engine/wrapper/windowwrapper.cpp index 4629d59592..0f895da92a 100644 --- a/src/engine/wrapper/windowwrapper.cpp +++ b/src/engine/wrapper/windowwrapper.cpp @@ -63,6 +63,7 @@ scripting::LuaLibrary WindowWrapper::luaLibrary() { { "setSynchronization", &luascriptfunctions::setSynchronization, + {}, "bool", "Enables or disables the frame synchronization of the cluster. If the " "synchronization is enabled, the computers in the cluster will swap " diff --git a/src/interaction/interactionhandler.cpp b/src/interaction/interactionhandler.cpp index 12bea7f7c9..c223936fc6 100644 --- a/src/interaction/interactionhandler.cpp +++ b/src/interaction/interactionhandler.cpp @@ -474,12 +474,14 @@ scripting::LuaLibrary InteractionHandler::luaLibrary() { { "clearKeys", &luascriptfunctions::clearKeys, + {}, "", "Clear all key bindings" }, { "bindKey", &luascriptfunctions::bindKey, + {}, "string, string [,string]", "Binds a key by name to a lua string command to execute both locally " "and to broadcast to clients if this is the host of a parallel session. " @@ -490,6 +492,7 @@ scripting::LuaLibrary InteractionHandler::luaLibrary() { { "bindKeyLocal", &luascriptfunctions::bindKeyLocal, + {}, "string, string [,string]", "Binds a key by name to a lua string command to execute only locally. " "The first argument is the key, the second argument is the Lua command " @@ -499,30 +502,35 @@ scripting::LuaLibrary InteractionHandler::luaLibrary() { { "saveCameraStateToFile", &luascriptfunctions::saveCameraStateToFile, + {}, "string", "Save the current camera state to file" }, { "restoreCameraStateFromFile", &luascriptfunctions::restoreCameraStateFromFile, + {}, "string", "Restore the camera state from file" }, { "resetCameraDirection", &luascriptfunctions::resetCameraDirection, + {}, "void", "Reset the camera direction to point at the focus node" }, { "goToChunk", &luascriptfunctions::goToChunk, + {}, "void", "Go to chunk with given index x, y, level" }, { "goToGeo", &luascriptfunctions::goToGeo, + {}, "void", "Go to geographic coordinates latitude and longitude" }, diff --git a/src/mission/missionmanager.cpp b/src/mission/missionmanager.cpp index 224c894c38..f35bbb7e37 100644 --- a/src/mission/missionmanager.cpp +++ b/src/mission/missionmanager.cpp @@ -95,12 +95,14 @@ scripting::LuaLibrary MissionManager::luaLibrary() { { "loadMission", &luascriptfunctions::loadMission, + {}, "string", "Load mission phases from file" }, { "setCurrentMission", &luascriptfunctions::setCurrentMission, + {}, "string", "Set the currnet mission" }, diff --git a/src/network/parallelconnection.cpp b/src/network/parallelconnection.cpp index 18ef602172..3e162263e7 100644 --- a/src/network/parallelconnection.cpp +++ b/src/network/parallelconnection.cpp @@ -1168,24 +1168,28 @@ scripting::LuaLibrary ParallelConnection::luaLibrary() { { "connect", &luascriptfunctions::connect, + {}, "", "Connect to parallel" }, { "disconnect", &luascriptfunctions::disconnect, + {}, "", "Disconnect from parallel" }, { "requestHostship", &luascriptfunctions::requestHostship, + {}, "", "Request to be the host for this session" }, { "resignHostship", &luascriptfunctions::resignHostship, + {}, "", "Resign hostship" }, diff --git a/src/rendering/renderengine.cpp b/src/rendering/renderengine.cpp index e37a913406..afc3506468 100644 --- a/src/rendering/renderengine.cpp +++ b/src/rendering/renderengine.cpp @@ -695,18 +695,21 @@ scripting::LuaLibrary RenderEngine::luaLibrary() { { "setRenderer", &luascriptfunctions::setRenderer, + {}, "string", "Sets the renderer (ABuffer or FrameBuffer)" }, { "toggleFade", &luascriptfunctions::toggleFade, + {}, "number", "Toggles fading in or out" }, { "fadeIn", &luascriptfunctions::fadeIn, + {}, "number", "" }, @@ -714,18 +717,21 @@ scripting::LuaLibrary RenderEngine::luaLibrary() { { "fadeOut", &luascriptfunctions::fadeOut, + {}, "number", "" }, { "registerScreenSpaceRenderable", &luascriptfunctions::registerScreenSpaceRenderable, + {}, "table", "Will create a ScreenSpaceRenderable from a lua Table and register it in the RenderEngine" }, { "unregisterScreenSpaceRenderable", &luascriptfunctions::unregisterScreenSpaceRenderable, + {}, "string", "Given a ScreenSpaceRenderable name this script will remove it from the renderengine" }, diff --git a/src/scene/scene.cpp b/src/scene/scene.cpp index 3689eb2b4f..93e6cf47f0 100644 --- a/src/scene/scene.cpp +++ b/src/scene/scene.cpp @@ -391,6 +391,7 @@ scripting::LuaLibrary Scene::luaLibrary() { { "setPropertyValue", &luascriptfunctions::property_setValue, + {}, "string, *", "Sets all property(s) identified by the URI (with potential wildcards) " "in the first argument. The second argument can be any type, but it has " @@ -403,6 +404,7 @@ scripting::LuaLibrary Scene::luaLibrary() { { "setPropertyValueRegex", &luascriptfunctions::property_setValueRegex, + {}, "string, *", "Sets all property(s) that pass the regular expression in the first " "argument. The second argument can be any type, but it has to match " @@ -416,6 +418,7 @@ scripting::LuaLibrary Scene::luaLibrary() { { "setPropertyValueSingle", &luascriptfunctions::property_setValueSingle, + {}, "string, *", "Sets all property(s) identified by the URI in the first argument to the " "value passed in the second argument. The type of the second argument is " @@ -428,6 +431,7 @@ scripting::LuaLibrary Scene::luaLibrary() { { "getPropertyValue", &luascriptfunctions::property_getValue, + {}, "string", "Returns the value the property, identified by " "the provided URI." @@ -435,6 +439,7 @@ scripting::LuaLibrary Scene::luaLibrary() { { "loadScene", &luascriptfunctions::loadScene, + {}, "string", "Loads the scene found at the file passed as an " "argument. If a scene is already loaded, it is unloaded first" @@ -442,6 +447,7 @@ scripting::LuaLibrary Scene::luaLibrary() { { "addSceneGraphNode", &luascriptfunctions::addSceneGraphNode, + {}, "table", "Loads the SceneGraphNode described in the table and adds it to the " "SceneGraph" @@ -449,6 +455,7 @@ scripting::LuaLibrary Scene::luaLibrary() { { "removeSceneGraphNode", &luascriptfunctions::removeSceneGraphNode, + {}, "string", "Removes the SceneGraphNode identified by name" } diff --git a/src/scripting/scriptengine.cpp b/src/scripting/scriptengine.cpp index abdd72a3e6..759f2b27aa 100644 --- a/src/scripting/scriptengine.cpp +++ b/src/scripting/scriptengine.cpp @@ -369,7 +369,10 @@ void ScriptEngine::addLibraryFunctions(lua_State* state, LuaLibrary& library, bo lua_pop(state, 1); } lua_pushstring(state, p.name.c_str()); - lua_pushcfunction(state, p.function); + for (void* d : p.userdata) { + lua_pushlightuserdata(state, d); + } + lua_pushcclosure(state, p.function, p.userdata.size()); lua_settable(state, TableOffset); } @@ -421,6 +424,7 @@ void ScriptEngine::addBaseLibrary() { { "printTrace", &luascriptfunctions::printTrace, + {}, "*", "Logs the passed value to the installed LogManager with a LogLevel of " "'Trace'" @@ -428,6 +432,7 @@ void ScriptEngine::addBaseLibrary() { { "printDebug", &luascriptfunctions::printDebug, + {}, "*", "Logs the passed value to the installed LogManager with a LogLevel of " "'Debug'" @@ -435,6 +440,7 @@ void ScriptEngine::addBaseLibrary() { { "printInfo", &luascriptfunctions::printInfo, + {}, "*", "Logs the passed value to the installed LogManager with a LogLevel of " "'Info'" @@ -442,6 +448,7 @@ void ScriptEngine::addBaseLibrary() { { "printWarning", &luascriptfunctions::printWarning, + {}, "*", "Logs the passed value to the installed LogManager with a LogLevel of " "'Warning'" @@ -449,6 +456,7 @@ void ScriptEngine::addBaseLibrary() { { "printError", &luascriptfunctions::printError, + {}, "*", "Logs the passed value to the installed LogManager with a LogLevel of " "'Error'" @@ -456,6 +464,7 @@ void ScriptEngine::addBaseLibrary() { { "printFatal", &luascriptfunctions::printFatal, + {}, "*", "Logs the passed value to the installed LogManager with a LogLevel of " "'Fatal'" @@ -463,6 +472,7 @@ void ScriptEngine::addBaseLibrary() { { "absPath", &luascriptfunctions::absolutePath, + {}, "string", "Returns the absolute path to the passed path, resolving path tokens as " "well as resolving relative paths" @@ -470,12 +480,14 @@ void ScriptEngine::addBaseLibrary() { { "fileExists", &luascriptfunctions::fileExists, + {}, "string", "Checks whether the provided file exists." }, { "setPathToken", &luascriptfunctions::setPathToken, + {}, "string, string", "Registers a new path token provided by the first argument to the path " "provided in the second argument" @@ -483,6 +495,7 @@ void ScriptEngine::addBaseLibrary() { { "walkDirectory", &luascriptfunctions::walkDirectory, + {}, "string [bool, bool]", "Walks a directory and returns all contents (files and directories) of " "the directory as absolute paths. The first argument is the path of the " @@ -493,6 +506,7 @@ void ScriptEngine::addBaseLibrary() { { "walkDirectoryFiles", &luascriptfunctions::walkDirectoryFiles, + {}, "string [bool, bool]", "Walks a directory and returns the files of the directory as absolute " "paths. The first argument is the path of the directory that should be " @@ -503,6 +517,7 @@ void ScriptEngine::addBaseLibrary() { { "walkDirectoryFolder", &luascriptfunctions::walkDirectoryFolder, + {}, "string [bool, bool]", "Walks a directory and returns the subfolders of the directory as " "absolute paths. The first argument is the path of the directory that " diff --git a/src/scripting/scriptscheduler.cpp b/src/scripting/scriptscheduler.cpp index dd01785e9e..1c1a9618b9 100644 --- a/src/scripting/scriptscheduler.cpp +++ b/src/scripting/scriptscheduler.cpp @@ -269,6 +269,7 @@ LuaLibrary ScriptScheduler::luaLibrary() { { "loadFile", &luascriptfunctions::loadFile, + {}, "string", "Load timed scripts from a Lua script file that returns a list of " "scheduled scripts." @@ -276,6 +277,7 @@ LuaLibrary ScriptScheduler::luaLibrary() { { "loadScheduledScript", &luascriptfunctions::loadScheduledScript, + {}, "string, string, (string, string)", "Load a single scheduled script. The first argument is the time at which " "the scheduled script is triggered, the second argument is the script " @@ -287,6 +289,7 @@ LuaLibrary ScriptScheduler::luaLibrary() { { "clear", &luascriptfunctions::clear, + {}, "", "Clears all scheduled scripts." }, diff --git a/src/scripting/systemcapabilitiesbinding.cpp b/src/scripting/systemcapabilitiesbinding.cpp index 60fdd30982..2d30fd5b21 100644 --- a/src/scripting/systemcapabilitiesbinding.cpp +++ b/src/scripting/systemcapabilitiesbinding.cpp @@ -201,6 +201,7 @@ LuaLibrary generalSystemCapabilities() { { "operatingSystem", &luascripting::general::operatingSystem, + {}, "", "Returns a parsed string of the operating system type, for example " "Windows, Linux, MacOS, or others, together with the specific version, " @@ -209,6 +210,7 @@ LuaLibrary generalSystemCapabilities() { { "fullOperatingSystem", &luascripting::general::fullOperatingSystem, + {}, "", "Returns the operating system as a string. The exact format of the " "returned string is implementation and operating system-dependent but it " @@ -217,6 +219,7 @@ LuaLibrary generalSystemCapabilities() { { "installedMainMemory", &luascripting::general::installedMainMemory, + {}, "", "Returns the amount of available, installed main memory (RAM) on the " "system in MB." @@ -224,30 +227,35 @@ LuaLibrary generalSystemCapabilities() { { "cores", &luascripting::general::cores, + {}, "", "Returns the number of cores." }, { "cacheLineSize", &luascripting::general::cacheLineSize, + {}, "", "Returns the cache line size." }, { "L2Associativity", &luascripting::general::L2Associativity, + {}, "", "Returns the L2 associativity." }, { "cacheSize", &luascripting::general::cacheSize, + {}, "", "Returns the cache size." }, { "extensions", &luascripting::general::extensions, + {}, "", "Returns all supported exteions as comma-separated string." } @@ -262,6 +270,7 @@ LuaLibrary openglSystemCapabilities() { { "hasOpenGLVersion", &luascripting::opengl::hasOpenGLVersion, + {}, "string", "Tests whether the current instance supports the passed OpenGL version. " "The parameter has to have the form 'X.Y' or 'X.Y.Z'." @@ -269,12 +278,14 @@ LuaLibrary openglSystemCapabilities() { { "openGLVersion", &luascripting::opengl::openGLVersion, + {}, "", "Returns the maximum OpenGL version that is supported on this platform." }, { "glslCompiler", &luascripting::opengl::glslCompiler, + {}, "", "Returns the value of a call to glGetString(GL_VENDOR). " "This will give detailed information about the vendor of the main " @@ -284,24 +295,28 @@ LuaLibrary openglSystemCapabilities() { { "gpuVendor", &luascripting::opengl::gpuVendor, + {}, "", "Returns the vendor of the main graphics card." }, { "extensions", &luascripting::opengl::extensions, + {}, "", "Returns all available extensions as a list of names." }, { "isExtensionSupported", &luascripting::opengl::isExtensionSupported, + {}, "string", "Checks is a specific extension is supported or not." }, { "maxTextureUnits", &luascripting::opengl::maxTextureUnits, + {}, "", "Returns the maximum number of texture units that are available on the " "main graphics card." @@ -309,18 +324,21 @@ LuaLibrary openglSystemCapabilities() { { "max2DTextureSize", &luascripting::opengl::max2DTextureSize, + {}, "", "Returns the largest dimension for a 2D texture on this graphics card." }, { "max3DTextureSize", &luascripting::opengl::max3DTextureSize, + {}, "", "Returns the largest dimension for a 3D texture on this graphics card." }, { "maxAtomicCounterBufferBindings", &luascripting::opengl::maxAtomicCounterBufferBindings, + {}, "", "Returns the maximum number of atomic counter buffer bindings that are " "available on the main graphics card." @@ -328,6 +346,7 @@ LuaLibrary openglSystemCapabilities() { { "maxShaderStorageBufferBindings", &luascripting::opengl::maxShaderStorageBufferBindings, + {}, "", "Returns the maximum number of shader storage bindings that are " "available on the main graphics card." @@ -335,6 +354,7 @@ LuaLibrary openglSystemCapabilities() { { "maxUniformBufferBindings", &luascripting::opengl::maxUniformBufferBindings, + {}, "", "Returns the maximum number of uniform buffer bindings that are " "available on the main graphics card." diff --git a/src/util/spicemanager.cpp b/src/util/spicemanager.cpp index d73edaec64..aa1016a181 100644 --- a/src/util/spicemanager.cpp +++ b/src/util/spicemanager.cpp @@ -1192,6 +1192,7 @@ scripting::LuaLibrary SpiceManager::luaLibrary() { { "loadKernel", &luascriptfunctions::loadKernel, + {}, "string", "Loads the provided SPICE kernel by name. The name can contain path " "tokens, which are automatically resolved" @@ -1199,6 +1200,7 @@ scripting::LuaLibrary SpiceManager::luaLibrary() { { "unloadKernel", &luascriptfunctions::unloadKernel, + {}, "{string, number}", "Unloads the provided SPICE kernel. The name can contain path tokens, " "which are automatically resolved" diff --git a/src/util/time.cpp b/src/util/time.cpp index 12729b314f..3e55dc3745 100644 --- a/src/util/time.cpp +++ b/src/util/time.cpp @@ -160,6 +160,7 @@ scripting::LuaLibrary Time::luaLibrary() { { "setDeltaTime", &luascriptfunctions::time_setDeltaTime, + {}, "number", "Sets the amount of simulation time that happens " "in one second of real time" @@ -167,6 +168,7 @@ scripting::LuaLibrary Time::luaLibrary() { { "deltaTime", &luascriptfunctions::time_deltaTime, + {}, "", "Returns the amount of simulated time that passes in one " "second of real time" @@ -174,12 +176,14 @@ scripting::LuaLibrary Time::luaLibrary() { { "setPause", &luascriptfunctions::time_setPause, + {}, "bool", "Pauses the simulation time or restores the delta time" }, { "togglePause", &luascriptfunctions::time_togglePause, + {}, "", "Toggles the pause function, i.e. temporarily setting the delta time to 0" " and restoring it afterwards" @@ -187,6 +191,7 @@ scripting::LuaLibrary Time::luaLibrary() { { "setTime", &luascriptfunctions::time_setTime, + {}, "{number, string}", "Sets the current simulation time to the " "specified value. If the parameter is a number, the value is the number " @@ -196,6 +201,7 @@ scripting::LuaLibrary Time::luaLibrary() { { "currentTime", &luascriptfunctions::time_currentTime, + {}, "", "Returns the current time as the number of seconds since " "the J2000 epoch" @@ -203,6 +209,7 @@ scripting::LuaLibrary Time::luaLibrary() { { "UTC", &luascriptfunctions::time_currentTimeUTC, + {}, "", "Returns the current time as an ISO 8601 date string " "(YYYY-MM-DDTHH:MN:SS)" @@ -210,6 +217,7 @@ scripting::LuaLibrary Time::luaLibrary() { { "currentWallTime", &luascriptfunctions::time_currentWallTime, + {}, "", "Returns the current wall time as an ISO 8601 date string " "(YYYY-MM-DDTHH-MN-SS) in the UTC timezone"