Decompiled source of KeyRing v1.1.0

KeyRing.dll

Decompiled 6 months ago
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using LethalCompanyInputUtils.Api;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.InputSystem;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("KeyRing")]
[assembly: AssemblyDescription("Tired of the key taking up an entire inventory slot? Then this mod will be perfect for you!")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("ic23")]
[assembly: AssemblyProduct("KeyRing")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("7ce99b51-6182-441e-8c40-e7937f89de2e")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.1.0.0")]
namespace KeyRing;

public class KeyRingButton : LcInputActions
{
	[InputAction("<Keyboard>/x", Name = "KeyRing")]
	public InputAction KeyRing { get; set; }
}
public class KeyRingVar
{
	public ManualLogSource mls;

	public bool MasterKey;

	public int KeyRingCount;
}
[BepInPlugin("ic23.KeyRing", "KeyRing", "1.1.0")]
public class Modbase : BaseUnityPlugin
{
	[HarmonyPatch(typeof(PlayerControllerB))]
	internal class PlayerControllerBPatch
	{
		[HarmonyPatch("Update")]
		[HarmonyPostfix]
		private static void OpenDoorPatch(ref PlayerControllerB __instance)
		{
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0076: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Unknown result type (might be due to invalid IL or missing references)
			RaycastHit val = default(RaycastHit);
			if ((Object)(object)GameNetworkManager.Instance == (Object)null || (Object)(object)GameNetworkManager.Instance.localPlayerController == (Object)null || !(InputActionInstance.KeyRing.ReadValue<float>() > 0f) || KeyRingVar.KeyRingCount <= 0 || !Physics.Raycast(new Ray(((Component)GameNetworkManager.Instance.localPlayerController.gameplayCamera).transform.position, ((Component)GameNetworkManager.Instance.localPlayerController.gameplayCamera).transform.forward), ref val, 3f, 2816))
			{
				return;
			}
			DoorLock component = ((Component)((RaycastHit)(ref val)).transform).GetComponent<DoorLock>();
			if ((Object)(object)component != (Object)null && component.isLocked && !component.isPickingLock)
			{
				if (!KeyRingVar.MasterKey)
				{
					KeyRingVar.KeyRingCount--;
				}
				component.UnlockDoorSyncWithServer();
			}
		}

		[HarmonyPatch("GrabObjectClientRpc")]
		[HarmonyPostfix]
		private static void InventoryCheckPatch(ref PlayerControllerB __instance)
		{
			try
			{
				if (((object)GameNetworkManager.Instance.localPlayerController.ItemSlots[GameNetworkManager.Instance.localPlayerController.currentItemSlot]).GetType() == typeof(KeyItem) && (!KeyRingVar.MasterKey || KeyRingVar.KeyRingCount < 1))
				{
					KeyRingVar.KeyRingCount++;
					GameNetworkManager.Instance.localPlayerController.DespawnHeldObject();
					KeyRingVar.mls.LogInfo((object)"[KeyRing] The Key is taken!");
				}
			}
			catch
			{
			}
		}

		[HarmonyPatch("KillPlayer")]
		[HarmonyPostfix]
		private static void KeyRingLoss(ref PlayerControllerB __instance)
		{
			try
			{
				if (!((Object)(object)GameNetworkManager.Instance.localPlayerController == (Object)null))
				{
					if (GameNetworkManager.Instance.localPlayerController.isPlayerDead)
					{
						KeyRingVar.KeyRingCount = 0;
					}
					KeyRingVar.mls.LogInfo((object)"[KeyRing] All the keys have been lost!");
				}
			}
			catch
			{
			}
		}
	}

	[HarmonyPatch(typeof(DoorLock))]
	internal class DoorLockPatch
	{
		[HarmonyPatch("Update")]
		[HarmonyPostfix]
		public static void KeyRingPatch(ref bool ___isLocked, ref InteractTrigger ___doorTrigger, ref int ___playersPickingDoor, ref float ___playerPickingLockProgress, ref NavMeshObstacle ___navMeshObstacle, ref bool ___isPickingLock, ref float ___lockPickTimeLeft, ref DoorLock __instance)
		{
			if (!___isLocked || ___isPickingLock || (Object)(object)GameNetworkManager.Instance == (Object)null || (Object)(object)GameNetworkManager.Instance.localPlayerController == (Object)null)
			{
				return;
			}
			if (KeyRingVar.KeyRingCount > 0)
			{
				if (StartOfRound.Instance.localPlayerUsingController)
				{
					if (KeyRingVar.MasterKey)
					{
						___doorTrigger.disabledHoverTip = "MasterKey | Use key: [R-trigger]";
					}
					else
					{
						___doorTrigger.disabledHoverTip = "Keys: " + KeyRingVar.KeyRingCount + " | Use key: [R-trigger]";
					}
					return;
				}
				if (KeyRingVar.MasterKey)
				{
					___doorTrigger.disabledHoverTip = "MasterKey | Use key: [ " + InputActionRebindingExtensions.GetBindingDisplayString(InputActionInstance.KeyRing, 0, (DisplayStringOptions)0) + " ]";
					return;
				}
				___doorTrigger.disabledHoverTip = "Keys: " + KeyRingVar.KeyRingCount + " | Use key: [ " + InputActionRebindingExtensions.GetBindingDisplayString(InputActionInstance.KeyRing, 0, (DisplayStringOptions)0) + " ]";
			}
			else
			{
				___doorTrigger.disabledHoverTip = "Keys: 0 | Locked";
			}
		}
	}

	private const string modGUID = "ic23.KeyRing";

	private const string modName = "KeyRing";

	private const string modVersion = "1.1.0";

	internal static KeyRingButton InputActionInstance = new KeyRingButton();

	internal static KeyRingVar KeyRingVar = new KeyRingVar();

	private ConfigEntry<bool> configMasterKey;

	private readonly Harmony harmony = new Harmony("ic23.KeyRing");

	private static Modbase Instance;

	private void Awake()
	{
		if ((Object)(object)Instance == (Object)null)
		{
			Instance = this;
		}
		configMasterKey = ((BaseUnityPlugin)this).Config.Bind<bool>("General.Toggles", "MasterKey", false, "Open any door with one key");
		KeyRingVar.MasterKey = configMasterKey.Value;
		KeyRingVar.mls = Logger.CreateLogSource("ic23.KeyRing");
		KeyRingVar.mls.LogInfo((object)"[KeyRing] Mod is loaded!");
		harmony.PatchAll(typeof(Modbase));
		harmony.PatchAll(typeof(PlayerControllerBPatch));
		harmony.PatchAll(typeof(DoorLockPatch));
	}
}