From dcaa408bc1b14cf20a182fce811347dc02fea651 Mon Sep 17 00:00:00 2001 From: dankmeme01 <42031238+dankmeme01@users.noreply.github.com> Date: Tue, 9 Jan 2024 14:47:11 +0100 Subject: [PATCH] use string_view instead of string& in Mod --- loader/include/Geode/loader/Mod.hpp | 42 ++++++++++++++--------------- loader/src/loader/Mod.cpp | 12 ++++----- loader/src/loader/ModImpl.cpp | 25 +++++++++-------- loader/src/loader/ModImpl.hpp | 10 +++---- 4 files changed, 46 insertions(+), 43 deletions(-) diff --git a/loader/include/Geode/loader/Mod.hpp b/loader/include/Geode/loader/Mod.hpp index 5c75aa81..9f5e7c4c 100644 --- a/loader/include/Geode/loader/Mod.hpp +++ b/loader/include/Geode/loader/Mod.hpp @@ -90,12 +90,12 @@ namespace geode { ModMetadata getMetadata() const; ghc::filesystem::path getTempDir() const; /** - * Get the path to the mod's platform binary (.dll on Windows, .dylib + * Get the path to the mod's platform binary (.dll on Windows, .dylib * on Mac & iOS, .so on Android) */ ghc::filesystem::path getBinaryPath() const; /** - * Get the path to the mod's runtime resources directory (contains all + * Get the path to the mod's runtime resources directory (contains all * of its resources) */ ghc::filesystem::path getResourcesDir() const; @@ -119,25 +119,25 @@ namespace geode { bool hasSettings() const; std::vector getSettingKeys() const; - bool hasSetting(std::string const& key) const; - std::optional getSettingDefinition(std::string const& key) const; - SettingValue* getSetting(std::string const& key) const; + bool hasSetting(std::string_view const key) const; + std::optional getSettingDefinition(std::string_view const key) const; + SettingValue* getSetting(std::string_view const key) const; /** - * Register a custom setting's value class. See Mod::addCustomSetting - * for a convenience wrapper that creates the value in-place to avoid - * code duplication. Also see - * [the tutorial page](https://docs.geode-sdk.org/mods/settings) for + * Register a custom setting's value class. See Mod::addCustomSetting + * for a convenience wrapper that creates the value in-place to avoid + * code duplication. Also see + * [the tutorial page](https://docs.geode-sdk.org/mods/settings) for * more information about custom settings * @param key The setting's key * @param value The SettingValue class that shall handle this setting * @see addCustomSetting */ - void registerCustomSetting(std::string const& key, std::unique_ptr value); + void registerCustomSetting(std::string_view const key, std::unique_ptr value); /** - * Register a custom setting's value class. The new SettingValue class - * will be created in-place using `std::make_unique`. See - * [the tutorial page](https://docs.geode-sdk.org/mods/settings) for + * Register a custom setting's value class. The new SettingValue class + * will be created in-place using `std::make_unique`. See + * [the tutorial page](https://docs.geode-sdk.org/mods/settings) for * more information about custom settings * @param key The setting's key * @param value The value of the custom setting @@ -147,14 +147,14 @@ namespace geode { * } */ template - void addCustomSetting(std::string const& key, V const& value) { + void addCustomSetting(std::string_view const key, V const& value) { this->registerCustomSetting(key, std::make_unique(key, this->getID(), value)); } matjson::Value& getSaveContainer(); template - T getSettingValue(std::string const& key) const { + T getSettingValue(std::string_view const key) const { if (auto sett = this->getSetting(key)) { return SettingValueSetter::get(sett); } @@ -162,7 +162,7 @@ namespace geode { } template - T setSettingValue(std::string const& key, T const& value) { + T setSettingValue(std::string_view const key, T const& value) { if (auto sett = this->getSetting(key)) { auto old = this->getSettingValue(key); SettingValueSetter::set(sett, value); @@ -171,10 +171,10 @@ namespace geode { return T(); } - bool hasSavedValue(std::string const& key); + bool hasSavedValue(std::string_view const key); template - T getSavedValue(std::string const& key) { + T getSavedValue(std::string_view const key) { auto& saved = this->getSaveContainer(); if (saved.contains(key)) { try { @@ -188,7 +188,7 @@ namespace geode { } template - T getSavedValue(std::string const& key, T const& defaultValue) { + T getSavedValue(std::string_view const key, T const& defaultValue) { auto& saved = this->getSaveContainer(); if (saved.contains(key)) { try { @@ -210,7 +210,7 @@ namespace geode { * @returns The old value */ template - T setSavedValue(std::string const& key, T const& value) { + T setSavedValue(std::string_view const key, T const& value) { auto& saved = this->getSaveContainer(); auto old = this->getSavedValue(key); saved[key] = value; @@ -335,7 +335,7 @@ namespace geode { * Check whether or not this Mod * depends on another mod */ - bool depends(std::string const& id) const; + bool depends(std::string_view const id) const; /** * Check whether all the required diff --git a/loader/src/loader/Mod.cpp b/loader/src/loader/Mod.cpp index 1bd6f3eb..4f8d7640 100644 --- a/loader/src/loader/Mod.cpp +++ b/loader/src/loader/Mod.cpp @@ -105,19 +105,19 @@ std::vector Mod::getSettingKeys() const { return m_impl->getSettingKeys(); } -bool Mod::hasSetting(std::string const& key) const { +bool Mod::hasSetting(std::string_view const key) const { return m_impl->hasSetting(key); } -std::optional Mod::getSettingDefinition(std::string const& key) const { +std::optional Mod::getSettingDefinition(std::string_view const key) const { return m_impl->getSettingDefinition(key); } -SettingValue* Mod::getSetting(std::string const& key) const { +SettingValue* Mod::getSetting(std::string_view const key) const { return m_impl->getSetting(key); } -void Mod::registerCustomSetting(std::string const& key, std::unique_ptr value) { +void Mod::registerCustomSetting(std::string_view const key, std::unique_ptr value) { return m_impl->registerCustomSetting(key, std::move(value)); } @@ -172,7 +172,7 @@ ModRequestedAction Mod::getRequestedAction() const { return m_impl->getRequestedAction(); } -bool Mod::depends(std::string const& id) const { +bool Mod::depends(std::string_view const id) const { return m_impl->depends(id); } @@ -200,7 +200,7 @@ void Mod::setLoggingEnabled(bool enabled) { m_impl->setLoggingEnabled(enabled); } -bool Mod::hasSavedValue(std::string const& key) { +bool Mod::hasSavedValue(std::string_view const key) { return this->getSaveContainer().contains(key); } diff --git a/loader/src/loader/ModImpl.cpp b/loader/src/loader/ModImpl.cpp index fb7b7529..da733a1e 100644 --- a/loader/src/loader/ModImpl.cpp +++ b/loader/src/loader/ModImpl.cpp @@ -258,13 +258,14 @@ void Mod::Impl::setupSettings() { } } -void Mod::Impl::registerCustomSetting(std::string const& key, std::unique_ptr value) { - if (!m_settings.count(key)) { +void Mod::Impl::registerCustomSetting(std::string_view const key, std::unique_ptr value) { + auto keystr = std::string(key); + if (!m_settings.count(keystr)) { // load data if (m_savedSettingsData.contains(key)) { value->load(m_savedSettingsData[key]); } - m_settings.emplace(key, std::move(value)); + m_settings.emplace(keystr, std::move(value)); } } @@ -280,7 +281,7 @@ std::vector Mod::Impl::getSettingKeys() const { return keys; } -std::optional Mod::Impl::getSettingDefinition(std::string const& key) const { +std::optional Mod::Impl::getSettingDefinition(std::string_view const key) const { for (auto& setting : m_metadata.getSettings()) { if (setting.first == key) { return setting.second; @@ -289,14 +290,16 @@ std::optional Mod::Impl::getSettingDefinition(std::string const& key) c return std::nullopt; } -SettingValue* Mod::Impl::getSetting(std::string const& key) const { - if (m_settings.count(key)) { - return m_settings.at(key).get(); +SettingValue* Mod::Impl::getSetting(std::string_view const key) const { + auto keystr = std::string(key); + + if (m_settings.count(keystr)) { + return m_settings.at(keystr).get(); } return nullptr; } -bool Mod::Impl::hasSetting(std::string const& key) const { +bool Mod::Impl::hasSetting(std::string_view const key) const { for (auto& setting : m_metadata.getSettings()) { if (setting.first == key) { return true; @@ -449,7 +452,7 @@ bool Mod::Impl::hasUnresolvedIncompatibilities() const { return false; } -bool Mod::Impl::depends(std::string const& id) const { +bool Mod::Impl::depends(std::string_view const id) const { return utils::ranges::contains(m_metadata.getDependencies(), [id](ModMetadata::Dependency const& t) { return t.id == id; }); @@ -585,13 +588,13 @@ Result<> Mod::Impl::unzipGeodeFile(ModMetadata metadata) { if (ec) { return Err("Unable to delete temp dir: " + ec.message()); } - + (void)utils::file::createDirectoryAll(tempDir); auto res = file::writeString(datePath, modifiedHash); if (!res) { log::warn("Failed to write modified date of geode zip: {}", res.unwrapErr()); } - + GEODE_UNWRAP_INTO(auto unzip, file::Unzip::create(metadata.getPath())); if (!unzip.hasEntry(metadata.getBinaryName())) { diff --git a/loader/src/loader/ModImpl.hpp b/loader/src/loader/ModImpl.hpp index 1ca78a4f..2a9bf68e 100644 --- a/loader/src/loader/ModImpl.hpp +++ b/loader/src/loader/ModImpl.hpp @@ -109,10 +109,10 @@ namespace geode { bool hasSettings() const; std::vector getSettingKeys() const; - bool hasSetting(std::string const& key) const; - std::optional getSettingDefinition(std::string const& key) const; - SettingValue* getSetting(std::string const& key) const; - void registerCustomSetting(std::string const& key, std::unique_ptr value); + bool hasSetting(std::string_view const key) const; + std::optional getSettingDefinition(std::string_view const key) const; + SettingValue* getSetting(std::string_view const key) const; + void registerCustomSetting(std::string_view const key, std::unique_ptr value); std::vector getHooks() const; Result addHook(Hook* hook); @@ -129,7 +129,7 @@ namespace geode { // 1.3.0 additions ModRequestedAction getRequestedAction() const; - bool depends(std::string const& id) const; + bool depends(std::string_view const id) const; Result<> updateDependencies(); bool hasUnresolvedDependencies() const; bool hasUnresolvedIncompatibilities() const;