Add Lua function to query property identifiers

This commit is contained in:
Alexander Bock
2019-01-10 23:06:35 +01:00
parent ebf55f3029
commit be6915ecc1
2 changed files with 53 additions and 0 deletions

View File

@@ -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,

View File

@@ -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):