Files
2025-09-11 19:54:25 -04:00

94 lines
4.1 KiB
C#

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 VoiceAppID: '{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.");
}
}