Decompiled source of AutoRollPlugin v1.3.0

AutoRollPlugin.dll

Decompiled 5 months ago
using System;
using System.Collections;
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 ©  2023")]
[assembly: AssemblyTrademark("AutoRollPlugin")]
[assembly: ComVisible(false)]
[assembly: Guid("c303405d-e66c-4316-9cdb-4e3ca15c6360")]
[assembly: AssemblyFileVersion("1.3.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.3.0.0")]
namespace LordAshes;

[BepInPlugin("org.lordashes.plugins.autoroll", "Auto Roll Plug-In", "1.3.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_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_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			bool flag = false;
			DiceGroupDescriptor[] diceGroupDescriptors = rollDescriptor.DiceGroupDescriptors;
			foreach (DiceGroupDescriptor val in diceGroupDescriptors)
			{
				if (_self.diagnostics.Value >= DiagnosticLevel.ultra)
				{
					Debug.Log((object)("Auto Roll Plug-In: Patch: Dice Resource Name = " + val.Name));
				}
				if (val.Name != "")
				{
					flag = true;
				}
				else if (_self.diagnostics.Value >= DiagnosticLevel.ultra)
				{
					Debug.Log((object)"Auto Roll Plug-In: Patch: Ignoring Manually Added Dice");
				}
			}
			if (flag)
			{
				if (_self.diagnostics.Value >= DiagnosticLevel.ultra)
				{
					Debug.Log((object)"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_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			DiceGroupDescriptor[] diceGroupDescriptors = rollDescriptor.DiceGroupDescriptors;
			foreach (DiceGroupDescriptor val in diceGroupDescriptors)
			{
				if (_self.diagnostics.Value >= DiagnosticLevel.ultra)
				{
					Debug.Log((object)("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_00d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fa: 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;
			if (_self.diagnostics.Value >= DiagnosticLevel.ultra)
			{
				Vector3 position2 = ((Component)__result).transform.position;
				string? text = ((object)(Vector3)(ref position2)).ToString();
				Quaternion rotation2 = ((Component)__result).transform.rotation;
				Debug.Log((object)("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)
				{
					Debug.Log((object)ex);
				}
			};
		}

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

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

	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.3.0.0";

	public const string Author = "Lord Ashes";

	private static AutoRollPlugin _self = null;

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

	private void Awake()
	{
		//IL_00af: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b5: Expected O, but got Unknown
		_self = this;
		diagnostics = ((BaseUnityPlugin)this).Config.Bind<DiagnosticLevel>("Settings", "Diagnostic Level", DiagnosticLevel.info, (ConfigDescription)null);
		Debug.Log((object)("Auto Roll Plug-In: Active. (Diagnostic Mode = " + ((object)diagnostics).ToString() + ")"));
		if (diagnostics.Value >= DiagnosticLevel.info)
		{
			Debug.Log((object)"Auto Roll Plug-In: Dice added via talespire://dice protocol will be auto added and rolled.");
			Debug.Log((object)"Auto Roll Plug-In: For example:");
			Debug.Log((object)"Auto Roll Plug-In: talespire://dice/Investigation:1D20+5");
			Debug.Log((object)"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()
	{
		if (diagnostics.Value >= DiagnosticLevel.debug)
		{
			Debug.Log((object)"Auto Roll Plug-In: Spawning Dice");
		}
		UIDiceTray val = Object.FindObjectOfType<UIDiceTray>();
		bool flag = (bool)PatchAssistant.GetField(val, "_buttonHeld");
		PatchAssistant.SetField(val, "_buttonHeld", true);
		val.SpawnDice();
		PatchAssistant.SetField(val, "_buttonHeld", flag);
	}

	public void RollDice(RollId rollId)
	{
		//IL_0023: Unknown result type (might be due to invalid IL or missing references)
		//IL_0037: Unknown result type (might be due to invalid IL or missing references)
		//IL_0045: Unknown result type (might be due to invalid IL or missing references)
		if (diagnostics.Value >= DiagnosticLevel.debug)
		{
			Debug.Log((object)"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));
		}
	}

	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)
		if (diagnostics.Value >= DiagnosticLevel.debug)
		{
			Debug.Log((object)"Auto Roll Plug-In: Delayed Roll");
		}
		if (rollId.AsLong != -1)
		{
			yield return (object)new WaitForSeconds(0.5f);
			if (diagnostics.Value >= DiagnosticLevel.info)
			{
				Debug.Log((object)"Auto Roll Plug-In: Rolling Dice");
			}
			DiceManager dm = Object.FindObjectOfType<DiceManager>();
			if (diagnostics.Value >= DiagnosticLevel.ultra)
			{
				Debug.Log((object)("Auto Roll Plug-In: Rolling Dice (Roll Id: " + ((object)(RollId)(ref rollId)).ToString() + ")"));
			}
			float extraBounceHeight = ((BaseUnityPlugin)this).Config.Bind<float>("Setting", "Extra Bounce Height", 0f, (ConfigDescription)null).Value;
			if (extraBounceHeight > 0f)
			{
				dm.GatherDice(((Component)SimpleSingletonBehaviour<CameraController>.Instance).transform.position + extraBounceHeight * Vector3.up, rollId);
			}
			dm.ThrowDice(rollId, new float3(0f, 1f, 0f));
		}
	}

	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)
		if (diagnostics.Value >= DiagnosticLevel.debug)
		{
			Debug.Log((object)"Auto Roll Plug-In: Dice Cleanup");
		}
		if (delayHide >= 0f)
		{
			if (diagnostics.Value >= DiagnosticLevel.ultra)
			{
				Debug.Log((object)"Auto Roll Plug-In: Wait For Cleanup Started");
			}
			yield return (object)new WaitForSeconds(delayHide);
			if (rollId.AsLong != -1)
			{
				DiceManager dm = Object.FindObjectOfType<DiceManager>();
				if (diagnostics.Value >= DiagnosticLevel.ultra)
				{
					Debug.Log((object)("Auto Roll Plug-In: Clearing Dice (" + ((object)(RollId)(ref rollId)).ToString() + ")"));
				}
				try
				{
					dm.ClearDiceRoll(rollId);
				}
				catch
				{
				}
			}
			else if (diagnostics.Value >= DiagnosticLevel.ultra)
			{
				Debug.Log((object)"Auto Roll Plug-In: No Named Dice. Ignoring");
			}
		}
		else if (diagnostics.Value >= DiagnosticLevel.ultra)
		{
			Debug.Log((object)"Auto Roll Plug-In: No Dice Cleanup Desired");
		}
	}
}