Decompiled source of Unicode Fix v1.0.1

BepInEx/plugins/FontPatcher.dll

Decompiled 21 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Text;
using System.Text.RegularExpressions;
using BepInEx;
using BepInEx.Configuration;
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(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("LeKAKiD")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Replace in-game font with other things")]
[assembly: AssemblyFileVersion("1.2.4.0")]
[assembly: AssemblyInformationalVersion("1.2.4+d5fa67705b60c049a3e31d66d30cc939edaa91fb")]
[assembly: AssemblyProduct("FontPatcher")]
[assembly: AssemblyTitle("FontPatcher")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.2.4.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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;
		}
	}
}
internal class ResourcePath
{
	public const string NormalFont = "Normal";

	public const string TransmitFont = "Transmit";
}
namespace FontPatcher
{
	[HarmonyPatch]
	internal class FontLoader
	{
		private class FontBundle
		{
			public string BundleName;

			public TMP_FontAsset Normal;

			public TMP_FontAsset Transmit;
		}

		private static List<FontBundle> fontBundles = new List<FontBundle>();

		private static Regex normalRegex;

		private static Regex transmitRegex;

		public static void Load()
		{
			try
			{
				string directoryName = Path.GetDirectoryName(((BaseUnityPlugin)Plugin.Instance).Config.ConfigFilePath);
				string text = Path.Combine(directoryName, Plugin.configFontAssetPath.Value);
				Plugin.LogInfo("Font path: " + text);
				DirectoryInfo directoryInfo = new DirectoryInfo(text);
				FileInfo[] files = directoryInfo.GetFiles("*");
				int num = 0;
				int num2 = 0;
				FileInfo[] array = files;
				foreach (FileInfo fileInfo in array)
				{
					try
					{
						AssetBundle val = AssetBundle.LoadFromFile(fileInfo.FullName);
						Plugin.LogInfo("[" + fileInfo.Name + "] loaded");
						FontBundle fontBundle = new FontBundle
						{
							Normal = val.LoadAsset<TMP_FontAsset>("Normal"),
							Transmit = val.LoadAsset<TMP_FontAsset>("Transmit")
						};
						if (Object.op_Implicit((Object)(object)fontBundle.Normal))
						{
							fontBundle.BundleName = fileInfo.Name;
							((Object)fontBundle.Normal).name = fileInfo.Name + "(Normal)";
							Plugin.LogInfo("[" + fileInfo.Name + "] Normal font found (" + ((Object)fontBundle.Normal).name + ")");
						}
						if (Object.op_Implicit((Object)(object)fontBundle.Transmit))
						{
							fontBundle.BundleName = fileInfo.Name;
							((Object)fontBundle.Transmit).name = fileInfo.Name + "(Transmit)";
							Plugin.LogInfo("[" + fileInfo.Name + "] Transmit font found (" + ((Object)fontBundle.Transmit).name + ")");
						}
						if (fontBundle.BundleName == null)
						{
							throw new Exception("Not included recognizable font");
						}
						fontBundles.Add(fontBundle);
						num++;
					}
					catch (Exception ex)
					{
						Plugin.LogError("[" + fileInfo.Name + "] load failed: " + ex.Message);
						num2++;
					}
				}
				normalRegex = new Regex(Plugin.configNormalRegexPattern.Value);
				transmitRegex = new Regex(Plugin.configTransmitRegexPattern.Value);
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.Append($"{num} fonts loaded");
				if (num2 > 0)
				{
					stringBuilder.Append($", {num2} fonts load failed");
				}
				Plugin.LogInfo(stringBuilder.ToString());
			}
			catch (Exception ex2)
			{
				Plugin.LogError(ex2.ToString());
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(TMP_FontAsset), "Awake")]
		private static void PatchFontAwake(TMP_FontAsset __instance)
		{
			if ((Object)(object)__instance == (Object)null)
			{
				Plugin.LogError("PatchFontAwake: TMP_FontAsset 是 null,無法修補");
				return;
			}
			if ((Object)(object)((TMP_Asset)__instance).material == (Object)null)
			{
				Plugin.LogError("PatchFontAwake: " + ((Object)__instance).name + " 的 material 是 null,跳過修補");
				return;
			}
			((TMP_Asset)__instance).material.SetFloat("_UnderlayDilate", 1f);
			((TMP_Asset)__instance).material.SetFloat("_UnderlayOffsetX", 0.1f);
			if (__instance.fallbackFontAssetTable == null)
			{
				Plugin.LogError("PatchFontAwake: " + ((Object)__instance).name + " 的 fallbackFontAssetTable 是 null,跳過修補");
				return;
			}
			string name = ((Object)__instance).name;
			if (normalRegex.IsMatch(name))
			{
				if (!Plugin.configNormalIngameFont.Value)
				{
					DisableFont(__instance);
				}
				int num = 0;
				foreach (FontBundle fontBundle in fontBundles)
				{
					if (!((Object)(object)fontBundle.Normal == (Object)null) && !__instance.fallbackFontAssetTable.Contains(fontBundle.Normal))
					{
						__instance.fallbackFontAssetTable.Add(fontBundle.Normal);
						num++;
					}
				}
				if (num > 0)
				{
					Plugin.LogInfo("[" + name + "] 字體已修補 (Normal)");
				}
			}
			else if (transmitRegex.IsMatch(name))
			{
				if (!Plugin.configTransmitIngameFont.Value)
				{
					DisableFont(__instance);
				}
				int num2 = 0;
				foreach (FontBundle fontBundle2 in fontBundles)
				{
					if (!((Object)(object)fontBundle2.Transmit == (Object)null) && !__instance.fallbackFontAssetTable.Contains(fontBundle2.Transmit))
					{
						__instance.fallbackFontAssetTable.Add(fontBundle2.Transmit);
						num2++;
					}
				}
				if (num2 > 0)
				{
					Plugin.LogInfo("[" + name + "] 字體已修補 (Transmit)");
				}
			}
			else
			{
				Plugin.LogWarning("[" + name + "] 未匹配到任何字體修補規則");
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(/*Could not decode attribute arguments.*/)]
		private static void PatchTextFontSetter(TMP_FontAsset value)
		{
			PatchFontAwake(value);
		}

		private static void DisableFont(TMP_FontAsset font)
		{
			font.characterLookupTable.Clear();
			font.atlasPopulationMode = (AtlasPopulationMode)0;
		}
	}
	internal class PluginInfo
	{
		public const string GUID = "lekakid.universalfontpatcher";

