From 4f75be3d819649c3739a30cf983edfaea06efa0f Mon Sep 17 00:00:00 2001 From: Oliver Hamlet Date: Sat, 8 May 2021 21:46:25 +0100 Subject: [PATCH] Make PluginInterface::GetHeaderVersion() return an optional While the value could actually be NaN, that's also what esplugin uses to signal that the version could not be found, and either way a NaN value is useless. --- include/loot/plugin_interface.h | 6 +++--- src/api/plugin.cpp | 7 ++++++- src/api/plugin.h | 2 +- src/tests/api/internals/plugin_test.h | 14 +++++++------- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/include/loot/plugin_interface.h b/include/loot/plugin_interface.h index 2f41a1fb..7ba3c0f4 100644 --- a/include/loot/plugin_interface.h +++ b/include/loot/plugin_interface.h @@ -48,10 +48,10 @@ public: /** * Get the value of the version field in the HEDR subrecord of the plugin's * TES4 record. - * @return The value of the version field, or NaN if the field could not be - * found. + * @return The value of the version field, or an empty optional if that value + * is NaN or could not be found. */ - virtual float GetHeaderVersion() const = 0; + virtual std::optional GetHeaderVersion() const = 0; /** * Get the plugin's version number from its description field. diff --git a/src/api/plugin.cpp b/src/api/plugin.cpp index 96d5793d..72ef0928 100644 --- a/src/api/plugin.cpp +++ b/src/api/plugin.cpp @@ -90,14 +90,19 @@ Plugin::Plugin(const GameType gameType, std::string Plugin::GetName() const { return name_; } -float Plugin::GetHeaderVersion() const { +std::optional Plugin::GetHeaderVersion() const { float version; + auto ret = esp_plugin_header_version(esPlugin.get(), &version); if (ret != ESP_OK) { throw FileAccessError(name_ + " : esplugin error code: " + std::to_string(ret)); } + if (std::isnan(version)) { + return std::nullopt; + } + return version; } diff --git a/src/api/plugin.h b/src/api/plugin.h index a6acf300..cca7d26b 100644 --- a/src/api/plugin.h +++ b/src/api/plugin.h @@ -49,7 +49,7 @@ public: const bool headerOnly); std::string GetName() const; - float GetHeaderVersion() const; + std::optional GetHeaderVersion() const; std::optional GetVersion() const; std::vector GetMasters() const; std::vector GetBashTags() const; diff --git a/src/tests/api/internals/plugin_test.h b/src/tests/api/internals/plugin_test.h index 883af39c..5997413a 100644 --- a/src/tests/api/internals/plugin_test.h +++ b/src/tests/api/internals/plugin_test.h @@ -140,7 +140,7 @@ private: class OtherPluginType : public PluginInterface { public: std::string GetName() const { return ""; } - float GetHeaderVersion() const { return 0.0f; } + std::optional GetHeaderVersion() const { return 0.0f; } std::optional GetVersion() const { return std::nullopt; } std::vector GetMasters() const { return std::vector(); @@ -189,11 +189,11 @@ TEST_P(PluginTest, loadingHeaderOnlyShouldReadHeaderData) { EXPECT_EQ("5.0", plugin.GetVersion()); if (GetParam() == GameType::tes3) { - EXPECT_FLOAT_EQ(1.2f, plugin.GetHeaderVersion()); + EXPECT_FLOAT_EQ(1.2f, plugin.GetHeaderVersion().value()); } else if (GetParam() == GameType::tes4) { - EXPECT_FLOAT_EQ(0.8f, plugin.GetHeaderVersion()); + EXPECT_FLOAT_EQ(0.8f, plugin.GetHeaderVersion().value()); } else { - EXPECT_FLOAT_EQ(0.94f, plugin.GetHeaderVersion()); + EXPECT_FLOAT_EQ(0.94f, plugin.GetHeaderVersion().value()); } } @@ -215,11 +215,11 @@ TEST_P(PluginTest, loadingWholePluginShouldReadHeaderData) { EXPECT_EQ("5.0", plugin.GetVersion()); if (GetParam() == GameType::tes3) { - EXPECT_FLOAT_EQ(1.2f, plugin.GetHeaderVersion()); + EXPECT_FLOAT_EQ(1.2f, plugin.GetHeaderVersion().value()); } else if (GetParam() == GameType::tes4) { - EXPECT_FLOAT_EQ(0.8f, plugin.GetHeaderVersion()); + EXPECT_FLOAT_EQ(0.8f, plugin.GetHeaderVersion().value()); } else { - EXPECT_FLOAT_EQ(0.94f, plugin.GetHeaderVersion()); + EXPECT_FLOAT_EQ(0.94f, plugin.GetHeaderVersion().value()); } }