Skip to content

Commit

Permalink
Infrastructure: Only connect to adhoc server if connect_adhoc_for_gro…
Browse files Browse the repository at this point in the history
…uping is set in the json
  • Loading branch information
hrydgard committed Jan 20, 2025
1 parent 6b7dc6a commit 040b3ec
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 21 deletions.
47 changes: 31 additions & 16 deletions Core/HLE/sceNet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ std::string defaultNetSSID = "Wifi"; // fake AP/hotspot
int netApctlInfoId = 0;
SceNetApctlInfoInternal netApctlInfo;

bool netApctlInited;
bool g_netApctlInited;
u32 netApctlState;
u32 apctlProdCodeAddr = 0;
u32 apctlThreadHackAddr = 0;
Expand Down Expand Up @@ -185,6 +185,7 @@ bool LoadDNSForGameID(std::string_view gameID, InfraDNSConfig *dns) {
dns->revivalTeamURL = revived.getStringOr("url", "");
}
}
dns->connectAdHocForGrouping = def.getBool("connect_adhoc_for_grouping", false);
}

const JsonNode *games = root.getArray("games");
Expand Down Expand Up @@ -242,6 +243,7 @@ bool LoadDNSForGameID(std::string_view gameID, InfraDNSConfig *dns) {
dns->gameName = game.getStringOr("name", "");
dns->dns = game.getStringOr("dns", dns->dns.c_str());
dns->dyn_dns = game.getStringOr("dyn_dns", "");
dns->connectAdHocForGrouping = game.getBool("connect_adhoc_for_grouping", dns->connectAdHocForGrouping);
if (game.hasChild("domains", JSON_OBJECT)) {
const JsonGet domains = game.getDict("domains");
for (auto iter : domains.value_) {
Expand Down Expand Up @@ -332,7 +334,7 @@ int ScheduleApctlState(int event, int newState, int usec, const char* reason) {
}

void __NetApctlInit() {
netApctlInited = false;
g_netApctlInited = false;
netApctlState = PSP_NET_APCTL_STATE_DISCONNECTED;
apctlStateEvent = CoreTiming::RegisterEvent("__ApctlState", __ApctlState);
apctlHandlers.clear();
Expand Down Expand Up @@ -435,11 +437,11 @@ void __NetDoState(PointerWrap &p) {

auto cur_netInited = netInited;
auto cur_netInetInited = netInetInited;
auto cur_netApctlInited = netApctlInited;
auto cur_netApctlInited = g_netApctlInited;

Do(p, netInited);
Do(p, netInetInited);
Do(p, netApctlInited);
Do(p, g_netApctlInited);
Do(p, apctlHandlers);
Do(p, netMallocStat);
if (s < 2) {
Expand Down Expand Up @@ -488,7 +490,7 @@ void __NetDoState(PointerWrap &p) {

if (p.mode == p.MODE_READ) {
// Let's not change "Inited" value when Loading SaveState in the middle of multiplayer to prevent memory & port leaks
netApctlInited = cur_netApctlInited;
g_netApctlInited = cur_netApctlInited;
netInetInited = cur_netInetInited;
netInited = cur_netInited;

Expand Down Expand Up @@ -1005,7 +1007,7 @@ void NetApctl_InitInfo(int confId) {
}

static int sceNetApctlInit(int stackSize, int initPriority) {
if (netApctlInited) {
if (g_netApctlInited) {
return hleLogError(Log::sceNet, ERROR_NET_APCTL_ALREADY_INITIALIZED);
}

Expand Down Expand Up @@ -1037,16 +1039,22 @@ static int sceNetApctlInit(int stackSize, int initPriority) {
memcpy(prodCode->data, discID.c_str(), std::min(ADHOCCTL_ADHOCID_LEN, (int)discID.size()));
}

hleCall(sceNetAdhocctl, int, sceNetAdhocctlInit, stackSize, initPriority, apctlProdCodeAddr);

netApctlInited = true;
// Actually connect sceNetAdhocctl. TODO: Can we move this to sceNetApctlConnect? Would be good because a lot of games call Init but not Connect on startup.
if (g_infraDNSConfig.connectAdHocForGrouping) {
hleCall(sceNetAdhocctl, int, sceNetAdhocctlInit, stackSize, initPriority, apctlProdCodeAddr);
}

g_netApctlInited = true;
return hleLogSuccessInfoI(Log::sceNet, 0);
}

int NetApctl_Term() {
// Note: Since we're borrowing AdhocServer for Grouping purpose, we should cleanup too
NetAdhocctl_Term();
// Should we check the g_infraDNSConfig.connectAdHocForGrouping flag here?
if (g_netApctlInited) {
hleCall(sceNetAdhocctl, int, sceNetAdhocctlTerm);
}

if (apctlProdCodeAddr != 0) {
userMemory.Free(apctlProdCodeAddr);
apctlProdCodeAddr = 0;
Expand All @@ -1060,9 +1068,8 @@ int NetApctl_Term() {
apctlThreadID = 0;
}

netApctlInited = false;
g_netApctlInited = false;
netApctlState = PSP_NET_APCTL_STATE_DISCONNECTED;

return 0;
}

Expand Down Expand Up @@ -1274,12 +1281,18 @@ int sceNetApctlConnect(int confId) {

// Is this confId is the index to the scanning's result data or sceNetApctlGetBSSDescIDListUser result?
netApctlInfoId = confId;

// Note: We're borrowing AdhocServer for Grouping purpose, so we can simulate Broadcast over the internet just like Adhoc's pro-online implementation
int ret = hleCall(sceNetAdhocctl, int, sceNetAdhocctlConnect, "INFRA");
int ret = 0;
if (g_infraDNSConfig.connectAdHocForGrouping) {
ret = hleCall(sceNetAdhocctl, int, sceNetAdhocctlConnect, "INFRA");
}

if (netApctlState == PSP_NET_APCTL_STATE_DISCONNECTED)
if (netApctlState == PSP_NET_APCTL_STATE_DISCONNECTED) {
__UpdateApctlHandlers(0, PSP_NET_APCTL_STATE_JOINING, PSP_NET_APCTL_EVENT_CONNECT_REQUEST, 0);
//hleDelayResult(0, "give time to init/cleanup", adhocEventDelayMS * 1000);
}

// hleDelayResult(0, "give time to init/cleanup", adhocEventDelayMS * 1000);
// TODO: Blocks current thread and wait for a state change to prevent user-triggered connection attempt from causing events to piles up
return hleLogSuccessInfoI(Log::sceNet, 0, "connect = %i", ret);
}
Expand All @@ -1288,7 +1301,9 @@ int sceNetApctlDisconnect() {
// Like its 'sister' function sceNetAdhocctlDisconnect, we need to alert Apctl handlers that a disconnect took place
// or else games like Phantasy Star Portable 2 will hang at certain points (e.g. returning to the main menu after trying to connect to PSN).
// Note: Since we're borrowing AdhocServer for Grouping purpose, we should disconnect too
hleCall(sceNetAdhocctl, int, sceNetAdhocctlDisconnect);
if (g_infraDNSConfig.connectAdHocForGrouping) {
hleCall(sceNetAdhocctl, int, sceNetAdhocctlDisconnect);
}

// Discards any pending events so we can disconnect immediately
apctlEvents.clear();
Expand Down
8 changes: 7 additions & 1 deletion Core/HLE/sceNet.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class AfterApctlMipsCall : public PSPAction {
};

extern bool netInited;
extern bool netApctlInited;
extern bool g_netApctlInited;
extern u32 netApctlState;
extern SceNetApctlInfoInternal netApctlInfo;
extern std::string defaultNetConfigName;
Expand All @@ -127,6 +127,10 @@ void __NetDoState(PointerWrap &p);

int NetApctl_GetState();

inline bool __NetApctlConnected() {
return netApctlState >= PSP_NET_APCTL_STATE_GOT_IP;
}

int sceNetApctlConnect(int connIndex);

// These return false if allowed to be consistent with the similar function for achievements.
Expand Down Expand Up @@ -154,6 +158,8 @@ struct InfraDNSConfig {

std::string revivalTeam;
std::string revivalTeamURL;

bool connectAdHocForGrouping;
};

extern InfraDNSConfig g_infraDNSConfig;
4 changes: 2 additions & 2 deletions Core/HLE/sceNetAdhoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ void ZeroNetAdhocMatchingThreads();
void SaveNetAdhocMatchingInited();
void RestoreNetAdhocMatchingInited();


bool __NetAdhocConnected() {
return netAdhocInited && netAdhocctlInited && (adhocctlState == ADHOCCTL_STATE_CONNECTED || adhocctlState == ADHOCCTL_STATE_GAMEMODE);
}
Expand Down Expand Up @@ -1278,8 +1277,9 @@ int sceNetAdhocctlInit(int stackSize, int prio, u32 productAddr) {
// FIXME: Returning 0x8002013a (SCE_KERNEL_ERROR_LIBRARY_NOT_YET_LINKED) without adhoc module loaded first?
// FIXME: Sometimes returning 0x80410601 (ERROR_NET_ADHOC_AUTH_ALREADY_INITIALIZED / Library module is already initialized ?) when AdhocctlTerm is not fully done?

if (netAdhocctlInited)
if (netAdhocctlInited) {
return hleLogError(Log::sceNet, ERROR_NET_ADHOCCTL_ALREADY_INITIALIZED);
}

auto product = PSPPointer<SceNetAdhocctlAdhocId>::Create(productAddr);
if (product.IsValid()) {
Expand Down
3 changes: 2 additions & 1 deletion Core/HLE/sceNetAdhoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void __UpdateAdhocctlHandlers(u32 flags, u32 error);

bool __NetAdhocConnected();

// Called from netdialog
// Called from netdialog (and from sceNetApctl)
// NOTE: use hleCall for sceNet* ones!
int sceNetAdhocctlGetState(u32 ptrToStatus);
int sceNetAdhocctlCreate(const char * groupName);
Expand All @@ -116,6 +116,7 @@ int sceNetAdhocctlScan();
int sceNetAdhocctlGetScanInfo(u32 sizeAddr, u32 bufAddr);
int sceNetAdhocctlDisconnect();
int sceNetAdhocctlInit(int stackSize, int prio, u32 productAddr);
int sceNetAdhocctlTerm();

int NetAdhocctl_Term();
int NetAdhocctl_GetState();
Expand Down
2 changes: 1 addition & 1 deletion Core/HLE/sceNetInet.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class PointerWrap;

extern bool netInited;
extern bool netInetInited;
extern bool netApctlInited;
extern bool g_netApctlInited;
extern u32 netApctlState;
extern SceNetApctlInfoInternal netApctlInfo;
extern std::string defaultNetConfigName;
Expand Down

0 comments on commit 040b3ec

Please sign in to comment.