mirror of
https://github.com/OpenSpace/OpenSpace.git
synced 2026-02-22 12:59:07 -06:00
Add Lua function to query property identifiers
This commit is contained in:
@@ -605,6 +605,14 @@ scripting::LuaLibrary Scene::luaLibrary() {
|
||||
"Returns the value the property, identified by "
|
||||
"the provided URI."
|
||||
},
|
||||
{
|
||||
"getProperty",
|
||||
&luascriptfunctions::property_getProperty,
|
||||
{},
|
||||
"string",
|
||||
"Returns a list of property identifiers that match the passed regular "
|
||||
"expression"
|
||||
},
|
||||
{
|
||||
"loadScene",
|
||||
&luascriptfunctions::loadScene,
|
||||
|
||||
@@ -401,6 +401,51 @@ int property_getValue(lua_State* L) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* \ingroup LuaScripts
|
||||
* getProperty
|
||||
* Returns a list of property identifiers that match the passed regular expression
|
||||
*/
|
||||
int property_getProperty(lua_State* L) {
|
||||
ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::property_getProperty");
|
||||
|
||||
std::string regex = ghoul::lua::value<std::string>(L, 1);
|
||||
lua_pop(L, 1);
|
||||
|
||||
|
||||
// Replace all wildcards * with the correct regex (.*)
|
||||
size_t startPos = regex.find("*");
|
||||
while (startPos != std::string::npos) {
|
||||
regex.replace(startPos, 1, "(.*)");
|
||||
startPos += 4; // (.*)
|
||||
startPos = regex.find("*", startPos);
|
||||
}
|
||||
|
||||
|
||||
|
||||
std::regex r(regex);
|
||||
std::vector<properties::Property*> props = allProperties();
|
||||
std::vector<std::string> res;
|
||||
for (properties::Property* prop : props) {
|
||||
// Check the regular expression for all properties
|
||||
const std::string& id = prop->fullyQualifiedIdentifier();
|
||||
|
||||
if (std::regex_match(id, r)) {
|
||||
res.push_back(id);
|
||||
}
|
||||
}
|
||||
|
||||
lua_newtable(L);
|
||||
int number = 1;
|
||||
for (const std::string& s : res) {
|
||||
lua_pushstring(L, s.c_str());
|
||||
lua_rawseti(L, -2, number);
|
||||
++number;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* \ingroup LuaScripts
|
||||
* getPropertyValue(string):
|
||||
|
||||
Reference in New Issue
Block a user