Internals rewritten. PHoton config can now be fetched from the server, and if it fails, fallback to the local configuration.

This commit is contained in:
2025-08-16 21:04:26 -04:00
parent 8ca37170e4
commit 087d9a900e
20 changed files with 393 additions and 234 deletions

View File

@@ -0,0 +1,18 @@
using System.Reflection;
using HarmonyLib;
using undead_universal_patch_il2cpp.Core;
namespace undead_universal_patch_il2cpp.Patches.Photon;
[HarmonyPatch]
public class NameserverTest
{
static PatchTypesResult patchTypesResult = Util.PreparePatchTypes(
"Photon nameserver test",
"NetworkingPeer",
"GetNameServerAddress"
);
static bool Prepare() => patchTypesResult.Success;
static MethodBase TargetMethod() => patchTypesResult.Method;
}

View File

@@ -0,0 +1,108 @@
using System.Reflection;
using HarmonyLib;
using undead_universal_patch_il2cpp.Core;
using undead_universal_patch_il2cpp.Core.Config;
using undead_universal_patch_il2cpp.Core.Content.CustomRecNet.CustomPhoton;
namespace undead_universal_patch_il2cpp.Patches.Photon;
[HarmonyPatch]
public class ForceSelfHostedPhoton
{
class HarmonyState
{
public object code;
}
static PatchTypesResult patchResult = Util.ConfigsPreparePatchTypes(
[
PhotonConfig.PatchPhotonIds,
PhotonConfig.SelfHosted
],
"Force JoinOrCreateRoom when connected to master",
"PUNNetworkManager",
"OnConnectedToMaster"
);
static bool Prepare() => patchResult.Success;
static MethodBase TargetMethod() => patchResult.Method;
static void Prefix(ref PUNNetworkManager __instance, ref HarmonyState __state)
{
if (ServerPatchesConfig.CustomPhoton.Value && !CustomPhoton.ServerConfigFailed)
{
Util.ConditionalDebug("Skipping Selfhost Photon target server regionId roundtrip, CustomPhoton is enabled.");
return;
}
__state = new HarmonyState();
PropertyInfo targetGameSessionProperty = __instance.GetType().GetRuntimeProperty("targetGameSession");
if (targetGameSessionProperty == null)
{
UniversalPatchPlugin.Log.LogFatal("Cannot force masterserver: targetGameSessionProperty was null.");
return;
}
var targetGameSession = targetGameSessionProperty.GetValue(__instance);
if (targetGameSession == null)
{
UniversalPatchPlugin.Log.LogFatal("Cannot force masterserver: targetGameSession was null.");
return;
}
PropertyInfo photonRegionIdProperty = targetGameSession.GetType().GetRuntimeProperty("PhotonRegionId");
if (photonRegionIdProperty == null)
{
UniversalPatchPlugin.Log.LogFatal("Cannot force masterserver: photonRegionIdProperty was null.");
return;
}
__state.code = photonRegionIdProperty.GetValue(targetGameSession);
photonRegionIdProperty.SetValue(targetGameSession, 4);
Util.ConditionalDebug("Selfhost Photon target server regionId pre-roundtrip");
}
static void Postfix(ref PUNNetworkManager __instance, ref HarmonyState __state)
{
PropertyInfo targetGameSessionProperty = __instance.GetType().GetRuntimeProperty("targetGameSession");
if (targetGameSessionProperty == null)
{
UniversalPatchPlugin.Log.LogFatal("Cannot force masterserver postfix: targetGameSessionProperty was null.");
return;
}
var targetGameSession = targetGameSessionProperty.GetValue(__instance);
if (targetGameSession == null)
{
UniversalPatchPlugin.Log.LogFatal("Cannot force masterserver postfix: targetGameSession was null.");
return;
}
PropertyInfo photonRegionIdProperty = targetGameSession.GetType().GetRuntimeProperty("PhotonRegionId");
if (photonRegionIdProperty == null)
{
UniversalPatchPlugin.Log.LogFatal("Cannot force masterserver: photonRegionIdProperty was null.");
return;
}
photonRegionIdProperty.SetValue(targetGameSession, __state.code);
Util.ConditionalDebug("Selfhost Photon target server regionId post-roundtrip");
}
}
// Fixes some reflection used by Rec Room that, when TCP is used in the Photon patch config,
// incorrectly assumes that ENetPeer (UDP) is in use
// Allows TPeer (TCP) to be specified by PhotonConfig.ConnectionProtocol
[HarmonyPatch]
public class PhotonThrottlingPatch
{
static PatchTypesResult patchTypesResult = Util.ConfigPreparePatchTypes(
PhotonConfig.SelfHosted,
"ENet incorrect assumption patch",
"PUNNetworkManager",
"UpdatePhotonThrottling"
);
static bool Prepare() => patchTypesResult.Success;
static MethodBase TargetMethod() => patchTypesResult.Method;
static bool Prefix() => PhotonConfig.ConnectionProtocol.Value != 1;
}

