GameFileCache.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // Copyright 2017 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "UICommon/GameFileCache.h"
  4. #include <algorithm>
  5. #include <cstddef>
  6. #include <functional>
  7. #include <list>
  8. #include <memory>
  9. #include <mutex>
  10. #include <string>
  11. #include <unordered_set>
  12. #include <utility>
  13. #include <vector>
  14. #include "Common/ChunkFile.h"
  15. #include "Common/CommonTypes.h"
  16. #include "Common/FileSearch.h"
  17. #include "Common/FileUtil.h"
  18. #include "Common/IOFile.h"
  19. #include "DiscIO/DirectoryBlob.h"
  20. #include "UICommon/GameFile.h"
  21. namespace UICommon
  22. {
  23. static constexpr u32 CACHE_REVISION = 26; // Last changed in PR 10084
  24. std::vector<std::string> FindAllGamePaths(const std::vector<std::string>& directories_to_scan,
  25. bool recursive_scan)
  26. {
  27. static const std::vector<std::string> search_extensions = {
  28. ".gcm", ".tgc", ".bin", ".iso", ".ciso", ".gcz", ".wbfs",
  29. ".wia", ".rvz", ".nfs", ".wad", ".dol", ".elf", ".json"};
  30. // TODO: We could process paths iteratively as they are found
  31. return Common::DoFileSearch(directories_to_scan, search_extensions, recursive_scan);
  32. }
  33. GameFileCache::GameFileCache() : m_path(File::GetUserPath(D_CACHE_IDX) + "gamelist.cache")
  34. {
  35. }
  36. void GameFileCache::ForEach(const ForEachFn& f) const
  37. {
  38. for (const std::shared_ptr<GameFile>& item : m_cached_files)
  39. f(item);
  40. }
  41. size_t GameFileCache::GetSize() const
  42. {
  43. return m_cached_files.size();
  44. }
  45. void GameFileCache::Clear(DeleteOnDisk delete_on_disk)
  46. {
  47. if (delete_on_disk != DeleteOnDisk::No)
  48. File::Delete(m_path);
  49. m_cached_files.clear();
  50. }
  51. std::shared_ptr<const GameFile> GameFileCache::AddOrGet(const std::string& path,
  52. bool* cache_changed)
  53. {
  54. auto it = std::ranges::find(m_cached_files, path, &GameFile::GetFilePath);
  55. const bool found = it != m_cached_files.cend();
  56. if (!found)
  57. {
  58. std::shared_ptr<UICommon::GameFile> game = std::make_shared<GameFile>(path);
  59. if (!game->IsValid())
  60. return nullptr;
  61. m_cached_files.emplace_back(std::move(game));
  62. }
  63. std::shared_ptr<GameFile>& result = found ? *it : m_cached_files.back();
  64. if (UpdateAdditionalMetadata(&result) || !found)
  65. *cache_changed = true;
  66. return result;
  67. }
  68. bool GameFileCache::Update(std::span<const std::string> all_game_paths,
  69. const GameAddedToCacheFn& game_added_to_cache,
  70. const GameRemovedFromCacheFn& game_removed_from_cache,
  71. const std::atomic_bool& processing_halted)
  72. {
  73. // Copy game paths into a set, except ones that match DiscIO::ShouldHideFromGameList.
  74. // TODO: Prevent DoFileSearch from looking inside /files/ directories of DirectoryBlobs at all?
  75. // TODO: Make DoFileSearch support filter predicates so we don't have remove things afterwards?
  76. std::unordered_set<std::string> game_paths;
  77. game_paths.reserve(all_game_paths.size());
  78. for (const std::string& path : all_game_paths)
  79. {
  80. if (!DiscIO::ShouldHideFromGameList(path))
  81. game_paths.insert(path);
  82. }
  83. bool cache_changed = false;
  84. // Delete paths that aren't in game_paths from m_cached_files,
  85. // while simultaneously deleting paths that are in m_cached_files from game_paths.
  86. // For the sake of speed, we don't care about maintaining the order of m_cached_files.
  87. {
  88. auto it = m_cached_files.begin();
  89. auto end = m_cached_files.end();
  90. while (it != end)
  91. {
  92. if (processing_halted)
  93. break;
  94. if (game_paths.erase((*it)->GetFilePath()))
  95. {
  96. ++it;
  97. }
  98. else
  99. {
  100. if (game_removed_from_cache)
  101. game_removed_from_cache((*it)->GetFilePath());
  102. cache_changed = true;
  103. --end;
  104. *it = std::move(*end);
  105. }
  106. }
  107. m_cached_files.erase(it, m_cached_files.end());
  108. }
  109. // Now that the previous loop has run, game_paths only contains paths that
  110. // aren't in m_cached_files, so we simply add all of them to m_cached_files.
  111. for (const std::string& path : game_paths)
  112. {
  113. if (processing_halted)
  114. break;
  115. auto file = std::make_shared<GameFile>(path);
  116. if (file->IsValid())
  117. {
  118. if (game_added_to_cache)
  119. game_added_to_cache(file);
  120. cache_changed = true;
  121. m_cached_files.push_back(std::move(file));
  122. }
  123. }
  124. return cache_changed;
  125. }
  126. bool GameFileCache::UpdateAdditionalMetadata(const GameUpdatedFn& game_updated,
  127. const std::atomic_bool& processing_halted)
  128. {
  129. bool cache_changed = false;
  130. for (std::shared_ptr<GameFile>& file : m_cached_files)
  131. {
  132. if (processing_halted)
  133. break;
  134. const bool updated = UpdateAdditionalMetadata(&file);
  135. cache_changed |= updated;
  136. if (game_updated && updated)
  137. game_updated(file);
  138. }
  139. return cache_changed;
  140. }
  141. bool GameFileCache::UpdateAdditionalMetadata(std::shared_ptr<GameFile>* game_file)
  142. {
  143. const bool xml_metadata_changed = (*game_file)->XMLMetadataChanged();
  144. const bool wii_banner_changed = (*game_file)->WiiBannerChanged();
  145. const bool custom_banner_changed = (*game_file)->CustomBannerChanged();
  146. (*game_file)->DownloadDefaultCover();
  147. const bool default_cover_changed = (*game_file)->DefaultCoverChanged();
  148. const bool custom_cover_changed = (*game_file)->CustomCoverChanged();
  149. if (!xml_metadata_changed && !wii_banner_changed && !custom_banner_changed &&
  150. !default_cover_changed && !custom_cover_changed)
  151. {
  152. return false;
  153. }
  154. // If a cached file needs an update, apply the updates to a copy and delete the original.
  155. // This makes the usage of cached files in other threads safe.
  156. std::shared_ptr<GameFile> copy = std::make_shared<GameFile>(**game_file);
  157. if (xml_metadata_changed)
  158. copy->XMLMetadataCommit();
  159. if (wii_banner_changed)
  160. copy->WiiBannerCommit();
  161. if (custom_banner_changed)
  162. copy->CustomBannerCommit();
  163. if (default_cover_changed)
  164. copy->DefaultCoverCommit();
  165. if (custom_cover_changed)
  166. copy->CustomCoverCommit();
  167. *game_file = std::move(copy);
  168. return true;
  169. }
  170. bool GameFileCache::Load()
  171. {
  172. return SyncCacheFile(false);
  173. }
  174. bool GameFileCache::Save()
  175. {
  176. return SyncCacheFile(true);
  177. }
  178. bool GameFileCache::SyncCacheFile(bool save)
  179. {
  180. const char* open_mode = save ? "wb" : "rb";
  181. File::IOFile f(m_path, open_mode);
  182. if (!f)
  183. return false;
  184. bool success = false;
  185. if (save)
  186. {
  187. // Measure the size of the buffer.
  188. u8* ptr = nullptr;
  189. PointerWrap p_measure(&ptr, 0, PointerWrap::Mode::Measure);
  190. DoState(&p_measure);
  191. const size_t buffer_size = reinterpret_cast<size_t>(ptr);
  192. // Then actually do the write.
  193. std::vector<u8> buffer(buffer_size);
  194. ptr = buffer.data();
  195. PointerWrap p(&ptr, buffer_size, PointerWrap::Mode::Write);
  196. DoState(&p, buffer_size);
  197. if (f.WriteBytes(buffer.data(), buffer.size()))
  198. success = true;
  199. }
  200. else
  201. {
  202. std::vector<u8> buffer(f.GetSize());
  203. if (!buffer.empty() && f.ReadBytes(buffer.data(), buffer.size()))
  204. {
  205. u8* ptr = buffer.data();
  206. PointerWrap p(&ptr, buffer.size(), PointerWrap::Mode::Read);
  207. DoState(&p, buffer.size());
  208. if (p.IsReadMode())
  209. success = true;
  210. }
  211. }
  212. if (!success)
  213. {
  214. // If some file operation failed, try to delete the probably-corrupted cache
  215. f.Close();
  216. File::Delete(m_path);
  217. }
  218. return success;
  219. }
  220. void GameFileCache::DoState(PointerWrap* p, u64 size)
  221. {
  222. struct
  223. {
  224. u32 revision;
  225. u64 expected_size;
  226. } header = {CACHE_REVISION, size};
  227. p->Do(header);
  228. if (p->IsReadMode())
  229. {
  230. if (header.revision != CACHE_REVISION || header.expected_size != size)
  231. {
  232. p->SetMeasureMode();
  233. return;
  234. }
  235. }
  236. p->DoEachElement(m_cached_files, [](PointerWrap& state, std::shared_ptr<GameFile>& elem) {
  237. if (state.IsReadMode())
  238. elem = std::make_shared<GameFile>();
  239. elem->DoState(state);
  240. });
  241. }
  242. } // namespace UICommon