Decompiled source of ChronoCompassGPS v1.1.6

ChronoCompassGPS.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
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: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("0.0.0.0")]
[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 ChronoCompassGPS
{
	[BepInPlugin("cstei.chronocompassgps", "ChronoCompassGPS", "1.1.6")]
	public class ChronoCompassGPSPlugin : BaseUnityPlugin
	{
		internal static ManualLogSource Log;

		internal static ConfigEntry<bool> EnableGPS;

		internal static ConfigEntry<float> ViewDistance;

		internal static ConfigEntry<bool> ShowDecimal;

		internal static ConfigEntry<bool> ShowHeadingSpeed;

		internal static ConfigEntry<bool> ShowLocalTime;

		internal static ConfigEntry<bool> ShowGlobalTime;

		private static readonly string[] CompassDirections = new string[16]
		{
			"N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW",
			"SW", "WSW", "W", "WNW", "NW", "NNW"
		};

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			EnableGPS = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enable GPS", true, "Show lat/lon coordinates above chronocompasses");
			ViewDistance = ((BaseUnityPlugin)this).Config.Bind<float>("General", "View Distance", 3f, "Max distance from player to show text.");
			ShowHeadingSpeed = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Show Heading/Speed", true, "Show boat heading (degrees + compass direction) and speed in knots below the coordinates");
			ShowLocalTime = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Show Local Time", false, "Show the local solar time below the coordinates");
			ShowGlobalTime = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Show Global Time", false, "Show the global clock time below the coordinates");
			ShowDecimal = ((BaseUnityPlugin)this).Config.Bind<bool>("Format", "Decimal Degrees", true, "Show coordinates as decimal degrees (e.g. 31.52°) instead of deg/min (e.g. 31°30')");
			Harmony.CreateAndPatchAll(typeof(ChronoCompassPatches), "cstei.chronocompassgps");
			Log.LogInfo((object)string.Format("ChronoCompassGPS v{0} loaded", "1.1.6"));
		}

		private void Update()
		{
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Expected O, but got Unknown
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Expected O, but got Unknown
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: Expected O, but got Unknown
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			KeyValuePair<ShipItemCompass, TextMesh>[] array = ChronoCompassPatches.TextMeshes.ToArray();
			for (int i = 0; i < array.Length; i++)
			{
				KeyValuePair<ShipItemCompass, TextMesh> keyValuePair = array[i];
				ShipItemCompass key = keyValuePair.Key;
				TextMesh value = keyValuePair.Value;
				if ((Object)key == (Object)null || (Object)value == (Object)null || !((Component)value).gameObject.activeInHierarchy || (Object)((PickupableItem)key).held != (Object)null)
				{
					continue;
				}
				try
				{
					if (Vector3.Distance(((Component)Refs.observerMirror).transform.position, ((Component)key).transform.position) > ViewDistance.Value)
					{
						((Component)value).gameObject.SetActive(false);
					}
				}
				catch
				{
				}
			}
		}

		internal static string FormatDMS(float lat, float lon)
		{
			string text = ((lat >= 0f) ? "N" : "S");
			string text2 = ((lon >= 0f) ? "E" : "W");
			lat = Math.Abs(lat);
			lon = Math.Abs(lon);
			int num = (int)lat;
			int num2 = (int)((lat - (float)num) * 60f);
			int num3 = (int)lon;
			int num4 = (int)((lon - (float)num3) * 60f);
			return $"Lat: {num}°{num2}'{text} / Lon: {num3}°{num4}'{text2}";
		}

		internal static string CompassDirection(float heading)
		{
			int num = (int)Math.Round((double)heading / 22.5) % 16;
			return CompassDirections[num];
		}

		internal static string FormatTime(float time)
		{
			time = Mathf.Repeat(time, 24f);
			int num = Mathf.FloorToInt(time);
			int num2 = Mathf.FloorToInt((time - (float)num) * 60f);
			return $"{num:00}:{num2:00}";
		}
	}
	internal static class PluginInfo
	{
		public const string GUID = "cstei.chronocompassgps";

		public const string NAME = "ChronoCompassGPS";

		public const string VERSION = "1.1.6";
	}
	internal class ChronoCompassPatches
	{
		internal static Dictionary<ShipItemCompass, TextMesh> TextMeshes = new Dictionary<ShipItemCompass, TextMesh>();

		private static Dictionary<ShipItemCompass, Rigidbody> BoatBodies = new Dictionary<ShipItemCompass, Rigidbody>();

		[HarmonyPatch(typeof(ShipItemCompass), "OnLoad")]
		[HarmonyPrefix]
		public static void AddTextMesh(ShipItemCompass __instance)
		{
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_0082: 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_00ac: Unknown result type (might be due to invalid IL or missing references)
			if (!(((ShipItem)__instance).name != "chronocompass"))
			{
				Transform obj = Object.Instantiate<Transform>(((Component)DayLogs.instance).transform.parent.GetChild(0).GetChild(1));
				((Object)obj).name = "chronogps_text";
				obj.SetParent(((Component)__instance).transform);
				((Component)obj).gameObject.layer = 0;
				obj.localEulerAngles = new Vector3(0f, 0f, 0f);
				obj.localPosition = new Vector3(0f, 0.03f, 0f);
				TextMesh component = ((Component)obj).GetComponent<TextMesh>();
				component.color = Color32.op_Implicit(new Color32((byte)219, (byte)214, (byte)201, (byte)204));
				component.fontSize = 50;
				component.fontStyle = (FontStyle)0;
				component.anchor = (TextAnchor)4;
				component.lineSpacing = 0.7f;
				((Component)component).gameObject.SetActive(false);
				TextMeshes[__instance] = component;
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(ShipItemCompass), "ExtraLateUpdate")]
		public static void UpdateText(ShipItemCompass __instance)
		{
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_006a: Expected O, but got Unknown
			//IL_0083: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_008f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0094: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_0096: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c6: Expected O, but got Unknown
			//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_00d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00da: Unknown result type (might be due to invalid IL or missing references)
			//IL_00df: Unknown result type (might be due to invalid IL or missing references)
			//IL_0102: Unknown result type (might be due to invalid IL or missing references)
			//IL_0113: Unknown result type (might be due to invalid IL or missing references)
			//IL_011e: Expected O, but got Unknown
			//IL_0136: Unknown result type (might be due to invalid IL or missing references)
			//IL_013b: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_0200: Expected O, but got Unknown
			//IL_021a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0225: Expected O, but got Unknown
			//IL_022c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0231: Unknown result type (might be due to invalid IL or missing references)
			//IL_0254: Unknown result type (might be due to invalid IL or missing references)
			if (!ChronoCompassGPSPlugin.EnableGPS.Value || !GameState.playing || GameState.currentlyLoading || GameState.loadingBoatLocalItems || ((ShipItem)__instance).name != "chronocompass" || !TextMeshes.TryGetValue(__instance, out var value))
			{
				return;
			}
			if (!((ShipItem)__instance).sold || ((Component)__instance).gameObject.layer == 5 || (Object)((ShipItem)__instance).currentActualBoat == (Object)null)
			{
				((Component)value).gameObject.SetActive(false);
				return;
			}
			Vector3 position = ((Component)Refs.observerMirror).transform.position;
			Vector3 position2 = ((Component)__instance).transform.position;
			if (Vector3.Distance(position, position2) > ChronoCompassGPSPlugin.ViewDistance.Value)
			{
				((Component)value).gameObject.SetActive(false);
				return;
			}
			float num = (((Object)((PickupableItem)__instance).held != (Object)null) ? 0f : Vector3.SignedAngle(-((Component)__instance).transform.forward, position - position2, Vector3.up));
			((Component)value).transform.localEulerAngles = new Vector3(0f, num, 0f);
			value.fontSize = (((Object)((PickupableItem)__instance).held != (Object)null) ? 30 : 50);
			Vector3 globeCoords = FloatingOriginManager.instance.GetGlobeCoords(((Component)__instance).transform);
			float z = globeCoords.z;
			float x = globeCoords.x;
			string text = (ChronoCompassGPSPlugin.ShowDecimal.Value ? $"Lat: {z:F2}° / Lon: {x:F2}°" : ChronoCompassGPSPlugin.FormatDMS(z, x));
			if (ChronoCompassGPSPlugin.ShowLocalTime.Value)
			{
				text = text + "\nLocal: " + ChronoCompassGPSPlugin.FormatTime(Sun.sun.localTime);
			}
			if (ChronoCompassGPSPlugin.ShowGlobalTime.Value)
			{
				text = text + "\nGlobal: " + ChronoCompassGPSPlugin.FormatTime(Sun.sun.globalTime);
			}
			if (ChronoCompassGPSPlugin.ShowHeadingSpeed.Value)
			{
				Transform currentActualBoat = ((ShipItem)__instance).currentActualBoat;
				if (!BoatBodies.TryGetValue(__instance, out var value2) || (Object)value2 == (Object)null)
				{
					value2 = ((Component)currentActualBoat).GetComponentInParent<Rigidbody>();
					BoatBodies[__instance] = value2;
				}
				if ((Object)value2 != (Object)null)
				{
					Vector3 velocity = value2.velocity;
					velocity.y = 0f;
					float num2 = ((Vector3)(ref velocity)).magnitude * 1.944f;
					float num3 = Mathf.Repeat(((Component)__instance).transform.eulerAngles.y - 5f, 360f);
					text = $"{text}\n{Math.Round(num3)}° {ChronoCompassGPSPlugin.CompassDirection(num3)} / {num2:F1} kn";
				}
			}
			value.text = text;
			((Component)value).gameObject.SetActive(true);
		}
	}
}