Decompiled source of AutoInitiativePlugin v1.1.0

AutoInitiativePlugin.dll

Decompiled 2 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.ManagedCollections;
using Bounce.Singletons;
using HarmonyLib;
using TMPro;
using Unity.Mathematics;
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("AutoInitiativePlugin")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("AutoInitiativePlugin")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("AutoInitiativePlugin")]
[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.autoinitiative", "Auto Initiative Plug-In", "1.1.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class AutoInitiativePlugin : 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 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;
		}
	}

	[HarmonyPatch(typeof(UIDiceRollResult), "DisplayResult")]
	public static class PatchDisplayResult
	{
		public static bool Prefix(RollResults rollResultData, ClientGuid sender)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_0161: Unknown result type (might be due to invalid IL or missing references)
			//IL_026e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0175: Unknown result type (might be due to invalid IL or missing references)
			//IL_017b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0180: Unknown result type (might be due to invalid IL or missing references)
			//IL_027f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0285: Unknown result type (might be due to invalid IL or missing references)
			//IL_028a: 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_0081: Unknown result type (might be due to invalid IL or missing references)
			//IL_0087: 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_0189: Unknown result type (might be due to invalid IL or missing references)
			//IL_018e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0199: Unknown result type (might be due to invalid IL or missing references)
			//IL_0314: Unknown result type (might be due to invalid IL or missing references)
			//IL_031a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0321: Unknown result type (might be due to invalid IL or missing references)
			//IL_0327: 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_0200: Unknown result type (might be due to invalid IL or missing references)
			//IL_0205: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_0349: Unknown result type (might be due to invalid IL or missing references)
			//IL_034e: 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_037e: 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_0126: Unknown result type (might be due to invalid IL or missing references)
			//IL_0128: Unknown result type (might be due to invalid IL or missing references)
			//IL_0149: Unknown result type (might be due to invalid IL or missing references)
			//IL_014a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0150: Invalid comparison between Unknown and I4
			if (sender == LocalClient.Id)
			{
				Debug.Log((object)("Auto Initiative Plugin: Roll Generated By This Client (" + ((object)(ClientGuid)(ref sender)).ToString() + ")"));
				bool flag = false;
				int num = 0;
				int num2 = 0;
				num2 = 0;
				RollId rollId = rollResultData.RollId;
				Debug.Log((object)("Auto Initiative Plugin: Roll Id " + ((object)(RollId)(ref rollId)).ToString()));
				Enumerator<RollResultsGroup> enumerator = rollResultData.ResultsGroups.GetEnumerator();
				try
				{
					RollResultsOperation val = default(RollResultsOperation);
					RollResult val2 = default(RollResult);
					RollValue val3 = default(RollValue);
					while (enumerator.MoveNext())
					{
						RollResultsGroup current = enumerator.Current;
						num = 0;
						if (current.Name != null && Convert.ToString(current.Name).Trim() != "")
						{
							Debug.Log((object)("Auto Initiative Plugin: Roll Name " + current.Name));
							if (current.Name.ToUpper().Contains("INITIATIVE"))
							{
								flag = true;
							}
						}
						else
						{
							Debug.Log((object)"Auto Initiative Plugin: Roll Is Not Named");
						}
						if (!flag)
						{
							continue;
						}
						Debug.Log((object)"Auto Initiative Plugin: Calculating Initiative");
						Which val4 = ((RollOperand)(ref current.Result)).Get(ref val, ref val2, ref val3);
						if (((Enum)val4).HasFlag((Enum)(object)(Which)0))
						{
							num = 1;
							try
							{
								num = (((int)val.Operator != 1) ? 1 : (-1));
							}
							catch (Exception)
							{
							}
							if (val.Operands != null)
							{
								Enumerator<RollOperand> enumerator2 = val.Operands.GetEnumerator();
								try
								{
									while (enumerator2.MoveNext())
									{
										RollOperand current2 = enumerator2.Current;
										((RollOperand)(ref current2)).Get(ref val, ref val2, ref val3);
										if (((DieKind)(ref val2.Kind)).RegisteredName == "<unknown>")
										{
											short value = val3.Value;
											Debug.Log((object)("Auto Initiative Plugin: Adding Mod " + value + " x " + num));
											num2 += val3.Value * num;
											continue;
										}
										Enumerator<short> enumerator3 = val2.Results.GetEnumerator();
										try
										{
											while (enumerator3.MoveNext())
											{
												short current3 = enumerator3.Current;
												Debug.Log((object)("Auto Initiative Plugin: Adding Result (P1) " + current3));
												num2 += current3;
											}
										}
										finally
										{
											((IDisposable)enumerator3).Dispose();
										}
									}
								}
								finally
								{
									((IDisposable)enumerator2).Dispose();
								}
							}
							else if (val2.Results != null)
							{
								Enumerator<short> enumerator4 = val2.Results.GetEnumerator();
								try
								{
									while (enumerator4.MoveNext())
									{
										short current4 = enumerator4.Current;
										Debug.Log((object)("Auto Initiative Plugin: Adding Result (P2) " + current4));
										num2 += current4;
									}
								}
								finally
								{
									((IDisposable)enumerator4).Dispose();
								}
							}
							else
							{
								Debug.LogWarning((object)"Auto Initiative Plugin: Missing Operands");
							}
						}
						else
						{
							Debug.LogWarning((object)"Auto Initiative Plugin: Missing Top Level Operator");
						}
					}
				}
				finally
				{
					((IDisposable)enumerator).Dispose();
				}
				if (flag)
				{
					_ = LocalClient.SelectedCreatureId;
					CreatureGuid selectedCreatureId = LocalClient.SelectedCreatureId;
					CreatureGuid val5 = default(CreatureGuid);
					if (selectedCreatureId != val5)
					{
						string text = num2.ToString();
						val5 = LocalClient.SelectedCreatureId;
						Debug.Log((object)("Auto Initiative Plugin: Sending Initiative " + text + ":" + ((object)(CreatureGuid)(ref val5)).ToString()));
						string text2 = num2.ToString();
						val5 = LocalClient.SelectedCreatureId;
						AssetDataPlugin.SendInfo("org.lordashes.plugins.autoinitiative", text2 + ":" + ((object)(CreatureGuid)(ref val5)).ToString());
					}
				}
			}
			return true;
		}
	}

	public const string Name = "Auto Initiative Plug-In";

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

	public const string Version = "1.1.0.0";

	public const string Author = "Lord Ashes";

	private static Dictionary<CreatureGuid, int> initiatives = new Dictionary<CreatureGuid, int>();

	private ConfigEntry<KeyboardShortcut> triggerClear { get; set; }

	private ConfigEntry<KeyboardShortcut> triggerApply { get; set; }

	private void Awake()
	{
		//IL_0042: Unknown result type (might be due to invalid IL or missing references)
		//IL_0074: Unknown result type (might be due to invalid IL or missing references)
		//IL_008a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0090: Expected O, but got Unknown
		Debug.Log((object)("Auto Initiative Plugin: " + ((object)this).GetType().AssemblyQualifiedName + " Active."));
		triggerClear = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Hotkeys", "Clear Initiative Order", new KeyboardShortcut((KeyCode)105, (KeyCode[])(object)new KeyCode[1] { (KeyCode)303 }), (ConfigDescription)null);
		triggerApply = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Hotkeys", "Apply Initiative Order", new KeyboardShortcut((KeyCode)105, (KeyCode[])(object)new KeyCode[1] { (KeyCode)305 }), (ConfigDescription)null);
		Harmony val = new Harmony("org.lordashes.plugins.autoinitiative");
		val.PatchAll();
		AssetDataPlugin.Subscribe("org.lordashes.plugins.autoinitiative", (Action<DatumChange>)RequestHander);
		Utility.PostOnMainPage(((object)this).GetType());
	}

	private void Update()
	{
		//IL_0014: Unknown result type (might be due to invalid IL or missing references)
		//IL_0019: Unknown result type (might be due to invalid IL or missing references)
		//IL_008e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0093: Unknown result type (might be due to invalid IL or missing references)
		//IL_0057: Unknown result type (might be due to invalid IL or missing references)
		//IL_005c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0149: 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_0154: Unknown result type (might be due to invalid IL or missing references)
		if (!Utility.isBoardLoaded())
		{
			return;
		}
		KeyboardShortcut value = triggerClear.Value;
		if (((KeyboardShortcut)(ref value)).IsUp())
		{
			if (LocalClient.IsInGmMode)
			{
				Debug.Log((object)"Auto Initiative Plugin:\r\nClearing Stored Initiatives");
				initiatives.Clear();
				InitiativeManager.SetEditQueue((CreatureGuid[])(object)new CreatureGuid[0]);
				ChatManager.SendChatMessageToBoard("Please Roll Initiative", LocalPlayer.Id.Value, (float3?)null, false);
			}
			else
			{
				SystemMessage.DisplayInfoText("Auto Initiative Plugin: Clearing Initiative Is Available To GM Only", 2.5f);
			}
		}
		value = triggerApply.Value;
		if (!((KeyboardShortcut)(ref value)).IsUp())
		{
			return;
		}
		if (LocalClient.IsInGmMode)
		{
			Debug.Log((object)("Auto Initiative Plugin: Applying Initiative Order For " + initiatives.Count + " Creatures"));
			CreatureGuid[] array = initiatives.OrderByDescending(delegate(KeyValuePair<CreatureGuid, int> entry)
			{
				KeyValuePair<CreatureGuid, int> keyValuePair2 = entry;
				return keyValuePair2.Value;
			}).Select(delegate(KeyValuePair<CreatureGuid, int> entry)
			{
				//IL_0004: Unknown result type (might be due to invalid IL or missing references)
				KeyValuePair<CreatureGuid, int> keyValuePair = entry;
				return keyValuePair.Key;
			}).ToArray();
			Debug.Log((object)"Auto Initiative Plugin: Order:");
			CreatureGuid[] array2 = array;
			foreach (CreatureGuid val in array2)
			{
				CreatureBoardAsset val2 = null;
				CreaturePresenter.TryGetAsset(val, ref val2);
				Debug.Log((object)("Auto Initiative Plugin: " + Utility.GetCreatureName(val2.Name)));
			}
			InitiativeManager.SetEditQueue(array);
		}
		else
		{
			SystemMessage.DisplayInfoText("Auto Initiative Plugin:\r\nApplying Initiative Is Available To GM Only", 2.5f);
		}
	}

	private void RequestHander(DatumChange initiative)
	{
		//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
		//IL_0160: Unknown result type (might be due to invalid IL or missing references)
		//IL_013f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0144: Unknown result type (might be due to invalid IL or missing references)
		if (LocalClient.IsInGmMode)
		{
			Debug.Log((object)("Auto Initiative Plugin: Received Initiative Message '" + initiative.value.ToString() + "'. (GM Instance)"));
			int value = int.Parse(initiative.value.ToString().Substring(0, initiative.value.ToString().IndexOf(":")));
			CreatureGuid val = default(CreatureGuid);
			((CreatureGuid)(ref val))..ctor(initiative.value.ToString().Substring(initiative.value.ToString().IndexOf(":") + 1));
			Debug.Log((object)("Auto Initiative Plugin: Removing Any Previous Initiative For Creature " + ((object)(CreatureGuid)(ref val)).ToString()));
			if (initiatives.ContainsKey(val))
			{
				initiatives.Remove(val);
			}
			Debug.Log((object)("Auto Initiative Plugin: Storing Initiative " + value + " For Creature " + ((object)(CreatureGuid)(ref val)).ToString()));
			CreatureBoardAsset val2 = default(CreatureBoardAsset);
			CreaturePresenter.TryGetAsset(val, ref val2);
			if ((Object)(object)val2 != (Object)null)
			{
				ChatManager.SendChatMessageToBoard("(Initiative " + value + " For " + Utility.GetCreatureName(val2.Name) + " Stored)", LocalPlayer.Id.Value, (float3?)null, false);
			}
			initiatives.Add(val, value);
		}
		else
		{
			Debug.Log((object)("Auto Initiative Plugin: Ignoring Received Initiative Message '" + initiative.value.ToString() + "'. (Player Instance)"));
		}
	}
}