Decompiled source of UnlimitedHammer v1.0.3

BepInEx/plugins/UnlimitedHammer/UnlimitedHammer.dll

Decompiled 2 weeks 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 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("UnlimitedHammer")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.3.0")]
[assembly: AssemblyInformationalVersion("1.0.3")]
[assembly: AssemblyProduct("UnlimitedHammer")]
[assembly: AssemblyTitle("UnlimitedHammer")]
[assembly: AssemblyVersion("1.0.3.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 UnlimitedHammer
{
	[BepInPlugin("DogEggz.unlimitedhammer", "Unlimited hammer", "1.0.3")]
	public class Plugin : BaseUnityPlugin
	{
		private Harmony harmony;

		internal static ConfigEntry<bool> ModEnabled { get; private set; }

		internal static bool IsModEnabled
		{
			get
			{
				if (ModEnabled != null)
				{
					return ModEnabled.Value;
				}
				return true;
			}
		}

		private void Awake()
		{
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Expected O, but got Unknown
			ModEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("Settings", "Mod enabled", true, "Toggle all Unlimited hammer changes on or off.");
			harmony = new Harmony("DogEggz.unlimitedhammer");
			harmony.PatchAll(Assembly.GetExecutingAssembly());
		}

		private void OnDestroy()
		{
			Harmony obj = harmony;
			if (obj != null)
			{
				obj.UnpatchSelf();
			}
		}
	}
	internal static class ExtraNailableItems
	{
		private static readonly HashSet<string> Names = new HashSet<string>(StringComparer.Ordinal)
		{
			"137 model ship junk (big)", "138 model ship junk (small)", "110 lantern A", "111 lantern E yellow", "112 lantern E red", "113 lantern E green", "114 lantern M", "133 lantern M big", "134 lantern E blu", "190 flower pot",
			"191 flower pot 1", "192 flower pot 2", "193 flower pot 3", "194 flower pot 4 (small)", "195 flower pot 5 (small)"
		};

		public static bool Contains(ShipItem item)
		{
			if ((Object)(object)item == (Object)null)
			{
				return false;
			}
			return Names.Contains(NormalizeName(((Object)((Component)item).transform).name));
		}

		private static string NormalizeName(string itemName)
		{
			if (string.IsNullOrEmpty(itemName))
			{
				return string.Empty;
			}
			itemName = itemName.Trim();
			while (itemName.EndsWith("(Clone)", StringComparison.Ordinal))
			{
				itemName = itemName.Substring(0, itemName.Length - "(Clone)".Length).Trim();
			}
			return itemName;
		}
	}
	[HarmonyPatch(typeof(ShipItemHammer), "CanNail")]
	internal static class ShipItemHammerCanNailPatch
	{
		private static void Postfix(ShipItem item, ref bool __result)
		{
			if (Plugin.IsModEnabled && !__result && !((Object)(object)item == (Object)null) && item.sold)
			{
				__result = ExtraNailableItems.Contains(item);
			}
		}
	}
	[HarmonyPatch(typeof(ShipItemHammer), "NailItem")]
	internal static class ShipItemHammerNailItemPatch
	{
		private static bool Prefix(ShipItem item)
		{
			if (!Plugin.IsModEnabled)
			{
				return true;
			}
			if ((Object)(object)item == (Object)null)
			{
				return false;
			}
			item.nailed = true;
			UISoundPlayer.instance.PlayUISound((UISounds)11, 1f, 0.6f);
			Debug.Log((object)("Nailed " + item.name));
			return false;
		}
	}
	[HarmonyPatch(typeof(LookUI), "ShowLookText")]
	internal static class LookUIHammerPercentPatch
	{
		private const float NailDurationSeconds = 2f;

		private static readonly FieldInfo CurrentlyNailedItemField = AccessTools.Field(typeof(ShipItemHammer), "currentlyNailedItem");

		private static readonly FieldInfo NailTimerField = AccessTools.Field(typeof(ShipItemHammer), "nailTimer");

		private static void Postfix(GoPointerButton button, GoPointer ___pointer, TextMesh ___hintText)
		{
			if (!Plugin.IsModEnabled || (Object)(object)button == (Object)null || (Object)(object)___pointer == (Object)null || (Object)(object)___hintText == (Object)null)
			{
				return;
			}
			ShipItem component = ((Component)button).GetComponent<ShipItem>();
			if ((Object)(object)component == (Object)null || component.nailed)
			{
				return;
			}
			PickupableItem heldItem = ___pointer.GetHeldItem();
			ShipItemHammer val = (((Object)(object)heldItem == (Object)null) ? null : ((Component)heldItem).GetComponent<ShipItemHammer>());
			if (!((Object)(object)val == (Object)null) && ((ShipItem)val).sold && !((Object)/*isinst with value type is only supported in some contexts*/ != (Object)(object)component))
			{
				float num = ((NailTimerField?.GetValue(val) is float num2) ? num2 : 0f);
				if (!(num <= 0f))
				{
					int num3 = Mathf.Clamp(Mathf.FloorToInt(num / 2f * 100f), 0, 100);
					string text = $"<color=white>{num3}%</color>";
					___hintText.text = (string.IsNullOrEmpty(___hintText.text) ? text : (___hintText.text + "\n" + text));
				}
			}
		}
	}
	[HarmonyPatch(typeof(GoPointer), "DoRaycast")]
	internal static class GoPointerNailedLanternPatch
	{
		private static readonly MethodInfo TypeEquality = AccessTools.Method(typeof(Type), "op_Equality", new Type[2]
		{
			typeof(Type),
			typeof(Type)
		}, (Type[])null);

		private static readonly MethodInfo IsBedOrLanternMethod = AccessTools.Method(typeof(GoPointerNailedLanternPatch), "IsBedOrLantern", (Type[])null, (Type[])null);

		private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			List<CodeInstruction> list = new List<CodeInstruction>(instructions);
			bool flag = false;
			for (int i = 0; i < list.Count; i++)
			{
				if (list[i].opcode == OpCodes.Ldtoken && list[i].operand as Type == typeof(ShipItemBed))
				{
					flag = true;
				}
				else if (flag && CodeInstructionExtensions.Calls(list[i], TypeEquality))
				{
					list[i].operand = IsBedOrLanternMethod;
					return list;
				}
			}
			Debug.LogWarning((object)"Unlimited hammer: nailed lantern patch target not found; locked lanterns stay non-interactable.");
			return list;
		}

		private static bool IsBedOrLantern(Type itemType, Type bedType)
		{
			if (itemType == bedType)
			{
				return true;
			}
			if (Plugin.IsModEnabled)
			{
				return typeof(ShipItemLight).IsAssignableFrom(itemType);
			}
			return false;
		}
	}
	[HarmonyPatch(typeof(LookUI), "ShowLookText")]
	internal static class LookUINailedLanternPatch
	{
		private static void Postfix(GoPointerButton button, GoPointer ___pointer, TextMesh ___controlsText, Renderer ___mouseLIcon, TextMesh ___textLicon)
		{
			if (!Plugin.IsModEnabled || !Settings.controlsTextEnabled || (Object)(object)button == (Object)null || (Object)(object)___pointer == (Object)null || (Object)(object)___pointer.GetHeldItem() != (Object)null)
			{
				return;
			}
			ShipItemLight component = ((Component)button).GetComponent<ShipItemLight>();
			if (!((Object)(object)component == (Object)null) && ((ShipItem)component).sold && ((ShipItem)component).nailed)
			{
				if ((Object)(object)___controlsText != (Object)null)
				{
					___controlsText.text = "\nuse";
				}
				if ((Object)(object)___mouseLIcon != (Object)null)
				{
					___mouseLIcon.enabled = false;
				}
				if ((Object)(object)___textLicon != (Object)null)
				{
					___textLicon.text = "";
				}
			}
		}
	}
}