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