ResourcePack: Avoid crashes on invalid packs during Init().
This commit is contained in:
parent
107a928452
commit
efbf5a450b
|
@ -31,35 +31,45 @@ IniFile GetPackConfig()
|
||||||
bool Init()
|
bool Init()
|
||||||
{
|
{
|
||||||
packs.clear();
|
packs.clear();
|
||||||
auto pack_list = Common::DoFileSearch({File::GetUserPath(D_RESOURCEPACK_IDX)}, {".zip"});
|
const std::vector<std::string> pack_list =
|
||||||
|
Common::DoFileSearch({File::GetUserPath(D_RESOURCEPACK_IDX)}, {".zip"});
|
||||||
bool error = false;
|
|
||||||
|
|
||||||
IniFile file = GetPackConfig();
|
IniFile file = GetPackConfig();
|
||||||
|
|
||||||
auto* order = file.GetOrCreateSection("Order");
|
auto* order = file.GetOrCreateSection("Order");
|
||||||
|
|
||||||
std::sort(pack_list.begin(), pack_list.end(), [order](std::string& a, std::string& b) {
|
struct OrderHelper
|
||||||
std::string order_a = a, order_b = b;
|
|
||||||
|
|
||||||
order->Get(ResourcePack(a).GetManifest()->GetID(), &order_a);
|
|
||||||
order->Get(ResourcePack(b).GetManifest()->GetID(), &order_b);
|
|
||||||
|
|
||||||
return order_a < order_b;
|
|
||||||
});
|
|
||||||
|
|
||||||
for (size_t i = 0; i < pack_list.size(); i++)
|
|
||||||
{
|
{
|
||||||
const auto& path = pack_list[i];
|
size_t pack_list_index;
|
||||||
|
std::string manifest_id;
|
||||||
|
};
|
||||||
|
|
||||||
if (!Add(path))
|
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::sort(
|
||||||
|
pack_list_order.begin(), pack_list_order.end(),
|
||||||
|
[](const OrderHelper& a, const OrderHelper& b) { return a.manifest_id < b.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;
|
error = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (i < packs.size())
|
order->Set(pack->GetManifest()->GetID(), static_cast<u64>(i));
|
||||||
order->Set(packs[i].GetManifest()->GetID(), static_cast<u64>(i));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
file.Save(packs_path);
|
file.Save(packs_path);
|
||||||
|
@ -103,7 +113,7 @@ std::vector<ResourcePack*> GetHigherPriorityPacks(ResourcePack& pack)
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Add(const std::string& path, int offset)
|
ResourcePack* Add(const std::string& path, int offset)
|
||||||
{
|
{
|
||||||
if (offset == -1)
|
if (offset == -1)
|
||||||
offset = static_cast<int>(packs.size());
|
offset = static_cast<int>(packs.size());
|
||||||
|
@ -111,7 +121,7 @@ bool Add(const std::string& path, int offset)
|
||||||
ResourcePack pack(path);
|
ResourcePack pack(path);
|
||||||
|
|
||||||
if (!pack.IsValid())
|
if (!pack.IsValid())
|
||||||
return false;
|
return nullptr;
|
||||||
|
|
||||||
IniFile file = GetPackConfig();
|
IniFile file = GetPackConfig();
|
||||||
|
|
||||||
|
@ -124,9 +134,8 @@ bool Add(const std::string& path, int offset)
|
||||||
|
|
||||||
file.Save(packs_path);
|
file.Save(packs_path);
|
||||||
|
|
||||||
packs.insert(packs.begin() + offset, std::move(pack));
|
auto it = packs.insert(packs.begin() + offset, std::move(pack));
|
||||||
|
return &*it;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Remove(ResourcePack& pack)
|
bool Remove(ResourcePack& pack)
|
||||||
|
|
|
@ -12,7 +12,7 @@ namespace ResourcePack
|
||||||
{
|
{
|
||||||
bool Init();
|
bool Init();
|
||||||
|
|
||||||
bool Add(const std::string& path, int offset = -1);
|
ResourcePack* Add(const std::string& path, int offset = -1);
|
||||||
bool Remove(ResourcePack& pack);
|
bool Remove(ResourcePack& pack);
|
||||||
void SetInstalled(const ResourcePack& pack, bool installed);
|
void SetInstalled(const ResourcePack& pack, bool installed);
|
||||||
bool IsInstalled(const ResourcePack& pack);
|
bool IsInstalled(const ResourcePack& pack);
|
||||||
|
|
Loading…
Reference in New Issue