Decompiled source of Instantly Destroy Boats And Carts v1.0.4

InstantlyDestroyBoatsAndCarts.dll

Decompiled 7 months ago
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("InstantlyDestroyBoatsAndCarts")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("InstantlyDestroyBoatsAndCarts")]
[assembly: AssemblyCopyright("Copyright ©  2022")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("c74d03ce-f4f3-440e-9ed1-c96c0f0f0d75")]
[assembly: AssemblyFileVersion("1.0.4.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.4.0")]
[module: UnverifiableCode]
namespace InstantlyDestroyBoatsAndCarts;

internal class BoatWagonPatches
{
	[HarmonyPatch(typeof(Piece), "Awake")]
	public static class Awake_Patch
	{
		private static void Postfix(Piece __instance)
		{
			Ship componentInChildren = ((Component)__instance).GetComponentInChildren<Ship>();
			Vagon componentInChildren2 = ((Component)__instance).GetComponentInChildren<Vagon>();
			if (Object.op_Implicit((Object)(object)componentInChildren2))
			{
				__instance.m_canBeRemoved = true;
			}
			if (Object.op_Implicit((Object)(object)componentInChildren) && !IsValheimRaft(componentInChildren))
			{
				__instance.m_canBeRemoved = true;
			}
		}
	}

	[HarmonyPatch(typeof(Piece), "CanBeRemoved")]
	public static class CanBeRemoved_Patch
	{
		private static bool Prefix(Piece __instance, ref bool __result)
		{
			Ship componentInChildren = ((Component)__instance).GetComponentInChildren<Ship>();
			Vagon componentInChildren2 = ((Component)__instance).GetComponentInChildren<Vagon>();
			if (!Object.op_Implicit((Object)(object)componentInChildren) && !Object.op_Implicit((Object)(object)componentInChildren2))
			{
				return true;
			}
			if (Object.op_Implicit((Object)(object)componentInChildren) && IsValheimRaft(componentInChildren))
			{
				return true;
			}
			__result = false;
			InstantlyDestroyBoatsAndCartsPlugin.AllowDestroy value = InstantlyDestroyBoatsAndCartsPlugin.AllowDestroyFor.Value;
			if (Object.op_Implicit((Object)(object)componentInChildren) && (value == InstantlyDestroyBoatsAndCartsPlugin.AllowDestroy.OnlyBoats || value == InstantlyDestroyBoatsAndCartsPlugin.AllowDestroy.Both))
			{
				Container componentInChildren3 = ((Component)__instance).GetComponentInChildren<Container>();
				if (!CanRemoveContainer(componentInChildren3))
				{
					return false;
				}
				__result = componentInChildren.m_players.Count == 0;
				return false;
			}
			if (Object.op_Implicit((Object)(object)componentInChildren2) && (value == InstantlyDestroyBoatsAndCartsPlugin.AllowDestroy.OnlyCarts || value == InstantlyDestroyBoatsAndCartsPlugin.AllowDestroy.Both))
			{
				if (!CanRemoveContainer(componentInChildren2.m_container))
				{
					return false;
				}
				__result = !componentInChildren2.InUse();
				return false;
			}
			return false;
		}
	}

	private static bool CanRemoveContainer(Container container)
	{
		//IL_0014: Unknown result type (might be due to invalid IL or missing references)
		//IL_001a: Invalid comparison between Unknown and I4
		if (!Object.op_Implicit((Object)(object)container))
		{
			return true;
		}
		if (((int)container.m_privacy == 0 || InstantlyDestroyBoatsAndCartsPlugin.PreventWhenContainerIsNotEmpty.Value) && container.GetInventory().NrOfItems() > 0)
		{
			return false;
		}
		return true;
	}

	private static bool IsValheimRaft(Ship ship)
	{
		if (((Object)ship).name.StartsWith("MBRaft"))
		{
			return true;
		}
		Component[] components = ((Component)ship).GetComponents<Component>();
		foreach (Component val in components)
		{
			if (((object)val).GetType().Name == "MoveableBaseShipComponent")
			{
				return true;
			}
		}
		return false;
	}
}
[BepInPlugin("goldenrevolver.InstantlyDestroyBoatsAndCarts", "Instantly Destroy Boats and Carts", "1.0.4")]
public class InstantlyDestroyBoatsAndCartsPlugin : BaseUnityPlugin
{
	public enum AllowDestroy
	{
		Disabled,
		OnlyBoats,
		OnlyCarts,
		Both
	}

	public const string NAME = "Instantly Destroy Boats and Carts";

	public const string VERSION = "1.0.4";

	public static ConfigEntry<AllowDestroy> AllowDestroyFor;

	public static ConfigEntry<bool> PreventWhenContainerIsNotEmpty;

	protected void Awake()
	{
		LoadConfig();
		Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), (string)null);
	}

	private void LoadConfig()
	{
		string text = "General";
		AllowDestroyFor = ((BaseUnityPlugin)this).Config.Bind<AllowDestroy>(text, "AllowDestroyFor", AllowDestroy.Both, "Which of these you can now immediately destroy with a hammer, if PreventWhenContainerIsNotEmpty and the internal checks allow it.");
		PreventWhenContainerIsNotEmpty = ((BaseUnityPlugin)this).Config.Bind<bool>(text, "PreventWhenContainerIsNotEmpty", true, "Whether the container of the boat or wagon needs to be empty to allow for it to get immediately destroyed with a hammer.");
	}
}