summaryrefslogtreecommitdiff
path: root/Source/Core/UICommon/ResourcePack/Manager.cpp
blob: f3abf44ebc73b472554fda10d3f3e33cafe02551 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright 2018 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "UICommon/ResourcePack/Manager.h"

#include "Common/CommonTypes.h"
#include "Common/FileSearch.h"
#include "Common/FileUtil.h"
#include "Common/IniFile.h"

#include <algorithm>

namespace ResourcePack
{
namespace
{
std::vector<ResourcePack> packs;
std::string packs_path;

Common::IniFile GetPackConfig()
{
  packs_path = File::GetUserPath(D_RESOURCEPACK_IDX) + "/Packs.ini";

  Common::IniFile file;
  file.Load(packs_path);

  return file;
}
}  // Anonymous namespace

bool Init()
{
  packs.clear();
  const std::vector<std::string> pack_list =
      Common::DoFileSearch(File::GetUserPath(D_RESOURCEPACK_IDX), ".zip");

  Common::IniFile file = GetPackConfig();

  auto* order = file.GetOrCreateSection("Order");

  struct OrderHelper
  {
    size_t pack_list_index;
    std::string manifest_id;
  };

  std::vector<OrderHelper> pack_list_order;
  pack_list_order.reserve(pack_list.size());
  for (size_t i = 0; i < pack_list.size(); ++i)
  {
    const ResourcePack pack(pack_list[i]);
    std::string manifest_id = pack.IsValid() ? pack.GetManifest()->GetID() : pack_list[i];
    pack_list_order.emplace_back(OrderHelper{i, std::move(manifest_id)});
  }

  std::ranges::sort(pack_list_order, {}, &OrderHelper::manifest_id);

  bool error = false;
  for (size_t i = 0; i < pack_list_order.size(); ++i)
  {
    const auto& path = pack_list[pack_list_order[i].pack_list_index];

    const ResourcePack* const pack = Add(path);
    if (pack == nullptr)
    {
      error = true;
      continue;
    }

    order->Set(pack->GetManifest()->GetID(), static_cast<u64>(i));
  }

  file.Save(packs_path);

  return !error;
}

std::vector<ResourcePack>& GetPacks()
{
  return packs;
}

std::vector<ResourcePack*> GetLowerPriorityPacks(const ResourcePack& pack)
{
  std::vector<ResourcePack*> list;
  for (auto it = std::ranges::find(packs, pack) + 1; it != packs.end(); ++it)
  {
    auto& entry = *it;
    if (!IsInstalled(pack))
      continue;

    list.push_back(&entry);
  }

  return list;
}

std::vector<ResourcePack*> GetHigherPriorityPacks(const ResourcePack& pack)
{
  std::vector<ResourcePack*> list;
  auto end = std::ranges::find(packs, pack);

  for (auto it = packs.begin(); it != end; ++it)
  {
    auto& entry = *it;
    if (!IsInstalled(entry))
      continue;
    list.push_back(&entry);
  }

  return list;
}

ResourcePack* Add(const std::string& path, int offset)
{
  if (offset == -1)
    offset = static_cast<int>(packs.size());

  ResourcePack pack(path);

  if (!pack.IsValid())
    return nullptr;

  Common::IniFile file = GetPackConfig();

  auto* order = file.GetOrCreateSection("Order");

  order->Set(pack.GetManifest()->GetID(), offset);

  for (int i = offset; i < static_cast<int>(packs.size()); i++)
    order->Set(packs[i].GetManifest()->GetID(), i + 1);

  file.Save(packs_path);

  auto it = packs.insert(packs.begin() + offset, std::move(pack));
  return &*it;
}

bool Remove(ResourcePack& pack)
{
  const auto result = pack.Uninstall(File::GetUserPath(D_USER_IDX));

  if (!result)
    return false;

  auto pack_iterator = std::ranges::find(packs, pack);

  if (pack_iterator == packs.end())
    return false;

  Common::IniFile file = GetPackConfig();

  auto* order = file.GetOrCreateSection("Order");

  order->Delete(pack.GetManifest()->GetID());

  int offset = pack_iterator - packs.begin();

  for (int i = offset + 1; i < static_cast<int>(packs.size()); i++)
    order->Set(packs[i].GetManifest()->GetID(), i - 1);

  file.Save(packs_path);

  packs.erase(pack_iterator);

  return true;
}

void SetInstalled(const ResourcePack& pack, bool installed)
{
  Common::IniFile file = GetPackConfig();

  auto* install = file.GetOrCreateSection("Installed");

  if (installed)
    install->Set(pack.GetManifest()->GetID(), installed);
  else
    install->Delete(pack.GetManifest()->GetID());

  file.Save(packs_path);
}

bool IsInstalled(const ResourcePack& pack)
{
  Common::IniFile file = GetPackConfig();

  auto* install = file.GetOrCreateSection("Installed");

  bool installed;

  install->Get(pack.GetManifest()->GetID(), &installed, false);

  return installed;
}

}  // namespace ResourcePack