From 12f075543caa7a0b0bac6810fd98ecac60446f18 Mon Sep 17 00:00:00 2001 From: Oliver Hamlet Date: Sat, 25 Mar 2017 10:52:19 +0000 Subject: [PATCH] Refactor Game::GetArchiveFileExtension into Plugin It's the only place it's used. --- src/api/game/game.cpp | 7 ---- src/api/game/game.h | 1 - src/api/plugin/plugin.cpp | 44 +++++++++++++------- src/api/plugin/plugin.h | 2 + src/tests/api/internals/game/game_test.h | 9 ---- src/tests/api/internals/plugin/plugin_test.h | 11 ++++- 6 files changed, 39 insertions(+), 35 deletions(-) diff --git a/src/api/game/game.cpp b/src/api/game/game.cpp index fa7ec488..c2355478 100644 --- a/src/api/game/game.cpp +++ b/src/api/game/game.cpp @@ -80,13 +80,6 @@ boost::filesystem::path Game::DataPath() const { return gamePath_ / "Data"; } -std::string Game::GetArchiveFileExtension() const { - if (type_ == GameType::fo4) - return ".ba2"; - else - return ".bsa"; -} - std::shared_ptr Game::GetCache() { return cache_; } diff --git a/src/api/game/game.h b/src/api/game/game.h index 727e2d0d..d6a86309 100644 --- a/src/api/game/game.h +++ b/src/api/game/game.h @@ -45,7 +45,6 @@ public: GameType Type() const; boost::filesystem::path DataPath() const; - std::string GetArchiveFileExtension() const; std::shared_ptr GetCache(); diff --git a/src/api/plugin/plugin.cpp b/src/api/plugin/plugin.cpp index 03fab59a..3964eea9 100644 --- a/src/api/plugin/plugin.cpp +++ b/src/api/plugin/plugin.cpp @@ -96,22 +96,7 @@ Plugin::Plugin(const Game& game, const std::string& name, const bool headerOnly) // Get whether the plugin is active or not. isActive_ = game.IsPluginActive(name_); - // Get whether the plugin loads an archive (BSA/BA2) or not. - const string archiveExtension = game.GetArchiveFileExtension(); - - if (game.Type() == GameType::tes5) { - // Skyrim plugins only load BSAs that exactly match their basename. - loadsArchive_ = boost::filesystem::exists(game.DataPath() / (name_.substr(0, name_.length() - 4) + archiveExtension)); - } else if (game.Type() != GameType::tes4 || boost::iends_with(name_, ".esp")) { - //Oblivion .esp files and FO3, FNV, FO4 plugins can load archives which begin with the plugin basename. - string basename = name_.substr(0, name_.length() - 4); - for (boost::filesystem::directory_iterator it(game.DataPath()); it != boost::filesystem::directory_iterator(); ++it) { - if (boost::iequals(it->path().extension().string(), archiveExtension) && boost::istarts_with(it->path().filename().string(), basename)) { - loadsArchive_ = true; - break; - } - } - } + loadsArchive_ = LoadsArchive(name_, game.Type(), game.DataPath()); } catch (std::exception& e) { BOOST_LOG_TRIVIAL(error) << "Cannot read plugin file \"" << name << "\". Details: " << e.what(); throw FileAccessError((boost::format("Cannot read \"%1%\". Details: %2%") % name % e.what()).str()); @@ -243,6 +228,33 @@ bool Plugin::IsActive() const { return isActive_; } +std::string Plugin::GetArchiveFileExtension(const GameType gameType) { + if (gameType == GameType::fo4) + return ".ba2"; + else + return ".bsa"; +} + +bool Plugin::LoadsArchive(const std::string& pluginName, const GameType gameType, const boost::filesystem::path& dataPath) { + // Get whether the plugin loads an archive (BSA/BA2) or not. + const string archiveExtension = GetArchiveFileExtension(gameType); + + if (gameType == GameType::tes5) { + // Skyrim plugins only load BSAs that exactly match their basename. + return boost::filesystem::exists(dataPath / (pluginName.substr(0, pluginName.length() - 4) + archiveExtension)); + } else if (gameType != GameType::tes4 || boost::iends_with(pluginName, ".esp")) { + //Oblivion .esp files and FO3, FNV, FO4 plugins can load archives which begin with the plugin basename. + string basename = pluginName.substr(0, pluginName.length() - 4); + for (boost::filesystem::directory_iterator it(dataPath); it != boost::filesystem::directory_iterator(); ++it) { + if (boost::iequals(it->path().extension().string(), archiveExtension) && boost::istarts_with(it->path().filename().string(), basename)) { + return true; + } + } + } + + return false; +} + libespm::GameId Plugin::GetLibespmGameId(GameType gameType) { if (gameType == GameType::tes4) return libespm::GameId::OBLIVION; diff --git a/src/api/plugin/plugin.h b/src/api/plugin/plugin.h index 0aeb7024..62841cd5 100644 --- a/src/api/plugin/plugin.h +++ b/src/api/plugin/plugin.h @@ -68,6 +68,8 @@ public: bool operator < (const Plugin& rhs) const; private: + static std::string GetArchiveFileExtension(const GameType gameType); + static bool LoadsArchive(const std::string& pluginName, const GameType gameType, const boost::filesystem::path& dataPath); static libespm::GameId GetLibespmGameId(GameType gameType); bool isEmpty_; // Does the plugin contain any records other than the TES4 header? diff --git a/src/tests/api/internals/game/game_test.h b/src/tests/api/internals/game/game_test.h index 5652c1ed..51f3c59a 100644 --- a/src/tests/api/internals/game/game_test.h +++ b/src/tests/api/internals/game/game_test.h @@ -90,15 +90,6 @@ TEST_P(GameTest, constructingShouldNotThrowIfGameAndLocalPathsAreNotEmpty) { EXPECT_NO_THROW(Game(GetParam(), dataPath.parent_path(), localPath)); } -TEST_P(GameTest, getArchiveFileExtensionShouldReturnDotBa2IfGameIdIsFallout4AndDotBsaOtherwise) { - Game game = Game(GetParam(), dataPath.parent_path(), localPath); - - if (game.Type() == GameType::fo4) - EXPECT_EQ(".ba2", game.GetArchiveFileExtension()); - else - EXPECT_EQ(".bsa", game.GetArchiveFileExtension()); -} - TEST_P(GameTest, loadPluginsWithHeadersOnlyTrueShouldLoadTheHeadersOfAllInstalledPlugins) { Game game = Game(GetParam(), dataPath.parent_path(), localPath); diff --git a/src/tests/api/internals/plugin/plugin_test.h b/src/tests/api/internals/plugin/plugin_test.h index cc0c4349..526149f6 100644 --- a/src/tests/api/internals/plugin/plugin_test.h +++ b/src/tests/api/internals/plugin/plugin_test.h @@ -39,8 +39,8 @@ protected: nonPluginFile("NotAPlugin.esm"), lowercaseBlankEsp("blank.esp"), game_(GetParam(), dataPath.parent_path(), localPath), - blankArchive("Blank" + game_.GetArchiveFileExtension()), - blankSuffixArchive("Blank - Different - suffix" + game_.GetArchiveFileExtension()) {} + blankArchive("Blank" + GetArchiveFileExtension(game_.Type())), + blankSuffixArchive("Blank - Different - suffix" + GetArchiveFileExtension(game_.Type())) {} void SetUp() { CommonGameTestFixture::SetUp(); @@ -86,6 +86,13 @@ protected: const std::string lowercaseBlankEsp; const std::string blankArchive; const std::string blankSuffixArchive; +private: + static std::string GetArchiveFileExtension(const GameType gameType) { + if (gameType == GameType::fo4) + return ".ba2"; + else + return ".bsa"; + } }; class OtherPluginType : public PluginInterface {