Add function allowing insight into currently loaded assets. Don't print info message when loading asset that was already loaded

This commit is contained in:
Alexander Bock
2022-02-21 21:19:13 +01:00
parent abaaf437b6
commit ae40493627
3 changed files with 48 additions and 1 deletions
+4 -1
View File
@@ -40,7 +40,10 @@ if is_image_file(extension) then
TexturePath = "]] .. filename .. [["
});]] .. ReloadUIScript
elseif extension == ".asset" then
return [[openspace.printInfo("Adding asset: ']] .. filename .. [[' (drag-and-drop)");
return [[
if openspace.asset.isLoaded("]] .. filename .. [[") ~= true then
openspace.printInfo("Adding asset: ']] .. filename .. [[' (drag-and-drop)");
end
openspace.asset.add("]] .. filename .. [[");]] .. ReloadUIScript
elseif extension == ".osrec" or extension == ".osrectxt" then
return [[openspace.sessionRecording.startPlayback("]] .. filename .. [[")]]
+15
View File
@@ -885,6 +885,21 @@ scripting::LuaLibrary AssetManager::luaLibrary() {
"Removes the asset with the specfied name from the scene. The parameter "
"to this function is the same that was originally used to load this "
"asset, i.e. the path to the asset file"
},
{
"isLoaded",
&luascriptfunctions::asset::isLoaded,
"string",
"Returns true if the referenced asset already has been loaded. Otherwise "
"false is returned. The parameter to this function is the path of the "
"asset that should be tested"
},
{
"allAssets",
&luascriptfunctions::asset::allAssets,
"",
"Returns the paths to all loaded assets, loaded directly or indirectly, "
"as a table containing the paths to all loaded assets."
}
}
};
+29
View File
@@ -42,4 +42,33 @@ int remove(lua_State* L) {
return 0;
}
int isLoaded(lua_State* L) {
ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::isLoaded");
const std::string assetName = ghoul::lua::value<std::string>(L);
std::vector<const Asset*> as = global::openSpaceEngine->assetManager().allAssets();
for (const Asset* a : as) {
if (a->path() == assetName) {
ghoul::lua::push(L, true);
return 1;
}
}
ghoul::lua::push(L, false);
return 1;
}
int allAssets(lua_State* L) {
ghoul::lua::checkArgumentsAndThrow(L, 0, "lua::allAssets");
std::vector<const Asset*> as = global::openSpaceEngine->assetManager().allAssets();
std::vector<std::string> res;
res.reserve(as.size());
for (const Asset* a : as) {
res.push_back(a->path().string());
}
ghoul::lua::push(L, res);
return 1;
}
} // namespace openspace::luascriptfunctions