diff --git a/src/Components/Modules/ServerList.cpp b/src/Components/Modules/ServerList.cpp index b8204e2c..ffa62eb1 100644 --- a/src/Components/Modules/ServerList.cpp +++ b/src/Components/Modules/ServerList.cpp @@ -309,6 +309,44 @@ namespace Components Game::MessageBox("Server added to favourites.", "Success"); } + void ServerList::RemoveFavourite(std::string server) + { + std::vector servers; + + if (Utils::IO::FileExists("players/favourites.json")) + { + std::string data = Utils::IO::ReadFile("players/favourites.json"); + json11::Json object = json11::Json::parse(data, data); + + if (!object.is_array()) + { + Logger::Print("Favourites storage file is invalid!\n"); + Game::MessageBox("Favourites storage file is invalid!", "Error"); + return; + } + + for (auto& storedServer : object.array_items()) + { + if (storedServer.is_string() && storedServer.string_value() != server) + { + servers.push_back(storedServer.string_value()); + } + } + } + + servers.push_back(server); + + json11::Json data = json11::Json(servers); + Utils::IO::WriteFile("players/favourites.json", data.dump()); + + auto list = ServerList::GetList(); + if (list) list->clear(); + + ServerList::RefreshVisibleList(UIScript::Token()); + + Game::MessageBox("Server removed from favourites.", "Success"); + } + void ServerList::LoadFavourties() { if (ServerList::IsFavouriteList() && Utils::IO::FileExists("players/favourites.json")) @@ -705,7 +743,7 @@ namespace Components }); UIScript::Add("CreateCurrentServerFavorite", [] (UIScript::Token) { - if (Dvar::Var("cl_ingame").get()) + if (Game::CL_IsCgameInitialized()) { std::string addressText = Network::Address(*Game::connectedHost).getString(); if (addressText != "0.0.0.0:0" && addressText != "loopback") @@ -714,6 +752,15 @@ namespace Components } } }); + UIScript::Add("DeleteFavorite", [] (UIScript::Token) + { + ServerList::ServerInfo* info = ServerList::GetCurrentServer(); + + if (info) + { + ServerList::RemoveFavourite(info->addr.getString()); + }; + }); // Add required ownerDraws UIScript::AddOwnerDraw(220, ServerList::UpdateSource); diff --git a/src/Components/Modules/ServerList.hpp b/src/Components/Modules/ServerList.hpp index 3ea93e9c..c5caccd8 100644 --- a/src/Components/Modules/ServerList.hpp +++ b/src/Components/Modules/ServerList.hpp @@ -116,6 +116,7 @@ namespace Components static void LoadFavourties(); static void StoreFavourite(std::string server); + static void RemoveFavourite(std::string server); static ServerInfo* GetServer(unsigned int index); static std::vector* GetList();