Decompiled source of ToolLock v1.0.0

plugins/ToolLock.dll

Decompiled a week ago
using System;
using System.Collections.Generic;
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.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("ToolLock")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ToolLock")]
[assembly: AssemblyTitle("ToolLock")]
[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;
		}
	}
}
[BepInPlugin("ToolLock", "ToolLock", "1.0.0")]
public class ToolLock : BaseUnityPlugin
{
	public const string PluginGuid = "ToolLock";

	public const string PluginName = "ToolLock";

	public const string PluginVersion = "1.0.0";

	private const string DetectionSection = "0 - Detection";

	private const string HammerSection = "1 - Hammer";

	private const string HoeSection = "2 - Hoe";

	private const string CultivatorSection = "3 - Cultivator";

	private const string DisableSection = "4 - Disable";

	internal static ConfigEntry<float> DetectionDistance = null;

	internal static ConfigEntry<bool> RestrictWhenNearby = null;

	internal static ConfigEntry<bool> RestrictWhenSeen = null;

	internal static ConfigEntry<bool> RestrictWhenHeard = null;

	internal static ConfigEntry<bool> HammerEnabled = null;

	internal static ConfigEntry<string> HammerNearbyBlockedMessage = null;

	internal static ConfigEntry<string> HammerSeenBlockedMessage = null;

	internal static ConfigEntry<string> HammerHeardBlockedMessage = null;

	internal static ConfigEntry<bool> HoeEnabled = null;

	internal static ConfigEntry<string> HoeNearbyBlockedMessage = null;

	internal static ConfigEntry<string> HoeSeenBlockedMessage = null;

	internal static ConfigEntry<string> HoeHeardBlockedMessage = null;

	internal static ConfigEntry<bool> CultivatorEnabled = null;

	internal static ConfigEntry<string> CultivatorNearbyBlockedMessage = null;

	internal static ConfigEntry<string> CultivatorSeenBlockedMessage = null;

	internal static ConfigEntry<string> CultivatorHeardBlockedMessage = null;

	internal static ConfigEntry<bool> DisableHammer = null;

	internal static ConfigEntry<string> DisableHammerMessage = null;

	internal static ConfigEntry<bool> DisableHoe = null;

	internal static ConfigEntry<string> DisableHoeMessage = null;

	internal static ConfigEntry<bool> DisableCultivator = null;

	internal static ConfigEntry<string> DisableCultivatorMessage = null;

	private static readonly List<Character> NearbyCharacters = new List<Character>();

