Decompiled source of DiceProtocolExtension v1.1.0

DiceProtocolExtension.dll

Decompiled 3 weeks ago
using System;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using BepInEx;
using BepInEx.Configuration;
using Bounce.Singletons;
using Dice;
using HarmonyLib;
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("DiceProtocolExtension")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("DiceProtocolExtension")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("DiceProtocolExtension")]
[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;

[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInPlugin("org.lordashes.plugins.diceprotocolextension", "Dice Protocol Extension", "1.1.0.0")]
public class DiceProtocolExtension : 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)("Dice Protocol Extension: Base Loader '" + ((Object)match.GetChild(0)).name + "' Found"));
						return ((Component)match.GetChild(0)).gameObject;
					}
					Debug.LogWarning((object)"Dice Protocol Extension: 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)("Dice Protocol Extension: Asset Loader '" + ((Object)match.GetChild(0)).name + "' Found"));
						return ((Component)match.GetChild(0)).gameObject;
					}
					Debug.LogWarning((object)"Dice Protocol Extension: 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();
		}
	}

	[HarmonyPatch(typeof(UI_DiceRollGroup), "DisplayGroup")]
	private static class PatchDisplayResult2
	{
		public static bool Prefix(RollGroup resultsGroup, bool isFirst)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			if (isFirst)
			{
				lastRollName = Convert.ToString(resultsGroup.Name);
				LoggingPlugin.LogDebug("Recorded Roll As " + lastRollName);
				adjustment = 0;
				lastRollPass = 0;
			}
			return true;
		}
	}

	[HarmonyPatch(typeof(UI_DiceRollGroup), "DisplayResult")]
	private static class PatchDisplayResult
	{
		public static bool Prefix(RollResult result, bool isOutermost)
		{
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0062: 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_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_0107: Unknown result type (might be due to invalid IL or missing references)
			//IL_0113: 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_016d: Unknown result type (might be due to invalid IL or missing references)
			if (((DieKind)(ref result.Kind)).RegisteredName == "d20")
			{
				LoggingPlugin.LogDebug("D20 Dice");
				if (result.Results.Length == 2)
				{
					LoggingPlugin.LogDebug("Two D20s");
					if (lastRollName.Contains("(+)"))
					{
						adjustment = Math.Min(result.Results[0], result.Results[1]);
						LoggingPlugin.LogDebug("Rolling With Advantage. Removing " + adjustment + " From " + result.Results[0] + " And " + result.Results[1]);
					}
					else if (lastRollName.Contains("(-)"))
					{
						adjustment = Math.Max(result.Results[0], result.Results[1]);
						LoggingPlugin.LogDebug("Rolling With Disadvantage. Removing " + adjustment + " From " + result.Results[0] + " And " + result.Results[1]);
					}
				}
			}
			return true;
		}
	}

	[HarmonyPatch(typeof(UIDiceTotalItem), "SetResult")]
	private static class PatchSetResult
	{
		public static bool Prefix(ref int result)
		{
			lastRollPass++;
			LoggingPlugin.LogDebug("Result " + result + " (Pass=" + lastRollPass + ")");
			if (lastRollPass == 2 || lastRollPass == 4)
			{
				result -= adjustment;
				LoggingPlugin.LogDebug("Adjusted Result To " + result);
			}
			return true;
		}
	}

	[HarmonyPatch(typeof(LocalConnectionManager), "Update")]
	private static class PatchUpdate
	{
		public static bool Prefix(ref string ____url)
		{
			if (____url != null && ____url.ToLower().Trim().StartsWith("talespire://dice/"))
			{
				LoggingPlugin.LogDebug("Intercepted: " + ____url);
				string input = ____url.ToLower().Trim().Substring("talespire://dice/".Length);
				input = NormalizeDiceExpression(input);
				____url = "talespire://dice/" + input;
				LoggingPlugin.LogDebug("Revised: " + ____url);
			}
			return true;
		}
	}

	public const string Name = "Dice Protocol Extension";

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

	public const string Version = "1.1.0.0";

	public const string Author = "Lord Ashes'";

	private static string lastRollName = "";

	private static int adjustment = 0;

	private static int lastRollPass = 0;

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

	private void Awake()
	{
		//IL_0025: Unknown result type (might be due to invalid IL or missing references)
		//IL_003b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0040: Unknown result type (might be due to invalid IL or missing references)
		//IL_0063: Unknown result type (might be due to invalid IL or missing references)
		//IL_0069: Expected O, but got Unknown
		diagnostics = ((BaseUnityPlugin)this).Config.Bind<DiagnosticLevel>("Settings", "Diagnostic Level", (DiagnosticLevel)3, (ConfigDescription)null);
		LoggingPlugin.SetLogLevel(diagnostics.Value);
		DiagnosticLevel value = diagnostics.Value;
		LoggingPlugin.LogDebug("Active. (Diagnostic Mode = " + ((object)(DiagnosticLevel)(ref value)).ToString() + ")");
		Harmony val = new Harmony("org.lordashes.plugins.diceprotocolextension");
		val.PatchAll();
	}

	public static string NormalizeDiceExpression(string input)
	{
		if (string.IsNullOrWhiteSpace(input))
		{
			return string.Empty;
		}
		string[] array = input.Split(new string[1] { "/" }, StringSplitOptions.RemoveEmptyEntries);
		RegexOptions options = RegexOptions.IgnoreCase;
		for (int i = 0; i < array.Length; i++)
		{
			string text = array[i].Trim().Replace(" ", "");
			string text2 = "";
			string input2 = text;
			int num = text.IndexOf(':');
			if (num != -1)
			{
				text2 = text.Substring(0, num + 1);
				input2 = text.Substring(num + 1);
			}
			MatchCollection matchCollection = Regex.Matches(input2, "[+-]?\\d+D(4|6|8|10|12|20)|[+-]?\\d+", options);
			if (matchCollection.Count == 0)
			{
				array[i] = text;
				continue;
			}
			string text3 = matchCollection[0].Value;
			bool flag = Regex.IsMatch(matchCollection[0].Value, "\\d+D(4|6|8|10|12|20)", options);
			for (int j = 1; j < matchCollection.Count; j++)
			{
				string value = matchCollection[j].Value;
				bool flag2 = Regex.IsMatch(value, "\\d+D(4|6|8|10|12|20)", options);
				string text4 = "";
				if (flag && flag2)
				{
					text4 = "+0+";
				}
				else if (!flag && !flag2)
				{
					text4 = "+0D20+";
				}
				string text5 = ((value.StartsWith("+") || value.StartsWith("-")) ? "" : "+");
				text3 = text3 + text4 + text5 + value;
				flag = flag2;
			}
			array[i] = text2 + text3.Replace("++", "+").Replace("+-", "-").Replace("-+", "-")
				.Replace("--", "+");
		}
		return string.Join("/", array);
	}
}