From 20c15b30de3ebeed2684a0c427fd1b3d77ffc0ab Mon Sep 17 00:00:00 2001 From: elsid Date: Tue, 5 Jul 2022 00:53:15 +0200 Subject: [PATCH] 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. --- apps/openmw/mwlua/cellbindings.cpp | 2 +- apps/openmw/mwlua/luamanagerimp.cpp | 4 ++-- apps/openmw/mwlua/objectbindings.cpp | 2 +- apps/openmw/mwlua/types/types.cpp | 17 +++++++++++++++-- apps/openmw/mwlua/types/types.hpp | 7 +++++++ apps/openmw/mwworld/ptr.hpp | 19 ------------------- 6 files changed, 26 insertions(+), 25 deletions(-) diff --git a/apps/openmw/mwlua/cellbindings.cpp b/apps/openmw/mwlua/cellbindings.cpp index 183774ebf9..5048e68b7e 100644 --- a/apps/openmw/mwlua/cellbindings.cpp +++ b/apps/openmw/mwlua/cellbindings.cpp @@ -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; }; diff --git a/apps/openmw/mwlua/luamanagerimp.cpp b/apps/openmw/mwlua/luamanagerimp.cpp index 5854820361..f4ca54576e 100644 --- a/apps/openmw/mwlua/luamanagerimp.cpp +++ b/apps/openmw/mwlua/luamanagerimp.cpp @@ -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 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) diff --git a/apps/openmw/mwlua/objectbindings.cpp b/apps/openmw/mwlua/objectbindings.cpp index 1b0436ab7f..90ce134f24 100644 --- a/apps/openmw/mwlua/objectbindings.cpp +++ b/apps/openmw/mwlua/objectbindings.cpp @@ -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(); }); diff --git a/apps/openmw/mwlua/types/types.cpp b/apps/openmw/mwlua/types/types.cpp index 31f370180a..6235216670 100644 --- a/apps/openmw/mwlua/types/types.cpp +++ b/apps/openmw/mwlua/types/types.cpp @@ -1,6 +1,7 @@ #include "types.hpp" #include +#include 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(ptr.getLuaType()), /*fallback=*/ptr.getTypeDescription()); + return getLuaObjectTypeName(static_cast(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; diff --git a/apps/openmw/mwlua/types/types.hpp b/apps/openmw/mwlua/types/types.hpp index 0f9d82a65b..c4b9b63a45 100644 --- a/apps/openmw/mwlua/types/types.hpp +++ b/apps/openmw/mwlua/types/types.hpp @@ -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); diff --git a/apps/openmw/mwworld/ptr.hpp b/apps/openmw/mwworld/ptr.hpp index c96f936127..4dbdfa5545 100644 --- a/apps/openmw/mwworld/ptr.hpp +++ b/apps/openmw/mwworld/ptr.hpp @@ -7,8 +7,6 @@ #include #include -#include - #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";