mirror of
https://github.com/OpenMW/openmw.git
synced 2025-01-21 07:53:53 +00:00
Move content files functions to core.contentFiles
and add obj.contentFile
This commit is contained in:
parent
a778dff61d
commit
3b5849add8
3 changed files with 46 additions and 19 deletions
|
@ -84,6 +84,29 @@ namespace MWLua
|
||||||
// api["resume"] = []() {};
|
// 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)
|
static sol::table initCorePackage(const Context& context)
|
||||||
{
|
{
|
||||||
auto* lua = context.mLua;
|
auto* lua = context.mLua;
|
||||||
|
@ -97,21 +120,7 @@ namespace MWLua
|
||||||
context.mLuaEvents->addGlobalEvent(
|
context.mLuaEvents->addGlobalEvent(
|
||||||
{ std::move(eventName), LuaUtil::serialize(eventData, context.mSerializer) });
|
{ std::move(eventName), LuaUtil::serialize(eventData, context.mSerializer) });
|
||||||
};
|
};
|
||||||
api["getContentList"] = [](sol::this_state lua) -> sol::table {
|
api["contentFiles"] = initContentFilesBindings(lua->sol());
|
||||||
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["getFormId"] = [](std::string_view contentFile, unsigned int index) -> std::string {
|
api["getFormId"] = [](std::string_view contentFile, unsigned int index) -> std::string {
|
||||||
const std::vector<std::string>& contentList = MWBase::Environment::get().getWorld()->getContentFiles();
|
const std::vector<std::string>& contentList = MWBase::Environment::get().getWorld()->getContentFiles();
|
||||||
for (size_t i = 0; i < contentList.size(); ++i)
|
for (size_t i = 0; i < contentList.size(); ++i)
|
||||||
|
|
|
@ -145,6 +145,13 @@ namespace MWLua
|
||||||
void addBasicBindings(sol::usertype<ObjectT>& objectT, const Context& context)
|
void addBasicBindings(sol::usertype<ObjectT>& objectT, const Context& context)
|
||||||
{
|
{
|
||||||
objectT["id"] = sol::readonly_property([](const ObjectT& o) -> std::string { return o.id().toString(); });
|
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["isValid"] = [](const ObjectT& o) { return !o.ptrOrNull().isEmpty(); };
|
||||||
objectT["recordId"] = sol::readonly_property(
|
objectT["recordId"] = sol::readonly_property(
|
||||||
[](const ObjectT& o) -> std::string { return o.ptr().getCellRef().getRefId().serializeText(); });
|
[](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).
|
-- @{#ContentFiles}: functions working with the list of currently loaded content files.
|
||||||
-- @function [parent=#core] getContentList
|
-- @field [parent=#core] #ContentFiles contentFiles
|
||||||
-- @return #list<#string>
|
|
||||||
|
---
|
||||||
|
-- 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).
|
-- 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
|
-- @param #string contentFile
|
||||||
-- @return #number
|
-- @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.
|
-- Construct FormId string from content file name and the index in the file.
|
||||||
-- @function [parent=#core] getFormId
|
-- @function [parent=#core] getFormId
|
||||||
|
@ -132,6 +142,7 @@
|
||||||
-- @type GameObject
|
-- @type GameObject
|
||||||
-- @extends #userdata
|
-- @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 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 #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 position Object position.
|
||||||
-- @field openmw.util#Vector3 rotation Object rotation (ZXY order).
|
-- @field openmw.util#Vector3 rotation Object rotation (ZXY order).
|
||||||
|
|
Loading…
Reference in a new issue