	private void Awake()
	{
		//IL_02fa: Unknown result type (might be due to invalid IL or missing references)
		DetectionDistance = ((BaseUnityPlugin)this).Config.Bind<float>("0 - Detection", "DetectionDistance", 20f, "Distance in metres at which nearby enemies will prevent tool use. Default: 20 metres.");
		RestrictWhenNearby = ((BaseUnityPlugin)this).Config.Bind<bool>("0 - Detection", "RestrictWhenNearby", true, "If enabled, restricted tools cannot be used while an enemy is nearby.");
		RestrictWhenSeen = ((BaseUnityPlugin)this).Config.Bind<bool>("0 - Detection", "RestrictWhenSeen", true, "If enabled, restricted tools cannot be used while the player is seen by an enemy.");
		RestrictWhenHeard = ((BaseUnityPlugin)this).Config.Bind<bool>("0 - Detection", "RestrictWhenHeard", false, "If enabled, restricted tools cannot be used while the player is heard by an enemy.");
		HammerEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("1 - Hammer", "Enabled", true, "If enabled, the hammer is subject to the detection restrictions.");
		HammerNearbyBlockedMessage = ((BaseUnityPlugin)this).Config.Bind<string>("1 - Hammer", "NearbyBlockedMessage", "Hammer cannot be used while enemy is nearby.", "Message shown when hammer use is blocked because an enemy is nearby.");
		HammerSeenBlockedMessage = ((BaseUnityPlugin)this).Config.Bind<string>("1 - Hammer", "SeenBlockedMessage", "Hammer cannot be used while seen.", "Message shown when hammer use is blocked because the player is seen.");
		HammerHeardBlockedMessage = ((BaseUnityPlugin)this).Config.Bind<string>("1 - Hammer", "HeardBlockedMessage", "Hammer cannot be used while heard.", "Message shown when hammer use is blocked because the player is heard.");
		HoeEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("2 - Hoe", "Enabled", true, "If enabled, the hoe is subject to the detection restrictions.");
		HoeNearbyBlockedMessage = ((BaseUnityPlugin)this).Config.Bind<string>("2 - Hoe", "NearbyBlockedMessage", "Hoe cannot be used while enemy is nearby.", "Message shown when hoe use is blocked because an enemy is nearby.");
		HoeSeenBlockedMessage = ((BaseUnityPlugin)this).Config.Bind<string>("2 - Hoe", "SeenBlockedMessage", "Hoe cannot be used while seen.", "Message shown when hoe use is blocked because the player is seen.");
		HoeHeardBlockedMessage = ((BaseUnityPlugin)this).Config.Bind<string>("2 - Hoe", "HeardBlockedMessage", "Hoe cannot be used while heard.", "Message shown when hoe use is blocked because the player is heard.");
		CultivatorEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("3 - Cultivator", "Enabled", true, "If enabled, the cultivator is subject to the detection restrictions.");
		CultivatorNearbyBlockedMessage = ((BaseUnityPlugin)this).Config.Bind<string>("3 - Cultivator", "NearbyBlockedMessage", "Cultivator cannot be used while enemy is nearby.", "Message shown when cultivator use is blocked because an enemy is nearby.");
		CultivatorSeenBlockedMessage = ((BaseUnityPlugin)this).Config.Bind<string>("3 - Cultivator", "SeenBlockedMessage", "Cultivator cannot be used while seen.", "Message shown when cultivator use is blocked because the player is seen.");
		CultivatorHeardBlockedMessage = ((BaseUnityPlugin)this).Config.Bind<string>("3 - Cultivator", "HeardBlockedMessage", "Cultivator cannot be used while heard.", "Message shown when cultivator use is blocked because the player is heard.");
		DisableHammer = ((BaseUnityPlugin)this).Config.Bind<bool>("4 - Disable", "DisableHammer", false, "If enabled, the hammer cannot be used at all, regardless of detection settings.");
		DisableHammerMessage = ((BaseUnityPlugin)this).Config.Bind<string>("4 - Disable", "DisableHammerMessage", "Hammer is disabled.", "Message shown when the hammer is permanently disabled.");
		DisableHoe = ((BaseUnityPlugin)this).Config.Bind<bool>("4 - Disable", "DisableHoe", false, "If enabled, the hoe cannot be used at all, regardless of detection settings.");
		DisableHoeMessage = ((BaseUnityPlugin)this).Config.Bind<string>("4 - Disable", "DisableHoeMessage", "Hoe is disabled.", "Message shown when the hoe is permanently disabled.");
		DisableCultivator = ((BaseUnityPlugin)this).Config.Bind<bool>("4 - Disable", "DisableCultivator", false, "If enabled, the cultivator cannot be used at all, regardless of detection settings.");
		DisableCultivatorMessage = ((BaseUnityPlugin)this).Config.Bind<string>("4 - Disable", "DisableCultivatorMessage", "Cultivator is disabled.", "Message shown when the cultivator is permanently disabled.");
		new Harmony("ToolLock").PatchAll();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"ToolLock loaded (1.0.0)");
	}

	internal static bool IsToolRestricted(string itemName)
	{
		return itemName == "$item_hammer" || itemName == "$item_hoe" || itemName == "$item_cultivator";
	}

	internal static bool IsRestrictionEnabled(string itemName)
	{
		if (1 == 0)
		{
		}
		bool result = itemName switch
		{
			"$item_hammer" => HammerEnabled.Value, 
			"$item_hoe" => HoeEnabled.Value, 
			"$item_cultivator" => CultivatorEnabled.Value, 
			_ => false, 
		};
		if (1 == 0)
		{
		}
		return result;
	}

	internal static bool IsToolDisabled(string itemName)
	{
		if (1 == 0)
		{
		}
		bool result = itemName switch
		{
			"$item_hammer" => DisableHammer.Value, 
			"$item_hoe" => DisableHoe.Value, 
			"$item_cultivator" => DisableCultivator.Value, 
			_ => false, 
		};
		if (1 == 0)
		{
		}
		return result;
	}

	internal static string GetDisabledMessage(string itemName)
	{
		if (1 == 0)
		{
		}
		string result = itemName switch
		{
			"$item_hammer" => string.IsNullOrWhiteSpace(DisableHammerMessage.Value) ? "Hammer is disabled." : DisableHammerMessage.Value, 
			"$item_hoe" => string.IsNullOrWhiteSpace(DisableHoeMessage.Value) ? "Hoe is disabled." : DisableHoeMessage.Value, 
			"$item_cultivator" => string.IsNullOrWhiteSpace(DisableCultivatorMessage.Value) ? "Cultivator is disabled." : DisableCultivatorMessage.Value, 
			_ => "This tool is disabled.", 
		};
		if (1 == 0)
		{
		}
		return result;
	}

	internal static string GetNearbyBlockedMessage(string itemName)
	{
		if (1 == 0)
		{
		}
		string result = itemName switch
		{
			"$item_hammer" => string.IsNullOrWhiteSpace(HammerNearbyBlockedMessage.Value) ? "Hammer cannot be used while enemy is nearby." : HammerNearbyBlockedMessage.Value, 
			"$item_hoe" => string.IsNullOrWhiteSpace(HoeNearbyBlockedMessage.Value) ? "Hoe cannot be used while enemy is nearby." : HoeNearbyBlockedMessage.Value, 
			"$item_cultivator" => string.IsNullOrWhiteSpace(CultivatorNearbyBlockedMessage.Value) ? "Cultivator cannot be used while enemy is nearby." : CultivatorNearbyBlockedMessage.Value, 
			_ => "This tool cannot be used while enemy is nearby.", 
		};
		if (1 == 0)
		{
		}
		return result;
	}

	internal static string GetSeenBlockedMessage(string itemName)
	{
		if (1 == 0)
		{
		}
		string result = itemName switch
		{
			"$item_hammer" => string.IsNullOrWhiteSpace(HammerSeenBlockedMessage.Value) ? "Hammer cannot be used while seen." : HammerSeenBlockedMessage.Value, 
			"$item_hoe" => string.IsNullOrWhiteSpace(HoeSeenBlockedMessage.Value) ? "Hoe cannot be used while seen." : HoeSeenBlockedMessage.Value, 
			"$item_cultivator" => string.IsNullOrWhiteSpace(CultivatorSeenBlockedMessage.Value) ? "Cultivator cannot be used while seen." : CultivatorSeenBlockedMessage.Value, 
			_ => "This tool cannot be used while seen.", 
		};
		if (1 == 0)
		{
		}
		return result;
	}

	internal static string GetHeardBlockedMessage(string itemName)
	{
		if (1 == 0)
		{
		}
		string result = itemName switch
		{
			"$item_hammer" => string.IsNullOrWhiteSpace(HammerHeardBlockedMessage.Value) ? "Hammer cannot be used while heard." : HammerHeardBlockedMessage.Value, 
			"$item_hoe" => string.IsNullOrWhiteSpace(HoeHeardBlockedMessage.Value) ? "Hoe cannot be used while heard." : HoeHeardBlockedMessage.Value, 
			"$item_cultivator" => string.IsNullOrWhiteSpace(CultivatorHeardBlockedMessage.Value) ? "Cultivator cannot be used while heard." : CultivatorHeardBlockedMessage.Value, 
			_ => "This tool cannot be used while heard.", 
		};
		if (1 == 0)
		{
		}
		return result;
	}

	internal static bool IsEnemyNearby(Player player)
	{
		//IL_0043: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)player == (Object)null)
		{
			return false;
		}
		if (DetectionDistance.Value <= 0f)
		{
			return false;
		}
		NearbyCharacters.Clear();
		Character.GetCharactersInRange(((Component)player).transform.position, DetectionDistance.Value, NearbyCharacters);
		foreach (Character nearbyCharacter in NearbyCharacters)
		{
			if ((Object)(object)nearbyCharacter == (Object)null || (Object)(object)nearbyCharacter == (Object)(object)player || nearbyCharacter.IsPlayer() || nearbyCharacter.IsDead() || nearbyCharacter.IsTamed() || !BaseAI.IsEnemy((Character)(object)player, nearbyCharacter))
			{
				continue;
			}
			return true;
		}
		return false;
	}
}
[HarmonyPatch(typeof(Player), "UpdatePlacement")]
public static class Patch_Player_UpdatePlacement_ToolLock
{
	private static readonly FieldRef<Humanoid, ItemData> RightItemRef = AccessTools.FieldRefAccess<Humanoid, ItemData>("m_rightItem");

