Decompiled source of WiresTinyToolTweaks v1.0.2

WiresTinyToolTweaks.dll

Decompiled a week 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;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("WiresTinyToolTweaks")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("WiresTinyToolTweaks")]
[assembly: AssemblyTitle("WiresTinyToolTweaks")]
[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 WiresTinyToolTweaks
{
	[BepInPlugin("wire.valheim.tinytooltweaks", "Wire's Tiny Tool Tweaks", "1.0.2")]
	[BepInProcess("valheim.exe")]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string Guid = "wire.valheim.tinytooltweaks";

		public const string Name = "Wire's Tiny Tool Tweaks";

		public const string Version = "1.0.2";

		internal static ConfigEntry<bool> RemoveEmptyVehicles;

		internal static ConfigEntry<bool> InteractWithBuildTool;

		private Harmony _harmony;

		private void Awake()
		{
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Expected O, but got Unknown
			RemoveEmptyVehicles = ((BaseUnityPlugin)this).Config.Bind<bool>("Features", "Dismantle empty carts and ships", true, "Allow hammer removal of empty carts and ships, subject to vanilla ownership, access, station and in-use checks.");
			InteractWithBuildTool = ((BaseUnityPlugin)this).Config.Bind<bool>("Features", "Interact while holding build tool", true, "Allow normal interaction and held-tool interaction animation while the hammer or another build tool is equipped.");
			_harmony = new Harmony("wire.valheim.tinytooltweaks");
			_harmony.PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Wire's Tiny Tool Tweaks 1.0.2 loaded.");
		}

		private void OnDestroy()
		{
			if (_harmony != null)
			{
				_harmony.UnpatchSelf();
			}
		}
	}
	[HarmonyPatch(typeof(Player), "UpdateHover")]
	internal static class InteractWhileToolEquippedPatch
	{
		private delegate void FindHoverObjectDelegate(Player player, out GameObject hovering, out Character hoveringCreature);

		private static readonly FindHoverObjectDelegate FindHoverObject = AccessTools.MethodDelegate<FindHoverObjectDelegate>(AccessTools.Method(typeof(Player), "FindHoverObject", (Type[])null, (Type[])null), (object)null, true);

		private static readonly FieldRef<Player, GameObject> Hovering = AccessTools.FieldRefAccess<Player, GameObject>("m_hovering");

		private static readonly FieldRef<Player, Character> HoveringCreature = AccessTools.FieldRefAccess<Player, Character>("m_hoveringCreature");

		private static readonly FieldRef<Player, IDoodadController> DoodadController = AccessTools.FieldRefAccess<Player, IDoodadController>("m_doodadController");

		[HarmonyPostfix]
		private static void Postfix(Player __instance)
		{
			if (Plugin.InteractWithBuildTool != null && Plugin.InteractWithBuildTool.Value && !((Object)(object)__instance == (Object)null) && ((Character)__instance).InPlaceMode() && !((Character)__instance).IsDead() && DoodadController.Invoke(__instance) == null)
			{
				ItemData rightItem = ((Humanoid)__instance).RightItem;
				if (rightItem != null && rightItem.m_shared != null && !((Object)(object)rightItem.m_shared.m_buildPieces == (Object)null))
				{
					FindHoverObject(__instance, out var hovering, out var hoveringCreature);
					Hovering.Invoke(__instance) = hovering;
					HoveringCreature.Invoke(__instance) = hoveringCreature;
				}
			}
		}
	}
	[HarmonyPatch(typeof(Humanoid), "DoInteractAnimation")]
	internal static class ToolInteractAnimationPatch
	{
		private static readonly FieldRef<Character, ZSyncAnimation> Zanim = AccessTools.FieldRefAccess<Character, ZSyncAnimation>("m_zanim");

		[HarmonyPostfix]
		private static void Postfix(Humanoid __instance)
		{
			Player val = (Player)(object)((__instance is Player) ? __instance : null);
			if (Plugin.InteractWithBuildTool == null || !Plugin.InteractWithBuildTool.Value || (Object)(object)val == (Object)null || !((Character)val).InPlaceMode())
			{
				return;
			}
			ItemData rightItem = ((Humanoid)val).RightItem;
			if (rightItem != null && rightItem.m_shared != null && !((Object)(object)rightItem.m_shared.m_buildPieces == (Object)null) && rightItem.m_shared.m_attack != null)
			{
				ZSyncAnimation val2 = Zanim.Invoke((Character)(object)val);
				if (!((Object)(object)val2 == (Object)null) && !string.IsNullOrEmpty(rightItem.m_shared.m_attack.m_attackAnimation))
				{
					val2.SetTrigger(rightItem.m_shared.m_attack.m_attackAnimation);
				}
			}
		}
	}
	[HarmonyPatch(typeof(Player), "RemovePiece")]
	internal static class RemoveEmptyVehicleWithHammerPatch
	{
		private static readonly FieldRef<Character, ZSyncAnimation> Zanim = AccessTools.FieldRefAccess<Character, ZSyncAnimation>("m_zanim");

		private static readonly int VehicleRemoveMask = LayerMask.GetMask(new string[7] { "Default", "static_solid", "Default_small", "piece", "piece_nonsolid", "terrain", "vehicle" });

		[HarmonyPrefix]
		private static bool Prefix(Player __instance, ref bool __result)
		{
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			//IL_009f: 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_013f: Unknown result type (might be due to invalid IL or missing references)
			//IL_018d: Unknown result type (might be due to invalid IL or missing references)
			if (Plugin.RemoveEmptyVehicles == null || !Plugin.RemoveEmptyVehicles.Value || (Object)(object)__instance == (Object)null || (Object)(object)GameCamera.instance == (Object)null)
			{
				return true;
			}
			ItemData rightItem = ((Humanoid)__instance).RightItem;
			if (rightItem == null || rightItem.m_shared == null || (Object)(object)rightItem.m_shared.m_buildPieces == (Object)null || !rightItem.m_shared.m_buildPieces.m_canRemovePieces)
			{
				return true;
			}
			RaycastHit val = default(RaycastHit);
			if (!Physics.Raycast(((Component)GameCamera.instance).transform.position, ((Component)GameCamera.instance).transform.forward, ref val, 50f, VehicleRemoveMask))
			{
				return true;
			}
			if (Vector3.Distance(((RaycastHit)(ref val)).point, ((Character)__instance).GetEyePoint()) >= __instance.m_maxPlaceDistance)
			{
				return true;
			}
			Ship componentInParent = ((Component)((RaycastHit)(ref val)).collider).GetComponentInParent<Ship>();
			Vagon componentInParent2 = ((Component)((RaycastHit)(ref val)).collider).GetComponentInParent<Vagon>();
			if ((Object)(object)componentInParent == (Object)null && (Object)(object)componentInParent2 == (Object)null)
			{
				return true;
			}
			Component val2 = (Component)(((Object)(object)componentInParent != (Object)null) ? ((object)componentInParent) : ((object)componentInParent2));
			Piece val3 = val2.GetComponentInParent<Piece>() ?? val2.GetComponentInChildren<Piece>(true);
			if ((Object)(object)val3 == (Object)null)
			{
				return true;
			}
			if (Location.IsInsideNoBuildLocation(((Component)val3).transform.position))
			{
				((Character)__instance).Message((MessageType)2, "$msg_nobuildzone", 0, (Sprite)null, false);
				__result = false;
				return false;
			}
			if (!PrivateArea.CheckAccess(((Component)val3).transform.position, 0f, true, false))
			{
				((Character)__instance).Message((MessageType)2, "$msg_privatezone", 0, (Sprite)null, false);
				__result = false;
				return false;
			}
			CraftingStation craftingStation = val3.m_craftingStation;
			if ((Object)(object)CraftingStation.HaveBuildStationInRange(((Object)(object)craftingStation != (Object)null) ? craftingStation.m_name : "$piece_workbench", ((Component)__instance).transform.position) == (Object)null)
			{
				((Character)__instance).Message((MessageType)2, "$msg_missingstation", 0, (Sprite)null, false);
				__result = false;
				return false;
			}
			Container val4 = (((Object)(object)componentInParent2 != (Object)null) ? componentInParent2.m_container : ((Component)val3).GetComponentInChildren<Container>(true));
			if ((Object)(object)val4 != (Object)null && val4.GetInventory() != null && val4.GetInventory().NrOfItems() > 0)
			{
				((Character)__instance).Message((MessageType)2, "$msg_cantremovenow", 0, (Sprite)null, false);
				__result = false;
				return false;
			}
			if ((Object)(object)componentInParent != (Object)null && !componentInParent.CanBeRemoved())
			{
				((Character)__instance).Message((MessageType)2, "$msg_cantremovenow", 0, (Sprite)null, false);
				__result = false;
				return false;
			}
			if ((Object)(object)componentInParent2 != (Object)null && componentInParent2.InUse())
			{
				((Character)__instance).Message((MessageType)2, "$msg_cantremovenow", 0, (Sprite)null, false);
				__result = false;
				return false;
			}
			IRemoved component = ((Component)val3).GetComponent<IRemoved>();
			if (component != null)
			{
				component.OnRemoved();
			}
			WearNTear component2 = ((Component)val3).GetComponent<WearNTear>();
			if ((Object)(object)component2 != (Object)null)
			{
				component2.Remove(false);
			}
			else
			{
				ZNetView component3 = ((Component)val3).GetComponent<ZNetView>();
				if ((Object)(object)component3 == (Object)null)
				{
					__result = false;
					return false;
				}
				component3.ClaimOwnership();
				val3.DropResources((HitData)null);
				ZNetScene.instance.Destroy(((Component)val3).gameObject);
			}
			__instance.FaceLookDirection();
			ZSyncAnimation val5 = Zanim.Invoke((Character)(object)__instance);
			if ((Object)(object)val5 != (Object)null && !string.IsNullOrEmpty(rightItem.m_shared.m_attack.m_attackAnimation))
			{
				val5.SetTrigger(rightItem.m_shared.m_attack.m_attackAnimation);
			}
			__result = true;
			return false;
		}
	}
}