Decompiled source of Crosshairs v1.1.0

Mods/Crosshairs.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BoneLib;
using BoneLib.BoneMenu;
using HarmonyLib;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppSLZ.Marrow;
using Il2CppSystem;
using MelonLoader;
using MelonLoader.Preferences;
using MelonLoader.Utils;
using Microsoft.CodeAnalysis;
using NEP.Crosshairs;
using NEP.Crosshairs.Data;
using NEP.Crosshairs.Settings;
using NEP.Crosshairs.Skins;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("Crosshairs")]
[assembly: AssemblyCompany("Not Enough Photons")]
[assembly: AssemblyProduct("Crosshairs")]
[assembly: AssemblyCopyright("Developed by Not Enough Photons")]
[assembly: AssemblyTrademark("Not Enough Photons")]
[assembly: AssemblyFileVersion("1.1.0")]
[assembly: MelonInfo(typeof(Core), "Crosshairs", "1.1.0", "Not Enough Photons", "https://bonelab.thunderstore.io/p/NotEnoughPhotons/Crosshairs")]
[assembly: MelonGame("Stress Level Zero", "BONELAB")]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.0.0")]
[module: UnverifiableCode]
[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;
		}
	}
}
namespace NEP.Crosshairs
{
	public static class BuildInfo
	{
		public const string Name = "Crosshairs";

		public const string Author = "Not Enough Photons";

		public const string Company = "Not Enough Photons";

		public const string Version = "1.1.0";

		public const string DownloadLink = "https://bonelab.thunderstore.io/p/NotEnoughPhotons/Crosshairs";
	}
	public sealed class Crosshair
	{
		private CrosshairSkin m_skin;

		private GameObject m_object;

		private Transform m_art;

		private MeshRenderer m_renderer;

		private Gun m_gun;

		private Transform m_firePoint;

		private bool m_hidden;

		public Vector3 Position => m_object.transform.position;

		public Gun Gun => m_gun;

		public Crosshair(CrosshairSkin skin)
		{
			m_object = Object.Instantiate<GameObject>(DataManager.CrosshairObject);
			m_art = m_object.transform.GetChild(0);
			m_renderer = ((Component)m_art).GetComponent<MeshRenderer>();
			SetSkin(skin);
			Hide();
		}

		public void Update()
		{
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0062: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
			if (!m_hidden && Object.op_Implicit((Object)(object)m_object) && Object.op_Implicit((Object)(object)m_renderer))
			{
				((Renderer)m_renderer).material.color = new Color(CrosshairSettings.RedColor / 255f, CrosshairSettings.GreenColor / 255f, CrosshairSettings.BlueColor / 255f);
				Ray val = default(Ray);
				((Ray)(ref val))..ctor(m_firePoint.position, m_firePoint.forward);
				RaycastHit val2 = default(RaycastHit);
				if (Physics.Raycast(val, ref val2))
				{
					m_object.transform.position = ((RaycastHit)(ref val2)).point;
				}
				else
				{
					m_object.transform.position = ((Ray)(ref val)).origin + ((Ray)(ref val)).direction * 10f;
				}
				float num = Vector3.Distance(Player.Head.position, Position);
				m_art.localScale = Vector3.one * Mathf.Max(0.25f, num * 0.1f);
			}
		}

		public void SetSkin(CrosshairSkin skin)
		{
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			m_skin = skin;
			((Renderer)m_renderer).material.color = new Color(CrosshairSettings.RedColor / 255f, CrosshairSettings.GreenColor / 255f, CrosshairSettings.BlueColor / 255f);
			((Renderer)m_renderer).material.SetTexture("_BaseMap", (Texture)(object)m_skin.Texture);
		}

		public void Hide()
		{
			m_object.SetActive(false);
			((Component)m_art).gameObject.SetActive(false);
			m_hidden = true;
		}

		public void Show()
		{
			m_object.SetActive(true);
			((Component)m_art).gameObject.SetActive(true);
			m_hidden = false;
		}

		public void Attach(Gun gun)
		{
			m_gun = gun;
			m_firePoint = m_gun.firePointTransform;
			Show();
		}

		public void Detach()
		{
			m_gun = null;
			Hide();
		}
	}
	public static class CrosshairManager
	{
		private static List<Crosshair> m_crosshairs;

		public static void Initialize()
		{
			m_crosshairs = new List<Crosshair>();
		}

		public static void OnLevelLoaded()
		{
			m_crosshairs.Clear();
			Crosshair item = new Crosshair(SkinManager.SelectedSkin);
			Crosshair item2 = new Crosshair(SkinManager.SelectedSkin);
			m_crosshairs.Add(item);
			m_crosshairs.Add(item2);
		}

