From ff72a26ef204588cbe04a6c93c3c1fcf852ff3b4 Mon Sep 17 00:00:00 2001 From: Evil Eye Date: Sun, 30 Jun 2024 11:27:39 +0200 Subject: [PATCH] Don't use getRefIdString to get a fallback object name for non-string ids --- apps/openmw/mwclass/apparatus.cpp | 6 ++---- apps/openmw/mwclass/armor.cpp | 6 ++---- apps/openmw/mwclass/book.cpp | 6 ++---- apps/openmw/mwclass/clothing.cpp | 6 ++---- apps/openmw/mwclass/container.cpp | 6 ++---- apps/openmw/mwclass/creature.cpp | 6 ++---- apps/openmw/mwclass/door.cpp | 6 ++---- apps/openmw/mwclass/ingredient.cpp | 6 ++---- apps/openmw/mwclass/light.cpp | 5 ++--- apps/openmw/mwclass/lockpick.cpp | 6 ++---- apps/openmw/mwclass/misc.cpp | 6 ++---- apps/openmw/mwclass/nameorid.hpp | 25 +++++++++++++++++++++++++ apps/openmw/mwclass/npc.cpp | 7 +++---- apps/openmw/mwclass/potion.cpp | 6 ++---- apps/openmw/mwclass/probe.cpp | 6 ++---- apps/openmw/mwclass/repair.cpp | 6 ++---- apps/openmw/mwclass/weapon.cpp | 6 ++---- 17 files changed, 58 insertions(+), 63 deletions(-) create mode 100644 apps/openmw/mwclass/nameorid.hpp diff --git a/apps/openmw/mwclass/apparatus.cpp b/apps/openmw/mwclass/apparatus.cpp index bf5e4dc2f1..90112648b5 100644 --- a/apps/openmw/mwclass/apparatus.cpp +++ b/apps/openmw/mwclass/apparatus.cpp @@ -18,6 +18,7 @@ #include "../mwgui/tooltips.hpp" #include "classmodel.hpp" +#include "nameorid.hpp" namespace MWClass { @@ -42,10 +43,7 @@ namespace MWClass std::string_view Apparatus::getName(const MWWorld::ConstPtr& ptr) const { - const MWWorld::LiveCellRef* ref = ptr.get(); - const std::string& name = ref->mBase->mName; - - return !name.empty() ? name : ref->mBase->mId.getRefIdString(); + return getNameOrId(ptr); } std::unique_ptr Apparatus::activate(const MWWorld::Ptr& ptr, const MWWorld::Ptr& actor) const diff --git a/apps/openmw/mwclass/armor.cpp b/apps/openmw/mwclass/armor.cpp index 3853f53fc4..8bf9071f0c 100644 --- a/apps/openmw/mwclass/armor.cpp +++ b/apps/openmw/mwclass/armor.cpp @@ -27,6 +27,7 @@ #include "../mwgui/tooltips.hpp" #include "classmodel.hpp" +#include "nameorid.hpp" namespace MWClass { @@ -51,10 +52,7 @@ namespace MWClass std::string_view Armor::getName(const MWWorld::ConstPtr& ptr) const { - const MWWorld::LiveCellRef* ref = ptr.get(); - const std::string& name = ref->mBase->mName; - - return !name.empty() ? name : ref->mBase->mId.getRefIdString(); + return getNameOrId(ptr); } std::unique_ptr Armor::activate(const MWWorld::Ptr& ptr, const MWWorld::Ptr& actor) const diff --git a/apps/openmw/mwclass/book.cpp b/apps/openmw/mwclass/book.cpp index 95453e7a58..9cbaaa9071 100644 --- a/apps/openmw/mwclass/book.cpp +++ b/apps/openmw/mwclass/book.cpp @@ -24,6 +24,7 @@ #include "../mwmechanics/npcstats.hpp" #include "classmodel.hpp" +#include "nameorid.hpp" namespace MWClass { @@ -48,10 +49,7 @@ namespace MWClass std::string_view Book::getName(const MWWorld::ConstPtr& ptr) const { - const MWWorld::LiveCellRef* ref = ptr.get(); - const std::string& name = ref->mBase->mName; - - return !name.empty() ? name : ref->mBase->mId.getRefIdString(); + return getNameOrId(ptr); } std::unique_ptr Book::activate(const MWWorld::Ptr& ptr, const MWWorld::Ptr& actor) const diff --git a/apps/openmw/mwclass/clothing.cpp b/apps/openmw/mwclass/clothing.cpp index cdf51ef663..87d34c56d6 100644 --- a/apps/openmw/mwclass/clothing.cpp +++ b/apps/openmw/mwclass/clothing.cpp @@ -22,6 +22,7 @@ #include "../mwrender/renderinginterface.hpp" #include "classmodel.hpp" +#include "nameorid.hpp" namespace MWClass { @@ -46,10 +47,7 @@ namespace MWClass std::string_view Clothing::getName(const MWWorld::ConstPtr& ptr) const { - const MWWorld::LiveCellRef* ref = ptr.get(); - const std::string& name = ref->mBase->mName; - - return !name.empty() ? name : ref->mBase->mId.getRefIdString(); + return getNameOrId(ptr); } std::unique_ptr Clothing::activate(const MWWorld::Ptr& ptr, const MWWorld::Ptr& actor) const diff --git a/apps/openmw/mwclass/container.cpp b/apps/openmw/mwclass/container.cpp index 75b8543b0a..5cf9bf919a 100644 --- a/apps/openmw/mwclass/container.cpp +++ b/apps/openmw/mwclass/container.cpp @@ -34,6 +34,7 @@ #include "../mwmechanics/npcstats.hpp" #include "classmodel.hpp" +#include "nameorid.hpp" namespace MWClass { @@ -217,10 +218,7 @@ namespace MWClass std::string_view Container::getName(const MWWorld::ConstPtr& ptr) const { - const MWWorld::LiveCellRef* ref = ptr.get(); - const std::string& name = ref->mBase->mName; - - return !name.empty() ? name : ref->mBase->mId.getRefIdString(); + return getNameOrId(ptr); } MWWorld::ContainerStore& Container::getContainerStore(const MWWorld::Ptr& ptr) const diff --git a/apps/openmw/mwclass/creature.cpp b/apps/openmw/mwclass/creature.cpp index a5effe8e78..007338095f 100644 --- a/apps/openmw/mwclass/creature.cpp +++ b/apps/openmw/mwclass/creature.cpp @@ -48,6 +48,7 @@ #include "../mwgui/tooltips.hpp" #include "classmodel.hpp" +#include "nameorid.hpp" namespace { @@ -209,10 +210,7 @@ namespace MWClass std::string_view Creature::getName(const MWWorld::ConstPtr& ptr) const { - const MWWorld::LiveCellRef* ref = ptr.get(); - const std::string& name = ref->mBase->mName; - - return !name.empty() ? name : ref->mBase->mId.getRefIdString(); + return getNameOrId(ptr); } MWMechanics::CreatureStats& Creature::getCreatureStats(const MWWorld::Ptr& ptr) const diff --git a/apps/openmw/mwclass/door.cpp b/apps/openmw/mwclass/door.cpp index c5cdc60cec..e31da8415d 100644 --- a/apps/openmw/mwclass/door.cpp +++ b/apps/openmw/mwclass/door.cpp @@ -35,6 +35,7 @@ #include "../mwmechanics/actorutil.hpp" #include "classmodel.hpp" +#include "nameorid.hpp" namespace MWClass { @@ -101,10 +102,7 @@ namespace MWClass std::string_view Door::getName(const MWWorld::ConstPtr& ptr) const { - const MWWorld::LiveCellRef* ref = ptr.get(); - const std::string& name = ref->mBase->mName; - - return !name.empty() ? name : ref->mBase->mId.getRefIdString(); + return getNameOrId(ptr); } std::unique_ptr Door::activate(const MWWorld::Ptr& ptr, const MWWorld::Ptr& actor) const diff --git a/apps/openmw/mwclass/ingredient.cpp b/apps/openmw/mwclass/ingredient.cpp index e18d6ad5f3..94a1e8cc89 100644 --- a/apps/openmw/mwclass/ingredient.cpp +++ b/apps/openmw/mwclass/ingredient.cpp @@ -22,6 +22,7 @@ #include "../mwrender/renderinginterface.hpp" #include "classmodel.hpp" +#include "nameorid.hpp" namespace MWClass { @@ -46,10 +47,7 @@ namespace MWClass std::string_view Ingredient::getName(const MWWorld::ConstPtr& ptr) const { - const MWWorld::LiveCellRef* ref = ptr.get(); - const std::string& name = ref->mBase->mName; - - return !name.empty() ? name : ref->mBase->mId.getRefIdString(); + return getNameOrId(ptr); } std::unique_ptr Ingredient::activate(const MWWorld::Ptr& ptr, const MWWorld::Ptr& actor) const diff --git a/apps/openmw/mwclass/light.cpp b/apps/openmw/mwclass/light.cpp index 06b1901864..763ce86326 100644 --- a/apps/openmw/mwclass/light.cpp +++ b/apps/openmw/mwclass/light.cpp @@ -27,6 +27,7 @@ #include "../mwrender/renderinginterface.hpp" #include "classmodel.hpp" +#include "nameorid.hpp" namespace MWClass { @@ -81,9 +82,7 @@ namespace MWClass if (ref->mBase->mModel.empty()) return {}; - - const std::string& name = ref->mBase->mName; - return !name.empty() ? name : ref->mBase->mId.getRefIdString(); + return getNameOrId(ptr); } bool Light::isItem(const MWWorld::ConstPtr& ptr) const diff --git a/apps/openmw/mwclass/lockpick.cpp b/apps/openmw/mwclass/lockpick.cpp index dc3b73da63..7955d5af20 100644 --- a/apps/openmw/mwclass/lockpick.cpp +++ b/apps/openmw/mwclass/lockpick.cpp @@ -21,6 +21,7 @@ #include "../mwrender/renderinginterface.hpp" #include "classmodel.hpp" +#include "nameorid.hpp" namespace MWClass { @@ -45,10 +46,7 @@ namespace MWClass std::string_view Lockpick::getName(const MWWorld::ConstPtr& ptr) const { - const MWWorld::LiveCellRef* ref = ptr.get(); - const std::string& name = ref->mBase->mName; - - return !name.empty() ? name : ref->mBase->mId.getRefIdString(); + return getNameOrId(ptr); } std::unique_ptr Lockpick::activate(const MWWorld::Ptr& ptr, const MWWorld::Ptr& actor) const diff --git a/apps/openmw/mwclass/misc.cpp b/apps/openmw/mwclass/misc.cpp index dcd91c51af..cd97ecf216 100644 --- a/apps/openmw/mwclass/misc.cpp +++ b/apps/openmw/mwclass/misc.cpp @@ -25,6 +25,7 @@ #include "../mwrender/renderinginterface.hpp" #include "classmodel.hpp" +#include "nameorid.hpp" namespace MWClass { @@ -56,10 +57,7 @@ namespace MWClass std::string_view Miscellaneous::getName(const MWWorld::ConstPtr& ptr) const { - const MWWorld::LiveCellRef* ref = ptr.get(); - const std::string& name = ref->mBase->mName; - - return !name.empty() ? name : ref->mBase->mId.getRefIdString(); + return getNameOrId(ptr); } std::unique_ptr Miscellaneous::activate(const MWWorld::Ptr& ptr, const MWWorld::Ptr& actor) const diff --git a/apps/openmw/mwclass/nameorid.hpp b/apps/openmw/mwclass/nameorid.hpp new file mode 100644 index 0000000000..b001cc5fc8 --- /dev/null +++ b/apps/openmw/mwclass/nameorid.hpp @@ -0,0 +1,25 @@ +#ifndef OPENMW_MWCLASS_NAMEORID_H +#define OPENMW_MWCLASS_NAMEORID_H + +#include + +#include "../mwworld/livecellref.hpp" +#include "../mwworld/ptr.hpp" + +#include + +namespace MWClass +{ + template + std::string_view getNameOrId(const MWWorld::ConstPtr& ptr) + { + const MWWorld::LiveCellRef* ref = ptr.get(); + if (!ref->mBase->mName.empty()) + return ref->mBase->mName; + if (const auto* id = ref->mBase->mId.template getIf()) + return id->getValue(); + return {}; + } +} + +#endif diff --git a/apps/openmw/mwclass/npc.cpp b/apps/openmw/mwclass/npc.cpp index 9d3d2f344e..2da36fd170 100644 --- a/apps/openmw/mwclass/npc.cpp +++ b/apps/openmw/mwclass/npc.cpp @@ -61,6 +61,8 @@ #include "../mwgui/tooltips.hpp" +#include "nameorid.hpp" + namespace { struct NpcParts @@ -541,10 +543,7 @@ namespace MWClass return store.find("sWerewolfPopup")->mValue.getString(); } - const MWWorld::LiveCellRef* ref = ptr.get(); - const std::string& name = ref->mBase->mName; - - return !name.empty() ? name : ref->mBase->mId.getRefIdString(); + return getNameOrId(ptr); } MWMechanics::CreatureStats& Npc::getCreatureStats(const MWWorld::Ptr& ptr) const diff --git a/apps/openmw/mwclass/potion.cpp b/apps/openmw/mwclass/potion.cpp index 0d98302fe6..9dca460724 100644 --- a/apps/openmw/mwclass/potion.cpp +++ b/apps/openmw/mwclass/potion.cpp @@ -22,6 +22,7 @@ #include "../mwmechanics/spellutil.hpp" #include "classmodel.hpp" +#include "nameorid.hpp" namespace MWClass { @@ -46,10 +47,7 @@ namespace MWClass std::string_view Potion::getName(const MWWorld::ConstPtr& ptr) const { - const MWWorld::LiveCellRef* ref = ptr.get(); - const std::string& name = ref->mBase->mName; - - return !name.empty() ? name : ref->mBase->mId.getRefIdString(); + return getNameOrId(ptr); } std::unique_ptr Potion::activate(const MWWorld::Ptr& ptr, const MWWorld::Ptr& actor) const diff --git a/apps/openmw/mwclass/probe.cpp b/apps/openmw/mwclass/probe.cpp index beab45945c..4039f72cc5 100644 --- a/apps/openmw/mwclass/probe.cpp +++ b/apps/openmw/mwclass/probe.cpp @@ -21,6 +21,7 @@ #include "../mwrender/renderinginterface.hpp" #include "classmodel.hpp" +#include "nameorid.hpp" namespace MWClass { @@ -45,10 +46,7 @@ namespace MWClass std::string_view Probe::getName(const MWWorld::ConstPtr& ptr) const { - const MWWorld::LiveCellRef* ref = ptr.get(); - const std::string& name = ref->mBase->mName; - - return !name.empty() ? name : ref->mBase->mId.getRefIdString(); + return getNameOrId(ptr); } std::unique_ptr Probe::activate(const MWWorld::Ptr& ptr, const MWWorld::Ptr& actor) const { diff --git a/apps/openmw/mwclass/repair.cpp b/apps/openmw/mwclass/repair.cpp index 279352263e..922b33b67e 100644 --- a/apps/openmw/mwclass/repair.cpp +++ b/apps/openmw/mwclass/repair.cpp @@ -19,6 +19,7 @@ #include "../mwrender/renderinginterface.hpp" #include "classmodel.hpp" +#include "nameorid.hpp" namespace MWClass { @@ -43,10 +44,7 @@ namespace MWClass std::string_view Repair::getName(const MWWorld::ConstPtr& ptr) const { - const MWWorld::LiveCellRef* ref = ptr.get(); - const std::string& name = ref->mBase->mName; - - return !name.empty() ? name : ref->mBase->mId.getRefIdString(); + return getNameOrId(ptr); } std::unique_ptr Repair::activate(const MWWorld::Ptr& ptr, const MWWorld::Ptr& actor) const diff --git a/apps/openmw/mwclass/weapon.cpp b/apps/openmw/mwclass/weapon.cpp index 5f1f7f2772..089c8c2894 100644 --- a/apps/openmw/mwclass/weapon.cpp +++ b/apps/openmw/mwclass/weapon.cpp @@ -26,6 +26,7 @@ #include "../mwrender/renderinginterface.hpp" #include "classmodel.hpp" +#include "nameorid.hpp" namespace MWClass { @@ -50,10 +51,7 @@ namespace MWClass std::string_view Weapon::getName(const MWWorld::ConstPtr& ptr) const { - const MWWorld::LiveCellRef* ref = ptr.get(); - const std::string& name = ref->mBase->mName; - - return !name.empty() ? name : ref->mBase->mId.getRefIdString(); + return getNameOrId(ptr); } std::unique_ptr Weapon::activate(const MWWorld::Ptr& ptr, const MWWorld::Ptr& actor) const