[Favourite] Check if file is empty (#443)

This commit is contained in:
Edo 2022-08-19 18:35:36 +02:00 committed by GitHub
parent 2f6cbf4549
commit dd9e61910c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 37 deletions

View File

@ -324,11 +324,10 @@ namespace Components
{ {
std::vector<std::string> servers; std::vector<std::string> servers;
if (Utils::IO::FileExists("players/favourites.json")) const auto parseData = Utils::IO::ReadFile(FavouriteFile);
if (!parseData.empty())
{ {
std::string data = Utils::IO::ReadFile("players/favourites.json"); const nlohmann::json object = nlohmann::json::parse(parseData);
nlohmann::json object = nlohmann::json::parse(data);
if (!object.is_array()) if (!object.is_array())
{ {
Logger::Print("Favourites storage file is invalid!\n"); Logger::Print("Favourites storage file is invalid!\n");
@ -336,25 +335,24 @@ namespace Components
return; return;
} }
nlohmann::json::array_t storedServers = object; const nlohmann::json::array_t storedServers = object;
for (const auto& storedServer : storedServers)
for (unsigned int i = 0; i < storedServers.size(); ++i)
{ {
if (!storedServers[i].is_string()) continue; if (!storedServer.is_string()) continue;
if (storedServers[i].get<std::string>() == server) if (storedServer.get<std::string>() == server)
{ {
Game::ShowMessageBox("Server already marked as favourite.", "Error"); Game::ShowMessageBox("Server already marked as favourite.", "Error");
return; return;
} }
servers.push_back(storedServers[i].get<std::string>()); servers.push_back(storedServer.get<std::string>());
} }
} }
servers.push_back(server); servers.push_back(server);
nlohmann::json data = nlohmann::json(servers); const auto data = nlohmann::json(servers);
Utils::IO::WriteFile("players/favourites.json", data.dump()); Utils::IO::WriteFile(FavouriteFile, data.dump());
Game::ShowMessageBox("Server added to favourites.", "Success"); Game::ShowMessageBox("Server added to favourites.", "Success");
} }
@ -362,10 +360,10 @@ namespace Components
{ {
std::vector<std::string> servers; std::vector<std::string> servers;
if (Utils::IO::FileExists("players/favourites.json")) const auto parseData = Utils::IO::ReadFile(FavouriteFile);
if (!parseData.empty())
{ {
std::string data = Utils::IO::ReadFile("players/favourites.json"); const nlohmann::json object = nlohmann::json::parse(parseData);
nlohmann::json object = nlohmann::json::parse(data);
if (!object.is_array()) if (!object.is_array())
{ {
@ -374,8 +372,7 @@ namespace Components
return; return;
} }
nlohmann::json::array_t arr = object; const nlohmann::json::array_t arr = object;
for (auto& storedServer : arr) for (auto& storedServer : arr)
{ {
if (storedServer.is_string() && storedServer.get<std::string>() != server) if (storedServer.is_string() && storedServer.get<std::string>() != server)
@ -385,8 +382,8 @@ namespace Components
} }
} }
nlohmann::json data = nlohmann::json(servers); const auto data = nlohmann::json(servers);
Utils::IO::WriteFile("players/favourites.json", data.dump()); Utils::IO::WriteFile(FavouriteFile, data.dump());
auto list = ServerList::GetList(); auto list = ServerList::GetList();
if (list) list->clear(); if (list) list->clear();
@ -398,14 +395,21 @@ namespace Components
void ServerList::LoadFavourties() void ServerList::LoadFavourties()
{ {
if (ServerList::IsFavouriteList() && Utils::IO::FileExists("players/favourites.json")) if (!ServerList::IsFavouriteList())
{ {
return;
}
auto list = ServerList::GetList(); auto list = ServerList::GetList();
if (list) list->clear(); if (list) list->clear();
std::string data = Utils::IO::ReadFile("players/favourites.json"); const auto parseData = Utils::IO::ReadFile(FavouriteFile);
nlohmann::json object = nlohmann::json::parse(data); if (parseData.empty())
{
return;
}
const nlohmann::json object = nlohmann::json::parse(parseData);
if (!object.is_array()) if (!object.is_array())
{ {
Logger::Print("Favourites storage file is invalid!\n"); Logger::Print("Favourites storage file is invalid!\n");
@ -413,13 +417,11 @@ namespace Components
return; return;
} }
nlohmann::json::array_t servers = object; const nlohmann::json::array_t servers = object;
for (const auto& server : servers)
for (unsigned int i = 0; i < servers.size(); ++i)
{ {
if (!servers[i].is_string()) continue; if (!server.is_string()) continue;
ServerList::InsertRequest(servers[i].get<std::string>()); ServerList::InsertRequest(server.get<std::string>());
}
} }
} }

View File

@ -71,6 +71,8 @@ namespace Components
Ping, Ping,
}; };
static constexpr auto* FavouriteFile = "players/favourites.json";
#pragma pack(push, 1) #pragma pack(push, 1)
union MasterEntry union MasterEntry
{ {