Decompiled source of RuleSet5EAooPlugin v1.1.0

Rulest5eAooPlugin.dll

Decompiled 9 months ago
using System;
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 HarmonyLib;
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("Rulest5eAooPlugin")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Rulest5eAooPlugin")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("Rulest5eAooPlugin")]
[assembly: ComVisible(false)]
[assembly: Guid("c303405d-e66c-4316-9cdb-4e3ca15c6360")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.1.0.0")]
namespace LordAshes;

[BepInPlugin("org.lordashes.plugins.ruleset5eaooplugin", "Ruleset 5e AOO Plugin", "1.1.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class Ruleset5eAOOPlugin : 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)("Ruleset 5e AOO Plugin: Base Loader '" + ((Object)match.GetChild(0)).name + "' Found"));
						return ((Component)match.GetChild(0)).gameObject;
					}
					Debug.LogWarning((object)"Ruleset 5e AOO 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)("Ruleset 5e AOO Plugin: Asset Loader '" + ((Object)match.GetChild(0)).name + "' Found"));
						return ((Component)match.GetChild(0)).gameObject;
					}
					Debug.LogWarning((object)"Ruleset 5e AOO 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 Target
	{
		public float distance { get; set; }

		public CreatureBoardAsset asset { get; set; }
	}

	public const string Name = "Ruleset 5e AOO Plugin";

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

	public const string Version = "1.1.0.0";

	public const string Author = "Lord Ashes";

	private static bool attackMenuOpen = false;

	private static int selectedAttack = 0;

	private static List<Tuple<string, Roll>> attacks = new List<Tuple<string, Roll>>();

	private static List<Target> closestFoes = new List<Target>();

	private GUIStyle Header1Style = new GUIStyle();

	private GUIStyle Header2Style = new GUIStyle();

	private GUIStyle ListStyle = new GUIStyle();

	private GUIStyle ButtonStyle = new GUIStyle();

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


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

	private void Awake()
	{
		//IL_0044: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: Expected O, but got Unknown
		//IL_0072: Unknown result type (might be due to invalid IL or missing references)
		//IL_009f: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f3: Unknown result type (might be due to invalid IL or missing references)
		//IL_0145: Unknown result type (might be due to invalid IL or missing references)
		//IL_0197: Unknown result type (might be due to invalid IL or missing references)
		//IL_01da: 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)("Ruleset 5e AOO Plugin: Active. (Diagnostic Mode = " + ((object)diagnostics).ToString() + ")"));
		Harmony val = new Harmony("org.lordashes.plugins.ruleset5eaooplugin");
		val.PatchAll();
		triggerKey[0] = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Hotkeys", "Open Attack Menu", new KeyboardShortcut((KeyCode)280, Array.Empty<KeyCode>()), (ConfigDescription)null);
		triggerKey[1] = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Hotkeys", "Cycle Attack", new KeyboardShortcut((KeyCode)281, Array.Empty<KeyCode>()), (ConfigDescription)null);
		Texture2D background = Image.LoadTexture("aoo_plate.png", (CacheType)999);
		Texture2D background2 = Image.LoadTexture("aoo_button.png", (CacheType)999);
		Header1Style.fontSize = 32;
		Header1Style.normal.textColor = new Color(0.5f, 0.2f, 0f);
		Header1Style.normal.background = background;
		Header1Style.alignment = (TextAnchor)4;
		Header2Style.fontSize = 28;
		Header2Style.normal.textColor = new Color(0.5f, 0.2f, 0f);
		Header2Style.normal.background = background;
		Header2Style.alignment = (TextAnchor)4;
		ListStyle.fontSize = 28;
		ListStyle.normal.textColor = new Color(0.5f, 0.2f, 0f);
		ListStyle.normal.background = background;
		ListStyle.alignment = (TextAnchor)4;
		ButtonStyle.fontSize = 14;
		ButtonStyle.normal.textColor = Color.black;
		ButtonStyle.normal.background = background2;
		ButtonStyle.alignment = (TextAnchor)4;
		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_0333: Unknown result type (might be due to invalid IL or missing references)
		//IL_0338: Unknown result type (might be due to invalid IL or missing references)
		//IL_008b: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
		//IL_0108: Unknown result type (might be due to invalid IL or missing references)
		//IL_010d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0112: 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())
		{
			if (diagnostics.Value >= DiagnosticLevel.info)
			{
				Debug.Log((object)("Ruleset 5e AOO Plugin: Toggling Attack Menu (To " + attackMenuOpen.ToString().ToUpper() + ")"));
			}
			attackMenuOpen = !attackMenuOpen;
			if (attackMenuOpen)
			{
				selectedAttack = 0;
				CreatureBoardAsset val = null;
				CreaturePresenter.TryGetAsset(LocalClient.SelectedCreatureId, ref val);
				closestFoes.Clear();
				foreach (CreatureBoardAsset item in (IEnumerable<CreatureBoardAsset>)CreaturePresenter.GetTempReadOnlyViewOfAllCreatureAssets())
				{
					if (item.CreatureId != val.CreatureId)
					{
						List<Target> list = closestFoes;
						Target target = new Target();
						Vector3 val2 = ((Component)val).gameObject.transform.position - ((Component)item).gameObject.transform.position;
						target.distance = ((Vector3)(ref val2)).magnitude;
						target.asset = item;
						list.Add(target);
					}
				}
				closestFoes = closestFoes.OrderBy((Target m) => m.distance).ToList();
				string creatureName = Utility.GetCreatureName(val.Name);
				try
				{
					attacks.Clear();
					Character val3 = JsonConvert.DeserializeObject<Character>(File.ReadAllText(creatureName + ".dnd5e", (CacheType)999));
					foreach (Roll attack in val3.attacks)
					{
						if (attack.aoo)
						{
							attacks.Add(new Tuple<string, Roll>("Attack", attack));
						}
					}
					foreach (Roll item2 in val3.attacksDC)
					{
						if (item2.aoo)
						{
							attacks.Add(new Tuple<string, Roll>("AttackDC", item2));
						}
					}
					foreach (Roll skill in val3.skills)
					{
						if (skill.aoo)
						{
							attacks.Add(new Tuple<string, Roll>("Skill", skill));
						}
					}
				}
				catch (Exception ex)
				{
					if (diagnostics.Value >= DiagnosticLevel.warning)
					{
						Debug.Log((object)("Ruleset 5e AOO Plugin: Issue Reading Attacks For '" + creatureName + "' (" + ex?.ToString() + ")"));
					}
				}
			}
		}
		value = triggerKey[1].Value;
		if (((KeyboardShortcut)(ref value)).IsUp())
		{
			if (diagnostics.Value >= DiagnosticLevel.ultra)
			{
				Debug.Log((object)"Ruleset 5e AOO Plugin: Cycling Attack");
			}
			selectedAttack++;
			if (selectedAttack > attacks.Count)
			{
				selectedAttack = 0;
			}
		}
	}

	private void OnGUI()
	{
		//IL_0010: 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_0052: 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_00ce: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ae: Unknown result type (might be due to invalid IL or missing references)
		//IL_0232: Unknown result type (might be due to invalid IL or missing references)
		//IL_02b9: Unknown result type (might be due to invalid IL or missing references)
		//IL_0340: Unknown result type (might be due to invalid IL or missing references)
		if (!attackMenuOpen)
		{
			return;
		}
		CreatureBoardAsset val = null;
		CreaturePresenter.TryGetAsset(LocalClient.SelectedCreatureId, ref val);
		if ((Object)(object)val != (Object)null)
		{
			GUI.Label(new Rect((float)(Screen.width / 2 - 200), (float)(Screen.height / 2 - 200), 400f, 60f), Utility.GetCreatureName(val.Name), Header1Style);
			GUI.Label(new Rect((float)(Screen.width / 2 - 200), (float)(Screen.height / 2 - 140), 400f, 60f), "Attack Of Opportunity", Header2Style);
			try
			{
				GUI.Label(new Rect((float)(Screen.width / 2 - 300), (float)(Screen.height / 2 - 45), 600f, 60f), attacks.ElementAt(selectedAttack).Item2.name, Header2Style);
			}
			catch
			{
				selectedAttack = 0;
			}
			if (GUI.Button(new Rect((float)(Screen.width / 2 - 600), (float)(Screen.height / 2 + 40), 200f, 30f), (closestFoes[0] != null) ? Utility.GetCreatureName(closestFoes[0].asset.Name) : "", ButtonStyle))
			{
				attackMenuOpen = false;
				Activate(val, closestFoes[0].asset);
			}
			if (GUI.Button(new Rect((float)(Screen.width / 2 - 350), (float)(Screen.height / 2 + 40), 200f, 30f), (closestFoes[1] != null) ? Utility.GetCreatureName(closestFoes[1].asset.Name) : "", ButtonStyle))
			{
				attackMenuOpen = false;
				Activate(val, closestFoes[1].asset);
			}
			if (GUI.Button(new Rect((float)(Screen.width / 2 - 100), (float)(Screen.height / 2 + 40), 200f, 30f), (closestFoes[2] != null) ? Utility.GetCreatureName(closestFoes[2].asset.Name) : "", ButtonStyle))
			{
				attackMenuOpen = false;
				Activate(val, closestFoes[2].asset);
			}
			if (GUI.Button(new Rect((float)(Screen.width / 2 + 150), (float)(Screen.height / 2 + 40), 200f, 30f), (closestFoes[3] != null) ? Utility.GetCreatureName(closestFoes[3].asset.Name) : "", ButtonStyle))
			{
				attackMenuOpen = false;
				Activate(val, closestFoes[3].asset);
			}
			if (GUI.Button(new Rect((float)(Screen.width / 2 + 400), (float)(Screen.height / 2 + 40), 200f, 30f), (closestFoes[4] != null) ? Utility.GetCreatureName(closestFoes[4].asset.Name) : "", ButtonStyle))
			{
				attackMenuOpen = false;
				Activate(val, closestFoes[4].asset);
			}
		}
	}

	private void Activate(CreatureBoardAsset instigator, CreatureBoardAsset victim)
	{
		//IL_0087: Unknown result type (might be due to invalid IL or missing references)
		//IL_008c: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
		if (diagnostics.Value >= DiagnosticLevel.info)
		{
			Debug.Log((object)("Ruleset 5e AOO Plugin: Requesting Attack By " + Utility.GetCreatureName(instigator.Name) + " On " + Utility.GetCreatureName(victim.Name) + " Using " + attacks.ElementAt(selectedAttack).Item2.name));
		}
		string[] array = new string[7];
		CreatureGuid creatureId = instigator.CreatureId;
		array[0] = ((object)(CreatureGuid)(ref creatureId)).ToString();
		array[1] = "|";
		creatureId = victim.CreatureId;
		array[2] = ((object)(CreatureGuid)(ref creatureId)).ToString();
		array[3] = "|";
		array[4] = attacks.ElementAt(selectedAttack).Item1;
		array[5] = "|";
		array[6] = attacks.ElementAt(selectedAttack).Item2.name;
		AssetDataPlugin.SendInfo("org.lordashes.plugins.ruleset5e.Request", string.Concat(array));
	}
}