Decompiled source of Motion Tracker V2 v1.1.4

MotionTracker-V2.dll

Decompiled 3 months ago
using System;
using System.CodeDom.Compiler;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using GameNetcodeStuff;
using HarmonyLib;
using LethalLib.Modules;
using Microsoft.CodeAnalysis;
using MotionTracker.Config;
using MotionTracker.Patches;
using Unity.Collections;
using Unity.Netcode;
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(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("LC_MotionTracker")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("LC_MotionTracker")]
[assembly: AssemblyTitle("LC_MotionTracker")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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;
		}
	}
}
public struct LineDrawer
{
	private LineRenderer lineRenderer;

	private float lineSize;

	public LineDrawer(float lineSize = 0.2f)
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Expected O, but got Unknown
		//IL_0028: Unknown result type (might be due to invalid IL or missing references)
		//IL_0032: Expected O, but got Unknown
		GameObject val = new GameObject("LineObj");
		lineRenderer = val.AddComponent<LineRenderer>();
		((Renderer)lineRenderer).material = new Material(Shader.Find("Hidden/Internal-Colored"));
		this.lineSize = lineSize;
	}

	private void Init(float lineSize = 0.2f)
	{
		//IL_0017: Unknown result type (might be due to invalid IL or missing references)
		//IL_001d: Expected O, but got Unknown
		//IL_0039: Unknown result type (might be due to invalid IL or missing references)
		//IL_0043: Expected O, but got Unknown
		if ((Object)(object)lineRenderer == (Object)null)
		{
			GameObject val = new GameObject("LineObj");
			lineRenderer = val.AddComponent<LineRenderer>();
			((Renderer)lineRenderer).material = new Material(Shader.Find("Hidden/Internal-Colored"));
			this.lineSize = lineSize;
		}
	}

	public void DrawLineInGameView(Vector3 start, Vector3 end, Color color)
	{
		//IL_0025: Unknown result type (might be due to invalid IL or missing references)
		//IL_0032: Unknown result type (might be due to invalid IL or missing references)
		//IL_0071: Unknown result type (might be due to invalid IL or missing references)
		//IL_007f: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)lineRenderer == (Object)null)
		{
			Init();
		}
		lineRenderer.startColor = color;
		lineRenderer.endColor = color;
		lineRenderer.startWidth = lineSize;
		lineRenderer.endWidth = lineSize;
		lineRenderer.positionCount = 2;
		lineRenderer.SetPosition(0, start);
		lineRenderer.SetPosition(1, end);
	}

	public void Destroy()
	{
		if ((Object)(object)lineRenderer != (Object)null)
		{
			Object.Destroy((Object)(object)((Component)lineRenderer).gameObject);
		}
	}
}
namespace LC_MotionTracker
{
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "LC_MotionTracker";

