Decompiled source of CraftFromContainersFix v1.0.0

CraftFromContainersFix.dll

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

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("CraftFromContainersFix")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("CraftFromContainersFix")]
[assembly: AssemblyTitle("CraftFromContainersFix")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace CraftFromContainersFix
{
	[BepInPlugin("qua8ion.valheim.craftfromcontainersfix", "CraftFromContainersFix", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class CraftFromContainersFixPlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "qua8ion.valheim.craftfromcontainersfix";

		public const string PluginName = "CraftFromContainersFix";

		public const string PluginVersion = "1.0.0";

		private const string TargetGuid = "aedenthorn.CraftFromContainers";

		private const string TargetPatchType = "CraftFromContainers.BepInExPlugin+Smelter_OnAddOre_Patch";

		private void Awake()
		{
			//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d6: Expected O, but got Unknown
			CheatedFlagFix.Log = ((BaseUnityPlugin)this).Logger;
			if (!Chainloader.PluginInfos.TryGetValue("aedenthorn.CraftFromContainers", out var value) || (Object)(object)value.Instance == (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"CraftFromContainers is not loaded, nothing to fix");
				return;
			}
			Type type = ((object)value.Instance).GetType().Assembly.GetType("CraftFromContainers.BepInExPlugin+Smelter_OnAddOre_Patch");
			MethodInfo methodInfo = ((type == null) ? null : AccessTools.DeclaredMethod(type, "Prefix", (Type[])null, (Type[])null));
			if (methodInfo == null)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("Smelter_OnAddOre_Patch.Prefix not found in CraftFromContainers " + value.Metadata.Version?.ToString() + ", nothing patched"));
			}
			else
			{
				new Harmony("qua8ion.valheim.craftfromcontainersfix").Patch((MethodBase)methodInfo, (HarmonyMethod)null, (HarmonyMethod)null, new HarmonyMethod(typeof(CheatedFlagFix), "Transpiler", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null);
			}
		}
	}
	public static class CheatedFlagFix
	{
		internal static ManualLogSource Log;

		public static int LastReplaced { get; private set; }

		public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, MethodBase original)
		{
			FieldInfo fieldInfo = AccessTools.Field(typeof(ItemData), "m_cheated");
			FieldInfo fieldInfo2 = AccessTools.Field(typeof(ItemData), "m_dropPrefab");
			List<CodeInstruction> list = new List<CodeInstruction>(instructions);
			LastReplaced = 0;
			int num = FindItemArgument(original);
			if (fieldInfo == null || fieldInfo2 == null || num < 0)
			{
				ManualLogSource log = Log;
				if (log != null)
				{
					log.LogWarning((object)"ItemData.m_cheated, m_dropPrefab or the item parameter not found, nothing patched");
				}
				return list;
			}
			CodeInstruction val = null;
			for (int i = 1; i < list.Count; i++)
			{
				if (CodeInstructionExtensions.LoadsField(list[i], fieldInfo2, false) && CodeInstructionExtensions.IsLdloc(list[i - 1], (LocalBuilder)null))
				{
					val = list[i - 1];
				}
				else if (CodeInstructionExtensions.LoadsField(list[i], fieldInfo, false) && CodeInstructionExtensions.IsLdarg(list[i - 1], (int?)num))
				{
					if (val != null)
					{
						list[i - 1].opcode = val.opcode;
						list[i - 1].operand = val.operand;
					}
					else
					{
						list[i - 1].opcode = OpCodes.Ldc_I4_0;
						list[i - 1].operand = null;
						list[i].opcode = OpCodes.Nop;
						list[i].operand = null;
					}
					LastReplaced++;
				}
			}
			if (LastReplaced > 0)
			{
				ManualLogSource log2 = Log;
				if (log2 != null)
				{
					log2.LogInfo((object)("Fixed " + LastReplaced + " RPC_AddOre call(s) in CraftFromContainers"));
				}
			}
			else
			{
				ManualLogSource log3 = Log;
				if (log3 != null)
				{
					log3.LogInfo((object)"No broken RPC_AddOre calls found in CraftFromContainers, nothing to fix");
				}
			}
			return list;
		}

		private static int FindItemArgument(MethodBase original)
		{
			if (original == null)
			{
				return -1;
			}
			int num = ((!original.IsStatic) ? 1 : 0);
			ParameterInfo[] parameters = original.GetParameters();
			foreach (ParameterInfo parameterInfo in parameters)
			{
				if (parameterInfo.ParameterType == typeof(ItemData))
				{
					return parameterInfo.Position + num;
				}
			}
			return -1;
		}
	}
}