1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-03-26 11:37:12 +00:00

Move getLuaType out of MWWorld::PtrBase

This function is used only for Lua related code and don't need to be present
everywhere ptr.hpp is included.
This commit is contained in:
elsid 2022-07-05 00:53:15 +02:00
parent 4613840914
commit 20c15b30de
No known key found for this signature in database
GPG Key ID: 4DE04C198CBA7625
6 changed files with 26 additions and 25 deletions

View File

@ -62,7 +62,7 @@ namespace MWLua
auto visitor = [&](const MWWorld::Ptr& ptr)
{
worldView->getObjectRegistry()->registerPtr(ptr);
if (ptr.getLuaType() == ptr.getType())
if (getLiveCellRefType(ptr.mRef) == ptr.getType())
res->push_back(getId(ptr));
return true;
};

View File

@ -335,7 +335,7 @@ namespace MWLua
if (!localScripts)
{
LuaUtil::ScriptIdsWithInitializationData autoStartConf =
mConfiguration.getLocalConf(ptr.getLuaType(), ptr.getCellRef().getRefId(), getId(ptr));
mConfiguration.getLocalConf(getLiveCellRefType(ptr.mRef), ptr.getCellRef().getRefId(), getId(ptr));
if (!autoStartConf.empty())
{
localScripts = createLocalScripts(ptr, std::move(autoStartConf));
@ -411,7 +411,7 @@ namespace MWLua
{
assert(mInitialized);
std::shared_ptr<LocalScripts> scripts;
uint32_t type = ptr.getLuaType();
const uint32_t type = getLiveCellRefType(ptr.mRef);
if (type == ESM::REC_STAT)
throw std::runtime_error("Lua scripts on static objects are not allowed");
else if (type == ESM::REC_INTERNAL_PLAYER)

View File

@ -165,7 +165,7 @@ namespace MWLua
objectT["type"] = sol::readonly_property([types=getTypeToPackageTable(context.mLua->sol())](const ObjectT& o) mutable
{
return types[o.ptr().getLuaType()];
return types[getLiveCellRefType(o.ptr().mRef)];
});
objectT["count"] = sol::readonly_property([](const ObjectT& o) { return o.ptr().getRefData().getCount(); });

View File

@ -1,6 +1,7 @@
#include "types.hpp"
#include <components/lua/luastate.hpp>
#include <components/misc/resourcehelpers.hpp>
namespace MWLua
{
@ -60,6 +61,18 @@ namespace MWLua
}
unsigned int getLiveCellRefType(const MWWorld::LiveCellRefBase* ref)
{
if (ref == nullptr)
throw std::runtime_error("Can't get type name from an empty object.");
const std::string_view id = ref->mRef.getRefId();
if (id == "player")
return ESM::REC_INTERNAL_PLAYER;
if (Misc::ResourceHelpers::isHiddenMarker(id))
return ESM::REC_INTERNAL_MARKER;
return ref->getType();
}
std::string_view getLuaObjectTypeName(ESM::RecNameInts type, std::string_view fallback)
{
auto it = luaObjectTypeInfo.find(type);
@ -71,7 +84,7 @@ namespace MWLua
std::string_view getLuaObjectTypeName(const MWWorld::Ptr& ptr)
{
return getLuaObjectTypeName(static_cast<ESM::RecNameInts>(ptr.getLuaType()), /*fallback=*/ptr.getTypeDescription());
return getLuaObjectTypeName(static_cast<ESM::RecNameInts>(getLiveCellRefType(ptr.mRef)), /*fallback=*/ptr.getTypeDescription());
}
const MWWorld::Ptr& verifyType(ESM::RecNameInts recordType, const MWWorld::Ptr& ptr)
@ -125,7 +138,7 @@ namespace MWLua
}
t["objectIsInstance"] = [types=recTypes](const Object& o)
{
unsigned int type = o.ptr().getLuaType();
unsigned int type = getLiveCellRefType(o.ptr().mRef);
for (ESM::RecNameInts t : types)
if (t == type)
return true;

View File

@ -10,6 +10,13 @@
namespace MWLua
{
// `getLiveCellRefType()` is not exactly what we usually mean by "type" because some refids have special meaning.
// This function handles these special refids (and by this adds some performance overhead).
// We use this "fixed" type in Lua because we don't want to expose the weirdness of Morrowind internals to our API.
// TODO: Implement https://gitlab.com/OpenMW/openmw/-/issues/6617 and make `MWWorld::PtrBase::getType` work the
// same as `getLiveCellRefType`.
unsigned int getLiveCellRefType(const MWWorld::LiveCellRefBase* ref);
std::string_view getLuaObjectTypeName(ESM::RecNameInts type, std::string_view fallback = "Unknown");
std::string_view getLuaObjectTypeName(const MWWorld::Ptr& ptr);
const MWWorld::Ptr& verifyType(ESM::RecNameInts type, const MWWorld::Ptr& ptr);

View File

@ -7,8 +7,6 @@
#include <string_view>
#include <sstream>
#include <components/misc/resourcehelpers.hpp>
#include "livecellref.hpp"
namespace MWWorld
@ -49,23 +47,6 @@ namespace MWWorld
throw std::runtime_error("Can't get type name from an empty object.");
}
// `getType()` is not exactly what we usually mean by "type" because some refids have special meaning.
// This function handles these special refids (and by this adds some performance overhead).
// We use this "fixed" type in Lua because we don't want to expose the weirdness of Morrowind internals to our API.
// TODO: Implement https://gitlab.com/OpenMW/openmw/-/issues/6617 and make `getType` work the same as `getLuaType`.
unsigned int getLuaType() const
{
if(mRef == nullptr)
throw std::runtime_error("Can't get type name from an empty object.");
std::string_view id = mRef->mRef.getRefId();
if (id == "player")
return ESM::REC_INTERNAL_PLAYER;
else if (Misc::ResourceHelpers::isHiddenMarker(id))
return ESM::REC_INTERNAL_MARKER;
else
return mRef->getType();
}
std::string_view getTypeDescription() const
{
return mRef ? mRef->getTypeDescription() : "nullptr";