Decompiled source of AzuModsValheim1Compat v1.0.7

patchers/AzuModsValheim1Compat/AzuModsValheim1Compat.dll

Decompiled 2 weeks ago
using System;
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.Text;
using BepInEx;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Collections.Generic;

[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("Aborigen")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("BepInEx Preloader compatibility patch for Azumatt mods on Valheim 1.0.7")]
[assembly: AssemblyFileVersion("1.0.7.0")]
[assembly: AssemblyInformationalVersion("1.0.7+d8b7c73381175a1313c2e90994c26cba4bd9ecd3")]
[assembly: AssemblyProduct("AzuModsValheim1Compat")]
[assembly: AssemblyTitle("AzuModsValheim1Compat")]
[assembly: AssemblyVersion("1.0.7.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 AzuModsValheim1Compat
{
	public static class Patcher
	{
		private static readonly ManualLogSource Log = Logger.CreateLogSource("AzuModsValheim1Compat");

		private static bool _pluginsPatched = false;

		private static bool _resolverInstalled = false;

		public static IEnumerable<string> TargetDLLs
		{
			get
			{
				InstallAssemblyResolver();
				ExecutePluginPatch();
				return new string[1] { "assembly_valheim.dll" };
			}
		}

		public static void Initialize()
		{
			InstallAssemblyResolver();
			ExecutePluginPatch();
		}

		public static void Patch(AssemblyDefinition assembly)
		{
			if (assembly == null || ((AssemblyNameReference)assembly.Name).Name != "assembly_valheim")
			{
				return;
			}
			try
			{
				ExecutePluginPatch();
				Log.LogInfo((object)"Applying Valheim 1.0.7 API bridge methods to assembly_valheim...");
				InjectBridges(assembly);
				Log.LogInfo((object)"Valheim 1.0.7 API bridges injected successfully!");
			}
			catch (Exception ex)
			{
				Log.LogError((object)("Failed to inject API bridges into assembly_valheim: " + ex.Message + "\n" + ex.StackTrace));
			}
		}

		private static void InjectBridges(AssemblyDefinition assembly)
		{
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_0141: Unknown result type (might be due to invalid IL or missing references)
			//IL_0158: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00db: Expected O, but got Unknown
			//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f5: 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)
			//IL_0200: Unknown result type (might be due to invalid IL or missing references)
			//IL_0207: Expected O, but got Unknown
			//IL_036a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0371: Expected O, but got Unknown
			//IL_063c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0643: Expected O, but got Unknown
			//IL_0650: Unknown result type (might be due to invalid IL or missing references)
			//IL_065b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0666: Unknown result type (might be due to invalid IL or missing references)
			//IL_0672: Unknown result type (might be due to invalid IL or missing references)
			//IL_023a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0244: Expected O, but got Unknown
			//IL_025c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0267: Unknown result type (might be due to invalid IL or missing references)
			//IL_0272: Unknown result type (might be due to invalid IL or missing references)
			//IL_027d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0288: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_03a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_03ae: Expected O, but got Unknown
			//IL_03c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_03d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_03dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_03e7: Unknown result type (might be due to invalid IL or missing references)
			//IL_03f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_040a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0415: Unknown result type (might be due to invalid IL or missing references)
			//IL_0420: Unknown result type (might be due to invalid IL or missing references)
			//IL_042c: Unknown result type (might be due to invalid IL or missing references)
			//IL_04cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_04d3: Expected O, but got Unknown
			//IL_0712: Unknown result type (might be due to invalid IL or missing references)
			//IL_0719: Expected O, but got Unknown
			//IL_0d2a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0d31: Expected O, but got Unknown
			//IL_0b71: Unknown result type (might be due to invalid IL or missing references)
			//IL_0b78: Expected O, but got Unknown
			//IL_0506: Unknown result type (might be due to invalid IL or missing references)
			//IL_0510: Expected O, but got Unknown
			//IL_0528: Unknown result type (might be due to invalid IL or missing references)
			//IL_0533: Unknown result type (might be due to invalid IL or missing references)
			//IL_053e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0549: Unknown result type (might be due to invalid IL or missing references)
			//IL_0554: Unknown result type (might be due to invalid IL or missing references)
			//IL_056c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0577: Unknown result type (might be due to invalid IL or missing references)
			//IL_0582: Unknown result type (might be due to invalid IL or missing references)
			//IL_058e: Unknown result type (might be due to invalid IL or missing references)
			//IL_133e: Unknown result type (might be due to invalid IL or missing references)
			//IL_1345: Expected O, but got Unknown
			//IL_1035: Unknown result type (might be due to invalid IL or missing references)
			//IL_103c: Expected O, but got Unknown
			//IL_0f02: Unknown result type (might be due to invalid IL or missing references)
			//IL_0f09: Expected O, but got Unknown
			//IL_074c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0756: Expected O, but got Unknown
			//IL_105c: Unknown result type (might be due to invalid IL or missing references)
			//IL_1066: Expected O, but got Unknown
			//IL_0d64: Unknown result type (might be due to invalid IL or missing references)
			//IL_0d6e: Expected O, but got Unknown
			//IL_0bab: Unknown result type (might be due to invalid IL or missing references)
			//IL_0bb5: Expected O, but got Unknown
			//IL_0bcd: Unknown result type (might be due to invalid IL or missing references)
			//IL_0bd8: Unknown result type (might be due to invalid IL or missing references)
			//IL_0be3: Unknown result type (might be due to invalid IL or missing references)
			//IL_0bee: Unknown result type (might be due to invalid IL or missing references)
			//IL_0bf9: Unknown result type (might be due to invalid IL or missing references)
			//IL_0c11: Unknown result type (might be due to invalid IL or missing references)
			//IL_0c29: Unknown result type (might be due to invalid IL or missing references)
			//IL_0c36: Unknown result type (might be due to invalid IL or missing references)
			//IL_0c42: Unknown result type (might be due to invalid IL or missing references)
			//IL_0937: Unknown result type (might be due to invalid IL or missing references)
			//IL_093e: Expected O, but got Unknown
			//IL_0f3c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0f46: Expected O, but got Unknown
			//IL_13ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_13bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_13ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_10ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_10b3: Expected O, but got Unknown
			//IL_10c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_10cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_10d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_0dad: Unknown result type (might be due to invalid IL or missing references)
			//IL_0db7: Expected O, but got Unknown
			//IL_0dd3: Unknown result type (might be due to invalid IL or missing references)
			//IL_0971: Unknown result type (might be due to invalid IL or missing references)
			//IL_097b: Expected O, but got Unknown
			//IL_099e: Unknown result type (might be due to invalid IL or missing references)
			//IL_09a8: Expected O, but got Unknown
			//IL_09b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_0834: Unknown result type (might be due to invalid IL or missing references)
			//IL_0840: Unknown result type (might be due to invalid IL or missing references)
			//IL_084c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0858: Unknown result type (might be due to invalid IL or missing references)
			//IL_0864: Unknown result type (might be due to invalid IL or missing references)
			//IL_087d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0889: Unknown result type (might be due to invalid IL or missing references)
			//IL_0897: Unknown result type (might be due to invalid IL or missing references)
			//IL_0815: Unknown result type (might be due to invalid IL or missing references)
			//IL_0826: Unknown result type (might be due to invalid IL or missing references)
			//IL_1199: Unknown result type (might be due to invalid IL or missing references)
			//IL_11a0: Expected O, but got Unknown
			//IL_11af: Unknown result type (might be due to invalid IL or missing references)
			//IL_11b9: Expected O, but got Unknown
			//IL_11c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_11d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_11dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_11e8: Unknown result type (might be due to invalid IL or missing references)
			//IL_0f66: Unknown result type (might be due to invalid IL or missing references)
			//IL_0f8b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0f97: Unknown result type (might be due to invalid IL or missing references)
			//IL_0fa5: Unknown result type (might be due to invalid IL or missing references)
			//IL_07e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_07eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_07f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_0803: Unknown result type (might be due to invalid IL or missing references)
			//IL_080d: Expected O, but got Unknown
			//IL_080f: Expected O, but got Unknown
			//IL_0de4: Unknown result type (might be due to invalid IL or missing references)
			//IL_0e09: Unknown result type (might be due to invalid IL or missing references)
			//IL_09c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_09ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_0a07: Unknown result type (might be due to invalid IL or missing references)
			//IL_0a13: Unknown result type (might be due to invalid IL or missing references)
			//IL_0a1f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0a2d: Unknown result type (might be due to invalid IL or missing references)
			//IL_1249: Unknown result type (might be due to invalid IL or missing references)
			//IL_124e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0e1a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0e40: Unknown result type (might be due to invalid IL or missing references)
			//IL_0e4e: Unknown result type (might be due to invalid IL or missing references)
			//IL_125d: Unknown result type (might be due to invalid IL or missing references)
			//IL_1262: Unknown result type (might be due to invalid IL or missing references)
			ModuleDefinition mainModule = assembly.MainModule;
			TypeDefinition type = mainModule.GetType("ZRoutedRpc");
			FieldDefinition val = ((type != null) ? ((IEnumerable<FieldDefinition>)type.Fields).FirstOrDefault((Func<FieldDefinition, bool>)((FieldDefinition f) => ((MemberReference)f).Name == "Everybody")) : null);
			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;
				MethodDefinition val2 = ((IEnumerable<MethodDefinition>)type.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition m) => m.IsConstructor && m.IsStatic));
				if (val2 == null)
				{
					val2 = new MethodDefinition(".cctor", (MethodAttributes)6161, mainModule.TypeSystem.Void);
					ILProcessor iLProcessor = val2.Body.GetILProcessor();
					iLProcessor.Emit(OpCodes.Ldc_I8, 0L);
					iLProcessor.Emit(OpCodes.Stsfld, (FieldReference)(object)val);
					iLProcessor.Emit(OpCodes.Ret);
					type.Methods.Add(val2);
				}
				else
				{
					ILProcessor iLProcessor2 = val2.Body.GetILProcessor();
					Instruction val3 = val2.Body.Instructions[0];
					iLProcessor2.InsertBefore(val3, iLProcessor2.Create(OpCodes.Ldc_I8, 0L));
					iLProcessor2.InsertBefore(val3, iLProcessor2.Create(OpCodes.Stsfld, (FieldReference)(object)val));
				}
				Log.LogInfo((object)" - Converted ZRoutedRpc.Everybody from const to runtime static field [Fixes ServerSync / Azumatt mods in memory]");
			}
			TypeDefinition type2 = mainModule.GetType("Character");
			if (type2 != null)
			{
				MethodDefinition val4 = ((IEnumerable<MethodDefinition>)type2.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition m) => ((MemberReference)m).Name == "Message" && ((MethodReference)m).Parameters.Count == 5));
				MethodDefinition val5 = ((IEnumerable<MethodDefinition>)type2.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition m) => ((MemberReference)m).Name == "Message" && ((MethodReference)m).Parameters.Count == 4));
				if (val4 != null && val5 == null)
				{
					MethodDefinition val6 = new MethodDefinition("Message", (MethodAttributes)134, ((MethodReference)val4).ReturnType);
					for (int num = 0; num < 4; num++)
					{
						((MethodReference)val6).Parameters.Add(new ParameterDefinition(((ParameterReference)((MethodReference)val4).Parameters[num]).Name, (ParameterAttributes)0, ((ParameterReference)((MethodReference)val4).Parameters[num]).ParameterType));
					}
					ILProcessor iLProcessor3 = val6.Body.GetILProcessor();
					iLProcessor3.Emit(OpCodes.Ldarg_0);
					iLProcessor3.Emit(OpCodes.Ldarg_1);
					iLProcessor3.Emit(OpCodes.Ldarg_2);
					iLProcessor3.Emit(OpCodes.Ldarg_3);
					iLProcessor3.Emit(OpCodes.Ldarg, ((MethodReference)val6).Parameters[3]);
					iLProcessor3.Emit(OpCodes.Ldc_I4_0);
					iLProcessor3.Emit(OpCodes.Callvirt, (MethodReference)(object)val4);
					iLProcessor3.Emit(OpCodes.Ret);
					type2.Methods.Add(val6);
					Log.LogInfo((object)" - Injected Character.Message(MessageType, string, int, Sprite)");
				}
			}
			TypeDefinition type3 = mainModule.GetType("SEMan");
			if (type3 != null)
			{
				MethodDefinition val7 = ((IEnumerable<MethodDefinition>)type3.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition m) => ((MemberReference)m).Name == "AddStatusEffect" && ((MethodReference)m).Parameters.Count == 5 && ((MemberReference)((ParameterReference)((MethodReference)m).Parameters[0]).ParameterType).Name == "StatusEffect"));
				MethodDefinition val8 = ((IEnumerable<MethodDefinition>)type3.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition m) => ((MemberReference)m).Name == "AddStatusEffect" && ((MethodReference)m).Parameters.Count == 4 && ((MemberReference)((ParameterReference)((MethodReference)m).Parameters[0]).ParameterType).Name == "StatusEffect"));
				if (val7 != null && val8 == null)
				{
					MethodDefinition val9 = new MethodDefinition("AddStatusEffect", (MethodAttributes)134, ((MethodReference)val7).ReturnType);
					for (int num2 = 0; num2 < 4; num2++)
					{
						((MethodReference)val9).Parameters.Add(new ParameterDefinition(((ParameterReference)((MethodReference)val7).Parameters[num2]).Name, (ParameterAttributes)0, ((ParameterReference)((MethodReference)val7).Parameters[num2]).ParameterType));
					}
					ILProcessor iLProcessor4 = val9.Body.GetILProcessor();
					iLProcessor4.Emit(OpCodes.Ldarg_0);
					iLProcessor4.Emit(OpCodes.Ldarg_1);
					iLProcessor4.Emit(OpCodes.Ldarg_2);
					iLProcessor4.Emit(OpCodes.Ldarg_3);
					iLProcessor4.Emit(OpCodes.Ldarg, ((MethodReference)val9).Parameters[3]);
					iLProcessor4.Emit(OpCodes.Ldc_I4_0);
					iLProcessor4.Emit(OpCodes.Conv_I2);
					iLProcessor4.Emit(OpCodes.Callvirt, (MethodReference)(object)val7);
					iLProcessor4.Emit(OpCodes.Ret);
					type3.Methods.Add(val9);
					Log.LogInfo((object)" - Injected SEMan.AddStatusEffect(StatusEffect, bool, int, float)");
				}
				MethodDefinition val10 = ((IEnumerable<MethodDefinition>)type3.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition m) => ((MemberReference)m).Name == "AddStatusEffect" && ((MethodReference)m).Parameters.Count == 5 && ((MemberReference)((ParameterReference)((MethodReference)m).Parameters[0]).ParameterType).Name == "Int32"));
				MethodDefinition val11 = ((IEnumerable<MethodDefinition>)type3.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition m) => ((MemberReference)m).Name == "AddStatusEffect" && ((MethodReference)m).Parameters.Count == 4 && ((MemberReference)((ParameterReference)((MethodReference)m).Parameters[0]).ParameterType).Name == "Int32"));
				if (val10 != null && val11 == null)
				{
					MethodDefinition val12 = new MethodDefinition("AddStatusEffect", (MethodAttributes)134, ((MethodReference)val10).ReturnType);
					for (int num3 = 0; num3 < 4; num3++)
					{
						((MethodReference)val12).Parameters.Add(new ParameterDefinition(((ParameterReference)((MethodReference)val10).Parameters[num3]).Name, (ParameterAttributes)0, ((ParameterReference)((MethodReference)val10).Parameters[num3]).ParameterType));
					}
					ILProcessor iLProcessor5 = val12.Body.GetILProcessor();
					iLProcessor5.Emit(OpCodes.Ldarg_0);
					iLProcessor5.Emit(OpCodes.Ldarg_1);
					iLProcessor5.Emit(OpCodes.Ldarg_2);
					iLProcessor5.Emit(OpCodes.Ldarg_3);
					iLProcessor5.Emit(OpCodes.Ldarg, ((MethodReference)val12).Parameters[3]);
					iLProcessor5.Emit(OpCodes.Ldc_I4_0);
					iLProcessor5.Emit(OpCodes.Conv_I2);
					iLProcessor5.Emit(OpCodes.Callvirt, (MethodReference)(object)val10);
					iLProcessor5.Emit(OpCodes.Ret);
					type3.Methods.Add(val12);
					Log.LogInfo((object)" - Injected SEMan.AddStatusEffect(int, bool, int, float)");
				}
			}
			TypeDefinition type4 = mainModule.GetType("Inventory");
			if (type4 != null)
			{
				MethodDefinition val13 = ((IEnumerable<MethodDefinition>)type4.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition m) => ((MemberReference)m).Name == "IsTeleportable" && ((MethodReference)m).Parameters.Count == 1));
				MethodDefinition val14 = ((IEnumerable<MethodDefinition>)type4.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition m) => ((MemberReference)m).Name == "IsTeleportable" && ((MethodReference)m).Parameters.Count == 0));
				if (val13 != null && val14 == null)
				{
					MethodDefinition val15 = new MethodDefinition("IsTeleportable", (MethodAttributes)134, ((MethodReference)val13).ReturnType);
					ILProcessor iLProcessor6 = val15.Body.GetILProcessor();
					iLProcessor6.Emit(OpCodes.Ldarg_0);
					iLProcessor6.Emit(OpCodes.Ldc_I4_0);
					iLProcessor6.Emit(OpCodes.Callvirt, (MethodReference)(object)val13);
					iLProcessor6.Emit(OpCodes.Ret);
					type4.Methods.Add(val15);
					Log.LogInfo((object)" - Injected Inventory.IsTeleportable()");
				}
				MethodDefinition val16 = ((IEnumerable<MethodDefinition>)type4.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition m) => ((MemberReference)m).Name == "AddItem" && ((MethodReference)m).Parameters.Count == 5 && ((MemberReference)((ParameterReference)((MethodReference)m).Parameters[0]).ParameterType).Name == "ItemData"));
				MethodDefinition val17 = ((IEnumerable<MethodDefinition>)type4.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition m) => ((MemberReference)m).Name == "AddItem" && ((MethodReference)m).Parameters.Count == 4 && ((MemberReference)((ParameterReference)((MethodReference)m).Parameters[0]).ParameterType).Name == "ItemData"));
				if (val16 != null && val17 == null)
				{
					MethodDefinition val18 = new MethodDefinition("AddItem", (MethodAttributes)134, ((MethodReference)val16).ReturnType);
					for (int num4 = 0; num4 < 4; num4++)
					{
						((MethodReference)val18).Parameters.Add(new ParameterDefinition(((ParameterReference)((MethodReference)val16).Parameters[num4]).Name, (ParameterAttributes)0, ((ParameterReference)((MethodReference)val16).Parameters[num4]).ParameterType));
					}
					ILProcessor iLProcessor7 = val18.Body.GetILProcessor();
					MemberReference? obj = mainModule.GetMemberReferences().FirstOrDefault((Func<MemberReference, bool>)((MemberReference m) => ((MemberReference)m.DeclaringType).Name == "ZLog" && m.Name == "Log" && m is MethodReference));
					MethodReference val19 = (MethodReference)(object)((obj is MethodReference) ? obj : null);
					if (val19 == null)
					{
						TypeReference val20 = mainModule.GetTypeReferences().FirstOrDefault((Func<TypeReference, bool>)((TypeReference t) => ((MemberReference)t).Name == "ZLog"));
						if (val20 != null)
						{
							MethodReference val21 = new MethodReference("Log", mainModule.TypeSystem.Void, val20)
							{
								HasThis = false
							};
							val21.Parameters.Add(new ParameterDefinition(mainModule.TypeSystem.Object));
							val19 = val21;
						}
					}
					if (val19 != null)
					{
						iLProcessor7.Emit(OpCodes.Ldstr, "AddItem bridge");
						iLProcessor7.Emit(OpCodes.Call, val19);
					}
					iLProcessor7.Emit(OpCodes.Ldarg_0);
					iLProcessor7.Emit(OpCodes.Ldarg_1);
					iLProcessor7.Emit(OpCodes.Ldarg_2);
					iLProcessor7.Emit(OpCodes.Ldarg_3);
					iLProcessor7.Emit(OpCodes.Ldarg, ((MethodReference)val18).Parameters[3]);
					iLProcessor7.Emit(OpCodes.Ldc_I4_0);
					iLProcessor7.Emit(OpCodes.Callvirt, (MethodReference)(object)val16);
					iLProcessor7.Emit(OpCodes.Ret);
					type4.Methods.Add(val18);
					Log.LogInfo((object)" - Injected Inventory.AddItem(ItemData, int, int, int) [with ZLog placeholder for AzuAutoStore / MUC RemoveLogging]");
				}
				MethodDefinition val22 = ((IEnumerable<MethodDefinition>)type4.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition m) => ((MemberReference)m).Name == "AddItem" && ((MethodReference)m).Parameters.Count == 10 && ((MemberReference)((ParameterReference)((MethodReference)m).Parameters[0]).ParameterType).Name == "String"));
				MethodDefinition val23 = ((IEnumerable<MethodDefinition>)type4.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition m) => ((MemberReference)m).Name == "AddItem" && ((MethodReference)m).Parameters.Count == 8 && ((MemberReference)((ParameterReference)((MethodReference)m).Parameters[0]).ParameterType).Name == "String" && ((MemberReference)((ParameterReference)((MethodReference)m).Parameters[6]).ParameterType).Name == "Vector2i"));
				if (val22 != null && val23 == null)
				{
					MethodDefinition val24 = new MethodDefinition("AddItem", (MethodAttributes)134, ((MethodReference)val22).ReturnType);
					for (int num5 = 0; num5 < 7; num5++)
					{
						((MethodReference)val24).Parameters.Add(new ParameterDefinition(((ParameterReference)((MethodReference)val22).Parameters[num5]).Name, (ParameterAttributes)0, ((ParameterReference)((MethodReference)val22).Parameters[num5]).ParameterType));
					}
					((MethodReference)val24).Parameters.Add(new ParameterDefinition("worldGen", (ParameterAttributes)0, mainModule.TypeSystem.Boolean));
					ILProcessor iLProcessor8 = val24.Body.GetILProcessor();
					iLProcessor8.Emit(OpCodes.Ldarg_0);
					for (int num6 = 0; num6 < 7; num6++)
					{
						iLProcessor8.Emit(OpCodes.Ldarg, ((MethodReference)val24).Parameters[num6]);
					}
					iLProcessor8.Emit(OpCodes.Ldarg, ((MethodReference)val24).Parameters[7]);
					iLProcessor8.Emit(OpCodes.Ldc_I4_0);
					iLProcessor8.Emit(OpCodes.Ldc_I4_0);
					iLProcessor8.Emit(OpCodes.Callvirt, (MethodReference)(object)val22);
					iLProcessor8.Emit(OpCodes.Ret);
					type4.Methods.Add(val24);
					Log.LogInfo((object)" - Injected Inventory.AddItem(8 params) [Fixes ItemDataManager / Cooking / EpicLoot / ExtraSlots]");
				}
				MethodDefinition val25 = ((IEnumerable<MethodDefinition>)type4.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition m) => ((MemberReference)m).Name == "Load" && ((MethodReference)m).Parameters.Count == 2 && ((MemberReference)((ParameterReference)((MethodReference)m).Parameters[1]).ParameterType).Name == "Boolean"));
				if (val25 != null)
				{
					((MemberReference)val25).Name = "Load_Unused";
					Log.LogInfo((object)" - Renamed redundant Inventory.Load(ZPackage, bool) to Load_Unused [Fixes AzuAutoStore AmbiguousMatchException]");
				}
			}
			TypeDefinition type5 = mainModule.GetType("EffectList");
			if (type5 != null)
			{
				MethodDefinition val26 = ((IEnumerable<MethodDefinition>)type5.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition m) => ((MemberReference)m).Name == "Create" && ((MethodReference)m).Parameters.Count == 6));
				MethodDefinition val27 = ((IEnumerable<MethodDefinition>)type5.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition m) => ((MemberReference)m).Name == "Create" && ((MethodReference)m).Parameters.Count == 5));
				TypeDefinition type6 = mainModule.GetType("ZDOID");
				FieldDefinition val28 = ((type6 != null) ? ((IEnumerable<FieldDefinition>)type6.Fields).FirstOrDefault((Func<FieldDefinition, bool>)((FieldDefinition f) => ((MemberReference)f).Name == "None")) : null);
				if (val26 != null && val27 == null && val28 != null)
				{
					MethodDefinition val29 = new MethodDefinition("Create", (MethodAttributes)134, ((MethodReference)val26).ReturnType);
					for (int num7 = 0; num7 < 5; num7++)
					{
						((MethodReference)val29).Parameters.Add(new ParameterDefinition(((ParameterReference)((MethodReference)val26).Parameters[num7]).Name, (ParameterAttributes)0, ((ParameterReference)((MethodReference)val26).Parameters[num7]).ParameterType));
					}
					ILProcessor iLProcessor9 = val29.Body.GetILProcessor();
					iLProcessor9.Emit(OpCodes.Ldarg_0);
					iLProcessor9.Emit(OpCodes.Ldarg_1);
					iLProcessor9.Emit(OpCodes.Ldarg_2);
					iLProcessor9.Emit(OpCodes.Ldarg_3);
					iLProcessor9.Emit(OpCodes.Ldarg, ((MethodReference)val29).Parameters[3]);
					iLProcessor9.Emit(OpCodes.Ldarg, ((MethodReference)val29).Parameters[4]);
					iLProcessor9.Emit(OpCodes.Ldsfld, (FieldReference)(object)val28);
					iLProcessor9.Emit(OpCodes.Callvirt, (MethodReference)(object)val26);
					iLProcessor9.Emit(OpCodes.Ret);
					type5.Methods.Add(val29);
					Log.LogInfo((object)" - Injected EffectList.Create(Vector3, Quaternion, Transform, float, int)");
				}
			}
			TypeDefinition type7 = mainModule.GetType("Terminal");
			TypeDefinition val30 = ((type7 != null) ? ((IEnumerable<TypeDefinition>)type7.NestedTypes).FirstOrDefault((Func<TypeDefinition, bool>)((TypeDefinition t) => ((MemberReference)t).Name == "ConsoleCommand")) : null);
			if (val30 != null)
			{
				MethodDefinition val31 = ((IEnumerable<MethodDefinition>)val30.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition m) => m.IsConstructor && ((MethodReference)m).Parameters.Count == 13 && ((MemberReference)((ParameterReference)((MethodReference)m).Parameters[2]).ParameterType).Name == "ConsoleEvent"));
				MethodDefinition val32 = ((IEnumerable<MethodDefinition>)val30.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition m) => m.IsConstructor && ((MethodReference)m).Parameters.Count == 12 && ((MemberReference)((ParameterReference)((MethodReference)m).Parameters[2]).ParameterType).Name == "ConsoleEvent"));
				if (val31 != null && val32 == null)
				{
					MethodDefinition val33 = new MethodDefinition(".ctor", (MethodAttributes)6278, mainModule.TypeSystem.Void);
					for (int num8 = 0; num8 < 8; num8++)
					{
						((MethodReference)val33).Parameters.Add(new ParameterDefinition(((ParameterReference)((MethodReference)val31).Parameters[num8]).Name, (ParameterAttributes)0, ((ParameterReference)((MethodReference)val31).Parameters[num8]).ParameterType));
					}
					for (int num9 = 9; num9 < 13; num9++)
					{
						((MethodReference)val33).Parameters.Add(new ParameterDefinition(((ParameterReference)((MethodReference)val31).Parameters[num9]).Name, (ParameterAttributes)0, ((ParameterReference)((MethodReference)val31).Parameters[num9]).ParameterType));
					}
					ILProcessor iLProcessor10 = val33.Body.GetILProcessor();
					iLProcessor10.Emit(OpCodes.Ldarg_0);
					for (int num10 = 0; num10 < 8; num10++)
					{
						iLProcessor10.Emit(OpCodes.Ldarg, ((MethodReference)val33).Parameters[num10]);
					}
					iLProcessor10.Emit(OpCodes.Ldc_I4_0);
					for (int num11 = 8; num11 < 12; num11++)
					{
						iLProcessor10.Emit(OpCodes.Ldarg, ((MethodReference)val33).Parameters[num11]);
					}
					iLProcessor10.Emit(OpCodes.Call, (MethodReference)(object)val31);
					iLProcessor10.Emit(OpCodes.Ret);
					val30.Methods.Add(val33);
					Log.LogInfo((object)" - Injected Terminal.ConsoleCommand..ctor (12 params)");
				}
			}
			TypeDefinition type8 = mainModule.GetType("ItemDrop/ItemData");
			if (type8 != null)
			{
				MethodDefinition val34 = ((IEnumerable<MethodDefinition>)type8.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition m) => ((MemberReference)m).Name == "GetTooltip" && ((MethodReference)m).Parameters.Count == 6 && m.IsStatic));
				MethodDefinition val35 = ((IEnumerable<MethodDefinition>)type8.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition m) => ((MemberReference)m).Name == "GetTooltip" && ((MethodReference)m).Parameters.Count == 5 && m.IsStatic));
				if (val34 != null && val35 == null)
				{
					MethodDefinition val36 = new MethodDefinition("GetTooltip", (MethodAttributes)150, ((MethodReference)val34).ReturnType);
					for (int num12 = 0; num12 < 5; num12++)
					{
						((MethodReference)val36).Parameters.Add(new ParameterDefinition(((ParameterReference)((MethodReference)val34).Parameters[num12]).Name, (ParameterAttributes)0, ((ParameterReference)((MethodReference)val34).Parameters[num12]).ParameterType));
					}
					ILProcessor iLProcessor11 = val36.Body.GetILProcessor();
					for (int num13 = 0; num13 < 5; num13++)
					{
						iLProcessor11.Emit(OpCodes.Ldarg, ((MethodReference)val36).Parameters[num13]);
					}
					iLProcessor11.Emit(OpCodes.Ldc_I4_0);
					iLProcessor11.Emit(OpCodes.Call, (MethodReference)(object)val34);
					iLProcessor11.Emit(OpCodes.Ret);
					type8.Methods.Add(val36);
					Log.LogInfo((object)" - Injected ItemDrop.ItemData.GetTooltip(5 params) [Fixes Blacksmithing & Cooking]");
				}
			}
			TypeDefinition type9 = mainModule.GetType("InventoryGrid");
			TypeDefinition type10 = mainModule.GetType("InventoryElement");
			if (type9 != null && type10 != null)
			{
				if (((IEnumerable<TypeDefinition>)type9.NestedTypes).FirstOrDefault((Func<TypeDefinition, bool>)((TypeDefinition t) => ((MemberReference)t).Name == "Element")) == null)
				{
					TypeDefinition val37 = new TypeDefinition("", "Element", (TypeAttributes)1048578, (TypeReference)(object)type10);
					TypeDefinition type11 = mainModule.GetType("Vector2i");
					if (type11 != null)
					{
						val37.Fields.Add(new FieldDefinition("m_pos", (FieldAttributes)6, (TypeReference)(object)type11));
					}
					MethodDefinition val38 = ((IEnumerable<MethodDefinition>)type10.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition m) => m.IsConstructor && !m.IsStatic && ((MethodReference)m).Parameters.Count == 0));
					if (val38 != null)
					{
						MethodDefinition val39 = new MethodDefinition(".ctor", (MethodAttributes)6278, mainModule.TypeSystem.Void);
						ILProcessor iLProcessor12 = val39.Body.GetILProcessor();
						iLProcessor12.Emit(OpCodes.Ldarg_0);
						iLProcessor12.Emit(OpCodes.Call, (MethodReference)(object)val38);
						iLProcessor12.Emit(OpCodes.Ret);
						val37.Methods.Add(val39);
					}
					type9.NestedTypes.Add(val37);
					Log.LogInfo((object)" - Injected InventoryGrid.Element subclassing InventoryElement");
				}
				MethodDefinition? obj2 = ((IEnumerable<MethodDefinition>)type9.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition m) => ((MemberReference)m).Name == "OnRightClick" && ((MethodReference)m).Parameters.Count == 1 && ((MemberReference)((ParameterReference)((MethodReference)m).Parameters[0]).ParameterType).Name == "UIInputHandler"));
				MethodDefinition val40 = ((IEnumerable<MethodDefinition>)type9.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition m) => ((MemberReference)m).Name == "OnRightDown" && ((MethodReference)m).Parameters.Count == 1 && ((MemberReference)((ParameterReference)((MethodReference)m).Parameters[0]).ParameterType).Name == "UIInputHandler"));
				if (obj2 == null && val40 != null)
				{
					TypeReference parameterType = ((ParameterReference)((MethodReference)val40).Parameters[0]).ParameterType;
					MethodDefinition val41 = new MethodDefinition("OnRightClick", (MethodAttributes)134, mainModule.TypeSystem.Void);
					((MethodReference)val41).Parameters.Add(new ParameterDefinition("element", (ParameterAttributes)0, parameterType));
					ILProcessor iLProcessor13 = val41.Body.GetILProcessor();
					iLProcessor13.Emit(OpCodes.Ldarg_0);
					iLProcessor13.Emit(OpCodes.Ldarg_1);
					iLProcessor13.Emit(OpCodes.Call, (MethodReference)(object)val40);
					iLProcessor13.Emit(OpCodes.Ret);
					type9.Methods.Add(val41);
					MethodDefinition val42 = ((IEnumerable<MethodDefinition>)type9.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition m) => ((MemberReference)m).Name == "UpdateGui"));
					if (val42 != null && val42.HasBody)
					{
						Enumerator<Instruction> enumerator = val42.Body.Instructions.GetEnumerator();
						try
						{
							while (enumerator.MoveNext())
							{
								Instruction current = enumerator.Current;
								if (current.OpCode == OpCodes.Ldftn)
								{
									object operand = current.Operand;
									MethodReference val43 = (MethodReference)((operand is MethodReference) ? operand : null);
									if (val43 != null && ((MemberReference)val43).Name == "OnRightDown")
									{
										current.Operand = val41;
									}
								}
							}
						}
						finally
						{
							((IDisposable)enumerator/*cast due to .constrained prefix*/).Dispose();
						}
					}
					Log.LogInfo((object)" - Injected InventoryGrid.OnRightClick(UIInputHandler) [Fixes AzuAutoStore Favoriting]");
				}
			}
			TypeDefinition type12 = mainModule.GetType("PlayerProfile");
			if (type12 != null && ((IEnumerable<FieldDefinition>)type12.Fields).FirstOrDefault((Func<FieldDefinition, bool>)((FieldDefinition f) => ((MemberReference)f).Name == "m_itemCraftStats")) == null)
			{
				TypeReference val44 = mainModule.ImportReference(typeof(Dictionary<string, float>));
				MethodReference val45 = mainModule.ImportReference((MethodBase)typeof(Dictionary<string, float>).GetConstructor(Type.EmptyTypes));
				FieldDefinition val46 = new FieldDefinition("m_itemCraftStats", (FieldAttributes)6, val44);
				type12.Fields.Add(val46);
				MethodDefinition val47 = ((IEnumerable<MethodDefinition>)type12.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition m) => m.IsConstructor && !m.IsStatic));
				if (val47 != null && val47.HasBody)
				{
					ILProcessor iLProcessor14 = val47.Body.GetILProcessor();
					Instruction val48 = val47.Body.Instructions[0];
					Instruction val49 = iLProcessor14.Create(OpCodes.Ldarg_0);
					Instruction val50 = iLProcessor14.Create(OpCodes.Newobj, val45);
					Instruction val51 = iLProcessor14.Create(OpCodes.Stfld, (FieldReference)(object)val46);
					iLProcessor14.InsertBefore(val48, val49);
					iLProcessor14.InsertAfter(val49, val50);
					iLProcessor14.InsertAfter(val50, val51);
				}
				Log.LogInfo((object)" - Injected PlayerProfile.m_itemCraftStats with auto-initialization [Fixes Blacksmithing character load/save crash]");
			}
		}

		public static void ExecutePluginPatch()
		{
			if (_pluginsPatched)
			{
				return;
			}
			_pluginsPatched = true;
			try
			{
				string text = Paths.PluginPath;
				if (string.IsNullOrEmpty(text) || !Directory.Exists(text))
				{
					try
					{
						text = Path.Combine(Paths.BepInExRootPath, "plugins");
					}
					catch
					{
					}
				}
				if (string.IsNullOrEmpty(text) || !Directory.Exists(text))
				{
					Log.LogWarning((object)("Plugins directory not found at: " + text));
					return;
				}
				string[] files = Directory.GetFiles(text, "*.dll", SearchOption.AllDirectories);
				string[] array = files;
				foreach (string text2 in array)
				{
					string text3 = text2 + ".orig.bak";
					if (!File.Exists(text3))
					{
						continue;
					}
					string fileName = Path.GetFileName(text2);
					if (fileName.Equals("MistBeGone.dll", StringComparison.OrdinalIgnoreCase) || fileName.Equals("AzuAutoStore.dll", StringComparison.OrdinalIgnoreCase))
					{
						try
						{
							File.Copy(text3, text2, overwrite: true);
							File.Delete(text3);
							Log.LogInfo((object)("Restored original " + fileName + " from backup (memory patch handles Inventory.AddItem & ZRoutedRpc.Everybody)."));
						}
						catch (Exception ex)
						{
							Log.LogWarning((object)("Could not restore backup for " + fileName + ": " + ex.Message));
						}
					}
				}
				array = files;
				foreach (string text4 in array)
				{
					string fileName2 = Path.GetFileName(text4);
					if (!fileName2.EndsWith(".bak", StringComparison.OrdinalIgnoreCase) && !fileName2.EndsWith(".tmp", StringComparison.OrdinalIgnoreCase) && !fileName2.Equals("AzuModsValheim1Compat.dll", StringComparison.OrdinalIgnoreCase) && !fileName2.Equals("Backpacks.dll", StringComparison.OrdinalIgnoreCase))
					{
						try
						{
							PatchAttachArmorCalls(text4);
							PatchBlacksmithing(text4);
							PatchAzuCraftyBoxes(text4);
						}
						catch (Exception ex2)
						{
							Log.LogError((object)("Error checking plugin compatibility in " + fileName2 + ": " + ex2.Message));
						}
					}
				}
			}
			catch (Exception ex3)
			{
				Log.LogError((object)("Critical error during plugin compatibility patch execution: " + ex3.Message + "\n" + ex3.StackTrace));
			}
		}

		private static void PatchAttachArmorCalls(string dllPath)
		{
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: 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_0064: Unknown result type (might be due to invalid IL or missing references)
			//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_014e: 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_0160: Unknown result type (might be due to invalid IL or missing references)
			//IL_016d: 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_017c: Expected O, but got Unknown
			//IL_0183: Unknown result type (might be due to invalid IL or missing references)
			//IL_0188: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bb: Expected O, but got Unknown
			//IL_01f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fb: Expected O, but got Unknown
			//IL_0201: Unknown result type (might be due to invalid IL or missing references)
			string fileName = Path.GetFileName(dllPath);
			byte[] array = File.ReadAllBytes(dllPath);
			if (!Encoding.ASCII.GetString(array).Contains("AttachArmor"))
			{
				return;
			}
			using MemoryStream memoryStream = new MemoryStream(array);
			AssemblyDefinition val = AssemblyDefinition.ReadAssembly((Stream)memoryStream, GetReaderParameters());
			try
			{
				int num = 0;
				Enumerator<TypeDefinition> enumerator = val.MainModule.Types.GetEnumerator();
				try
				{
					while (enumerator.MoveNext())
					{
						Enumerator<MethodDefinition> enumerator2 = enumerator.Current.Methods.GetEnumerator();
						try
						{
							while (enumerator2.MoveNext())
							{
								MethodDefinition current = enumerator2.Current;
								if (!current.HasBody || current.Body.Instructions == null)
								{
									continue;
								}
								ILProcessor iLProcessor = current.Body.GetILProcessor();
								Instruction[] array2 = current.Body.Instructions.ToArray();
								foreach (Instruction val2 in array2)
								{
									if (!(val2.OpCode == OpCodes.Callvirt))
									{
										continue;
									}
									object operand = val2.Operand;
									MethodReference val3 = (MethodReference)((operand is MethodReference) ? operand : null);
									if (val3 == null || ((MemberReference)val3).DeclaringType == null || !(((MemberReference)((MemberReference)val3).DeclaringType).Name == "VisEquipment") || !(((MemberReference)val3).Name == "AttachArmor") || val3.Parameters.Count != 2)
									{
										continue;
									}
									MethodReference val4 = new MethodReference("AttachArmor", val3.ReturnType, ((MemberReference)val3).DeclaringType)
									{
										HasThis = val3.HasThis,
										ExplicitThis = val3.ExplicitThis,
										CallingConvention = val3.CallingConvention
									};
									Enumerator<ParameterDefinition> enumerator3 = val3.Parameters.GetEnumerator();
									try
									{
										while (enumerator3.MoveNext())
										{
											ParameterDefinition current2 = enumerator3.Current;
											val4.Parameters.Add(new ParameterDefinition(((ParameterReference)current2).Name, current2.Attributes, ((ParameterReference)current2).ParameterType));
										}
									}
									finally
									{
										((IDisposable)enumerator3/*cast due to .constrained prefix*/).Dispose();
									}
									val4.Parameters.Add(new ParameterDefinition("quality", (ParameterAttributes)0, val.MainModule.TypeSystem.Int32));
									iLProcessor.InsertBefore(val2, iLProcessor.Create(OpCodes.Ldc_I4_0));
									val2.Operand = val4;
									num++;
								}
							}
						}
						finally
						{
							((IDisposable)enumerator2/*cast due to .constrained prefix*/).Dispose();
						}
					}
				}
				finally
				{
					((IDisposable)enumerator/*cast due to .constrained prefix*/).Dispose();
				}
				if (num > 0)
				{
					string text = dllPath + ".orig.bak";
					if (!File.Exists(text))
					{
						File.Copy(dllPath, text, overwrite: false);
					}
					string text2 = dllPath + ".tmp";
					if (File.Exists(text2))
					{
						File.Delete(text2);
					}
					val.Write(text2);
					File.Copy(text2, dllPath, overwrite: true);
					File.Delete(text2);
					Log.LogInfo((object)$"[Compatibility Fix] Patched {fileName}: updated {num} VisEquipment.AttachArmor call(s) to 3-parameter version with quality=0.");
				}
			}
			finally
			{
				((IDisposable)val)?.Dispose();
			}
		}

		private static void PatchBlacksmithing(string dllPath)
		{
			//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0143: 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_00c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c7: 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)
			string fileName = Path.GetFileName(dllPath);
			if (!fileName.Equals("Blacksmithing.dll", StringComparison.OrdinalIgnoreCase))
			{
				return;
			}
			try
			{
				using MemoryStream memoryStream = new MemoryStream(File.ReadAllBytes(dllPath));
				AssemblyDefinition val = AssemblyDefinition.ReadAssembly((Stream)memoryStream, GetReaderParameters());
				try
				{
					TypeDefinition type = val.MainModule.GetType("Blacksmithing.Blacksmithing");
					MethodDefinition val2 = ((type != null) ? ((IEnumerable<MethodDefinition>)type.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition m) => ((MemberReference)m).Name == "ApplyTranspilerToAll")) : null);
					if (val2 == null || !val2.HasBody)
					{
						return;
					}
					Instruction val3 = null;
					for (int num = 0; num < val2.Body.Instructions.Count; num++)
					{
						Instruction val4 = val2.Body.Instructions[num];
						if (val4.OpCode == OpCodes.Ldarg_1 && val4.Next != null && val4.Next.OpCode == OpCodes.Newobj)
						{
							object operand = val4.Next.Operand;
							MethodReference val5 = (MethodReference)((operand is MethodReference) ? operand : null);
							if (val5 != null && ((MemberReference)((MemberReference)val5).DeclaringType).Name == "HarmonyMethod")
							{
								val3 = val4;
								break;
							}
						}
					}
					if (val3 == null)
					{
						return;
					}
					Instruction val6 = val2.Body.Instructions[3];
					if (!(val6.OpCode == OpCodes.Br) || val6.Operand != val3)
					{
						val2.Body.Instructions[3] = Instruction.Create(OpCodes.Br, val3);
						string text = dllPath + ".orig.bak";
						if (!File.Exists(text))
						{
							File.Copy(dllPath, text, overwrite: false);
						}
						string text2 = dllPath + ".tmp";
						if (File.Exists(text2))
						{
							File.Delete(text2);
						}
						val.Write(text2);
						File.Copy(text2, dllPath, overwrite: true);
						File.Delete(text2);
						Log.LogInfo((object)("[Compatibility Fix] Patched " + fileName + ": bypassed foreign hook scan in ApplyTranspilerToAll [Fixes InvalidOperationException / Unity 6 crash]."));
					}
				}
				finally
				{
					((IDisposable)val)?.Dispose();
				}
			}
			catch (Exception ex)
			{
				Log.LogError((object)("Failed to patch Blacksmithing.dll: " + ex.Message));
			}
		}

		private static void PatchAzuCraftyBoxes(string dllPath)
		{
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_013c: Unknown result type (might be due to invalid IL or missing references)
			string fileName = Path.GetFileName(dllPath);
			if (!fileName.Equals("AzuCraftyBoxes.dll", StringComparison.OrdinalIgnoreCase))
			{
				return;
			}
			try
			{
				byte[] array = File.ReadAllBytes(dllPath);
				if (!Encoding.ASCII.GetString(array).Contains("EpicLoot-UnityLib"))
				{
					return;
				}
				using MemoryStream memoryStream = new MemoryStream(array);
				AssemblyDefinition val = AssemblyDefinition.ReadAssembly((Stream)memoryStream, GetReaderParameters());
				try
				{
					int num = 0;
					Enumerator<TypeDefinition> enumerator = val.MainModule.Types.GetEnumerator();
					try
					{
						while (enumerator.MoveNext())
						{
							Enumerator<TypeDefinition> enumerator2 = enumerator.Current.NestedTypes.GetEnumerator();
							try
							{
								while (enumerator2.MoveNext())
								{
									Enumerator<MethodDefinition> enumerator3 = enumerator2.Current.Methods.GetEnumerator();
									try
									{
										while (enumerator3.MoveNext())
										{
											Enumerator<CustomAttribute> enumerator4 = enumerator3.Current.CustomAttributes.GetEnumerator();
											try
											{
												while (enumerator4.MoveNext())
												{
													CustomAttribute current = enumerator4.Current;
													if (!(((MemberReference)current.AttributeType).Name == "HarmonyPatch"))
													{
														continue;
													}
													for (int i = 0; i < current.ConstructorArguments.Count; i++)
													{
														CustomAttributeArgument val2 = current.ConstructorArguments[i];
														if (((CustomAttributeArgument)(ref val2)).Value is string text && text.Contains("EpicLoot-UnityLib"))
														{
															string text2 = text.Replace(", EpicLoot-UnityLib", "").Replace("EpicLoot-UnityLib", "EpicLoot");
															current.ConstructorArguments[i] = new CustomAttributeArgument(((CustomAttributeArgument)(ref val2)).Type, (object)text2);
															num++;
														}
													}
												}
											}
											finally
											{
												((IDisposable)enumerator4/*cast due to .constrained prefix*/).Dispose();
											}
										}
									}
									finally
									{
										((IDisposable)enumerator3/*cast due to .constrained prefix*/).Dispose();
									}
								}
							}
							finally
							{
								((IDisposable)enumerator2/*cast due to .constrained prefix*/).Dispose();
							}
						}
					}
					finally
					{
						((IDisposable)enumerator/*cast due to .constrained prefix*/).Dispose();
					}
					if (num > 0)
					{
						string text3 = dllPath + ".orig.bak";
						if (!File.Exists(text3))
						{
							File.Copy(dllPath, text3, overwrite: false);
						}
						string text4 = dllPath + ".tmp";
						if (File.Exists(text4))
						{
							File.Delete(text4);
						}
						val.Write(text4);
						File.Copy(text4, dllPath, overwrite: true);
						File.Delete(text4);
						Log.LogInfo((object)$"[Compatibility Fix] Patched {fileName}: updated {num} EpicLoot HarmonyPatch references from 'EpicLoot-UnityLib' to standalone type name.");
					}
				}
				finally
				{
					((IDisposable)val)?.Dispose();
				}
			}
			catch (Exception ex)
			{
				Log.LogError((object)("Failed to patch AzuCraftyBoxes.dll: " + ex.Message));
			}
		}

		private static ReaderParameters GetReaderParameters()
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Expected O, but got Unknown
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bd: Expected O, but got Unknown
			DefaultAssemblyResolver val = new DefaultAssemblyResolver();
			try
			{
				if (Paths.DllSearchPaths != null)
				{
					string[] dllSearchPaths = Paths.DllSearchPaths;
					foreach (string text in dllSearchPaths)
					{
						if (!string.IsNullOrEmpty(text) && Directory.Exists(text))
						{
							((BaseAssemblyResolver)val).AddSearchDirectory(text);
						}
					}
				}
			}
			catch
			{
			}
			try
			{
				if (!string.IsNullOrEmpty(Paths.BepInExAssemblyDirectory) && Directory.Exists(Paths.BepInExAssemblyDirectory))
				{
					((BaseAssemblyResolver)val).AddSearchDirectory(Paths.BepInExAssemblyDirectory);
				}
				if (!string.IsNullOrEmpty(Paths.ManagedPath) && Directory.Exists(Paths.ManagedPath))
				{
					((BaseAssemblyResolver)val).AddSearchDirectory(Paths.ManagedPath);
				}
				if (!string.IsNullOrEmpty(Paths.PluginPath) && Directory.Exists(Paths.PluginPath))
				{
					((BaseAssemblyResolver)val).AddSearchDirectory(Paths.PluginPath);
				}
			}
			catch
			{
			}
			return new ReaderParameters
			{
				AssemblyResolver = (IAssemblyResolver)(object)val
			};
		}

		private static void InstallAssemblyResolver()
		{
			if (_resolverInstalled)
			{
				return;
			}
			_resolverInstalled = true;
			try
			{
				AppDomain.CurrentDomain.AssemblyResolve += delegate(object sender, ResolveEventArgs args)
				{
					try
					{
						if (new AssemblyName(args.Name).Name.Equals("EpicLoot-UnityLib", StringComparison.OrdinalIgnoreCase))
						{
							return AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault((Assembly a) => a.GetName().Name.Equals("EpicLoot", StringComparison.OrdinalIgnoreCase));
						}
					}
					catch
					{
					}
					return (Assembly)null;
				};
			}
			catch (Exception ex)
			{
				Log.LogWarning((object)("Could not install EpicLoot assembly resolver: " + ex.Message));
			}
		}
	}
}