Move content files functions to `core.contentFiles` and add `obj.contentFile`

revert-6246b479
Petr Mikheev 2 years ago
parent a778dff61d
commit 3b5849add8

@ -84,6 +84,29 @@ namespace MWLua
// api["resume"] = []() {};
}
static sol::table initContentFilesBindings(sol::state_view& lua)
{
const std::vector<std::string>& 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<int> {
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<std::string>& 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<int> {
const std::vector<std::string>& 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<std::string>& contentList = MWBase::Environment::get().getWorld()->getContentFiles();
for (size_t i = 0; i < contentList.size(); ++i)

@ -145,6 +145,13 @@ namespace MWLua
void addBasicBindings(sol::usertype<ObjectT>& 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<std::string> {
int contentFileIndex = o.id().mContentFile;
const std::vector<std::string>& contentList = MWBase::Environment::get().getWorld()->getContentFiles();
if (contentFileIndex < 0 || contentFileIndex >= static_cast<int>(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(); });

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

Loading…
Cancel
Save