From 90f52b88a1fbd84cc8f9a1ada8733ecef6dd48c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Fri, 20 Mar 2026 13:17:43 +0100 Subject: [PATCH] Initial work on downloading and showing the status from adhoc servers --- Core/HLE/sceNetAdhoc.cpp | 3 +- UI/AdhocServerScreen.cpp | 112 ++++++++++++++++++++++++++++++++++++++ assets/adhoc-servers.json | 6 +- 3 files changed, 118 insertions(+), 3 deletions(-) diff --git a/Core/HLE/sceNetAdhoc.cpp b/Core/HLE/sceNetAdhoc.cpp index 1900a166d6..f75a303152 100644 --- a/Core/HLE/sceNetAdhoc.cpp +++ b/Core/HLE/sceNetAdhoc.cpp @@ -121,6 +121,7 @@ static const char *AdhocDataModeToString(AdhocDataMode mode) { std::mutex g_proAdhocServerListMutex; std::vector g_proAdhocServerList; +// TODO: Should convert this to use rapidjson static bool ParseServerListEntriesJSON(std::string_view json) { using namespace json; @@ -149,7 +150,7 @@ static bool ParseServerListEntriesJSON(std::string_view json) { entry.location = server.getStringOr("location", ""); entry.description = server.getStringOr("description", ""); entry.mode = equals(server.getStringOr("data_mode", ""), "AemuPostoffice") ? AdhocDataMode::AemuPostoffice : AdhocDataMode::P2P; - entry.statusUrl = server.getStringOr("status_url", ""); + entry.statusUrl = server.getStringOr("status_data_json", ""); if (entry.host.empty()) { // Skipping invalid entry. diff --git a/UI/AdhocServerScreen.cpp b/UI/AdhocServerScreen.cpp index fa82bc1675..c15f23d35d 100644 --- a/UI/AdhocServerScreen.cpp +++ b/UI/AdhocServerScreen.cpp @@ -1,13 +1,95 @@ #include + +#undef new +#include "ext/rapidjson/include/rapidjson/document.h" +#include "Common/DbgNew.h" +//#include "rapidjson/document.h" + #include "AdhocServerScreen.h" +#include "Core/Util/GameDB.h" #include "Common/Net/Resolve.h" #include "Common/UI/Root.h" #include "Common/UI/PopupScreens.h" #include "Common/StringUtils.h" +#include "Common/Net/HTTPClient.h" #include "Core/HLE/sceNetAdhoc.h" #include "UI/MiscViews.h" +struct User { + std::string name; + std::vector pdp_ports; + std::vector ptp_ports; +}; + +struct Group { + std::string name; + int usercount; + std::vector users; +}; + +struct Game { + std::string name; + int usercount; + std::vector groups; + std::vector game_ids; +}; + +static void UpgradeName(std::string *str) { + if (str->size() == 9) { // TODO: Make a better check + // It's probably a game ID. Convert it to a name using the database. + std::vector infos; + if (g_gameDB.GetGameInfos(*str, &infos)) { + *str = infos[0].title; + } + } +} + +std::vector ParseDataJson(std::string_view json) { + rapidjson::Document d; + d.Parse(json.data(), json.size()); + + std::vector gameList; + + if (d.HasParseError() || !d.HasMember("games")) return gameList; + + const auto& gamesArray = d["games"]; + for (auto& g : gamesArray.GetArray()) { + Game game; + game.name = g["name"].GetString(); + UpgradeName(&game.name); + + // Handle string-to-int conversion for usercount + game.usercount = std::stoi(g["usercount"].GetString()); + + if (g.HasMember("groups")) { + for (auto& grp : g["groups"].GetArray()) { + Group group; + group.name = grp["name"].GetString(); + group.usercount = std::stoi(grp["usercount"].GetString()); + + if (grp.HasMember("users")) { + for (auto& u : grp["users"].GetArray()) { + User user; + user.name = u["name"].GetString(); + + for (auto& p : u["pdp_ports"].GetArray()) + user.pdp_ports.push_back(p.GetInt()); + + for (auto& p : u["ptp_ports"].GetArray()) + user.ptp_ports.push_back(p.GetInt()); + + group.users.push_back(user); + } + } + game.groups.push_back(group); + } + } + gameList.push_back(game); + } + return gameList; +} + class AdhocAddServerPopupScreen : public UI::PopupScreen { public: AdhocAddServerPopupScreen(std::string *outEditValue) : PopupScreen(T(I18NCat::NETWORKING, "Add server"), T(I18NCat::DIALOG, "Add"), T(I18NCat::DIALOG, "Cancel")), outEditValue_(outEditValue) { @@ -90,6 +172,10 @@ class AdhocServerInfoScreen : public UI::PopupScreen { public: AdhocServerInfoScreen(const AdhocServerListEntry &entry) : PopupScreen(entry.name, T(I18NCat::DIALOG, "Back")), entry_(entry) { + + if (!entry.statusUrl.empty()) { + statusRequest_ = g_DownloadManager.StartDownload(entry.statusUrl, Path(), http::RequestFlags::KeepInMemory | http::RequestFlags::Cached24H, nullptr, "status"); + } } // PopupScreen will translate Back on its own const char *tag() const override { return "AdhocServerInfo"; } @@ -135,12 +221,38 @@ protected: } } + if (entry_.statusUrl.empty()) { + content->Add(new TextView("This server has no data.json status page.")); + } else { + if (games_.empty()) { + content->Add(new TextView("No games are currently being played on this server.")); + } else { + // TODO: Do something more sophisticated here, like showing groups and users. For now, just show game names. + for (const auto &game : games_) { + content->Add(new TextView(game.name)); + } + } + } + scroll->Add(content); parent->Add(scroll); } + void update() override { + UI::PopupScreen::update(); + if (statusRequest_ && statusRequest_->Done()) { + std::string json; + statusRequest_->buffer().TakeAll(&json); + games_ = ParseDataJson(json); + statusRequest_.reset(); + RecreateViews(); + } + } + private: AdhocServerListEntry entry_; + std::vector games_; + std::shared_ptr statusRequest_; }; class AdhocServerRow : public UI::LinearLayout { diff --git a/assets/adhoc-servers.json b/assets/adhoc-servers.json index f14af8e491..37a8b8701e 100644 --- a/assets/adhoc-servers.json +++ b/assets/adhoc-servers.json @@ -25,7 +25,8 @@ "web": "", "location": "France", "description": "Mostly for Medal of Honor Heros 2 & Need For Speed Most Wanted players, but can be used for other games", - "data_mode": "AemuPostoffice" + "data_mode": "AemuPostoffice", + "status_data_json": "https://adhoc.eahub.eu/data.json" }, { "name": "Psi-Hate", @@ -34,7 +35,8 @@ "web": "", "location": "Minnesota USA", "description": "For players looking to play any games", - "data_mode": "AemuPostoffice" + "data_mode": "AemuPostoffice", + "status_data_json": "http://psi-hate.com:27315/data.json" }, { "name": "Relay Brasileiro",