From cc49e5079d5e6311440fe21a3f4041de69fc5e4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Thu, 5 Mar 2026 10:16:26 +0100 Subject: [PATCH] Refactor a bit, preparing for async download --- Common/Net/HTTPRequest.cpp | 2 +- Core/HLE/sceNetAdhoc.cpp | 42 +++++++++++++++++++++++--------------- Core/HLE/sceNetAdhoc.h | 7 +------ UI/AdhocServerScreen.cpp | 13 +++--------- UI/AdhocServerScreen.h | 2 -- UI/NativeApp.cpp | 5 +++++ 6 files changed, 36 insertions(+), 35 deletions(-) diff --git a/Common/Net/HTTPRequest.cpp b/Common/Net/HTTPRequest.cpp index 4369fa685d..b956d16b76 100644 --- a/Common/Net/HTTPRequest.cpp +++ b/Common/Net/HTTPRequest.cpp @@ -91,7 +91,7 @@ std::shared_ptr RequestManager::StartDownload(std::string_view url, con if (File::ReadBinaryFileToString(cacheFile, &contents)) { INFO_LOG(Log::sceNet, "Returning cached file for %.*s: %s", (int)url.size(), url.data(), cacheFile.c_str()); // All is well, but we've indented a bit much here. - std::shared_ptr dl(new CachedRequest(RequestMethod::GET, url, "infra-dns.json", nullptr, flags, contents)); + std::shared_ptr dl(new CachedRequest(RequestMethod::GET, url, KeepAfterLast(url, '/'), nullptr, flags, contents)); newDownloads_.push_back(dl); return dl; } else { diff --git a/Core/HLE/sceNetAdhoc.cpp b/Core/HLE/sceNetAdhoc.cpp index 6e0a4f3f85..1b6f337195 100644 --- a/Core/HLE/sceNetAdhoc.cpp +++ b/Core/HLE/sceNetAdhoc.cpp @@ -118,32 +118,42 @@ const std::vector defaultProAdhocServerList = { {"Sony PSP & PSVita Fans", "psp.gameplayer.club", "https://psp.gameplayer.club/", "Unknown", "For players looking to play any games", AdhocDataMode::P2P}, }; -AdhocDataMode getAdhocServerDataMode(const std::string &server) { - auto list = defaultProAdhocServerList; +// TODO download the list from somewhere and probably cache it on disk +// should also make the url configurable and support file:/ local list besides https:// online lists +std::mutex g_proAdhocServerListMutex; +std::vector g_proAdhocServerList; - downloadedProAdhocServerListMutex.lock(); - if (downloadedProAdhocServerList.size() != 0) { - INFO_LOG(Log::sceNet, "Using downloaded adhoc server list"); - list = downloadedProAdhocServerList; - } - downloadedProAdhocServerListMutex.unlock(); +void AdhocLoadServerList() { + // Do this async. + std::thread thread([]() { + // Pretend for now, actually load later. + // We'll first load the default hardcoded list. Then we try: + // 1. Cached list in PSP/SYSTEM/CACHE + // 2. Online list from some url, and cache it in PSP/SYSTEM/CACHE for next time. + std::lock_guard guard(g_proAdhocServerListMutex); + g_proAdhocServerList = defaultProAdhocServerList; + }); + thread.detach(); +} +std::vector AdhocGetServerList() { + std::lock_guard guard(g_proAdhocServerListMutex); + return g_proAdhocServerList; +} + +static AdhocDataMode AdhocGetServerDataMode(std::string_view server) { + std::vector list = AdhocGetServerList(); for (const auto &item : list) { if (equals(server, item.hostname)) { - INFO_LOG(Log::sceNet, "server %s is in known list, using data mode %s", server.c_str(), AdhocDataModeToString(item.mode)); + INFO_LOG(Log::sceNet, "server %.*s is in known list, using data mode %s", STR_VIEW(server), AdhocDataModeToString(item.mode)); return item.mode; } } - INFO_LOG(Log::sceNet, "server %s is not in known list, using data mode %s", server.c_str(), AdhocDataModeToString(AdhocDataMode::P2P)); + INFO_LOG(Log::sceNet, "server %.*s is not in known list, using data mode %s", STR_VIEW(server), AdhocDataModeToString(AdhocDataMode::P2P)); return AdhocDataMode::P2P; } -// TODO download the list from somewhere and probably cache it on disk -// should also make the url configurable and support file:/ local list besides https:// online lists -std::mutex downloadedProAdhocServerListMutex; -std::vector downloadedProAdhocServerList; - int AcceptPtpSocket(int ptpId, int newsocket, sockaddr_in& peeraddr, SceNetEtherAddr* addr, u16_le* port); int PollAdhocSocket(SceNetAdhocPollSd* sds, int count, int timeout, int nonblock); int FlushPtpSocket(int socketId); @@ -1705,7 +1715,7 @@ u32 sceNetAdhocInit() { case AdhocServerRelayMode::Auto: serverHasRelay = false; // Fetch data mode from server list - if (getAdhocServerDataMode(g_Config.sProAdhocServer) == AdhocDataMode::AemuPostoffice) { + if (AdhocGetServerDataMode(g_Config.sProAdhocServer) == AdhocDataMode::AemuPostoffice) { serverHasRelay = true; } break; diff --git a/Core/HLE/sceNetAdhoc.h b/Core/HLE/sceNetAdhoc.h index a99fa38c62..34fe0ef6fc 100644 --- a/Core/HLE/sceNetAdhoc.h +++ b/Core/HLE/sceNetAdhoc.h @@ -112,12 +112,7 @@ struct AdhocServerListEntry { AdhocDataMode mode = AdhocDataMode::P2P; }; -extern const std::vector defaultProAdhocServerList; - -extern std::mutex downloadedProAdhocServerListMutex; -extern std::vector downloadedProAdhocServerList; - -AdhocDataMode getAdhocServerDataMode(const std::string &server); +std::vector AdhocGetServerList(); class PointerWrap; diff --git a/UI/AdhocServerScreen.cpp b/UI/AdhocServerScreen.cpp index efa94155e6..6cbc793ec8 100644 --- a/UI/AdhocServerScreen.cpp +++ b/UI/AdhocServerScreen.cpp @@ -11,15 +11,6 @@ AdhocServerScreen::AdhocServerScreen(std::string *value, std::string_view title) thiz->ResolverThread(); }, this); editValue_ = *value; - - auto list_to_use = defaultProAdhocServerList; - downloadedProAdhocServerListMutex.lock(); - if (downloadedProAdhocServerList.size() != 0) { - list_to_use = downloadedProAdhocServerList; - } - downloadedProAdhocServerListMutex.unlock(); - - listItems_ = list_to_use; } AdhocServerScreen::~AdhocServerScreen() { @@ -37,10 +28,12 @@ void AdhocServerScreen::CreatePopupContents(UI::ViewGroup *parent) { auto di = GetI18NCategory(I18NCat::DIALOG); auto n = GetI18NCategory(I18NCat::NETWORKING); + auto listItems = AdhocGetServerList(); + PopupTextInputChoice *textInputChoice = parent->Add(new PopupTextInputChoice(GetRequesterToken(), &editValue_, n->T("Hostname"), "", 256, screenManager())); std::vector listIP; - for (const auto &item : listItems_) { + for (const auto &item : listItems) { listIP.push_back(item.hostname); } diff --git a/UI/AdhocServerScreen.h b/UI/AdhocServerScreen.h index 3d42077101..ac5abb12e9 100644 --- a/UI/AdhocServerScreen.h +++ b/UI/AdhocServerScreen.h @@ -9,7 +9,6 @@ #include "Common/UI/PopupScreens.h" #include "Core/Config.h" // for AdhocServerListEntry! #include "Common/UI/Notice.h" -#include "Core/HLE/sceNetAdhoc.h" class AdhocServerScreen : public UI::PopupScreen { public: @@ -37,7 +36,6 @@ private: std::string *value_; std::string editValue_; - std::vector listItems_; NoticeView *progressView_ = nullptr; std::thread resolver_; diff --git a/UI/NativeApp.cpp b/UI/NativeApp.cpp index 4abdcfaa50..a53be59cc3 100644 --- a/UI/NativeApp.cpp +++ b/UI/NativeApp.cpp @@ -327,6 +327,8 @@ static void ClearFailedGPUBackends() { File::Delete(failedBackendsFile); } +void AdhocLoadServerList(); + void NativeInit(int argc, const char *argv[], const char *savegame_dir, const char *external_dir, const char *cache_dir) { net::Init(); // This needs to happen before we load the config. So on Windows we also run it in Main. It's fine to call multiple times. @@ -346,6 +348,9 @@ void NativeInit(int argc, const char *argv[], const char *savegame_dir, const ch // Make sure UI state is MENU. ResetUIState(); + // We kind of have to do this during startup, since these are visible in settings. + AdhocLoadServerList(); + bool skipLogo = false; setlocale( LC_ALL, "C" ); std::string user_data_path = savegame_dir;