Merge pull request #417 from dankmeme01/main

use string_view instead of string& in Mod
This commit is contained in:
mat 2024-01-09 11:00:24 -03:00 committed by GitHub
commit e1161a96c6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 43 deletions

View file

@ -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<std::string> getSettingKeys() const;
bool hasSetting(std::string const& key) const;
std::optional<Setting> getSettingDefinition(std::string const& key) const;
SettingValue* getSetting(std::string const& key) const;
bool hasSetting(std::string_view const key) const;
std::optional<Setting> 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<SettingValue> value);
void registerCustomSetting(std::string_view const key, std::unique_ptr<SettingValue> 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 <class T, class V>
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<T>(key, this->getID(), value));
}
matjson::Value& getSaveContainer();
template <class T>
T getSettingValue(std::string const& key) const {
T getSettingValue(std::string_view const key) const {
if (auto sett = this->getSetting(key)) {
return SettingValueSetter<T>::get(sett);
}
@ -162,7 +162,7 @@ namespace geode {
}
template <class T>
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<T>(key);
SettingValueSetter<T>::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 <class T>
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 <class T>
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 <class T>
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<T>(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

View file

@ -105,19 +105,19 @@ std::vector<std::string> 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<Setting> Mod::getSettingDefinition(std::string const& key) const {
std::optional<Setting> 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<SettingValue> value) {
void Mod::registerCustomSetting(std::string_view const key, std::unique_ptr<SettingValue> 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);
}

View file

@ -258,13 +258,14 @@ void Mod::Impl::setupSettings() {
}
}
void Mod::Impl::registerCustomSetting(std::string const& key, std::unique_ptr<SettingValue> value) {
if (!m_settings.count(key)) {
void Mod::Impl::registerCustomSetting(std::string_view const key, std::unique_ptr<SettingValue> 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<std::string> Mod::Impl::getSettingKeys() const {
return keys;
}
std::optional<Setting> Mod::Impl::getSettingDefinition(std::string const& key) const {
std::optional<Setting> 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<Setting> 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())) {

View file

@ -109,10 +109,10 @@ namespace geode {
bool hasSettings() const;
std::vector<std::string> getSettingKeys() const;
bool hasSetting(std::string const& key) const;
std::optional<Setting> getSettingDefinition(std::string const& key) const;
SettingValue* getSetting(std::string const& key) const;
void registerCustomSetting(std::string const& key, std::unique_ptr<SettingValue> value);
bool hasSetting(std::string_view const key) const;
std::optional<Setting> 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<SettingValue> value);
std::vector<Hook*> getHooks() const;
Result<Hook*> 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;