using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Cecil.Rocks;
using Mono.Collections.Generic;
using MonoMod.Cil;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("PEAKERRpcInfo")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyCopyright("Copyright © 2024")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("PEAKERRpcInfo")]
[assembly: AssemblyTitle("PEAKERRpcInfo")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
internal sealed class EmbeddedAttribute : Attribute
{
}
}
namespace System.Runtime.CompilerServices
{
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
internal sealed class RefSafetyRulesAttribute : Attribute
{
public readonly int Version;
public RefSafetyRulesAttribute(int P_0)
{
Version = P_0;
}
}
}
namespace PEAKERRpcInfo
{
public static class PEAKERRpcInfo
{
private record struct PatchedMethod(string OriginalFullName, MethodDefinition Method);
private static readonly ManualLogSource logger = Logger.CreateLogSource("PEAKERRpcInfo");
public static IEnumerable<string> TargetDLLs { get; } = new <>z__ReadOnlySingleElementList<string>("Assembly-CSharp.dll");
public static void Patch(AssemblyDefinition assembly)
{
//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
//IL_00a9: Expected O, but got Unknown
//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
//IL_00e0: Expected O, but got Unknown
//IL_01fe: Unknown result type (might be due to invalid IL or missing references)
//IL_0203: Unknown result type (might be due to invalid IL or missing references)
//IL_020c: Expected O, but got Unknown
TypeReference photonMessageInfoTypeRef = default(TypeReference);
if (!assembly.MainModule.TryGetTypeReference("Photon.Pun.PhotonMessageInfo", ref photonMessageInfoTypeRef))
{
logger.LogFatal((object)"Photon.Pun.PhotonMessageInfo type not found.");
return;
}
List<PatchedMethod> patchedMethods = new List<PatchedMethod>();
PatchAssemblyRpcParams(assembly, photonMessageInfoTypeRef, ref patchedMethods);
PatchAssemblyRpcInvokes(assembly, photonMessageInfoTypeRef, importTypeRef: false, patchedMethods, out var _);
foreach (string item in Directory.EnumerateFiles(Paths.PluginPath, "*.dll", SearchOption.AllDirectories))
{
if (Path.GetFileName(item).Contains("Assembly-CSharp"))
{
continue;
}
try
{
DefaultAssemblyResolver val = new DefaultAssemblyResolver();
try
{
((BaseAssemblyResolver)val).AddSearchDirectory(Path.GetDirectoryName(item));
((BaseAssemblyResolver)val).AddSearchDirectory(Paths.ManagedPath);
ReaderParameters val2 = new ReaderParameters
{
AssemblyResolver = (IAssemblyResolver)(object)val,
ReadWrite = false,
ReadSymbols = false
};
using MemoryStream memoryStream = new MemoryStream(File.ReadAllBytes(item));
AssemblyDefinition val3 = AssemblyDefinition.ReadAssembly((Stream)memoryStream, val2);
if (!((IEnumerable<AssemblyNameReference>)val3.MainModule.AssemblyReferences).Any((AssemblyNameReference pluginName) => pluginName.FullName == assembly.FullName))
{
logger.LogDebug((object)("No Assembly-CSharp reference in " + item));
continue;
}
if (!val3.MainModule.GetMemberReferences().Any((MemberReference memberRef) => patchedMethods.Any((PatchedMethod patchedMethod) => memberRef.FullName == patchedMethod.OriginalFullName)))
{
logger.LogDebug((object)("No patched method references in " + item));
continue;
}
logger.LogDebug((object)("Patching " + item + "..."));
PatchAssemblyRpcInvokes(val3, photonMessageInfoTypeRef, importTypeRef: true, patchedMethods, out var patched2);
if (patched2)
{
if (File.Exists(item + "-unrpcpatched.old"))
{
File.Delete(item + "-unrpcpatched.old");
}
File.Move(item, item + "-unrpcpatched.old");
WriterParameters val4 = new WriterParameters
{
WriteSymbols = false
};
val3.Write(item, val4);
logger.LogInfo((object)("Patched " + item));
}
else
{
logger.LogDebug((object)("Nothing to patch in " + item));
}
}
finally
{
((IDisposable)val)?.Dispose();
}
}
catch (Exception arg)
{
logger.LogError((object)$"Failed to patch {item}: {arg}");
}
}
}
private static void PatchAssemblyRpcParams(AssemblyDefinition assembly, TypeReference photonMessageInfoTypeRef, ref List<PatchedMethod> patchedMethods)
{
//IL_0021: Unknown result type (might be due to invalid IL or missing references)
//IL_0026: Unknown result type (might be due to invalid IL or missing references)
//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
//IL_00c1: Expected O, but got Unknown
foreach (TypeDefinition type in assembly.MainModule.GetTypes())
{
Enumerator<MethodDefinition> enumerator2 = type.Methods.GetEnumerator();
try
{
while (enumerator2.MoveNext())
{
MethodDefinition current = enumerator2.Current;
try
{
if (current.HasBody && current.HasCustomAttributes && ((IEnumerable<CustomAttribute>)current.CustomAttributes).Any((CustomAttribute attribute) => ((object)attribute.AttributeType).ToString() == "Photon.Pun.PunRPC") && !((IEnumerable<ParameterDefinition>)((MethodReference)current).Parameters).Any((ParameterDefinition parameter) => ((object)((ParameterReference)parameter).ParameterType).ToString() == "Photon.Pun.PhotonMessageInfo"))
{
string fullName = ((MemberReference)current).FullName;
((MethodReference)current).Parameters.Add(new ParameterDefinition("info", (ParameterAttributes)16, photonMessageInfoTypeRef));
logger.LogDebug((object)("Added Photon.Pun.PhotonMessageInfo info parameter onto " + ((MemberReference)current).FullName));
patchedMethods.Add(new PatchedMethod(fullName, current));
}
}
catch (Exception arg)
{
logger.LogError((object)$"An exception occured adding to the parameters of {((MemberReference)current).FullName}:\n{arg}");
}
}
}
finally
{
((IDisposable)enumerator2).Dispose();
}
}
}
private static void PatchAssemblyRpcInvokes(AssemblyDefinition assembly, TypeReference photonMessageInfoTypeRef, bool importTypeRef, List<PatchedMethod> methodsToPatch, out bool patched)
{
//IL_0025: Unknown result type (might be due to invalid IL or missing references)
//IL_002a: Unknown result type (might be due to invalid IL or missing references)
//IL_005a: Unknown result type (might be due to invalid IL or missing references)
//IL_005f: Unknown result type (might be due to invalid IL or missing references)
//IL_015d: Unknown result type (might be due to invalid IL or missing references)
//IL_0164: Expected O, but got Unknown
//IL_0097: Unknown result type (might be due to invalid IL or missing references)
//IL_009c: Unknown result type (might be due to invalid IL or missing references)
//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
//IL_00af: Unknown result type (might be due to invalid IL or missing references)
//IL_01c1: Unknown result type (might be due to invalid IL or missing references)
//IL_01d8: Unknown result type (might be due to invalid IL or missing references)
//IL_01ee: Unknown result type (might be due to invalid IL or missing references)
//IL_0210: Unknown result type (might be due to invalid IL or missing references)
patched = false;
foreach (TypeDefinition type in assembly.MainModule.GetTypes())
{
Enumerator<MethodDefinition> enumerator2 = type.Methods.GetEnumerator();
try
{
while (enumerator2.MoveNext())
{
MethodDefinition current = enumerator2.Current;
if (((current != null) ? current.Body : null) == null)
{
continue;
}
List<Instruction> list = new List<Instruction>();
try
{
Enumerator<Instruction> enumerator3 = current.Body.Instructions.GetEnumerator();
try
{
while (enumerator3.MoveNext())
{
Instruction current2 = enumerator3.Current;
foreach (PatchedMethod item in methodsToPatch)
{
if (ILPatternMatchingExt.MatchCallOrCallvirt(current2, (MethodReference)(object)item.Method) || ((current2.OpCode == OpCodes.Call || current2.OpCode == OpCodes.Callvirt) && ((MemberReference)/*isinst with value type is only supported in some contexts*/).FullName == item.OriginalFullName))
{
list.Add(current2);
current2.Operand = assembly.MainModule.ImportReference((MethodReference)(object)item.Method);
break;
}
}
}
}
finally
{
((IDisposable)enumerator3).Dispose();
}
if (list.Count == 0)
{
continue;
}
if (!patched)
{
patched = true;
if (importTypeRef)
{
photonMessageInfoTypeRef = assembly.MainModule.ImportReference(photonMessageInfoTypeRef);
}
}
VariableDefinition val = new VariableDefinition(photonMessageInfoTypeRef);
current.Body.Variables.Add(val);
logger.LogDebug((object)("Added Photon.Pun.PhotonMessageInfo variable to " + ((MemberReference)current).FullName));
MethodBodyRocks.SimplifyMacros(current.Body);
ILProcessor iLProcessor = current.Body.GetILProcessor();
foreach (Instruction item2 in list)
{
iLProcessor.InsertBefore(item2, iLProcessor.Create(OpCodes.Ldloca_S, val));
iLProcessor.InsertBefore(item2, iLProcessor.Create(OpCodes.Initobj, photonMessageInfoTypeRef));
iLProcessor.InsertBefore(item2, iLProcessor.Create(OpCodes.Ldloc_S, val));
logger.LogDebug((object)("Inserted default(Photon.Pun.PhotonMessageInfo) before call to " + ((MemberReference)(MethodReference)item2.Operand).FullName));
}
}
catch (Exception arg)
{
logger.LogError((object)$"An exception occured inserting default(Photon.Pun.PhotonMessageInfo) into {((MemberReference)current).FullName}:\n{arg}");
}
finally
{
if (list.Count != 0)
{
MethodBodyRocks.OptimizeMacros(current.Body);
}
}
}
}
finally
{
((IDisposable)enumerator2).Dispose();
}
}
}
}
}
[CompilerGenerated]
internal sealed class <>z__ReadOnlySingleElementList<T> : IEnumerable, ICollection, IList, IEnumerable<T>, IReadOnlyCollection<T>, IReadOnlyList<T>, ICollection<T>, IList<T>
{
private sealed class Enumerator : IDisposable, IEnumerator, IEnumerator<T>
{
object IEnumerator.Current => _item;
T IEnumerator<T>.Current => _item;
public Enumerator(T item)
{
_item = item;
}
bool IEnumerator.MoveNext()
{
if (!_moveNextCalled)
{
return _moveNextCalled = true;
}
return false;
}
void IEnumerator.Reset()
{
_moveNextCalled = false;
}
void IDisposable.Dispose()
{
}
}
int ICollection.Count => 1;
bool ICollection.IsSynchronized => false;
object ICollection.SyncRoot => this;
object IList.this[int index]
{
get
{
if (index != 0)
{
throw new IndexOutOfRangeException();
}
return _item;
}
set
{
throw new NotSupportedException();
}
}
bool IList.IsFixedSize => true;
bool IList.IsReadOnly => true;
int IReadOnlyCollection<T>.Count => 1;
T IReadOnlyList<T>.this[int index]
{
get
{
if (index != 0)
{
throw new IndexOutOfRangeException();
}
return _item;
}
}
int ICollection<T>.Count => 1;
bool ICollection<T>.IsReadOnly => true;
T IList<T>.this[int index]
{
get
{
if (index != 0)
{
throw new IndexOutOfRangeException();
}
return _item;
}
set
{
throw new NotSupportedException();
}
}
public <>z__ReadOnlySingleElementList(T item)
{
_item = item;
}
IEnumerator IEnumerable.GetEnumerator()
{
return new Enumerator(_item);
}
void ICollection.CopyTo(Array array, int index)
{
array.SetValue(_item, index);
}
int IList.Add(object value)
{
throw new NotSupportedException();
}
void IList.Clear()
{
throw new NotSupportedException();
}
bool IList.Contains(object value)
{
return EqualityComparer<T>.Default.Equals(_item, (T)value);
}
int IList.IndexOf(object value)
{
if (!EqualityComparer<T>.Default.Equals(_item, (T)value))
{
return -1;
}
return 0;
}
void IList.Insert(int index, object value)
{
throw new NotSupportedException();
}
void IList.Remove(object value)
{
throw new NotSupportedException();
}
void IList.RemoveAt(int index)
{
throw new NotSupportedException();
}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return new Enumerator(_item);
}
void ICollection<T>.Add(T item)
{
throw new NotSupportedException();
}
void ICollection<T>.Clear()
{
throw new NotSupportedException();
}
bool ICollection<T>.Contains(T item)
{
return EqualityComparer<T>.Default.Equals(_item, item);
}
void ICollection<T>.CopyTo(T[] array, int arrayIndex)
{
array[arrayIndex] = _item;
}
bool ICollection<T>.Remove(T item)
{
throw new NotSupportedException();
}
int IList<T>.IndexOf(T item)
{
if (!EqualityComparer<T>.Default.Equals(_item, item))
{
return -1;
}
return 0;
}
void IList<T>.Insert(int index, T item)
{
throw new NotSupportedException();
}
void IList<T>.RemoveAt(int index)
{
throw new NotSupportedException();
}
}