Refactor Game::GetArchiveFileExtension into Plugin

It's the only place it's used.
This commit is contained in:
Oliver Hamlet
2017-03-25 11:29:40 +00:00
parent be80f5a244
commit 12f075543c
6 changed files with 39 additions and 35 deletions
-7
View File
@@ -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<GameCache> Game::GetCache() {
return cache_;
}
-1
View File
@@ -45,7 +45,6 @@ public:
GameType Type() const;
boost::filesystem::path DataPath() const;
std::string GetArchiveFileExtension() const;
std::shared_ptr<GameCache> GetCache();
+28 -16
View File
@@ -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;
+2
View File
@@ -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?
-9
View File
@@ -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);
+9 -2
View File
@@ -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 {