Decompiled source of Realistic TZP v1.0.2

RealisticTZP.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Threading.Tasks;
using BepInEx;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using Newtonsoft.Json;
using RealisticTZP.Patches;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.LowLevel;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("RealisticTZP")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("RealisticTZP")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("16992a46-6566-421a-983c-e16900d42eb3")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace RealisticTZP
{
	[BepInPlugin("D1.RealisticTZP", "Realistic TZP", "1.0.0")]
	public class RealisticTZPBase : BaseUnityPlugin
	{
		public const string modGUID = "D1.RealisticTZP";

		private const string modName = "Realistic TZP";

		private const string modVersion = "1.0.0";

		private readonly Harmony harmony = new Harmony("D1.RealisticTZP");

		internal ManualLogSource Log;

		public static RealisticTZPBase Instance { get; private set; }

		private void Awake()
		{
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
			}
			Log = Logger.CreateLogSource("D1.RealisticTZP");
			Log.LogInfo((object)"Realistic TZP has loaded!");
			harmony.PatchAll(typeof(RealisticTZPBase));
			harmony.PatchAll(typeof(PlayerControllerB_Patch));
		}
	}
}
namespace RealisticTZP.Patches
{
	[HarmonyPatch(typeof(PlayerControllerB))]
	internal class PlayerControllerB_Patch
	{
		[Serializable]
		public class KeybindingData
		{
			public List<Binding> bindings;
		}

		public class Binding
		{
			public string action;

			public string id;

			public string path;
		}

		private static Vector2 mouseVelocity = Vector2.zero;

		public static float inertia = 0.9f;

		public static float drag = 0.05f;

		private static float drunknessCooldown = 1f;

		private static float DrunknessTime = 1f;

		private static bool isKilling = false;

		public static float QuickActionTime = Random.Range(4f, 7f);

		public static float LongActionTime = Random.Range(6f, 8f);

		public static float QuickActionChance = 0.7f;

		public static float LongActionChance = 0.7f;