		public static void SetSkin(CrosshairSkin skin)
		{
			foreach (Crosshair crosshair in m_crosshairs)
			{
				crosshair.SetSkin(skin);
			}
		}

		public static void Update()
		{
			foreach (Crosshair crosshair in m_crosshairs)
			{
				crosshair.Update();
			}
		}

		public static void OnGunAttached(Gun gun)
		{
			if (!CrosshairSettings.ShowCrosshair || !IsGunFromLocalPlayer(gun))
			{
				return;
			}
			foreach (Crosshair crosshair in m_crosshairs)
			{
				if (!Object.op_Implicit((Object)(object)crosshair.Gun))
				{
					crosshair.Attach(gun);
					break;
				}
			}
		}

		public static void OnGunDetached(Gun gun)
		{
			if (!CrosshairSettings.ShowCrosshair || !IsGunFromLocalPlayer(gun))
			{
				return;
			}
			foreach (Crosshair crosshair in m_crosshairs)
			{
				if (Object.op_Implicit((Object)(object)crosshair.Gun) && (Object)(object)crosshair.Gun == (Object)(object)gun)
				{
					crosshair.Detach();
				}
			}
		}

		public static void HideCrosshairs()
		{
			foreach (Crosshair crosshair in m_crosshairs)
			{
				if (Object.op_Implicit((Object)(object)crosshair.Gun))
				{
					crosshair.Hide();
				}
			}
		}

		public static void ShowCrosshairs()
		{
			foreach (Crosshair crosshair in m_crosshairs)
			{
				if (Object.op_Implicit((Object)(object)crosshair.Gun))
				{
					crosshair.Show();
				}
			}
		}

		private static bool IsGunFromLocalPlayer(Gun gun)
		{
			return ((Object)gun.host.LastGrabbedProxy.root).Equals((Object)(object)((Component)Player.RigManager).gameObject);
		}
	}
	public class Core : MelonMod
	{
		internal static Instance Logger => new Instance("Crosshairs");

		public override void OnInitializeMelon()
		{
			SkinManager.Initialize();
			CrosshairSettings.CreateMelonPrefs();
			CrosshairSettings.CreateMenu();
			DataManager.Initialize();
			DataManager.LoadObjectsFromBundle();
			CrosshairManager.Initialize();
			Hooking.OnLevelLoaded += OnLevelLoaded;
		}

		public override void OnDeinitializeMelon()
		{
			Hooking.OnLevelLoaded -= OnLevelLoaded;
		}

		public void OnLevelLoaded(LevelInfo _)
		{
			CrosshairManager.OnLevelLoaded();
		}

		public override void OnUpdate()
		{
			CrosshairManager.Update();
		}
	}
}
namespace NEP.Crosshairs.Settings
{
	public static class CrosshairSettings
	{
		internal static Page m_root;

		internal static Page m_menu;

		internal static Page m_skins;

		internal static Page m_colorMenu;

		internal static FloatElement m_redColor;

		internal static FloatElement m_greenColor;

		internal static FloatElement m_blueColor;

		internal static MelonPreferences_Category m_crosshairsCategory;

		internal static MelonPreferences_Entry<bool> m_showCrosshairEntry;

		internal static MelonPreferences_Entry<float> m_redColorEntry;

		internal static MelonPreferences_Entry<float> m_greenColorEntry;

		internal static MelonPreferences_Entry<float> m_blueColorEntry;

		internal static MelonPreferences_Entry<string> m_defaultSkinEntry;

		internal static MelonPreferences_Entry<string> m_selectedSkinEntry;

		public static bool ShowCrosshair { get; private set; } = true;

		public static float RedColor { get; private set; } = 255f;

		public static float GreenColor { get; private set; } = 255f;

		public static float BlueColor { get; private set; } = 255f;