		public const string PLUGIN_NAME = "LC_MotionTracker";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace MotionTracker
{
	[GeneratedCode("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
	[DebuggerNonUserCode]
	[CompilerGenerated]
	internal class MotionTrackerResource
	{
		private static ResourceManager resourceMan;

		private static CultureInfo resourceCulture;

		[EditorBrowsable(EditorBrowsableState.Advanced)]
		internal static ResourceManager ResourceManager
		{
			get
			{
				if (resourceMan == null)
				{
					ResourceManager resourceManager = new ResourceManager("LC_MotionTracker.MotionTrackerResource", typeof(MotionTrackerResource).Assembly);
					resourceMan = resourceManager;
				}
				return resourceMan;
			}
		}

		[EditorBrowsable(EditorBrowsableState.Advanced)]
		internal static CultureInfo Culture
		{
			get
			{
				return resourceCulture;
			}
			set
			{
				resourceCulture = value;
			}
		}

		internal static byte[] motiontrackerled
		{
			get
			{
				object @object = ResourceManager.GetObject("motiontrackerled", resourceCulture);
				return (byte[])@object;
			}
		}

		internal MotionTrackerResource()
		{
		}
	}
	internal class Utils
	{
		public class Main
		{
			public static bool IsIngame()
			{
				GameObject val = GameObject.Find("Environment");
				if ((Object)(object)val != (Object)null)
				{
					return true;
				}
				return false;
			}
		}
	}
	[BepInPlugin("Entity378.MotionTracker-V2", "MotionTracker-V2", "1.1.4")]
	[BepInDependency("evaisa.lethallib", "0.14.2")]
	public class Plugin : BaseUnityPlugin
	{
		private readonly Harmony harmony = new Harmony("Entity378.MotionTracker-V2");

		private const string GUID = "Entity378.MotionTracker-V2";

		private const string NAME = "MotionTracker-V2";

		private const string VERSION = "1.1.4";

		private static Item motionTrackerLED_Item;

		private static MotionTrackerScript spawnedMotionTracker;

		private void Awake()
		{
			MotionTrackerConfig.LoadConfig(((BaseUnityPlugin)this).Config);
			Harmony.CreateAndPatchAll(typeof(MotionTrackerConfig), (string)null);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"MotionTrackerLog: Config loaded");
			AssetBundle val = AssetBundle.LoadFromMemory(MotionTrackerResource.motiontrackerled);
			motionTrackerLED_Item = val.LoadAsset<Item>("assets/MotionTrackerItem.asset");
			NetworkObject component = motionTrackerLED_Item.spawnPrefab.GetComponent<NetworkObject>();
			component.AutoObjectParentSync = false;
			spawnedMotionTracker = motionTrackerLED_Item.spawnPrefab.AddComponent<MotionTrackerScript>();
			((GrabbableObject)spawnedMotionTracker).itemProperties = motionTrackerLED_Item;
			((GrabbableObject)spawnedMotionTracker).isInFactory = true;
			Items.RegisterShopItem(motionTrackerLED_Item, MotionTrackerConfig.MotionTrackerCost);
			NetworkPrefabs.RegisterNetworkPrefab(motionTrackerLED_Item.spawnPrefab);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"MotionTrackerLog: Plugin MotionTracker-V2 is loaded!");
		}
	}
}
namespace MotionTracker.Patches
{
	public struct ScannedEntity
	{
		public Collider obj;

		public Vector3 position;

		public Vector3 rawPosition;

		public float speed;

		public GameObject blip;
	}
	public class MotionTrackerScript : GrabbableObject
	{
		private GameObject baseRadar;

		private GameObject baseRadarOff;

		private GameObject LED;

		private GameObject blip;

		private GameObject blipParent;

		private float searchRadius = MotionTrackerConfig.MotionTrackerRange;

		private Hashtable scannedEntities = new Hashtable();

		private List<GameObject> blipPool = new List<GameObject>();

		private int maxEntities = 50;

		private Collider[] colliders = (Collider[])(object)new Collider[200];

		public void Awake()
		{
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Expected O, but got Unknown
			base.grabbable = true;
			base.grabbableToEnemies = true;
			base.mainObjectRenderer = ((Component)this).GetComponent<MeshRenderer>();
			base.useCooldown = 1f;
			base.insertedBattery = new Battery(false, 1f);
			base.itemProperties.batteryUsage = MotionTrackerConfig.MotionTrackerBatteryDuration;
			baseRadar = ((Component)((Component)this).transform.Find("Canvas/BaseRadar")).gameObject;
			baseRadarOff = ((Component)((Component)this).transform.Find("Canvas/BaseRadar_off")).gameObject;
			LED = ((Component)((Component)this).transform.Find("LED")).gameObject;
			blipParent = ((Component)((Component)this).transform.Find("Canvas/BlipParent")).gameObject;
			blip = ((Component)((Component)this).transform.Find("Canvas/BlipParent/Blip")).gameObject;
			blip.SetActive(false);
			blipPool.Add(blip);
			for (int i = 1; i < maxEntities; i++)
			{
				blipPool.Add(((Component)((Component)this).transform.Find($"Canvas/BlipParent/Blip ({i})")).gameObject);
				blipPool[i].SetActive(false);
			}
			Enable(enable: false);
		}

