From fb9bc5f535318315003d484905df6cf9b3e615a2 Mon Sep 17 00:00:00 2001 From: Evil Eye Date: Sun, 28 Aug 2022 17:20:49 +0200 Subject: [PATCH] Use string_view in Fallback::Map --- apps/opencs/model/world/columns.cpp | 2 +- apps/opencs/view/render/cellwater.cpp | 5 ++-- apps/openmw/engine.cpp | 4 +-- apps/openmw/mwbase/windowmanager.hpp | 2 +- apps/openmw/mwgui/charactercreation.cpp | 8 +++--- apps/openmw/mwgui/levelupdialog.cpp | 7 +++-- apps/openmw/mwgui/windowmanagerimp.cpp | 4 +-- apps/openmw/mwgui/windowmanagerimp.hpp | 2 +- apps/openmw/mwrender/effectmanager.cpp | 2 +- apps/openmw/mwrender/effectmanager.hpp | 2 +- apps/openmw/mwrender/renderingmanager.cpp | 2 +- apps/openmw/mwrender/renderingmanager.hpp | 2 +- apps/openmw/mwrender/ripplesimulation.cpp | 2 +- apps/openmw/mwrender/water.cpp | 4 +-- apps/openmw/mwscript/miscextensions.cpp | 2 +- apps/openmw/mwworld/worldimp.cpp | 6 ++-- components/fallback/fallback.cpp | 35 ++++++++++++----------- components/fallback/fallback.hpp | 13 +++++---- components/fontloader/fontloader.cpp | 4 +-- components/widgets/tags.cpp | 4 +-- 20 files changed, 59 insertions(+), 53 deletions(-) diff --git a/apps/opencs/model/world/columns.cpp b/apps/opencs/model/world/columns.cpp index fef5d99659..050b65a36a 100644 --- a/apps/opencs/model/world/columns.cpp +++ b/apps/opencs/model/world/columns.cpp @@ -629,7 +629,7 @@ std::vector>CSMWorld::Columns::getEnums (ColumnId col { for (int i=0; i<8; i++) { - const std::string& bloodName = Fallback::Map::getString("Blood_Texture_Name_" + std::to_string(i)); + std::string_view bloodName = Fallback::Map::getString("Blood_Texture_Name_" + std::to_string(i)); if (!bloodName.empty()) enums.emplace_back(i, bloodName); } diff --git a/apps/opencs/view/render/cellwater.cpp b/apps/opencs/view/render/cellwater.cpp index 74eb4dddfc..9681e5cdce 100644 --- a/apps/opencs/view/render/cellwater.cpp +++ b/apps/opencs/view/render/cellwater.cpp @@ -162,8 +162,9 @@ namespace CSVRender mWaterGeometry->setStateSet(SceneUtil::createSimpleWaterStateSet(Alpha, RenderBin)); // Add water texture - std::string textureName = Fallback::Map::getString("Water_SurfaceTexture"); - textureName = "textures/water/" + textureName + "00.dds"; + std::string textureName = "textures/water/"; + textureName += Fallback::Map::getString("Water_SurfaceTexture"); + textureName += "00.dds"; Resource::ImageManager* imageManager = mData.getResourceSystem()->getImageManager(); diff --git a/apps/openmw/engine.cpp b/apps/openmw/engine.cpp index 47a400d297..067fe50100 100644 --- a/apps/openmw/engine.cpp +++ b/apps/openmw/engine.cpp @@ -850,7 +850,7 @@ void OMW::Engine::prepareEngine() if (!mSkipMenu) { - const std::string& logo = Fallback::Map::getString("Movies_Company_Logo"); + std::string_view logo = Fallback::Map::getString("Movies_Company_Logo"); if (!logo.empty()) mWindowManager->playVideo(logo, true); } @@ -1069,7 +1069,7 @@ void OMW::Engine::go() // start in main menu mWindowManager->pushGuiMode (MWGui::GM_MainMenu); mSoundManager->playTitleMusic(); - const std::string& logo = Fallback::Map::getString("Movies_Morrowind_Logo"); + std::string_view logo = Fallback::Map::getString("Movies_Morrowind_Logo"); if (!logo.empty()) mWindowManager->playVideo(logo, /*allowSkipping*/true, /*overrideSounds*/false); } diff --git a/apps/openmw/mwbase/windowmanager.hpp b/apps/openmw/mwbase/windowmanager.hpp index e3a1a0c725..95eb3e68df 100644 --- a/apps/openmw/mwbase/windowmanager.hpp +++ b/apps/openmw/mwbase/windowmanager.hpp @@ -108,7 +108,7 @@ namespace MWBase /// @note This method will block until the video finishes playing /// (and will continually update the window while doing so) - virtual void playVideo(const std::string& name, bool allowSkipping, bool overrideSounds = true) = 0; + virtual void playVideo(std::string_view name, bool allowSkipping, bool overrideSounds = true) = 0; virtual void setNewGame(bool newgame) = 0; diff --git a/apps/openmw/mwgui/charactercreation.cpp b/apps/openmw/mwgui/charactercreation.cpp index b817decad3..c715db6c7a 100644 --- a/apps/openmw/mwgui/charactercreation.cpp +++ b/apps/openmw/mwgui/charactercreation.cpp @@ -45,10 +45,10 @@ namespace { number++; - std::string question = Fallback::Map::getString("Question_" + MyGUI::utility::toString(number) + "_Question"); - std::string answer0 = Fallback::Map::getString("Question_" + MyGUI::utility::toString(number) + "_AnswerOne"); - std::string answer1 = Fallback::Map::getString("Question_" + MyGUI::utility::toString(number) + "_AnswerTwo"); - std::string answer2 = Fallback::Map::getString("Question_" + MyGUI::utility::toString(number) + "_AnswerThree"); + std::string question{Fallback::Map::getString("Question_" + MyGUI::utility::toString(number) + "_Question")}; + std::string answer0{Fallback::Map::getString("Question_" + MyGUI::utility::toString(number) + "_AnswerOne")}; + std::string answer1{Fallback::Map::getString("Question_" + MyGUI::utility::toString(number) + "_AnswerTwo")}; + std::string answer2{Fallback::Map::getString("Question_" + MyGUI::utility::toString(number) + "_AnswerThree")}; std::string sound = "vo\\misc\\chargen qa" + MyGUI::utility::toString(number) + ".wav"; Response r0 = {answer0, ESM::Class::Combat}; diff --git a/apps/openmw/mwgui/levelupdialog.cpp b/apps/openmw/mwgui/levelupdialog.cpp index 14de3fd274..a4609ec16a 100644 --- a/apps/openmw/mwgui/levelupdialog.cpp +++ b/apps/openmw/mwgui/levelupdialog.cpp @@ -18,6 +18,7 @@ #include "../mwmechanics/actorutil.hpp" #include "class.hpp" +#include "ustring.hpp" namespace MWGui { @@ -138,13 +139,13 @@ namespace MWGui int level = creatureStats.getLevel ()+1; mLevelText->setCaptionWithReplacing("#{sLevelUpMenu1} " + MyGUI::utility::toString(level)); - std::string levelupdescription; + std::string_view levelupdescription; levelupdescription = Fallback::Map::getString("Level_Up_Level"+MyGUI::utility::toString(level)); - if (levelupdescription == "") + if (levelupdescription.empty()) levelupdescription = Fallback::Map::getString("Level_Up_Default"); - mLevelDescription->setCaption (levelupdescription); + mLevelDescription->setCaption(toUString(levelupdescription)); unsigned int availableAttributes = 0; for (int i = 0; i < 8; ++i) diff --git a/apps/openmw/mwgui/windowmanagerimp.cpp b/apps/openmw/mwgui/windowmanagerimp.cpp index 0c4d36a185..e51d3c7b33 100644 --- a/apps/openmw/mwgui/windowmanagerimp.cpp +++ b/apps/openmw/mwgui/windowmanagerimp.cpp @@ -1807,9 +1807,9 @@ namespace MWGui && (!isGuiMode() || (mGuiModes.size() == 1 && (getMode() == GM_MainMenu || getMode() == GM_Rest))); } - void WindowManager::playVideo(const std::string &name, bool allowSkipping, bool overrideSounds) + void WindowManager::playVideo(std::string_view name, bool allowSkipping, bool overrideSounds) { - mVideoWidget->playVideo("video\\" + name); + mVideoWidget->playVideo("video\\" + std::string{name}); mVideoWidget->eventKeyButtonPressed.clear(); mVideoBackground->eventKeyButtonPressed.clear(); diff --git a/apps/openmw/mwgui/windowmanagerimp.hpp b/apps/openmw/mwgui/windowmanagerimp.hpp index 14a7f89115..0b506d17a2 100644 --- a/apps/openmw/mwgui/windowmanagerimp.hpp +++ b/apps/openmw/mwgui/windowmanagerimp.hpp @@ -149,7 +149,7 @@ namespace MWGui /// @note This method will block until the video finishes playing /// (and will continually update the window while doing so) - void playVideo(const std::string& name, bool allowSkipping, bool overrideSounds = true) override; + void playVideo(std::string_view name, bool allowSkipping, bool overrideSounds = true) override; /// Warning: do not use MyGUI::InputManager::setKeyFocusWidget directly. Instead use this. void setKeyFocusWidget (MyGUI::Widget* widget) override; diff --git a/apps/openmw/mwrender/effectmanager.cpp b/apps/openmw/mwrender/effectmanager.cpp index 371f488c3d..6b1543eda9 100644 --- a/apps/openmw/mwrender/effectmanager.cpp +++ b/apps/openmw/mwrender/effectmanager.cpp @@ -27,7 +27,7 @@ EffectManager::~EffectManager() clear(); } -void EffectManager::addEffect(const std::string &model, const std::string& textureOverride, const osg::Vec3f &worldPosition, float scale, bool isMagicVFX) +void EffectManager::addEffect(const std::string& model, std::string_view textureOverride, const osg::Vec3f& worldPosition, float scale, bool isMagicVFX) { osg::ref_ptr node = mResourceSystem->getSceneManager()->getInstance(model); diff --git a/apps/openmw/mwrender/effectmanager.hpp b/apps/openmw/mwrender/effectmanager.hpp index 0d94a63c8f..f974a3a01e 100644 --- a/apps/openmw/mwrender/effectmanager.hpp +++ b/apps/openmw/mwrender/effectmanager.hpp @@ -33,7 +33,7 @@ namespace MWRender ~EffectManager(); /// Add an effect. When it's finished playing, it will be removed automatically. - void addEffect (const std::string& model, const std::string& textureOverride, const osg::Vec3f& worldPosition, float scale, bool isMagicVFX = true); + void addEffect(const std::string& model, std::string_view textureOverride, const osg::Vec3f& worldPosition, float scale, bool isMagicVFX = true); void update(float dt); diff --git a/apps/openmw/mwrender/renderingmanager.cpp b/apps/openmw/mwrender/renderingmanager.cpp index fc45d06828..bf0d2e3baa 100644 --- a/apps/openmw/mwrender/renderingmanager.cpp +++ b/apps/openmw/mwrender/renderingmanager.cpp @@ -1174,7 +1174,7 @@ namespace MWRender mActorsPaths->updatePtr(old, updated); } - void RenderingManager::spawnEffect(const std::string &model, const std::string &texture, const osg::Vec3f &worldPosition, float scale, bool isMagicVFX) + void RenderingManager::spawnEffect(const std::string& model, std::string_view texture, const osg::Vec3f& worldPosition, float scale, bool isMagicVFX) { mEffectManager->addEffect(model, texture, worldPosition, scale, isMagicVFX); } diff --git a/apps/openmw/mwrender/renderingmanager.hpp b/apps/openmw/mwrender/renderingmanager.hpp index 83dfd32287..68a77c39f3 100644 --- a/apps/openmw/mwrender/renderingmanager.hpp +++ b/apps/openmw/mwrender/renderingmanager.hpp @@ -184,7 +184,7 @@ namespace MWRender SkyManager* getSkyManager(); - void spawnEffect(const std::string &model, const std::string &texture, const osg::Vec3f &worldPosition, float scale = 1.f, bool isMagicVFX = true); + void spawnEffect(const std::string& model, std::string_view texture, const osg::Vec3f& worldPosition, float scale = 1.f, bool isMagicVFX = true); /// Clear all savegame-specific data void clear(); diff --git a/apps/openmw/mwrender/ripplesimulation.cpp b/apps/openmw/mwrender/ripplesimulation.cpp index 037ed4455f..0bb92cdd49 100644 --- a/apps/openmw/mwrender/ripplesimulation.cpp +++ b/apps/openmw/mwrender/ripplesimulation.cpp @@ -34,7 +34,7 @@ namespace if (rippleFrameCount <= 0) return; - const std::string& tex = Fallback::Map::getString("Water_RippleTexture"); + std::string_view tex = Fallback::Map::getString("Water_RippleTexture"); std::vector > textures; for (int i=0; i > textures; const int frameCount = std::clamp(Fallback::Map::getInt("Water_SurfaceFrameCount"), 0, 320); - const std::string& texture = Fallback::Map::getString("Water_SurfaceTexture"); + std::string_view texture = Fallback::Map::getString("Water_SurfaceTexture"); for (int i=0; i &textures) { const int frameCount = std::clamp(Fallback::Map::getInt("Water_SurfaceFrameCount"), 0, 320); - const std::string& texture = Fallback::Map::getString("Water_SurfaceTexture"); + std::string_view texture = Fallback::Map::getString("Water_SurfaceTexture"); for (int i=0; iplayVideo(video, true); } @@ -3713,12 +3713,12 @@ namespace MWWorld if (ptr == getPlayerPtr() && Settings::Manager::getBool("hit fader", "GUI")) return; - std::string texture = Fallback::Map::getString("Blood_Texture_" + std::to_string(ptr.getClass().getBloodTexture(ptr))); + std::string_view texture = Fallback::Map::getString("Blood_Texture_" + std::to_string(ptr.getClass().getBloodTexture(ptr))); if (texture.empty()) texture = Fallback::Map::getString("Blood_Texture_0"); std::string model = Misc::ResourceHelpers::correctMeshPath( - Fallback::Map::getString("Blood_Model_" + std::to_string(Misc::Rng::rollDice(3))), // [0, 2] + std::string{Fallback::Map::getString("Blood_Model_" + std::to_string(Misc::Rng::rollDice(3)))}, // [0, 2] mResourceSystem->getVFS()); mRendering->spawnEffect(model, texture, worldPosition, 1.0f, false); diff --git a/components/fallback/fallback.cpp b/components/fallback/fallback.cpp index a151bd40b6..361f7b71fc 100644 --- a/components/fallback/fallback.cpp +++ b/components/fallback/fallback.cpp @@ -6,29 +6,31 @@ namespace Fallback { - std::map Map::mFallbackMap; + std::map> Map::mFallbackMap; void Map::init(const std::map& fallback) { - mFallbackMap = fallback; + for(const auto& entry : fallback) + mFallbackMap.insert(entry); } - std::string Map::getString(const std::string& fall) + std::string_view Map::getString(std::string_view fall) { - std::map::const_iterator it; - if ((it = mFallbackMap.find(fall)) == mFallbackMap.end()) + auto it = mFallbackMap.find(fall); + if (it == mFallbackMap.end()) { - return std::string(); + return {}; } return it->second; } - float Map::getFloat(const std::string& fall) + float Map::getFloat(std::string_view fall) { - const std::string& fallback = getString(fall); + std::string_view fallback = getString(fall); if (!fallback.empty()) { - std::stringstream stream(fallback); + std::stringstream stream; + stream << fallback; float number = 0.f; stream >> number; return number; @@ -37,12 +39,13 @@ namespace Fallback return 0; } - int Map::getInt(const std::string& fall) + int Map::getInt(std::string_view fall) { - const std::string& fallback = getString(fall); + std::string_view fallback = getString(fall); if (!fallback.empty()) { - std::stringstream stream(fallback); + std::stringstream stream; + stream << fallback; int number = 0; stream >> number; return number; @@ -51,15 +54,15 @@ namespace Fallback return 0; } - bool Map::getBool(const std::string& fall) + bool Map::getBool(std::string_view fall) { - const std::string& fallback = getString(fall); + std::string_view fallback = getString(fall); return !fallback.empty() && fallback != "0"; } - osg::Vec4f Map::getColour(const std::string& fall) + osg::Vec4f Map::getColour(std::string_view fall) { - const std::string& sum = getString(fall); + std::string_view sum = getString(fall); if (!sum.empty()) { try diff --git a/components/fallback/fallback.hpp b/components/fallback/fallback.hpp index 75a73be5d7..8eb7440cfa 100644 --- a/components/fallback/fallback.hpp +++ b/components/fallback/fallback.hpp @@ -3,6 +3,7 @@ #include #include +#include #include @@ -11,15 +12,15 @@ namespace Fallback /// @brief contains settings imported from the Morrowind INI file. class Map { - static std::map mFallbackMap; + static std::map> mFallbackMap; public: static void init(const std::map& fallback); - static std::string getString(const std::string& fall); - static float getFloat(const std::string& fall); - static int getInt(const std::string& fall); - static bool getBool(const std::string& fall); - static osg::Vec4f getColour(const std::string& fall); + static std::string_view getString(std::string_view fall); + static float getFloat(std::string_view fall); + static int getInt(std::string_view fall); + static bool getBool(std::string_view fall); + static osg::Vec4f getColour(std::string_view fall); }; } #endif diff --git a/components/fontloader/fontloader.cpp b/components/fontloader/fontloader.cpp index e575b8cbc0..05a584e065 100644 --- a/components/fontloader/fontloader.cpp +++ b/components/fontloader/fontloader.cpp @@ -169,8 +169,8 @@ namespace Gui void FontLoader::loadFonts() { - std::string defaultFont = Fallback::Map::getString("Fonts_Font_0"); - std::string scrollFont = Fallback::Map::getString("Fonts_Font_2"); + std::string defaultFont{Fallback::Map::getString("Fonts_Font_0")}; + std::string scrollFont{Fallback::Map::getString("Fonts_Font_2")}; loadFont(defaultFont, "DefaultFont"); loadFont(scrollFont, "ScrollFont"); loadFont("DejaVuLGCSansMono", "MonoFont"); // We need to use a TrueType monospace font to display debug texts properly. diff --git a/components/widgets/tags.cpp b/components/widgets/tags.cpp index e3575ea8f5..93b0fca447 100644 --- a/components/widgets/tags.cpp +++ b/components/widgets/tags.cpp @@ -18,7 +18,7 @@ bool replaceTag(const MyGUI::UString& tag, MyGUI::UString& out) if (tag.compare(0, fontcolourLength, fontcolour) == 0) { std::string fallbackName = "FontColor_color_" + tag.substr(fontcolourLength); - std::string str = Fallback::Map::getString(fallbackName); + std::string_view str = Fallback::Map::getString(fallbackName); if (str.empty()) throw std::runtime_error("Unknown fallback name: " + fallbackName); @@ -36,7 +36,7 @@ bool replaceTag(const MyGUI::UString& tag, MyGUI::UString& out) else if (tag.compare(0, fontcolourhtmlLength, fontcolourhtml) == 0) { std::string fallbackName = "FontColor_color_" + tag.substr(fontcolourhtmlLength); - std::string str = Fallback::Map::getString(fallbackName); + std::string_view str = Fallback::Map::getString(fallbackName); if (str.empty()) throw std::runtime_error("Unknown fallback name: " + fallbackName);