Decompiled source of BepInEx Console Extensions v2.0.0

BCE.dll

Decompiled a year 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 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.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("BCE")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Bepinex Console Extensions")]
[assembly: AssemblyFileVersion("2.0.0.0")]
[assembly: AssemblyInformationalVersion("2.0")]
[assembly: AssemblyProduct("BCE")]
[assembly: AssemblyTitle("BCE")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("2.0.0.0")]
[module: UnverifiableCode]
namespace BCE;

[BepInPlugin("space.customizing.console", "Console Extensions", "1.1.0.0")]
public class console : BaseUnityPlugin
{
	public static DiskLogListener DiskLogListener;

	public static void Init()
	{
		//IL_0040: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: Expected O, but got Unknown
		if (DiskLogListener != null)
		{
			return;
		}
		foreach (ILogListener listener in Logger.Listeners)
		{
			if (((object)listener).GetType() == typeof(DiskLogListener))
			{
				DiskLogListener = (DiskLogListener)listener;
			}
		}
	}

	public static void WriteDisk(string text)
	{
		DiskLogListener diskLogListener = DiskLogListener;
		if (diskLogListener != null)
		{
			diskLogListener.LogWriter.Write(text);
		}
	}

	public static void WriteLineDisk(string text)
	{
		DiskLogListener diskLogListener = DiskLogListener;
		if (diskLogListener != null)
		{
			diskLogListener.LogWriter.WriteLine(text);
		}
	}

	public static void WriteLine(string text, ConsoleColor consoleColor)
	{
		Init();
		ConsoleManager.SetConsoleColor(consoleColor);
		ConsoleManager.StandardOutStream.WriteLine(text);
		WriteLineDisk(text);
	}

	public static void Write(string text, ConsoleColor consoleColor)
	{
		Init();
		ConsoleManager.SetConsoleColor(consoleColor);
		ConsoleManager.StandardOutStream.Write(text);
		WriteDisk(text);
	}
}
public static class MatcherExtensions
{
	public static void LogILPre(this CodeMatcher _matcher, int lines = 1, int pre = 5, int post = 5, [CallerLineNumber] int lineNumber = 0)
	{
		_DumpMatcher(_matcher, lines, pre, post, lineNumber, 1, GetCallerMethod());
	}

	public static void LogILPost(this CodeMatcher _matcher, int lines = 1, int pre = 5, int post = 5, [CallerLineNumber] int lineNumber = 0)
	{
		_DumpMatcher(_matcher, lines, pre, post, lineNumber, 2, GetCallerMethod());
	}

	public static void LogIL(this CodeMatcher _matcher, int lines = 1, int pre = 5, int post = 5, [CallerLineNumber] int lineNumber = 0)
	{
		_DumpMatcher(_matcher, lines, pre, post, lineNumber, 0, GetCallerMethod());
	}

	public static void Log(this IEnumerable<CodeInstruction> instructions, string method, int start = 0, int count = 0, [CallerLineNumber] int lineNumber = 0)
	{
		List<CodeInstruction> list = instructions.ToList();
		string text = $"IL for {method} - {GetCallerMethod()} on line {lineNumber}";
		int num = Mathf.Max(text.Length + 2, 120);
		int num2 = num - text.Length;
		num2 /= 2;
		if (list.Count == 0)
		{
			console.WriteLine($"Instructions Log: List is empty | {GetCallerMethod()} on line {lineNumber}", ConsoleColor.Red);
		}
		if (count == 0)
		{
			count = list.Count;
		}
		if (start > list.Count)
		{
			start = 0;
		}
		for (int i = start; i < count + start && i < list.Count; i++)
		{
			CodeInstruction val = list[i];
			string arg = $"{val.opcode,-10}";
			string arg2 = $"{val.operand,-60}";
			console.Write($"  {i}  {arg} {arg2}", ConsoleColor.DarkGray);
			console.WriteLine($" // {val.operand?.GetType()}", ConsoleColor.DarkGreen);
		}
	}

