Decompiled source of MoreCommands v0.2.0

MoreCommands.dll

Decompiled 3 days ago
using System;
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 HarmonyLib;
using Microsoft.CodeAnalysis;
using MoreCommands.Accessors;
using MoreCommands.Commands;
using MoreCommands.Patches;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("MoreCommands")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("More console commands for White Knuckle")]
[assembly: AssemblyFileVersion("0.2.0.0")]
[assembly: AssemblyInformationalVersion("0.2.0+8f615d9a9ec018b15efc61cefc9a37f418407496")]
[assembly: AssemblyProduct("MoreCommands")]
[assembly: AssemblyTitle("MoreCommands")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.2.0.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 MoreCommands
{
	[BepInPlugin("shishyando.WK.MoreCommands", "MoreCommands", "0.2.0")]
	public class MoreCommandsPlugin : BaseUnityPlugin
	{
		public static ManualLogSource Logger;

		private readonly Harmony Harmony = new Harmony("shishyando.WK.MoreCommands");

		private void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
			CommandRegistry.InitializeCommands();
			Harmony.PatchAll(typeof(ENT_Player_Patcher));
			Logger.LogInfo((object)"shishyando.WK.MoreCommands is loaded");
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "shishyando.WK.MoreCommands";

		public const string PLUGIN_NAME = "MoreCommands";

		public const string PLUGIN_VERSION = "0.2.0";
	}
}
namespace MoreCommands.Patches
{
	[HarmonyPatch(typeof(ENT_Player), "CreateCommands")]
	public static class ENT_Player_Patcher
	{
		public static bool exploreActive;

		[HarmonyPostfix]
		public static void AddMorePlayerCommands(ENT_Player __instance)
		{
			foreach (ICommand item in CommandRegistry.GetCommandsByTag(CommandTag.Player))
			{
				CommandConsole.AddCommand(item.Cmd, item.GetCallback(), false);
			}
		}
	}
}
namespace MoreCommands.Commands
{
	public interface ICommand
	{
		string Cmd { get; }

		CommandTag Tag { get; }

		bool Enabled { get; set; }

		Action<string[]> GetCallback();

		void UpdateEnabled(string[] args);

		string[] WhenEnabled();

		string[] WhenDisabled();
	}
	public abstract class Command<T> : ICommand where T : Command<T>
	{
		public abstract string Cmd { get; }

		public abstract CommandTag Tag { get; }

		public bool Enabled { get; set; }

		public abstract Action<string[]> GetCallback();

		public void UpdateEnabled(string[] args)
		{
			bool result;
			if (args.Length == 0)
			{
				Enabled = !Enabled;
			}
			else if (!bool.TryParse(args[0], out result))
			{
				MoreCommandsPlugin.Logger.LogInfo((object)("Unable to parse `" + string.Join(" ", args) + "`, arg needs to be a boolean (true/false/0/1)."));
			}
			else
			{
				Enabled = result;
			}
		}

		public string[] WhenEnabled()
		{
			return new string[1] { Enabled.ToString().ToLower() };
		}

		public string[] WhenDisabled()
		{
			return new string[1] { (!Enabled).ToString().ToLower() };
		}
	}
	public enum CommandTag
	{
		Player
	}
	public sealed class ExploreCommand : Command<ExploreCommand>
	{
		public override string Cmd => "explore";

		public override CommandTag Tag => CommandTag.Player;

		public override Action<string[]> GetCallback()
		{
			return delegate(string[] args)
			{
				UpdateEnabled(args);
				if (base.Enabled)
				{
					CommandConsoleAccessor.EnsureCheatsAreEnabld();
				}
				ENT_Player playerObject = ENT_Player.playerObject;
				if (playerObject != null)
				{
					playerObject.SetGodMode(base.Enabled);
				}
				if (playerObject != null)
				{
					playerObject.Noclip(WhenEnabled());
				}
				if (playerObject != null)
				{
					playerObject.InfiniteStaminaCommand(WhenEnabled());
				}
				FXManager.Fullbright(WhenEnabled());
				DEN_DeathFloor instance = DEN_DeathFloor.instance;
				if (instance != null)
				{
					instance.DeathGooToggle(WhenDisabled());
				}
			};
		}
	}
	public sealed class FreerunCommand : Command<FreerunCommand>
	{
		public override string Cmd => "freerun";

		public override CommandTag Tag => CommandTag.Player;

		public override Action<string[]> GetCallback()
		{
			return delegate(string[] args)
			{
				UpdateEnabled(args);
				if (base.Enabled)
				{
					CommandConsoleAccessor.EnsureCheatsAreEnabld();
				}
				ENT_Player playerObject = ENT_Player.playerObject;
				if (playerObject != null)
				{
					playerObject.SetGodMode(base.Enabled);
				}
				if (playerObject != null)
				{
					playerObject.InfiniteStaminaCommand(WhenEnabled());
				}
				FXManager.Fullbright(WhenEnabled());
				DEN_DeathFloor instance = DEN_DeathFloor.instance;
				if (instance != null)
				{
					instance.DeathGooToggle(WhenDisabled());
				}
			};
		}
	}
	public static class CommandRegistry
	{
		private static readonly List<ICommand> _registeredCommands = new List<ICommand>();

		private static bool _initialized = false;

		public static void InitializeCommands()
		{
			if (_initialized)
			{
				return;
			}
			Assembly assembly = typeof(CommandRegistry).Assembly;
			List<Type> list = (from t in assembly.GetTypes()
				where t.IsClass && !t.IsAbstract && !t.ContainsGenericParameters && InheritsFromGenericCommand(t)
				select t).ToList();
			foreach (Type item in list)
			{
				try
				{
					ICommand command = (ICommand)Activator.CreateInstance(item);
					Register(command);
					MoreCommandsPlugin.Logger.LogInfo((object)("Registered command: " + command.Cmd));
				}
				catch (Exception ex)
				{
					MoreCommandsPlugin.Logger.LogError((object)("Failed to register command " + item.FullName + ": " + ex.Message));
				}
			}
			_initialized = true;
		}

		private static bool InheritsFromGenericCommand(Type type)
		{
			Type type2 = type;
			while (type2 != null && type2 != typeof(object))
			{
				Type type3 = (type2.IsGenericType ? type2.GetGenericTypeDefinition() : type2);
				if (type3 == typeof(Command<>))
				{
					return true;
				}
				type2 = type2.BaseType;
			}
			return false;
		}

		public static void Register(ICommand command)
		{
			if (!_registeredCommands.Any((ICommand c) => c.GetType() == command.GetType()))
			{
				_registeredCommands.Add(command);
			}
		}

		public static List<ICommand> GetCommandsByTag(CommandTag tag)
		{
			return _registeredCommands.Where((ICommand c) => c.Tag == tag).ToList();
		}

		public static List<ICommand> GetAllCommands()
		{
			return _registeredCommands.ToList();
		}

		public static ICommand GetCommandByName(string cmdName)
		{
			return _registeredCommands.FirstOrDefault((ICommand c) => c.Cmd.Equals(cmdName, StringComparison.OrdinalIgnoreCase));
		}
	}
}
namespace MoreCommands.Accessors
{
	public static class CommandConsoleAccessor
	{
		private static readonly Action<CommandConsole, string[]> EnableCheatsRaw = AccessTools.MethodDelegate<Action<CommandConsole, string[]>>(AccessTools.Method(typeof(CommandConsole), "EnableCheatsCommand", new Type[1] { typeof(string[]) }, (Type[])null), (object)null, true);

		private static readonly Action<CommandConsole> CheatsEnabler = delegate(CommandConsole inst)
		{
			EnableCheatsRaw(inst, new string[1] { "true" });
		};

		public static void EnsureCheatsAreEnabld()
		{
			if (!CommandConsole.hasCheated)
			{
				CommandConsole instance = CommandConsole.instance;
				if ((Object)(object)instance == (Object)null)
				{
					MoreCommandsPlugin.Logger.LogWarning((object)"CommandConsoleAccessor::EnableCheats instance is null");
				}
				else
				{
					CheatsEnabler(CommandConsole.instance);
				}
			}
		}
	}
}