Decompiled source of InventoryChangedCompat v1.0.0

patchers/InventoryChangedCompat/InventoryChangedCompat.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Collections.Generic;

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

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

	private static void Say(string msg)
	{
		Console.WriteLine("[InventoryChangedCompat] " + msg);
	}

	public static void Patch(AssemblyDefinition assembly)
	{
		//IL_0029: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		//IL_007a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0080: Invalid comparison between Unknown and I4
		//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f3: Expected O, but got Unknown
		//IL_0105: Unknown result type (might be due to invalid IL or missing references)
		//IL_0118: Unknown result type (might be due to invalid IL or missing references)
		//IL_012b: Unknown result type (might be due to invalid IL or missing references)
		//IL_013e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0152: Unknown result type (might be due to invalid IL or missing references)
		//IL_0093: Unknown result type (might be due to invalid IL or missing references)
		//IL_0099: Invalid comparison between Unknown and I4
		TypeDefinition type = assembly.MainModule.GetType("Inventory");
		if (type == null)
		{
			Say("WARN: Inventory type not found; nothing to do.");
			return;
		}
		MethodDefinition val = null;
		MethodDefinition val2 = null;
		Enumerator<MethodDefinition> enumerator = type.Methods.GetEnumerator();
		try
		{
			while (enumerator.MoveNext())
			{
				MethodDefinition current = enumerator.Current;
				if (!(((MemberReference)current).Name != "Changed"))
				{
					if (((MethodReference)current).Parameters.Count == 0)
					{
						val = current;
					}
					if (((MethodReference)current).Parameters.Count == 2 && (int)((ParameterReference)((MethodReference)current).Parameters[0]).ParameterType.MetadataType == 2 && (int)((ParameterReference)((MethodReference)current).Parameters[1]).ParameterType.MetadataType == 2)
					{
						val2 = current;
					}
				}
			}
		}
		finally
		{
			((IDisposable)enumerator/*cast due to .constrained prefix*/).Dispose();
		}
		if (val != null)
		{
			Say("Inventory.Changed() already present; no patch needed.");
			return;
		}
		if (val2 == null)
		{
			Say("WARN: Inventory.Changed(bool,bool) not found; cannot bridge.");
			return;
		}
		MethodDefinition val3 = new MethodDefinition("Changed", (MethodAttributes)134, assembly.MainModule.TypeSystem.Void);
		ILProcessor iLProcessor = val3.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)val2));
		iLProcessor.Append(iLProcessor.Create(OpCodes.Ret));
		type.Methods.Add(val3);
		Say("Injected Inventory.Changed() -> Changed(false, false) [restores pre-1.0 overload for AzuCraftyBoxes and similar mods]");
	}
}