		[HarmonyPatch("Update")]
		[HarmonyPostfix]
		private static void MovementPatch(PlayerControllerB __instance)
		{
			//IL_070c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0711: Unknown result type (might be due to invalid IL or missing references)
			Dictionary<string, (string id, string key, string action, string keybindkey, float chance)> keybindLookup = new Dictionary<string, (string, string, string, string, float)>
			{
				["Move_Forward"] = ("ae3df94a-dcc6-4177-b026-0938f8413a45", "<Keyboard>/w", "Movement/Move", "", 0.09580839f),
				["Move_Back"] = ("bc252037-120e-4c64-9671-40d365f856b3", "<Keyboard>/s", "Movement/Move", "", 0.059880238f),
				["Move_Left"] = ("ad96b1ce-c0f3-4913-be03-acf077c11064", "<Keyboard>/a", "Movement/Move", "", 0.07185629f),
				["Move_Right"] = ("756f3db2-a6e6-42d7-9580-70d42154cd11", "<Keyboard>/d", "Movement/Move", "", 0.07185629f),
				["Push_To_Talk"] = ("654df1e4-2b33-44e0-afdb-b57dcce462a5", "<Keyboard>/t", "Movement/VoiceButton", "", 0f),
				["Jump"] = ("29d0e539-abdc-46ae-8dff-27436261f379", "<Keyboard>/space", "Movement/Jump", "", 0.047904193f),
				["Sprint"] = ("c243b846-2159-41a2-87d5-5a36f89e70da", "<Keyboard>/shift", "Movement/Sprint", "", 0.047904193f),
				["Crouch"] = ("3bfe566d-e645-4254-8fd2-2c98358dbd17", "<Keyboard>/ctrl", "Movement/Crouch", "", 0.053892214f),
				["Interact"] = ("155d449f-a2c5-43ab-9244-955fec5e0497", "<Keyboard>/e", "Movement/Interact", "", 0.07185629f),
				["Ping_Scan"] = ("f41287a6-fe38-4620-a1ec-b4d871a72d17", "<Mouse>/rightButton", "Movement/PingScan", "", 0.023952097f),
				["Item_Primary_Use"] = ("19548cb7-6c57-4972-a4e3-28a4e7e71595", "<Mouse>/leftButton", "Movement/ActivateItem", "", 0.08982036f),
				["Item_Secondary_Use"] = ("788e33f1-366b-4eb0-b1ed-ebc6dcaaaa21", "<Keyboard>/q", "Movement/ItemSecondaryUse", "", 0.07185629f),
				["Item_Tertiary_Use"] = ("d0787104-91af-4c01-ae46-b9150232edb6", "<Keyboard>/e", "Movement/ItemTertiaryUse", "", 0.047904193f),
				["Item_Drop"] = ("c3c0f7fa-24e9-49bc-9e71-0405fdd4cf02", "<Keyboard>/g", "Movement/Discard", "", 0.047904193f),
				["Item_Switch_Left"] = ("fb0b095c-497a-4c00-b02f-742282882d16", "<Mouse>/scroll/up", "Movement/SwitchItem", "", 0.029940119f),
				["Item_Switch_Right"] = ("2d8f3396-cbb0-401e-8751-38cc079ff90e", "<Mouse>/scroll/down", "Movement/SwitchItem", "", 0.029940119f),
				["Item_Inspect"] = ("e2f85d45-4161-43dd-9792-9655d4ffb7fe", "<Keyboard>/z", "Movement/InspectItem", "", 0.047904193f),
				["Replace_Furniture"] = ("b64c453d-9f9e-462f-9227-51307bcab6ed", "<Keyboard>/b", " Movement/BuildMode", "", 0f),
				["Turn_Furniture"] = ("91068d88-dacd-4f4e-8d78-b360b12c8f99", "<Keyboard>/r", "Movement/ReloadBatteries", "", 0f),
				["Store_Furniture"] = ("e2f369bc-1ae3-4dcb-837e-e6a2e7c37e5e", "<Keyboard>/x", "Movement/Delete", "", 0f),
				["Dance"] = ("8c5fe1cb-4346-4fe7-a829-2f6af2459ed2", "<Keyboard>/1", "Movement/Emote1", "", 0.029940119f),
				["Point"] = ("84dee523-67bb-4220-bf80-3d770065c31b", "<Keyboard>/2", "Movement/Emote2", "", 0.029940119f),
				["Enable_Chat"] = ("e8ff5cf5-ee6d-4e54-a800-27c16bf307fc", "<Keyboard>/divide", "Movement/EnableChat", "", 0f),
				["Submit_Chat"] = ("e6fe9b9c-ff67-4d42-83b9-979bfb1623df", "<Keyboard>/enter", "Movement/SubmitChat", "", 0.029940119f)
			};
			if (!((Object)(object)__instance == (Object)(object)GameNetworkManager.Instance.localPlayerController))
			{
				return;
			}
			if (IngamePlayerSettings.Instance.settings.keyBindings != string.Empty)
			{
				ReadKeybinds(IngamePlayerSettings.Instance.settings.keyBindings);
			}
			if (__instance.drunkness > 0f)
			{
				drunknessCooldown = 1f;
				AnimationCurve drunknessSideEffect = StartOfRound.Instance.drunknessSideEffect;
				float num = drunknessSideEffect.Evaluate(__instance.drunkness);
				float num2 = Mathf.Clamp(QuickActionChance * (num * 2f), QuickActionChance, 0.3f);
				if (Time.time % 3f < Time.deltaTime && Random.Range(0f, 1f) <= num2)
				{
					foreach (KeyValuePair<string, (string, string, string, string, float)> item7 in keybindLookup)
					{
						float item = keybindLookup[item7.Key].chance;
						if (Random.Range(0f, 1f) <= item)
						{
							RealisticTZPBase.Instance.Log.LogInfo((object)("Short Press: " + item7.Key));
							SimulateKeyPress(item7.Key);
							break;
						}
					}
				}
				float num3 = Mathf.Clamp(LongActionChance * (num * 2f), LongActionChance, 0.3f);
				if (Time.time % 4f < Time.deltaTime && Random.Range(0f, 1f) <= num3)
				{
					foreach (KeyValuePair<string, (string, string, string, string, float)> item8 in keybindLookup)
					{
						float item2 = keybindLookup[item8.Key].chance;
						if (Random.Range(0f, 1f) <= item2)
						{
							RealisticTZPBase.Instance.Log.LogInfo((object)("Long Press: " + item8.Key));
							SimulateInputForSeconds(item8.Key, 3f);
							break;
						}
					}
				}
				MouseSensitivityInertia();
				if (num > 0.78f)
				{
					DrunknessTime += Time.deltaTime;
				}
				else
				{
					DrunknessTime = 0f;
				}
				if (DrunknessTime >= 50f)
				{
					isKilling = true;
				}
			}
			else if (drunknessCooldown > 0f)
			{
				drunknessCooldown -= Time.deltaTime;
			}
			else
			{
				mouseVelocity = Vector2.zero;
			}
			if (isKilling && DrunknessTime < 50f)
			{
				isKilling = false;
			}
			void MouseSensitivityInertia()
			{
				//IL_0065: Unknown result type (might be due to invalid IL or missing references)
				//IL_006a: Unknown result type (might be due to invalid IL or missing references)
				//IL_0093: 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_009e: Unknown result type (might be due to invalid IL or missing references)
				//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
				//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
				//IL_00af: Unknown result type (might be due to invalid IL or missing references)
				//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
				//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
				//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
				//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
				//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
				//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
				//IL_0081: Unknown result type (might be due to invalid IL or missing references)
				//IL_0086: Unknown result type (might be due to invalid IL or missing references)
				//IL_0088: Unknown result type (might be due to invalid IL or missing references)
				//IL_008d: Unknown result type (might be due to invalid IL or missing references)
				AnimationCurve drunknessSideEffect2 = StartOfRound.Instance.drunknessSideEffect;
				float num4 = drunknessSideEffect2.Evaluate(__instance.drunkness);
				float num5 = 1f - num4;
				float num6 = Mathf.Clamp(inertia * (0.5f + num4), 0f, 1f);
				float num7 = Mathf.Clamp(drag * num5, 0.025f, 10f);
				Vector2 val = ((InputControl<Vector2>)(object)((Pointer)Mouse.current).delta).ReadValue();
				if (((Vector2)(ref val)).magnitude > 0.01f)
				{
					mouseVelocity += val;
				}
				mouseVelocity *= num6;
				mouseVelocity -= mouseVelocity * num7;
				InputSystem.QueueDeltaStateEvent<Vector2>((InputControl)(object)((Pointer)Mouse.current).delta, mouseVelocity, -1.0);
				InputSystem.Update();
				mouseVelocity = Vector2.ClampMagnitude(mouseVelocity, 100f);
			}
			void ReadKeybinds(string jsonString)
			{
				if (string.IsNullOrEmpty(jsonString))
				{
					RealisticTZPBase.Instance.Log.LogWarning((object)"JsonString Empty! Detected: ReadKeybinds...");
					return;
				}
				KeybindingData keybindingData = JsonConvert.DeserializeObject<KeybindingData>(jsonString);
				foreach (Binding binding in keybindingData.bindings)
				{
					string path = binding.path;
					string action = binding.action;
					string id = binding.id;
					foreach (string item9 in keybindLookup.Keys.ToList())
					{
						if (keybindLookup[item9].id.Equals(id))
						{
							keybindLookup[item9] = (keybindLookup[item9].id, keybindLookup[item9].key, keybindLookup[item9].action, path, keybindLookup[item9].chance);
							break;
						}
					}
				}
			}
			async Task SimulateInputForSeconds(string input, float duration)
			{
				float startTime = Time.realtimeSinceStartup;
				while (Time.realtimeSinceStartup - startTime < duration)
				{
					SimulateKeyPress(input);
					await Task.Delay(30);
				}
			}
			void SimulateKeyPress(string KeyAction)
			{
				//IL_0005: Unknown result type (might be due to invalid IL or missing references)
				//IL_001a: Unknown result type (might be due to invalid IL or missing references)
				//IL_0408: Unknown result type (might be due to invalid IL or missing references)
				//IL_03ef: Unknown result type (might be due to invalid IL or missing references)
				//IL_03d3: Unknown result type (might be due to invalid IL or missing references)
				//IL_0217: Unknown result type (might be due to invalid IL or missing references)
				//IL_0282: Unknown result type (might be due to invalid IL or missing references)
				//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
				//IL_02a2: Unknown result type (might be due to invalid IL or missing references)
				//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
				//IL_02c2: Unknown result type (might be due to invalid IL or missing references)
				//IL_0115: Unknown result type (might be due to invalid IL or missing references)
				//IL_02e2: Unknown result type (might be due to invalid IL or missing references)
				//IL_0135: Unknown result type (might be due to invalid IL or missing references)
				//IL_0302: Unknown result type (might be due to invalid IL or missing references)
				//IL_0155: Unknown result type (might be due to invalid IL or missing references)
				bool flag = false;
				MouseState val2 = default(MouseState);
				InputControlExtensions.CopyState<MouseState>((InputDevice)(object)Mouse.current, ref val2);
				KeyboardState val3 = default(KeyboardState);
				InputControlExtensions.CopyState<KeyboardState>((InputDevice)(object)Keyboard.current, ref val3);
				if (keybindLookup.TryGetValue(KeyAction, out (string, string, string, string, float) value))
				{
					string item3 = value.Item1;
					string item4 = value.Item2;
					string item5 = value.Item4;
					string item6 = value.Item3;
					if (item5 == "")
					{
						if (item4.Contains("<Mouse>/"))
						{
							flag = true;
							if (!item4.Contains("scroll"))
							{
								switch (item4.Replace("<Mouse>/", ""))
								{
								case "leftButton":
									((MouseState)(ref val2)).WithButton((MouseButton)0, true);
									break;
								case "rightButton":
									((MouseState)(ref val2)).WithButton((MouseButton)1, true);
									break;
								case "middleButton":
									((MouseState)(ref val2)).WithButton((MouseButton)2, true);
									break;
								case "forwardButton":
									((MouseState)(ref val2)).WithButton((MouseButton)3, true);
									break;
								case "backButton":
									((MouseState)(ref val2)).WithButton((MouseButton)4, true);
									break;
								}
							}
							else
							{
								item4 = item4.Replace("<Mouse>/scroll/", "");
								if (item4 == "up")
								{
									InputControlExtensions.QueueValueChange<float>((InputControl<float>)(object)Mouse.current.scroll.up, 1f, -1.0);
								}
								else if (item4 == "down")
								{
									InputControlExtensions.QueueValueChange<float>((InputControl<float>)(object)Mouse.current.scroll.down, 1f, -1.0);
								}
								InputSystem.Update();
							}
						}
						else
						{
							item4 = item4.Replace("<Keyboard>/", "");
							if (Enum.TryParse<Key>(item4, ignoreCase: true, out Key result))
							{
								((KeyboardState)(ref val3)).Press(result);
							}
						}
					}
					else if (item5.Contains("<Mouse>/"))
					{
						flag = true;
						if (!item5.Contains("scroll"))
						{
							switch (item5.Replace("<Mouse>/", ""))
							{
							case "leftButton":
								((MouseState)(ref val2)).WithButton((MouseButton)0, true);
								break;
							case "rightButton":
								((MouseState)(ref val2)).WithButton((MouseButton)1, true);
								break;
							case "middleButton":
								((MouseState)(ref val2)).WithButton((MouseButton)2, true);
								break;
							case "forwardButton":
								((MouseState)(ref val2)).WithButton((MouseButton)3, true);
								break;
							case "backButton":
								((MouseState)(ref val2)).WithButton((MouseButton)4, true);
								break;
							}
						}
						else
						{
							item5 = item5.Replace("<Mouse>/scroll/", "");
							RealisticTZPBase.Instance.Log.LogWarning((object)"replaced scroll");
							if (item5 == "up")
							{
								InputControlExtensions.QueueValueChange<float>((InputControl<float>)(object)Mouse.current.scroll.up, 1f, -1.0);
							}
							else if (item5 == "down")
							{
								InputControlExtensions.QueueValueChange<float>((InputControl<float>)(object)Mouse.current.scroll.down, 1f, -1.0);
							}
						}
					}
					else
					{
						item5 = item5.Replace("<Keyboard>/", "");
						if (Enum.TryParse<Key>(item5, ignoreCase: true, out Key result2))
						{
							((KeyboardState)(ref val3)).Press(result2);
						}
					}
				}
				if (!flag)
				{
					InputSystem.QueueStateEvent<KeyboardState>((InputDevice)(object)Keyboard.current, val3, -1.0);
				}
				else
				{
					InputSystem.QueueStateEvent<MouseState>((InputDevice)(object)Mouse.current, val2, -1.0);
					InputSystem.Update();
				}
			}
		}
	}
}