diff --git a/apps/openmw/mwlua/luabindings.cpp b/apps/openmw/mwlua/luabindings.cpp index 9d1416b813..a25f1db124 100644 --- a/apps/openmw/mwlua/luabindings.cpp +++ b/apps/openmw/mwlua/luabindings.cpp @@ -84,6 +84,29 @@ namespace MWLua // api["resume"] = []() {}; } + static sol::table initContentFilesBindings(sol::state_view& lua) + { + const std::vector& contentList = MWBase::Environment::get().getWorld()->getContentFiles(); + sol::table list(lua, sol::create); + for (size_t i = 0; i < contentList.size(); ++i) + list[i + 1] = Misc::StringUtils::lowerCase(contentList[i]); + sol::table res(lua, sol::create); + res["list"] = LuaUtil::makeReadOnly(list); + res["indexOf"] = [&contentList](std::string_view contentFile) -> sol::optional { + for (size_t i = 0; i < contentList.size(); ++i) + if (Misc::StringUtils::ciEqual(contentList[i], contentFile)) + return i + 1; + return sol::nullopt; + }; + res["has"] = [&contentList](std::string_view contentFile) -> bool { + for (size_t i = 0; i < contentList.size(); ++i) + if (Misc::StringUtils::ciEqual(contentList[i], contentFile)) + return true; + return false; + }; + return LuaUtil::makeReadOnly(res); + } + static sol::table initCorePackage(const Context& context) { auto* lua = context.mLua; @@ -97,21 +120,7 @@ namespace MWLua context.mLuaEvents->addGlobalEvent( { std::move(eventName), LuaUtil::serialize(eventData, context.mSerializer) }); }; - api["getContentList"] = [](sol::this_state lua) -> sol::table { - const std::vector& contentList = MWBase::Environment::get().getWorld()->getContentFiles(); - sol::table res(lua, sol::create); - int i = 1; - for (const std::string& s : contentList) - res[i++] = Misc::StringUtils::lowerCase(s); - return res; - }; - api["getContentFileIndex"] = [](std::string_view contentFile) -> sol::optional { - const std::vector& contentList = MWBase::Environment::get().getWorld()->getContentFiles(); - for (size_t i = 0; i < contentList.size(); ++i) - if (Misc::StringUtils::ciEqual(contentList[i], contentFile)) - return i + 1; - return sol::nullopt; - }; + api["contentFiles"] = initContentFilesBindings(lua->sol()); api["getFormId"] = [](std::string_view contentFile, unsigned int index) -> std::string { const std::vector& contentList = MWBase::Environment::get().getWorld()->getContentFiles(); for (size_t i = 0; i < contentList.size(); ++i) diff --git a/apps/openmw/mwlua/objectbindings.cpp b/apps/openmw/mwlua/objectbindings.cpp index 01ef08ffdb..d72d778688 100644 --- a/apps/openmw/mwlua/objectbindings.cpp +++ b/apps/openmw/mwlua/objectbindings.cpp @@ -145,6 +145,13 @@ namespace MWLua void addBasicBindings(sol::usertype& objectT, const Context& context) { objectT["id"] = sol::readonly_property([](const ObjectT& o) -> std::string { return o.id().toString(); }); + objectT["contentFile"] = sol::readonly_property([](const ObjectT& o) -> sol::optional { + int contentFileIndex = o.id().mContentFile; + const std::vector& contentList = MWBase::Environment::get().getWorld()->getContentFiles(); + if (contentFileIndex < 0 || contentFileIndex >= static_cast(contentList.size())) + return sol::nullopt; + return Misc::StringUtils::lowerCase(contentList[contentFileIndex]); + }); objectT["isValid"] = [](const ObjectT& o) { return !o.ptrOrNull().isEmpty(); }; objectT["recordId"] = sol::readonly_property( [](const ObjectT& o) -> std::string { return o.ptr().getCellRef().getRefId().serializeText(); }); diff --git a/files/lua_api/openmw/core.lua b/files/lua_api/openmw/core.lua index 9661d7bb14..78fd395d91 100644 --- a/files/lua_api/openmw/core.lua +++ b/files/lua_api/openmw/core.lua @@ -103,16 +103,26 @@ --- --- Return the current load order (list of content file names). --- @function [parent=#core] getContentList --- @return #list<#string> +-- @{#ContentFiles}: functions working with the list of currently loaded content files. +-- @field [parent=#core] #ContentFiles contentFiles + +--- +-- Functions working with the list of currently loaded content files. +-- @type ContentFiles +-- @field #list<#string> list The current load order (list of content file names). --- -- Return the index of a specific content file in the load order (or `nil` if there is no such content file). --- @function [parent=#core] getContentFileIndex +-- @function [parent=#ContentFiles] indexOf -- @param #string contentFile -- @return #number +--- +-- Check if the content file with given name present in the load order. +-- @function [parent=#ContentFiles] has +-- @param #string contentFile +-- @return #boolean + --- -- Construct FormId string from content file name and the index in the file. -- @function [parent=#core] getFormId @@ -132,6 +142,7 @@ -- @type GameObject -- @extends #userdata -- @field #string id A unique id of this object (not record id), can be used as a key in a table. +-- @field #string contentFile Lower cased file name of the content file that defines this object; nil for dynamically created objects. -- @field #boolean enabled Whether the object is enabled or disabled. Global scripts can set the value. Items in containers or inventories can't be disabled. -- @field openmw.util#Vector3 position Object position. -- @field openmw.util#Vector3 rotation Object rotation (ZXY order).