Decompiled source of AzuAutoStoreCompat v1.0.0

patchers/AzuAutoStoreCompat.Patcher.dll

Decompiled 5 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using Mono.Cecil;
using Mono.Cecil.Cil;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName = ".NET Standard 2.0")]
[assembly: AssemblyCompany("AzuAutoStoreCompat.Patcher")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("AzuAutoStoreCompat.Patcher")]
[assembly: AssemblyTitle("AzuAutoStoreCompat.Patcher")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 KRINJUN.AzuAutoStoreCompat.Patcher
{
	public static class Patcher
	{
		private static readonly ManualLogSource Logger = Logger.CreateLogSource("AzuAutoStoreCompat.Patcher");

		public static IEnumerable<string> TargetDLLs { get; } = new string[1] { "assembly_valheim.dll" };

		public static void Initialize()
		{
			Logger.LogInfo((object)"AzuAutoStoreCompat.Patcher initialized.");
		}

		public static void Patch(AssemblyDefinition assembly)
		{
			try
			{
				PatchZRoutedRpc(assembly);
				PatchInventory(assembly);
				PatchInventoryGrid(assembly);
				PatchItemData(assembly);
				PatchConsoleCommand(assembly);
				PatchCharacter(assembly);
				PatchEffectList(assembly);
			}
			catch (Exception arg)
			{
				Logger.LogError((object)$"Failed during preloader patch: {arg}");
			}
		}

		private static void PatchZRoutedRpc(AssemblyDefinition assembly)
		{
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0100: Unknown result type (might be due to invalid IL or missing references)
			TypeDefinition type = assembly.MainModule.GetType("ZRoutedRpc");
			if (type == null)
			{
				return;
			}
			FieldDefinition val = ((IEnumerable<FieldDefinition>)type.Fields).FirstOrDefault((Func<FieldDefinition, bool>)((FieldDefinition x) => ((MemberReference)x).Name == "Everybody"));
			if (val != null && val.IsLiteral)
			{
				val.Attributes = (FieldAttributes)(val.Attributes & 0xFFBF);
				val.Attributes = (FieldAttributes)(val.Attributes & 0x7FFF);
				val.Attributes = (FieldAttributes)(val.Attributes | 0x16);
				val.Constant = null;
				val.HasDefault = false;
				MethodDefinition val2 = ((IEnumerable<MethodDefinition>)type.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition m) => ((MemberReference)m).Name == ".cctor"));
				if (val2 != null && val2.HasBody)
				{
					ILProcessor iLProcessor = val2.Body.GetILProcessor();
					Instruction val3 = val2.Body.Instructions[0];
					iLProcessor.InsertBefore(val3, iLProcessor.Create(OpCodes.Ldc_I8, 0L));
					iLProcessor.InsertBefore(val3, iLProcessor.Create(OpCodes.Stsfld, (FieldReference)(object)val));
				}
				Logger.LogInfo((object)"Successfully patched ZRoutedRpc.Everybody from const literal to public static long.");
			}
		}

		private static void PatchInventory(AssemblyDefinition assembly)
		{
			//IL_008f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Expected O, but got Unknown
			//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bc: Expected O, but got Unknown
			//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e3: Expected O, but got Unknown
			//IL_0100: Unknown result type (might be due to invalid IL or missing references)
			//IL_010a: Expected O, but got Unknown
			//IL_0127: Unknown result type (might be due to invalid IL or missing references)
			//IL_0131: Expected O, but got Unknown
			//IL_013d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0148: Unknown result type (might be due to invalid IL or missing references)
			//IL_0153: Unknown result type (might be due to invalid IL or missing references)
			//IL_015e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0169: Unknown result type (might be due to invalid IL or missing references)
			//IL_0180: Unknown result type (might be due to invalid IL or missing references)
			//IL_018b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0196: Unknown result type (might be due to invalid IL or missing references)
			//IL_0235: Unknown result type (might be due to invalid IL or missing references)
			//IL_023c: Expected O, but got Unknown
			//IL_0249: Unknown result type (might be due to invalid IL or missing references)
			//IL_0254: Unknown result type (might be due to invalid IL or missing references)
			//IL_025f: Unknown result type (might be due to invalid IL or missing references)
			//IL_026a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0275: Unknown result type (might be due to invalid IL or missing references)
			TypeDefinition type = assembly.MainModule.GetType("Inventory");
			if (type == null)
			{
				return;
			}
			if (!((IEnumerable<MethodDefinition>)type.Methods).Any((MethodDefinition m) => ((MemberReference)m).Name == "AddItem" && ((MethodReference)m).Parameters.Count == 4))
			{
				MethodDefinition val = ((IEnumerable<MethodDefinition>)type.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition m) => ((MemberReference)m).Name == "AddItem" && ((MethodReference)m).Parameters.Count == 5));
				if (val != null)
				{
					MethodDefinition val2 = new MethodDefinition("AddItem", (MethodAttributes)134, assembly.MainModule.TypeSystem.Boolean);
					((MethodReference)val2).Parameters.Add(new ParameterDefinition("item", (ParameterAttributes)0, ((ParameterReference)((MethodReference)val).Parameters[0]).ParameterType));
					((MethodReference)val2).Parameters.Add(new ParameterDefinition("amount", (ParameterAttributes)0, ((ParameterReference)((MethodReference)val).Parameters[1]).ParameterType));
					((MethodReference)val2).Parameters.Add(new ParameterDefinition("x", (ParameterAttributes)0, ((ParameterReference)((MethodReference)val).Parameters[2]).ParameterType));
					((MethodReference)val2).Parameters.Add(new ParameterDefinition("y", (ParameterAttributes)0, ((ParameterReference)((MethodReference)val).Parameters[3]).ParameterType));
					ILProcessor iLProcessor = val2.Body.GetILProcessor();
					iLProcessor.Emit(OpCodes.Ldarg_0);
					iLProcessor.Emit(OpCodes.Ldarg_1);
					iLProcessor.Emit(OpCodes.Ldarg_2);
					iLProcessor.Emit(OpCodes.Ldarg_3);
					iLProcessor.Emit(OpCodes.Ldarg_S, ((MethodReference)val2).Parameters[3]);
					iLProcessor.Emit(OpCodes.Ldc_I4_M1);
					iLProcessor.Emit(OpCodes.Callvirt, (MethodReference)(object)val);
					iLProcessor.Emit(OpCodes.Ret);
					type.Methods.Add(val2);
					Logger.LogInfo((object)"Successfully injected Inventory.AddItem(ItemData, int, int, int) bridge.");
				}
			}
			if (!((IEnumerable<MethodDefinition>)type.Methods).Any((MethodDefinition m) => ((MemberReference)m).Name == "Changed" && ((MethodReference)m).Parameters.Count == 0))
			{
				MethodDefinition val3 = ((IEnumerable<MethodDefinition>)type.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition m) => ((MemberReference)m).Name == "Changed" && ((MethodReference)m).Parameters.Count == 2));
				if (val3 != null)
				{
					MethodDefinition val4 = new MethodDefinition("Changed", (MethodAttributes)134, assembly.MainModule.TypeSystem.Void);
					ILProcessor iLProcessor2 = val4.Body.GetILProcessor();
					iLProcessor2.Emit(OpCodes.Ldarg_0);
					iLProcessor2.Emit(OpCodes.Ldc_I4_1);
					iLProcessor2.Emit(OpCodes.Ldc_I4_1);
					iLProcessor2.Emit(OpCodes.Callvirt, (MethodReference)(object)val3);
					iLProcessor2.Emit(OpCodes.Ret);
					type.Methods.Add(val4);
					Logger.LogInfo((object)"Successfully injected Inventory.Changed() bridge.");
				}
			}
		}

		private static void PatchInventoryGrid(AssemblyDefinition assembly)
		{
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0062: Expected O, but got Unknown
			TypeDefinition type = assembly.MainModule.GetType("InventoryGrid");
			if (type != null && ((IEnumerable<TypeDefinition>)type.NestedTypes).FirstOrDefault((Func<TypeDefinition, bool>)((TypeDefinition t) => ((MemberReference)t).Name == "Element")) == null)
			{
				TypeDefinition val = new TypeDefinition("", "Element", (TypeAttributes)2, assembly.MainModule.TypeSystem.Object);
				type.NestedTypes.Add(val);
				Logger.LogInfo((object)"Successfully injected dummy nested class InventoryGrid/Element.");
			}
		}

		private static void PatchItemData(AssemblyDefinition assembly)
		{
			//IL_008f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Expected O, but got Unknown
			//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bc: Expected O, but got Unknown
			//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e3: Expected O, but got Unknown
			//IL_0100: Unknown result type (might be due to invalid IL or missing references)
			//IL_010a: Expected O, but got Unknown
			//IL_0127: Unknown result type (might be due to invalid IL or missing references)
			//IL_0131: Expected O, but got Unknown
			//IL_014e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0158: Expected O, but got Unknown
			//IL_0164: Unknown result type (might be due to invalid IL or missing references)
			//IL_016f: Unknown result type (might be due to invalid IL or missing references)
			//IL_017a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0185: Unknown result type (might be due to invalid IL or missing references)
			//IL_0190: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bd: Unknown result type (might be due to invalid IL or missing references)
			TypeDefinition type = assembly.MainModule.GetType("ItemDrop/ItemData");
			if (type != null && !((IEnumerable<MethodDefinition>)type.Methods).Any((MethodDefinition m) => ((MemberReference)m).Name == "GetTooltip" && ((MethodReference)m).Parameters.Count == 5))
			{
				MethodDefinition val = ((IEnumerable<MethodDefinition>)type.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition m) => ((MemberReference)m).Name == "GetTooltip" && ((MethodReference)m).Parameters.Count == 6));
				if (val != null)
				{
					MethodDefinition val2 = new MethodDefinition("GetTooltip", (MethodAttributes)150, assembly.MainModule.TypeSystem.String);
					((MethodReference)val2).Parameters.Add(new ParameterDefinition("item", (ParameterAttributes)0, ((ParameterReference)((MethodReference)val).Parameters[0]).ParameterType));
					((MethodReference)val2).Parameters.Add(new ParameterDefinition("qualityLevel", (ParameterAttributes)0, ((ParameterReference)((MethodReference)val).Parameters[1]).ParameterType));
					((MethodReference)val2).Parameters.Add(new ParameterDefinition("crafting", (ParameterAttributes)0, ((ParameterReference)((MethodReference)val).Parameters[2]).ParameterType));
					((MethodReference)val2).Parameters.Add(new ParameterDefinition("worldLevel", (ParameterAttributes)0, ((ParameterReference)((MethodReference)val).Parameters[3]).ParameterType));
					((MethodReference)val2).Parameters.Add(new ParameterDefinition("stackOverride", (ParameterAttributes)0, ((ParameterReference)((MethodReference)val).Parameters[4]).ParameterType));
					ILProcessor iLProcessor = val2.Body.GetILProcessor();
					iLProcessor.Emit(OpCodes.Ldarg_0);
					iLProcessor.Emit(OpCodes.Ldarg_1);
					iLProcessor.Emit(OpCodes.Ldarg_2);
					iLProcessor.Emit(OpCodes.Ldarg_3);
					iLProcessor.Emit(OpCodes.Ldarg_S, ((MethodReference)val2).Parameters[4]);
					iLProcessor.Emit(OpCodes.Ldc_I4_0);
					iLProcessor.Emit(OpCodes.Call, (MethodReference)(object)val);
					iLProcessor.Emit(OpCodes.Ret);
					type.Methods.Add(val2);
					Logger.LogInfo((object)"Successfully injected ItemDrop.ItemData.GetTooltip 5-param bridge.");
				}
			}
		}

		private static void PatchConsoleCommand(AssemblyDefinition assembly)
		{
			//IL_008f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Expected O, but got Unknown
			//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cc: Expected O, but got Unknown
			//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fc: Expected O, but got Unknown
			//IL_011a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0124: Expected O, but got Unknown
			//IL_0142: Unknown result type (might be due to invalid IL or missing references)
			//IL_014c: Expected O, but got Unknown
			//IL_016a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0174: Expected O, but got Unknown
			//IL_0180: Unknown result type (might be due to invalid IL or missing references)
			//IL_018b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0196: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01da: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_0208: Unknown result type (might be due to invalid IL or missing references)
			//IL_021f: Unknown result type (might be due to invalid IL or missing references)
			//IL_022a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0241: Unknown result type (might be due to invalid IL or missing references)
			//IL_0259: Unknown result type (might be due to invalid IL or missing references)
			//IL_0271: Unknown result type (might be due to invalid IL or missing references)
			//IL_0289: Unknown result type (might be due to invalid IL or missing references)
			//IL_0294: Unknown result type (might be due to invalid IL or missing references)
			TypeDefinition type = assembly.MainModule.GetType("Terminal/ConsoleCommand");
			if (type == null || ((IEnumerable<MethodDefinition>)type.Methods).Any((MethodDefinition m) => m.IsConstructor && ((MethodReference)m).Parameters.Count == 12))
			{
				return;
			}
			MethodDefinition val = ((IEnumerable<MethodDefinition>)type.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition m) => m.IsConstructor && ((MethodReference)m).Parameters.Count == 13 && ((MemberReference)((ParameterReference)((MethodReference)m).Parameters[2]).ParameterType).Name == "ConsoleEvent"));
			if (val != null)
			{
				MethodDefinition val2 = new MethodDefinition(".ctor", (MethodAttributes)6278, assembly.MainModule.TypeSystem.Void);
				for (int num = 0; num < 8; num++)
				{
					((MethodReference)val2).Parameters.Add(new ParameterDefinition(((ParameterReference)((MethodReference)val).Parameters[num]).Name, (ParameterAttributes)0, ((ParameterReference)((MethodReference)val).Parameters[num]).ParameterType));
				}
				((MethodReference)val2).Parameters.Add(new ParameterDefinition("optionsFetcher", (ParameterAttributes)0, ((ParameterReference)((MethodReference)val).Parameters[9]).ParameterType));
				((MethodReference)val2).Parameters.Add(new ParameterDefinition("alwaysRefreshTabOptions", (ParameterAttributes)0, ((ParameterReference)((MethodReference)val).Parameters[10]).ParameterType));
				((MethodReference)val2).Parameters.Add(new ParameterDefinition("remoteCommand", (ParameterAttributes)0, ((ParameterReference)((MethodReference)val).Parameters[11]).ParameterType));
				((MethodReference)val2).Parameters.Add(new ParameterDefinition("onlyAdmin", (ParameterAttributes)0, ((ParameterReference)((MethodReference)val).Parameters[12]).ParameterType));
				ILProcessor iLProcessor = val2.Body.GetILProcessor();
				iLProcessor.Emit(OpCodes.Ldarg_0);
				iLProcessor.Emit(OpCodes.Ldarg_1);
				iLProcessor.Emit(OpCodes.Ldarg_2);
				iLProcessor.Emit(OpCodes.Ldarg_3);
				iLProcessor.Emit(OpCodes.Ldarg_S, ((MethodReference)val2).Parameters[3]);
				iLProcessor.Emit(OpCodes.Ldarg_S, ((MethodReference)val2).Parameters[4]);
				iLProcessor.Emit(OpCodes.Ldarg_S, ((MethodReference)val2).Parameters[5]);
				iLProcessor.Emit(OpCodes.Ldarg_S, ((MethodReference)val2).Parameters[6]);
				iLProcessor.Emit(OpCodes.Ldarg_S, ((MethodReference)val2).Parameters[7]);
				iLProcessor.Emit(OpCodes.Ldc_I4_0);
				iLProcessor.Emit(OpCodes.Ldarg_S, ((MethodReference)val2).Parameters[8]);
				iLProcessor.Emit(OpCodes.Ldarg_S, ((MethodReference)val2).Parameters[9]);
				iLProcessor.Emit(OpCodes.Ldarg_S, ((MethodReference)val2).Parameters[10]);
				iLProcessor.Emit(OpCodes.Ldarg_S, ((MethodReference)val2).Parameters[11]);
				iLProcessor.Emit(OpCodes.Call, (MethodReference)(object)val);
				iLProcessor.Emit(OpCodes.Ret);
				type.Methods.Add(val2);
				Logger.LogInfo((object)"Successfully injected Terminal.ConsoleCommand 12-param constructor bridge.");
			}
		}

		private static void PatchCharacter(AssemblyDefinition assembly)
		{
			//IL_008f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Expected O, but got Unknown
			//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cc: Expected O, but got Unknown
			//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0101: Unknown result type (might be due to invalid IL or missing references)
			//IL_010c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0123: Unknown result type (might be due to invalid IL or missing references)
			//IL_012e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0139: Unknown result type (might be due to invalid IL or missing references)
			TypeDefinition type = assembly.MainModule.GetType("Character");
			if (type == null || ((IEnumerable<MethodDefinition>)type.Methods).Any((MethodDefinition m) => ((MemberReference)m).Name == "Message" && ((MethodReference)m).Parameters.Count == 4))
			{
				return;
			}
			MethodDefinition val = ((IEnumerable<MethodDefinition>)type.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition m) => ((MemberReference)m).Name == "Message" && ((MethodReference)m).Parameters.Count == 5));
			if (val != null)
			{
				MethodDefinition val2 = new MethodDefinition("Message", (MethodAttributes)134, assembly.MainModule.TypeSystem.Void);
				for (int num = 0; num < 4; num++)
				{
					((MethodReference)val2).Parameters.Add(new ParameterDefinition(((ParameterReference)((MethodReference)val).Parameters[num]).Name, (ParameterAttributes)0, ((ParameterReference)((MethodReference)val).Parameters[num]).ParameterType));
				}
				ILProcessor iLProcessor = val2.Body.GetILProcessor();
				iLProcessor.Emit(OpCodes.Ldarg_0);
				iLProcessor.Emit(OpCodes.Ldarg_1);
				iLProcessor.Emit(OpCodes.Ldarg_2);
				iLProcessor.Emit(OpCodes.Ldarg_3);
				iLProcessor.Emit(OpCodes.Ldarg_S, ((MethodReference)val2).Parameters[3]);
				iLProcessor.Emit(OpCodes.Ldc_I4_1);
				iLProcessor.Emit(OpCodes.Callvirt, (MethodReference)(object)val);
				iLProcessor.Emit(OpCodes.Ret);
				type.Methods.Add(val2);
				Logger.LogInfo((object)"Successfully injected Character.Message 4-param bridge.");
			}
		}

		private static void PatchEffectList(AssemblyDefinition assembly)
		{
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Expected O, but got Unknown
			//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c5: Expected O, but got Unknown
			//IL_0120: Unknown result type (might be due to invalid IL or missing references)
			//IL_012c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0138: Unknown result type (might be due to invalid IL or missing references)
			//IL_0144: Unknown result type (might be due to invalid IL or missing references)
			//IL_0150: Unknown result type (might be due to invalid IL or missing references)
			//IL_0168: Unknown result type (might be due to invalid IL or missing references)
			//IL_0192: Unknown result type (might be due to invalid IL or missing references)
			//IL_0183: Unknown result type (might be due to invalid IL or missing references)
			//IL_019e: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ab: Unknown result type (might be due to invalid IL or missing references)
			TypeDefinition type = assembly.MainModule.GetType("EffectList");
			if (type == null || ((IEnumerable<MethodDefinition>)type.Methods).Any((MethodDefinition m) => ((MemberReference)m).Name == "Create" && ((MethodReference)m).Parameters.Count == 5))
			{
				return;
			}
			MethodDefinition val = ((IEnumerable<MethodDefinition>)type.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition m) => ((MemberReference)m).Name == "Create" && ((MethodReference)m).Parameters.Count == 6));
			if (val != null)
			{
				MethodDefinition val2 = new MethodDefinition("Create", (MethodAttributes)134, ((MethodReference)val).ReturnType);
				for (int num = 0; num < 5; num++)
				{
					((MethodReference)val2).Parameters.Add(new ParameterDefinition(((ParameterReference)((MethodReference)val).Parameters[num]).Name, (ParameterAttributes)0, ((ParameterReference)((MethodReference)val).Parameters[num]).ParameterType));
				}
				TypeDefinition type2 = assembly.MainModule.GetType("ZDOID");
				FieldDefinition val3 = ((type2 != null) ? ((IEnumerable<FieldDefinition>)type2.Fields).FirstOrDefault((Func<FieldDefinition, bool>)((FieldDefinition f) => ((MemberReference)f).Name == "None")) : null);
				ILProcessor iLProcessor = val2.Body.GetILProcessor();
				iLProcessor.Emit(OpCodes.Ldarg_0);
				iLProcessor.Emit(OpCodes.Ldarg_1);
				iLProcessor.Emit(OpCodes.Ldarg_2);
				iLProcessor.Emit(OpCodes.Ldarg_3);
				iLProcessor.Emit(OpCodes.Ldarg_S, ((MethodReference)val2).Parameters[3]);
				iLProcessor.Emit(OpCodes.Ldarg_S, ((MethodReference)val2).Parameters[4]);
				if (val3 != null)
				{
					iLProcessor.Emit(OpCodes.Ldsfld, (FieldReference)(object)val3);
				}
				else
				{
					iLProcessor.Emit(OpCodes.Ldc_I4_0);
				}
				iLProcessor.Emit(OpCodes.Callvirt, (MethodReference)(object)val);
				iLProcessor.Emit(OpCodes.Ret);
				type.Methods.Add(val2);
				Logger.LogInfo((object)"Successfully injected EffectList.Create 5-param bridge.");
			}
		}
	}
}

