Decompiled source of FixVstr v1.0.0

FixVstr.dll

Decompiled 11 months 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.Permissions;
using System.Text;
using BepInEx;
using BepInEx.Logging;
using HG.Reflection;
using IL.RoR2;
using Microsoft.CodeAnalysis;
using Mono.Cecil.Cil;
using MonoMod.Cil;
using R2API.Utils;
using RoR2;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: OptIn]
[assembly: TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName = ".NET Standard 2.0")]
[assembly: AssemblyCompany("Chinchi")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Fix and enhance the vstr functionality (command aliases).")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("FixVstr")]
[assembly: AssemblyTitle("FixVstr")]
[assembly: AssemblyMetadata("RepositoryUrl", "https://github.com/schinchi/FixVstr")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[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 FixVstr
{
	internal class Commands
	{
		[ConCommand(/*Could not decode attribute arguments.*/)]
		public static void CCClearVstr(ConCommandArgs _)
		{
			Console.instance.vstrs.Clear();
		}

		[ConCommand(/*Could not decode attribute arguments.*/)]
		public static void CCDelVstr(ConCommandArgs args)
		{
			((ConCommandArgs)(ref args)).CheckArgumentCount(1);
			Console.instance.vstrs.Remove(((ConCommandArgs)(ref args))[0]);
		}

		[ConCommand(/*Could not decode attribute arguments.*/)]
		public static void CCGetVstr(ConCommandArgs args)
		{
			if (((ConCommandArgs)(ref args)).Count > 0)
			{
				if (Console.instance.vstrs.TryGetValue(((ConCommandArgs)(ref args))[0], out var value))
				{
					Debug.Log((object)(((ConCommandArgs)(ref args))[0] + " = " + value));
				}
				else
				{
					Debug.Log((object)"vstr not found");
				}
				return;
			}
			Dictionary<string, string> vstrs = Console.instance.vstrs;
			List<string> list = vstrs.Keys.ToList();
			list.Sort();
			StringBuilder stringBuilder = new StringBuilder();
			foreach (string item in list)
			{
				stringBuilder.AppendLine(item + " = " + vstrs[item]);
			}
			Debug.Log((object)stringBuilder.ToString().TrimEnd(new char[1] { '\n' }));
		}
	}
	[BepInPlugin("Chinchi.FixVstr", "FixVstr", "1.0.0")]
	[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
	public class FixVstr : BaseUnityPlugin
	{
		public const string PluginGUID = "Chinchi.FixVstr";

		public const string PluginAuthor = "Chinchi";

		public const string PluginName = "FixVstr";

		public const string PluginVersion = "1.0.0";

		internal static ManualLogSource Logger;

		public void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
			Hooks.Init();
		}
	}
	internal class Hooks
	{
		[CompilerGenerated]
		private static class <>O
		{
			public static Manipulator <0>__Console_CCSetVstr;

			public static Manipulator <1>__Console_SubmitCmd_CmdSender_string_bool;
		}

		public static void Init()
		{
			//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
			//IL_0030: 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_003b: Expected O, but got Unknown
			object obj = <>O.<0>__Console_CCSetVstr;
			if (obj == null)
			{
				Manipulator val = Console_CCSetVstr;
				<>O.<0>__Console_CCSetVstr = val;
				obj = (object)val;
			}
			Console.CCSetVstr += (Manipulator)obj;
			object obj2 = <>O.<1>__Console_SubmitCmd_CmdSender_string_bool;
			if (obj2 == null)
			{
				Manipulator val2 = Console_SubmitCmd_CmdSender_string_bool;
				<>O.<1>__Console_SubmitCmd_CmdSender_string_bool = val2;
				obj2 = (object)val2;
			}
			Console.SubmitCmd_CmdSender_string_bool += (Manipulator)obj2;
		}

		private static void Console_CCSetVstr(ILContext il)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Expected O, but got Unknown
			//IL_013f: Unknown result type (might be due to invalid IL or missing references)
			ILCursor val = new ILCursor(il);
			if (!val.TryGotoNext(new Func<Instruction, bool>[8]
			{
				(Instruction x) => ILPatternMatchingExt.MatchCall<Console>(x, "get_instance"),
				(Instruction x) => ILPatternMatchingExt.MatchLdfld<Console>(x, "vstrs"),
				(Instruction x) => ILPatternMatchingExt.MatchLdarga(x, 0),
				(Instruction x) => ILPatternMatchingExt.MatchLdcI4(x, 0),
				(Instruction x) => ILPatternMatchingExt.MatchCall<ConCommandArgs>(x, "get_Item"),
				(Instruction x) => ILPatternMatchingExt.MatchLdarga(x, 0),
				(Instruction x) => ILPatternMatchingExt.MatchLdcI4(x, 1),
				(Instruction x) => ILPatternMatchingExt.MatchCall<ConCommandArgs>(x, "get_Item")
			}))
			{
				FixVstr.Logger.LogError((object)"Failed to patch Console.CCSetVstr");
				return;
			}
			val.RemoveRange(9);
			val.Emit(OpCodes.Ldarg_0);
			val.EmitDelegate<Action<ConCommandArgs>>((Action<ConCommandArgs>)delegate(ConCommandArgs args)
			{
				string key = ((ConCommandArgs)(ref args))[0];
				if (Console.instance.concommandCatalog.ContainsKey(key) || Console.instance.allConVars.ContainsKey(key))
				{
					Debug.Log((object)"Setting an already registered command/cvar as an alias is prohibited.");
				}
				else
				{
					Console.instance.vstrs[key] = ((ConCommandArgs)(ref args))[1];
				}
			});
		}

		private static void Console_SubmitCmd_CmdSender_string_bool(ILContext il)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Expected O, but got Unknown
			ILCursor val = new ILCursor(il);
			if (!val.TryGotoNext((MoveType)2, new Func<Instruction, bool>[1]
			{
				(Instruction x) => ILPatternMatchingExt.MatchCallvirt(x, typeof(Lexer), "GetTokens")
			}))
			{
				FixVstr.Logger.LogError((object)"Failed to patch Console.SubmitCmd(CmdSender, string, bool)");
				return;
			}
			val.EmitDelegate<Func<Queue<string>, Queue<string>>>((Func<Queue<string>, Queue<string>>)delegate(Queue<string> tokens)
			{
				//IL_0032: Unknown result type (might be due to invalid IL or missing references)
				Queue<string> queue = new Queue<string>();
				string text = "";
				while (tokens.Count > 0)
				{
					string text2 = tokens.Dequeue();
					if (!IsVstrRelatedCommand(text) && Console.instance.vstrs.TryGetValue(text2, out var value))
					{
						Queue<string> tokens2 = new Lexer(value).GetTokens();
						while (tokens2.Count > 1)
						{
							text = tokens2.Dequeue();
							queue.Enqueue(text);
						}
					}
					else
					{
						text = text2;
						queue.Enqueue(text2);
					}
				}
				return queue;
			});
		}

		private static bool IsVstrRelatedCommand(string token)
		{
			if (!token.Equals("del_vstr") && !token.Equals("get_vstr"))
			{
				return token.Equals("set_vstr");
			}
			return true;
		}
	}
}