Decompiled source of CrossHair v1.1.0

BepInEx/plugins/com.ctnoriginals.cw.crosshair.dll

Decompiled 8 months ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using CTNOriginals.ContentWarning.CrossHair.Handlers;
using CTNOriginals.ContentWarning.CrossHair.Patches;
using CTNOriginals.ContentWarning.CrossHair.Utilities;
using Microsoft.CodeAnalysis;
using On;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("com.ctnoriginals.cw.crosshair")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Adds a CrossHair to the center of your screen to indicate where you are aiming.")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyInformationalVersion("1.1.0")]
[assembly: AssemblyProduct("CrossHair")]
[assembly: AssemblyTitle("com.ctnoriginals.cw.crosshair")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.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;
		}
	}
}
namespace CTNOriginals.ContentWarning.CrossHair
{
	public class Config
	{
		public static ConfigFile Configurations = new ConfigFile(Paths.ConfigPath + "\\" + Plugin.PluginGUID + ".cfg", true);

		public static ConfigEntry<string> CrossHairText;

		public static ConfigEntry<float> CrossHairSize;

		public static ConfigEntry<bool> CrossHairShadow;

		public static ConfigEntry<string> CrossHairColor;

		public static ConfigEntry<int> CrossHairOpacity;

		public void LoadFile()
		{
			CrossHairText = Configurations.Bind<string>("!General", "CrossHairText", "-  +  -", "Text to display as crosshair (use \\n for new line)");
			CrossHairSize = Configurations.Bind<float>("!General", "CrossHairSize", 40f, "Size of the crosshair");
			CrossHairShadow = Configurations.Bind<bool>("!General", "CrossHairShadow", true, "Whether to display a shadow behind the crosshair");
			CrossHairColor = Configurations.Bind<string>("Appearance", "CrossHairColor", "ffffff", "Color of the crosshair in hexadecimal (Do not include the #)");
			CrossHairOpacity = Configurations.Bind<int>("Appearance", "CrossHairOpacity", 80, "Opacity of the crosshair (0 to 100)%");
			CLogger.LogInfo("CrossHairText: " + CrossHairText.Value);
			CLogger.LogInfo($"CrossHairSize: {CrossHairSize.Value}");
			CLogger.LogInfo($"CrossHairShadow: {CrossHairShadow.Value}");
			CLogger.LogInfo("CrossHairColor: " + CrossHairColor.Value);
			CLogger.LogInfo($"CrossHairOpacity: {CrossHairOpacity.Value}");
			Configurations.Save();
		}
	}
	[BepInPlugin("com.ctnoriginals.cw.crosshair", "CrossHair", "1.1.0")]
	public class Plugin : BaseUnityPlugin
	{
		public static ManualLogSource ManualLog;

		public static string PluginGUID = "com.ctnoriginals.cw.crosshair";

		public static string PluginName = "CrossHair";

		public static string PluginVersion = "1.1.0";

		public static bool DebugMode = false;

		public static bool OutputDebugLogs = false;

		public static Config ConfigFile = new Config();

		private void Awake()
		{
			ManualLog = ((BaseUnityPlugin)this).Logger;
			ManualLog.LogInfo((object)("Plugin " + PluginName + " is loaded! Version: " + PluginVersion + " (" + (DebugMode ? "Debug" : "Release") + ")"));
			ConfigFile.LoadFile();
			HelmetUIPatch.Patch();
			PlayerPatch.Patch();
		}

		private void Update()
		{
		}
	}
}
namespace CTNOriginals.ContentWarning.CrossHair.Utilities
{
	public static class CLogger
	{
		public static void Log(string message)
		{
			SendLog(message, "Log");
		}

		public static void LogInfo(string message)
		{
			SendLog(message, "LogInfo");
		}

		public static void LogError(string message)
		{
			SendLog(message, "LogError");
		}

		public static void LogWarning(string message)
		{
			SendLog(message, "LogWarning");
		}

		public static void LogDebug(string message)
		{
			SendLog(message, "LogDebug");
		}

		public static void LogFatal(string message)
		{
			SendLog(message, "LogFatal");
		}

		public static void LogMessage(string message)
		{
			SendLog(message, "LogMessage");
		}

