diff --git a/apps/openmw/mwbase/world.hpp b/apps/openmw/mwbase/world.hpp index bae14e4a8e..437a048f5a 100644 --- a/apps/openmw/mwbase/world.hpp +++ b/apps/openmw/mwbase/world.hpp @@ -190,12 +190,12 @@ namespace MWBase virtual char getGlobalVariableType(std::string_view name) const = 0; ///< Return ' ', if there is no global variable with this name. - virtual std::string getCellName (const MWWorld::CellStore *cell = nullptr) const = 0; + virtual std::string_view getCellName(const MWWorld::CellStore* cell = nullptr) const = 0; ///< Return name of the cell. /// /// \note If cell==0, the cell the player is currently in will be used instead to /// generate a name. - virtual std::string getCellName(const ESM::Cell* cell) const = 0; + virtual std::string_view getCellName(const ESM::Cell* cell) const = 0; virtual void removeRefScript (MWWorld::RefData *ref) = 0; //< Remove the script attached to ref from mLocalScripts diff --git a/apps/openmw/mwdialogue/filter.cpp b/apps/openmw/mwdialogue/filter.cpp index 516cfff767..20092f936c 100644 --- a/apps/openmw/mwdialogue/filter.cpp +++ b/apps/openmw/mwdialogue/filter.cpp @@ -138,7 +138,7 @@ bool MWDialogue::Filter::testPlayer (const ESM::DialInfo& info) const if (!info.mCell.empty()) { // supports partial matches, just like getPcCell - const std::string& playerCell = MWBase::Environment::get().getWorld()->getCellName(player.getCell()); + std::string_view playerCell = MWBase::Environment::get().getWorld()->getCellName(player.getCell()); bool match = playerCell.length()>=info.mCell.length() && Misc::StringUtils::ciEqual(playerCell.substr (0, info.mCell.length()), info.mCell); if (!match) @@ -485,7 +485,7 @@ bool MWDialogue::Filter::getSelectStructBoolean (const SelectWrapper& select) co case SelectWrapper::Function_NotCell: { - const std::string& actorCell = MWBase::Environment::get().getWorld()->getCellName(mActor.getCell()); + std::string_view actorCell = MWBase::Environment::get().getWorld()->getCellName(mActor.getCell()); return !(actorCell.length() >= select.getName().length() && Misc::StringUtils::ciEqual(actorCell.substr(0, select.getName().length()), select.getName())); } diff --git a/apps/openmw/mwgui/windowmanagerimp.cpp b/apps/openmw/mwgui/windowmanagerimp.cpp index e8b92eaae6..77bc0d7713 100644 --- a/apps/openmw/mwgui/windowmanagerimp.cpp +++ b/apps/openmw/mwgui/windowmanagerimp.cpp @@ -917,7 +917,7 @@ namespace MWGui { mMap->requestMapRender(cell); - std::string name = MWBase::Environment::get().getWorld()->getCellName (cell); + std::string name{MWBase::Environment::get().getWorld()->getCellName(cell)}; mMap->setCellName( name ); mHud->setCellName( name ); diff --git a/apps/openmw/mwscript/cellextensions.cpp b/apps/openmw/mwscript/cellextensions.cpp index 0419d37962..a8d918e6cb 100644 --- a/apps/openmw/mwscript/cellextensions.cpp +++ b/apps/openmw/mwscript/cellextensions.cpp @@ -170,11 +170,8 @@ namespace MWScript } const MWWorld::CellStore *cell = MWMechanics::getPlayer().getCell(); - std::string current = MWBase::Environment::get().getWorld()->getCellName(cell); - Misc::StringUtils::lowerCaseInPlace(current); - - bool match = current.length()>=name.length() && - current.substr (0, name.length())==name; + std::string_view current = MWBase::Environment::get().getWorld()->getCellName(cell); + bool match = Misc::StringUtils::ciCompareLen(name, current, name.length()) == 0; runtime.push (match ? 1 : 0); } diff --git a/apps/openmw/mwscript/globalscripts.cpp b/apps/openmw/mwscript/globalscripts.cpp index 0739c49f1b..26cef4f54f 100644 --- a/apps/openmw/mwscript/globalscripts.cpp +++ b/apps/openmw/mwscript/globalscripts.cpp @@ -95,14 +95,14 @@ namespace struct IdGettingVisitor { - std::string operator()(const MWWorld::Ptr& ptr) const + std::string_view operator()(const MWWorld::Ptr& ptr) const { if(ptr.isEmpty()) return {}; return ptr.mRef->mRef.getRefId(); } - std::string operator()(const std::pair& pair) const + std::string_view operator()(const std::pair& pair) const { return pair.second; } @@ -125,7 +125,7 @@ namespace MWScript return ptr; } - std::string GlobalScriptDesc::getId() const + std::string_view GlobalScriptDesc::getId() const { return std::visit(IdGettingVisitor {}, mTarget); } diff --git a/apps/openmw/mwscript/globalscripts.hpp b/apps/openmw/mwscript/globalscripts.hpp index 556f815238..92272c05bd 100644 --- a/apps/openmw/mwscript/globalscripts.hpp +++ b/apps/openmw/mwscript/globalscripts.hpp @@ -2,6 +2,7 @@ #define GAME_SCRIPT_GLOBALSCRIPTS_H #include +#include #include #include #include @@ -44,7 +45,7 @@ namespace MWScript MWWorld::Ptr getPtr(); // Resolves mTarget to a Ptr and caches the (potentially empty) result - std::string getId() const; // Returns the target's ID -- if any + std::string_view getId() const; // Returns the target's ID -- if any }; class GlobalScripts diff --git a/apps/openmw/mwscript/interpretercontext.cpp b/apps/openmw/mwscript/interpretercontext.cpp index 535697df89..6490d07d05 100644 --- a/apps/openmw/mwscript/interpretercontext.cpp +++ b/apps/openmw/mwscript/interpretercontext.cpp @@ -26,8 +26,7 @@ namespace MWScript { - const MWWorld::Ptr InterpreterContext::getReferenceImp ( - const std::string& id, bool activeOnly, bool doThrow) const + const MWWorld::Ptr InterpreterContext::getReferenceImp(std::string_view id, bool activeOnly, bool doThrow) const { if (!id.empty()) { @@ -45,7 +44,7 @@ namespace MWScript } } - const Locals& InterpreterContext::getMemberLocals (std::string& id, bool global) + const Locals& InterpreterContext::getMemberLocals(std::string_view& id, bool global) const { if (global) @@ -66,7 +65,7 @@ namespace MWScript } } - Locals& InterpreterContext::getMemberLocals (std::string& id, bool global) + Locals& InterpreterContext::getMemberLocals(std::string_view& id, bool global) { if (global) { @@ -88,8 +87,7 @@ namespace MWScript MissingImplicitRefError::MissingImplicitRefError() : std::runtime_error("no implicit reference") {} - int InterpreterContext::findLocalVariableIndex (const std::string& scriptId, - std::string_view name, char type) const + int InterpreterContext::findLocalVariableIndex(std::string_view scriptId, std::string_view name, char type) const { int index = MWBase::Environment::get().getScriptManager()->getLocals (scriptId). searchIndex (type, name); @@ -130,7 +128,7 @@ namespace MWScript mGlobalScriptDesc = globalScriptDesc; } - std::string InterpreterContext::getTarget() const + std::string_view InterpreterContext::getTarget() const { if(!mReference.isEmpty()) return mReference.mRef->mRef.getRefId(); @@ -237,7 +235,7 @@ namespace MWScript MWBase::Environment::get().getWorld()->getStore().get(); std::vector ids; - for (auto& globalVariable : globals) + for (const auto& globalVariable : globals) { ids.emplace_back(globalVariable.mId); } @@ -273,7 +271,7 @@ namespace MWScript return "None"; } - std::string InterpreterContext::getActorName() const + std::string_view InterpreterContext::getActorName() const { const MWWorld::Ptr& ptr = getReferenceImp(); if (ptr.getClass().isNpc()) @@ -286,28 +284,28 @@ namespace MWScript return creature->mName; } - std::string InterpreterContext::getNPCRace() const + std::string_view InterpreterContext::getNPCRace() const { - ESM::NPC npc = *getReferenceImp().get()->mBase; - const ESM::Race* race = MWBase::Environment::get().getWorld()->getStore().get().find(npc.mRace); + const ESM::NPC* npc = getReferenceImp().get()->mBase; + const ESM::Race* race = MWBase::Environment::get().getWorld()->getStore().get().find(npc->mRace); return race->mName; } - std::string InterpreterContext::getNPCClass() const + std::string_view InterpreterContext::getNPCClass() const { - ESM::NPC npc = *getReferenceImp().get()->mBase; - const ESM::Class* class_ = MWBase::Environment::get().getWorld()->getStore().get().find(npc.mClass); + const ESM::NPC* npc = getReferenceImp().get()->mBase; + const ESM::Class* class_ = MWBase::Environment::get().getWorld()->getStore().get().find(npc->mClass); return class_->mName; } - std::string InterpreterContext::getNPCFaction() const + std::string_view InterpreterContext::getNPCFaction() const { - ESM::NPC npc = *getReferenceImp().get()->mBase; - const ESM::Faction* faction = MWBase::Environment::get().getWorld()->getStore().get().find(npc.mFaction); + const ESM::NPC* npc = getReferenceImp().get()->mBase; + const ESM::Faction* faction = MWBase::Environment::get().getWorld()->getStore().get().find(npc->mFaction); return faction->mName; } - std::string InterpreterContext::getNPCRank() const + std::string_view InterpreterContext::getNPCRank() const { const MWWorld::Ptr& ptr = getReferenceImp(); std::string_view faction = ptr.getClass().getPrimaryFaction(ptr); @@ -324,28 +322,27 @@ namespace MWScript return fact->mRanks[rank]; } - std::string InterpreterContext::getPCName() const + std::string_view InterpreterContext::getPCName() const { MWBase::World *world = MWBase::Environment::get().getWorld(); - ESM::NPC player = *world->getPlayerPtr().get()->mBase; - return player.mName; + return world->getPlayerPtr().get()->mBase->mName; } - std::string InterpreterContext::getPCRace() const + std::string_view InterpreterContext::getPCRace() const { MWBase::World *world = MWBase::Environment::get().getWorld(); const std::string& race = world->getPlayerPtr().get()->mBase->mRace; return world->getStore().get().find(race)->mName; } - std::string InterpreterContext::getPCClass() const + std::string_view InterpreterContext::getPCClass() const { MWBase::World *world = MWBase::Environment::get().getWorld(); const std::string& class_ = world->getPlayerPtr().get()->mBase->mClass; return world->getStore().get().find(class_)->mName; } - std::string InterpreterContext::getPCRank() const + std::string_view InterpreterContext::getPCRank() const { MWBase::World *world = MWBase::Environment::get().getWorld(); MWWorld::Ptr player = world->getPlayerPtr(); @@ -374,7 +371,7 @@ namespace MWScript return faction->mRanks[rank]; } - std::string InterpreterContext::getPCNextRank() const + std::string_view InterpreterContext::getPCNextRank() const { MWBase::World *world = MWBase::Environment::get().getWorld(); MWWorld::Ptr player = world->getPlayerPtr(); @@ -411,9 +408,9 @@ namespace MWScript return player.getClass().getNpcStats (player).getBounty(); } - std::string InterpreterContext::getCurrentCellName() const + std::string_view InterpreterContext::getCurrentCellName() const { - return MWBase::Environment::get().getWorld()->getCellName(); + return MWBase::Environment::get().getWorld()->getCellName(); } void InterpreterContext::executeActivation(const MWWorld::Ptr& ptr, const MWWorld::Ptr& actor) @@ -430,59 +427,47 @@ namespace MWScript int InterpreterContext::getMemberShort(std::string_view id, std::string_view name, bool global) const { - std::string scriptId (id); + const Locals& locals = getMemberLocals(id, global); - const Locals& locals = getMemberLocals (scriptId, global); - - return locals.mShorts[findLocalVariableIndex (scriptId, name, 's')]; + return locals.mShorts[findLocalVariableIndex(id, name, 's')]; } int InterpreterContext::getMemberLong(std::string_view id, std::string_view name, bool global) const { - std::string scriptId (id); - - const Locals& locals = getMemberLocals (scriptId, global); + const Locals& locals = getMemberLocals(id, global); - return locals.mLongs[findLocalVariableIndex (scriptId, name, 'l')]; + return locals.mLongs[findLocalVariableIndex(id, name, 'l')]; } float InterpreterContext::getMemberFloat(std::string_view id, std::string_view name, bool global) const { - std::string scriptId (id); - - const Locals& locals = getMemberLocals (scriptId, global); + const Locals& locals = getMemberLocals(id, global); - return locals.mFloats[findLocalVariableIndex (scriptId, name, 'f')]; + return locals.mFloats[findLocalVariableIndex(id, name, 'f')]; } void InterpreterContext::setMemberShort(std::string_view id, std::string_view name, int value, bool global) { - std::string scriptId (id); + Locals& locals = getMemberLocals(id, global); - Locals& locals = getMemberLocals (scriptId, global); - - locals.mShorts[findLocalVariableIndex (scriptId, name, 's')] = value; + locals.mShorts[findLocalVariableIndex(id, name, 's')] = value; } void InterpreterContext::setMemberLong(std::string_view id, std::string_view name, int value, bool global) { - std::string scriptId (id); - - Locals& locals = getMemberLocals (scriptId, global); + Locals& locals = getMemberLocals(id, global); - locals.mLongs[findLocalVariableIndex (scriptId, name, 'l')] = value; + locals.mLongs[findLocalVariableIndex(id, name, 'l')] = value; } void InterpreterContext::setMemberFloat(std::string_view id, std::string_view name, float value, bool global) { - std::string scriptId (id); - - Locals& locals = getMemberLocals (scriptId, global); + Locals& locals = getMemberLocals(id, global); - locals.mFloats[findLocalVariableIndex (scriptId, name, 'f')] = value; + locals.mFloats[findLocalVariableIndex(id, name, 'f')] = value; } MWWorld::Ptr InterpreterContext::getReference(bool required) const diff --git a/apps/openmw/mwscript/interpretercontext.hpp b/apps/openmw/mwscript/interpretercontext.hpp index b675e850d2..c53dd88ab3 100644 --- a/apps/openmw/mwscript/interpretercontext.hpp +++ b/apps/openmw/mwscript/interpretercontext.hpp @@ -28,18 +28,16 @@ namespace MWScript /// If \a id is empty, a reference the script is run from is returned or in case /// of a non-local script the reference derived from the target ID. - const MWWorld::Ptr getReferenceImp (const std::string& id = "", - bool activeOnly = false, bool doThrow=true) const; + const MWWorld::Ptr getReferenceImp(std::string_view id = {}, bool activeOnly = false, bool doThrow = true) const; - const Locals& getMemberLocals (std::string& id, bool global) const; + const Locals& getMemberLocals(std::string_view& id, bool global) const; ///< \a id is changed to the respective script ID, if \a id wasn't a script ID before - Locals& getMemberLocals (std::string& id, bool global); + Locals& getMemberLocals(std::string_view& id, bool global); ///< \a id is changed to the respective script ID, if \a id wasn't a script ID before /// Throws an exception if local variable can't be found. - int findLocalVariableIndex (const std::string& scriptId, std::string_view name, - char type) const; + int findLocalVariableIndex(std::string_view scriptId, std::string_view name, char type) const; public: InterpreterContext (std::shared_ptr globalScriptDesc); @@ -47,7 +45,7 @@ namespace MWScript InterpreterContext (MWScript::Locals *locals, const MWWorld::Ptr& reference); ///< The ownership of \a locals is not transferred. 0-pointer allowed. - std::string getTarget() const override; + std::string_view getTarget() const override; int getLocalShort (int index) const override; @@ -87,29 +85,29 @@ namespace MWScript std::string getActionBinding(std::string_view action) const override; - std::string getActorName() const override; + std::string_view getActorName() const override; - std::string getNPCRace() const override; + std::string_view getNPCRace() const override; - std::string getNPCClass() const override; + std::string_view getNPCClass() const override; - std::string getNPCFaction() const override; + std::string_view getNPCFaction() const override; - std::string getNPCRank() const override; + std::string_view getNPCRank() const override; - std::string getPCName() const override; + std::string_view getPCName() const override; - std::string getPCRace() const override; + std::string_view getPCRace() const override; - std::string getPCClass() const override; + std::string_view getPCClass() const override; - std::string getPCRank() const override; + std::string_view getPCRank() const override; - std::string getPCNextRank() const override; + std::string_view getPCNextRank() const override; int getPCBounty() const override; - std::string getCurrentCellName() const override; + std::string_view getCurrentCellName() const override; void executeActivation(const MWWorld::Ptr& ptr, const MWWorld::Ptr& actor); ///< Execute the activation action for this ptr. If ptr is mActivated, mark activation as handled. diff --git a/apps/openmw/mwworld/worldimp.cpp b/apps/openmw/mwworld/worldimp.cpp index d6f8db9f24..14ac64f51f 100644 --- a/apps/openmw/mwworld/worldimp.cpp +++ b/apps/openmw/mwworld/worldimp.cpp @@ -670,14 +670,14 @@ namespace MWWorld return mCurrentDate->getMonthName(month); } - std::string World::getCellName (const MWWorld::CellStore *cell) const + std::string_view World::getCellName(const MWWorld::CellStore* cell) const { if (!cell) cell = mWorldScene->getCurrentCell(); return getCellName(cell->getCell()); } - std::string World::getCellName(const ESM::Cell* cell) const + std::string_view World::getCellName(const ESM::Cell* cell) const { if (cell) { diff --git a/apps/openmw/mwworld/worldimp.hpp b/apps/openmw/mwworld/worldimp.hpp index 68a0d73eb8..c48504cb05 100644 --- a/apps/openmw/mwworld/worldimp.hpp +++ b/apps/openmw/mwworld/worldimp.hpp @@ -280,12 +280,12 @@ namespace MWWorld char getGlobalVariableType(std::string_view name) const override; ///< Return ' ', if there is no global variable with this name. - std::string getCellName (const MWWorld::CellStore *cell = nullptr) const override; + std::string_view getCellName(const MWWorld::CellStore* cell = nullptr) const override; ///< Return name of the cell. /// /// \note If cell==0, the cell the player is currently in will be used instead to /// generate a name. - std::string getCellName(const ESM::Cell* cell) const override; + std::string_view getCellName(const ESM::Cell* cell) const override; void removeRefScript (MWWorld::RefData *ref) override; //< Remove the script attached to ref from mLocalScripts diff --git a/apps/openmw_test_suite/mwscript/test_utils.hpp b/apps/openmw_test_suite/mwscript/test_utils.hpp index 780a0ac519..67173810ca 100644 --- a/apps/openmw_test_suite/mwscript/test_utils.hpp +++ b/apps/openmw_test_suite/mwscript/test_utils.hpp @@ -140,7 +140,7 @@ namespace LocalVariables mLocals; std::map> mMembers; public: - std::string getTarget() const override { return {}; }; + std::string_view getTarget() const override { return {}; }; int getLocalShort(int index) const override { return mLocals.getShort(index); }; @@ -176,29 +176,29 @@ namespace std::string getActionBinding(std::string_view action) const override { return {}; }; - std::string getActorName() const override { return {}; }; + std::string_view getActorName() const override { return {}; }; - std::string getNPCRace() const override { return {}; }; + std::string_view getNPCRace() const override { return {}; }; - std::string getNPCClass() const override { return {}; }; + std::string_view getNPCClass() const override { return {}; }; - std::string getNPCFaction() const override { return {}; }; + std::string_view getNPCFaction() const override { return {}; }; - std::string getNPCRank() const override { return {}; }; + std::string_view getNPCRank() const override { return {}; }; - std::string getPCName() const override { return {}; }; + std::string_view getPCName() const override { return {}; }; - std::string getPCRace() const override { return {}; }; + std::string_view getPCRace() const override { return {}; }; - std::string getPCClass() const override { return {}; }; + std::string_view getPCClass() const override { return {}; }; - std::string getPCRank() const override { return {}; }; + std::string_view getPCRank() const override { return {}; }; - std::string getPCNextRank() const override { return {}; }; + std::string_view getPCNextRank() const override { return {}; }; int getPCBounty() const override { return {}; }; - std::string getCurrentCellName() const override { return {}; }; + std::string_view getCurrentCellName() const override { return {}; }; int getMemberShort(std::string_view id, std::string_view name, bool global) const override { diff --git a/components/interpreter/context.hpp b/components/interpreter/context.hpp index df97bcf232..7ff627be0a 100644 --- a/components/interpreter/context.hpp +++ b/components/interpreter/context.hpp @@ -13,7 +13,7 @@ namespace Interpreter virtual ~Context() {} - virtual std::string getTarget() const = 0; + virtual std::string_view getTarget() const = 0; virtual int getLocalShort (int index) const = 0; @@ -56,29 +56,29 @@ namespace Interpreter virtual std::string getActionBinding(std::string_view action) const = 0; - virtual std::string getActorName() const = 0; + virtual std::string_view getActorName() const = 0; - virtual std::string getNPCRace() const = 0; + virtual std::string_view getNPCRace() const = 0; - virtual std::string getNPCClass() const = 0; + virtual std::string_view getNPCClass() const = 0; - virtual std::string getNPCFaction() const = 0; + virtual std::string_view getNPCFaction() const = 0; - virtual std::string getNPCRank() const = 0; + virtual std::string_view getNPCRank() const = 0; - virtual std::string getPCName() const = 0; + virtual std::string_view getPCName() const = 0; - virtual std::string getPCRace() const = 0; + virtual std::string_view getPCRace() const = 0; - virtual std::string getPCClass() const = 0; + virtual std::string_view getPCClass() const = 0; - virtual std::string getPCRank() const = 0; + virtual std::string_view getPCRank() const = 0; - virtual std::string getPCNextRank() const = 0; + virtual std::string_view getPCNextRank() const = 0; virtual int getPCBounty() const = 0; - virtual std::string getCurrentCellName() const = 0; + virtual std::string_view getCurrentCellName() const = 0; virtual int getMemberShort(std::string_view id, std::string_view name, bool global) const = 0;