Skip to content

Commit

Permalink
Merge pull request hrydgard#19903 from hrydgard/network-connection-de…
Browse files Browse the repository at this point in the history
…tection

Don't disable savestates just because adhoc is "inited"
  • Loading branch information
hrydgard authored Jan 21, 2025
2 parents a7ce204 + 2a924ff commit d7aa236
Show file tree
Hide file tree
Showing 15 changed files with 220 additions and 69 deletions.
1 change: 1 addition & 0 deletions Core/Dialog/PSPDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.

#include <algorithm>
#include "Common/Data/Text/I18n.h"

#include "Common/Serialize/Serializer.h"
Expand Down
40 changes: 24 additions & 16 deletions Core/HLE/sceNet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
#include "Core/HLE/sceNetInet.h"
#include "Core/HLE/sceNetResolver.h"

bool netInited;
bool g_netInited;

u32 netDropRate = 0;
u32 netDropDuration = 0;
Expand All @@ -64,8 +64,9 @@ static struct SceNetMallocStat netMallocStat;

static std::map<int, ApctlHandler> apctlHandlers;

std::string defaultNetConfigName = "NetConf";
std::string defaultNetSSID = "Wifi"; // fake AP/hotspot
const char * const defaultNetConfigName = "NetConf";
const char * const defaultNetSSID = "Wifi"; // fake AP/hotspot

int netApctlInfoId = 0;
SceNetApctlInfoInternal netApctlInfo;

Expand All @@ -88,8 +89,13 @@ int NetApctl_Term();
void NetApctl_InitDefaultInfo();
void NetApctl_InitInfo(int confId);

bool IsNetworkConnected() {
// TODO: Tweak this.
return __NetApctlConnected() || __NetAdhocConnected();
}