		private static void SendLog(string message, string level = null)
		{
			if (!Plugin.DebugMode && (level == "LogDebug" || level == "LogInfo"))
			{
				return;
			}
			switch (level)
			{
			case "LogInfo":
				Plugin.ManualLog.LogInfo((object)message);
				return;
			case "LogError":
				Plugin.ManualLog.LogError((object)message);
				return;
			case "LogWarning":
				Plugin.ManualLog.LogWarning((object)message);
				return;
			case "LogDebug":
				Plugin.ManualLog.LogDebug((object)message);
				return;
			case "LogFatal":
				Plugin.ManualLog.LogFatal((object)message);
				return;
			case "LogMessage":
				Plugin.ManualLog.LogMessage((object)message);
				return;
			}
			if (level != "Log")
			{
				Debug.Log((object)("[" + level + "]: " + message));
			}
			else
			{
				Debug.Log((object)message);
			}
		}
	}
}
namespace CTNOriginals.ContentWarning.CrossHair.Patches
{
	public class HelmetUIPatch
	{
		[CompilerGenerated]
		private static class <>O
		{
			public static hook_Start <0>__StartPatch;

			public static hook_Update <1>__UpdatePatch;
		}

		public static Transform CrossHair;

		public static TextMeshProUGUI CrossHairTMP;

		public static float CrossHairAlpha = 200f;

		public static Transform CrossHairShadow;

		public static float CrossHairShadowAlpha = 100f;

		public static void Patch()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Expected O, but got Unknown
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Expected O, but got Unknown
			object obj = <>O.<0>__StartPatch;
			if (obj == null)
			{
				hook_Start val = StartPatch;
				<>O.<0>__StartPatch = val;
				obj = (object)val;
			}
			HelmetUIToggler.Start += (hook_Start)obj;
			object obj2 = <>O.<1>__UpdatePatch;
			if (obj2 == null)
			{
				hook_Update val2 = UpdatePatch;
				<>O.<1>__UpdatePatch = val2;
				obj2 = (object)val2;
			}
			HelmetUIToggler.Update += (hook_Update)obj2;
		}

		private static void StartPatch(orig_Start orig, HelmetUIToggler self)
		{
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d0: 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_0298: Unknown result type (might be due to invalid IL or missing references)
			//IL_029d: 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)
			orig.Invoke(self);
			CLogger.LogDebug("HelmetUIToggler.Start() called");
			Transform transform = GameObject.Find("GAME/HelmetUI/Pivot/Others/").transform;
			CLogger.LogDebug("CrossHair Parent: " + ((Object)transform).name);
			CrossHair = ((TMP_Text)new GameObject().AddComponent<TextMeshProUGUI>()).transform;
			CrossHairTMP = ((Component)CrossHair).GetComponent<TextMeshProUGUI>();
			RectTransform rectTransform = ((TMP_Text)CrossHairTMP).rectTransform;
			((Object)CrossHair).name = "CrossHair";
			((TMP_Text)CrossHairTMP).SetText("-  +  -", true);
			((TMP_Text)CrossHairTMP).alignment = (TextAlignmentOptions)514;
			((Transform)rectTransform).SetParent(transform, false);
			rectTransform.anchoredPosition = new Vector2(0f, 0f);
			rectTransform.offsetMin = new Vector2(-500f, -500f);
			rectTransform.offsetMax = new Vector2(500f, 500f);
			string text = Config.CrossHairColor.Value;
			if (text.Length != 6)
			{
				text = HexFormatException("character amount: \"" + text + "\"");
			}
			int num = 16777215;
			try
			{
				num = int.Parse(text.Replace("#", ""), NumberStyles.HexNumber);
			}
			catch (FormatException)
			{
				num = int.Parse(HexFormatException("color: \"" + text + "\""), NumberStyles.HexNumber);
			}
			Color color = Color.FromArgb(num);
			CrossHairAlpha = (int)(byte)(Config.CrossHairOpacity.Value * 255 / 100);
			CrossHairShadowAlpha = (int)(byte)(CrossHairAlpha * 50f / 100f);
			((TMP_Text)CrossHairTMP).text = Config.CrossHairText.Value;
			((TMP_Text)CrossHairTMP).fontSize = Config.CrossHairSize.Value;
			((Graphic)CrossHairTMP).color = Color32.op_Implicit(new Color32(color.R, color.G, color.B, (byte)Mathf.RoundToInt(CrossHairAlpha)));
			CLogger.LogDebug($"CrossHairColor: ({color.R}, {color.G}, {color.B}, {CrossHairAlpha})");
			((TMP_Text)CrossHairTMP).alignment = (TextAlignmentOptions)514;
			((Behaviour)CrossHairTMP).enabled = true;
			if (Config.CrossHairShadow.Value)
			{
				CrossHairShadow = Object.Instantiate<Transform>(CrossHair, transform);
				TextMeshProUGUI component = ((Component)CrossHairShadow).GetComponent<TextMeshProUGUI>();
				((Object)CrossHairShadow).name = "CrossHairShadow";
				((TMP_Text)component).fontSize = Config.CrossHairSize.Value;
				((Graphic)component).color = Color32.op_Implicit(new Color32((byte)0, (byte)0, (byte)0, (byte)CrossHairShadowAlpha));
				((Transform)((TMP_Text)component).rectTransform).localPosition = new Vector3(2f, -2f, 0f);
				((Transform)rectTransform).SetAsLastSibling();
				FadeHandler.FadeFields.Clear();
				FadeHandler.Initiate();
			}
		}

