Decompiled source of VoiceDebug v1.2.0

BepInEx/plugins/AntsLab.VoiceDebug/AntsLab.VoiceDebug.dll

Decompiled 2 months ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
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: AssemblyTitle("VoiceDebug")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("VoiceDebug")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("deb12e99-f611-4b0e-963f-6a274e2b7929")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace AntsLab.VoiceDebug;

[BepInPlugin("antslab.VoiceDebug", "AntsLab Voice Debug", "1.2.0")]
public class VoiceDebugPlugin : BaseUnityPlugin
{
	internal static class State
	{
		public static string LastResult = "";

		public static float LastResultTime = 0f;

		public static bool OverlayMounted;
	}

	public const string PluginGuid = "antslab.VoiceDebug";

	public const string PluginName = "AntsLab Voice Debug";

	public const string PluginVersion = "1.2.0";

	internal static VoiceDebugPlugin Instance;

	private Harmony _harmony;

	internal static ConfigEntry<bool> ShowOverlay;

	internal static ConfigEntry<bool> UseGameFont;

	internal static ConfigEntry<KeyCode> ToggleKey;

	internal static ConfigEntry<float> FadeSeconds;

	internal static ConfigEntry<string> TextAlign;

	private ConfigEntry<int> _fontSize;

	private ConfigEntry<float> _verticalPct;

	internal static int FontSize => Mathf.Max(10, Instance._fontSize.Value);

	internal static float VerticalPct => Mathf.Clamp01(Instance._verticalPct.Value);

	private void Awake()
	{
		//IL_0100: Unknown result type (might be due to invalid IL or missing references)
		//IL_010a: Expected O, but got Unknown
		Instance = this;
		ToggleKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("UI", "ToggleKey", (KeyCode)289, "Toggle HUD (on/off).");
		ShowOverlay = ((BaseUnityPlugin)this).Config.Bind<bool>("UI", "ShowOverlay", true, "Show HUD by default?");
		_verticalPct = ((BaseUnityPlugin)this).Config.Bind<float>("UI", "VerticalPercent", 0.8f, "Vertical Position (%) (0=top, 1=bottom).");
		TextAlign = ((BaseUnityPlugin)this).Config.Bind<string>("UI", "TextAlign", "MiddleCenter", "Method to align the text.");
		_fontSize = ((BaseUnityPlugin)this).Config.Bind<int>("UI", "FontSize", 30, "Font Size.");
		UseGameFont = ((BaseUnityPlugin)this).Config.Bind<bool>("UI", "UseGameFont", true, "Use Game font or a generic one?");
		FadeSeconds = ((BaseUnityPlugin)this).Config.Bind<float>("UI", "FadeSeconds", 8f, "Duration of fade out from last result.");
		_harmony = new Harmony("antslab.VoiceDebug");
		TryPatch_VoiceControlListener_TryResult();
		EnsureOverlay();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"AntsLab Voice Debug 1.2.0 loaded.");
	}

	private void OnDestroy()
	{
		try
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
		catch
		{
		}
		Instance = null;
	}

	private void Update()
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		if (Input.GetKeyDown(ToggleKey.Value))
		{
			ShowOverlay.Value = !ShowOverlay.Value;
		}
	}

	private void EnsureOverlay()
	{
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_0017: Expected O, but got Unknown
		if (!State.OverlayMounted)
		{
			GameObject val = new GameObject("AntsLab_VoiceDebug_Overlay");
			Object.DontDestroyOnLoad((Object)(object)val);
			((Object)val).hideFlags = (HideFlags)52;
			val.AddComponent<VoiceOverlay>();
			State.OverlayMounted = true;
		}
	}

	private void TryPatch_VoiceControlListener_TryResult()
	{
		//IL_0086: Unknown result type (might be due to invalid IL or missing references)
		//IL_008c: Expected O, but got Unknown
		try
		{
			Type type = AccessTools.TypeByName("VoiceControlListener");
			if (type == null)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"[VoiceDebug] Type VoiceControlListener not found.");
				return;
			}
			MethodInfo methodInfo = AccessTools.Method(type, "tryresult", new Type[1] { typeof(string) }, (Type[])null);
			if (methodInfo == null)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"[VoiceDebug] VoiceControlListener.tryresult(string) not found.");
				return;
			}
			HarmonyMethod val = new HarmonyMethod(typeof(VoiceDebugPlugin).GetMethod("TryResult_Prefix", BindingFlags.Static | BindingFlags.NonPublic));
			_harmony.Patch((MethodBase)methodInfo, val, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		}
		catch (Exception ex)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)("[VoiceDebug] Failed to patch tryresult: " + ex.Message));
		}
	}

	private static void TryResult_Prefix(object __instance, string res)
	{
		if (!string.IsNullOrEmpty(res))
		{
			State.LastResult = res;
			State.LastResultTime = Time.time;
		}
	}
}
internal class VoiceOverlay : MonoBehaviour
{
	private GUIStyle _label;

