Decompiled source of AutoInitiativePlugin v1.3.0

AutoInitiativePlugin.dll

Decompiled 2 weeks 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 ModdingTales;
using Unity.Mathematics;
using UnityEngine;

[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("Nth Dimension")]
[assembly: AssemblyProduct("AutoInitiativePlugin")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("AutoInitiativePlugin")]
[assembly: ComVisible(false)]
[assembly: Guid("c303405d-e66c-4316-9cdb-4e3ca15c6360")]
[assembly: AssemblyFileVersion("1.3.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.3.0.0")]
namespace LordAshes;

[BepInPlugin("org.lordashes.plugins.autoinitiative", "Auto Initiative Plug-In", "1.3.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 void PostOnMainPage(BaseUnityPlugin plugin)
		{
			string text = "Lord Ashes" + ("Lord Ashes".ToUpper().EndsWith("S") ? "'" : "'s");
			ModdingUtils.AddPluginToMenuList(plugin, text);
		}
	}

	[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)
			{
				LoggingPlugin.LogInfo("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;
				LoggingPlugin.LogDebug("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() != "")
						{
							LoggingPlugin.LogDebug("Roll Name " + current.Name);
							if (current.Name.ToUpper().Contains("INITIATIVE"))
							{
								flag = true;
							}
						}
						else
						{
							LoggingPlugin.LogDebug("Roll Is Not Named");
						}
						if (!flag)
						{
							continue;
						}
						LoggingPlugin.LogDebug("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;
											LoggingPlugin.LogDebug("Adding Mod " + value + " x " + num);
											num2 += val3.Value * num;
											continue;
										}
										Enumerator<short> enumerator3 = val2.Results.GetEnumerator();
										try
										{
											while (enumerator3.MoveNext())
											{
												short current3 = enumerator3.Current;
												LoggingPlugin.LogDebug("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;
										LoggingPlugin.LogDebug("Adding Result (P2) " + current4);
										num2 += current4;
									}
								}
								finally
								{
									((IDisposable)enumerator4).Dispose();
								}
							}
							else
							{
								Debug.LogWarning((object)"Missing Operands");
							}
						}
						else
						{
							Debug.LogWarning((object)"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;
						LoggingPlugin.LogDebug("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.3.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_0018: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		//IL_0038: Unknown result type (might be due to invalid IL or missing references)
		//IL_0077: 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_00bf: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c5: Expected O, but got Unknown
		LoggingPlugin.SetLogLevel(((BaseUnityPlugin)this).Config.Bind<DiagnosticLevel>("Settings", "Diagnostic Level", (DiagnosticLevel)3, (ConfigDescription)null).Value);
		string? assemblyQualifiedName = ((object)this).GetType().AssemblyQualifiedName;
		DiagnosticLevel logLevel = LoggingPlugin.GetLogLevel();
		LoggingPlugin.LogInfo(assemblyQualifiedName + " Active. (Diagnostic Level = " + ((object)(DiagnosticLevel)(ref logLevel)).ToString() + ")");
		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((BaseUnityPlugin)(object)this);
	}

	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_0093: Unknown result type (might be due to invalid IL or missing references)
		//IL_0098: 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_014e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0153: Unknown result type (might be due to invalid IL or missing references)
		//IL_0159: Unknown result type (might be due to invalid IL or missing references)
		//IL_0194: 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_019f: 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)
			{
				LoggingPlugin.LogInfo("Clearing Stored Initiatives");
				initiatives.Clear();
				InitiativeManager.SetEditQueue((QueueElement[])(object)new QueueElement[0]);
				ChatManager.SendChatMessageToBoard("Please Roll Initiative", LocalPlayer.Id.Value, (float3?)null, false);
			}
			else
			{
				SystemMessage.DisplayInfoText("Auto Initiative Plugin:\r\nClearing Initiative Is Available To GM Only", 2.5f, 0f);
			}
		}
		value = triggerApply.Value;
		if (!((KeyboardShortcut)(ref value)).IsUp())
		{
			return;
		}
		if (LocalClient.IsInGmMode)
		{
			LoggingPlugin.LogInfo("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();
			LoggingPlugin.LogDebug("Order:");
			CreatureGuid[] array2 = array;
			foreach (CreatureGuid val in array2)
			{
				CreatureBoardAsset val2 = null;
				CreaturePresenter.TryGetAsset(val, ref val2);
				LoggingPlugin.LogDebug(val2.Name);
			}
			List<QueueElement> list = new List<QueueElement>();
			CreatureGuid[] array3 = array;
			QueueElement val4 = default(QueueElement);
			foreach (CreatureGuid val3 in array3)
			{
				((QueueElement)(ref val4))..ctor((ElementType)0, val3);
			}
			InitiativeManager.SetEditQueue(list.ToArray());
		}
		else
		{
			SystemMessage.DisplayInfoText("Auto Initiative Plugin:\r\nApplying Initiative Is Available To GM Only", 2.5f, 0f);
		}
	}

	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_015b: 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_013f: Unknown result type (might be due to invalid IL or missing references)
		if (LocalClient.IsInGmMode)
		{
			LoggingPlugin.LogDebug("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));
			LoggingPlugin.LogDebug("Removing Any Previous Initiative For Creature " + ((object)(CreatureGuid)(ref val)).ToString());
			if (initiatives.ContainsKey(val))
			{
				initiatives.Remove(val);
			}
			LoggingPlugin.LogDebug("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 " + val2.Name + " Stored)", LocalPlayer.Id.Value, (float3?)null, false);
			}
			initiatives.Add(val, value);
		}
		else
		{
			LoggingPlugin.LogDebug("Ignoring Received Initiative Message '" + initiative.value.ToString() + "'. (Player Instance)");
		}
	}
}