		private static void UpdatePatch(orig_Update orig, HelmetUIToggler self)
		{
			orig.Invoke(self);
			FadeHandler.Update();
		}

		private static string HexFormatException(string message = "color")
		{
			CLogger.LogMessage("Invalid hex " + message + ", using default color (ffffff)");
			Config.CrossHairColor.Value = "ffffff";
			Config.Configurations.Save();
			return "ffffff";
		}
	}
	public class PlayerPatch
	{
		[CompilerGenerated]
		private static class <>O
		{
			public static hook_Start <0>__StartPatch;

			public static hook_Update <1>__UpdatePatch;
		}

		public static Box<bool> AimingCamera = new Box<bool>(value: false);

		private static bool hasStarted = false;

		private static bool registerOnStart = false;

		public static void Patch()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Expected O, but got Unknown
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Expected O, but got Unknown
			object obj = <>O.<0>__StartPatch;
			if (obj == null)
			{
				hook_Start val = StartPatch;
				<>O.<0>__StartPatch = val;
				obj = (object)val;
			}
			Player.Start += (hook_Start)obj;
			object obj2 = <>O.<1>__UpdatePatch;
			if (obj2 == null)
			{
				hook_Update val2 = UpdatePatch;
				<>O.<1>__UpdatePatch = val2;
				obj2 = (object)val2;
			}
			Player.Update += (hook_Update)obj2;
		}

		private static IEnumerator StartPatch(orig_Start orig, Player self)
		{
			IEnumerator origEnum = orig.Invoke(self);
			while (origEnum.MoveNext())
			{
				yield return origEnum.Current;
			}
			CLogger.LogDebug("Player.Start()");
			hasStarted = true;
			if (registerOnStart)
			{
				registerOnStart = false;
				RegisterFields();
			}
		}

		private static void UpdatePatch(orig_Update orig, Player self)
		{
			orig.Invoke(self);
			if (self.data.isLocal)
			{
				AimingCamera.Value = self.input.aimIsPressed && Object.op_Implicit((Object)(object)self.data.currentItem) && ((Object)self.data.currentItem).name.Contains("Camera");
			}
		}

		public static void RegisterFields()
		{
			if (!hasStarted)
			{
				registerOnStart = true;
			}
			FadeHandler.AddFadeField(AimingCamera, targetValue: true, 0f, 0.1f, 1f);
		}
	}
}
namespace CTNOriginals.ContentWarning.CrossHair.Handlers
{
	public class Box<T> where T : struct
	{
		public T Value { get; set; }

		public Box(T value = default(T))
		{
			Value = value;
		}
	}
	public class FadeHandler
	{
		public class FadeValueField
		{
			public bool targetValue;

			public Box<bool> currentValue;

			private readonly float fadeValue;