		public static void CreateMelonPrefs()
		{
			m_crosshairsCategory = MelonPreferences.CreateCategory("Crosshairs");
			m_redColorEntry = m_crosshairsCategory.CreateEntry<float>("Red", 255f, (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
			m_greenColorEntry = m_crosshairsCategory.CreateEntry<float>("Green", 255f, (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
			m_blueColorEntry = m_crosshairsCategory.CreateEntry<float>("Blue", 255f, (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
			m_defaultSkinEntry = m_crosshairsCategory.CreateEntry<string>("Default Skin", "Default/Cross", (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
			m_selectedSkinEntry = m_crosshairsCategory.CreateEntry<string>("Selected Skin", "Default/Cross", (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
			RedColor = m_redColorEntry.Value;
			GreenColor = m_greenColorEntry.Value;
			BlueColor = m_blueColorEntry.Value;
			SkinManager.SetDefaultSkin(m_defaultSkinEntry.Value);
			SkinManager.SetSelectedSkin(m_selectedSkinEntry.Value);
		}

		public static void CreateMenu()
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: 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_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d6: 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_0122: Unknown result type (might be due to invalid IL or missing references)
			//IL_0152: Unknown result type (might be due to invalid IL or missing references)
			//IL_019a: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e2: Unknown result type (might be due to invalid IL or missing references)
			m_root = Page.Root.CreatePage("Not Enough Photons", Color.white, 0, true);
			m_menu = m_root.CreatePage("Crosshairs", Color.white, 0, true);
			m_skins = m_menu.CreatePage("Skins", Color.white, 0, true);
			foreach (string key in SkinManager.SkinTable.Keys)
			{
				CrosshairSkin skin = SkinManager.GetSkin(key);
				m_skins.CreateFunction(skin.Name, Color.white, (Action)delegate
				{
					SetSelectedSkin(skin.Name);
					SkinManager.SetSelectedSkin(skin.Name);
					CrosshairManager.SetSkin(SkinManager.SelectedSkin);
				}).Logo = skin.Texture;
			}
			m_colorMenu = m_menu.CreatePage("Colors", Color.white, 0, true);
			m_menu.CreateBool("Show Crosshairs", Color.white, true, (Action<bool>)SetShowCrosshair);
			m_colorMenu.CreateFunction("Paste From Clipboard", Color.white, (Action)PasteColor);
			m_redColor = m_colorMenu.CreateFloat("Red", Color.red, 255f, 5f, 0f, 255f, (Action<float>)SetRedColor);
			m_greenColor = m_colorMenu.CreateFloat("Green", Color.green, 255f, 5f, 0f, 255f, (Action<float>)SetGreenColor);
			m_blueColor = m_colorMenu.CreateFloat("Blue", Color.blue, 255f, 5f, 0f, 255f, (Action<float>)SetBlueColor);
		}

		public static Color GetHexColor(string input)
		{
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
			if (string.IsNullOrEmpty(input))
			{
				return Color.white;
			}
			if (input[0] == '#')
			{
				_ = Color.white;
				int i = 1;
				int j = 1;
				List<byte> list = new List<byte>();
				for (; i < input.Length; i++)
				{
					for (; j < i + 1; j += 2)
					{
						int num = j;
						int index = num + 1;
						string s = $"{input[num]}{input[index]}";
						list.Add(byte.Parse(s, NumberStyles.HexNumber));
					}
				}
				return new Color((float)(int)list[0], (float)(int)list[1], (float)(int)list[2], 255f);
			}
			return Color.white;
		}

		private static void PasteColor()
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: 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)
			Color hexColor = GetHexColor(GUIUtility.systemCopyBuffer);
			SetRedColor(hexColor.r);
			SetGreenColor(hexColor.g);
			SetBlueColor(hexColor.b);
		}

		private static void SetShowCrosshair(bool show)
		{
			ShowCrosshair = show;
			if (show)
			{
				CrosshairManager.ShowCrosshairs();
			}
			else
			{
				CrosshairManager.HideCrosshairs();
			}
		}

		private static void SetRedColor(float red)
		{
			RedColor = red;
			m_redColor.Value = RedColor;
			m_redColorEntry.Value = RedColor;
			m_crosshairsCategory.SaveToFile(false);
		}

		private static void SetGreenColor(float green)
		{
			GreenColor = green;
			m_greenColor.Value = GreenColor;
			m_greenColorEntry.Value = GreenColor;
			m_crosshairsCategory.SaveToFile(false);
		}

		private static void SetBlueColor(float blue)
		{
			BlueColor = blue;
			m_blueColor.Value = BlueColor;
			m_blueColorEntry.Value = BlueColor;
			m_crosshairsCategory.SaveToFile(false);
		}

		private static void SetSelectedSkin(string skin)
		{
			m_selectedSkinEntry.Value = skin;
			m_crosshairsCategory.SaveToFile(false);
		}
	}
}
namespace NEP.Crosshairs.Patches
{
	[HarmonyPatch(typeof(Gun))]
	[HarmonyPatch("OnTriggerGripAttached")]
	public static class OnGunAttached
	{
		public static void Postfix(Gun __instance)
		{
			CrosshairManager.OnGunAttached(__instance);
		}
	}
	[HarmonyPatch(typeof(Gun))]
	[HarmonyPatch("OnTriggerGripDetached")]
	public static class OnGunDetached
	{
		public static void Postfix(Gun __instance)
		{
			CrosshairManager.OnGunDetached(__instance);
		}
	}
}
namespace NEP.Crosshairs.Data
{
	public static class DataManager
	{
		internal static string m_modDirectory = Path.Combine(MelonEnvironment.UserDataDirectory, "Not Enough Photons/Crosshairs");

		public static AssetBundle AssetBundle { get; private set; }

		public static GameObject CrosshairObject { get; private set; }

		public static string ModDirectory => m_modDirectory;

		public static void Initialize()
		{
			if (!Directory.Exists(m_modDirectory))
			{
				Directory.CreateDirectory(m_modDirectory);
			}
			if (!HelperMethods.IsAndroid())
			{
				AssetBundle = HelperMethods.LoadEmbeddedAssetBundle(Assembly.GetExecutingAssembly(), "NEP.Crosshairs.Resources.crosshairs.pcvr");
			}
			else
			{
				AssetBundle = HelperMethods.LoadEmbeddedAssetBundle(Assembly.GetExecutingAssembly(), "NEP.Crosshairs.Resources.crosshairs.quest");
			}
		}

		public static void LoadObjectsFromBundle()
		{
			CrosshairObject = ((Il2CppObjectBase)AssetBundle.LoadAsset("Crosshair")).Cast<GameObject>();
			((Object)CrosshairObject).hideFlags = (HideFlags)32;
			((Renderer)((Component)CrosshairObject.transform.GetChild(0)).gameObject.GetComponent<MeshRenderer>()).material.SetTexture("_BaseMap", (Texture)(object)SkinManager.SkinTable[SkinManager.SelectedSkin.Name].Texture);
			Core.Logger.Msg("Crosshair object loaded successfully.");
		}
	}
}
namespace NEP.Crosshairs.Skins
{
	public sealed class CrosshairSkin
	{
		private string m_name;

		private Texture2D m_texture;

		private Color m_color;

		public string Name => m_name;

		public Texture2D Texture => m_texture;

		public Color Color => m_color;

		public CrosshairSkin(string name, Texture2D texture)
		{
			//IL_0015: 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)
			m_name = name;
			m_texture = texture;
			m_color = Color.white;
		}

		public CrosshairSkin(string name, Texture2D texture, Color color)
		{
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			m_name = name;
			m_texture = texture;
			m_color = color;
		}
	}
	public static class SkinManager
	{
		public static Dictionary<string, CrosshairSkin> SkinTable;

		private static CrosshairSkin m_defaultSkin;

		private static CrosshairSkin m_selectedSkin;

		internal static string[] m_skinModDirectories;

		public static CrosshairSkin DefaultSkin => m_defaultSkin;

		public static CrosshairSkin SelectedSkin => m_selectedSkin;

		public static void Initialize()
		{
			SkinTable = new Dictionary<string, CrosshairSkin>();
			m_skinModDirectories = Directory.GetDirectories(DataManager.ModDirectory);
			if (m_skinModDirectories.Length == 0)
			{
				Core.Logger.Warning("No skin folders found in the directory. You will see a white square as your crosshair.");
			}
			LoadSkins();
		}

		public static CrosshairSkin GetSkin(string path)
		{
			if (SkinTable.TryGetValue(path, out var value))
			{
				return value;
			}
			return null;
		}

		public static void SetDefaultSkin(string path)
		{
			m_defaultSkin = GetSkin(path);
		}

		public static void SetSelectedSkin(string path)
		{
			m_selectedSkin = GetSkin(path);
		}

		public static void LoadSkins()
		{
			string[] skinModDirectories = m_skinModDirectories;
			for (int i = 0; i < skinModDirectories.Length; i++)
			{
				string[] files = Directory.GetFiles(skinModDirectories[i]);
				foreach (string path in files)
				{
					DirectoryInfo directoryInfo = new DirectoryInfo(path);
					if (!(directoryInfo.Extension.ToLower() != ".png"))
					{
						string name = directoryInfo.Parent.Name + "/" + directoryInfo.Name;
						CrosshairSkin crosshairSkin = CreateSkin(path, name);
						if (!SkinTable.TryAdd(crosshairSkin.Name, crosshairSkin))
						{
							throw new Exception("Skin " + directoryInfo.Name + " already exists with the same name!");
						}
					}
				}
			}
			m_defaultSkin = GetSkin("Default/Cross - Default");
		}

		public static CrosshairSkin CreateSkin(string path, string name)
		{
			if (!File.Exists(path))
			{
				throw new Exception("File does not exist! " + path);
			}
			Texture2D texture = LoadTexture(path, name);
			return new CrosshairSkin(name.Split(".png")[0], texture);
		}

		private static Texture2D LoadTexture(string skinPath, string name)
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Expected O, but got Unknown
			if (!File.Exists(skinPath))
			{
				return Texture2D.whiteTexture;
			}
			Texture2D val = new Texture2D(2, 2);
			byte[] array = File.ReadAllBytes(skinPath);
			if (!ImageConversion.LoadImage(val, Il2CppStructArray<byte>.op_Implicit(array)))
			{
				return Texture2D.whiteTexture;
			}
			((Object)val).hideFlags = (HideFlags)32;
			return val;
		}
	}
}