Decompiled source of FixCharWarn v1.0.0

FixCharWarn.dll

Decompiled a month ago
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
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: AssemblyTitle("FixCharWarn")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("FixCharWarn")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("6964290d-06d4-4810-84f0-aaf3de08e041")]
[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 FixCharWarn;

[BepInPlugin("FixCharWarn", "chuxia.FixCharWarn", "1.0.0")]
public class FixCharWarnPlugin : BaseUnityPlugin
{
	public static ManualLogSource ManualLog;

	public void Awake()
	{
		ManualLog = ((BaseUnityPlugin)this).Logger;
		Harmony.CreateAndPatchAll(typeof(Patches), (string)null);
	}
}
public static class Patches
{
	public static List<TMP_FontAsset> TMP_Fonts { get; set; } = new List<TMP_FontAsset>();


	private static List<string> HandleResult { get; set; } = new List<string>();


	[HarmonyPatch(typeof(TMP_FontAsset), "Awake")]
	[HarmonyPostfix]
	[HarmonyWrapSafe]
	public static void TMP_FontAsset_Awake(TMP_FontAsset __instance)
	{
		if (!TMP_Fonts.Contains(__instance))
		{
			Dictionary<uint, TMP_Character> characterLookupTable = __instance.characterLookupTable;
			Debug.Log((object)("TMP_FontAsset_Awake: " + ((Object)__instance).name));
			TMP_Fonts.Add(__instance);
		}
	}

	[HarmonyPatch(/*Could not decode attribute arguments.*/)]
	[HarmonyPrefix]
	[HarmonyPriority(0)]
	[HarmonyWrapSafe]
	public static void TMP_Text_textSetter(ref string value)
	{
		string text = ReplaceInvalidCharacters(value);
		if (text != value)
		{
			Debug.Log((object)("TMP_Text_textSetter replaced text: " + value + " -> " + text));
			value = text;
		}
	}

	[HarmonyPatch(/*Could not decode attribute arguments.*/)]
	[HarmonyPrefix]
	[HarmonyPriority(0)]
	[HarmonyWrapSafe]
	public static void Text_textSetter(ref string value)
	{
		string text = ReplaceInvalidCharacters(value);
		if (text != value)
		{
			Debug.Log((object)("Text_textSetter replaced text: " + value + " -> " + text));
			value = text;
		}
	}

	public static string ReplaceInvalidCharacters(string text)
	{
		if (string.IsNullOrWhiteSpace(text))
		{
			return text;
		}
		if (HandleResult.Contains(text))
		{
			return text;
		}
		if (TMP_Fonts != null)
		{
			StringBuilder stringBuilder = new StringBuilder();
			foreach (char item in text)
			{
				if (TMP_Fonts.Any((TMP_FontAsset x) => x.HasCharacter((int)item)))
				{
					stringBuilder.Append(item);
				}
				else
				{
					stringBuilder.Append("□");
				}
			}
			return stringBuilder.ToString();
		}
		return text;
	}
}