Decompiled source of WherePlugin v1.2.1

WherePlugin.dll

Decompiled 2 months ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security.Cryptography;
using System.Text;
using BepInEx;
using BepInEx.Configuration;
using Bounce.Singletons;
using Bounce.Unmanaged;
using Newtonsoft.Json;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("WherePlugin")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("WherePlugin")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("WherePlugin")]
[assembly: ComVisible(false)]
[assembly: Guid("c303405d-e66c-4316-9cdb-4e3ca15c6360")]
[assembly: AssemblyFileVersion("1.2.1.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.2.1.0")]
namespace LordAshes;

[BepInPlugin("org.lordashes.plugins.where", "Where Plugin", "1.2.1.0")]
public class WherePlugin : BaseUnityPlugin
{
	public static class Utility
	{
		public static bool isBoardLoaded()
		{
			return SimpleSingletonBehaviour<CameraController>.HasInstance && SingletonStateMBehaviour<BoardSessionManager, State<BoardSessionManager>>.HasInstance && !BoardSessionManager.IsLoading;
		}

		public static Guid GuidFromString(string input)
		{
			using MD5 mD = MD5.Create();
			byte[] b = mD.ComputeHash(Encoding.Default.GetBytes(input));
			return new Guid(b);
		}

		public static GameObject GetBaseLoader(CreatureGuid cid)
		{
			//IL_0004: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				CreatureBoardAsset val = null;
				CreaturePresenter.TryGetAsset(cid, ref val);
				if ((Object)(object)val != (Object)null)
				{
					Transform match = null;
					Traverse(((Component)val).transform, "BaseLoader", 0, 10, ref match);
					if ((Object)(object)match != (Object)null)
					{
						Debug.Log((object)("Where Plugin: Base Loader '" + ((Object)match.GetChild(0)).name + "' Found"));
						return ((Component)match.GetChild(0)).gameObject;
					}
					Debug.LogWarning((object)"Where Plugin: Could Not Find Base Loader");
					return null;
				}
				return null;
			}
			catch
			{
				return null;
			}
		}

