Decompiled source of EffortlessHammer v1.0.1

plugins/EffortlessHammer.dll

Decompiled 2 days ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;

[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("EffortlessHammer")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1+136a8d4cfad58f59db927fe096fc67ef3939a75a")]
[assembly: AssemblyProduct("EffortlessHammer")]
[assembly: AssemblyTitle("EffortlessHammer")]
[assembly: AssemblyVersion("1.0.1.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 PlayToDate.EffortlessHammer
{
	[BepInPlugin("it.playtodate.effortlesshammer", "Effortless Hammer", "1.0.1")]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "it.playtodate.effortlesshammer";

		public const string Name = "Effortless Hammer";

		public const string Version = "1.0.1";

		private static ConfigEntry<bool> _hammer;

		private static ConfigEntry<bool> _hoe;

		private static ConfigEntry<bool> _cultivator;

		private static FieldInfo _localPlayer;

		private static FieldInfo _shared;

		private static FieldInfo _itemName;

		[ThreadStatic]
		private static int _building;

		private static MethodInfo _getRightItem;

		private static MethodInfo _inPlaceMode;

		private void Awake()
		{
			//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fc: Expected O, but got Unknown
			//IL_0143: Unknown result type (might be due to invalid IL or missing references)
			//IL_0151: Expected O, but got Unknown
			//IL_0282: Unknown result type (might be due to invalid IL or missing references)
			//IL_029f: Unknown result type (might be due to invalid IL or missing references)
			//IL_02aa: Expected O, but got Unknown
			//IL_02aa: Expected O, but got Unknown
			//IL_019c: Unknown result type (might be due to invalid IL or missing references)
			//IL_01aa: Expected O, but got Unknown
			_hammer = ((BaseUnityPlugin)this).Config.Bind<bool>("Tools", "Hammer", true, "Build, repair and remove with the hammer without using stamina. / Costruire, riparare e smontare con il martello senza stamina.");
			_hoe = ((BaseUnityPlugin)this).Config.Bind<bool>("Tools", "Hoe", false, "Also the hoe (level ground, paths) without using stamina. / Anche la zappa senza stamina.");
			_cultivator = ((BaseUnityPlugin)this).Config.Bind<bool>("Tools", "Cultivator", false, "Also the cultivator (planting) without using stamina. / Anche il coltivatore senza stamina.");
			Type type = AccessTools.TypeByName("Player");
			if (type == null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"Effortless Hammer: Player type not found, mod disabled");
				return;
			}
			_localPlayer = AccessTools.Field(type, "m_localPlayer");
			_getRightItem = AccessTools.Method(type, "GetRightItem", (Type[])null, (Type[])null);
			_inPlaceMode = AccessTools.Method(type, "InPlaceMode", (Type[])null, (Type[])null);
			if (_localPlayer == null || _getRightItem == null || _inPlaceMode == null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"Effortless Hammer: the game changed (missing m_localPlayer, GetRightItem or InPlaceMode), mod disabled");
				return;
			}
			Harmony val = new Harmony("it.playtodate.effortlesshammer");
			int num = 0;
			MethodInfo[] methods = type.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			foreach (MethodInfo methodInfo in methods)
			{
				try
				{
					if (methodInfo.Name == "UseStamina")
					{
						val.Patch((MethodBase)methodInfo, new HarmonyMethod(typeof(Plugin).GetMethod("UseStaminaPrefix", BindingFlags.Static | BindingFlags.NonPublic)), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
						num++;
					}
					else if (methodInfo.Name == "HaveStamina" && methodInfo.ReturnType == typeof(bool))
					{
						val.Patch((MethodBase)methodInfo, new HarmonyMethod(typeof(Plugin).GetMethod("HaveStaminaPrefix", BindingFlags.Static | BindingFlags.NonPublic)), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
						num++;
					}
				}
				catch (Exception ex)
				{
					((BaseUnityPlugin)this).Logger.LogWarning((object)("Effortless Hammer: could not patch Player." + methodInfo.Name + " (" + ex.Message + ")"));
				}
			}
			int num2 = 0;
			string[] array = new string[2] { "UpdatePlacement", "Repair" };
			foreach (string text in array)
			{
				MethodInfo methodInfo2 = AccessTools.Method(type, text, (Type[])null, (Type[])null);
				if (methodInfo2 == null)
				{
					((BaseUnityPlugin)this).Logger.LogWarning((object)("Effortless Hammer: Player." + text + " not found"));
					continue;
				}
				try
				{
					val.Patch((MethodBase)methodInfo2, new HarmonyMethod(typeof(Plugin).GetMethod("EnterBuild", BindingFlags.Static | BindingFlags.NonPublic)), (HarmonyMethod)null, (HarmonyMethod)null, new HarmonyMethod(typeof(Plugin).GetMethod("LeaveBuild", BindingFlags.Static | BindingFlags.NonPublic)), (HarmonyMethod)null);
					num2++;
				}
				catch (Exception ex2)
				{
					((BaseUnityPlugin)this).Logger.LogWarning((object)("Effortless Hammer: could not patch Player." + text + " (" + ex2.Message + ")"));
				}
			}
			if (num2 == 0)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"Effortless Hammer: build actions not found, stamina is never free (safe default)");
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Effortless Hammer 1.0.1 ready (" + num + " patches, " + num2 + " build actions)"));
		}

		private static bool Free(object player)
		{
			try
			{
				if (_building <= 0)
				{
					return false;
				}
				if (player == null || player != _localPlayer.GetValue(null))
				{
					return false;
				}
				object obj = _inPlaceMode.Invoke(player, null);
				if (!(obj is bool) || !(bool)obj)
				{
					return false;
				}
				object obj2 = _getRightItem.Invoke(player, null);
				if (obj2 == null)
				{
					return false;
				}
				if (_shared == null)
				{
					_shared = AccessTools.Field(obj2.GetType(), "m_shared");
				}
				object obj3 = ((_shared != null) ? _shared.GetValue(obj2) : null);
				if (obj3 == null)
				{
					return false;
				}
				if (_itemName == null)
				{
					_itemName = AccessTools.Field(obj3.GetType(), "m_name");
				}
				return ((_itemName != null) ? (_itemName.GetValue(obj3) as string) : null) switch
				{
					"$item_hammer" => _hammer.Value, 
					"$item_hoe" => _hoe.Value, 
					"$item_cultivator" => _cultivator.Value, 
					_ => false, 
				};
			}
			catch
			{
				return false;
			}
		}

		private static void EnterBuild()
		{
			_building++;
		}

		private static Exception LeaveBuild(Exception __exception)
		{
			if (_building > 0)
			{
				_building--;
			}
			return __exception;
		}

		private static bool UseStaminaPrefix(object __instance)
		{
			return !Free(__instance);
		}

		private static bool HaveStaminaPrefix(object __instance, ref bool __result)
		{
			if (!Free(__instance))
			{
				return true;
			}
			__result = true;
			return false;
		}
	}
}