	private static float _nextMsgTime;

	private static bool Prefix(Player __instance)
	{
		if (!((Character)__instance).InPlaceMode())
		{
			return true;
		}
		ItemData val = RightItemRef.Invoke((Humanoid)(object)__instance);
		if (val?.m_shared == null)
		{
			return true;
		}
		string name = val.m_shared.m_name;
		if (!ToolLock.IsToolRestricted(name))
		{
			return true;
		}
		if (ToolLock.IsToolDisabled(name))
		{
			ShowMessage(__instance, ToolLock.GetDisabledMessage(name));
			return false;
		}
		if (!ToolLock.IsRestrictionEnabled(name))
		{
			return true;
		}
		bool flag = ToolLock.RestrictWhenNearby.Value && ToolLock.IsEnemyNearby(__instance);
		bool flag2 = ToolLock.RestrictWhenSeen.Value && __instance.IsSensed();
		bool flag3 = ToolLock.RestrictWhenHeard.Value && __instance.IsTargeted();
		if (flag)
		{
			ShowMessage(__instance, ToolLock.GetNearbyBlockedMessage(name));
			return false;
		}
		if (flag2)
		{
			ShowMessage(__instance, ToolLock.GetSeenBlockedMessage(name));
			return false;
		}
		if (flag3)
		{
			ShowMessage(__instance, ToolLock.GetHeardBlockedMessage(name));
			return false;
		}
		return true;
	}

	private static void ShowMessage(Player player, string message)
	{
		if (!(Time.time < _nextMsgTime))
		{
			_nextMsgTime = Time.time + 1f;
			((Character)player).Message((MessageType)2, message, 0, (Sprite)null, false);
		}
	}
}