		public static GameObject GetAssetLoader(CreatureGuid cid)
		{
			//IL_0004: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				CreatureBoardAsset val = null;
				CreaturePresenter.TryGetAsset(cid, ref val);
				if ((Object)(object)val != (Object)null)
				{
					Transform match = null;
					Traverse(((Component)val).transform, "AssetLoader", 0, 10, ref match);
					if ((Object)(object)match != (Object)null)
					{
						Debug.Log((object)("Where Plugin: Asset Loader '" + ((Object)match.GetChild(0)).name + "' Found"));
						return ((Component)match.GetChild(0)).gameObject;
					}
					Debug.LogWarning((object)"Where Plugin: Could Not Find Asset Loader");
					return null;
				}
				return null;
			}
			catch
			{
				return null;
			}
		}

		public static void Traverse(Transform root, string seek, int depth, int depthMax, ref Transform match)
		{
			try
			{
				if ((Object)(object)match != (Object)null)
				{
					return;
				}
				if (((Object)root).name == seek)
				{
					match = root;
					return;
				}
				foreach (Transform item in ExtensionMethods.Children(root))
				{
					if (depth < depthMax)
					{
						Traverse(item, seek, depth + 1, depthMax, ref match);
					}
				}
			}
			catch
			{
			}
		}

		public static float ParseFloat(string value)
		{
			return float.Parse(value, CultureInfo.InvariantCulture);
		}

		public static string GetCreatureName(string nameBlock)
		{
			if (nameBlock == null)
			{
				return "(Unknown)";
			}
			if (!nameBlock.Contains("<size=0>"))
			{
				return nameBlock;
			}
			return nameBlock.Substring(0, nameBlock.IndexOf("<size=0>")).Trim();
		}

		public static void PostOnMainPage(MemberInfo plugin)
		{
			SceneManager.sceneLoaded += delegate(Scene scene, LoadSceneMode mode)
			{
				//IL_0072: Unknown result type (might be due to invalid IL or missing references)
				//IL_0079: Expected O, but got Unknown
				try
				{
					if (((Scene)(ref scene)).name == "UI")
					{
						TextMeshProUGUI uITextByName = GetUITextByName("BETA");
						if (Object.op_Implicit((Object)(object)uITextByName))
						{
							((TMP_Text)uITextByName).text = "INJECTED BUILD - unstable mods";
						}
					}
					else
					{
						TextMeshProUGUI uITextByName2 = GetUITextByName("TextMeshPro Text");
						if (Object.op_Implicit((Object)(object)uITextByName2))
						{
							BepInPlugin val = (BepInPlugin)Attribute.GetCustomAttribute(plugin, typeof(BepInPlugin));
							if (((TMP_Text)uITextByName2).text.EndsWith("</size>"))
							{
								((TMP_Text)uITextByName2).text = ((TMP_Text)uITextByName2).text + "\n\nMods Currently Installed:\n";
							}
							TextMeshProUGUI val2 = uITextByName2;
							((TMP_Text)val2).text = ((TMP_Text)val2).text + "\nLord Ashes' " + val.Name + " - " + val.Version;
						}
					}
				}
				catch (Exception ex)
				{
					Debug.Log((object)ex);
				}
			};
		}

		private static TextMeshProUGUI GetUITextByName(string name)
		{
			TextMeshProUGUI[] array = Object.FindObjectsOfType<TextMeshProUGUI>();
			for (int i = 0; i < array.Length; i++)
			{
				if (((Object)array[i]).name == name)
				{
					return array[i];
				}
			}
			return null;
		}
	}

	public enum DiagnosticLevel
	{
		none,
		error,
		warning,
		info,
		debug,
		ultra
	}

	private class Path
	{
		public Vector3 start;

		public Vector3 end;

		public Vector3 delta;

		public int index;
	}

	private class DistanceSpecs
	{
		public float direct;

		public float planar;

		public float elevation;
	}

	private enum AccuracyType
	{
		accurate,
		whole,
		tile
	}

	public const string Name = "Where Plugin";

	public const string Guid = "org.lordashes.plugins.where";

	public const string Version = "1.2.1.0";

	public const string Author = "Lord Ashes";

	private Dictionary<CreatureGuid, CreatureBoardAsset> assets = new Dictionary<CreatureGuid, CreatureBoardAsset>();

	private int assetSelectionIndex = -1;

	private DistanceSpecs assetSelectionDistance = null;

	private int displayX = 0;

	private int displayY = 0;

	private CutsceneData saveCameraPosition;

	private Path showPath = null;

	private string proneId = "806625a6-8f02-4bfe-ba30-4ebe95536c05";

	private ConfigEntry<KeyboardShortcut>[] triggerKey { get; set; } = new ConfigEntry<KeyboardShortcut>[2];


	private ConfigEntry<DiagnosticLevel> diagnostics { get; set; }

	private ConfigEntry<AccuracyType> accurate { get; set; }

	private ConfigEntry<bool> showProne { get; set; }

	private void Awake()
	{
		//IL_006e: Unknown result type (might be due to invalid IL or missing references)
		//IL_009b: Unknown result type (might be due to invalid IL or missing references)
		diagnostics = ((BaseUnityPlugin)this).Config.Bind<DiagnosticLevel>("Settings", "Diagnostic Level", DiagnosticLevel.info, (ConfigDescription)null);
		Debug.Log((object)("Where Plugin: Active. (Diagnostic Mode = " + diagnostics.Value.ToString() + ")"));
		triggerKey[0] = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Hotkeys", "Cycle Asset Selection", new KeyboardShortcut((KeyCode)291, Array.Empty<KeyCode>()), (ConfigDescription)null);
		triggerKey[1] = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Hotkeys", "Select Asset", new KeyboardShortcut((KeyCode)292, Array.Empty<KeyCode>()), (ConfigDescription)null);
		displayX = ((BaseUnityPlugin)this).Config.Bind<int>("Settigs", "Display Left Position", (int)Math.Floor((float)Screen.width * 0.9f), (ConfigDescription)null).Value;
		displayY = ((BaseUnityPlugin)this).Config.Bind<int>("Settigs", "Display Top Position", 40, (ConfigDescription)null).Value;
		accurate = ((BaseUnityPlugin)this).Config.Bind<AccuracyType>("Settings", "Partial Tile Distances", AccuracyType.tile, (ConfigDescription)null);
		showProne = ((BaseUnityPlugin)this).Config.Bind<bool>("Settings", "Show Prone", true, (ConfigDescription)null);
		Utility.PostOnMainPage(((object)this).GetType());
	}

	private void Update()
	{
		//IL_0016: Unknown result type (might be due to invalid IL or missing references)
		//IL_001b: Unknown result type (might be due to invalid IL or missing references)
		//IL_043d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0442: Unknown result type (might be due to invalid IL or missing references)
		//IL_002d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0090: 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_05cb: Unknown result type (might be due to invalid IL or missing references)
		//IL_04b9: Unknown result type (might be due to invalid IL or missing references)
		//IL_04be: Unknown result type (might be due to invalid IL or missing references)
		//IL_04c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_04eb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
		//IL_0527: Unknown result type (might be due to invalid IL or missing references)
		//IL_052c: Unknown result type (might be due to invalid IL or missing references)
		//IL_053e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0543: Unknown result type (might be due to invalid IL or missing references)
		//IL_0555: Unknown result type (might be due to invalid IL or missing references)
		//IL_0566: Unknown result type (might be due to invalid IL or missing references)
		//IL_056b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0575: Unknown result type (might be due to invalid IL or missing references)
		//IL_057a: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
		//IL_0124: Unknown result type (might be due to invalid IL or missing references)
		//IL_0129: Unknown result type (might be due to invalid IL or missing references)
		//IL_0162: Unknown result type (might be due to invalid IL or missing references)
		//IL_0306: Unknown result type (might be due to invalid IL or missing references)
		//IL_02c5: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ca: Unknown result type (might be due to invalid IL or missing references)
		//IL_0344: Unknown result type (might be due to invalid IL or missing references)
		//IL_0354: Unknown result type (might be due to invalid IL or missing references)
		//IL_0359: Unknown result type (might be due to invalid IL or missing references)
		//IL_035e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0379: Unknown result type (might be due to invalid IL or missing references)
		//IL_038e: Unknown result type (might be due to invalid IL or missing references)
		//IL_03ac: Unknown result type (might be due to invalid IL or missing references)
		//IL_03c2: Unknown result type (might be due to invalid IL or missing references)
		//IL_03cc: Unknown result type (might be due to invalid IL or missing references)
		//IL_03dc: Unknown result type (might be due to invalid IL or missing references)
		//IL_03f1: Unknown result type (might be due to invalid IL or missing references)
		//IL_03fb: Unknown result type (might be due to invalid IL or missing references)
		//IL_0400: Unknown result type (might be due to invalid IL or missing references)
		//IL_0405: Unknown result type (might be due to invalid IL or missing references)
		if (!Utility.isBoardLoaded())
		{
			return;
		}
		KeyboardShortcut value = triggerKey[0].Value;
		if (((KeyboardShortcut)(ref value)).IsUp())
		{
			CreatureBoardAsset val = null;
			CreaturePresenter.TryGetAsset(LocalClient.SelectedCreatureId, ref val);
			if ((Object)(object)val == (Object)null)
			{
				SystemMessage.DisplayInfoText("Where Plugin Requires Selected Asset", 2.5f);
			}
			else
			{
				assets.Clear();
				if (diagnostics.Value >= DiagnosticLevel.info)
				{
					Debug.Log((object)"Where Plugin: Refreshing Asset List");
				}
				foreach (CreatureBoardAsset item in (IEnumerable<CreatureBoardAsset>)CreaturePresenter.GetTempReadOnlyViewOfAllCreatureAssets())
				{
					CreatureDataV3 val2 = default(CreatureDataV3);
					CreatureManager.TryGetCreatureData(item.CreatureId, ref val2);
					bool flag = false;
					NGuid[] array = val2.ActiveEmoteIds.ToArray();
					for (int i = 0; i < array.Length; i++)
					{
						NGuid val3 = array[i];
						if (((object)(NGuid)(ref val3)).ToString() == proneId)
						{
							flag = true;
							break;
						}
					}
					if (item.CreatureId != LocalClient.SelectedCreatureId && ((MovableBoardAsset)item).IsVisible && (!flag || showProne.Value))
					{
						assets.Add(item.CreatureId, item);
					}
				}
				if (assets.Count == 0)
				{
					assetSelectionIndex = -1;
				}
				if (assetSelectionIndex >= assets.Count)
				{
					assetSelectionIndex = 0;
				}
				if (diagnostics.Value >= DiagnosticLevel.info)
				{
					Debug.Log((object)"Where Plugin: Cycling Assets");
				}
				assetSelectionIndex++;
				if (assetSelectionIndex >= assets.Count)
				{
					assetSelectionIndex = 0;
				}
				if (assets.Count == 0)
				{
					assetSelectionIndex = -1;
				}
				if (diagnostics.Value >= DiagnosticLevel.debug)
				{
					string[] obj = new string[5]
					{
						"Where Plugin: Selected '",
						(assets.Count == 0) ? "None" : Utility.GetCreatureName(assets.ElementAt(assetSelectionIndex).Value.Name),
						"' (",
						null,
						null
					};
					CreatureGuid key = assets.ElementAt(assetSelectionIndex).Key;
					obj[3] = ((object)(CreatureGuid)(ref key)).ToString();
					obj[4] = ")";
					Debug.Log((object)string.Concat(obj));
				}
				CreatureBoardAsset val4 = null;
				CreaturePresenter.TryGetAsset(assets.ElementAt(assetSelectionIndex).Key, ref val4);
				if ((Object)(object)val != (Object)null && (Object)(object)val4 != (Object)null)
				{
					DistanceSpecs distanceSpecs = new DistanceSpecs();
					Vector3 val5 = ((Component)val4).gameObject.transform.position - ((Component)val).gameObject.transform.position;
					distanceSpecs.direct = ((Vector3)(ref val5)).magnitude;
					distanceSpecs.elevation = ((Component)val4).gameObject.transform.position.y - (0f - ((Component)val).gameObject.transform.position.y);
					Vector2 val6 = new Vector2(((Component)val4).gameObject.transform.position.x, ((Component)val4).gameObject.transform.position.z) - new Vector2(((Component)val).gameObject.transform.position.x, ((Component)val).gameObject.transform.position.z);
					distanceSpecs.planar = ((Vector2)(ref val6)).magnitude;
					assetSelectionDistance = distanceSpecs;
				}
				Debug.Log((object)("Where Plugin: " + JsonConvert.SerializeObject((object)CampaignSessionManager.DistanceUnits)));
			}
		}
		value = triggerKey[1].Value;
		if (!((KeyboardShortcut)(ref value)).IsUp())
		{
			return;
		}
		if (diagnostics.Value >= DiagnosticLevel.info)
		{
			Debug.Log((object)"Where Plugin: Show Assets");
		}
		if (showPath == null)
		{
			if (diagnostics.Value >= DiagnosticLevel.info)
			{
				Debug.Log((object)"Where Plugin: Start Show Assets");
			}
			LegacyCutsceneSetup cutsceneSetup = CameraController.CutsceneSetup;
			saveCameraPosition = cutsceneSetup.GetCutsceneState();
			CreatureBoardAsset val7 = null;
			CreaturePresenter.TryGetAsset(LocalClient.SelectedCreatureId, ref val7);
			CreatureBoardAsset val8 = null;
			CreaturePresenter.TryGetAsset(assets.ElementAt(assetSelectionIndex).Key, ref val8);
			if ((Object)(object)val7 != (Object)null && (Object)(object)val8 != (Object)null)
			{
				showPath = new Path
				{
					start = ((Component)val7).gameObject.transform.position,
					end = ((Component)val8).gameObject.transform.position,
					delta = (((Component)val8).gameObject.transform.position - ((Component)val7).gameObject.transform.position) / 100f,
					index = 0
				};
			}
			((MonoBehaviour)this).StartCoroutine(UpdateShowPath());
		}
		else
		{
			if (diagnostics.Value >= DiagnosticLevel.info)
			{
				Debug.Log((object)"Where Plugin: End Show Assets");
			}
			LegacyCutsceneSetup cutsceneSetup2 = CameraController.CutsceneSetup;
			cutsceneSetup2.PreviewCutsceneState(saveCameraPosition);
			showPath = null;
			assetSelectionIndex = -1;
		}
	}

	private void OnGUI()
	{
		//IL_017a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0191: Unknown result type (might be due to invalid IL or missing references)
		//IL_0196: Unknown result type (might be due to invalid IL or missing references)
		//IL_019b: Unknown result type (might be due to invalid IL or missing references)
		//IL_007a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0099: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
		if (assets.Count > 0 && assetSelectionIndex > -1)
		{
			string text = "[" + (((MovableBoardAsset)assets.ElementAt(assetSelectionIndex).Value).IsFlying ? "F" : ((assetSelectionDistance.elevation > 0.1f) ? "E" : "G")) + "]";
			CreatureDataV3 val = default(CreatureDataV3);
			CreatureManager.TryGetCreatureData(assets.ElementAt(assetSelectionIndex).Value.CreatureId, ref val);
			GUI.Label(new Rect((float)displayX, (float)displayY, (float)Screen.width, 30f), Utility.GetCreatureName(assets.ElementAt(assetSelectionIndex).Value.Name) + ": " + DetermineDistance(assetSelectionDistance.direct) + " (" + DetermineDistance(assetSelectionDistance.planar) + " x " + DetermineDistance(assetSelectionDistance.elevation) + ") " + text);
		}
		if (showPath != null)
		{
			CameraController.ToggleCameraMovement(true);
			CameraController.MoveToPosition(showPath.start + (float)showPath.index * showPath.delta, false, true, false);
		}
	}

	private string DetermineDistance(float num)
	{
		float num2 = 0f;
		if (accurate.Value == AccuracyType.accurate)
		{
			num2 = num * CampaignSessionManager.DistanceUnits[0].NumberPerTile;
		}
		else if (accurate.Value == AccuracyType.whole)
		{
			num2 = (int)(num * CampaignSessionManager.DistanceUnits[0].NumberPerTile);
		}
		else
		{
			num2 = (int)Math.Floor((decimal)(num + 0.5f));
			num2 = (int)Math.Floor(num2 * CampaignSessionManager.DistanceUnits[0].NumberPerTile);
		}
		string text = num2.ToString();
		if (CampaignSessionManager.DistanceUnits[0].Name.ToUpper() == "FEET")
		{
			return text + "'";
		}
		if (CampaignSessionManager.DistanceUnits[0].Name.ToUpper() == "INCHES")
		{
			return text + "\"";
		}
		return text + " " + CampaignSessionManager.DistanceUnits[0].Name;
	}

	private IEnumerator UpdateShowPath()
	{
		while (showPath != null)
		{
			showPath.index++;
			if (showPath.index > 100)
			{
				showPath.index = 0;
			}
			yield return (object)new WaitForSeconds(0.01f);
		}
	}
}