bool NetworkWarnUserIfOnlineAndCantSavestate() {
if (netInited && !g_Config.bAllowSavestateWhileConnected) {
if (IsNetworkConnected() && !g_Config.bAllowSavestateWhileConnected) {
auto nw = GetI18NCategory(I18NCat::NETWORKING);
g_OSD.Show(OSDType::MESSAGE_INFO, nw->T("Save states are not available when online"), 3.0f, "saveonline");
return true;
Expand All @@ -99,7 +105,7 @@ bool NetworkWarnUserIfOnlineAndCantSavestate() {
}

bool NetworkWarnUserIfOnlineAndCantSpeed() {
if (netInited) {
if (IsNetworkConnected()) {
auto nw = GetI18NCategory(I18NCat::NETWORKING);
g_OSD.Show(OSDType::MESSAGE_INFO, nw->T("Speed controls are not available when online"), 3.0f, "speedonline");
return true;
Expand All @@ -109,11 +115,11 @@ bool NetworkWarnUserIfOnlineAndCantSpeed() {
}

bool NetworkAllowSpeedControl() {
return !netInited;
return !IsNetworkConnected();
}

bool NetworkAllowSaveState() {
return !netInited || g_Config.bAllowSavestateWhileConnected;
return !IsNetworkConnected() || g_Config.bAllowSavestateWhileConnected;
}

void AfterApctlMipsCall::DoState(PointerWrap & p) {
Expand Down Expand Up @@ -165,6 +171,8 @@ bool LoadDNSForGameID(std::string_view gameID, InfraDNSConfig *dns) {
return false;
}

dns->loaded = true;

std::string_view jsonStr = std::string_view((const char *)data.get(), jsonSize);
json::JsonReader reader(jsonStr.data(), jsonStr.length());
if (!reader.ok() || !reader.root()) {
Expand Down Expand Up @@ -343,7 +351,7 @@ void __NetApctlInit() {
}

static void __ResetInitNetLib() {
netInited = false;
g_netInited = false;
netInetInited = false; // shouldn't this actually do something?

memset(&netMallocStat, 0, sizeof(netMallocStat));
Expand Down Expand Up @@ -435,11 +443,11 @@ void __NetDoState(PointerWrap &p) {
if (!s)
return;

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

Do(p, netInited);
Do(p, g_netInited);
Do(p, netInetInited);
Do(p, g_netApctlInited);
Do(p, apctlHandlers);
Expand Down Expand Up @@ -492,7 +500,7 @@ void __NetDoState(PointerWrap &p) {
// Let's not change "Inited" value when Loading SaveState in the middle of multiplayer to prevent memory & port leaks
g_netApctlInited = cur_netApctlInited;
netInetInited = cur_netInetInited;
netInited = cur_netInited;
g_netInited = cur_netInited;

// Discard leftover events
apctlEvents.clear();
Expand Down Expand Up @@ -673,7 +681,7 @@ u32 Net_Term() {
//NetInet_Term();

// Library is initialized
if (netInited) {
if (g_netInited) {
// Delete Adhoc Sockets
deleteAllAdhocSockets();

Expand All @@ -691,7 +699,7 @@ u32 Net_Term() {
FreeUser(netPoolAddr);
FreeUser(netThread1Addr);
FreeUser(netThread2Addr);
netInited = false;
g_netInited = false;

g_infraDNSConfig = {};
return 0;
Expand Down Expand Up @@ -720,7 +728,7 @@ static int sceNetInit(u32 poolSize, u32 calloutPri, u32 calloutStack, u32 netini
// TODO: Create Network Threads using given priority & stack
// TODO: The correct behavior is actually to allocate more and leak the other threads/pool.
// But we reset here for historic reasons (GTA:VCS potentially triggers this.)
if (netInited) {
if (g_netInited) {
// This cleanup attempt might not worked when SaveState were loaded in the middle of multiplayer game and re-entering multiplayer, thus causing memory leaks & wasting binded ports.
// Maybe we shouldn't save/load "Inited" vars on SaveState?
Net_Term();
Expand Down Expand Up @@ -805,7 +813,7 @@ static int sceNetInit(u32 poolSize, u32 calloutPri, u32 calloutStack, u32 netini
}
}

netInited = true;
g_netInited = true;

auto n = GetI18NCategory(I18NCat::NETWORKING);

Expand Down Expand Up @@ -974,7 +982,7 @@ void NetApctl_InitInfo(int confId) {
truncate_cpy(netApctlInfo.name, sizeof(netApctlInfo.name), defaultNetConfigName + std::to_string(confId));
truncate_cpy(netApctlInfo.ssid, sizeof(netApctlInfo.ssid), defaultNetSSID);
memcpy(netApctlInfo.bssid, "\1\1\2\2\3\3", sizeof(netApctlInfo.bssid)); // fake AP's mac address
netApctlInfo.ssidLength = static_cast<unsigned int>(defaultNetSSID.length());
netApctlInfo.ssidLength = static_cast<unsigned int>(strlen(defaultNetSSID));
netApctlInfo.strength = 99;
netApctlInfo.channel = g_Config.iWlanAdhocChannel;
if (netApctlInfo.channel == PSP_SYSTEMPARAM_ADHOC_CHANNEL_AUTOMATIC) netApctlInfo.channel = defaultWlanChannel;
Expand Down
10 changes: 7 additions & 3 deletions Core/HLE/sceNet.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ class AfterApctlMipsCall : public PSPAction {
u32_le argsAddr = 0;
};

extern bool netInited;
extern bool g_netInited;
extern bool g_netApctlInited;
extern u32 netApctlState;
extern SceNetApctlInfoInternal netApctlInfo;
extern std::string defaultNetConfigName;
extern std::string defaultNetSSID;
extern const char * const defaultNetConfigName;
extern const char * const defaultNetSSID;

void Register_sceNet();
void Register_sceNetApctl();
Expand All @@ -131,6 +131,9 @@ bool __NetApctlConnected();

int sceNetApctlConnect(int connIndex);

// Are we connected - for the purpose of disabling speed consoles and savestates etc.
bool IsNetworkConnected();

// These return false if allowed to be consistent with the similar function for achievements.
bool NetworkWarnUserIfOnlineAndCantSavestate();
bool NetworkWarnUserIfOnlineAndCantSpeed();
Expand All @@ -146,6 +149,7 @@ enum class InfraGameState {

// Loaded an interpreted for a specific game from the JSON - doesn't represent the entire JSON.
struct InfraDNSConfig {
bool loaded;
std::string gameName;
std::string dns;
std::string dyn_dns;
Expand Down
24 changes: 6 additions & 18 deletions Core/HLE/sceNetAdhoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,16 @@

// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
// sceNetAdhoc

#if defined(_WIN32)
#include <WinSock2.h>
#include "Common/CommonWindows.h"
#endif
// This is a direct port of Coldbird's code from http://code.google.com/p/aemu/
// All credit goes to him!

#if !defined(_WIN32)
#include <sys/types.h>
#include <netinet/tcp.h>
#endif

#ifndef MSG_NOSIGNAL
// Default value to 0x00 (do nothing) in systems where it's not supported.
#define MSG_NOSIGNAL 0x00
#endif

#include <mutex>
// sceNetAdhoc
#include <string>

// This is a direct port of Coldbird's code from http://code.google.com/p/aemu/
// All credit goes to him!
#include "Common/Net/SocketCompat.h"
#include "Common/Data/Text/I18n.h"
#include "Common/Serialize/Serializer.h"
#include "Common/Serialize/SerializeFuncs.h"
Expand Down Expand Up @@ -89,7 +78,6 @@ int adhocEventDelay = 2000000; //2000000 on real PSP ?

constexpr u32 defaultLastRecvDelta = 10000; //10000 usec worked well for games published by Falcom (ie. Ys vs Sora Kiseki, Vantage Master Portable)


SceUID threadAdhocID;

std::recursive_mutex adhocEvtMtx;
Expand Down Expand Up @@ -1352,7 +1340,7 @@ int sceNetAdhocPdpCreate(const char *mac, int port, int bufferSize, u32 flag) {
return hleLogError(Log::sceNet, -1, "WLAN");
}

if (!netInited)
if (!g_netInited)
return hleLogError(Log::sceNet, 0x800201CA); //PSP_LWMUTEX_ERROR_NO_SUCH_LWMUTEX;

// Library is initialized
Expand Down
3 changes: 2 additions & 1 deletion Core/HLE/sceNetAdhoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include <deque>
#include "Core/HLE/proAdhoc.h"


#ifdef _MSC_VER
#pragma pack(push,1)
#endif
Expand Down Expand Up @@ -128,6 +127,8 @@ extern bool netAdhocInited;
extern bool netAdhocctlInited;
extern bool g_adhocServerConnected;
extern bool netAdhocGameModeEntered;
extern s32 netAdhocDiscoverStatus;

extern int netAdhocEnterGameModeTimeout;
extern int adhocDefaultTimeout; //3000000 usec
extern int adhocDefaultDelay; //10000
Expand Down
2 changes: 2 additions & 0 deletions Core/HLE/sceNetAdhocMatching.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ void RestoreNetAdhocMatchingInited();
void Register_sceNetAdhocMatching();
void __NetAdhocMatchingInit();
void __NetAdhocMatchingShutdown();

extern bool netAdhocMatchingInited;
13 changes: 13 additions & 0 deletions Core/HLE/sceNetApctl.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
// TODO: move apctl here

#include "sceNetApctl.h"

const char *ApctlStateToString(int apctlState) {
switch (apctlState) {
case PSP_NET_APCTL_STATE_DISCONNECTED: return "DISCONNECTED";
case PSP_NET_APCTL_STATE_SCANNING: return "SCANNING";
case PSP_NET_APCTL_STATE_JOINING: return "JOINING";
case PSP_NET_APCTL_STATE_GETTING_IP: return "GETTING_IP";
case PSP_NET_APCTL_STATE_GOT_IP: return "GOT_IP";
case PSP_NET_APCTL_STATE_EAP_AUTH: return "EAP_AUTH";
case PSP_NET_APCTL_STATE_KEY_EXCHANGE: return "KEY_EXCHANGE";
default: return "N/A";
}
}
2 changes: 2 additions & 0 deletions Core/HLE/sceNetApctl.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ enum {
PSP_NET_APCTL_EVENT_SCAN_STOP = 11 // FIXME: not sure what this is, MGS:PW seems to check this value within ApctlHandler during Recruit, related to sceNetApctlScanSSID2 ?
};

const char *ApctlStateToString(int apctlState);

#define PSP_NET_APCTL_INFO_PROFILE_NAME 0
#define PSP_NET_APCTL_INFO_BSSID 1
#define PSP_NET_APCTL_INFO_SSID 2
Expand Down
6 changes: 3 additions & 3 deletions Core/HLE/sceNetInet.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ typedef struct InetSocket {

class PointerWrap;

extern bool netInited;
extern bool g_netInited;
extern bool netInetInited;
extern bool g_netApctlInited;
extern u32 netApctlState;
extern SceNetApctlInfoInternal netApctlInfo;
extern std::string defaultNetConfigName;
extern std::string defaultNetSSID;
extern const char *const defaultNetConfigName;
extern const char *const defaultNetSSID;

void Register_sceNetInet();

Expand Down
18 changes: 7 additions & 11 deletions Core/HLE/scePspNpDrm_user.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,33 @@
#include "Core/HLE/sceIo.h"

static int sceNpDrmSetLicenseeKey(u32 npDrmKeyAddr) {
WARN_LOG(Log::HLE, "UNIMPL: sceNpDrmSetLicenseeKey(%08x)", npDrmKeyAddr);
return 0;
return hleLogWarning(Log::HLE, 0, "UNIMPL");
}

static int sceNpDrmClearLicenseeKey() {
WARN_LOG(Log::HLE, "UNIMPL: sceNpDrmClearLicenseeKey()");
return 0;
return hleLogWarning(Log::HLE, 0, "UNIMPL");
}

static int sceNpDrmRenameCheck(const char *filename) {
WARN_LOG(Log::HLE, "UNIMPL: sceNpDrmRenameCheck(%s)", filename);
return 0;
return hleLogWarning(Log::HLE, 0, "UNIMPL");
}

static int sceNpDrmEdataSetupKey(u32 edataFd) {
INFO_LOG(Log::HLE, "sceNpDrmEdataSetupKey(%x)", edataFd);
int usec = 0;
// set PGD offset
int retval = __IoIoctl(edataFd, 0x04100002, 0x90, 0, 0, 0, usec);
if (retval != 0) {
return hleDelayResult(retval, "io ctrl command", usec);
return hleDelayResult(hleLogError(Log::HLE, retval), "io ctrl command", usec);
}
// call PGD open
// Note that usec accumulates.
retval = __IoIoctl(edataFd, 0x04100001, 0, 0, 0, 0, usec);
return hleDelayResult(retval, "io ctrl command", usec);
return hleDelayResult(hleLogSuccessInfoI(Log::HLE, retval), "io ctrl command", usec);
}

static int sceNpDrmEdataGetDataSize(u32 edataFd) {
INFO_LOG(Log::HLE, "sceNpDrmEdataGetDataSize %x", edataFd);
return sceIoIoctl(edataFd, 0x04100010, 0, 0, 0, 0);
int retval = hleCall(IoFileMgrForKernel, u32, sceIoIoctl, edataFd, 0x04100010, 0, 0, 0, 0);
return hleLogSuccessInfoI(Log::HLE, retval);
}

static int sceNpDrmOpen() {
Expand Down
2 changes: 1 addition & 1 deletion Core/SaveState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ namespace SaveState

void Rewind(Callback callback, void *cbUserData)
{
if (netInited) {
if (g_netInited) {
return;
}
if (coreState == CoreState::CORE_RUNTIME_ERROR)
Expand Down
4 changes: 2 additions & 2 deletions UI/EmuScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,7 @@ void EmuScreen::onVKey(int virtualKeyCode, bool down) {
break;
case VIRTKEY_TOGGLE_WLAN:
// Let's not allow the user to toggle wlan while connected, could get confusing.
if (down && !netInited) {
if (down && !g_netInited) {
auto n = GetI18NCategory(I18NCat::NETWORKING);
auto di = GetI18NCategory(I18NCat::DIALOG);
g_Config.bEnableWlan = !g_Config.bEnableWlan;
Expand Down Expand Up @@ -1884,7 +1884,7 @@ void EmuScreen::resized() {
}

bool MustRunBehind() {
return netInited || __NetAdhocConnected();
return IsNetworkConnected();
}

bool ShouldRunBehind() {
Expand Down
Loading

0 comments on commit d7aa236

Please sign in to comment.