| 1 | // Copyright 2018 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "UICommon/ResourcePack/Manager.h" |
| 5 | |
| 6 | #include "Common/CommonTypes.h" |
| 7 | #include "Common/FileSearch.h" |
| 8 | #include "Common/FileUtil.h" |
| 9 | #include "Common/IniFile.h" |
| 10 | |
| 11 | #include <algorithm> |
| 12 | |
| 13 | namespace ResourcePack |
| 14 | { |
| 15 | namespace |
| 16 | { |
| 17 | std::vector<ResourcePack> packs; |
| 18 | std::string packs_path; |
| 19 | |
| 20 | Common::IniFile GetPackConfig() |
| 21 | { |
| 22 | packs_path = File::GetUserPath(D_RESOURCEPACK_IDX) + "/Packs.ini"; |
| 23 | |
| 24 | Common::IniFile file; |
| 25 | file.Load(packs_path); |
| 26 | |
| 27 | return file; |
| 28 | } |
| 29 | } // Anonymous namespace |
| 30 | |
| 31 | bool Init() |
| 32 | { |
| 33 | packs.clear(); |
| 34 | const std::vector<std::string> pack_list = |
| 35 | Common::DoFileSearch(File::GetUserPath(D_RESOURCEPACK_IDX), ".zip"); |
| 36 | |
| 37 | Common::IniFile file = GetPackConfig(); |
| 38 | |
| 39 | auto* order = file.GetOrCreateSection("Order"); |
| 40 | |
| 41 | struct OrderHelper |
| 42 | { |
| 43 | size_t pack_list_index; |
| 44 | std::string manifest_id; |
| 45 | }; |
| 46 | |
| 47 | std::vector<OrderHelper> pack_list_order; |
| 48 | pack_list_order.reserve(pack_list.size()); |
| 49 | for (size_t i = 0; i < pack_list.size(); ++i) |
| 50 | { |
| 51 | const ResourcePack pack(pack_list[i]); |
| 52 | std::string manifest_id = pack.IsValid() ? pack.GetManifest()->GetID() : pack_list[i]; |
| 53 | pack_list_order.emplace_back(OrderHelper{i, std::move(manifest_id)}); |
| 54 | } |
| 55 | |
| 56 | std::ranges::sort(pack_list_order, {}, &OrderHelper::manifest_id); |
| 57 | |
| 58 | bool error = false; |
| 59 | for (size_t i = 0; i < pack_list_order.size(); ++i) |
| 60 | { |
| 61 | const auto& path = pack_list[pack_list_order[i].pack_list_index]; |
| 62 | |
| 63 | const ResourcePack* const pack = Add(path); |
| 64 | if (pack == nullptr) |
| 65 | { |
| 66 | error = true; |
| 67 | continue; |
| 68 | } |
| 69 | |
| 70 | order->Set(pack->GetManifest()->GetID(), static_cast<u64>(i)); |
| 71 | } |
| 72 | |
| 73 | file.Save(packs_path); |
| 74 | |
| 75 | return !error; |
| 76 | } |
| 77 | |
| 78 | std::vector<ResourcePack>& GetPacks() |
| 79 | { |
| 80 | return packs; |
| 81 | } |
| 82 | |
| 83 | std::vector<ResourcePack*> GetLowerPriorityPacks(const ResourcePack& pack) |
| 84 | { |
| 85 | std::vector<ResourcePack*> list; |
| 86 | for (auto it = std::ranges::find(packs, pack) + 1; it != packs.end(); ++it) |
| 87 | { |
| 88 | auto& entry = *it; |
| 89 | if (!IsInstalled(pack)) |
| 90 | continue; |
| 91 | |
| 92 | list.push_back(&entry); |
| 93 | } |
| 94 | |
| 95 | return list; |
| 96 | } |
| 97 | |
| 98 | std::vector<ResourcePack*> GetHigherPriorityPacks(const ResourcePack& pack) |
| 99 | { |
| 100 | std::vector<ResourcePack*> list; |
| 101 | auto end = std::ranges::find(packs, pack); |
| 102 | |
| 103 | for (auto it = packs.begin(); it != end; ++it) |
| 104 | { |
| 105 | auto& entry = *it; |
| 106 | if (!IsInstalled(entry)) |
| 107 | continue; |
| 108 | list.push_back(&entry); |
| 109 | } |
| 110 | |
| 111 | return list; |
| 112 | } |
| 113 | |
| 114 | ResourcePack* Add(const std::string& path, int offset) |
| 115 | { |
| 116 | if (offset == -1) |
| 117 | offset = static_cast<int>(packs.size()); |
| 118 | |
| 119 | ResourcePack pack(path); |
| 120 | |
| 121 | if (!pack.IsValid()) |
| 122 | return nullptr; |
| 123 | |
| 124 | Common::IniFile file = GetPackConfig(); |
| 125 | |
| 126 | auto* order = file.GetOrCreateSection("Order"); |
| 127 | |
| 128 | order->Set(pack.GetManifest()->GetID(), offset); |
| 129 | |
| 130 | for (int i = offset; i < static_cast<int>(packs.size()); i++) |
| 131 | order->Set(packs[i].GetManifest()->GetID(), i + 1); |
| 132 | |
| 133 | file.Save(packs_path); |
| 134 | |
| 135 | auto it = packs.insert(packs.begin() + offset, std::move(pack)); |
| 136 | return &*it; |
| 137 | } |
| 138 | |
| 139 | bool Remove(ResourcePack& pack) |
| 140 | { |
| 141 | const auto result = pack.Uninstall(File::GetUserPath(D_USER_IDX)); |
| 142 | |
| 143 | if (!result) |
| 144 | return false; |
| 145 | |
| 146 | auto pack_iterator = std::ranges::find(packs, pack); |
| 147 | |
| 148 | if (pack_iterator == packs.end()) |
| 149 | return false; |
| 150 | |
| 151 | Common::IniFile file = GetPackConfig(); |
| 152 | |
| 153 | auto* order = file.GetOrCreateSection("Order"); |
| 154 | |
| 155 | order->Delete(pack.GetManifest()->GetID()); |
| 156 | |
| 157 | int offset = pack_iterator - packs.begin(); |
| 158 | |
| 159 | for (int i = offset + 1; i < static_cast<int>(packs.size()); i++) |
| 160 | order->Set(packs[i].GetManifest()->GetID(), i - 1); |
| 161 | |
| 162 | file.Save(packs_path); |
| 163 | |
| 164 | packs.erase(pack_iterator); |
| 165 | |
| 166 | return true; |
| 167 | } |
| 168 | |
| 169 | void SetInstalled(const ResourcePack& pack, bool installed) |
| 170 | { |
| 171 | Common::IniFile file = GetPackConfig(); |
| 172 | |
| 173 | auto* install = file.GetOrCreateSection("Installed"); |
| 174 | |
| 175 | if (installed) |
| 176 | install->Set(pack.GetManifest()->GetID(), installed); |
| 177 | else |
| 178 | install->Delete(pack.GetManifest()->GetID()); |
| 179 | |
| 180 | file.Save(packs_path); |
| 181 | } |
| 182 | |
| 183 | bool IsInstalled(const ResourcePack& pack) |
| 184 | { |
| 185 | Common::IniFile file = GetPackConfig(); |
| 186 | |
| 187 | auto* install = file.GetOrCreateSection("Installed"); |
| 188 | |
| 189 | bool installed; |
| 190 | |
| 191 | install->Get(pack.GetManifest()->GetID(), &installed, false); |
| 192 | |
| 193 | return installed; |
| 194 | } |
| 195 | |
| 196 | } // namespace ResourcePack |