		private void Enable(bool enable, bool inHand = true)
		{
			baseRadar.SetActive(enable);
			LED.SetActive(enable);
			if (inHand)
			{
				baseRadarOff.SetActive(!enable);
			}
			else
			{
				baseRadarOff.SetActive(false);
			}
			if (enable)
			{
				return;
			}
			foreach (GameObject item in blipPool)
			{
				item.SetActive(false);
			}
		}

		public override void ItemActivate(bool used, bool buttonDown = true)
		{
			((GrabbableObject)this).ItemActivate(used, buttonDown);
			Enable(used);
			Debug.Log((object)$"Motion tracker activate? : {used}");
		}

		public override void UseUpBatteries()
		{
			((GrabbableObject)this).UseUpBatteries();
			Enable(enable: false, inHand: false);
		}

		public override void Update()
		{
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
			//IL_0102: Unknown result type (might be due to invalid IL or missing references)
			//IL_0103: Unknown result type (might be due to invalid IL or missing references)
			//IL_014d: Unknown result type (might be due to invalid IL or missing references)
			//IL_015d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0162: Unknown result type (might be due to invalid IL or missing references)
			//IL_0167: Unknown result type (might be due to invalid IL or missing references)
			//IL_0175: Unknown result type (might be due to invalid IL or missing references)
			//IL_017a: Unknown result type (might be due to invalid IL or missing references)
			//IL_01af: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01da: Unknown result type (might be due to invalid IL or missing references)
			//IL_01df: Unknown result type (might be due to invalid IL or missing references)
			//IL_0252: Unknown result type (might be due to invalid IL or missing references)
			//IL_0257: Unknown result type (might be due to invalid IL or missing references)
			//IL_025b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0260: Unknown result type (might be due to invalid IL or missing references)
			//IL_026f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0293: Unknown result type (might be due to invalid IL or missing references)
			//IL_02bb: Unknown result type (might be due to invalid IL or missing references)
			((GrabbableObject)this).Update();
			if (base.isPocketed)
			{
				Enable(enable: false, inHand: false);
			}
			else
			{
				if (!base.isBeingUsed)
				{
					return;
				}
				if (base.isHeld)
				{
					Enable(enable: true);
					blipParent.transform.localRotation = Quaternion.Euler(0f, 0f, ((Component)base.playerHeldBy).transform.eulerAngles.y + 0f);
				}
				else
				{
					Enable(enable: false, inHand: false);
				}
				if (base.insertedBattery.empty)
				{
					Enable(enable: false);
					return;
				}
				for (int i = 0; i < maxEntities; i++)
				{
					blipPool[i].SetActive(false);
				}
				Hashtable hashtable = new Hashtable(scannedEntities);
				scannedEntities.Clear();
				Vector3 position = ((Component)this).transform.position;
				int num = Physics.OverlapSphereNonAlloc(position, searchRadius, colliders, 524296);
				int num2 = 0;
				for (int j = 0; j < num; j++)
				{
					Collider val = colliders[j];
					ScannedEntity scannedEntity = default(ScannedEntity);
					scannedEntity.obj = val;
					scannedEntity.position = ((Component)val).transform.position - baseRadar.transform.position;
					scannedEntity.rawPosition = ((Component)val).transform.position;
					ScannedEntity scannedEntity2 = scannedEntity;
					if (hashtable.Contains(((object)((Component)scannedEntity2.obj).transform).GetHashCode()))
					{
						Vector3 val2 = ((Component)val).transform.position - ((ScannedEntity)hashtable[((object)((Component)scannedEntity2.obj).transform).GetHashCode()]).rawPosition;
						scannedEntity2.speed = ((Vector3)(ref val2)).magnitude;
					}
					else
					{
						scannedEntity2.speed = 0f;
					}
					if (!scannedEntities.Contains(((object)((Component)scannedEntity2.obj).transform).GetHashCode()))
					{
						scannedEntity2.blip = blipPool[num2];
						num2++;
						Vector3 localPosition = scannedEntity2.blip.transform.localPosition;
						localPosition = scannedEntity2.position;
						scannedEntity2.blip.transform.localPosition = new Vector3(Remap(localPosition.x, 0f - searchRadius, searchRadius, -45f, 45f), Remap(localPosition.z, 0f - searchRadius, searchRadius, -45f, 45f), -0.1f);
						scannedEntity2.blip.SetActive(scannedEntity2.speed > MotionTrackerConfig.MotionTrackerSpeedDetect);
						scannedEntities.Add(((object)((Component)scannedEntity2.obj).transform).GetHashCode(), scannedEntity2);
					}
				}
			}
		}

