Decompiled source of Valheim1CompatBridges v1.1.0

patchers/Valheim1CompatBridges/Valheim1CompatBridges.dll

Decompiled 4 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;
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: AssemblyVersion("0.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 Valheim1CompatBridges
{
	public static class Patcher
	{
		private sealed class Lit
		{
			public readonly long V;

			public Lit(long v)
			{
				V = v;
			}
		}

		private static readonly ManualLogSource Log = Logger.CreateLogSource("Valheim1CompatBridges");

		private static int _added;

		private const bool F = false;

		private const bool T = true;

		private static readonly object D = null;

		private static MethodReference _stableHash;

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

		private static Lit L(long v)
		{
			return new Lit(v);
		}

		public static void Patch(AssemblyDefinition assembly)
		{
			ModuleDefinition mainModule = assembly.MainModule;
			_added = 0;
			BridgeInventoryChanged(mainModule);
			ConstToStaticField(mainModule, "ZRoutedRpc", "Everybody");
			InjectInventoryGridElement(mainModule);
			InjectMethodStub(mainModule, "Minimap", "OnMapRightClick");
			InjectAwakeCalledFromOnEnable(mainModule, "InventoryGrid");
			DefaultInterfaceMethod(mainModule, "Hoverable", "GetHoverOffset");
			Fwd(mainModule, "Character", "Message", A("MessageHud/MessageType", "System.String", "System.Int32", "UnityEngine.Sprite"), S(0, 1, 2, 3, false));
			Fwd(mainModule, "MessageHud", "ShowMessage", A("MessageHud/MessageType", "System.String", "System.Int32", "UnityEngine.Sprite", "System.Boolean"), S(0, 1, 2, 3, 4, true));
			Fwd(mainModule, "EffectList", "Create", A("UnityEngine.Vector3", "UnityEngine.Quaternion", "UnityEngine.Transform", "System.Single", "System.Int32"), S(0, 1, 2, 3, 4, D));
			Fwd(mainModule, "SEMan", "AddStatusEffect", A("System.Int32", "System.Boolean", "System.Int32", "System.Single"), S(0, 1, 2, 3, L(-1L)));
			Fwd(mainModule, "SEMan", "AddStatusEffect", A("StatusEffect", "System.Boolean", "System.Int32", "System.Single"), S(0, 1, 2, 3, L(-1L)));
			Fwd(mainModule, "ItemDrop/ItemData", "GetTooltip", A("ItemDrop/ItemData", "System.Int32", "System.Boolean", "System.Single", "System.Int32"), S(0, 1, 2, 3, 4, false));
			Fwd(mainModule, "Inventory", "IsTeleportable", A(), S(false));
			Fwd(mainModule, "Humanoid", "IsTeleportable", A(), S(false));
			Fwd(mainModule, "Inventory", "AddItem", A("System.String", "System.Int32", "System.Single", "Vector2i", "System.Boolean", "System.Int32", "System.Int32", "System.Int64", "System.String", "System.Collections.Generic.Dictionary`2<System.String,System.String>", "System.Int32", "System.Boolean"), S(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, false, false));
			Fwd(mainModule, "Terminal/ConsoleCommand", ".ctor", A("System.String", "System.String", "Terminal/ConsoleEvent", "System.Boolean", "System.Boolean", "System.Boolean", "System.Boolean", "System.Boolean", "Terminal/ConsoleOptionsFetcher", "System.Boolean", "System.Boolean", "System.Boolean"), S(0, 1, 2, 3, 4, 5, 6, 7, false, 8, 9, 10, 11));
			Fwd(mainModule, "Terminal/ConsoleEventArgs", ".ctor", A("System.String", "Terminal"), S(0, 1, D));
			Fwd(mainModule, "YesNoPopup", ".ctor", A("System.String", "System.String", "PopupButtonCallback", "PopupButtonCallback", "System.Boolean"), S(0, 1, 2, 3, 4, false));
			Fwd(mainModule, "Heightmap", "Poke", A("System.Boolean"), S(D, 0));
			Fwd(mainModule, "TerrainComp", "Save", A(), S(false));
			Fwd(mainModule, "Piece", "SetCreator", A("System.Int64"), S(0, D));
			Fwd(mainModule, "VisEquipment", "SetChestItem", A("System.String"), S(0));
			Fwd(mainModule, "VisEquipment", "SetLegItem", A("System.String"), S(0));
			Fwd(mainModule, "VisEquipment", "SetHelmetItem", A("System.String"), S(0));
			Fwd(mainModule, "VisEquipment", "SetRightItem", A("System.String"), S(0, D));
			Fwd(mainModule, "VisEquipment", "SetRightBackItem", A("System.String"), S(0, D));
			Fwd(mainModule, "VisEquipment", "SetLeftItem", A("System.String", "System.Int32"), S(0, 1, D));
			Fwd(mainModule, "VisEquipment", "SetLeftBackItem", A("System.String", "System.Int32"), S(0, 1, D));
			Fwd(mainModule, "VisEquipment", "SetShoulderItem", A("System.String", "System.Int32"), S(0, 1, D));
			Log.LogInfo((object)$"Gotowe: {_added} mostkow/wstrzykniec w assembly_valheim.dll.");
		}

		private static string[] A(params string[] types)
		{
			return types;
		}

		private static object[] S(params object[] spec)
		{
			return spec;
		}

		private static void BridgeInventoryChanged(ModuleDefinition module)
		{
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c4: Expected O, but got Unknown
			//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f3: Unknown result type (might be due to invalid IL or missing references)
			//IL_0104: Unknown result type (might be due to invalid IL or missing references)
			//IL_0115: 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_0155: 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_01a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fa: 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_020d: Unknown result type (might be due to invalid IL or missing references)
			//IL_021b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0227: Unknown result type (might be due to invalid IL or missing references)
			//IL_0233: Unknown result type (might be due to invalid IL or missing references)
			TypeDefinition type = module.GetType("Inventory");
			if (type == null)
			{
				Log.LogWarning((object)"Brak typu Inventory.");
				return;
			}
			if (((IEnumerable<MethodDefinition>)type.Methods).Any((MethodDefinition x) => ((MemberReference)x).Name == "Changed" && !((MethodReference)x).HasParameters))
			{
				Log.LogInfo((object)"Inventory.Changed() juz istnieje.");
				return;
			}
			MethodDefinition val = ((IEnumerable<MethodDefinition>)type.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition x) => ((MemberReference)x).Name == "Changed" && ((MethodReference)x).Parameters.Count == 2 && ((IEnumerable<ParameterDefinition>)((MethodReference)x).Parameters).All((ParameterDefinition p) => (int)((ParameterReference)p).ParameterType.MetadataType == 2)));
			if (val == null)
			{
				Log.LogWarning((object)"Inventory nie ma Changed(bool, bool) - most pominiety.");
				return;
			}
			((MemberReference)val).Name = "ChangedEx";
			MethodDefinition val2 = new MethodDefinition("Changed", (MethodAttributes)134, module.ImportReference(typeof(void)));
			ILProcessor iLProcessor = val2.Body.GetILProcessor();
			iLProcessor.Append(iLProcessor.Create(OpCodes.Ldarg_0));
			iLProcessor.Append(iLProcessor.Create(OpCodes.Ldc_I4_0));
			iLProcessor.Append(iLProcessor.Create(OpCodes.Ldc_I4_0));
			iLProcessor.Append(iLProcessor.Create(OpCodes.Call, (MethodReference)(object)val));
			iLProcessor.Append(iLProcessor.Create(OpCodes.Ret));
			type.Methods.Add(val2);
			int num = 0;
			foreach (TypeDefinition type2 in module.GetTypes())
			{
				Enumerator<MethodDefinition> enumerator2 = type2.Methods.GetEnumerator();
				try
				{
					while (enumerator2.MoveNext())
					{
						MethodDefinition current = enumerator2.Current;
						if (!current.HasBody || current == val2)
						{
							continue;
						}
						foreach (Instruction item in ((IEnumerable<Instruction>)current.Body.Instructions).ToList())
						{
							if ((!(item.OpCode != OpCodes.Call) || !(item.OpCode != OpCodes.Callvirt)) && item.Operand == val)
							{
								Instruction previous = item.Previous;
								Instruction val3 = ((previous != null) ? previous.Previous : null);
								if (previous != null && val3 != null && !(previous.OpCode != OpCodes.Ldc_I4_0) && !(val3.OpCode != OpCodes.Ldc_I4_0))
								{
									previous.OpCode = OpCodes.Nop;
									val3.OpCode = OpCodes.Nop;
									item.OpCode = OpCodes.Call;
									item.Operand = val2;
									num++;
								}
							}
						}
					}
				}
				finally
				{
					((IDisposable)enumerator2/*cast due to .constrained prefix*/).Dispose();
				}
			}
			_added++;
			Log.LogInfo((object)$"Inventory.Changed() -> ChangedEx(false, false); przepieto {num} wywolan gry.");
		}

		private static void ConstToStaticField(ModuleDefinition module, string typeName, string fieldName)
		{
			//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_010e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0115: Expected O, but got Unknown
			//IL_0121: Unknown result type (might be due to invalid IL or missing references)
			//IL_0173: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0187: Unknown result type (might be due to invalid IL or missing references)
			TypeDefinition type = module.GetType(typeName);
			FieldDefinition val = ((type != null) ? ((IEnumerable<FieldDefinition>)type.Fields).FirstOrDefault((Func<FieldDefinition, bool>)((FieldDefinition x) => ((MemberReference)x).Name == fieldName)) : null);
			if (val == null)
			{
				Log.LogWarning((object)("Brak pola " + typeName + "." + fieldName + "."));
				return;
			}
			if (!val.HasConstant)
			{
				Log.LogInfo((object)(typeName + "." + fieldName + " nie jest const - nic do roboty."));
				return;
			}
			object constant = val.Constant;
			val.HasConstant = false;
			val.Constant = null;
			val.Attributes = (FieldAttributes)((val.Attributes & 0x7FBF) | 0x10);
			MethodDefinition val2 = ((IEnumerable<MethodDefinition>)type.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition x) => ((MemberReference)x).Name == ".cctor"));
			if (val2 == null)
			{
				val2 = new MethodDefinition(".cctor", (MethodAttributes)6289, module.ImportReference(typeof(void)));
				val2.Body.GetILProcessor().Append(Instruction.Create(OpCodes.Ret));
				type.Methods.Add(val2);
			}
			ILProcessor iLProcessor = val2.Body.GetILProcessor();
			Instruction val3 = val2.Body.Instructions[0];
			Instruction val4 = ((constant is long num) ? iLProcessor.Create(OpCodes.Ldc_I8, num) : iLProcessor.Create(OpCodes.Ldc_I4, Convert.ToInt32(constant)));
			iLProcessor.InsertBefore(val3, val4);
			iLProcessor.InsertBefore(val3, iLProcessor.Create(OpCodes.Stsfld, (FieldReference)(object)val));
			_added++;
			Log.LogInfo((object)$"{typeName}.{fieldName}: const -> statyczne pole (= {constant}).");
		}

		private static void InjectInventoryGridElement(ModuleDefinition module)
		{
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_0096: Expected O, but got Unknown
			//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d2: Expected O, but got Unknown
			//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_0102: Unknown result type (might be due to invalid IL or missing references)
			//IL_0175: Unknown result type (might be due to invalid IL or missing references)
			//IL_017c: Expected O, but got Unknown
			//IL_0217: Unknown result type (might be due to invalid IL or missing references)
			//IL_0221: Expected O, but got Unknown
			//IL_0286: Unknown result type (might be due to invalid IL or missing references)
			//IL_028d: Expected O, but got Unknown
			//IL_029a: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a1: Expected O, but got Unknown
			//IL_02d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d9: Expected O, but got Unknown
			//IL_02ec: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f6: Expected O, but got Unknown
			//IL_0306: Unknown result type (might be due to invalid IL or missing references)
			//IL_0316: Unknown result type (might be due to invalid IL or missing references)
			//IL_0329: Unknown result type (might be due to invalid IL or missing references)
			//IL_033e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0395: Unknown result type (might be due to invalid IL or missing references)
			//IL_03a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_03bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_03d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_0406: Unknown result type (might be due to invalid IL or missing references)
			//IL_0419: 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_0441: Unknown result type (might be due to invalid IL or missing references)
			//IL_0456: Unknown result type (might be due to invalid IL or missing references)
			//IL_046b: Unknown result type (might be due to invalid IL or missing references)
			//IL_047e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0491: Unknown result type (might be due to invalid IL or missing references)
			//IL_04a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_04bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_04d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_055c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0563: Expected O, but got Unknown
			//IL_056a: Unknown result type (might be due to invalid IL or missing references)
			//IL_056f: Unknown result type (might be due to invalid IL or missing references)
			//IL_06c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_06c9: Expected O, but got Unknown
			//IL_06de: Unknown result type (might be due to invalid IL or missing references)
			//IL_06e5: Expected O, but got Unknown
			//IL_0704: Unknown result type (might be due to invalid IL or missing references)
			//IL_0709: Unknown result type (might be due to invalid IL or missing references)
			//IL_0712: Expected O, but got Unknown
			//IL_058c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0598: Unknown result type (might be due to invalid IL or missing references)
			//IL_05a2: Expected O, but got Unknown
			//IL_05cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_05e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_061c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0631: Unknown result type (might be due to invalid IL or missing references)
			//IL_0646: Unknown result type (might be due to invalid IL or missing references)
			//IL_0771: Unknown result type (might be due to invalid IL or missing references)
			//IL_0786: Unknown result type (might be due to invalid IL or missing references)
			//IL_07a3: Unknown result type (might be due to invalid IL or missing references)
			TypeDefinition type = module.GetType("InventoryGrid");
			TypeDefinition src = module.GetType("InventoryElement");
			if (type == null || src == null)
			{
				Log.LogWarning((object)"Brak InventoryGrid/InventoryElement.");
				return;
			}
			if (((IEnumerable<TypeDefinition>)type.NestedTypes).Any((TypeDefinition x) => ((MemberReference)x).Name == "Element"))
			{
				Log.LogInfo((object)"InventoryGrid.Element juz istnieje.");
				return;
			}
			TypeDefinition val = new TypeDefinition((string)null, "Element", (TypeAttributes)1048578, module.TypeSystem.Object);
			MethodReference val2 = module.ImportReference((MethodBase)typeof(object).GetConstructor(Type.EmptyTypes));
			MethodDefinition val3 = new MethodDefinition(".ctor", (MethodAttributes)6278, module.ImportReference(typeof(void)));
			ILProcessor iLProcessor = val3.Body.GetILProcessor();
			iLProcessor.Append(iLProcessor.Create(OpCodes.Ldarg_0));
			iLProcessor.Append(iLProcessor.Create(OpCodes.Call, val2));
			iLProcessor.Append(iLProcessor.Create(OpCodes.Ret));
			val.Methods.Add(val3);
			List<FieldDefinition> list = new List<FieldDefinition>();
			foreach (FieldDefinition item in ((IEnumerable<FieldDefinition>)src.Fields).Where((FieldDefinition x) => !x.IsStatic && ((MemberReference)x).Name.StartsWith("m_")))
			{
				FieldDefinition val4 = new FieldDefinition(((MemberReference)item).Name, (FieldAttributes)6, ((FieldReference)item).FieldType);
				val.Fields.Add(val4);
				list.Add(val4);
			}
			MethodDefinition val5 = ((IEnumerable<MethodDefinition>)src.Methods).First((MethodDefinition x) => ((MemberReference)x).Name == "GetElementRectTransform");
			MethodDefinition val6 = ((IEnumerable<MethodDefinition>)src.Methods).First((MethodDefinition x) => ((MemberReference)x).Name == "get_Position");
			AssemblyDefinition val7 = module.AssemblyResolver.Resolve(new AssemblyNameReference("UnityEngine.CoreModule", (Version)null));
			TypeDefinition type2 = val7.MainModule.GetType("UnityEngine.Component");
			MethodReference val8 = module.ImportReference((MethodReference)(object)((IEnumerable<MethodDefinition>)type2.Methods).First((MethodDefinition x) => ((MemberReference)x).Name == "get_gameObject"));
			FieldDefinition val9 = new FieldDefinition("m_go", (FieldAttributes)6, module.ImportReference((TypeReference)(object)val7.MainModule.GetType("UnityEngine.GameObject")));
			FieldDefinition val10 = new FieldDefinition("m_pos", (FieldAttributes)6, ((MethodReference)val6).ReturnType);
			val.Fields.Add(val9);
			val.Fields.Add(val10);
			type.NestedTypes.Add(val);
			MethodDefinition val11 = new MethodDefinition("Wrap", (MethodAttributes)150, (TypeReference)(object)val);
			((MethodReference)val11).Parameters.Add(new ParameterDefinition("e", (ParameterAttributes)0, (TypeReference)(object)src));
			ILProcessor iLProcessor2 = val11.Body.GetILProcessor();
			Instruction val12 = iLProcessor2.Create(OpCodes.Ldnull);
			iLProcessor2.Append(iLProcessor2.Create(OpCodes.Ldarg_0));
			iLProcessor2.Append(iLProcessor2.Create(OpCodes.Brfalse, val12));
			iLProcessor2.Append(iLProcessor2.Create(OpCodes.Newobj, (MethodReference)(object)val3));
			foreach (FieldDefinition nf in list)
			{
				FieldDefinition val13 = ((IEnumerable<FieldDefinition>)src.Fields).First((FieldDefinition x) => ((MemberReference)x).Name == ((MemberReference)nf).Name);
				iLProcessor2.Append(iLProcessor2.Create(OpCodes.Dup));
				iLProcessor2.Append(iLProcessor2.Create(OpCodes.Ldarg_0));
				iLProcessor2.Append(iLProcessor2.Create(OpCodes.Ldfld, (FieldReference)(object)val13));
				iLProcessor2.Append(iLProcessor2.Create(OpCodes.Stfld, (FieldReference)(object)nf));
			}
			iLProcessor2.Append(iLProcessor2.Create(OpCodes.Dup));
			iLProcessor2.Append(iLProcessor2.Create(OpCodes.Ldarg_0));
			iLProcessor2.Append(iLProcessor2.Create(OpCodes.Callvirt, (MethodReference)(object)val5));
			iLProcessor2.Append(iLProcessor2.Create(OpCodes.Callvirt, val8));
			iLProcessor2.Append(iLProcessor2.Create(OpCodes.Stfld, (FieldReference)(object)val9));
			iLProcessor2.Append(iLProcessor2.Create(OpCodes.Dup));
			iLProcessor2.Append(iLProcessor2.Create(OpCodes.Ldarg_0));
			iLProcessor2.Append(iLProcessor2.Create(OpCodes.Callvirt, (MethodReference)(object)val6));
			iLProcessor2.Append(iLProcessor2.Create(OpCodes.Stfld, (FieldReference)(object)val10));
			iLProcessor2.Append(iLProcessor2.Create(OpCodes.Ret));
			iLProcessor2.Append(val12);
			iLProcessor2.Append(iLProcessor2.Create(OpCodes.Ret));
			val.Methods.Add(val11);
			string[] array = new string[2] { "GetElement", "GetHoveredElement" };
			foreach (string name in array)
			{
				MethodDefinition val14 = ((IEnumerable<MethodDefinition>)type.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition x) => ((MemberReference)x).Name == name && (object)((MethodReference)x).ReturnType == src));
				if (val14 == null)
				{
					continue;
				}
				MethodDefinition val15 = new MethodDefinition(name, (MethodAttributes)134, (TypeReference)(object)val);
				Enumerator<ParameterDefinition> enumerator3 = ((MethodReference)val14).Parameters.GetEnumerator();
				try
				{
					while (enumerator3.MoveNext())
					{
						ParameterDefinition current2 = enumerator3.Current;
						((MethodReference)val15).Parameters.Add(new ParameterDefinition(((ParameterReference)current2).Name, current2.Attributes, ((ParameterReference)current2).ParameterType));
					}
				}
				finally
				{
					((IDisposable)enumerator3/*cast due to .constrained prefix*/).Dispose();
				}
				ILProcessor iLProcessor3 = val15.Body.GetILProcessor();
				iLProcessor3.Append(iLProcessor3.Create(OpCodes.Ldarg_0));
				for (int num2 = 0; num2 < ((MethodReference)val15).Parameters.Count; num2++)
				{
					iLProcessor3.Append(iLProcessor3.Create(OpCodes.Ldarg, ((MethodReference)val15).Parameters[num2]));
				}
				iLProcessor3.Append(iLProcessor3.Create(OpCodes.Call, (MethodReference)(object)val14));
				iLProcessor3.Append(iLProcessor3.Create(OpCodes.Call, (MethodReference)(object)val11));
				iLProcessor3.Append(iLProcessor3.Create(OpCodes.Ret));
				type.Methods.Add(val15);
			}
			FieldDefinition val16 = ((IEnumerable<FieldDefinition>)type.Fields).FirstOrDefault((Func<FieldDefinition, bool>)((FieldDefinition x) => ((MemberReference)x).Name == "m_elements"));
			if (val16 != null)
			{
				TypeReference fieldType = ((FieldReference)val16).FieldType;
				GenericInstanceType val17 = (GenericInstanceType)(object)((fieldType is GenericInstanceType) ? fieldType : null);
				if (val17 != null)
				{
					GenericInstanceType val18 = new GenericInstanceType(((TypeSpecification)val17).ElementType);
					val18.GenericArguments.Add((TypeReference)(object)val);
					FieldDefinition val19 = new FieldDefinition("m_elements_compat", (FieldAttributes)6, (TypeReference)(object)val18);
					type.Fields.Add(val19);
					MethodReference val20 = new MethodReference(".ctor", module.TypeSystem.Void, (TypeReference)(object)val18)
					{
						HasThis = true
					};
					foreach (MethodDefinition item2 in ((IEnumerable<MethodDefinition>)type.Methods).Where((MethodDefinition x) => x.IsConstructor && !x.IsStatic && x.HasBody))
					{
						ILProcessor iLProcessor4 = item2.Body.GetILProcessor();
						Instruction val21 = item2.Body.Instructions[0];
						iLProcessor4.InsertBefore(val21, iLProcessor4.Create(OpCodes.Ldarg_0));
						iLProcessor4.InsertBefore(val21, iLProcessor4.Create(OpCodes.Newobj, module.ImportReference(val20)));
						iLProcessor4.InsertBefore(val21, iLProcessor4.Create(OpCodes.Stfld, (FieldReference)(object)val19));
					}
				}
			}
			_added++;
			Log.LogInfo((object)$"InventoryGrid.Element (wrapper, {list.Count + 2} pol) + przeciazenia GetElement/GetHoveredElement + pusty List<Element> m_elements_compat.");
		}

		private static void DefaultInterfaceMethod(ModuleDefinition module, string ifaceName, string methodName)
		{
			//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c1: Invalid comparison between Unknown and I4
			//IL_01d4: 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_00ce: Invalid comparison between Unknown and I4
			//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f3: Invalid comparison between Unknown and I4
			//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_0115: Unknown result type (might be due to invalid IL or missing references)
			//IL_011c: Invalid comparison between Unknown and I4
			//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_0120: 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_01b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_0147: Unknown result type (might be due to invalid IL or missing references)
			//IL_014e: Expected O, but got Unknown
			//IL_016e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0181: Unknown result type (might be due to invalid IL or missing references)
			//IL_0193: Unknown result type (might be due to invalid IL or missing references)
			TypeDefinition type = module.GetType(ifaceName);
			MethodDefinition val = ((type != null) ? ((IEnumerable<MethodDefinition>)type.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition x) => ((MemberReference)x).Name == methodName)) : null);
			if (val == null)
			{
				Log.LogWarning((object)("Brak " + ifaceName + "." + methodName + "."));
				return;
			}
			if (val.HasBody)
			{
				Log.LogInfo((object)(ifaceName + "." + methodName + " ma juz cialo."));
				return;
			}
			val.IsAbstract = false;
			val.IsVirtual = true;
			ILProcessor iLProcessor = val.Body.GetILProcessor();
			TypeReference returnType = ((MethodReference)val).ReturnType;
			if ((int)returnType.MetadataType != 1)
			{
				if ((int)returnType.MetadataType == 12)
				{
					iLProcessor.Append(iLProcessor.Create(OpCodes.Ldc_R4, 0f));
				}
				else if ((int)returnType.MetadataType == 13)
				{
					iLProcessor.Append(iLProcessor.Create(OpCodes.Ldc_R8, 0.0));
				}
				else if ((int)returnType.MetadataType == 10)
				{
					iLProcessor.Append(iLProcessor.Create(OpCodes.Ldc_I8, 0L));
				}
				else if (returnType.IsValueType && !returnType.IsPrimitive)
				{
					VariableDefinition val2 = new VariableDefinition(returnType);
					val.Body.Variables.Add(val2);
					val.Body.InitLocals = true;
					iLProcessor.Append(iLProcessor.Create(OpCodes.Ldloca, val2));
					iLProcessor.Append(iLProcessor.Create(OpCodes.Initobj, returnType));
					iLProcessor.Append(iLProcessor.Create(OpCodes.Ldloc, val2));
				}
				else if (returnType.IsValueType)
				{
					iLProcessor.Append(iLProcessor.Create(OpCodes.Ldc_I4_0));
				}
				else
				{
					iLProcessor.Append(iLProcessor.Create(OpCodes.Ldnull));
				}
			}
			iLProcessor.Append(iLProcessor.Create(OpCodes.Ret));
			_added++;
			Log.LogInfo((object)(ifaceName + "." + methodName + ": domyslna implementacja w interfejsie (dla klas ze starych modow)."));
		}

		private static void InjectMethodStub(ModuleDefinition module, string typeName, string methodName)
		{
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_008e: Expected O, but got Unknown
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			TypeDefinition type = module.GetType(typeName);
			if (type == null)
			{
				Log.LogWarning((object)("Brak typu " + typeName + "."));
				return;
			}
			if (((IEnumerable<MethodDefinition>)type.Methods).Any((MethodDefinition x) => ((MemberReference)x).Name == methodName))
			{
				Log.LogInfo((object)(typeName + "." + methodName + " istnieje - stub pominiety."));
				return;
			}
			MethodDefinition val = new MethodDefinition(methodName, (MethodAttributes)134, module.ImportReference(typeof(void)));
			val.Body.GetILProcessor().Append(Instruction.Create(OpCodes.Ret));
			type.Methods.Add(val);
			_added++;
			Log.LogInfo((object)(typeName + "." + methodName + "(): pusty stub (w 1.0 tej metody nie ma)."));
		}

		private static void InjectAwakeCalledFromOnEnable(ModuleDefinition module, string typeName)
		{
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cc: Expected O, but got Unknown
			//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_0115: Unknown result type (might be due to invalid IL or missing references)
			//IL_0128: Unknown result type (might be due to invalid IL or missing references)
			TypeDefinition type = module.GetType(typeName);
			if (type == null)
			{
				Log.LogWarning((object)("Brak typu " + typeName + "."));
				return;
			}
			if (((IEnumerable<MethodDefinition>)type.Methods).Any((MethodDefinition x) => ((MemberReference)x).Name == "Awake"))
			{
				Log.LogInfo((object)(typeName + ".Awake istnieje."));
				return;
			}
			MethodDefinition val = ((IEnumerable<MethodDefinition>)type.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition x) => ((MemberReference)x).Name == "OnEnable" && x.HasBody));
			if (val == null)
			{
				Log.LogWarning((object)(typeName + " nie ma OnEnable - Awake pominiete."));
				return;
			}
			MethodDefinition val2 = new MethodDefinition("Awake", (MethodAttributes)134, module.ImportReference(typeof(void)));
			val2.Body.GetILProcessor().Append(Instruction.Create(OpCodes.Ret));
			type.Methods.Add(val2);
			ILProcessor iLProcessor = val.Body.GetILProcessor();
			Instruction val3 = val.Body.Instructions[0];
			iLProcessor.InsertBefore(val3, iLProcessor.Create(OpCodes.Ldarg_0));
			iLProcessor.InsertBefore(val3, iLProcessor.Create(OpCodes.Call, (MethodReference)(object)val2));
			_added++;
			Log.LogInfo((object)(typeName + ".Awake() dodane i wolane z OnEnable."));
		}

		private static MethodReference StableHash(ModuleDefinition module)
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Expected O, but got Unknown
			if (_stableHash != null)
			{
				return _stableHash;
			}
			MethodDefinition val = ((IEnumerable<MethodDefinition>)module.AssemblyResolver.Resolve(new AssemblyNameReference("assembly_utils", (Version)null)).MainModule.GetType("StringExtensionMethods").Methods).First((MethodDefinition x) => ((MemberReference)x).Name == "GetStableHashCode" && ((MethodReference)x).Parameters.Count == 1);
			return _stableHash = module.ImportReference((MethodReference)(object)val);
		}

		private static void Fwd(ModuleDefinition module, string typeName, string methodName, string[] oldTypes, object[] spec)
		{
			//IL_0112: Unknown result type (might be due to invalid IL or missing references)
			//IL_011b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0121: Unknown result type (might be due to invalid IL or missing references)
			//IL_0122: Unknown result type (might be due to invalid IL or missing references)
			//IL_0136: Unknown result type (might be due to invalid IL or missing references)
			//IL_013d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0144: Expected O, but got Unknown
			//IL_012b: 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_012f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0240: 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_04e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fe: Expected O, but got Unknown
			//IL_028c: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c9: Invalid comparison between Unknown and I4
			//IL_0330: Unknown result type (might be due to invalid IL or missing references)
			//IL_0337: Invalid comparison between Unknown and I4
			//IL_030d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0306: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_035c: Unknown result type (might be due to invalid IL or missing references)
			//IL_033d: Unknown result type (might be due to invalid IL or missing references)
			//IL_04a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_0412: Unknown result type (might be due to invalid IL or missing references)
			//IL_0419: Invalid comparison between Unknown and I4
			//IL_038f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0395: Invalid comparison between Unknown and I4
			//IL_042a: Unknown result type (might be due to invalid IL or missing references)
			//IL_041d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0424: Invalid comparison between Unknown and I4
			//IL_0399: Unknown result type (might be due to invalid IL or missing references)
			//IL_03a0: Expected O, but got Unknown
			//IL_03c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_03d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_03ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_043f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0446: Invalid comparison between Unknown and I4
			//IL_0464: Unknown result type (might be due to invalid IL or missing references)
			//IL_046b: Invalid comparison between Unknown and I4
			//IL_044c: Unknown result type (might be due to invalid IL or missing references)
			//IL_048f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0471: Unknown result type (might be due to invalid IL or missing references)
			TypeDefinition type = module.GetType(typeName);
			if (type == null)
			{
				Log.LogWarning((object)("Brak typu " + typeName + " - pomijam " + methodName + "."));
				return;
			}
			MethodDefinition val = ((IEnumerable<MethodDefinition>)type.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition x) => ((MemberReference)x).Name == methodName && ((MethodReference)x).Parameters.Count == spec.Length && !((IEnumerable<ParameterDefinition>)((MethodReference)x).Parameters).Select((ParameterDefinition p) => ((MemberReference)((ParameterReference)p).ParameterType).FullName).SequenceEqual(oldTypes) && SpecFits(x, oldTypes, spec)));
			if (val == null)
			{
				Log.LogWarning((object)$"Brak {typeName}.{methodName} z {spec.Length} parametrami pasujacymi do specyfikacji - gra znow zmienila API, most pominiety.");
				return;
			}
			if (((IEnumerable<MethodDefinition>)type.Methods).Any((MethodDefinition x) => ((MemberReference)x).Name == methodName && ((IEnumerable<ParameterDefinition>)((MethodReference)x).Parameters).Select((ParameterDefinition p) => ((MemberReference)((ParameterReference)p).ParameterType).FullName).SequenceEqual(oldTypes)))
			{
				Log.LogInfo((object)(typeName + "." + methodName + "(" + string.Join(",", oldTypes) + ") juz istnieje."));
				return;
			}
			MethodAttributes val2 = (MethodAttributes)134;
			if (val.IsConstructor)
			{
				val2 = (MethodAttributes)(val2 | 0x1800);
			}
			if (val.IsStatic)
			{
				val2 = (MethodAttributes)(val2 | 0x10);
			}
			MethodDefinition val3 = new MethodDefinition(methodName, val2, ((MethodReference)val).ReturnType);
			int i;
			for (i = 0; i < oldTypes.Length; i++)
			{
				int num = Array.FindIndex(spec, (object s) => s is int num4 && num4 == i);
				TypeReference val4 = ((num >= 0 && oldTypes[i] != "System.String") ? ((ParameterReference)((MethodReference)val).Parameters[num]).ParameterType : ResolveType(module, oldTypes[i]));
				((MethodReference)val3).Parameters.Add(new ParameterDefinition((num >= 0) ? ((ParameterReference)((MethodReference)val).Parameters[num]).Name : ("p" + i), (ParameterAttributes)0, val4));
			}
			ILProcessor iLProcessor = val3.Body.GetILProcessor();
			if (!val.IsStatic)
			{
				iLProcessor.Append(iLProcessor.Create(OpCodes.Ldarg_0));
			}
			for (int num2 = 0; num2 < spec.Length; num2++)
			{
				TypeReference parameterType = ((ParameterReference)((MethodReference)val).Parameters[num2]).ParameterType;
				object obj = spec[num2];
				if (obj is int num3)
				{
					iLProcessor.Append(iLProcessor.Create(OpCodes.Ldarg, ((MethodReference)val3).Parameters[num3]));
					if (oldTypes[num3] == "System.String" && (int)parameterType.MetadataType == 8)
					{
						iLProcessor.Append(iLProcessor.Create(OpCodes.Call, StableHash(module)));
					}
				}
				else if (obj is bool flag)
				{
					iLProcessor.Append(iLProcessor.Create(flag ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0));
				}
				else if (obj is Lit lit)
				{
					if ((int)parameterType.MetadataType == 10)
					{
						iLProcessor.Append(iLProcessor.Create(OpCodes.Ldc_I8, lit.V));
					}
					else
					{
						iLProcessor.Append(iLProcessor.Create(OpCodes.Ldc_I4, (int)lit.V));
					}
				}
				else if (parameterType.IsValueType && !parameterType.IsPrimitive && (int)parameterType.MetadataType != 2)
				{
					VariableDefinition val5 = new VariableDefinition(parameterType);
					val3.Body.Variables.Add(val5);
					val3.Body.InitLocals = true;
					iLProcessor.Append(iLProcessor.Create(OpCodes.Ldloca, val5));
					iLProcessor.Append(iLProcessor.Create(OpCodes.Initobj, parameterType));
					iLProcessor.Append(iLProcessor.Create(OpCodes.Ldloc, val5));
				}
				else if (parameterType.IsValueType)
				{
					if ((int)parameterType.MetadataType == 10 || (int)parameterType.MetadataType == 11)
					{
						iLProcessor.Append(iLProcessor.Create(OpCodes.Ldc_I8, 0L));
					}
					else if ((int)parameterType.MetadataType == 12)
					{
						iLProcessor.Append(iLProcessor.Create(OpCodes.Ldc_R4, 0f));
					}
					else if ((int)parameterType.MetadataType == 13)
					{
						iLProcessor.Append(iLProcessor.Create(OpCodes.Ldc_R8, 0.0));
					}
					else
					{
						iLProcessor.Append(iLProcessor.Create(OpCodes.Ldc_I4_0));
					}
				}
				else
				{
					iLProcessor.Append(iLProcessor.Create(OpCodes.Ldnull));
				}
			}
			iLProcessor.Append(iLProcessor.Create(OpCodes.Call, (MethodReference)(object)val));
			iLProcessor.Append(iLProcessor.Create(OpCodes.Ret));
			type.Methods.Add(val3);
			_added++;
			Log.LogInfo((object)string.Format("{0}.{1}({2}) -> {3} z {4} parametrami.", typeName, methodName, string.Join(", ", oldTypes.Select(Short)), methodName, spec.Length));
		}

		private static bool SpecFits(MethodDefinition candidate, string[] oldTypes, object[] spec)
		{
			for (int i = 0; i < spec.Length; i++)
			{
				if (spec[i] is int num)
				{
					string text = oldTypes[num];
					string fullName = ((MemberReference)((ParameterReference)((MethodReference)candidate).Parameters[i]).ParameterType).FullName;
					if (!(text == fullName) && (!(text == "System.String") || !(fullName == "System.Int32")))
					{
						return false;
					}
				}
			}
			return true;
		}

		private static TypeReference ResolveType(ModuleDefinition module, string fullName)
		{
			switch (fullName)
			{
			case "System.String":
				return module.TypeSystem.String;
			case "System.Int32":
				return module.TypeSystem.Int32;
			case "System.Boolean":
				return module.TypeSystem.Boolean;
			default:
			{
				TypeDefinition type = module.GetType(fullName);
				if (type != null)
				{
					return (TypeReference)(object)type;
				}
				throw new InvalidOperationException("Nie umiem rozwiazac typu " + fullName);
			}
			}
		}

		private static string Short(string fullName)
		{
			return fullName.Substring(fullName.LastIndexOfAny(new char[2] { '.', '/' }) + 1);
		}
	}
}