Decompiled source of RepoChatLengthPlus v1.0.0

plugins\RepoChatLengthPlus\RepoChatLengthPlus.dll

Decompiled 44 minutes ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("RepoChatLengthPlus")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Raises the R.E.P.O. chat character limit and keeps the full text visible and spoken by TTS.")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("Repo Chat Length Plus")]
[assembly: AssemblyTitle("RepoChatLengthPlus")]
[assembly: AssemblyMetadata("AI_Assisted_Creation", "This assembly was partially or fully created with the assistance of Generative AI (code generation and documentation).")]
[assembly: AssemblyMetadata("AI_Model_Vendor", "DeepSeek")]
[assembly: AssemblyMetadata("AI_Agent", "DeepSeek Harness (deepseek-flash)")]
[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 RepoChatLengthPlus
{
	internal static class ChatLimitPatches
	{
		private const int VanillaLimit = 50;

		[HarmonyPatch(typeof(ChatManager), "StateActive")]
		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> StateActive_Transpiler(IEnumerable<CodeInstruction> instructions, MethodBase original)
		{
			return ReplaceLimitConstants(instructions, original);
		}

		[HarmonyPatch(typeof(ChatManager), "ChatPasteLogic")]
		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> ChatPasteLogic_Transpiler(IEnumerable<CodeInstruction> instructions, MethodBase original)
		{
			return ReplaceLimitConstants(instructions, original);
		}

		[HarmonyPatch(typeof(ChatManager), "GamepadTextInputShow")]
		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> GamepadTextInputShow_Transpiler(IEnumerable<CodeInstruction> instructions, MethodBase original)
		{
			return ReplaceLimitConstants(instructions, original);
		}

		[HarmonyPatch(typeof(ChatManager), "GamepadTextInputSubmitted")]
		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> GamepadTextInputSubmitted_Transpiler(IEnumerable<CodeInstruction> instructions, MethodBase original)
		{
			return ReplaceLimitConstants(instructions, original);
		}

		private static IEnumerable<CodeInstruction> ReplaceLimitConstants(IEnumerable<CodeInstruction> instructions, MethodBase original)
		{
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			//IL_0078: Expected O, but got Unknown
			MethodInfo operand;
			try
			{
				operand = AccessTools.PropertyGetter(typeof(Plugin), "MaxLength");
			}
			catch (Exception ex)
			{
				ManualLogSource log = Plugin.Log;
				if (log != null)
				{
					log.LogError((object)("Cannot resolve MaxLength getter, chat limit left at vanilla 50: " + ex));
				}
				return instructions;
			}
			int num = 0;
			List<CodeInstruction> list = new List<CodeInstruction>();
			foreach (CodeInstruction instruction in instructions)
			{
				if (IsVanillaLimitConstant(instruction))
				{
					num++;
					CodeInstruction val = new CodeInstruction(instruction);
					val.opcode = OpCodes.Call;
					val.operand = operand;
					list.Add(val);
				}
				else
				{
					list.Add(instruction);
				}
			}
			ManualLogSource log2 = Plugin.Log;
			if (log2 != null)
			{
				log2.LogInfo((object)$"[{original.DeclaringType?.Name}.{original.Name}] replaced {num} hard-coded chat limit(s) with config value.");
			}
			if (num == 0)
			{
				ManualLogSource log3 = Plugin.Log;
				if (log3 != null)
				{
					log3.LogWarning((object)($"[{original.DeclaringType?.Name}.{original.Name}] no '{50}' constant found; " + "the game may have changed. That input path keeps the vanilla limit."));
				}
			}
			return list;
		}

		private static bool IsVanillaLimitConstant(CodeInstruction instruction)
		{
			if (instruction.opcode == OpCodes.Ldc_I4_S)
			{
				try
				{
					return Convert.ToInt32(instruction.operand) == 50;
				}
				catch
				{
					return false;
				}
			}
			if (instruction.opcode == OpCodes.Ldc_I4)
			{
				try
				{
					return Convert.ToInt32(instruction.operand) == 50;
				}
				catch
				{
					return false;
				}
			}
			return false;
		}
	}
	internal static class ChatDisplayPatches
	{
		private const float MinReasonableWidth = 310f;

		private const float FallbackWidth = 600f;

		[HarmonyPatch(typeof(ChatUI), "Start")]
		[HarmonyPostfix]
		private static void ChatUI_Start_Postfix(ChatUI __instance)
		{
			//IL_009f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_0110: Unknown result type (might be due to invalid IL or missing references)
			//IL_0115: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if ((Object)(object)__instance == (Object)null)
				{
					return;
				}
				TextMeshProUGUI chatText = __instance.chatText;
				if ((Object)(object)chatText == (Object)null)
				{
					ManualLogSource log = Plugin.Log;
					if (log != null)
					{
						log.LogWarning((object)"ChatUI.chatText is null; display tweak skipped.");
					}
					return;
				}
				if (Plugin.WrapEntry != null && Plugin.WrapEntry.Value)
				{
					((TMP_Text)chatText).textWrappingMode = (TextWrappingModes)1;
					((TMP_Text)chatText).overflowMode = (TextOverflowModes)0;
				}
				RectTransform rectTransform = ((TMP_Text)chatText).rectTransform;
				if ((Object)(object)rectTransform == (Object)null)
				{
					return;
				}
				float num = ((Plugin.TextWidthEntry != null) ? Plugin.TextWidthEntry.Value : 0f);
				if (num <= 0f)
				{
					num = ResolveAutoWidth(__instance);
				}
				if (num > 0f)
				{
					Vector2 sizeDelta = rectTransform.sizeDelta;
					sizeDelta.x = Mathf.Max(310f, num);
					rectTransform.sizeDelta = sizeDelta;
				}
				if (!Plugin.DebugEnabled)
				{
					return;
				}
				ManualLogSource log2 = Plugin.Log;
				if (log2 != null)
				{
					string text = $"Chat text configured: wrap={((TMP_Text)chatText).textWrappingMode}, size={rectTransform.sizeDelta}, ";
					float num2;
					if (!((Object)(object)__instance.chatBackgroundRect != (Object)null))
					{
						num2 = -1f;
					}
					else
					{
						Rect rect = __instance.chatBackgroundRect.rect;
						num2 = ((Rect)(ref rect)).width;
					}
					log2.LogInfo((object)(text + $"backgroundWidth={num2}"));
				}
			}
			catch (Exception ex)
			{
				ManualLogSource log3 = Plugin.Log;
				if (log3 != null)
				{
					log3.LogError((object)("ChatUI display tweak failed: " + ex));
				}
			}
		}

		private static float ResolveAutoWidth(ChatUI ui)
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if ((Object)(object)ui.chatBackgroundRect != (Object)null)
				{
					Rect rect = ui.chatBackgroundRect.rect;
					float width = ((Rect)(ref rect)).width;
					if (width > 200f)
					{
						return Mathf.Clamp(width - 100f, 310f, width - 40f);
					}
				}
			}
			catch
			{
			}
			return 600f;
		}
	}
	[BepInPlugin("repo.chatlengthplus", "Repo Chat Length Plus", "1.0.0")]
	[BepInProcess("REPO.exe")]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string Guid = "repo.chatlengthplus";

		public const string PluginName = "Repo Chat Length Plus";

		public const string PluginVersion = "1.0.0";

		internal static ManualLogSource Log;

		internal static Plugin Instance;

		internal static ConfigEntry<int> MaxLengthEntry;

		internal static ConfigEntry<bool> WrapEntry;

		internal static ConfigEntry<float> TextWidthEntry;

		internal static ConfigEntry<bool> DebugEntry;

		internal static int MaxLength
		{
			get
			{
				try
				{
					return Mathf.Clamp((MaxLengthEntry != null) ? MaxLengthEntry.Value : 500, 50, 2000);
				}
				catch
				{
					return 500;
				}
			}
		}

		internal static bool DebugEnabled
		{
			get
			{
				try
				{
					return DebugEntry != null && DebugEntry.Value;
				}
				catch
				{
					return false;
				}
			}
		}

		private void Awake()
		{
			//IL_009e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: Expected O, but got Unknown
			Instance = this;
			Log = ((BaseUnityPlugin)this).Logger;
			MaxLengthEntry = ((BaseUnityPlugin)this).Config.Bind<int>("Chat", "MaxMessageLength", 500, "单条聊天消息的字数上限。原版是 50,这里默认 500(范围 50-2000)。\nMax characters per chat message. Vanilla is 50; default 500 (clamped to 50-2000).");
			WrapEntry = ((BaseUnityPlugin)this).Config.Bind<bool>("Display", "EnableWordWrap", true, "让聊天文字自动换行,长消息不会被截到屏幕外。\nWrap the chat text so long messages stay on screen.");
			TextWidthEntry = ((BaseUnityPlugin)this).Config.Bind<float>("Display", "ChatTextWidth", 600f, "聊天文字区域的宽度(像素)。0 = 自动按聊天框宽度计算。\nWidth of the chat text area in pixels. 0 = derive from the chat box width.");
			DebugEntry = ((BaseUnityPlugin)this).Config.Bind<bool>("Debug", "DebugLogging", false, "输出调试日志。");
			Harmony val = new Harmony("repo.chatlengthplus");
			try
			{
				val.PatchAll(typeof(ChatLimitPatches));
			}
			catch (Exception ex)
			{
				Log.LogError((object)("Failed to patch chat length limits: " + ex));
			}
			try
			{
				val.PatchAll(typeof(ChatDisplayPatches));
			}
			catch (Exception ex2)
			{
				Log.LogError((object)("Failed to patch chat display: " + ex2));
			}
			Log.LogInfo((object)string.Format("{0} v{1} loaded. Max chat length = {2}, wrap = {3}.", "Repo Chat Length Plus", "1.0.0", MaxLength, WrapEntry != null && WrapEntry.Value));
		}
	}
}