		public float Remap(float from, float fromMin, float fromMax, float toMin, float toMax)
		{
			float num = from - fromMin;
			float num2 = fromMax - fromMin;
			float num3 = num / num2;
			float num4 = toMax - toMin;
			float num5 = num4 * num3;
			return num5 + toMin;
		}
	}
}
namespace MotionTracker.Config
{
	public class MotionTrackerConfig
	{
		[CompilerGenerated]
		private static class <>O
		{
			public static HandleNamedMessageDelegate <0>__OnRequestSync;

			public static HandleNamedMessageDelegate <1>__OnReceiveSync;
		}

		private const int byteDim = 4;

		private const int byteDimConfig = 21;

		private static int MotionTrackerCostLocal = 30;

		private static float MotionTrackerBatteryDurationLocal = 200f;

		private static float MotionTrackerSpeedDetectLocal = 0.01f;

		private static float MotionTrackerRangeLocal = 50f;

		public static int MotionTrackerCost = 30;

		public static float MotionTrackerBatteryDuration = 200f;

		public static float MotionTrackerSpeedDetect = 0.01f;

		public static float MotionTrackerRange = 50f;

		private static void SetValues(int Cost, float BatteryDuration, float SpeedDetect, float Range)
		{
			MotionTrackerCost = Cost;
			MotionTrackerBatteryDuration = BatteryDuration;
			MotionTrackerSpeedDetect = SpeedDetect;
			MotionTrackerRange = Range;
		}

		private static void SetToLocalValues()
		{
			SetValues(MotionTrackerCostLocal, MotionTrackerBatteryDurationLocal, MotionTrackerSpeedDetectLocal, MotionTrackerRangeLocal);
		}

		public static void LoadConfig(ConfigFile config)
		{
			MotionTrackerCostLocal = Math.Clamp(config.Bind<int>("General", "MotionTrackerCost", 30, "Motion Tracker's cost").Value, 0, 9999);
			MotionTrackerBatteryDurationLocal = Mathf.Clamp(config.Bind<int>("General", "MotionTrackerBatteryDuration", 200, "Motion Tracker's battery life").Value, 0, 9999);
			MotionTrackerSpeedDetectLocal = Mathf.Clamp(config.Bind<float>("General", "MotionTrackerSpeedDetect", 0.01f, "Minimum speed at which entities can be detected by the Motion Tracker (0.05 is faster than a vanilla crouch walk)").Value, 0f, 9999f);
			MotionTrackerRangeLocal = Mathf.Clamp(config.Bind<int>("General", "MotionTrackerRange", 50, "Motion Tracker's range of action").Value, 0, 9999);
			SetToLocalValues();
		}

		public static byte[] GetSettings()
		{
			byte[] array = new byte[21];
			array[0] = 1;
			Array.Copy(BitConverter.GetBytes(MotionTrackerCostLocal), 0, array, 1, 4);
			Array.Copy(BitConverter.GetBytes(MotionTrackerBatteryDurationLocal), 0, array, 5, 4);
			Array.Copy(BitConverter.GetBytes(MotionTrackerSpeedDetectLocal), 0, array, 9, 4);
			Array.Copy(BitConverter.GetBytes(MotionTrackerRangeLocal), 0, array, 13, 4);
			return array;
		}