			private readonly float fadeOutDuration;

			private readonly float fadeInDuration;

			private bool isFading;

			public float currentFade = 1f;

			private float fadeTimer;

			public FadeValueField(Box<bool> refValue, bool targetValue, float fadeValue = 0.2f, float fadeOutDuration = 0.2f, float fadeInDuration = 0.5f)
			{
				this.targetValue = targetValue;
				currentValue = refValue;
				this.fadeValue = fadeValue;
				this.fadeOutDuration = fadeOutDuration;
				this.fadeInDuration = fadeInDuration;
			}

			public void Update()
			{
				if (isFading)
				{
					if (currentValue.Value != targetValue)
					{
						isFading = false;
						fadeTimer = currentFade;
					}
					else if (currentFade > fadeValue)
					{
						currentFade = Mathf.Lerp(1f, fadeValue, fadeTimer);
						fadeTimer += Time.deltaTime / fadeOutDuration;
					}
				}
				else if (currentValue.Value == targetValue)
				{
					isFading = true;
					fadeTimer = 1f - currentFade;
				}
				else if (currentFade < 1f)
				{
					currentFade = Mathf.Lerp(fadeValue, 1f, fadeTimer);
					fadeTimer += Time.deltaTime / fadeInDuration;
				}
				currentFade = Mathf.Clamp(currentFade, 0f, 1f);
				fadeTimer = Mathf.Clamp(fadeTimer, 0f, 1f);
			}

			public void LogValues()
			{
				CLogger.LogDebug($"\nValue: {currentValue.Value} - Target: {targetValue}\nFade: {fadeValue} - Duration: {fadeOutDuration} - {fadeInDuration}");
			}
		}

		public static List<FadeValueField> FadeFields = new List<FadeValueField>();

		private static bool hasStarted = false;

		private static int debugCount = 0;

		public static void Initiate()
		{
			if (!hasStarted)
			{
				hasStarted = true;
				foreach (FadeValueField fadeField in FadeFields)
				{
					fadeField.LogValues();
				}
			}
			PlayerPatch.RegisterFields();
			CLogger.LogDebug($"Registered fields: {FadeFields.Count}");
		}

		public static void Update()
		{
			if (FadeFields.Count == 0)
			{
				if (debugCount % 100 == 0)
				{
					CLogger.LogDebug($"{(float)debugCount * 0.001f}: No fields, returning...");
				}
				debugCount++;
				return;
			}
			if (!hasStarted)
			{
				Initiate();
				return;
			}
			float num = 1f;
			foreach (FadeValueField fadeField in FadeFields)
			{
				fadeField.Update();
				if (fadeField.currentFade < num)
				{
					num = fadeField.currentFade;
				}
			}
			if (Mathf.Abs(((TMP_Text)HelmetUIPatch.CrossHairTMP).alpha - num * (HelmetUIPatch.CrossHairAlpha / 255f)) > 0.0001f)
			{
				SetCrossHairAlphaPercent(num);
			}
		}

		public static void AddFadeField(Box<bool> field, bool targetValue, float fadeValue = 0.2f, float fadeOutDuration = 0.2f, float fadeInDuration = 0.5f)
		{
			if (FadeFields.Any((FadeValueField x) => x.currentValue == field))
			{
				CLogger.LogDebug("Field already added");
				return;
			}
			FadeValueField item = new FadeValueField(field, targetValue, fadeValue, fadeOutDuration, fadeInDuration);
			FadeFields.Add(item);
		}

		private static void SetCrossHairAlphaPercent(float target)
		{
			if (Object.op_Implicit((Object)(object)HelmetUIPatch.CrossHair))
			{
				((TMP_Text)((Component)HelmetUIPatch.CrossHair).GetComponent<TextMeshProUGUI>()).alpha = target * (HelmetUIPatch.CrossHairAlpha / 255f);
				if (Object.op_Implicit((Object)(object)HelmetUIPatch.CrossHairShadow))
				{
					((TMP_Text)((Component)HelmetUIPatch.CrossHairShadow).GetComponent<TextMeshProUGUI>()).alpha = target * (HelmetUIPatch.CrossHairShadowAlpha / 255f);
				}
			}
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}