Decompiled source of CustomCursors v1.0.1

CustomCursors.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using Atomicrops.Core.CameraSystem;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
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.0", FrameworkDisplayName = ".NET Standard 2.0")]
[assembly: AssemblyCompany("CustomCursors")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("My first plugin")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("CustomCursors")]
[assembly: AssemblyTitle("CustomCursors")]
[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;
		}
	}
}
namespace Template
{
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "CustomCursors";

		public const string PLUGIN_NAME = "CustomCursors";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace CustomCursors
{
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "pauli.plugin.CustomCursors";

		public const string PLUGIN_NAME = "CustomCursors";

		public const string PLUGIN_VERSION = "1.0.0";
	}
	[BepInPlugin("pauli.plugin.CustomCursors", "CustomCursors", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public static ManualLogSource Log;

		private void Awake()
		{
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin pauli.plugin.CustomCursors is loaded!");
			Harmony val = new Harmony("pauli.plugin.CustomCursors");
			val.PatchAll();
		}
	}
	[HarmonyPatch(typeof(PixelCursor), "Awake")]
	public class PixelCursorAwakePatch
	{
		public static List<Sprite> cursorSprites = new List<Sprite>();

		public static List<Texture2D> handTextures = new List<Texture2D>();

		public static int? currentIndex = null;

		public static string ModDirectory
		{
			get
			{
				string location = Assembly.GetExecutingAssembly().Location;
				return Path.GetDirectoryName(location);
			}
		}

		private static void Postfix(PixelCursor __instance)
		{
			//IL_02d8: Unknown result type (might be due to invalid IL or missing references)
			if (cursorSprites.Count == 0 || handTextures.Count == 0)
			{
				cursorSprites.Add(LoadSpriteFromFile("DefaultCursor.png"));
				cursorSprites.Add(LoadSpriteFromFile("BlankCursor.png"));
				handTextures.Add(LoadTexture2DFromFile("DefaultHand.png"));
				handTextures.Add(LoadTexture2DFromFile("BlankHand.png"));
				int num = 1;
				while (true)
				{
					try
					{
						string fileName = Path.Combine(ModDirectory, $"Hand{num}.png");
						string fileName2 = Path.Combine(ModDirectory, $"Cursor{num}.png");
						Sprite val = LoadSpriteFromFile(fileName2);
						Texture2D val2 = LoadTexture2DFromFile(fileName);
						if ((Object)(object)val == (Object)null || (Object)(object)val2 == (Object)null)
						{
							break;
						}
						cursorSprites.Add(val);
						handTextures.Add(val2);
						goto IL_0109;
					}
					catch (FileNotFoundException)
					{
					}
					break;
					IL_0109:
					num++;
				}
				int num2 = Math.Min(cursorSprites.Count, handTextures.Count);
				if (cursorSprites.Count != num2)
				{
					cursorSprites = cursorSprites.GetRange(0, num2);
				}
				if (handTextures.Count != num2)
				{
					handTextures = handTextures.GetRange(0, num2);
				}
			}
			if (!currentIndex.HasValue)
			{
				string path = Path.Combine(ModDirectory, "CursorSaveData.txt");
				try
				{
					if (File.Exists(path))
					{
						string s = File.ReadAllText(path);
						if (int.TryParse(s, out var result) && result >= 0 && result < cursorSprites.Count && result < handTextures.Count)
						{
							currentIndex = result;
						}
						else
						{
							currentIndex = 0;
							File.WriteAllText(path, "0");
						}
					}
					else
					{
						currentIndex = 0;
						File.WriteAllText(path, "0");
					}
				}
				catch (Exception ex2)
				{
					Debug.LogError((object)("Error reading or writing CursorSaveData.txt: " + ex2.Message));
					currentIndex = 0;
				}
			}
			if (currentIndex.HasValue && currentIndex.Value >= 0 && currentIndex.Value < handTextures.Count && currentIndex.Value < cursorSprites.Count)
			{
				Cursor.SetCursor(handTextures[currentIndex.Value], new Vector2(8f, 8f), (CursorMode)0);
				SpriteRenderer component = ((Component)PixelCursor.I.CursorTrans).gameObject.GetComponent<SpriteRenderer>();
				if ((Object)(object)component != (Object)null)
				{
					component.sprite = cursorSprites[currentIndex.Value];
				}
				else
				{
					Debug.LogError((object)"SpriteRenderer component not found on the CursorTrans GameObject.");
				}
			}
			else
			{
				Debug.LogError((object)$"Invalid currentIndex: {currentIndex}");
			}
		}

		public static Sprite LoadSpriteFromFile(string fileName)
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Expected O, but got Unknown
			//IL_0068: 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)
			string text = Path.Combine(ModDirectory, fileName);
			byte[] array = File.ReadAllBytes(text);
			Texture2D val = new Texture2D(2, 2);
			((Texture)val).filterMode = (FilterMode)0;
			if (ImageConversion.LoadImage(val, array))
			{
				Rect val2 = default(Rect);
				((Rect)(ref val2))..ctor(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height);
				Vector2 val3 = default(Vector2);
				((Vector2)(ref val3))..ctor(0.5f, 0.5f);
				float num = 32f;
				return Sprite.Create(val, val2, val3, num);
			}
			Debug.LogError((object)("Failed to load image from " + text + "."));
			return null;
		}

		public static Texture2D LoadTexture2DFromFile(string fileName)
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Expected O, but got Unknown
			string path = Path.Combine(ModDirectory, fileName);
			byte[] array = File.ReadAllBytes(path);
			Texture2D val = new Texture2D(2, 2);
			((Texture)val).filterMode = (FilterMode)0;
			if (ImageConversion.LoadImage(val, array))
			{
				return val;
			}
			return null;
		}
	}
	[HarmonyPatch(typeof(OptionsMenuVideo), "Awake")]
	public class OptionsMenuVideoAwakePatch
	{
		private static UiEleHorizontalSelect horizontalSelectComponent;

		private static void Postfix(OptionsMenuVideo __instance)
		{
			GameObject[] array = Object.FindObjectsOfType<GameObject>();
			GameObject val = null;
			GameObject[] array2 = array;
			foreach (GameObject val2 in array2)
			{
				if (((Object)val2).name == "Resolution")
				{
					val = val2;
					break;
				}
			}
			if ((Object)(object)val != (Object)null)
			{
				Plugin.Log.LogInfo((object)"Resolution GameObject found!");
				GameObject val3 = Object.Instantiate<GameObject>(val);
				val3.transform.SetParent(val.transform.parent, false);
				horizontalSelectComponent = val3.GetComponent<UiEleHorizontalSelect>();
				if ((Object)(object)horizontalSelectComponent != (Object)null)
				{
					List<string> list = new List<string> { "Default Cursor", "No Cursor" };
					for (int j = 2; j < PixelCursorAwakePatch.cursorSprites.Count && j < PixelCursorAwakePatch.handTextures.Count; j++)
					{
						list.Add($"Cursor Set {j - 1}");
					}
					horizontalSelectComponent.SetOptions(list);
					horizontalSelectComponent.SetIdx(PixelCursorAwakePatch.currentIndex.HasValue ? PixelCursorAwakePatch.currentIndex.Value : 0);
					horizontalSelectComponent.OnDeselectCallback += valueChanged;
				}
				else
				{
					Plugin.Log.LogError((object)"UiEleHorizontalSelect component not found on the copied GameObject.");
				}
			}
			else
			{
				Plugin.Log.LogError((object)"Could not find the Resolution GameObject. Check the path.");
			}
		}

		private static void valueChanged(object sender, EventArgs e)
		{
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			int idx = horizontalSelectComponent.GetIdx();
			if (idx >= 0 && idx < PixelCursorAwakePatch.cursorSprites.Count && idx < PixelCursorAwakePatch.handTextures.Count)
			{
				Cursor.SetCursor(PixelCursorAwakePatch.handTextures[idx], new Vector2(8f, 8f), (CursorMode)0);
				SpriteRenderer component = ((Component)PixelCursor.I.CursorTrans).gameObject.GetComponent<SpriteRenderer>();
				if ((Object)(object)component != (Object)null)
				{
					component.sprite = PixelCursorAwakePatch.cursorSprites[idx];
				}
				else
				{
					Debug.LogError((object)"SpriteRenderer component not found on the CursorTrans GameObject.");
				}
				PixelCursorAwakePatch.currentIndex = idx;
				string path = Path.Combine(PixelCursorAwakePatch.ModDirectory, "CursorSaveData.txt");
				try
				{
					File.WriteAllText(path, idx.ToString());
					return;
				}
				catch (Exception ex)
				{
					Debug.LogError((object)("Error writing to CursorSaveData.txt: " + ex.Message));
					return;
				}
			}
			Debug.LogError((object)"Invalid cursor index selected.");
		}
	}
}