	private static Font TryGetMenuFontFromTMP()
	{
		try
		{
			ScriptableObject[] array = Resources.FindObjectsOfTypeAll<ScriptableObject>();
			foreach (ScriptableObject val in array)
			{
				if ((Object)(object)val == (Object)null)
				{
					continue;
				}
				Type type = ((object)val).GetType();
				if (type.FullName == "TMPro.TMP_FontAsset" && ((Object)val).name.IndexOf("KELMSCOT", StringComparison.OrdinalIgnoreCase) >= 0)
				{
					PropertyInfo property = type.GetProperty("sourceFontFile", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
					Font val2 = (Font)((property != null) ? /*isinst with value type is only supported in some contexts*/: null);
					if ((Object)(object)val2 != (Object)null)
					{
						return val2;
					}
				}
			}
		}
		catch
		{
		}
		return null;
	}

	private void SetupStyles()
	{
		//IL_001d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0027: Expected O, but got Unknown
		//IL_009b: Unknown result type (might be due to invalid IL or missing references)
		//IL_009c->IL009c: Incompatible stack types: O vs I4
		//IL_009b->IL009c: Incompatible stack types: I4 vs O
		//IL_009b->IL009c: Incompatible stack types: O vs I4
		if (_label != null)
		{
			return;
		}
		_label = new GUIStyle(GUI.skin.label);
		_label.fontSize = Math.Max(16, VoiceDebugPlugin.FontSize);
		_label.wordWrap = true;
		if (VoiceDebugPlugin.UseGameFont.Value)
		{
			Font val = TryGetMenuFontFromTMP();
			if ((Object)(object)val != (Object)null)
			{
				_label.font = val;
			}
		}
		object obj = _label;
		int num;
		if (Enum.TryParse<TextAnchor>(VoiceDebugPlugin.TextAlign.Value, ignoreCase: true, out TextAnchor result))
		{
			obj = result;
			num = (int)obj;
		}
		else
		{
			num = 4;
			obj = num;
			num = (int)obj;
		}
		((GUIStyle)num).alignment = (TextAnchor)obj;
	}

	private void OnGUI()
	{
		//IL_005a: Unknown result type (might be due to invalid IL or missing references)
		if (VoiceDebugPlugin.ShowOverlay.Value)
		{
			SetupStyles();
			float num = Mathf.Round((float)Screen.height * VoiceDebugPlugin.VerticalPct);
			Rect r = default(Rect);
			((Rect)(ref r))..ctor(0f, num - (float)VoiceDebugPlugin.FontSize * 0.6f, (float)Screen.width, (float)VoiceDebugPlugin.FontSize * 1.4f);
			DrawBottomBar(r);
		}
	}

	private void DrawBottomBar(Rect r)
	{
		//IL_003e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0043: Unknown result type (might be due to invalid IL or missing references)
		//IL_0064: Unknown result type (might be due to invalid IL or missing references)
		//IL_006f: Unknown result type (might be due to invalid IL or missing references)
		//IL_008c: Unknown result type (might be due to invalid IL or missing references)
		string text = VoiceDebugPlugin.State.LastResult ?? "";
		float num = Time.time - VoiceDebugPlugin.State.LastResultTime;
		float num2 = Mathf.Clamp01(1f - num / Mathf.Max(0.01f, VoiceDebugPlugin.FadeSeconds.Value));
		Color color = GUI.color;
		GUI.color = new Color(1f, 1f, 1f, 0.85f * Mathf.Max(0.2f, num2));
		GUI.Label(r, string.IsNullOrEmpty(text) ? " " : text, _label);
		GUI.color = color;
	}
}