From 13060bb0cf33343424e4bb771c5f5aff9c2dd5ce Mon Sep 17 00:00:00 2001 From: Oliver Hamlet Date: Thu, 5 Nov 2015 17:26:58 +0000 Subject: [PATCH] Make Plugin.IsValid a static member --- src/backend/game/game.cpp | 2 +- src/backend/metadata/condition_grammar.h | 2 +- src/backend/plugin/plugin.cpp | 21 +++++++++++++-------- src/backend/plugin/plugin.h | 2 +- src/tests/backend/plugin/test_plugin.h | 11 +++-------- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/backend/game/game.cpp b/src/backend/game/game.cpp index 79db51d4..e63c7191 100644 --- a/src/backend/game/game.cpp +++ b/src/backend/game/game.cpp @@ -123,7 +123,7 @@ namespace loot { // First find out how many plugins there are, and their sizes. BOOST_LOG_TRIVIAL(trace) << "Scanning for plugins in " << this->DataPath(); for (fs::directory_iterator it(this->DataPath()); it != fs::directory_iterator(); ++it) { - if (fs::is_regular_file(it->status()) && Plugin(it->path().filename().string()).IsValid(*this)) { + if (fs::is_regular_file(it->status()) && Plugin::IsValid(it->path().filename().string(), *this)) { Plugin temp(it->path().filename().string()); BOOST_LOG_TRIVIAL(info) << "Found plugin: " << temp.Name(); diff --git a/src/backend/metadata/condition_grammar.h b/src/backend/metadata/condition_grammar.h index b5380683..e3b622d4 100644 --- a/src/backend/metadata/condition_grammar.h +++ b/src/backend/metadata/condition_grammar.h @@ -326,7 +326,7 @@ namespace loot { Version trueVersion; if (file == "LOOT") trueVersion = Version(boost::filesystem::absolute("LOOT.exe")); - else if (Plugin(file).IsValid(*_game)) { + else if (Plugin::IsValid(file, *_game)) { Plugin plugin(*_game, file, true); trueVersion = Version(plugin.Version()); } diff --git a/src/backend/plugin/plugin.cpp b/src/backend/plugin/plugin.cpp index b8c10764..55aa6380 100644 --- a/src/backend/plugin/plugin.cpp +++ b/src/backend/plugin/plugin.cpp @@ -224,24 +224,29 @@ namespace loot { return _isEmpty; } - bool Plugin::IsValid(const Game& game) const { - BOOST_LOG_TRIVIAL(trace) << "Checking to see if \"" << name << "\" is a valid plugin."; - // Check the extension, because only plugins with the .esm or .esp - // extension (or .ghost, which is trimmed) should be handled by LOOT, - // even if the file content is valid. + bool Plugin::IsValid(const std::string& filename, const Game& game) { + BOOST_LOG_TRIVIAL(trace) << "Checking to see if \"" << filename << "\" is a valid plugin."; + + //If the filename passed ends in '.ghost', that should be trimmed. + std::string name; + if (boost::iends_with(filename, ".ghost")) + name = filename.substr(0, filename.length() - 6); + else + name = filename; + + // Check that the file has a valid extension. if (!boost::iends_with(name, ".esm") && !boost::iends_with(name, ".esp")) return false; + // Add the ".ghost" file extension if the plugin is ghosted. boost::filesystem::path filepath = game.DataPath() / name; - - // In case the plugin is ghosted. if (!boost::filesystem::exists(filepath) && boost::filesystem::exists(filepath.string() + ".ghost")) filepath += ".ghost"; if (libespm::Plugin::isValid(filepath, game.LibespmId(), true)) return true; - BOOST_LOG_TRIVIAL(warning) << "The .es(p|m) file \"" << name << "\" is not a valid plugin."; + BOOST_LOG_TRIVIAL(warning) << "The .es(p|m) file \"" << filename << "\" is not a valid plugin."; return false; } diff --git a/src/backend/plugin/plugin.h b/src/backend/plugin/plugin.h index 10e46792..642f0af8 100644 --- a/src/backend/plugin/plugin.h +++ b/src/backend/plugin/plugin.h @@ -54,7 +54,6 @@ namespace loot { size_t NumOverrideFormIDs() const; bool LoadsBSA(const Game& game) const; - bool IsValid(const Game& game) const; bool IsActive(const Game& game) const; //Compare name strings. @@ -67,6 +66,7 @@ namespace loot { //Validity checks. bool CheckInstallValidity(const Game& game); //Checks that reqs and masters are all present, and that no incs are present. Returns true if the plugin is dirty. + static bool IsValid(const std::string& filename, const Game& game); private: bool _isEmpty; // Does the plugin contain any records other than the TES4 header? std::vector masters; diff --git a/src/tests/backend/plugin/test_plugin.h b/src/tests/backend/plugin/test_plugin.h index 4f060b69..8104c7fc 100644 --- a/src/tests/backend/plugin/test_plugin.h +++ b/src/tests/backend/plugin/test_plugin.h @@ -119,14 +119,9 @@ TEST_F(Plugin, IsValid) { game.SetGamePath(dataPath.parent_path()); ASSERT_NO_THROW(game.Init(false, localPath)); - loot::Plugin plugin("Blank.esm"); - EXPECT_TRUE(plugin.IsValid(game)); - - plugin = loot::Plugin("NotAPlugin.esm"); - EXPECT_FALSE(plugin.IsValid(game)); - - plugin = loot::Plugin("EmptyFile.esm"); - EXPECT_FALSE(plugin.IsValid(game)); + EXPECT_TRUE(loot::Plugin::IsValid("Blank.esm", game)); + EXPECT_FALSE(loot::Plugin::IsValid("NotAPlugin.esm", game)); + EXPECT_FALSE(loot::Plugin::IsValid("EmptyFile.esm", game)); } TEST_F(Plugin, IsActive) {