From 827a2f0b776130bd447e2dacedb2aef22d1a850c Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 21 Aug 2022 18:53:38 +0200 Subject: [PATCH] Use string_view in animation code --- apps/openmw/mwrender/actoranimation.cpp | 29 +++++++++++----------- apps/openmw/mwrender/actoranimation.hpp | 10 ++++---- apps/openmw/mwrender/animation.cpp | 11 ++++---- apps/openmw/mwrender/animation.hpp | 2 +- apps/openmw/mwrender/creatureanimation.cpp | 2 +- apps/openmw/mwrender/npcanimation.cpp | 18 +++++++------- apps/openmw/mwrender/npcanimation.hpp | 4 +-- apps/openmw/mwrender/sky.cpp | 2 +- apps/openmw/mwrender/weaponanimation.cpp | 2 +- apps/openmw/mwworld/projectilemanager.cpp | 3 ++- components/sceneutil/attach.cpp | 20 +++++++-------- components/sceneutil/attach.hpp | 4 +-- components/sceneutil/visitor.cpp | 6 ++--- components/sceneutil/visitor.hpp | 13 +++++----- 14 files changed, 63 insertions(+), 63 deletions(-) diff --git a/apps/openmw/mwrender/actoranimation.cpp b/apps/openmw/mwrender/actoranimation.cpp index d3b9defe3d..b73a9d8a76 100644 --- a/apps/openmw/mwrender/actoranimation.cpp +++ b/apps/openmw/mwrender/actoranimation.cpp @@ -60,7 +60,7 @@ ActorAnimation::ActorAnimation(const MWWorld::Ptr& ptr, osg::ref_ptr ActorAnimation::~ActorAnimation() = default; -PartHolderPtr ActorAnimation::attachMesh(const std::string& model, const std::string& bonename, bool enchantedGlow, osg::Vec4f* glowColor) +PartHolderPtr ActorAnimation::attachMesh(const std::string& model, std::string_view bonename, bool enchantedGlow, osg::Vec4f* glowColor) { osg::Group* parent = getBoneByName(bonename); if (!parent) @@ -71,22 +71,22 @@ PartHolderPtr ActorAnimation::attachMesh(const std::string& model, const std::st const NodeMap& nodeMap = getNodeMap(); NodeMap::const_iterator found = nodeMap.find(bonename); if (found == nodeMap.end()) - return PartHolderPtr(); + return {}; if (enchantedGlow) mGlowUpdater = SceneUtil::addEnchantedGlow(instance, mResourceSystem, *glowColor); - return PartHolderPtr(new PartHolder(instance)); + return std::make_unique(instance); } -osg::ref_ptr ActorAnimation::attach(const std::string& model, const std::string& bonename, const std::string& bonefilter, bool isLight) +osg::ref_ptr ActorAnimation::attach(const std::string& model, std::string_view bonename, std::string_view bonefilter, bool isLight) { osg::ref_ptr templateNode = mResourceSystem->getSceneManager()->getTemplate(model); const NodeMap& nodeMap = getNodeMap(); auto found = nodeMap.find(bonename); if (found == nodeMap.end()) - throw std::runtime_error("Can't find attachment node " + bonename); + throw std::runtime_error("Can't find attachment node " + std::string{bonename}); if(isLight) { osg::Quat rotation(osg::DegreesToRadians(-90.f), osg::Vec3f(1,0,0)); @@ -109,7 +109,7 @@ std::string ActorAnimation::getShieldMesh(const MWWorld::ConstPtr& shield, bool if (part.mPart != ESM::PRT_Shield) continue; - std::string bodypartName; + std::string_view bodypartName; if (female && !part.mFemale.empty()) bodypartName = part.mFemale; else if (!part.mMale.empty()) @@ -212,7 +212,7 @@ void ActorAnimation::updateHolsteredShield(bool showCarriedLeft) if (mesh.empty()) return; - std::string boneName = "Bip01 AttachShield"; + std::string_view boneName = "Bip01 AttachShield"; osg::Vec4f glowColor = shield->getClass().getEnchantmentColor(*shield); std::string holsteredName = mesh; holsteredName = holsteredName.replace(holsteredName.size()-4, 4, "_sh.nif"); @@ -275,22 +275,21 @@ bool ActorAnimation::useShieldAnimations() const return false; } -osg::Group* ActorAnimation::getBoneByName(const std::string& boneName) const +osg::Group* ActorAnimation::getBoneByName(std::string_view boneName) const { if (!mObjectRoot) return nullptr; - SceneUtil::FindByNameVisitor findVisitor (boneName); + SceneUtil::FindByNameVisitor findVisitor(boneName); mObjectRoot->accept(findVisitor); return findVisitor.mFoundNode; } -std::string ActorAnimation::getHolsteredWeaponBoneName(const MWWorld::ConstPtr& weapon) +std::string_view ActorAnimation::getHolsteredWeaponBoneName(const MWWorld::ConstPtr& weapon) { - std::string boneName; if(weapon.isEmpty()) - return boneName; + return {}; auto type = weapon.getClass().getType(); if(type == ESM::Weapon::sRecordId) @@ -300,7 +299,7 @@ std::string ActorAnimation::getHolsteredWeaponBoneName(const MWWorld::ConstPtr& return MWMechanics::getWeaponType(weaponType)->mSheathingBone; } - return boneName; + return {}; } void ActorAnimation::resetControllers(osg::Node* node) @@ -336,7 +335,7 @@ void ActorAnimation::updateHolsteredWeapon(bool showHolsteredWeapons) std::string mesh = weapon->getClass().getModel(*weapon); std::string scabbardName = mesh; - std::string boneName = getHolsteredWeaponBoneName(*weapon); + std::string_view boneName = getHolsteredWeaponBoneName(*weapon); if (mesh.empty() || boneName.empty()) return; @@ -405,7 +404,7 @@ void ActorAnimation::updateQuiver() return; std::string mesh = weapon->getClass().getModel(*weapon); - std::string boneName = getHolsteredWeaponBoneName(*weapon); + std::string_view boneName = getHolsteredWeaponBoneName(*weapon); if (mesh.empty() || boneName.empty()) return; diff --git a/apps/openmw/mwrender/actoranimation.hpp b/apps/openmw/mwrender/actoranimation.hpp index 41978eb785..788458ac09 100644 --- a/apps/openmw/mwrender/actoranimation.hpp +++ b/apps/openmw/mwrender/actoranimation.hpp @@ -43,20 +43,20 @@ class ActorAnimation : public Animation, public MWWorld::ContainerStoreListener void removeFromScene() override; protected: - osg::Group* getBoneByName(const std::string& boneName) const; + osg::Group* getBoneByName(std::string_view boneName) const; virtual void updateHolsteredWeapon(bool showHolsteredWeapons); virtual void updateHolsteredShield(bool showCarriedLeft); virtual void updateQuiver(); std::string getShieldMesh(const MWWorld::ConstPtr& shield, bool female) const; virtual std::string getSheathedShieldMesh(const MWWorld::ConstPtr& shield) const; - virtual std::string getHolsteredWeaponBoneName(const MWWorld::ConstPtr& weapon); - virtual PartHolderPtr attachMesh(const std::string& model, const std::string& bonename, bool enchantedGlow, osg::Vec4f* glowColor); - virtual PartHolderPtr attachMesh(const std::string& model, const std::string& bonename) + virtual std::string_view getHolsteredWeaponBoneName(const MWWorld::ConstPtr& weapon); + virtual PartHolderPtr attachMesh(const std::string& model, std::string_view bonename, bool enchantedGlow, osg::Vec4f* glowColor); + virtual PartHolderPtr attachMesh(const std::string& model, std::string_view bonename) { osg::Vec4f stubColor = osg::Vec4f(0,0,0,0); return attachMesh(model, bonename, false, &stubColor); }; - osg::ref_ptr attach(const std::string& model, const std::string& bonename, const std::string& bonefilter, bool isLight); + osg::ref_ptr attach(const std::string& model, std::string_view bonename, std::string_view bonefilter, bool isLight); PartHolderPtr mScabbard; PartHolderPtr mHolsteredShield; diff --git a/apps/openmw/mwrender/animation.cpp b/apps/openmw/mwrender/animation.cpp index 07562a7593..7fc84693ff 100644 --- a/apps/openmw/mwrender/animation.cpp +++ b/apps/openmw/mwrender/animation.cpp @@ -612,10 +612,9 @@ namespace MWRender } } - void Animation::addAnimSource(const std::string &model, const std::string& baseModel) + void Animation::addAnimSource(std::string_view model, const std::string& baseModel) { - std::string kfname = model; - Misc::StringUtils::lowerCaseInPlace(kfname); + std::string kfname = Misc::StringUtils::lowerCase(model); if(kfname.size() > 4 && kfname.compare(kfname.size()-4, 4, ".nif") == 0) kfname.replace(kfname.size()-4, 4, ".kf"); @@ -670,20 +669,20 @@ namespace MWRender if (!mAccumRoot) { // Priority matters! bip01 is preferred. - static const std::array accumRootNames = + static const std::initializer_list accumRootNames = { "bip01", "root bone" }; NodeMap::const_iterator found = nodeMap.end(); - for (const std::string& name : accumRootNames) + for (const std::string_view& name : accumRootNames) { found = nodeMap.find(name); if (found == nodeMap.end()) continue; for (SceneUtil::KeyframeHolder::KeyframeControllerMap::const_iterator it = controllerMap.begin(); it != controllerMap.end(); ++it) { - if (Misc::StringUtils::lowerCase(it->first) == name) + if (Misc::StringUtils::ciEqual(it->first, name)) { mAccumRoot = found->second; break; diff --git a/apps/openmw/mwrender/animation.hpp b/apps/openmw/mwrender/animation.hpp index b3604dcaf1..66831d3ca6 100644 --- a/apps/openmw/mwrender/animation.hpp +++ b/apps/openmw/mwrender/animation.hpp @@ -330,7 +330,7 @@ protected: * @param model The file to add the keyframes for. Note that the .nif file extension will be replaced with .kf. * @param baseModel The filename of the mObjectRoot, only used for error messages. */ - void addAnimSource(const std::string &model, const std::string& baseModel); + void addAnimSource(std::string_view model, const std::string& baseModel); void addSingleAnimSource(const std::string &model, const std::string& baseModel); /** Adds an additional light to the given node using the specified ESM record. */ diff --git a/apps/openmw/mwrender/creatureanimation.cpp b/apps/openmw/mwrender/creatureanimation.cpp index 9f647f877b..e3c777674e 100644 --- a/apps/openmw/mwrender/creatureanimation.cpp +++ b/apps/openmw/mwrender/creatureanimation.cpp @@ -114,7 +114,7 @@ void CreatureWeaponAnimation::updatePart(PartHolderPtr& scene, int slot) } MWWorld::ConstPtr item = *it; - std::string bonename; + std::string_view bonename; std::string itemModel = item.getClass().getModel(item); if (slot == MWWorld::InventoryStore::Slot_CarriedRight) { diff --git a/apps/openmw/mwrender/npcanimation.cpp b/apps/openmw/mwrender/npcanimation.cpp index a7c48864dd..5f24bbaa0b 100644 --- a/apps/openmw/mwrender/npcanimation.cpp +++ b/apps/openmw/mwrender/npcanimation.cpp @@ -472,8 +472,8 @@ void NpcAnimation::updateNpcBase() mHeadModel.clear(); mHairModel.clear(); - std::string headName = isWerewolf ? "WerewolfHead" : mNpc->mHead; - std::string hairName = isWerewolf ? "WerewolfHair" : mNpc->mHair; + std::string_view headName = isWerewolf ? std::string_view{"WerewolfHead"} : mNpc->mHead; + std::string_view hairName = isWerewolf ? std::string_view{"WerewolfHair"} : mNpc->mHair; if (!headName.empty()) { @@ -514,7 +514,7 @@ void NpcAnimation::updateNpcBase() if(!is1stPerson) { - const std::string base = Settings::Manager::getString("xbaseanim", "Models"); + const std::string& base = Settings::Manager::getString("xbaseanim", "Models"); if (smodel != base && !isWerewolf) addAnimSource(base, smodel); @@ -528,7 +528,7 @@ void NpcAnimation::updateNpcBase() } else { - const std::string base = Settings::Manager::getString("xbaseanim1st", "Models"); + const std::string& base = Settings::Manager::getString("xbaseanim1st", "Models"); if (smodel != base && !isWerewolf) addAnimSource(base, smodel); @@ -701,13 +701,13 @@ void NpcAnimation::updateParts() -PartHolderPtr NpcAnimation::insertBoundedPart(const std::string& model, const std::string& bonename, const std::string& bonefilter, bool enchantedGlow, osg::Vec4f* glowColor, bool isLight) +PartHolderPtr NpcAnimation::insertBoundedPart(const std::string& model, std::string_view bonename, std::string_view bonefilter, bool enchantedGlow, osg::Vec4f* glowColor, bool isLight) { osg::ref_ptr attached = attach(model, bonename, bonefilter, isLight); if (enchantedGlow) mGlowUpdater = SceneUtil::addEnchantedGlow(attached, mResourceSystem, *glowColor); - return PartHolderPtr(new PartHolder(attached)); + return std::make_unique(attached); } osg::Vec3f NpcAnimation::runAnimation(float timepassed) @@ -786,7 +786,7 @@ bool NpcAnimation::addOrReplaceIndividualPart(ESM::PartReferenceType type, int g mPartPriorities[type] = priority; try { - std::string bonename = sPartList.at(type); + std::string_view bonename = sPartList.at(type); if (type == ESM::PRT_Weapon) { const MWWorld::InventoryStore& inv = mPtr.getClass().getInventoryStore(mPtr); @@ -794,7 +794,7 @@ bool NpcAnimation::addOrReplaceIndividualPart(ESM::PartReferenceType type, int g if(weapon != inv.end() && weapon->getType() == ESM::Weapon::sRecordId) { int weaponType = weapon->get()->mBase->mData.mType; - const std::string weaponBonename = MWMechanics::getWeaponType(weaponType)->mAttachBone; + const std::string& weaponBonename = MWMechanics::getWeaponType(weaponType)->mAttachBone; if (weaponBonename != bonename) { @@ -807,7 +807,7 @@ bool NpcAnimation::addOrReplaceIndividualPart(ESM::PartReferenceType type, int g } // PRT_Hair seems to be the only type that breaks consistency and uses a filter that's different from the attachment bone - const std::string bonefilter = (type == ESM::PRT_Hair) ? "hair" : bonename; + const std::string_view bonefilter = (type == ESM::PRT_Hair) ? std::string_view{"hair"} : bonename; mObjectParts[type] = insertBoundedPart(mesh, bonename, bonefilter, enchantedGlow, glowColor, isLight); } catch (std::exception& e) diff --git a/apps/openmw/mwrender/npcanimation.hpp b/apps/openmw/mwrender/npcanimation.hpp index 15454ea794..82b21d556b 100644 --- a/apps/openmw/mwrender/npcanimation.hpp +++ b/apps/openmw/mwrender/npcanimation.hpp @@ -82,8 +82,8 @@ private: NpcType getNpcType() const; - PartHolderPtr insertBoundedPart(const std::string &model, const std::string &bonename, - const std::string &bonefilter, bool enchantedGlow, osg::Vec4f* glowColor, bool isLight); + PartHolderPtr insertBoundedPart(const std::string &model, std::string_view bonename, + std::string_view bonefilter, bool enchantedGlow, osg::Vec4f* glowColor, bool isLight); void removeIndividualPart(ESM::PartReferenceType type); void reserveIndividualPart(ESM::PartReferenceType type, int group, int priority); diff --git a/apps/openmw/mwrender/sky.cpp b/apps/openmw/mwrender/sky.cpp index 7f45603977..592f3f7e86 100644 --- a/apps/openmw/mwrender/sky.cpp +++ b/apps/openmw/mwrender/sky.cpp @@ -689,7 +689,7 @@ namespace MWRender SetupVisitor alphaFaderSetupVisitor(mPrecipitationAlpha); mParticleEffect->accept(alphaFaderSetupVisitor); - SceneUtil::FindByClassVisitor findPSVisitor(std::string("ParticleSystem")); + SceneUtil::FindByClassVisitor findPSVisitor("ParticleSystem"); mParticleEffect->accept(findPSVisitor); for (unsigned int i = 0; i < findPSVisitor.mFoundNodes.size(); ++i) diff --git a/apps/openmw/mwrender/weaponanimation.cpp b/apps/openmw/mwrender/weaponanimation.cpp index 31ed5c0e11..c331235466 100644 --- a/apps/openmw/mwrender/weaponanimation.cpp +++ b/apps/openmw/mwrender/weaponanimation.cpp @@ -94,7 +94,7 @@ void WeaponAnimation::attachArrow(const MWWorld::Ptr& actor) osg::ref_ptr arrow = getResourceSystem()->getSceneManager()->getInstance(model, parent); - mAmmunition = PartHolderPtr(new PartHolder(arrow)); + mAmmunition = std::make_unique(arrow); } } diff --git a/apps/openmw/mwworld/projectilemanager.cpp b/apps/openmw/mwworld/projectilemanager.cpp index e610dda19f..a5c94e28c9 100644 --- a/apps/openmw/mwworld/projectilemanager.cpp +++ b/apps/openmw/mwworld/projectilemanager.cpp @@ -221,7 +221,8 @@ namespace MWWorld std::ostringstream nodeName; nodeName << "Dummy" << std::setw(2) << std::setfill('0') << iter; const ESM::Weapon* weapon = MWBase::Environment::get().getWorld()->getStore().get().find (state.mIdMagic.at(iter)); - SceneUtil::FindByNameVisitor findVisitor(nodeName.str()); + std::string nameToFind = nodeName.str(); + SceneUtil::FindByNameVisitor findVisitor(nameToFind); attachTo->accept(findVisitor); if (findVisitor.mFoundNode) mResourceSystem->getSceneManager()->getInstance( diff --git a/components/sceneutil/attach.cpp b/components/sceneutil/attach.cpp index 3529ffe2f5..36d79ce8c6 100644 --- a/components/sceneutil/attach.cpp +++ b/components/sceneutil/attach.cpp @@ -23,12 +23,11 @@ namespace SceneUtil class CopyRigVisitor : public osg::NodeVisitor { public: - CopyRigVisitor(osg::ref_ptr parent, const std::string& filter) + CopyRigVisitor(osg::ref_ptr parent, std::string_view filter) : osg::NodeVisitor(TRAVERSE_ALL_CHILDREN) , mParent(parent) - , mFilter(Misc::StringUtils::lowerCase(filter)) + , mFilter(std::move(filter)) { - mFilter2 = "tri " + mFilter; } void apply(osg::MatrixTransform& node) override @@ -71,19 +70,20 @@ namespace SceneUtil private: - bool filterMatches(const std::string& name) const + bool filterMatches(std::string_view name) const { - std::string lowerName = Misc::StringUtils::lowerCase(name); - return (lowerName.size() >= mFilter.size() && lowerName.compare(0, mFilter.size(), mFilter) == 0) - || (lowerName.size() >= mFilter2.size() && lowerName.compare(0, mFilter2.size(), mFilter2) == 0); + if (Misc::StringUtils::ciCompareLen(name, mFilter, mFilter.size()) == 0) + return true; + if (Misc::StringUtils::ciCompareLen(name, "tri ", 4) == 0) + return Misc::StringUtils::ciCompareLen(name.substr(4), mFilter, mFilter.size()) == 0; + return false; } using NodeSet = std::set>; NodeSet mToCopy; osg::ref_ptr mParent; - std::string mFilter; - std::string mFilter2; + std::string_view mFilter; }; void mergeUserData(const osg::UserDataContainer* source, osg::Object* target) @@ -100,7 +100,7 @@ namespace SceneUtil } } - osg::ref_ptr attach(osg::ref_ptr toAttach, osg::Node *master, const std::string &filter, osg::Group* attachNode, Resource::SceneManager* sceneManager, const osg::Quat* attitude) + osg::ref_ptr attach(osg::ref_ptr toAttach, osg::Node* master, std::string_view filter, osg::Group* attachNode, Resource::SceneManager* sceneManager, const osg::Quat* attitude) { if (dynamic_cast(toAttach.get())) { diff --git a/components/sceneutil/attach.hpp b/components/sceneutil/attach.hpp index ed0299dece..ab55f9c2f0 100644 --- a/components/sceneutil/attach.hpp +++ b/components/sceneutil/attach.hpp @@ -1,7 +1,7 @@ #ifndef OPENMW_COMPONENTS_SCENEUTIL_ATTACH_H #define OPENMW_COMPONENTS_SCENEUTIL_ATTACH_H -#include +#include #include @@ -24,7 +24,7 @@ namespace SceneUtil /// Otherwise, just attach all of the toAttach scenegraph to the attachment node on the master scenegraph, with no filtering. /// @note The master scene graph is expected to include a skeleton. /// @return A newly created node that is directly attached to the master scene graph - osg::ref_ptr attach(osg::ref_ptr toAttach, osg::Node* master, const std::string& filter, osg::Group* attachNode, Resource::SceneManager *sceneManager, const osg::Quat* attitude = nullptr); + osg::ref_ptr attach(osg::ref_ptr toAttach, osg::Node* master, std::string_view filter, osg::Group* attachNode, Resource::SceneManager* sceneManager, const osg::Quat* attitude = nullptr); } diff --git a/components/sceneutil/visitor.cpp b/components/sceneutil/visitor.cpp index bd7829f7a1..47bb9dbf5d 100644 --- a/components/sceneutil/visitor.cpp +++ b/components/sceneutil/visitor.cpp @@ -26,7 +26,7 @@ namespace SceneUtil void FindByClassVisitor::apply(osg::Node &node) { - if (Misc::StringUtils::ciEqual(std::string_view(node.className()), mNameToFind)) + if (Misc::StringUtils::ciEqual(node.className(), mNameToFind)) mFoundNodes.push_back(&node); traverse(node); @@ -52,7 +52,7 @@ namespace SceneUtil { // Choose first found node in file - if (trans.libraryName() == std::string("osgAnimation")) + if (trans.libraryName() == std::string_view("osgAnimation")) { std::string nodeName = trans.getName(); // Convert underscores to whitespaces as a workaround for Collada (OpenMW's animation system uses whitespace-separated names) @@ -144,7 +144,7 @@ namespace SceneUtil void RemoveTriBipVisitor::applyImpl(osg::Node& node) { - const std::string toFind = "tri bip"; + const std::string_view toFind = "tri bip"; if (Misc::StringUtils::ciCompareLen(node.getName(), toFind, toFind.size()) == 0) { osg::Group* parent = static_cast(*(getNodePath().end()-2)); diff --git a/components/sceneutil/visitor.hpp b/components/sceneutil/visitor.hpp index 5fafcec759..5bd4bed3e5 100644 --- a/components/sceneutil/visitor.hpp +++ b/components/sceneutil/visitor.hpp @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -17,9 +18,9 @@ namespace SceneUtil class FindByNameVisitor : public osg::NodeVisitor { public: - FindByNameVisitor(const std::string& nameToFind) + FindByNameVisitor(std::string_view nameToFind) : osg::NodeVisitor(TRAVERSE_ALL_CHILDREN) - , mNameToFind(nameToFind) + , mNameToFind(std::move(nameToFind)) , mFoundNode(nullptr) { } @@ -30,22 +31,22 @@ namespace SceneUtil bool checkGroup(osg::Group& group); - std::string mNameToFind; + std::string_view mNameToFind; osg::Group* mFoundNode; }; class FindByClassVisitor : public osg::NodeVisitor { public: - FindByClassVisitor(const std::string& nameToFind) + FindByClassVisitor(std::string_view nameToFind) : osg::NodeVisitor(TRAVERSE_ALL_CHILDREN) - , mNameToFind(nameToFind) + , mNameToFind(std::move(nameToFind)) { } void apply(osg::Node &node) override; - std::string mNameToFind; + std::string_view mNameToFind; std::vector mFoundNodes; };