From 6342e51dea99d17958a0e684ff321d85618a3338 Mon Sep 17 00:00:00 2001 From: Oliver Hamlet Date: Sat, 12 Dec 2015 14:02:48 +0000 Subject: [PATCH] Optimise file and version conditions for plugins Check the plugin cache before falling back to filesystem reads. --- src/backend/metadata/condition_grammar.h | 43 ++++++++++++++++++------ 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/src/backend/metadata/condition_grammar.h b/src/backend/metadata/condition_grammar.h index f4532840..0f98ee29 100644 --- a/src/backend/metadata/condition_grammar.h +++ b/src/backend/metadata/condition_grammar.h @@ -158,10 +158,20 @@ namespace loot { if (_game == nullptr) return; - if (boost::iends_with(file, ".esp") || boost::iends_with(file, ".esm")) - result = boost::filesystem::exists(_game->DataPath() / file) || boost::filesystem::exists(_game->DataPath() / (file + ".ghost")); - else - result = boost::filesystem::exists(_game->DataPath() / file); + // Try first checking the plugin cache, as most file entries are + // for plugins. + try { + // GetPlugin throws if it can't find an entry. + _game->GetPlugin(file); + result = true; + } + catch (...) { + // Not a loaded plugin, check the filesystem. + if (boost::iends_with(file, ".esp") || boost::iends_with(file, ".esm")) + result = boost::filesystem::exists(_game->DataPath() / file) || boost::filesystem::exists(_game->DataPath() / (file + ".ghost")); + else + result = boost::filesystem::exists(_game->DataPath() / file); + } if (result) BOOST_LOG_TRIVIAL(trace) << "The file does exist."; @@ -230,7 +240,6 @@ namespace loot { //Now we have a valid parent path and a regex filename. Check that //the parent path exists and is a directory. - boost::filesystem::path parent_path = _game->DataPath() / pathRegex.first; if (!boost::filesystem::exists(parent_path) || !boost::filesystem::is_directory(parent_path)) { BOOST_LOG_TRIVIAL(trace) << "The path \"" << parent_path << "\" does not exist or is not a directory."; @@ -332,12 +341,26 @@ namespace loot { Version trueVersion; if (file == "LOOT") trueVersion = Version(boost::filesystem::absolute("LOOT.exe")); - else if (Plugin::IsValid(file, *_game)) { - Plugin plugin(*_game, file, true); - trueVersion = Version(plugin.getDescription()); + else { + // If the file is a plugin, its version needs to be extracted + // from its description field. Try getting an entry from the + // plugin cache. + try { + Plugin plugin = _game->GetPlugin(file); + trueVersion = Version(plugin.getDescription()); + } + catch (...) { + // The file wasn't in the plugin cache, load it as a plugin + // if it appears to be valid, otherwise treat it as a non + // plugin file. + if (Plugin::IsValid(file, *_game)) { + Plugin plugin(*_game, file, true); + trueVersion = Version(plugin.getDescription()); + } + else + trueVersion = Version(_game->DataPath() / file); + } } - else - trueVersion = Version(_game->DataPath() / file); BOOST_LOG_TRIVIAL(trace) << "Version extracted: " << trueVersion.AsString();