Decompiled source of WKConsoleGUI v0.0.1

plugins/WKConsoleGUI.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Newtonsoft.Json;
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.6", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("WKConsoleGUI")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+d7e584e12c39ab17ee65038547cf59730f1bbf0f")]
[assembly: AssemblyProduct("My first plugin")]
[assembly: AssemblyTitle("WKConsoleGUI")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.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 WKConsoleGUI
{
	[BepInPlugin("huoyan1231.WKconsoleGUI", "WKconsoleGUI", "0.0.0.1")]
	[BepInProcess("White Knuckle.exe")]
	public class Plugin : BaseUnityPlugin
	{
		public class CommandEntry
		{
			public string Label;

			public string Command;

			public string Description;

			public CommandEntry(string label, string command, string description)
			{
				Label = label;
				Command = command;
				Description = description;
			}
		}

		internal static ManualLogSource Logger;

		private ConfigEntry<KeyboardShortcut> toggleConsoleKey;

		private ConfigEntry<KeyboardShortcut> toggleGUIKey;

		private ConfigEntry<string> customCommandsJson;

		private ConfigEntry<float> guiScaleFactor;

		private float _pendingScaleFactor;

		private ConfigEntry<int> buttonsPerRow;

		private bool showGUI = false;

		private Rect windowRect = new Rect(100f, 100f, 500f, 550f);

		private GUIStyle tooltipStyle;

		private bool tooltipStyleInitialized = false;

		private Vector2 scrollPosition = Vector2.zero;

		private List<CommandEntry> commands = new List<CommandEntry>();

		private void Awake()
		{
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_00da: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e4: Expected O, but got Unknown
			//IL_010c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0116: Expected O, but got Unknown
			Logger = ((BaseUnityPlugin)this).Logger;
			Logger.LogInfo((object)"Plugin WKConsoleGUI is loaded!");
			toggleConsoleKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("General", "ToggleConsoleHotkey", new KeyboardShortcut((KeyCode)288, Array.Empty<KeyCode>()), "用于切换游戏内置控制台的快捷键。");
			toggleGUIKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("General", "ToggleGUIHotkey", new KeyboardShortcut((KeyCode)289, Array.Empty<KeyCode>()), "用于切换此Mod界面的快捷键。");
			customCommandsJson = ((BaseUnityPlugin)this).Config.Bind<string>("Commands", "CustomCommands", JsonConvert.SerializeObject((object)new List<CommandEntry>()), "自定义控制台命令列表 (JSON格式)。");
			LoadCommands();
			guiScaleFactor = ((BaseUnityPlugin)this).Config.Bind<float>("GUI", "ScaleFactor", 1f, new ConfigDescription("GUI 窗口的缩放比例。在高DPI屏幕上可以设置为1.5, 2.0等,以增大窗口尺寸。", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.5f, 3f), Array.Empty<object>()));
			buttonsPerRow = ((BaseUnityPlugin)this).Config.Bind<int>("GUI", "ButtonsPerRow", 3, new ConfigDescription("每行显示的命令按钮数量。", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 5), Array.Empty<object>()));
			_pendingScaleFactor = guiScaleFactor.Value;
		}

		private void Update()
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			KeyboardShortcut value = toggleConsoleKey.Value;
			if (((KeyboardShortcut)(ref value)).IsDown())
			{
				ManualLogSource logger = Logger;
				value = toggleConsoleKey.Value;
				logger.LogInfo((object)(((object)(KeyboardShortcut)(ref value)).ToString() + " pressed. Attempting to toggle console..."));
				ToggleConsoleViaReflection();
			}
			value = toggleGUIKey.Value;
			if (((KeyboardShortcut)(ref value)).IsDown())
			{
				ToggleConsoleViaReflection();
				showGUI = !showGUI;
			}
		}

		private void ToggleConsoleViaReflection()
		{
			Type type = AccessTools.TypeByName("CommandConsole");
			if (type == null)
			{
				Logger.LogWarning((object)"未找到 CommandConsole 类型!");
				return;
			}
			FieldInfo fieldInfo = AccessTools.Field(type, "instance");
			object value = fieldInfo.GetValue(null);
			if (value == null)
			{
				Logger.LogWarning((object)"CommandConsole.instance 为 null!");
				return;
			}
			AccessTools.Method(type, "ToggleConsole", (Type[])null, (Type[])null)?.Invoke(value, null);
			Logger.LogInfo((object)"调用 CommandConsole.ToggleConsole()");
		}

		private void OnGUI()
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b4: Invalid comparison between Unknown and I4
			//IL_0081: Unknown result type (might be due to invalid IL or missing references)
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: Expected O, but got Unknown
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: 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_00da: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f1: Expected O, but got Unknown
			//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f3: Unknown result type (might be due to invalid IL or missing references)
			//IL_0102: Unknown result type (might be due to invalid IL or missing references)
			//IL_0123: Unknown result type (might be due to invalid IL or missing references)
			//IL_014b: 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_0192: Unknown result type (might be due to invalid IL or missing references)
			//IL_0199: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_015e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0165: Unknown result type (might be due to invalid IL or missing references)
			Matrix4x4 matrix = GUI.matrix;
			float value = guiScaleFactor.Value;
			if (value > 0f && value != 1f)
			{
				Vector3 val = default(Vector3);
				((Vector3)(ref val))..ctor((float)(Screen.width / 2), (float)(Screen.height / 2), 0f);
				GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(value, value, 1f));
			}
			if (showGUI)
			{
				windowRect = GUI.Window(((object)this).GetHashCode(), windowRect, new WindowFunction(DrawWindow), "WKConsoleGUI");
			}
			GUI.matrix = matrix;
			if ((int)Event.current.type != 7 || string.IsNullOrEmpty(GUI.tooltip))
			{
				return;
			}
			Vector2 mousePosition = Event.current.mousePosition;
			Vector2 val2 = tooltipStyle.CalcSize(new GUIContent(GUI.tooltip));
			float num = mousePosition.x + 15f;
			float num2 = mousePosition.y + 15f;
			float num3 = Screen.width;
			float num4 = Screen.height;
			if (num + val2.x > num3)
			{
				num = num3 - val2.x - 5f;
			}
			if (num2 + val2.y > num4)
			{
				num2 = mousePosition.y - val2.y - 15f;
				if (num2 < 0f)
				{
					num2 = 0f;
				}
			}
			Rect val3 = default(Rect);
			((Rect)(ref val3))..ctor(num, num2, val2.x, val2.y);
			GUI.Label(val3, GUI.tooltip, tooltipStyle);
		}

		private void DrawWindow(int windowID)
		{
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
			//IL_0100: Expected O, but got Unknown
			//IL_0129: Unknown result type (might be due to invalid IL or missing references)
			//IL_012e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0130: Unknown result type (might be due to invalid IL or missing references)
			//IL_013a: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ec: Unknown result type (might be due to invalid IL or missing references)
			GUILayout.BeginVertical(Array.Empty<GUILayoutOption>());
			scrollPosition = GUILayout.BeginScrollView(scrollPosition, (GUILayoutOption[])(object)new GUILayoutOption[2]
			{
				GUILayout.Width(((Rect)(ref windowRect)).width),
				GUILayout.Height(((Rect)(ref windowRect)).height - 150f)
			});
			int num = 0;
			int value = buttonsPerRow.Value;
			float num2 = ((Rect)(ref windowRect)).width - 25f;
			float num3 = 5f;
			float num4 = (num2 - (float)(value - 1) * num3) / (float)value;
			if (num4 < 50f)
			{
				num4 = 50f;
			}
			foreach (CommandEntry command in commands)
			{
				if (num % value == 0)
				{
					if (num > 0)
					{
						GUILayout.EndHorizontal();
						GUILayout.Space(5f);
					}
					GUILayout.BeginHorizontal(Array.Empty<GUILayoutOption>());
				}
				GUIContent val = new GUIContent(command.Label, command.Description);
				Rect rect = GUILayoutUtility.GetRect(val, GUI.skin.box, (GUILayoutOption[])(object)new GUILayoutOption[2]
				{
					GUILayout.Width(num4),
					GUILayout.Height(40f)
				});
				GUI.Box(rect, val);
				if (GUI.Button(rect, GUIContent.none, GUIStyle.none))
				{
					ExecuteCommand(command.Command);
				}
				num++;
			}
			if (num > 0)
			{
				GUILayout.EndHorizontal();
			}
			GUILayout.EndScrollView();
			GUILayout.Space(10f);
			GUILayout.Label($"GUI 缩放 ({_pendingScaleFactor:F1}x)", Array.Empty<GUILayoutOption>());
			_pendingScaleFactor = GUILayout.HorizontalSlider(_pendingScaleFactor, 0.5f, 3f, Array.Empty<GUILayoutOption>());
			if (GUILayout.Button("应用缩放", Array.Empty<GUILayoutOption>()))
			{
				if (guiScaleFactor.Value != _pendingScaleFactor)
				{
					guiScaleFactor.Value = _pendingScaleFactor;
					Logger.LogInfo((object)$"GUI 缩放已应用: {guiScaleFactor.Value:F1}x");
				}
				else
				{
					Logger.LogInfo((object)"缩放值未改变,无需应用。");
				}
			}
			GUILayout.Space(10f);
			if (GUILayout.Button("重载配置", Array.Empty<GUILayoutOption>()))
			{
				Logger.LogInfo((object)"尝试重载配置...");
				((BaseUnityPlugin)this).Config.Reload();
				_pendingScaleFactor = guiScaleFactor.Value;
				LoadCommands();
				Logger.LogInfo((object)"配置已重载!");
			}
			GUI.DragWindow(new Rect(0f, 0f, ((Rect)(ref windowRect)).width, ((Rect)(ref windowRect)).height));
			GUILayout.EndVertical();
		}

		private void ExecuteCommand(string commandText)
		{
			Type type = AccessTools.TypeByName("CommandConsole");
			FieldInfo fieldInfo = AccessTools.Field(type, "instance");
			object value = fieldInfo.GetValue(null);
			if (value == null)
			{
				Logger.LogWarning((object)"CommandConsole.instance 为 null!");
				return;
			}
			AccessTools.Method(type, "ExecuteCommand", (Type[])null, (Type[])null)?.Invoke(value, new object[1] { commandText });
			Logger.LogInfo((object)("执行命令:" + commandText));
		}

		private void LoadCommands()
		{
			//IL_00a7: Expected O, but got Unknown
			commands.Clear();
			string text = Path.Combine(Paths.ConfigPath, "GUICommandsButtons.json");
			if (File.Exists(text))
			{
				try
				{
					string text2 = File.ReadAllText(text);
					List<CommandEntry> list = JsonConvert.DeserializeObject<List<CommandEntry>>(text2);
					if (list != null)
					{
						commands.AddRange(list);
						Logger.LogInfo((object)$"成功从 '{text}' 加载 {commands.Count} 条命令。");
					}
					else
					{
						Logger.LogWarning((object)("JSON 文件 '" + text + "' 内容为空或格式不正确。将加载默认命令。"));
						LoadDefaultCommands();
					}
					return;
				}
				catch (JsonException val)
				{
					JsonException val2 = val;
					Logger.LogError((object)("解析 JSON 文件 '" + text + "' 失败: " + ((Exception)(object)val2).Message + "。请检查文件格式。将加载默认命令。"));
					Logger.LogDebug((object)("JSON 解析错误详情: " + ((object)val2).ToString()));
					LoadDefaultCommands();
					return;
				}
				catch (IOException ex)
				{
					Logger.LogError((object)("读取文件 '" + text + "' 失败: " + ex.Message + "。将加载默认命令。"));
					LoadDefaultCommands();
					return;
				}
				catch (Exception ex2)
				{
					Logger.LogError((object)("加载命令时发生未知错误: " + ex2.Message + "。将加载默认命令。"));
					LoadDefaultCommands();
					return;
				}
			}
			Logger.LogWarning((object)("未找到命令文件 '" + text + "'。将加载默认命令,并尝试创建示例文件。"));
			LoadDefaultCommands();
			SaveDefaultCommandsToFile(text);
		}

		private void LoadDefaultCommands()
		{
			commands.Clear();
			commands.Add(new CommandEntry("开启作弊", "cheats", "切换作弊,要开,不开下面用不了,开了禁用进度和成就防止成为世1开"));
			commands.Add(new CommandEntry("推动(?", "addforcetoplayer 1, 1, 1", ""));
			commands.Add(new CommandEntry("全亮", "fullbright", "███████"));
			commands.Add(new CommandEntry("获取种子", "getgenerationseed", "目前还不能种地(大概"));
			commands.Add(new CommandEntry("无敌", "godmode", "但是还是得爬"));
			commands.Add(new CommandEntry("无限体力", "infinitestamina", "我编不出来了"));
			commands.Add(new CommandEntry("默认测试1", "test_default 1", "这是从代码加载的默认命令1"));
			commands.Add(new CommandEntry("默认测试2", "test_default 2", "这是从代码加载的默认命令2"));
		}

		private void SaveDefaultCommandsToFile(string filePath)
		{
			try
			{
				List<CommandEntry> list = new List<CommandEntry>
				{
					new CommandEntry("开启作弊", "cheats", "切换作弊,要开,不开下面用不了,开了禁用进度和成就防止成为世1开"),
					new CommandEntry("推动(?", "addforcetoplayer 1, 1, 1", ""),
					new CommandEntry("全亮", "fullbright", "███████"),
					new CommandEntry("获取种子", "getgenerationseed", "目前还不能种地(大概"),
					new CommandEntry("无敌", "godmode", "但是还是得爬"),
					new CommandEntry("无限体力", "infinitestamina", "我编不出来了")
				};
				string contents = JsonConvert.SerializeObject((object)list, (Formatting)1);
				File.WriteAllText(filePath, contents);
				Logger.LogInfo((object)("已在 '" + filePath + "' 创建示例命令文件。"));
			}
			catch (Exception ex)
			{
				Logger.LogError((object)("创建示例命令文件失败: " + ex.Message));
			}
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "WKConsoleGUI";

		public const string PLUGIN_NAME = "My first plugin";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}