		public const string Name = "Universal Font Patcher";

		public const string Version = "2.0.0";
	}
	[BepInPlugin("lekakid.universalfontpatcher", "Universal Font Patcher", "2.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public static Plugin Instance;

		public static ConfigEntry<bool> configNormalIngameFont;

		public static ConfigEntry<bool> configTransmitIngameFont;

		public static ConfigEntry<string> configNormalRegexPattern;

		public static ConfigEntry<string> configTransmitRegexPattern;

		public static ConfigEntry<string> configFontAssetPath;

		public static ConfigEntry<bool> configDebugLog;

		private void Awake()
		{
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
			}
			configNormalIngameFont = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "UsingNormalIngameFont", true, "是否使用內建普通字體");
			configTransmitIngameFont = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "UsingTransmitIngameFont", true, "是否使用內建傳輸字體");
			configNormalRegexPattern = ((BaseUnityPlugin)this).Config.Bind<string>("Regex", "NormalFontNameRegex", ".*", "正則表達式來匹配一般字體");
			configTransmitRegexPattern = ((BaseUnityPlugin)this).Config.Bind<string>("Regex", "TransmitFontNameRegex", ".*", "正則表達式來匹配傳輸字體");
			configFontAssetPath = ((BaseUnityPlugin)this).Config.Bind<string>("Path", "FontAssetsPath", "FontPatcher", "指定字體資產存放目錄");
			configDebugLog = ((BaseUnityPlugin)this).Config.Bind<bool>("Debug", "Log", true, "啟用 Debug 日誌");
			LogInfo("字體修補插件初始化...");
			FontLoader.Load();
			Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), (string)null);
		}

		public static void LogInfo(string msg)
		{
			if (configDebugLog.Value)
			{
				((BaseUnityPlugin)Instance).Logger.LogInfo((object)msg);
			}
		}

		public static void LogWarning(string msg)
		{
			if (configDebugLog.Value)
			{
				((BaseUnityPlugin)Instance).Logger.LogWarning((object)msg);
			}
		}

		public static void LogError(string msg)
		{
			((BaseUnityPlugin)Instance).Logger.LogError((object)msg);
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "FontPatcher";

		public const string PLUGIN_NAME = "FontPatcher";

		public const string PLUGIN_VERSION = "1.2.4";
	}
}