		public static void SetSettings(byte[] data)
		{
			byte b = data[0];
			byte b2 = b;
			if (b2 == 1)
			{
				MotionTrackerCost = BitConverter.ToInt32(data, 1);
				MotionTrackerBatteryDuration = BitConverter.ToSingle(data, 5);
				MotionTrackerSpeedDetect = BitConverter.ToSingle(data, 9);
				MotionTrackerRange = BitConverter.ToSingle(data, 13);
				Debug.Log((object)"MotionTrackerLog: Host config set successfully");
				return;
			}
			throw new Exception("MotionTrackerLog: Invalid version byte");
		}

		private static bool IsHost()
		{
			return NetworkManager.Singleton.IsHost;
		}

		public static void OnRequestSync(ulong clientID, FastBufferReader reader)
		{
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			if (!IsHost())
			{
				return;
			}
			Debug.Log((object)("MotionTrackerLog: Sending config to client " + clientID));
			byte[] settings = GetSettings();
			FastBufferWriter val = default(FastBufferWriter);
			((FastBufferWriter)(ref val))..ctor(settings.Length, (Allocator)2, settings.Length);
			try
			{
				((FastBufferWriter)(ref val)).WriteBytes(settings, -1, 0);
				NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage("MotionTracker_OnReceiveConfigSync", clientID, val, (NetworkDelivery)2);
			}
			catch (Exception ex)
			{
				Debug.LogError((object)("MotionTrackerLog: Failed to send config: " + ex));
			}
			finally
			{
				((FastBufferWriter)(ref val)).Dispose();
			}
		}

		public static void OnReceiveSync(ulong clientID, FastBufferReader reader)
		{
			Debug.Log((object)"MotionTrackerLog: Received config from host");
			byte[] settings = new byte[21];
			try
			{
				((FastBufferReader)(ref reader)).ReadBytes(ref settings, 21, 0);
				SetSettings(settings);
			}
			catch (Exception ex)
			{
				Debug.LogError((object)("MotionTrackerLog: Failed to receive config: " + ex));
				SetToLocalValues();
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(PlayerControllerB), "ConnectClientToPlayerObject")]
		private static void ServerConnect()
		{
			//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Expected O, but got Unknown
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Expected O, but got Unknown
			if (IsHost())
			{
				Debug.Log((object)"MotionTrackerLog: Started hosting, using local settings");
				SetToLocalValues();
				CustomMessagingManager customMessagingManager = NetworkManager.Singleton.CustomMessagingManager;
				object obj = <>O.<0>__OnRequestSync;
				if (obj == null)
				{
					HandleNamedMessageDelegate val = OnRequestSync;
					<>O.<0>__OnRequestSync = val;
					obj = (object)val;
				}
				customMessagingManager.RegisterNamedMessageHandler("MotionTracker_OnRequestConfigSync", (HandleNamedMessageDelegate)obj);
				return;
			}
			Debug.Log((object)"MotionTrackerLog: Connected to server, requesting settings");
			CustomMessagingManager customMessagingManager2 = NetworkManager.Singleton.CustomMessagingManager;
			object obj2 = <>O.<1>__OnReceiveSync;
			if (obj2 == null)
			{
				HandleNamedMessageDelegate val2 = OnReceiveSync;
				<>O.<1>__OnReceiveSync = val2;
				obj2 = (object)val2;
			}
			customMessagingManager2.RegisterNamedMessageHandler("MotionTracker_OnReceiveConfigSync", (HandleNamedMessageDelegate)obj2);
			FastBufferWriter val3 = default(FastBufferWriter);
			((FastBufferWriter)(ref val3))..ctor(4, (Allocator)2, -1);
			NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage("MotionTracker_OnRequestConfigSync", 0uL, val3, (NetworkDelivery)2);
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(GameNetworkManager), "StartDisconnect")]
		private static void ServerDisconnect()
		{
			Debug.Log((object)"MotionTrackerLog: Server disconnect");
			SetToLocalValues();
		}
	}
}