Decompiled source of StoreAndCraftMucCompat v1.0.0

StoreAndCraftMucCompat.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.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("PICS0UL")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+f2e90ec056aa4c7db5ffae9b79d3d286bd5f4764")]
[assembly: AssemblyProduct("StoreAndCraftMucCompat")]
[assembly: AssemblyTitle("StoreAndCraftMucCompat")]
[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 StoreAndCraftMucCompat
{
	[BepInPlugin("com.pics0ul.valheim.storeandcraftmuccompat", "StoreAndCraft MUC Compat", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "com.pics0ul.valheim.storeandcraftmuccompat";

		public const string PluginName = "StoreAndCraft MUC Compat";

		public const string PluginVersion = "1.0.0";

		public const string StoreAndCraftGuid = "com.morda.storeandcraft";

		public const string MultiUserChestGuid = "com.maxsch.valheim.MultiUserChest";

		internal static ManualLogSource ModLogger;

		internal static ConfigEntry<bool> Enabled;

		private void Awake()
		{
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0099: Expected O, but got Unknown
			ModLogger = ((BaseUnityPlugin)this).Logger;
			Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Let StoreAndCraft's take-stack pull from chests another player has open. Multi User Chest makes that safe. Turn this off to restore StoreAndCraft's stock behaviour without uninstalling.");
			MethodInfo methodInfo = ResolveTarget();
			if (methodInfo == null)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"StoreAndCraft.TransferService.TakeIntoExistingStack was not found. StoreAndCraft has probably been restructured, so take-stack on in-use chests stays disabled. Nothing else is affected and nothing is at risk.");
				return;
			}
			if (TakeStackPatch.IsInUseMethod == null)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"Container.IsInUse was not found; patch skipped.");
				return;
			}
			try
			{
				new Harmony("com.pics0ul.valheim.storeandcraftmuccompat").Patch((MethodBase)methodInfo, (HarmonyMethod)null, (HarmonyMethod)null, new HarmonyMethod(AccessTools.Method(typeof(TakeStackPatch), "Transpiler", (Type[])null, (Type[])null)), (HarmonyMethod)null, (HarmonyMethod)null);
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("Failed to patch TakeIntoExistingStack: " + ex.Message));
				return;
			}
			if (!TakeStackPatch.Applied)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"Patched TakeIntoExistingStack but found no Container.IsInUse call to neutralise. StoreAndCraft may have removed the guard itself. If take-stack already works on chests other players have open, this mod is no longer needed.");
			}
			else
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"StoreAndCraft MUC Compat 1.0.0 loaded - take-stack enabled on chests other players have open.");
			}
		}

		private static MethodInfo ResolveTarget()
		{
			Type type = AccessTools.TypeByName("StoreAndCraft.TransferService");
			if (!(type == null))
			{
				return AccessTools.Method(type, "TakeIntoExistingStack", (Type[])null, (Type[])null);
			}
			return null;
		}
	}
	internal static class TakeStackPatch
	{
		internal static readonly MethodInfo IsInUseMethod = AccessTools.Method(typeof(Container), "IsInUse", (Type[])null, (Type[])null);

		internal static bool Applied;

		internal static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			MethodInfo replacement = AccessTools.Method(typeof(TakeStackPatch), "BlocksRemoteTakeStack", (Type[])null, (Type[])null);
			foreach (CodeInstruction instruction in instructions)
			{
				if (CodeInstructionExtensions.Calls(instruction, IsInUseMethod))
				{
					Applied = true;
					instruction.opcode = OpCodes.Call;
					instruction.operand = replacement;
				}
				yield return instruction;
			}
		}

		public static bool BlocksRemoteTakeStack(Container container)
		{
			if ((Object)(object)container == (Object)null)
			{
				return false;
			}
			if (Plugin.Enabled != null && !Plugin.Enabled.Value)
			{
				return container.IsInUse();
			}
			return false;
		}
	}
}