plugins/AzuAutoStoreCompat.dll

Decompiled 5 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;

[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("AzuAutoStoreCompat")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("AzuAutoStoreCompat")]
[assembly: AssemblyTitle("AzuAutoStoreCompat")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 KRINJUN.AzuAutoStoreCompat
{
	[BepInPlugin("000_KRINJUN.AzuAutoStoreCompat", "AzuAutoStoreCompat", "1.0.2")]
	public class AzuAutoStoreCompatPlugin : BaseUnityPlugin
	{
		public const string ModGUID = "000_KRINJUN.AzuAutoStoreCompat";

		public const string ModName = "AzuAutoStoreCompat";

		public const string ModVersion = "1.0.2";

		internal static ManualLogSource Log;

		private static Harmony _harmony;

		private void Awake()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Expected O, but got Unknown
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			//IL_0087: Expected O, but got Unknown
			//IL_0121: Unknown result type (might be due to invalid IL or missing references)
			//IL_012f: Expected O, but got Unknown
			//IL_01b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c6: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			_harmony = new Harmony("000_KRINJUN.AzuAutoStoreCompat");
			Log.LogInfo((object)"AzuAutoStoreCompat initializing...");
			try
			{
				MethodInfo methodInfo = AccessTools.Method(typeof(Harmony), "PatchAll", new Type[1] { typeof(Assembly) }, (Type[])null);
				if (methodInfo != null)
				{
					MethodInfo methodInfo2 = AccessTools.Method(typeof(AzuAutoStoreCompatPlugin), "PatchAllPrefix", (Type[])null, (Type[])null);
					_harmony.Patch((MethodBase)methodInfo, new HarmonyMethod(methodInfo2), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
					Log.LogInfo((object)"Successfully hooked Harmony.PatchAll(Assembly) with selective patcher prefix.");
				}
				else
				{
					Log.LogError((object)"Could not find Harmony.PatchAll(Assembly) method to hook!");
				}
				MethodInfo methodInfo3 = AccessTools.Method(typeof(AccessTools), "DeclaredMethod", new Type[4]
				{
					typeof(Type),
					typeof(string),
					typeof(Type[]),
					typeof(Type[])
				}, (Type[])null);
				if (methodInfo3 != null)
				{
					MethodInfo methodInfo4 = AccessTools.Method(typeof(AzuAutoStoreCompatPlugin), "DeclaredMethodPrefix", (Type[])null, (Type[])null);
					_harmony.Patch((MethodBase)methodInfo3, new HarmonyMethod(methodInfo4), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
					Log.LogInfo((object)"Successfully hooked AccessTools.DeclaredMethod to disambiguate legacy bridge overloads.");
				}
				MethodInfo methodInfo5 = AccessTools.Method(typeof(AccessTools), "Method", new Type[4]
				{
					typeof(Type),
					typeof(string),
					typeof(Type[]),
					typeof(Type[])
				}, (Type[])null);
				if (methodInfo5 != null)
				{
					MethodInfo methodInfo6 = AccessTools.Method(typeof(AzuAutoStoreCompatPlugin), "MethodPrefix", (Type[])null, (Type[])null);
					_harmony.Patch((MethodBase)methodInfo5, new HarmonyMethod(methodInfo6), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
					Log.LogInfo((object)"Successfully hooked AccessTools.Method to disambiguate legacy bridge overloads.");
				}
			}
			catch (Exception arg)
			{
				Log.LogError((object)$"Error initializing AzuAutoStoreCompat: {arg}");
			}
		}

		public static bool DeclaredMethodPrefix(Type type, string name, Type[] parameters, Type[] generics, ref MethodInfo __result)
		{
			if (parameters == null && type != null && !string.IsNullOrEmpty(name))
			{
				if (type.Name == "Inventory" && name == "Changed")
				{
					MethodInfo methodInfo = type.GetMethods(AccessTools.allDeclared).FirstOrDefault((MethodInfo x) => x.Name == "Changed" && x.GetParameters().Length == 2);
					if (methodInfo != null)
					{
						__result = methodInfo;
						return false;
					}
				}
				if (type.Name == "Character" && name == "Message")
				{
					MethodInfo methodInfo2 = type.GetMethods(AccessTools.allDeclared).FirstOrDefault((MethodInfo x) => x.Name == "Message" && x.GetParameters().Length == 5);
					if (methodInfo2 != null)
					{
						__result = methodInfo2;
						return false;
					}
				}
				if (type.Name == "EffectList" && name == "Create")
				{
					MethodInfo methodInfo3 = type.GetMethods(AccessTools.allDeclared).FirstOrDefault((MethodInfo x) => x.Name == "Create" && x.GetParameters().Length == 6);
					if (methodInfo3 != null)
					{
						__result = methodInfo3;
						return false;
					}
				}
				try
				{
					type.GetMethod(name, AccessTools.allDeclared);
				}
				catch (AmbiguousMatchException)
				{
					MethodInfo methodInfo4 = (from m in type.GetMethods(AccessTools.allDeclared)
						where m.Name == name
						orderby m.GetParameters().Length descending
						select m).FirstOrDefault();
					if (methodInfo4 != null)
					{
						if (generics != null && generics.Length != 0 && methodInfo4.IsGenericMethodDefinition)
						{
							methodInfo4 = methodInfo4.MakeGenericMethod(generics);
						}
						__result = methodInfo4;
						ManualLogSource log = Log;
						if (log != null)
						{
							log.LogWarning((object)$"[AzuAutoStoreCompat] Disambiguated DeclaredMethod {type.Name}.{name} -> {methodInfo4}");
						}
						return false;
					}
				}
			}
			return true;
		}

		public static bool MethodPrefix(Type type, string name, Type[] parameters, Type[] generics, ref MethodInfo __result)
		{
			if (parameters == null && type != null && !string.IsNullOrEmpty(name))
			{
				if (type.Name == "Inventory" && name == "Changed")
				{
					MethodInfo methodInfo = type.GetMethods(AccessTools.all).FirstOrDefault((MethodInfo x) => x.Name == "Changed" && x.GetParameters().Length == 2);
					if (methodInfo != null)
					{
						__result = methodInfo;
						return false;
					}
				}
				if (type.Name == "Character" && name == "Message")
				{
					MethodInfo methodInfo2 = type.GetMethods(AccessTools.all).FirstOrDefault((MethodInfo x) => x.Name == "Message" && x.GetParameters().Length == 5);
					if (methodInfo2 != null)
					{
						__result = methodInfo2;
						return false;
					}
				}
				if (type.Name == "EffectList" && name == "Create")
				{
					MethodInfo methodInfo3 = type.GetMethods(AccessTools.all).FirstOrDefault((MethodInfo x) => x.Name == "Create" && x.GetParameters().Length == 6);
					if (methodInfo3 != null)
					{
						__result = methodInfo3;
						return false;
					}
				}
				try
				{
					type.GetMethod(name, AccessTools.all);
				}
				catch (AmbiguousMatchException)
				{
					MethodInfo methodInfo4 = (from m in type.GetMethods(AccessTools.all)
						where m.Name == name
						orderby m.GetParameters().Length descending
						select m).FirstOrDefault();
					if (methodInfo4 != null)
					{
						if (generics != null && generics.Length != 0 && methodInfo4.IsGenericMethodDefinition)
						{
							methodInfo4 = methodInfo4.MakeGenericMethod(generics);
						}
						__result = methodInfo4;
						ManualLogSource log = Log;
						if (log != null)
						{
							log.LogWarning((object)$"[AzuAutoStoreCompat] Disambiguated Method {type.Name}.{name} -> {methodInfo4}");
						}
						return false;
					}
				}
			}
			return true;
		}

		public static bool PatchAllPrefix(Harmony __instance, Assembly assembly)
		{
			if (assembly != null && assembly.GetName().Name == "AzuAutoStore")
			{
				SafePatchAll(__instance, assembly);
				return false;
			}
			return true;
		}

		public static void SafePatchAll(Harmony harmony, Assembly assembly)
		{
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Expected O, but got Unknown
			Log.LogInfo((object)("[AzuAutoStoreCompat] Intercepted PatchAll for " + assembly.GetName().Name + ". Performing safe selective patching..."));
			try
			{
				Type type = assembly.GetType("AzuAutoStore.AzuAutoStorePlugin");
				if (type != null)
				{
					MethodInfo methodInfo = AccessTools.Method(type, "Start", (Type[])null, (Type[])null);
					if (methodInfo != null)
					{
						MethodInfo methodInfo2 = AccessTools.Method(typeof(AzuAutoStoreCompatPlugin), "StartPrefix", (Type[])null, (Type[])null);
						harmony.Patch((MethodBase)methodInfo, new HarmonyMethod(methodInfo2), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
						Log.LogInfo((object)"[AzuAutoStoreCompat] Hooked AzuAutoStorePlugin.Start() with safe prefix.");
					}
				}
			}
			catch (Exception ex)
			{
				Log.LogWarning((object)("[AzuAutoStoreCompat] Could not hook AzuAutoStorePlugin.Start(): " + ex.Message));
			}
			Type[] array;
			try
			{
				array = assembly.GetTypes();
			}
			catch (ReflectionTypeLoadException ex2)
			{
				array = ex2.Types.Where((Type t) => t != null).ToArray();
			}
			int num = 0;
			int num2 = 0;
			Type[] array2 = array;
			foreach (Type type2 in array2)
			{
				if (type2.FullName != null && (type2.FullName.StartsWith("AzuAutoStore.Patches.Favoriting.") || type2.FullName.Contains("InventoryGridGetHoveredElementPatch")))
				{
					Log.LogInfo((object)("[AzuAutoStoreCompat] Skipping Favoriting patch type: " + type2.FullName));
					num2++;
					continue;
				}
				if (type2.Namespace != null && type2.Namespace.StartsWith("AzuAutoStore.APIs.MUC.MUCSrc.Patches"))
				{
					Log.LogInfo((object)("[AzuAutoStoreCompat] Skipping MUC patch type: " + type2.FullName));
					num2++;
					continue;
				}
				try
				{
					List<MethodInfo> list = harmony.CreateClassProcessor(type2).Patch();
					if (list != null && list.Count > 0)
					{
						Log.LogInfo((object)$"[AzuAutoStoreCompat] Successfully patched: {type2.FullName} ({list.Count} methods)");
						num++;
					}
				}
				catch (Exception ex3)
				{
					Log.LogWarning((object)("[AzuAutoStoreCompat] Skipped type " + type2.FullName + " (" + ex3.Message + ")"));
					num2++;
				}
			}
			Log.LogInfo((object)$"[AzuAutoStoreCompat] SafePatchAll finished. Successfully patched: {num}, Skipped: {num2}");
		}

		public static bool StartPrefix(object __instance)
		{
			try
			{
				FieldInfo fieldInfo = AccessTools.Field(__instance.GetType(), "BackpacksIsLoaded");
				if (fieldInfo != null)
				{
					bool flag = Chainloader.PluginInfos != null && Chainloader.PluginInfos.ContainsKey("org.bepinex.plugins.backpacks");
					fieldInfo.SetValue(null, flag);
				}
				Type type = AccessTools.TypeByName("AzuAutoStore.APIs.MUC.MUCCompat");
				if (type != null)
				{
					AccessTools.Method(type, "ForceEnableMUC", (Type[])null, (Type[])null)?.Invoke(null, new object[1] { true });
				}
				Log.LogInfo((object)"[AzuAutoStoreCompat] Executed safe Start() for AzuAutoStore (skipped broken Favoriting border initialization).");
			}
			catch (Exception arg)
			{
				Log.LogError((object)$"[AzuAutoStoreCompat] Error in StartPrefix: {arg}");
			}
			return false;
		}
	}
}