	public static void _DumpMatcher(CodeMatcher _matcher, int lines = 1, int pre = 5, int post = 5, [CallerLineNumber] int lineNumber = 0, int type = 0, string callerMethod = "")
	{
		if (callerMethod == "")
		{
			callerMethod = GetCallerMethod();
		}
		string text = string.Format("{0}Transpile IL for {1} on line {2}", type switch
		{
			2 => "Post", 
			1 => "Pre", 
			_ => "", 
		}, callerMethod, lineNumber);
		int num = Mathf.Max(text.Length + 2, 120);
		int num2 = num - text.Length;
		num2 /= 2;
		LogTop(num);
		Console.WriteLine(string.Format("{0," + num2 + "}{1," + num2 + "}", "", text), ConsoleColor.DarkCyan);
		CodeMatcher val = _matcher.Clone();
		int num3 = lines + pre;
		while (val.Pos > 0 && num3 > 1)
		{
			val.Advance(-1);
			num3--;
		}
		num3 = pre;
		while (val.Remaining > 0 && num3 > 0)
		{
			CodeInstruction instruction = val.Instruction;
			string arg = $"{instruction.opcode,-10}";
			string arg2 = $"{instruction.operand,-50}";
			console.Write($"  {val.Pos}  {arg} {arg2}", ConsoleColor.DarkGray);
			console.WriteLine($" // {instruction.operand?.GetType()}", ConsoleColor.DarkGreen);
			val.Advance(1);
			num3--;
		}
		num3 = lines;
		while (val.Remaining > 0 && num3 > 0)
		{
			CodeInstruction instruction2 = val.Instruction;
			string arg3 = $"{instruction2.opcode,-10}";
			string arg4 = $"{instruction2.operand,-50}";
			console.Write($"  {val.Pos}  {arg3} {arg4}", ConsoleColor.White);
			console.WriteLine($" // {instruction2.operand?.GetType()}", ConsoleColor.DarkGreen);
			val.Advance(1);
			num3--;
		}
		num3 = post;
		while (val.Remaining > 0 && num3 > 0)
		{
			CodeInstruction instruction3 = val.Instruction;
			string arg5 = $"{instruction3.opcode,-10}";
			string arg6 = $"{instruction3.operand,-50}";
			console.Write($"  {val.Pos}  {arg5} {arg6}", ConsoleColor.DarkGray);
			console.WriteLine($" // {instruction3.operand?.GetType()}", ConsoleColor.DarkGreen);
			val.Advance(1);
			num3--;
		}
		LogBot(num);
	}

	public static void LogTop(int width = 80)
	{
		int num = width - 21;
		int num2 = Mathf.FloorToInt((float)(num / 2)) - 2;
		int spaces = num - num2 - 4;
		console.Write("\r\n╔═", ConsoleColor.White);
		console.Write("═", ConsoleColor.Gray);
		console.Write("═", ConsoleColor.DarkGray);
		console.Write(SpaceString(num2), ConsoleColor.Green);
		console.Write("*", ConsoleColor.Yellow);
		console.Write(".·", ConsoleColor.Gray);
		console.Write(":", ConsoleColor.DarkGray);
		console.Write("·.", ConsoleColor.Gray);
		console.Write("✧", ConsoleColor.DarkCyan);
		console.Write(" ✦ ", ConsoleColor.Cyan);
		console.Write("✧", ConsoleColor.DarkCyan);
		console.Write(".·", ConsoleColor.Gray);
		console.Write(":", ConsoleColor.DarkGray);
		console.Write("·.", ConsoleColor.Gray);
		console.Write("*", ConsoleColor.Yellow);
		console.Write(SpaceString(spaces), ConsoleColor.Green);
		console.Write("═", ConsoleColor.DarkGray);
		console.Write("═", ConsoleColor.Gray);
		console.Write("═╗\r\n", ConsoleColor.White);
	}

	public static void LogBot(int width = 80)
	{
		int num = width - 21;
		int num2 = Mathf.FloorToInt((float)(num / 2)) - 2;
		int spaces = num - num2 - 4;
		console.Write("╚═", ConsoleColor.White);
		console.Write("═", ConsoleColor.Gray);
		console.Write("═", ConsoleColor.DarkGray);
		console.Write(SpaceString(num2), ConsoleColor.Green);
		console.Write("*", ConsoleColor.Yellow);
		console.Write(".·", ConsoleColor.Gray);
		console.Write(":", ConsoleColor.DarkGray);
		console.Write("·.", ConsoleColor.Gray);
		console.Write("✧", ConsoleColor.DarkCyan);
		console.Write(" ✦ ", ConsoleColor.Cyan);
		console.Write("✧", ConsoleColor.DarkCyan);
		console.Write(".·", ConsoleColor.Gray);
		console.Write(":", ConsoleColor.DarkGray);
		console.Write("·.", ConsoleColor.Gray);
		console.Write("*", ConsoleColor.Yellow);
		console.Write(SpaceString(spaces), ConsoleColor.Green);
		console.Write("═", ConsoleColor.DarkGray);
		console.Write("═", ConsoleColor.Gray);
		console.WriteLine("═╝", ConsoleColor.White);
	}

	public static string SpaceString(int spaces)
	{
		return new string(' ', spaces);
	}

	public static string GetCallerMethod()
	{
		int num = 2;
		StackTrace stackTrace = new StackTrace();
		if (stackTrace.FrameCount <= num)
		{
			return "";
		}
		string text = stackTrace.GetFrame(num).GetMethod().Name;
		Type reflectedType = stackTrace.GetFrame(num).GetMethod().ReflectedType;
		if (reflectedType != null)
		{
			string text2 = reflectedType.ToString().Split(new char[1] { '.' })[^1];
			if (text2.Contains("+"))
			{
				text2 = text2.Split(new char[1] { '+' })[0];
			}
			if (text == ".ctor")
			{
				text = "<Constructor>";
			}
			else
			{
				if (text.Contains(">"))
				{
					text = text.Split(new char[1] { '>' })[0];
				}
				text = text.Replace("<", "");
			}
			return text2 + ":" + text;
		}
		return "ERROR GETTING CALLER";
	}
}