Decompiled source of SunkenCompass v1.0.2

SunkenCompass.dll

Decompiled 7 months ago
using System;
using System.Diagnostics;
using System.IO;
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 JetBrains.Annotations;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("SunkenCompass")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Azumatt")]
[assembly: AssemblyProduct("SunkenCompass")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("3DC357FF-84FD-4BB0-89C7-FA6059583C24")]
[assembly: AssemblyFileVersion("1.0.2")]
[assembly: TargetFramework(".NETFramework,Version=v4.8.1", FrameworkDisplayName = ".NET Framework 4.8.1")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.2.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.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;
		}
	}
	[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 SunkenCompass
{
	[BepInPlugin("Azumatt.SunkenCompass", "SunkenCompass", "1.0.2")]
	public class SunkenCompassPlugin : BaseUnityPlugin
	{
		public enum Toggle
		{
			Off,
			On
		}

		private class ConfigurationManagerAttributes
		{
			[UsedImplicitly]
			public int? Order;

			[UsedImplicitly]
			public bool? Browsable;

			[UsedImplicitly]
			public string? Category;

			[UsedImplicitly]
			public Action<ConfigEntryBase>? CustomDrawer;
		}

		internal const string ModName = "SunkenCompass";

		internal const string ModVersion = "1.0.2";

		internal const string Author = "Azumatt";

		private const string ModGuid = "Azumatt.SunkenCompass";

		private static string ConfigFileName = "Azumatt.SunkenCompass.cfg";

		private static string ConfigFileFullPath;

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

		public static readonly ManualLogSource SunkenCompassLogger;

		internal static Sprite CompassSprite;

		internal static Sprite CompassCenter;

		internal static Sprite CompassMask;

		internal static GameObject ObjectCompass;

		internal static GameObject ObjectParent;

		internal static GameObject ObjectCenterMark;

		internal static bool GotCompassImage;

		internal static bool GotCompassMask;

		internal static bool GotCompassCenter;

		internal static ConfigEntry<Toggle> ConfigEnabled;

		internal static ConfigEntry<Color> ColorCompass;

		internal static ConfigEntry<Color> ColorCenterMark;

		internal static ConfigEntry<Toggle> CompassUsePlayerDirection;

		internal static ConfigEntry<int> CompassYOffset;

		internal static ConfigEntry<float> CompassScale;

		internal static ConfigEntry<Toggle> CompassShowCenterMark;

		private void Awake()
		{
			//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
			ConfigEnabled = ((BaseUnityPlugin)this).Config.Bind<Toggle>("1 - Sunken Compass", "Enabled", Toggle.On, "Enable or disable the Sunken Compass.");
			CompassUsePlayerDirection = ((BaseUnityPlugin)this).Config.Bind<Toggle>("2 - Compass Display", "Use Player Direction", Toggle.Off, "Orient the compass based on the direction the player is facing, rather than the middle of the screen.");
			CompassScale = ((BaseUnityPlugin)this).Config.Bind<float>("2 - Compass Display", "Scale (Compass)", 0.75f, "Enlarge or shrink the scale of the compass.");
			CompassYOffset = ((BaseUnityPlugin)this).Config.Bind<int>("2 - Compass Display", "Offset (Y)", 0, "Offset from the top of the screen in pixels.");
			CompassShowCenterMark = ((BaseUnityPlugin)this).Config.Bind<Toggle>("2 - Compass Display", "Show Center Mark", Toggle.Off, "Show center mark graphic.");
			ColorCompass = ((BaseUnityPlugin)this).Config.Bind<Color>("3 - Color Adjustment", "Color (Compass)", Color.white, "Adjust the color of the compass.");
			ColorCenterMark = ((BaseUnityPlugin)this).Config.Bind<Color>("3 - Color Adjustment", "Color (Center Mark)", Color.yellow, "Adjust the color of the center mark graphic.");
			if (ConfigEnabled.Value == Toggle.On)
			{
				CompassSprite = Utilities.LoadSprite("compass.png");
				CompassCenter = Utilities.LoadSprite("center.png");
				CompassMask = Utilities.LoadSprite("mask.png");
				GotCompassImage = (Object)(object)CompassSprite != (Object)null;
				GotCompassMask = (Object)(object)CompassMask != (Object)null;
				GotCompassCenter = (Object)(object)CompassCenter != (Object)null;
				_harmony.PatchAll();
				SetupWatcher();
			}
			else
			{
				SunkenCompassLogger.LogInfo((object)"SunkenCompass v[1.0.2] not enabled in configuration.");
			}
		}

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

		private void SetupWatcher()
		{
			FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(Paths.ConfigPath, ConfigFileName);
			fileSystemWatcher.Changed += ReadConfigValues;
			fileSystemWatcher.Created += ReadConfigValues;
			fileSystemWatcher.Renamed += ReadConfigValues;
			fileSystemWatcher.IncludeSubdirectories = true;
			fileSystemWatcher.SynchronizingObject = ThreadingHelper.SynchronizingObject;
			fileSystemWatcher.EnableRaisingEvents = true;
		}

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

		static SunkenCompassPlugin()
		{
			string configPath = Paths.ConfigPath;
			char directorySeparatorChar = Path.DirectorySeparatorChar;
			ConfigFileFullPath = configPath + directorySeparatorChar + ConfigFileName;
			SunkenCompassLogger = Logger.CreateLogSource("SunkenCompass");
			CompassSprite = null;
			CompassCenter = null;
			CompassMask = null;
			ObjectCompass = null;
			ObjectParent = null;
			ObjectCenterMark = null;
			ConfigEnabled = null;
			ColorCompass = null;
			ColorCenterMark = null;
			CompassUsePlayerDirection = null;
			CompassYOffset = null;
			CompassScale = null;
			CompassShowCenterMark = null;
		}
	}
	[HarmonyPatch(typeof(UICombat), "Start")]
	internal static class UICombatStartPatch
	{
		private static void Prefix(UICombat __instance)
		{
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_0147: Unknown result type (might be due to invalid IL or missing references)
			//IL_0151: Expected O, but got Unknown
			//IL_017a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0181: Expected O, but got Unknown
			//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_022f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0239: Expected O, but got Unknown
			//IL_0260: Unknown result type (might be due to invalid IL or missing references)
			//IL_026b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0295: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_0204: Unknown result type (might be due to invalid IL or missing references)
			//IL_02db: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e5: Expected O, but got Unknown
			//IL_030c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0317: Unknown result type (might be due to invalid IL or missing references)
			//IL_0341: Unknown result type (might be due to invalid IL or missing references)
			//IL_0137: Unknown result type (might be due to invalid IL or missing references)
			//IL_013c: Unknown result type (might be due to invalid IL or missing references)
			if (((Texture)SunkenCompassPlugin.CompassSprite.texture).width < 1)
			{
				SunkenCompassPlugin.SunkenCompassLogger.LogDebug((object)"Image for compass was invalid or zero pixels in width.");
				return;
			}
			float num = (float)((Texture)SunkenCompassPlugin.CompassSprite.texture).width / 2f;
			Sprite sprite = Sprite.Create(SunkenCompassPlugin.CompassSprite.texture, new Rect(0f, 0f, (float)((Texture)SunkenCompassPlugin.CompassSprite.texture).width, (float)((Texture)SunkenCompassPlugin.CompassSprite.texture).height), Vector2.zero);
			Sprite sprite2 = null;
			if (SunkenCompassPlugin.GotCompassMask && ((Texture)SunkenCompassPlugin.CompassMask.texture).width > 0)
			{
				sprite2 = Sprite.Create(SunkenCompassPlugin.CompassMask.texture, new Rect(0f, 0f, num, (float)((Texture)SunkenCompassPlugin.CompassMask.texture).height), Vector2.zero);
			}
			Sprite sprite3 = null;
			if (SunkenCompassPlugin.CompassShowCenterMark.Value == SunkenCompassPlugin.Toggle.On && SunkenCompassPlugin.GotCompassImage && SunkenCompassPlugin.GotCompassMask && SunkenCompassPlugin.GotCompassCenter && ((Texture)SunkenCompassPlugin.CompassCenter.texture).width > 0)
			{
				sprite3 = Sprite.Create(SunkenCompassPlugin.CompassCenter.texture, new Rect(0f, 0f, (float)((Texture)SunkenCompassPlugin.CompassCenter.texture).width, (float)((Texture)SunkenCompassPlugin.CompassCenter.texture).height), Vector2.zero);
			}
			SunkenCompassPlugin.ObjectParent = new GameObject();
			((Object)SunkenCompassPlugin.ObjectParent).name = "Compass";
			((Transform)SunkenCompassPlugin.ObjectParent.AddComponent<RectTransform>()).SetParent(__instance.CombatRoot.transform);
			GameObject val = new GameObject();
			if ((Object)(object)SunkenCompassPlugin.CompassMask.texture != (Object)null && ((Texture)SunkenCompassPlugin.CompassMask.texture).width > 0)
			{
				((Object)val).name = "Mask";
				RectTransform obj = val.AddComponent<RectTransform>();
				((Transform)obj).SetParent(SunkenCompassPlugin.ObjectParent.transform);
				obj.sizeDelta = new Vector2(num, (float)((Texture)SunkenCompassPlugin.CompassSprite.texture).height);
				((Transform)obj).localScale = Vector3.one * SunkenCompassPlugin.CompassScale.Value;
				obj.anchoredPosition = Vector2.zero;
				Image obj2 = val.AddComponent<Image>();
				obj2.sprite = sprite2;
				obj2.preserveAspect = true;
				val.AddComponent<Mask>().showMaskGraphic = false;
			}
			SunkenCompassPlugin.ObjectCompass = new GameObject();
			((Object)SunkenCompassPlugin.ObjectCompass).name = "Image";
			RectTransform obj3 = SunkenCompassPlugin.ObjectCompass.AddComponent<RectTransform>();
			((Transform)obj3).SetParent(val.transform);
			((Transform)obj3).localScale = Vector3.one;
			obj3.anchoredPosition = Vector2.zero;
			obj3.sizeDelta = new Vector2((float)((Texture)SunkenCompassPlugin.CompassSprite.texture).width, (float)((Texture)SunkenCompassPlugin.CompassSprite.texture).height);
			Image obj4 = SunkenCompassPlugin.ObjectCompass.AddComponent<Image>();
			obj4.sprite = sprite;
			obj4.preserveAspect = true;
			if (SunkenCompassPlugin.CompassShowCenterMark.Value == SunkenCompassPlugin.Toggle.On && ((Texture)SunkenCompassPlugin.CompassCenter.texture).width > 0)
			{
				SunkenCompassPlugin.ObjectCenterMark = new GameObject();
				((Object)SunkenCompassPlugin.ObjectCenterMark).name = "CenterMark";
				RectTransform obj5 = SunkenCompassPlugin.ObjectCenterMark.AddComponent<RectTransform>();
				((Transform)obj5).SetParent(val.transform);
				((Transform)obj5).localScale = Vector3.one;
				obj5.anchoredPosition = Vector2.zero;
				obj5.sizeDelta = new Vector2((float)((Texture)SunkenCompassPlugin.CompassCenter.texture).width, (float)((Texture)SunkenCompassPlugin.CompassCenter.texture).height);
				Image obj6 = SunkenCompassPlugin.ObjectCenterMark.AddComponent<Image>();
				obj6.sprite = sprite3;
				obj6.preserveAspect = true;
			}
			SunkenCompassPlugin.SunkenCompassLogger.LogDebug((object)"Finished attempting to add compass to game hud.");
		}
	}
	[HarmonyPatch(typeof(UICombat), "Update")]
	internal static class UICombatUpdatePatch
	{
		private static void Prefix(UICombat __instance)
		{
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_007c: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00de: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0100: Unknown result type (might be due to invalid IL or missing references)
			//IL_011c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0121: Unknown result type (might be due to invalid IL or missing references)
			//IL_013a: Unknown result type (might be due to invalid IL or missing references)
			//IL_014e: 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_01b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_0206: Unknown result type (might be due to invalid IL or missing references)
			if (Object.op_Implicit((Object)(object)WorldScene.code) && Object.op_Implicit((Object)(object)Global.code.Player) && SunkenCompassPlugin.ConfigEnabled.Value != 0 && Object.op_Implicit((Object)(object)Global.code.Player) && SunkenCompassPlugin.GotCompassImage && SunkenCompassPlugin.GotCompassMask)
			{
				SunkenCompassPlugin.ObjectParent.SetActive(!Global.code.OnGUI);
				float num = ((SunkenCompassPlugin.CompassUsePlayerDirection.Value == SunkenCompassPlugin.Toggle.Off) ? ((Component)FPSPlayer.code.CameraControlComponent).transform.eulerAngles.y : ((Component)Global.code.Player).transform.eulerAngles.y);
				if ((double)num > 180.0)
				{
					num -= 360f;
				}
				float num2 = num * (-(float)Math.PI / 180f);
				Rect rect = SunkenCompassPlugin.ObjectCompass.GetComponent<Image>().sprite.rect;
				((Transform)SunkenCompassPlugin.ObjectCompass.GetComponent<RectTransform>()).localPosition = Vector3.right * (((Rect)(ref rect)).width / 2f) * num2 / 6.283185f - new Vector3(((Rect)(ref rect)).width * 0.125f, 0f, 0f);
				((Graphic)SunkenCompassPlugin.ObjectCompass.GetComponent<Image>()).color = SunkenCompassPlugin.ColorCompass.Value;
				((Transform)SunkenCompassPlugin.ObjectParent.GetComponent<RectTransform>()).localScale = Vector3.one * SunkenCompassPlugin.CompassScale.Value;
				SunkenCompassPlugin.ObjectParent.GetComponent<RectTransform>().anchoredPosition = new Vector2(0f, (float)(((double)Screen.height / 1.0 - (double)((Texture)SunkenCompassPlugin.ObjectCompass.GetComponent<Image>().sprite.texture).height * (double)SunkenCompassPlugin.CompassScale.Value) / 2.0)) - Vector2.up * (float)SunkenCompassPlugin.CompassYOffset.Value;
				if (SunkenCompassPlugin.CompassShowCenterMark.Value == SunkenCompassPlugin.Toggle.On && !((Object)(object)SunkenCompassPlugin.ObjectCenterMark == (Object)null))
				{
					((Graphic)SunkenCompassPlugin.ObjectCenterMark.GetComponent<Image>()).color = SunkenCompassPlugin.ColorCenterMark.Value;
					SunkenCompassPlugin.ObjectCenterMark.SetActive(true);
				}
			}
		}
	}
	public class Utilities
	{
		private static byte[] ReadEmbeddedFileBytes(string name)
		{
			using MemoryStream memoryStream = new MemoryStream();
			Assembly.GetExecutingAssembly().GetManifestResourceStream(Assembly.GetExecutingAssembly().GetName().Name + "." + name).CopyTo(memoryStream);
			return memoryStream.ToArray();
		}

		internal static Texture2D LoadTexture(string name)
		{
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Expected O, but got Unknown
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Expected O, but got Unknown
			Texture2D val = null;
			val = ((!(name == "mask.png") && !(name == "compass.png")) ? new Texture2D(0, 0, (TextureFormat)4, true, true) : new Texture2D(2, 2, (TextureFormat)4, true, true));
			ImageConversion.LoadImage(val, ReadEmbeddedFileBytes("images." + name));
			return val;
		}

		internal static Sprite LoadSprite(string name)
		{
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			Texture2D val = LoadTexture(name);
			if ((Object)(object)val != (Object)null)
			{
				return Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), Vector2.zero);
			}
			return null;
		}
	}
}