Remove game CRC cache

Non-plugin CRCs that were stored in the game's cache don't need to
be, as they are only used in conditions, which are cached
separately.

Plugin objects are now used to cache their CRC values along with
almost all plugin data.

This does remove the ability to cache plugin CRCs calculated when
evaluating conditions, but I think they were never used.
This commit is contained in:
Oliver Hamlet
2015-12-07 13:57:03 +00:00
parent 835d2ba463
commit 29ddff40c3
6 changed files with 17 additions and 47 deletions
-19
View File
@@ -42,22 +42,15 @@ namespace loot {
GameCache::GameCache() {}
GameCache::GameCache(const GameCache& cache)
: conditionCache(cache.conditionCache),
crcCache(cache.crcCache),
activePlugins(cache.activePlugins) {}
GameCache& GameCache::operator=(const GameCache& cache) {
conditionCache = cache.conditionCache;
crcCache = cache.crcCache;
activePlugins = cache.activePlugins;
return *this;
}
void GameCache::CacheCrc(const std::string& plugin, uint32_t crc) {
std::lock_guard<std::mutex> guard(mutex);
crcCache.insert(pair<string, uint32_t>(boost::locale::to_lower(plugin), crc));
}
void GameCache::CacheCondition(const std::string& condition, bool result) {
std::lock_guard<std::mutex> guard(mutex);
conditionCache.insert(pair<string, bool>(boost::locale::to_lower(condition), result));
@@ -68,17 +61,6 @@ namespace loot {
activePlugins = plugins;
}
uint32_t GameCache::GetCachedCrc(const std::string& plugin) const {
std::lock_guard<std::mutex> guard(mutex);
auto it = crcCache.find(boost::locale::to_lower(plugin));
if (it != crcCache.end())
return it->second;
else
return 0;
}
std::pair<bool, bool> GameCache::GetCachedCondition(const std::string& condition) const {
std::lock_guard<std::mutex> guard(mutex);
@@ -100,7 +82,6 @@ namespace loot {
std::lock_guard<std::mutex> guard(mutex);
conditionCache.clear();
crcCache.clear();
activePlugins.clear();
}
}
-4
View File
@@ -39,12 +39,9 @@ namespace loot {
GameCache& operator=(const GameCache& cache);
void CacheCrc(const std::string& plugin, uint32_t crc);
void CacheCondition(const std::string& condition, bool result);
void CacheActivePlugins(const std::unordered_set<std::string>& plugins);
// Returns 0 if no cached CRC.
uint32_t GetCachedCrc(const std::string& plugin) const;
// Returns false for second bool if no cached condition.
std::pair<bool, bool> GetCachedCondition(const std::string& condition) const;
bool IsPluginActive(const std::string& plugin) const;
@@ -53,7 +50,6 @@ namespace loot {
private:
//Caches for condition results, CRCs and active plugins.
std::unordered_map<std::string, bool> conditionCache;
std::unordered_map<std::string, uint32_t> crcCache;
std::unordered_set<std::string> activePlugins;
mutable std::mutex mutex;
+15 -10
View File
@@ -287,21 +287,26 @@ namespace loot {
if (_game == nullptr)
return;
uint32_t crc = _game->GetCachedCrc(file);
uint32_t crc = 0;
if (file == "LOOT")
crc = GetCrc32(boost::filesystem::absolute("LOOT.exe"));
else {
// CRC could be for a plugin or a file.
// Get the CRC from the game plugin cache if possible.
auto pluginPairIt = _game->plugins.find(boost::locale::to_lower(file));
if (pluginPairIt != _game->plugins.end())
crc = pluginPairIt->second.Crc();
if (crc == 0) {
if (file == "LOOT")
crc = GetCrc32(boost::filesystem::absolute("LOOT.exe"));
if (boost::filesystem::exists(_game->DataPath() / file))
crc = GetCrc32(_game->DataPath() / file);
else if ((boost::iends_with(file, ".esp") || boost::iends_with(file, ".esm")) && boost::filesystem::exists(_game->DataPath() / (file + ".ghost")))
crc = GetCrc32(_game->DataPath() / (file + ".ghost"));
if (crc == 0) {
if (boost::filesystem::exists(_game->DataPath() / file))
crc = GetCrc32(_game->DataPath() / file);
else if ((boost::iends_with(file, ".esp") || boost::iends_with(file, ".esm")) && boost::filesystem::exists(_game->DataPath() / (file + ".ghost")))
crc = GetCrc32(_game->DataPath() / (file + ".ghost"));
}
else {
result = false;
return;
}
_game->CacheCrc(file, crc);
}
result = checksum == crc;
+1 -2
View File
@@ -46,7 +46,7 @@ namespace loot {
crc(0),
numOverrideRecords(0) {}
Plugin::Plugin(Game& game, const std::string& name, const bool headerOnly) :
Plugin::Plugin(const Game& game, const std::string& name, const bool headerOnly) :
PluginMetadata(name),
libespm::Plugin(game.LibespmId()),
_isEmpty(true),
@@ -67,7 +67,6 @@ namespace loot {
if (!headerOnly) {
BOOST_LOG_TRIVIAL(trace) << Name() << ": Caching CRC value.";
crc = GetCrc32(filepath);
game.CacheCrc(Name(), crc);
}
BOOST_LOG_TRIVIAL(trace) << Name() << ": Counting override FormIDs.";
+1 -1
View File
@@ -42,7 +42,7 @@ namespace loot {
class Plugin : public PluginMetadata, private libespm::Plugin {
public:
Plugin(const std::string& name);
Plugin(Game& game, const std::string& name, const bool headerOnly);
Plugin(const Game& game, const std::string& name, const bool headerOnly);
using libespm::Plugin::getDescription;
using libespm::Plugin::getFormIds;
-11
View File
@@ -35,12 +35,10 @@ TEST_F(GameCache, Constructors) {
loot::GameCache cache;
std::unordered_set<std::string> plugins({"skyrim.esm"});
EXPECT_NO_THROW(cache.CacheCrc("Blank.esp", 5));
EXPECT_NO_THROW(cache.CacheCondition("True Condition", true));
EXPECT_NO_THROW(cache.CacheActivePlugins(plugins));
loot::GameCache cache2(cache);
EXPECT_EQ(5, cache2.GetCachedCrc("blank.Esp"));
EXPECT_EQ(std::make_pair(true, true), cache2.GetCachedCondition("true Condition"));
EXPECT_TRUE(cache2.IsPluginActive("Skyrim.esm"));
}
@@ -49,23 +47,14 @@ TEST_F(GameCache, AssignmentOperator) {
loot::GameCache cache;
std::unordered_set<std::string> plugins({"skyrim.esm"});
EXPECT_NO_THROW(cache.CacheCrc("Blank.esp", 5));
EXPECT_NO_THROW(cache.CacheCondition("True Condition", true));
EXPECT_NO_THROW(cache.CacheActivePlugins(plugins));
loot::GameCache cache2 = cache;
EXPECT_EQ(5, cache2.GetCachedCrc("blank.Esp"));
EXPECT_EQ(std::make_pair(true, true), cache2.GetCachedCondition("true Condition"));
EXPECT_TRUE(cache2.IsPluginActive("Skyrim.esm"));
}
TEST_F(GameCache, CacheCrc) {
loot::GameCache cache;
EXPECT_NO_THROW(cache.CacheCrc("Blank.esp", 5));
EXPECT_EQ(5, cache.GetCachedCrc("blank.Esp"));
EXPECT_EQ(0, cache.GetCachedCrc("Blank.missing.esp"));
}
TEST_F(GameCache, CacheCondition) {
loot::GameCache cache;
EXPECT_NO_THROW(cache.CacheCondition("True Condition", true));