View File

@@ -0,0 +1,93 @@
using System;
using System.Reflection;
using HarmonyLib;
using undead_universal_patch_il2cpp.Core;
using undead_universal_patch_il2cpp.Core.Config;
using undead_universal_patch_il2cpp.Core.Content.CustomRecNet.CustomPhoton;
namespace undead_universal_patch_il2cpp.Patches.Photon;
public class PhotonPatch
{
public static void Patch(PhotonConfigDTO photonConfig)
{
// It's fine to reference enums after we're pretty sure the relevant types exist
// i think
Type photonNetworkType = AccessTools.TypeByName("PhotonNetwork");
Type serverSettingsType = AccessTools.TypeByName("ServerSettings");
if (photonNetworkType == null || serverSettingsType == null)
{
UniversalPatchPlugin.Log.LogError("Cannot patch Photon: PhotonNetwork or ServerSettings types were not found. Is this build supported?");
return;
}
PropertyInfo photonServerSettingsProperty = photonNetworkType.GetRuntimeProperty("PhotonServerSettings");
object serverSettings = photonServerSettingsProperty.GetValue(serverSettingsType);
if (PhotonConfig.PunLogging.Value)
{
PropertyInfo loggingProperty = serverSettingsType.GetRuntimeProperty("PunLogging");
loggingProperty.SetValue(serverSettings, PhotonLogLevel.Full);
PropertyInfo networkLoggingProperty = serverSettingsType.GetRuntimeProperty("NetworkLogging");
networkLoggingProperty.SetValue(serverSettings, ExitGames.Client.Photon.DebugLevel.ALL);
}
PropertyInfo appIdProperty = serverSettingsType.GetRuntimeProperty("AppID");
PropertyInfo voiceAppIdProperty = serverSettingsType.GetRuntimeProperty("VoiceAppID");
Util.ConditionalDebug($"New Photon AppID: '{photonConfig.AppID}'");
Util.ConditionalDebug($"New Photon AppID: '{photonConfig.VoiceAppID}'");
appIdProperty.SetValue(serverSettings, photonConfig.AppID);
voiceAppIdProperty.SetValue(serverSettings, photonConfig.VoiceAppID);
if (photonConfig.SelfHosted)
{
MethodInfo useMyServerMethod = serverSettingsType.GetMethod("UseMyServer");
useMyServerMethod.Invoke(serverSettings, [
photonConfig.ServerAddress,
photonConfig.ServerPort,
photonConfig.AppID,
]);
Type authValuesType = AccessTools.TypeByName("AuthenticationValues");
if (authValuesType != null)
{
PropertyInfo networkingPeerProperty = photonNetworkType.GetRuntimeProperty("networkingPeer");
if (networkingPeerProperty == null)
{
UniversalPatchPlugin.Log.LogError("Cannot continue to patch Photon: networkingPeerProperty was null. Is this build supported?");
return;
}
if (networkingPeerProperty.GetValue(null) == null)
{
UniversalPatchPlugin.Log.LogError("Cannot continue to patch Photon: networkingPeerInstance was null. Is this build supported?");
return;
}
int id = RecNetUtil.GetAccountId();
PhotonNetwork.AuthValues = new AuthenticationValues
{
UserId = id.ToString()
};
Util.ConditionalDebug($"Set the authValues userId to {id}");
}
Type connectionProtocolType = AccessTools.TypeByName("ExitGames.Client.Photon.ConnectionProtocol");
PropertyInfo protocolProp = serverSettingsType.GetRuntimeProperty("Protocol");
if (connectionProtocolType != null && protocolProp != null)
{
protocolProp.SetValue(serverSettings, photonConfig.ConnectionProtocol);
}
else
{
UniversalPatchPlugin.Log.LogError("Cannot continue to patch Photon: connectionProtocolType or protocolProp was null");
return;
}
}
UniversalPatchPlugin.Log.LogInfo($"Photon patch ({(photonConfig.SelfHosted ? "self-hosted" : "cloud")}) succeeded.");
}
}