summaryrefslogtreecommitdiff
path: root/Source/Core/DolphinQt2/GameList/GameFileCache.cpp
blob: 40e1928649f46b417ae9073cb72d5ef22d72593a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright 2017 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include "GameFileCache.h"

#include <QDataStream>
#include <QDir>
#include <QFileInfo>

#include "Common/FileUtil.h"
#include "Core/ConfigManager.h"
#include "DolphinQt2/Settings.h"

static const int CACHE_VERSION = 1;  // Last changed in PR #5927
static const int DATASTREAM_VERSION = QDataStream::Qt_5_0;

GameFileCache::GameFileCache()
    : m_path(QString::fromStdString(File::GetUserPath(D_CACHE_IDX) + "qt_gamefile.cache"))
{
}

bool GameFileCache::IsCached(const QString& path)
{
  std::lock_guard<std::mutex> guard(m_mutex);

  return m_gamefiles.contains(path);
}

GameFile GameFileCache::GetFile(const QString& path)
{
  std::lock_guard<std::mutex> guard(m_mutex);

  return m_gamefiles[path];
}

void GameFileCache::Load()
{
  std::lock_guard<std::mutex> guard(m_mutex);

  QFile file(m_path);

  if (!file.open(QIODevice::ReadOnly))
    return;

  QDataStream stream(&file);
  stream.setVersion(DATASTREAM_VERSION);

  qint32 cache_version;
  stream >> cache_version;

  // If the cache file is using an older version, ignore it and create it from scratch
  if (cache_version != CACHE_VERSION)
    return;

  stream >> m_gamefiles;
}

void GameFileCache::Save()
{
  std::lock_guard<std::mutex> guard(m_mutex);

  QFile file(m_path);

  if (!file.open(QIODevice::WriteOnly))
    return;

  QDataStream stream(&file);
  stream.setVersion(DATASTREAM_VERSION);

  stream << static_cast<qint32>(CACHE_VERSION);
  stream << m_gamefiles;
}

void GameFileCache::Update(const GameFile& gamefile)
{
  std::lock_guard<std::mutex> guard(m_mutex);

  m_gamefiles[gamefile.GetFilePath()] = gamefile;
}

QList<QString> GameFileCache::GetCached()
{
  std::lock_guard<std::mutex> guard(m_mutex);

  return m_gamefiles.keys();
}