Decompiled source of JapaneseTextInput v2.2.0

plugins/JapaneseTextInput.dll

Decompiled 2 weeks ago
using System;
using System.Diagnostics;
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 TMPro;
using UnityEngine;
using UnityEngine.InputSystem;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: AssemblyCompany("JapaneseTextInput")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+85761cf16f6fcbba7c516af6e23bac8bfa4d3667")]
[assembly: AssemblyProduct("JapaneseTextInput")]
[assembly: AssemblyTitle("JapaneseTextInput")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 JapaneseTextInput
{
	internal static class ClipboardPaste
	{
		public static bool TryRead(out string text)
		{
			text = null;
			if ((!Input.GetKey((KeyCode)306) && !Input.GetKey((KeyCode)305)) || !Input.GetKeyDown((KeyCode)118))
			{
				return false;
			}
			string systemCopyBuffer = GUIUtility.systemCopyBuffer;
			if (string.IsNullOrEmpty(systemCopyBuffer))
			{
				return false;
			}
			systemCopyBuffer = Sanitize(systemCopyBuffer);
			if (systemCopyBuffer.Length == 0)
			{
				return false;
			}
			text = systemCopyBuffer;
			return true;
		}

		public static string Sanitize(string raw)
		{
			string text = raw.Replace("\t", " ").Replace("\r", " ").Replace("\n", " ");
			for (int num = text.Length - 1; num >= 0; num--)
			{
				if (char.IsControl(text[num]))
				{
					text = text.Remove(num, 1);
				}
			}
			return text.Trim();
		}

		public static string Append(string current, string pasted, int maxLength)
		{
			string text = current + pasted;
			if (text.Length <= maxLength)
			{
				return text;
			}
			return text.Remove(maxLength);
		}
	}
	internal static class ImeState
	{
		private const int GraceFrames = 1;

		private const float GraceSeconds = 0.05f;

		private static int s_observedFrame = -1;

		private static bool s_wasComposing;

		private static int s_endFrame = -100;

		private static float s_endTime = float.NegativeInfinity;

		public static bool IsComposing => !string.IsNullOrEmpty(Input.compositionString);

		public static bool OwnsEditingKeys
		{
			get
			{
				Refresh();
				if (!IsComposing && Time.frameCount - s_endFrame > 1)
				{
					return Time.unscaledTime - s_endTime < 0.05f;
				}
				return true;
			}
		}

		public static void Refresh()
		{
			int frameCount = Time.frameCount;
			if (frameCount != s_observedFrame)
			{
				s_observedFrame = frameCount;
				bool isComposing = IsComposing;
				if (s_wasComposing && !isComposing)
				{
					s_endFrame = frameCount;
					s_endTime = Time.unscaledTime;
				}
				s_wasComposing = isComposing;
			}
		}

		public static string PreviewMarkup(string composition)
		{
			return "<u><color=#FFD700>" + composition + "</color></u>";
		}
	}
	internal static class MenuIme
	{
		private static bool s_enabled;

		public static void Enable()
		{
			if (!s_enabled)
			{
				s_enabled = true;
				Input.imeCompositionMode = (IMECompositionMode)1;
				Keyboard current = Keyboard.current;
				if (current != null)
				{
					current.SetIMEEnabled(true);
				}
			}
		}

		public static void Disable()
		{
			if (s_enabled)
			{
				s_enabled = false;
				Input.imeCompositionMode = (IMECompositionMode)2;
				Keyboard current = Keyboard.current;
				if (current != null)
				{
					current.SetIMEEnabled(false);
				}
			}
		}

		public static void PlaceCandidateWindow(Transform anchor)
		{
			//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_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			Vector3 position = anchor.position;
			Input.compositionCursorPos = new Vector2(position.x, (float)Screen.height - position.y);
		}
	}
	internal sealed class RichTextLease
	{
		private bool m_forced;

		public void Set(TMP_Text text, bool wanted)
		{
			if (wanted && !text.richText)
			{
				text.richText = true;
				m_forced = true;
			}
			else if (!wanted && m_forced)
			{
				text.richText = false;
				m_forced = false;
			}
		}
	}
	[HarmonyPatch(typeof(SemiFunc), "InputDown", new Type[] { typeof(InputKey) })]
	internal static class Patch_SemiFunc_InputDown
	{
		private static bool Prefix(InputKey key, ref bool __result)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0003: Invalid comparison between Unknown and I4
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Invalid comparison between Unknown and I4
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Invalid comparison between Unknown and I4
			if ((int)key != 9 && (int)key != 13 && (int)key != 18)
			{
				return true;
			}
			if (SemiFunc.NoTextInputsActive())
			{
				return true;
			}
			if (!ImeState.OwnsEditingKeys)
			{
				return true;
			}
			__result = false;
			return false;
		}
	}
	[HarmonyPatch(typeof(MenuPagePopUpTextInput), "ButtonEventConfirm")]
	internal static class Patch_MenuPagePopUpTextInput_Confirm
	{
		private static bool Prefix()
		{
			return !ImeState.OwnsEditingKeys;
		}
	}
	[HarmonyPatch(typeof(MenuPagePassword), "ConfirmButton")]
	internal static class Patch_MenuPagePassword_Confirm
	{
		private static bool Prefix()
		{
			return !ImeState.OwnsEditingKeys;
		}
	}
	[BepInPlugin("com.sukunabikona.japanesetextinput", "Japanese Text Input", "2.2.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string GUID = "com.sukunabikona.japanesetextinput";

		public const string Name = "Japanese Text Input";

		public const string Version = "2.2.0";

		internal static ManualLogSource Logger;

		private void Awake()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			Logger = ((BaseUnityPlugin)this).Logger;
			new Harmony("com.sukunabikona.japanesetextinput").PatchAll();
			Logger.LogInfo((object)"Japanese Text Input v2.2.0 loaded.");
		}
	}
	[HarmonyPatch(typeof(MenuTextInput), "Update")]
	internal static class Patch_MenuTextInput_Update
	{
		private static readonly RichTextLease s_richText = new RichTextLease();

		private static void Prefix(MenuTextInput __instance)
		{
			ImeState.Refresh();
			if (IsPasswordField(__instance))
			{
				MenuIme.Disable();
				return;
			}
			MenuIme.Enable();
			if ((Object)(object)__instance.textCursor != (Object)null)
			{
				MenuIme.PlaceCandidateWindow(((TMP_Text)__instance.textCursor).transform);
			}
		}

		private static bool IsPasswordField(MenuTextInput input)
		{
			MenuPagePassword instance = MenuPagePassword.instance;
			if ((Object)(object)instance != (Object)null)
			{
				return (Object)(object)instance.passwordInput == (Object)(object)input;
			}
			return false;
		}

		private static void Postfix(MenuTextInput __instance)
		{
			if (ClipboardPaste.TryRead(out var text))
			{
				if (__instance.disallowSpaces)
				{
					text = text.Replace(" ", "");
				}
				__instance.textCurrent = ClipboardPaste.Append(__instance.textCurrent, text, __instance.maxLength);
			}
			if (!((Object)(object)__instance.textMain == (Object)null))
			{
				string compositionString = Input.compositionString;
				bool flag = !string.IsNullOrEmpty(compositionString) && !__instance.obscureText;
				s_richText.Set((TMP_Text)(object)__instance.textMain, flag);
				if (flag)
				{
					TextMeshProUGUI textMain = __instance.textMain;
					((TMP_Text)textMain).text = ((TMP_Text)textMain).text + ImeState.PreviewMarkup(compositionString);
				}
			}
		}
	}
	[HarmonyPatch(typeof(MenuTextInput), "OnDisable")]
	internal static class Patch_MenuTextInput_OnDisable
	{
		private static void Postfix()
		{
			MenuIme.Disable();
		}
	}
	[HarmonyPatch(typeof(ChatManager), "Update")]
	internal static class Patch_ChatManager_Update
	{
		private static readonly RichTextLease s_richText = new RichTextLease();

		private static void Postfix(ChatManager __instance)
		{
			if ((Object)(object)__instance.chatText == (Object)null)
			{
				return;
			}
			string text = (__instance.StateIsActive() ? __instance.compositionString : null);
			if (string.IsNullOrEmpty(text))
			{
				s_richText.Set((TMP_Text)(object)__instance.chatText, wanted: false);
				return;
			}
			string text2 = ((TMP_Text)__instance.chatText).text;
			string text3 = __instance.chatMessage + text;
			if (text2.StartsWith(text3, StringComparison.Ordinal))
			{
				string text4 = text2.Substring(text3.Length);
				s_richText.Set((TMP_Text)(object)__instance.chatText, wanted: true);
				((TMP_Text)__instance.chatText).text = __instance.chatMessage + ImeState.PreviewMarkup(text) + text4;
			}
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}