| 1 | // Copyright 2018 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "UICommon/ResourcePack/ResourcePack.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <memory> |
| 8 | |
| 9 | #include <mz.h> |
| 10 | #include <mz_os.h> |
| 11 | #include <mz_strm.h> |
| 12 | #include <mz_zip.h> |
| 13 | #include <mz_zip_rw.h> |
| 14 | |
| 15 | #include "Common/CommonPaths.h" |
| 16 | #include "Common/Contains.h" |
| 17 | #include "Common/FileSearch.h" |
| 18 | #include "Common/FileUtil.h" |
| 19 | #include "Common/IOFile.h" |
| 20 | #include "Common/MinizipUtil.h" |
| 21 | #include "Common/ScopeGuard.h" |
| 22 | #include "Common/StringUtil.h" |
| 23 | |
| 24 | #include "UICommon/ResourcePack/Manager.h" |
| 25 | #include "UICommon/ResourcePack/Manifest.h" |
| 26 | |
| 27 | namespace ResourcePack |
| 28 | { |
| 29 | constexpr char TEXTURE_PATH[] = HIRES_TEXTURES_DIR DIR_SEP; |
| 30 | |
| 31 | ResourcePack::ResourcePack(const std::string& path) : m_path(path) |
| 32 | { |
| 33 | void* zip_reader = mz_zip_reader_create(); |
| 34 | if (!zip_reader) |
| 35 | { |
| 36 | m_valid = false; |
| 37 | m_error = "Failed to create zip reader"; |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | Common::ScopeGuard file_guard{[&] { mz_zip_reader_delete(&zip_reader); }}; |
| 42 | |
| 43 | if (mz_zip_reader_open_file(zip_reader, path.c_str()) != MZ_OK) |
| 44 | { |
| 45 | m_valid = false; |
| 46 | m_error = "Failed to open resource pack"; |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | if (mz_zip_reader_locate_entry(zip_reader, "manifest.json", 0) != MZ_OK) |
| 51 | { |
| 52 | m_valid = false; |
| 53 | m_error = "Resource pack is missing a manifest."; |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | mz_zip_file* manifest_info; |
| 58 | if (mz_zip_reader_entry_get_info(zip_reader, &manifest_info) != MZ_OK) |
| 59 | { |
| 60 | m_valid = false; |
| 61 | m_error = "Failed to access manifest.json"; |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | std::string manifest_contents(manifest_info->uncompressed_size, '\0'); |
| 66 | if (!Common::ReadFileFromZip(zip_reader, &manifest_contents)) |
| 67 | { |
| 68 | m_valid = false; |
| 69 | m_error = "Failed to read manifest.json"; |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | m_manifest = std::make_shared<Manifest>(manifest_contents); |
| 74 | if (!m_manifest->IsValid()) |
| 75 | { |
| 76 | m_valid = false; |
| 77 | m_error = "Manifest error: " + m_manifest->GetError(); |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | if (mz_zip_reader_locate_entry(zip_reader, "logo.png", 0) == MZ_OK) |
| 82 | { |
| 83 | mz_zip_file* logo_info; |
| 84 | mz_zip_reader_entry_get_info(zip_reader, &logo_info); |
| 85 | |
| 86 | m_logo_data.resize(logo_info->uncompressed_size); |
| 87 | |
| 88 | if (!Common::ReadFileFromZip(zip_reader, &m_logo_data)) |
| 89 | { |
| 90 | m_valid = false; |
| 91 | m_error = "Failed to read logo.png"; |
| 92 | return; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | mz_zip_reader_goto_first_entry(zip_reader); |
| 97 | |
| 98 | do |
| 99 | { |
| 100 | mz_zip_file* texture_info; |
| 101 | mz_zip_reader_entry_get_info(zip_reader, &texture_info); |
| 102 | std::string filename(texture_info->filename); |
| 103 | |
| 104 | if (!filename.starts_with("textures/") || texture_info->uncompressed_size == 0) |
| 105 | continue; |
| 106 | |
| 107 | // If a texture is compressed and the manifest doesn't state that, abort. |
| 108 | if (!m_manifest->IsCompressed() && texture_info->compression_method != 0) |
| 109 | { |
| 110 | m_valid = false; |
| 111 | m_error = "Texture " + filename + " is compressed!"; |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | m_textures.push_back(filename.substr(9)); |
| 116 | } while (mz_zip_reader_goto_next_entry(zip_reader) != MZ_END_OF_LIST); |
| 117 | } |
| 118 | |
| 119 | bool ResourcePack::IsValid() const |
| 120 | { |
| 121 | return m_valid; |
| 122 | } |
| 123 | |
| 124 | const std::vector<char>& ResourcePack::GetLogo() const |
| 125 | { |
| 126 | return m_logo_data; |
| 127 | } |
| 128 | |
| 129 | const std::string& ResourcePack::GetPath() const |
| 130 | { |
| 131 | return m_path; |
| 132 | } |
| 133 | |
| 134 | const std::string& ResourcePack::GetError() const |
| 135 | { |
| 136 | return m_error; |
| 137 | } |
| 138 | |
| 139 | const Manifest* ResourcePack::GetManifest() const |
| 140 | { |
| 141 | return m_manifest.get(); |
| 142 | } |
| 143 | |
| 144 | const std::vector<std::string>& ResourcePack::GetTextures() const |
| 145 | { |
| 146 | return m_textures; |
| 147 | } |
| 148 | |
| 149 | bool ResourcePack::Install(const std::string& path) |
| 150 | { |
| 151 | if (!IsValid()) |
| 152 | { |
| 153 | m_error = "Invalid pack"; |
| 154 | return false; |
| 155 | } |
| 156 | |
| 157 | void* zip_reader = mz_zip_reader_create(); |
| 158 | if (!zip_reader) |
| 159 | { |
| 160 | m_valid = false; |
| 161 | m_error = "Failed to create zip reader"; |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | Common::ScopeGuard file_guard{[&] { mz_zip_reader_delete(&zip_reader); }}; |
| 166 | |
| 167 | if (mz_zip_reader_open_file(zip_reader, m_path.c_str()) != MZ_OK) |
| 168 | { |
| 169 | m_valid = false; |
| 170 | m_error = "Failed to open resource pack"; |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | if (mz_zip_reader_goto_first_entry(zip_reader) != MZ_OK) |
| 175 | return false; |
| 176 | |
| 177 | do |
| 178 | { |
| 179 | mz_zip_file* texture_info{}; |
| 180 | if (mz_zip_reader_entry_get_info(zip_reader, &texture_info) != MZ_OK) |
| 181 | { |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | const std::string texture_zip_path = texture_info->filename; |
| 186 | |
| 187 | const std::string texture_zip_path_prefix = "textures/"; |
| 188 | if (!texture_zip_path.starts_with(texture_zip_path_prefix)) |
| 189 | continue; |
| 190 | const std::string texture_name = texture_zip_path.substr(texture_zip_path_prefix.size()); |
| 191 | |
| 192 | auto texture_it = std::ranges::find_if(m_textures, [&texture_name](const std::string& texture) { |
| 193 | return mz_path_compare_wc(texture.c_str(), texture_name.c_str(), 1) == MZ_OK; |
| 194 | }); |
| 195 | if (texture_it == m_textures.cend()) |
| 196 | continue; |
| 197 | const auto texture = *texture_it; |
| 198 | |
| 199 | // Check if a higher priority pack already provides a given texture, don't overwrite it |
| 200 | bool provided_by_other_pack = false; |
| 201 | for (const auto& pack : GetHigherPriorityPacks(*this)) |
| 202 | { |
| 203 | if (Common::Contains(pack->GetTextures(), texture)) |
| 204 | { |
| 205 | provided_by_other_pack = true; |
| 206 | break; |
| 207 | } |
| 208 | } |
| 209 | if (provided_by_other_pack) |
| 210 | continue; |
| 211 | |
| 212 | const std::string texture_path = path + TEXTURE_PATH + texture; |
| 213 | std::string texture_full_dir; |
| 214 | if (!SplitPath(texture_path, &texture_full_dir, nullptr, nullptr)) |
| 215 | continue; |
| 216 | |
| 217 | if (!File::CreateFullPath(texture_full_dir)) |
| 218 | { |
| 219 | m_error = "Failed to create full path " + texture_full_dir; |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | const size_t data_size = static_cast<size_t>(texture_info->uncompressed_size); |
| 224 | auto data = std::make_unique<u8[]>(data_size); |
| 225 | if (!Common::ReadFileFromZip(zip_reader, data.get(), data_size)) |
| 226 | { |
| 227 | m_error = "Failed to read texture " + texture; |
| 228 | return false; |
| 229 | } |
| 230 | |
| 231 | File::IOFile out(texture_path, "wb"); |
| 232 | if (!out) |
| 233 | { |
| 234 | m_error = "Failed to open " + texture; |
| 235 | return false; |
| 236 | } |
| 237 | if (!out.WriteBytes(data.get(), data_size)) |
| 238 | { |
| 239 | m_error = "Failed to write " + texture; |
| 240 | return false; |
| 241 | } |
| 242 | |
| 243 | } while (mz_zip_reader_goto_next_entry(zip_reader) == MZ_OK); |
| 244 | |
| 245 | SetInstalled(*this, true); |
| 246 | return true; |
| 247 | } |
| 248 | |
| 249 | bool ResourcePack::Uninstall(const std::string& path) |
| 250 | { |
| 251 | if (!IsValid()) |
| 252 | { |
| 253 | m_error = "Invalid pack"; |
| 254 | return false; |
| 255 | } |
| 256 | |
| 257 | auto lower = GetLowerPriorityPacks(*this); |
| 258 | |
| 259 | SetInstalled(*this, false); |
| 260 | |
| 261 | for (const auto& texture : m_textures) |
| 262 | { |
| 263 | bool provided_by_other_pack = false; |
| 264 | |
| 265 | // Check if a higher priority pack already provides a given texture, don't delete it |
| 266 | for (const auto& pack : GetHigherPriorityPacks(*this)) |
| 267 | { |
| 268 | if (::ResourcePack::IsInstalled(*pack) && Common::Contains(pack->GetTextures(), texture)) |
| 269 | { |
| 270 | provided_by_other_pack = true; |
| 271 | break; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | if (provided_by_other_pack) |
| 276 | continue; |
| 277 | |
| 278 | // Check if a lower priority pack provides a given texture - if so, install it. |
| 279 | for (auto& pack : lower) |
| 280 | { |
| 281 | if (::ResourcePack::IsInstalled(*pack) && Common::Contains(pack->GetTextures(), texture)) |
| 282 | { |
| 283 | pack->Install(path); |
| 284 | |
| 285 | provided_by_other_pack = true; |
| 286 | break; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | if (provided_by_other_pack) |
| 291 | continue; |
| 292 | |
| 293 | const std::string texture_path = path + TEXTURE_PATH + texture; |
| 294 | if (File::Exists(texture_path) && !File::Delete(texture_path)) |
| 295 | { |
| 296 | m_error = "Failed to delete texture " + texture; |
| 297 | return false; |
| 298 | } |
| 299 | |
| 300 | // Recursively delete empty directories |
| 301 | |
| 302 | std::string dir; |
| 303 | SplitPath(texture_path, &dir, nullptr, nullptr); |
| 304 | |
| 305 | while (dir.length() > (path + TEXTURE_PATH).length()) |
| 306 | { |
| 307 | const auto is_empty = Common::DoFileSearch(dir).empty(); |
| 308 | |
| 309 | if (is_empty) |
| 310 | File::DeleteDir(dir); |
| 311 | |
| 312 | SplitPath(dir.substr(0, dir.size() - 2), &dir, nullptr, nullptr); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | return true; |
| 317 | } |
| 318 | |
| 319 | bool ResourcePack::operator==(const ResourcePack& pack) const |
| 320 | { |
| 321 | return pack.GetPath() == m_path; |
| 322 | } |
| 323 | |
| 324 | } // namespace ResourcePack |