Decompiled source of CrimsonDice v2.0.2

CrimsonDice.dll

Decompiled a week ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using CrimsonDice.Services;
using CrimsonDice.Structs;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using ProjectM.Network;
using Unity.Mathematics;
using Unity.Transforms;
using VAMP;
using VAMP.Utilities;
using VampireCommandFramework;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("CrimsonDice")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Roll dice in local chat to resolve disputes!")]
[assembly: AssemblyFileVersion("2.0.2.0")]
[assembly: AssemblyInformationalVersion("2.0.2+1.Branch.master.Sha.b1d34c1c9937c1aa12414e8e11fd1038a168c6d2.b1d34c1c9937c1aa12414e8e11fd1038a168c6d2")]
[assembly: AssemblyProduct("CrimsonDice")]
[assembly: AssemblyTitle("CrimsonDice")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("2.0.2.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace CrimsonDice
{
	[BepInPlugin("CrimsonDice", "CrimsonDice", "2.0.2")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BasePlugin
	{
		private Harmony _harmony;

		public static Plugin Instance { get; private set; }

		public static Harmony Harmony => Instance._harmony;

		public static ManualLogSource LogInstance => ((BasePlugin)Instance).Log;

		public static Settings Settings { get; private set; }

		public static DiceService DiceService { get; private set; }

		public override void Load()
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Expected O, but got Unknown
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			//IL_007a: Expected O, but got Unknown
			Instance = this;
			Settings = default(Settings);
			Settings.InitConfig();
			ManualLogSource log = ((BasePlugin)this).Log;
			bool flag = default(bool);
			BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(27, 2, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Plugin ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("CrimsonDice");
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" version ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("2.0.2");
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" is loaded!");
			}
			log.LogInfo(val);
			_harmony = new Harmony("CrimsonDice");
			_harmony.PatchAll(Assembly.GetExecutingAssembly());
			CommandRegistry.RegisterAll();
			Events.OnCoreLoaded = (Action)Delegate.Combine(Events.OnCoreLoaded, new Action(Loaded));
		}

		private void Loaded()
		{
			DiceService = new DiceService();
		}

		public override bool Unload()
		{
			CommandRegistry.UnregisterAssembly();
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			return true;
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "CrimsonDice";

		public const string PLUGIN_NAME = "CrimsonDice";

		public const string PLUGIN_VERSION = "2.0.2";
	}
}
namespace CrimsonDice.Structs
{
	[StructLayout(LayoutKind.Sequential, Size = 1)]
	public readonly struct Settings
	{
		private static readonly List<string> OrderedSections = new List<string> { "Config" };

		public static ConfigEntry<bool> ToggleMod { get; private set; }

		public static ConfigEntry<bool> AllowInGlobal { get; private set; }

		public static ConfigEntry<string> ResultColor { get; private set; }

		public static void InitConfig()
		{
			ToggleMod = InitConfigEntry(OrderedSections[0], "Toggle", defaultValue: true, "If true the mod will be usable; otherwise it will be disabled.");
			ResultColor = InitConfigEntry(OrderedSections[0], "ResultColor", "#34C6EB", "The color displayed for the player's roll sum.");
			AllowInGlobal = InitConfigEntry(OrderedSections[0], "AllowInGlobal", defaultValue: false, "If set to true, people will be able to roll dice in Global chat.");
			ReorderConfigSections();
		}

		private static ConfigEntry<T> InitConfigEntry<T>(string section, string key, T defaultValue, string description)
		{
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			ConfigEntry<T> val = ((BasePlugin)Plugin.Instance).Config.Bind<T>(section, key, defaultValue, description);
			string text = Path.Combine(Paths.ConfigPath, "CrimsonDice.cfg");
			ConfigEntry<T> val2 = default(ConfigEntry<T>);
			if (File.Exists(text) && new ConfigFile(text, true).TryGetEntry<T>(section, key, ref val2))
			{
				val.Value = val2.Value;
			}
			return val;
		}

		private static void ReorderConfigSections()
		{
			string path = Path.Combine(Paths.ConfigPath, "CrimsonDice.cfg");
			if (!File.Exists(path))
			{
				return;
			}
			List<string> list = File.ReadAllLines(path).ToList();
			Dictionary<string, List<string>> dictionary = new Dictionary<string, List<string>>();
			string text = "";
			foreach (string item in list)
			{
				if (item.StartsWith("["))
				{
					text = item.Trim('[', ']');
					dictionary[text] = new List<string> { item };
				}
				else if (!string.IsNullOrWhiteSpace(text))
				{
					dictionary[text].Add(item);
				}
			}
			using StreamWriter streamWriter = new StreamWriter(path, append: false);
			foreach (string orderedSection in OrderedSections)
			{
				if (!dictionary.ContainsKey(orderedSection))
				{
					continue;
				}
				foreach (string item2 in dictionary[orderedSection])
				{
					streamWriter.WriteLine(item2);
				}
				streamWriter.WriteLine();
			}
		}
	}
}
namespace CrimsonDice.Services
{
	public class DiceService
	{
		public static List<int> ValidDice;

		public DiceService()
		{
			ValidDice = new List<int> { 4, 6, 8, 10, 12, 20, 100 };
		}

		public static bool IsValidDie(int die)
		{
			return ValidDice.Contains(die);
		}

		public static int RollDie(int die)
		{
			return new Random().Next(1, die + 1);
		}

		public static int RollDice(int die, int count)
		{
			int num = 0;
			for (int i = 0; i < count; i++)
			{
				num += RollDie(die);
			}
			return num;
		}

		public static bool ValidateHandOfDice(string hand, out int die, out int amount)
		{
			die = 0;
			amount = 0;
			if (string.IsNullOrEmpty(hand))
			{
				return false;
			}
			if (!hand.Contains('d'))
			{
				return false;
			}
			string[] array = hand.ToLower().Split('d');
			if (array.Length != 2)
			{
				return false;
			}
			if (!int.TryParse(array[0], out amount) || !int.TryParse(array[1], out die))
			{
				return false;
			}
			if (amount <= 0)
			{
				return false;
			}
			return IsValidDie(die);
		}
	}
}
namespace CrimsonDice.Commands
{
	[CommandGroup("dice", null)]
	internal static class DiceCommands
	{
		[Command("roll", "r", null, "Roll dice", null, false)]
		public static void Roll(ChatCommandContext ctx, string dies = "1d20")
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: 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_008e: 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_0108: Unknown result type (might be due to invalid IL or missing references)
			//IL_011c: Unknown result type (might be due to invalid IL or missing references)
			//IL_011e: Invalid comparison between Unknown and I4
			//IL_014c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0120: Unknown result type (might be due to invalid IL or missing references)
			//IL_0122: Invalid comparison between Unknown and I4
			//IL_0155: Unknown result type (might be due to invalid IL or missing references)
			//IL_012a: Unknown result type (might be due to invalid IL or missing references)
			//IL_012f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0134: Unknown result type (might be due to invalid IL or missing references)
			//IL_0138: Unknown result type (might be due to invalid IL or missing references)
			//IL_013d: 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_0141: Unknown result type (might be due to invalid IL or missing references)
			ChatMessageType type = ctx.Event.Type;
			if (((object)(ChatMessageType)(ref type)).Equals((object)(ChatMessageType)3))
			{
				ctx.Reply("Dice rolling is not allowed in Whispers.");
				return;
			}
			if (((object)(ChatMessageType)(ref type)).Equals((object)(ChatMessageType)0) && !Settings.AllowInGlobal.Value)
			{
				ctx.Reply("Dice rolling is disabled in Global chat.");
				return;
			}
			if (!DiceService.ValidateHandOfDice(dies, out var die, out var amount))
			{
				ctx.Reply("Invalid dice format. Use the format '{count}d{dieSides}' from the follow options: d4, d6, d8, d10, d12, d20, d100");
				return;
			}
			int value = DiceService.RollDice(die, amount);
			User user = ctx.User;
			string text = $"{user.CharacterName} rolled {amount}d{die} = <color={Settings.ResultColor.Value}>{value}</color>.";
			if ((int)type != 0)
			{
				text = "[{channel.ToString()}] " + text;
				if ((int)type != 2)
				{
					if ((int)type == 4)
					{
						LocalToWorld val = EntityUtil.Read<LocalToWorld>(ctx.Event.SenderCharacterEntity);
						float3 position = ((LocalToWorld)(ref val)).Position;
						ChatUtil.SystemSendLocal(user, position, text);
					}
				}
				else
				{
					ChatUtil.SystemSendTeam(user, text);
				}
				ChatUtil.SystemSendUser(user, $"You rolled <color={Settings.ResultColor.Value}>{value}</color>.");
			}
			else
			{
				ChatUtil.SystemSendAll(text);
			}
			ctx.Event.Cancel();
		}
	}
}