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.
This commit is contained in:
Oliver Hamlet
2021-05-08 21:46:25 +01:00
parent 97b8db9191
commit 4f75be3d81
4 changed files with 17 additions and 12 deletions
+3 -3
View File
@@ -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<float> GetHeaderVersion() const = 0;
/**
* Get the plugin's version number from its description field.
+6 -1
View File
@@ -90,14 +90,19 @@ Plugin::Plugin(const GameType gameType,
std::string Plugin::GetName() const { return name_; }
float Plugin::GetHeaderVersion() const {
std::optional<float> 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;
}
+1 -1
View File
@@ -49,7 +49,7 @@ public:
const bool headerOnly);
std::string GetName() const;
float GetHeaderVersion() const;
std::optional<float> GetHeaderVersion() const;
std::optional<std::string> GetVersion() const;
std::vector<std::string> GetMasters() const;
std::vector<Tag> GetBashTags() const;
+7 -7
View File
@@ -140,7 +140,7 @@ private:
class OtherPluginType : public PluginInterface {
public:
std::string GetName() const { return ""; }
float GetHeaderVersion() const { return 0.0f; }
std::optional<float> GetHeaderVersion() const { return 0.0f; }
std::optional<std::string> GetVersion() const { return std::nullopt; }
std::vector<std::string> GetMasters() const {
return std::vector<std::string>();
@@ -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());
}
}