Decompiled source of R2API CommandHelper v1.0.2

plugins/R2API.CommandHelper/R2API.CommandHelper.dll

Decompiled 3 months 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.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using On.RoR2;
using R2API.AutoVersionGen;
using RoR2;
using RoR2.ConVar;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("R2API.CommandHelper")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.2.0")]
[assembly: AssemblyInformationalVersion("1.0.2+733d42438524ee7fa178e1f85d00f8b8f9772b6a")]
[assembly: AssemblyProduct("R2API.CommandHelper")]
[assembly: AssemblyTitle("R2API.CommandHelper")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.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.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[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 System.Diagnostics.CodeAnalysis
{
	[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
	[ExcludeFromCodeCoverage]
	[DebuggerNonUserCode]
	internal sealed class MemberNotNullAttribute : Attribute
	{
		public string[] Members { get; }

		public MemberNotNullAttribute(string member)
		{
			Members = new string[1] { member };
		}

		public MemberNotNullAttribute(params string[] members)
		{
			Members = members;
		}
	}
	[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
	[ExcludeFromCodeCoverage]
	[DebuggerNonUserCode]
	internal sealed class MemberNotNullWhenAttribute : Attribute
	{
		public bool ReturnValue { get; }

		public string[] Members { get; }

		public MemberNotNullWhenAttribute(bool returnValue, string member)
		{
			ReturnValue = returnValue;
			Members = new string[1] { member };
		}

		public MemberNotNullWhenAttribute(bool returnValue, params string[] members)
		{
			ReturnValue = returnValue;
			Members = members;
		}
	}
}
namespace R2API.AutoVersionGen
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	internal class AutoVersionAttribute : Attribute
	{
	}
}
namespace R2API.Utils
{
	[Obsolete("Add [assembly: HG.Reflection.SearchableAttribute.OptInAttribute] to your assembly instead")]
	[AutoVersion]
	public class CommandHelper
	{
		[CompilerGenerated]
		private static class <>O
		{
			public static hook_InitConVarsCoroutine <0>__ConsoleReady;
		}

		public const string PluginGUID = "com.bepis.r2api.commandhelper";

		public const string PluginName = "R2API.CommandHelper";

		private static readonly Queue<Assembly> Assemblies = new Queue<Assembly>();

		private static Console _console;

		private static bool _hooksEnabled = false;

		public const string PluginVersion = "1.0.2";

		[Obsolete("All submodules are automatically loaded and this property is now unused")]
		public static bool Loaded => true;

		public static void AddToConsoleWhenReady()
		{
			SetHooks();
			Assembly callingAssembly = Assembly.GetCallingAssembly();
			Assemblies.Enqueue(callingAssembly);
			HandleCommandsConvars();
		}

		internal static void SetHooks()
		{
			//IL_0018: 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_0023: Expected O, but got Unknown
			if (!_hooksEnabled)
			{
				object obj = <>O.<0>__ConsoleReady;
				if (obj == null)
				{
					hook_InitConVarsCoroutine val = ConsoleReady;
					<>O.<0>__ConsoleReady = val;
					obj = (object)val;
				}
				Console.InitConVarsCoroutine += (hook_InitConVarsCoroutine)obj;
				_hooksEnabled = true;
			}
		}

		internal static void UnsetHooks()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Expected O, but got Unknown
			object obj = <>O.<0>__ConsoleReady;
			if (obj == null)
			{
				hook_InitConVarsCoroutine val = ConsoleReady;
				<>O.<0>__ConsoleReady = val;
				obj = (object)val;
			}
			Console.InitConVarsCoroutine -= (hook_InitConVarsCoroutine)obj;
			_hooksEnabled = false;
		}

		private static IEnumerator ConsoleReady(orig_InitConVarsCoroutine orig, Console self)
		{
			yield return orig.Invoke(self);
			_console = self;
			HandleCommandsConvars();
		}

		private static void HandleCommandsConvars()
		{
			if (!((Object)(object)_console == (Object)null))
			{
				while (Assemblies.Count > 0)
				{
					Assembly assembly = Assemblies.Dequeue();
					RegisterCommands(assembly);
					RegisterConVars(assembly);
				}
			}
		}

		private static void RegisterCommands(Assembly assembly)
		{
			//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: 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_00d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f0: Expected O, but got Unknown
			//IL_00f2: Expected O, but got Unknown
			if (assembly == null)
			{
				return;
			}
			Type[] source = default(Type[]);
			Reflection.GetTypesSafe(assembly, ref source);
			try
			{
				Dictionary<string, ConCommand> concommandCatalog = _console.concommandCatalog;
				foreach (MethodInfo item in source.SelectMany((Type t) => from m in t.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
					where ((MemberInfo)m).GetCustomAttribute<ConCommandAttribute>() != null
					select m))
				{
					if (!item.IsStatic)
					{
						CommandHelperPlugin.Logger.LogError((object)("ConCommand defined as " + item.Name + " in " + assembly.FullName + " could not be registered. ConCommands must be static methods."));
						continue;
					}
					foreach (ConCommandAttribute customAttribute in ((MemberInfo)item).GetCustomAttributes<ConCommandAttribute>())
					{
						ConCommand value = new ConCommand
						{
							flags = customAttribute.flags,
							helpText = customAttribute.helpText,
							action = (ConCommandDelegate)Delegate.CreateDelegate(typeof(ConCommandDelegate), item)
						};
						concommandCatalog[customAttribute.commandName.ToLower()] = value;
					}
				}
			}
			catch (Exception arg)
			{
				CommandHelperPlugin.Logger.LogError((object)string.Format("{0} failed to scan the assembly called {1}. Exception : {2}", "CommandHelper", assembly.FullName, arg));
			}
		}

		private static void RegisterConVars(Assembly assembly)
		{
			//IL_011f: 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_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_007c: Expected O, but got Unknown
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_008f: Expected O, but got Unknown
			if (assembly == null)
			{
				return;
			}
			Type[] array = default(Type[]);
			Reflection.GetTypesSafe(assembly, ref array);
			try
			{
				List<BaseConVar> list = new List<BaseConVar>();
				Type[] array2 = array;
				foreach (Type type in array2)
				{
					FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
					foreach (FieldInfo fieldInfo in fields)
					{
						if (fieldInfo.FieldType.IsSubclassOf(typeof(BaseConVar)))
						{
							if (fieldInfo.IsStatic)
							{
								_console.RegisterConVarInternal((BaseConVar)fieldInfo.GetValue(null));
								list.Add((BaseConVar)fieldInfo.GetValue(null));
							}
							else if (type.GetCustomAttribute<CompilerGeneratedAttribute>() == null)
							{
								CommandHelperPlugin.Logger.LogError((object)("ConVar defined as " + type.Name + " in " + assembly.FullName + ". " + fieldInfo.Name + " could not be registered. ConVars must be static fields."));
							}
						}
					}
				}
				foreach (BaseConVar item in list)
				{
					if ((item.flags & 0x10) != 0)
					{
						item.defaultValue = item.GetString();
					}
					else if (item.defaultValue != null)
					{
						item.AttemptSetString(item.defaultValue);
					}
				}
			}
			catch (Exception arg)
			{
				CommandHelperPlugin.Logger.LogError((object)string.Format("{0} failed to scan the assembly called {1}. Exception : {2}", "CommandHelper", assembly.FullName, arg));
			}
		}
	}
	[BepInPlugin("com.bepis.r2api.commandhelper", "R2API.CommandHelper", "1.0.2")]
	public sealed class CommandHelperPlugin : BaseUnityPlugin
	{
		internal static ManualLogSource Logger { get; set; }

		private void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
		}

		private void OnDestroy()
		{
			CommandHelper.UnsetHooks();
		}
	}
}