Decompiled source of Cursor Swapper v1.1.4

CursorSwapper.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("CursorSwapper")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Azumatt")]
[assembly: AssemblyProduct("CursorSwapper")]
[assembly: AssemblyCopyright("Copyright ©  2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("4358610B-F3F4-4843-B7AF-98B7BC60DCDE")]
[assembly: AssemblyFileVersion("1.1.4")]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.4.0")]
[module: UnverifiableCode]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
}
namespace CursorSwapper
{
	[BepInPlugin("Azumatt.CursorSwapper", "CursorSwapper", "1.1.4")]
	public class CursorSwapperPlugin : BaseUnityPlugin
	{
		private enum Toggle
		{
			On = 1,
			Off = 0
		}

		internal const string ModName = "CursorSwapper";

		internal const string ModVersion = "1.1.4";

		internal const string Author = "Azumatt";

		private const string ModGuid = "Azumatt.CursorSwapper";

		private const string ConfigFileName = "Azumatt.CursorSwapper.cfg";

		private static readonly string ConfigFileFullPath;

		internal static string ConnectionError;

		private readonly Harmony _harmony = new Harmony("Azumatt.CursorSwapper");

		private static readonly ManualLogSource CursorSwapperLogger;

		private static Texture2D _cursorSprite;

		private static Texture2D _vanillaCursorSprite;

		private static ConfigEntry<Toggle> _useCustomCursor;

		public void Awake()
		{
			_useCustomCursor = ((BaseUnityPlugin)this).Config.Bind<Toggle>("1 - General", "Use Custom Cursor", Toggle.On, "If set to on, the mod will attempt to search for a cursor.png file located in the plugins folder.\nIf it's not found, a warning will be presented in the console and the default game cursor will be used.");
			_vanillaCursorSprite = Resources.FindObjectsOfTypeAll<Texture2D>().First((Texture2D s) => ((Object)s).name == "cursor" && ((Texture)s).isReadable);
			if (_useCustomCursor.Value == Toggle.On)
			{
				ApplyCursor();
			}
			_useCustomCursor.SettingChanged += delegate
			{
				//IL_0011: Unknown result type (might be due to invalid IL or missing references)
				if (_useCustomCursor.Value == Toggle.Off)
				{
					Cursor.SetCursor(_vanillaCursorSprite, Vector2.zero, (CursorMode)0);
				}
				else
				{
					ApplyCursor();
				}
			};
			Assembly executingAssembly = Assembly.GetExecutingAssembly();
			_harmony.PatchAll(executingAssembly);
			SetupWatcher();
		}

		private void OnDestroy()
		{
			((BaseUnityPlugin)this).Config.Save();
		}

		private void SetupWatcher()
		{
			FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(Paths.ConfigPath, "Azumatt.CursorSwapper.cfg");
			fileSystemWatcher.Changed += ReadConfigValues;
			fileSystemWatcher.Created += ReadConfigValues;
			fileSystemWatcher.Renamed += ReadConfigValues;
			fileSystemWatcher.IncludeSubdirectories = true;
			fileSystemWatcher.SynchronizingObject = ThreadingHelper.SynchronizingObject;
			fileSystemWatcher.EnableRaisingEvents = true;
			FileSystemWatcher fileSystemWatcher2 = new FileSystemWatcher(Paths.PluginPath);
			fileSystemWatcher2.Changed += UpdateCursor;
			fileSystemWatcher2.Created += UpdateCursor;
			fileSystemWatcher2.Deleted += UpdateCursor;
			fileSystemWatcher2.Renamed += UpdateCursor;
			fileSystemWatcher2.Error += OnError;
			fileSystemWatcher2.IncludeSubdirectories = true;
			fileSystemWatcher2.SynchronizingObject = ThreadingHelper.SynchronizingObject;
			fileSystemWatcher2.EnableRaisingEvents = true;
		}

		private void ReadConfigValues(object sender, FileSystemEventArgs e)
		{
			if (!File.Exists(ConfigFileFullPath))
			{
				return;
			}
			try
			{
				CursorSwapperLogger.LogDebug((object)"ReadConfigValues called");
				((BaseUnityPlugin)this).Config.Reload();
			}
			catch
			{
				CursorSwapperLogger.LogError((object)"There was an issue loading your Azumatt.CursorSwapper.cfg");
				CursorSwapperLogger.LogError((object)"Please check your config entries for spelling and format!");
			}
		}

		private static void UpdateCursor(object sender, FileSystemEventArgs e)
		{
			if (_useCustomCursor.Value == Toggle.On)
			{
				ApplyCursor();
			}
		}

		private static void OnError(object sender, ErrorEventArgs e)
		{
			PrintException(e.GetException());
		}

		private static void PrintException(Exception? ex)
		{
			while (ex != null)
			{
				CursorSwapperLogger.LogError((object)("Message: " + ex.Message));
				CursorSwapperLogger.LogError((object)"Stacktrace:");
				CursorSwapperLogger.LogError((object)ex.StackTrace);
				ex = ex.InnerException;
			}
		}

		internal static void ApplyCursor()
		{
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			_cursorSprite = LoadTexture("cursor.png");
			if ((Object)(object)_cursorSprite != (Object)null)
			{
				Cursor.SetCursor(_cursorSprite, new Vector2(6f, 5f), (CursorMode)0);
			}
			else if ((Object)(object)_vanillaCursorSprite != (Object)null)
			{
				Cursor.SetCursor(_vanillaCursorSprite, Vector2.zero, (CursorMode)0);
			}
		}

		private static Texture2D LoadTexture(string name)
		{
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Expected O, but got Unknown
			string name2 = name;
			Texture2D val = new Texture2D(0, 0);
			string directoryName = Path.GetDirectoryName(Paths.PluginPath);
			if (directoryName == null)
			{
				return val;
			}
			List<string> list = Directory.GetFiles(directoryName, "cursor.png", SearchOption.AllDirectories).OrderBy(Path.GetFileName).ToList();
			try
			{
				byte[] array = File.ReadAllBytes(list.Find((string x) => x.Contains(name2)));
				ImageConversion.LoadImage(val, array);
			}
			catch
			{
				CursorSwapperLogger.LogWarning((object)("The file " + name2 + " couldn't be found in the directory path. Please make sure you are naming your files correctly and they are location somewhere in the BepInEx/plugins folder.\nOptionally, you can turn off 'Use Custom Cursor' inside of your configuration file. If you no longer wish to see this error."));
				val = _vanillaCursorSprite;
			}
			return val;
		}

		static CursorSwapperPlugin()
		{
			string configPath = Paths.ConfigPath;
			char directorySeparatorChar = Path.DirectorySeparatorChar;
			ConfigFileFullPath = configPath + directorySeparatorChar + "Azumatt.CursorSwapper.cfg";
			ConnectionError = "";
			CursorSwapperLogger = Logger.CreateLogSource("CursorSwapper");
			_cursorSprite = null;
			_vanillaCursorSprite = null;
			_useCustomCursor = null;
		}
	}
	[HarmonyPatch(typeof(ZNetScene), "Awake")]
	internal static class SwapAfterJewelcraftingBullshit
	{
		[HarmonyAfter(new string[] { "org.bepinex.plugins.jewelcrafting" })]
		private static void Postfix(ZNetScene __instance)
		{
			CursorSwapperPlugin.ApplyCursor();
		}
	}
}