Decompiled source of GrizzCompany Compat v1.0.0

patchers\GrizzCompanyCompat.Patcher.dll

Decompiled 12 hours ago
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx.Logging;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Collections.Generic;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace GrizzCompanyCompat;

public static class Patcher
{
	public static IEnumerable<string> TargetDLLs => new string[1] { "Assembly-CSharp.dll" };

	public static void Patch(AssemblyDefinition assembly)
	{
		try
		{
			string text = Apply(assembly);
			Logger.CreateLogSource("GrizzCompanyCompat").LogInfo((object)text);
		}
		catch (Exception ex)
		{
			Logger.CreateLogSource("GrizzCompanyCompat").LogError((object)("Compatibility shim skipped: " + ex));
		}
	}

	public static string Apply(AssemblyDefinition assembly)
	{
		//IL_0059: Unknown result type (might be due to invalid IL or missing references)
		//IL_005e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0156: Unknown result type (might be due to invalid IL or missing references)
		//IL_015b: Unknown result type (might be due to invalid IL or missing references)
		//IL_016b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0170: Unknown result type (might be due to invalid IL or missing references)
		//IL_0174: Unknown result type (might be due to invalid IL or missing references)
		//IL_0179: Unknown result type (might be due to invalid IL or missing references)
		//IL_017b: Unknown result type (might be due to invalid IL or missing references)
		//IL_017f: Invalid comparison between Unknown and I4
		//IL_01aa: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ae: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c0: Expected I4, but got Unknown
		//IL_0181: Unknown result type (might be due to invalid IL or missing references)
		//IL_019c: Expected I4, but got Unknown
		//IL_01c0: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c4: Invalid comparison between Unknown and I4
		//IL_019c: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a0: Invalid comparison between Unknown and I4
		//IL_01c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_01cd: Invalid comparison between Unknown and I4
		//IL_02c4: Unknown result type (might be due to invalid IL or missing references)
		//IL_02c9: Unknown result type (might be due to invalid IL or missing references)
		//IL_038b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0392: Expected O, but got Unknown
		//IL_03aa: Unknown result type (might be due to invalid IL or missing references)
		//IL_03b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_03c2: Unknown result type (might be due to invalid IL or missing references)
		//IL_03ce: Unknown result type (might be due to invalid IL or missing references)
		//IL_03db: Unknown result type (might be due to invalid IL or missing references)
		if (((AssemblyNameReference)assembly.Name).Name != "Assembly-CSharp")
		{
			throw new InvalidOperationException("Unexpected assembly.");
		}
		TypeDefinition type = assembly.MainModule.GetType("CompatibleNoun");
		if (type == null || ((TypeReference)type).IsValueType || type.IsAbstract)
		{
			throw new InvalidOperationException("CompatibleNoun is not the expected class.");
		}
		MethodDefinition val = null;
		Enumerator<MethodDefinition> enumerator = type.Methods.GetEnumerator();
		try
		{
			while (enumerator.MoveNext())
			{
				MethodDefinition current = enumerator.Current;
				if (!current.IsConstructor || current.IsStatic)
				{
					continue;
				}
				if (((MethodReference)current).Parameters.Count == 0)
				{
					if (!current.IsPublic)
					{
						throw new InvalidOperationException("Existing constructor is not public.");
					}
					return "SKIPPED: public CompatibleNoun() already exists.";
				}
				if (current.IsPublic && ((MethodReference)current).Parameters.Count == 2 && ((MemberReference)((ParameterReference)((MethodReference)current).Parameters[0]).ParameterType).FullName == "TerminalKeyword" && ((MemberReference)((ParameterReference)((MethodReference)current).Parameters[1]).ParameterType).FullName == "TerminalNode")
				{
					if (val != null)
					{
						throw new InvalidOperationException("Ambiguous constructor.");
					}
					val = current;
				}
			}
		}
		finally
		{
			((IDisposable)enumerator/*cast due to .constrained prefix*/).Dispose();
		}
		if (val == null || !val.HasBody)
		{
			throw new InvalidOperationException("Expected new constructor is missing.");
		}
		Enumerator<Instruction> enumerator2 = val.Body.Instructions.GetEnumerator();
		try
		{
			while (enumerator2.MoveNext())
			{
				Instruction current2 = enumerator2.Current;
				OpCode opCode = current2.OpCode;
				Code code = ((OpCode)(ref opCode)).Code;
				if ((int)code <= 14)
				{
					switch ((int)code)
					{
					default:
						if ((int)code == 14)
						{
							continue;
						}
						break;
					case 1:
						break;
					case 0:
					case 2:
					case 3:
					case 4:
						continue;
					}
				}
				else
				{
					switch (code - 39)
					{
					default:
					{
						if ((int)code != 122)
						{
							if ((int)code == 199)
							{
								continue;
							}
							break;
						}
						object operand2 = current2.Operand;
						FieldReference val3 = (FieldReference)((operand2 is FieldReference) ? operand2 : null);
						if (val3 != null && !(((MemberReference)((MemberReference)val3).DeclaringType).FullName != ((MemberReference)type).FullName))
						{
							continue;
						}
						throw new InvalidOperationException("Unexpected constructor field: " + current2);
					}
					case 0:
					{
						object operand = current2.Operand;
						MethodReference val2 = (MethodReference)((operand is MethodReference) ? operand : null);
						if (val2 != null && !(((MemberReference)val2).FullName != "System.Void System.Object::.ctor()"))
						{
							continue;
						}
						throw new InvalidOperationException("Unexpected constructor call: " + current2);
					}
					case 1:
						break;
					case 2:
						continue;
					}
				}
				throw new InvalidOperationException("Unsafe null-placeholder constructor: " + current2);
			}
		}
		finally
		{
			((IDisposable)enumerator2/*cast due to .constrained prefix*/).Dispose();
		}
		if (val.Body.ExceptionHandlers.Count != 0)
		{
			throw new InvalidOperationException("Unexpected exception handlers.");
		}
		string[] array = new string[2] { "noun", "result" };
		foreach (string text in array)
		{
			bool flag = false;
			Enumerator<FieldDefinition> enumerator3 = type.Fields.GetEnumerator();
			try
			{
				while (enumerator3.MoveNext())
				{
					FieldDefinition current3 = enumerator3.Current;
					if (((MemberReference)current3).Name == text && current3.IsPublic && !current3.IsStatic && !current3.IsInitOnly && ((MemberReference)((FieldReference)current3).FieldType).FullName == ((text == "noun") ? "TerminalKeyword" : "TerminalNode"))
					{
						flag = true;
					}
				}
			}
			finally
			{
				((IDisposable)enumerator3/*cast due to .constrained prefix*/).Dispose();
			}
			if (!flag)
			{
				throw new InvalidOperationException("Legacy writable field missing: " + text);
			}
		}
		MethodDefinition val4 = new MethodDefinition(".ctor", (MethodAttributes)6278, assembly.MainModule.TypeSystem.Void);
		((MethodReference)val4).HasThis = true;
		ILProcessor iLProcessor = val4.Body.GetILProcessor();
		iLProcessor.Emit(OpCodes.Ldarg_0);
		iLProcessor.Emit(OpCodes.Ldnull);
		iLProcessor.Emit(OpCodes.Ldnull);
		iLProcessor.Emit(OpCodes.Call, (MethodReference)(object)val);
		iLProcessor.Emit(OpCodes.Ret);
		val4.Body.MaxStackSize = 3;
		type.Methods.Add(val4);
		return "PATCHED: restored CompatibleNoun() in memory. Original game and mod files unchanged.";
	}
}