Decompiled source of AutoRollPlugin v1.4.0

AutoRollPlugin.dll

Decompiled 2 weeks ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
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("AutoRollPlugin")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("AutoRollPlugin")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("AutoRollPlugin")]
[assembly: ComVisible(false)]
[assembly: Guid("c303405d-e66c-4316-9cdb-4e3ca15c6360")]
[assembly: AssemblyFileVersion("1.4.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.4.0.0")]
namespace LordAshes;

[BepInPlugin("org.lordashes.plugins.autoroll", "Auto Roll Plug-In", "1.4.0.0")]
public class AutoRollPlugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(UIDiceTray), "SetDiceUrl")]
	public static class Patches
	{
		public static bool Prefix(DiceRollDescriptor rollDescriptor, bool showResult)
		{
			return true;
		}

		public static void Postfix(DiceRollDescriptor rollDescriptor, bool showResult)
		{
			//IL_0004: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			bool flag = false;
			DiceGroupDescriptor[] diceGroupDescriptors = rollDescriptor.DiceGroupDescriptors;
			foreach (DiceGroupDescriptor val in diceGroupDescriptors)
			{
				LoggingPlugin.LogTrace("Auto Roll Plug-In: Patch: Dice Resource Name = " + val.Name);
				if (val.Name != "")
				{
					flag = true;
				}
				else
				{
					LoggingPlugin.LogTrace("Auto Roll Plug-In: Patch: Ignoring Manually Added Dice");
				}
			}
			if (flag)
			{
				LoggingPlugin.LogTrace("Auto Roll Plug-In: Spawning Dice");
				if (callbackAutoSpawn != null)
				{
					callbackAutoSpawn();
				}
			}
		}
	}

	[HarmonyPatch(typeof(DiceManager), "CreateLocalRoll")]
	public static class PatchCreateLocalRoll
	{
		public static bool Prefix(DiceRollDescriptor rollDescriptor, bool isGmRoll, bool showResult, RollId rollId)
		{
			return true;
		}

		public static void Postfix(DiceRollDescriptor rollDescriptor, bool isGmRoll, bool showResult, RollId rollId)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: 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_004e: Unknown result type (might be due to invalid IL or missing references)
			DiceGroupDescriptor[] diceGroupDescriptors = rollDescriptor.DiceGroupDescriptors;
			foreach (DiceGroupDescriptor val in diceGroupDescriptors)
			{
				LoggingPlugin.LogTrace("Auto Roll Plug-In: Patch: Dice '" + val.Name + "' Created");
			}
			if (callbackAutoRoll != null)
			{
				callbackAutoRoll(rollId);
			}
		}
	}

	[HarmonyPatch(typeof(Die), "Spawn")]
	public static class PatchDieSpawn
	{
		public static bool Prefix(DieKind kind, float3 pos, quaternion rot, RollId rollId, byte groupId, bool gmOnlyDie, bool showResult)
		{
			return true;
		}

		public static void Postfix(DieKind kind, float3 pos, quaternion rot, RollId rollId, byte groupId, bool gmOnlyDie, bool showResult, ref Die __result)
		{
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			//IL_0097: 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_00ba: 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_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)
			HeightHidePlane val = Object.FindObjectOfType<HeightHidePlane>();
			Quaternion rotation = Quaternion.Euler(new Vector3((float)random.Next(0, 180), (float)random.Next(0, 180), (float)random.Next(0, 180)));
			Vector3 position = default(Vector3);
			((Vector3)(ref position))..ctor(((Component)__result).transform.position.x, ((Component)__result).transform.position.y + val.Height / 25f, ((Component)__result).transform.position.z);
			((Component)__result).transform.position = position;
			((Component)__result).transform.rotation = rotation;
			Vector3 position2 = ((Component)__result).transform.position;
			string? text = ((object)(Vector3)(ref position2)).ToString();
			Quaternion rotation2 = ((Component)__result).transform.rotation;
			LoggingPlugin.LogTrace("Auto Roll Plug-In: Adjusted Die: Pos: " + text + ", Rot: " + ((object)(Quaternion)(ref rotation2)).ToString());
		}
	}

	public static class PatchAssistant
	{
		public static object GetProperty(object instance, string propertyName)
		{
			Type type = instance.GetType();
			foreach (PropertyInfo runtimeProperty in type.GetRuntimeProperties())
			{
				if (runtimeProperty.Name.Contains(propertyName))
				{
					return runtimeProperty.GetValue(instance);
				}
			}
			PropertyInfo[] properties = type.GetProperties();
			foreach (PropertyInfo propertyInfo in properties)
			{
				if (propertyInfo.Name.Contains(propertyName))
				{
					return propertyInfo.GetValue(instance);
				}
			}
			return null;
		}

		public static void SetProperty(object instance, string propertyName, object value)
		{
			Type type = instance.GetType();
			foreach (PropertyInfo runtimeProperty in type.GetRuntimeProperties())
			{
				if (runtimeProperty.Name.Contains(propertyName))
				{
					runtimeProperty.SetValue(instance, value);
					return;
				}
			}
			PropertyInfo[] properties = type.GetProperties();
			foreach (PropertyInfo propertyInfo in properties)
			{
				if (propertyInfo.Name.Contains(propertyName))
				{
					propertyInfo.SetValue(instance, value);
					break;
				}
			}
		}

		public static object GetField(object instance, string fieldName)
		{
			Type type = instance.GetType();
			foreach (FieldInfo runtimeField in type.GetRuntimeFields())
			{
				if (runtimeField.Name.Contains(fieldName))
				{
					return runtimeField.GetValue(instance);
				}
			}
			FieldInfo[] fields = type.GetFields();
			foreach (FieldInfo fieldInfo in fields)
			{
				if (fieldInfo.Name.Contains(fieldName))
				{
					return fieldInfo.GetValue(instance);
				}
			}
			return null;
		}

		public static void SetField(object instance, string fieldName, object value)
		{
			Type type = instance.GetType();
			foreach (FieldInfo runtimeField in type.GetRuntimeFields())
			{
				if (runtimeField.Name.Contains(fieldName))
				{
					runtimeField.SetValue(instance, value);
					return;
				}
			}
			FieldInfo[] fields = type.GetFields();
			foreach (FieldInfo fieldInfo in fields)
			{
				if (fieldInfo.Name.Contains(fieldName))
				{
					fieldInfo.SetValue(instance, value);
					break;
				}
			}
		}

		public static object UseMethod(object instance, string methodName, object[] parameters)
		{
			Type type = instance.GetType();
			foreach (MethodInfo runtimeMethod in type.GetRuntimeMethods())
			{
				if (runtimeMethod.Name.Contains(methodName))
				{
					return runtimeMethod.Invoke(instance, parameters);
				}
			}
			MethodInfo[] methods = type.GetMethods();
			foreach (MethodInfo methodInfo in methods)
			{
				if (methodInfo.Name.Contains(methodName))
				{
					return methodInfo.Invoke(instance, parameters);
				}
			}
			return null;
		}
	}

	public static class Utility
	{
		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)
				{
					LoggingPlugin.LogError(ex.ToString());
				}
			};
		}

		public static bool isBoardLoaded()
		{
			return SimpleSingletonBehaviour<CameraController>.HasInstance && SingletonStateMBehaviour<BoardSessionManager, State<BoardSessionManager>>.HasInstance && !BoardSessionManager.IsLoading;
		}

		public static bool StrictKeyCheck(KeyboardShortcut check)
		{
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			if (!((KeyboardShortcut)(ref check)).IsUp())
			{
				return false;
			}
			KeyCode[] array = new KeyCode[6];
			RuntimeHelpers.InitializeArray(array, (RuntimeFieldHandle)/*OpCode not supported: LdMemberToken*/);
			KeyCode[] array2 = (KeyCode[])(object)array;
			foreach (KeyCode val in array2)
			{
				if (Input.GetKey(val) != ((KeyboardShortcut)(ref check)).Modifiers.Contains(val))
				{
					return false;
				}
			}
			return true;
		}

		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;
		}
	}

	[CompilerGenerated]
	private sealed class <DelayedHide>d__22 : IEnumerator<object>, IDisposable, IEnumerator
	{
		private int <>1__state;

		private object <>2__current;

		public RollId rollId;

		public float delayHide;

		public AutoRollPlugin <>4__this;

		private DiceManager <dm>5__1;

		object IEnumerator<object>.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		object IEnumerator.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		[DebuggerHidden]
		public <DelayedHide>d__22(int <>1__state)
		{
			this.<>1__state = <>1__state;
		}

		[DebuggerHidden]
		void IDisposable.Dispose()
		{
			<dm>5__1 = null;
			<>1__state = -2;
		}

		private bool MoveNext()
		{
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Expected O, but got Unknown
			//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
			switch (<>1__state)
			{
			default:
				return false;
			case 0:
				<>1__state = -1;
				LoggingPlugin.LogDebug("Auto Roll Plug-In: Dice Cleanup");
				if (delayHide >= 0f)
				{
					LoggingPlugin.LogTrace("Auto Roll Plug-In: Wait For Cleanup Started");
					<>2__current = (object)new WaitForSeconds(delayHide);
					<>1__state = 1;
					return true;
				}
				LoggingPlugin.LogTrace("Auto Roll Plug-In: No Dice Cleanup Desired");
				break;
			case 1:
				<>1__state = -1;
				if (rollId.AsLong != -1)
				{
					<dm>5__1 = Object.FindObjectOfType<DiceManager>();
					LoggingPlugin.LogTrace("Auto Roll Plug-In: Clearing Dice (" + ((object)(RollId)(ref rollId)).ToString() + ")");
					try
					{
						<dm>5__1.ClearDiceRoll(rollId);
					}
					catch
					{
					}
					<dm>5__1 = null;
				}
				else
				{
					LoggingPlugin.LogTrace("Auto Roll Plug-In: No Named Dice. Ignoring");
				}
				break;
			}
			return false;
		}

		bool IEnumerator.MoveNext()
		{
			//ILSpy generated this explicit interface implementation from .override directive in MoveNext
			return this.MoveNext();
		}

		[DebuggerHidden]
		void IEnumerator.Reset()
		{
			throw new NotSupportedException();
		}
	}

	[CompilerGenerated]
	private sealed class <DelayedRoll>d__21 : IEnumerator<object>, IDisposable, IEnumerator
	{
		private int <>1__state;

		private object <>2__current;

		public RollId rollId;

		public AutoRollPlugin <>4__this;

		private DiceManager <dm>5__1;

		private float <extraBounceHeight>5__2;

		object IEnumerator<object>.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		object IEnumerator.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		[DebuggerHidden]
		public <DelayedRoll>d__21(int <>1__state)
		{
			this.<>1__state = <>1__state;
		}

		[DebuggerHidden]
		void IDisposable.Dispose()
		{
			<dm>5__1 = null;
			<>1__state = -2;
		}

		private bool MoveNext()
		{
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Expected O, but got Unknown
			//IL_011c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0130: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_0103: Unknown result type (might be due to invalid IL or missing references)
			//IL_0109: Unknown result type (might be due to invalid IL or missing references)
			switch (<>1__state)
			{
			default:
				return false;
			case 0:
				<>1__state = -1;
				LoggingPlugin.LogDebug("Auto Roll Plug-In: Delayed Roll");
				if (rollId.AsLong != -1)
				{
					<>2__current = (object)new WaitForSeconds(0.5f);
					<>1__state = 1;
					return true;
				}
				break;
			case 1:
				<>1__state = -1;
				LoggingPlugin.LogInfo("Auto Roll Plug-In: Rolling Dice");
				<dm>5__1 = Object.FindObjectOfType<DiceManager>();
				LoggingPlugin.LogTrace("Auto Roll Plug-In: Rolling Dice (Roll Id: " + ((object)(RollId)(ref rollId)).ToString() + ")");
				<extraBounceHeight>5__2 = ((BaseUnityPlugin)<>4__this).Config.Bind<float>("Setting", "Extra Bounce Height", 0f, (ConfigDescription)null).Value;
				if (<extraBounceHeight>5__2 > 0f)
				{
					<dm>5__1.GatherDice(((Component)SimpleSingletonBehaviour<CameraController>.Instance).transform.position + <extraBounceHeight>5__2 * Vector3.up, rollId);
				}
				<dm>5__1.ThrowDice(rollId, new float3(0f, 1f, 0f));
				<dm>5__1 = null;
				break;
			}
			return false;
		}

		bool IEnumerator.MoveNext()
		{
			//ILSpy generated this explicit interface implementation from .override directive in MoveNext
			return this.MoveNext();
		}

		[DebuggerHidden]
		void IEnumerator.Reset()
		{
			throw new NotSupportedException();
		}
	}

	public static Action callbackAutoSpawn = null;

	public static Action<RollId> callbackAutoRoll = null;

	private static Random random = new Random();

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

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

	public const string Version = "1.4.0.0";

	public const string Author = "Lord Ashes";

	private static AutoRollPlugin _self = null;

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

	private void Awake()
	{
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0039: Unknown result type (might be due to invalid IL or missing references)
		//IL_003e: Unknown result type (might be due to invalid IL or missing references)
		//IL_00af: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b5: Expected O, but got Unknown
		_self = this;
		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 Mode = " + ((object)(DiagnosticLevel)(ref logLevel)).ToString() + ")");
		LoggingPlugin.LogInfo("Auto Roll Plug-In: Dice added via talespire://dice protocol will be auto added and rolled.");
		LoggingPlugin.LogInfo("Auto Roll Plug-In: For example:");
		LoggingPlugin.LogInfo("Auto Roll Plug-In: talespire://dice/Investigation:1D20+5");
		LoggingPlugin.LogInfo("Auto Roll Plug-In: talespire://dice/Attack:1D20+5/Damage:2D6+3");
		callbackAutoSpawn = SpawnDice;
		callbackAutoRoll = RollDice;
		Harmony val = new Harmony("org.lordashes.plugins.autoroll");
		val.PatchAll();
		Utility.PostOnMainPage(((object)this).GetType());
	}

	private void Update()
	{
	}

	public void SpawnDice()
	{
		LoggingPlugin.LogDebug("Auto Roll Plug-In: Spawning Dice");
		LoggingPlugin.LogTrace("Auto Roll Plug-In: Getting Dice Tray");
		UIDiceTray val = Object.FindObjectOfType<UIDiceTray>();
		LoggingPlugin.LogTrace("Auto Roll Plug-In: Saving Left Mouse Button Held");
		bool flag = (bool)PatchAssistant.GetField(val, "_leftMouseButtonHeld");
		PatchAssistant.SetField(val, "_leftMouseButtonHeld", true);
		LoggingPlugin.LogTrace("Auto Roll Plug-In: Performing Spawn");
		val.SpawnDice();
		LoggingPlugin.LogTrace("Auto Roll Plug-In: Restoring Left Mouse Button Held");
		PatchAssistant.SetField(val, "_leftMouseButtonHeld", flag);
	}

	public void RollDice(RollId rollId)
	{
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0020: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		LoggingPlugin.LogDebug("Auto Roll Plug-In: Getting Ready To Roll Dice");
		if (rollId.AsLong != -1)
		{
			((MonoBehaviour)this).StartCoroutine(DelayedRoll(rollId));
			((MonoBehaviour)this).StartCoroutine(DelayedHide(rollId, ((BaseUnityPlugin)this).Config.Bind<float>("Settings", "Seconds Before Dice Clear", 5f, (ConfigDescription)null).Value));
		}
	}

	[IteratorStateMachine(typeof(<DelayedRoll>d__21))]
	private IEnumerator DelayedRoll(RollId rollId)
	{
		//IL_000e: Unknown result type (might be due to invalid IL or missing references)
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
		return new <DelayedRoll>d__21(0)
		{
			<>4__this = this,
			rollId = rollId
		};
	}

	[IteratorStateMachine(typeof(<DelayedHide>d__22))]
	private IEnumerator DelayedHide(RollId rollId, float delayHide)
	{
		//IL_000e: Unknown result type (might be due to invalid IL or missing references)
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
		return new <DelayedHide>d__22(0)
		{
			<>4__this = this,
			rollId = rollId,
			delayHide = delayHide
		};
	}
}