Decompiled source of JapaneseTextInput v2.1.1

plugins/JapaneseTextInput.dll

Decompiled 5 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using TMPro;
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(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("JapaneseTextInput")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+ce5b35ba6d38776afe2fe98ee513519e3532134a")]
[assembly: AssemblyProduct("JapaneseTextInput")]
[assembly: AssemblyTitle("JapaneseTextInput")]
[assembly: AssemblyVersion("1.0.0.0")]
[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
{
	[HarmonyPatch(typeof(MenuTextInput), "Update")]
	internal static class Patch_MenuTextInput_Ime
	{
		private static void Prefix(MenuTextInput __instance)
		{
			//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)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			Input.imeCompositionMode = (IMECompositionMode)1;
			if ((Object)(object)__instance.textCursor != (Object)null)
			{
				Vector3 position = ((TMP_Text)__instance.textCursor).transform.position;
				Input.compositionCursorPos = new Vector2(position.x, (float)Screen.height - position.y);
			}
		}

		private static void Postfix(MenuTextInput __instance)
		{
			string compositionString = Input.compositionString;
			if (!string.IsNullOrEmpty(compositionString) && !((Object)(object)__instance.textMain == (Object)null))
			{
				((TMP_Text)__instance.textMain).richText = true;
				TextMeshProUGUI textMain = __instance.textMain;
				((TMP_Text)textMain).text = ((TMP_Text)textMain).text + "<u><color=#FFD700>" + compositionString + "</color></u>";
			}
		}
	}
	[HarmonyPatch(typeof(MenuPageServerListCreateNew), "Update")]
	internal static class Patch_MenuPageServerListCreateNew_ImeUpdate
	{
		private static bool s_wasComposing;

		internal static bool s_blockConfirm;

		private static void Prefix()
		{
			bool flag = !string.IsNullOrEmpty(Input.compositionString);
			s_blockConfirm = s_wasComposing && !flag;
			s_wasComposing = flag;
		}
	}
	[HarmonyPatch(typeof(MenuPageServerListCreateNew), "ButtonConfirm")]
	internal static class Patch_MenuPageServerListCreateNew_ButtonConfirm
	{
		private static bool Prefix()
		{
			if (Patch_MenuPageServerListCreateNew_ImeUpdate.s_blockConfirm || !string.IsNullOrEmpty(Input.compositionString))
			{
				return false;
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(SemiFunc), "InputDown")]
	internal static class Patch_SemiFunc_InputDown_ImeBlock
	{
		private static bool Prefix(InputKey key, ref bool __result)
		{
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Invalid comparison between Unknown and I4
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Invalid comparison between Unknown and I4
			bool flag = (Object)(object)ChatManager.instance != (Object)null && ChatManager.instance.StateIsActive();
			bool flag2 = Patch_ChatManager_Ime.s_blockConfirm || !string.IsNullOrEmpty(Input.compositionString);
			if (flag && flag2 && ((int)key == 13 || (int)key == 9))
			{
				__result = false;
				return false;
			}
			return true;
		}

		private static void Postfix(InputKey key, bool __result)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			ChatInputFilter.ObserveInputDown(key, __result);
		}
	}
	internal static class ChatInputFilter
	{
		private static int s_lastChatDeleteFrame = int.MinValue;

		internal static void ObserveInputDown(InputKey key, bool result)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0004: Invalid comparison between Unknown and I4
			if ((int)key == 13 && result && (Object)(object)ChatManager.instance != (Object)null && ChatManager.instance.StateIsActive())
			{
				s_lastChatDeleteFrame = Time.frameCount;
			}
		}

		internal static string GetInputString()
		{
			string inputString = Input.inputString;
			if (inputString == "\b" && Time.frameCount == s_lastChatDeleteFrame + 1)
			{
				return string.Empty;
			}
			return inputString;
		}
	}
	[HarmonyPatch(typeof(ChatManager), "StateActive")]
	internal static class Patch_ChatManager_StateActive_InputFilter
	{
		private static readonly MethodInfo m_inputStringGetter = AccessTools.PropertyGetter(typeof(Input), "inputString");

		private static readonly MethodInfo m_filteredInputString = AccessTools.Method(typeof(ChatInputFilter), "GetInputString", (Type[])null, (Type[])null);

		private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			foreach (CodeInstruction instruction in instructions)
			{
				if (CodeInstructionExtensions.Calls(instruction, m_inputStringGetter))
				{
					instruction.operand = m_filteredInputString;
				}
				yield return instruction;
			}
		}
	}
	[HarmonyPatch(typeof(ChatManager), "Update")]
	internal static class Patch_ChatManager_Ime
	{
		private static bool s_wasComposing = false;

		internal static bool s_blockConfirm = false;

		private static readonly FieldInfo f_chatState = typeof(ChatManager).GetField("chatState", BindingFlags.Instance | BindingFlags.NonPublic);

		private static void Prefix(ChatManager __instance)
		{
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: 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_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			if (!__instance.StateIsActive())
			{
				s_blockConfirm = false;
				if (string.IsNullOrEmpty(Input.compositionString))
				{
					Input.imeCompositionMode = (IMECompositionMode)0;
				}
				return;
			}
			Input.imeCompositionMode = (IMECompositionMode)1;
			if ((Object)(object)__instance.chatText != (Object)null)
			{
				Vector3 position = ((TMP_Text)__instance.chatText).transform.position;
				Input.compositionCursorPos = new Vector2(position.x, (float)Screen.height - position.y);
			}
			s_blockConfirm = s_wasComposing && string.IsNullOrEmpty(Input.compositionString);
		}

		private static void Postfix(ChatManager __instance)
		{
			if (s_blockConfirm && __instance.StateIsSend() && f_chatState != null)
			{
				f_chatState.SetValue(__instance, (object)(ChatState)1);
			}
			s_wasComposing = __instance.StateIsActive() && !string.IsNullOrEmpty(Input.compositionString);
			if (!__instance.StateIsActive())
			{
				return;
			}
			string compositionString = Input.compositionString;
			if (!string.IsNullOrEmpty(compositionString) && !((Object)(object)__instance.chatText == (Object)null))
			{
				string text = ((TMP_Text)__instance.chatText).text;
				bool flag = text.EndsWith("|");
				if (flag)
				{
					text = text.Substring(0, text.Length - 1);
				}
				((TMP_Text)__instance.chatText).richText = true;
				((TMP_Text)__instance.chatText).text = text + "<u><color=#FFD700>" + compositionString + "</color></u>" + (flag ? "|" : "");
			}
		}
	}
	[HarmonyPatch(typeof(MenuTextInput), "Update")]
	internal static class Patch_MenuTextInput_Update
	{
		private static readonly FieldInfo f_textCurrent = typeof(MenuTextInput).GetField("textCurrent", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

		private static bool s_warned = false;

		private static void Postfix(MenuTextInput __instance)
		{
			if (f_textCurrent == null)
			{
				if (!s_warned)
				{
					Plugin.Logger.LogWarning((object)"[JapaneseTextInput] MenuTextInput.textCurrent フィールドが見つかりません。ゲームのバージョンに対応していない可能性があります。");
					s_warned = true;
				}
			}
			else
			{
				if ((!Input.GetKey((KeyCode)306) && !Input.GetKey((KeyCode)305)) || !Input.GetKeyDown((KeyCode)118))
				{
					return;
				}
				string systemCopyBuffer = GUIUtility.systemCopyBuffer;
				if (string.IsNullOrEmpty(systemCopyBuffer))
				{
					return;
				}
				systemCopyBuffer = systemCopyBuffer.Replace("\r\n", "").Replace("\n", "").Replace("\r", "");
				if (systemCopyBuffer.Length != 0)
				{
					if (__instance.upperOnly)
					{
						systemCopyBuffer = systemCopyBuffer.ToUpper();
					}
					string text = ((string)f_textCurrent.GetValue(__instance)) ?? "";
					text += systemCopyBuffer;
					if (text.Length > __instance.maxLength)
					{
						text = text.Remove(__instance.maxLength);
					}
					f_textCurrent.SetValue(__instance, text);
					Plugin.Logger.LogDebug((object)("[Paste:Menu] '" + text + "'"));
				}
			}
		}
	}
	[HarmonyPatch(typeof(ChatManager), "Update")]
	internal static class Patch_ChatManager_Update
	{
		private const int MaxChatLength = 50;

		private static readonly FieldInfo f_chatMessage = typeof(ChatManager).GetField("chatMessage", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

		private static bool s_warned = false;

		private static void Postfix(ChatManager __instance)
		{
			if (f_chatMessage == null)
			{
				if (!s_warned)
				{
					Plugin.Logger.LogWarning((object)"[JapaneseTextInput] ChatManager.chatMessage フィールドが見つかりません。ゲームのバージョンに対応していない可能性があります。");
					s_warned = true;
				}
			}
			else
			{
				if (!__instance.StateIsActive() || (!Input.GetKey((KeyCode)306) && !Input.GetKey((KeyCode)305)) || !Input.GetKeyDown((KeyCode)118))
				{
					return;
				}
				string systemCopyBuffer = GUIUtility.systemCopyBuffer;
				if (string.IsNullOrEmpty(systemCopyBuffer))
				{
					return;
				}
				systemCopyBuffer = systemCopyBuffer.Replace("\r\n", "").Replace("\n", "").Replace("\r", "");
				if (systemCopyBuffer.Length != 0)
				{
					string text = ((string)f_chatMessage.GetValue(__instance)) ?? "";
					string text2 = text + systemCopyBuffer;
					if (text2.Length > 50)
					{
						text2 = text2.Remove(50);
					}
					f_chatMessage.SetValue(__instance, text2);
					if ((Object)(object)__instance.chatText != (Object)null)
					{
						((TMP_Text)__instance.chatText).text = text2;
					}
					Plugin.Logger.LogDebug((object)("[Paste:Chat] '" + text2 + "'"));
				}
			}
		}
	}
	[BepInPlugin("com.sukunabikona.japanesetextinput", "Japanese Text Input", "2.1.1")]
	public class Plugin : BaseUnityPlugin
	{
		public const string GUID = "com.sukunabikona.japanesetextinput";

		public const string Name = "Japanese Text Input";

		public const string Version = "2.1.1";

		internal static ManualLogSource Logger;

		private void Awake()
		{
			//IL_0011: 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.1.1 loaded.");
		}
	}
}