Decompiled source of LCChineseFont v1.2.5

plugins/FontPatcher.dll

Decompiled 11 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 GameNetcodeStuff;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Events;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("LeKAKiD")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Replace in-game font with other things")]
[assembly: AssemblyFileVersion("1.2.4.0")]
[assembly: AssemblyInformationalVersion("1.2.4+e5e215e8ab3f80d37484c38fd5c1fbff56e2517a")]
[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]
	[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;
		}
	}
}
internal class ResourcePath
{
	public const string NormalFont = "Normal";

	public const string TransmitFont = "Transmit";
}
[HarmonyPatch(typeof(HUDManager))]
internal class HUDManagerPatch
{
	[HarmonyPostfix]
	[HarmonyPatch("OnEnable")]
	private static void PostfixOnEnable()
	{
		((UnityEvent<string>)(object)HUDManager.Instance.chatTextField.onSubmit).AddListener((UnityAction<string>)OnSubmitChat);
	}

	[HarmonyPostfix]
	[HarmonyPatch("OnDisable")]
	private static void PrefixOnDisable()
	{
		((UnityEvent<string>)(object)HUDManager.Instance.chatTextField.onSubmit).RemoveListener((UnityAction<string>)OnSubmitChat);
	}

	private static void OnSubmitChat(string chatString)
	{
		//IL_0058: Unknown result type (might be due to invalid IL or missing references)
		//IL_006e: Unknown result type (might be due to invalid IL or missing references)
		PlayerControllerB localPlayerController = GameNetworkManager.Instance.localPlayerController;
		if (!string.IsNullOrEmpty(chatString) && chatString.Length < 50)
		{
			HUDManager.Instance.AddTextToChatOnServer(chatString, (int)localPlayerController.playerClientId);
		}
		for (int i = 0; i < StartOfRound.Instance.allPlayerScripts.Length; i++)
		{
			if (StartOfRound.Instance.allPlayerScripts[i].isPlayerControlled && Vector3.Distance(((Component)GameNetworkManager.Instance.localPlayerController).transform.position, ((Component)StartOfRound.Instance.allPlayerScripts[i]).transform.position) > 24.4f && (!GameNetworkManager.Instance.localPlayerController.holdingWalkieTalkie || !StartOfRound.Instance.allPlayerScripts[i].holdingWalkieTalkie))
			{
				HUDManager.Instance.playerCouldRecieveTextChatAnimator.SetTrigger("ping");
				break;
			}
		}
		localPlayerController.isTypingChat = false;
		HUDManager.Instance.chatTextField.text = "";
		EventSystem.current.SetSelectedGameObject((GameObject)null);
		HUDManager.Instance.PingHUDElement(HUDManager.Instance.Chat, 2f, 1f, 0.2f);
		((Behaviour)HUDManager.Instance.typingIndicator).enabled = false;
	}

	[HarmonyPrefix]
	[HarmonyPatch("SubmitChat_performed")]
	private static void PrefixSubmitChat_performed(ref bool __runOriginal)
	{
		__runOriginal = false;
	}
}
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()
		{
			normalRegex = new Regex(Plugin.configNormalRegexPattern.Value);
			transmitRegex = new Regex(Plugin.configTransmitRegexPattern.Value);
			try
			{
				string text = Path.Combine(Path.GetDirectoryName(((BaseUnityPlugin)Plugin.Instance).Config.ConfigFilePath), Plugin.configFontAssetPath.Value);
				Plugin.LogInfo("Font path: " + text);
				DirectoryInfo directoryInfo = new DirectoryInfo(text);
				if (!directoryInfo.Exists)
				{
					Plugin.LogError("Font directory not found: " + text);
					return;
				}
				FileInfo[] files = directoryInfo.GetFiles("*");
				Array.Sort(files, (FileInfo a, FileInfo b) => string.CompareOrdinal(a.Name, b.Name));
				int num = 0;
				int num2 = 0;
				FileInfo[] array = files;
				foreach (FileInfo fileInfo in array)
				{
					if (fileInfo.Extension.Equals(".manifest", StringComparison.OrdinalIgnoreCase) || fileInfo.Extension.Equals(".meta", StringComparison.OrdinalIgnoreCase))
					{
						continue;
					}
					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)";
							EnsureMainTexture(fontBundle.Normal);
							DiagnoseFontAsset(fontBundle.Normal, 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)";
							EnsureMainTexture(fontBundle.Transmit);
							DiagnoseFontAsset(fontBundle.Transmit, 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++;
					}
				}
				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());
			}
		}

		private static void EnsureMainTexture(TMP_FontAsset font)
		{
			if (Object.op_Implicit((Object)(object)((TMP_Asset)font).material) && !Object.op_Implicit((Object)(object)((TMP_Asset)font).material.GetTexture(ShaderUtilities.ID_MainTex)) && font.atlasTextures != null && font.atlasTextures.Length != 0 && Object.op_Implicit((Object)(object)font.atlasTextures[0]))
			{
				((TMP_Asset)font).material.mainTexture = (Texture)(object)font.atlasTextures[0];
				Plugin.LogInfo("[" + ((Object)font).name + "] material._MainTex was null; re-bound to atlasTextures[0]");
			}
		}

		private static void DiagnoseFontAsset(TMP_FontAsset font, string label)
		{
			//IL_0215: Unknown result type (might be due to invalid IL or missing references)
			//IL_01eb: Unknown result type (might be due to invalid IL or missing references)
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("[" + label + "] material=" + (Object.op_Implicit((Object)(object)((TMP_Asset)font).material) ? "ok" : "NULL"));
			if (Object.op_Implicit((Object)(object)((TMP_Asset)font).material))
			{
				Material material = ((TMP_Asset)font).material;
				stringBuilder.Append(", shader=" + (Object.op_Implicit((Object)(object)material.shader) ? ((Object)material.shader).name : "NULL"));
				stringBuilder.Append(", _MainTex=" + (Object.op_Implicit((Object)(object)material.GetTexture(ShaderUtilities.ID_MainTex)) ? "ok" : "NULL"));
				stringBuilder.Append(", _GradientScale=" + DescribeFloat(material, ShaderUtilities.ID_GradientScale));
				stringBuilder.Append(", _TextureWidth=" + DescribeFloat(material, ShaderUtilities.ID_TextureWidth));
				stringBuilder.Append(", _TextureHeight=" + DescribeFloat(material, ShaderUtilities.ID_TextureHeight));
				stringBuilder.Append(", _WeightNormal=" + DescribeFloat(material, ShaderUtilities.ID_WeightNormal));
				stringBuilder.Append(", _WeightBold=" + DescribeFloat(material, ShaderUtilities.ID_WeightBold));
				stringBuilder.Append(", _FaceDilate=" + DescribeFloat(material, ShaderUtilities.ID_FaceDilate));
				stringBuilder.Append(", _UnderlayDilate=" + DescribeFloat(material, ShaderUtilities.ID_UnderlayDilate));
			}
			stringBuilder.Append($", atlasTextures={((font.atlasTextures != null) ? font.atlasTextures.Length : 0)}");
			if (font.atlasTextures != null && font.atlasTextures.Length != 0 && Object.op_Implicit((Object)(object)font.atlasTextures[0]))
			{
				stringBuilder.Append($", atlasPage0={((Texture)font.atlasTextures[0]).width}x{((Texture)font.atlasTextures[0]).height}/{((Texture)font.atlasTextures[0]).filterMode}");
			}
			else
			{
				stringBuilder.Append(", atlasPage0=NULL");
			}
			stringBuilder.Append($", populationMode={font.atlasPopulationMode}");
			stringBuilder.Append(", sourceFontFile=" + (Object.op_Implicit((Object)(object)font.sourceFontFile) ? "ok" : "NULL"));
			Plugin.LogInfo(stringBuilder.ToString());
		}

		private static string DescribeFloat(Material m, int propertyId)
		{
			if (!m.HasProperty(propertyId))
			{
				return "absent";
			}
			return m.GetFloat(propertyId).ToString("0.##");
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(TMP_MaterialManager), "GetFallbackMaterial", new Type[]
		{
			typeof(Material),
			typeof(Material)
		})]
		private static void LogDerivedFallbackMaterial(Material sourceMaterial, Material targetMaterial, Material __result)
		{
			if (!((Object)(object)__result == (Object)null))
			{
				Plugin.LogInfo("[fallback material] source=" + ((Object.op_Implicit((Object)(object)sourceMaterial) && Object.op_Implicit((Object)(object)sourceMaterial.shader)) ? ((Object)sourceMaterial.shader).name : "NULL") + " target=" + ((Object.op_Implicit((Object)(object)targetMaterial) && Object.op_Implicit((Object)(object)targetMaterial.shader)) ? ((Object)targetMaterial.shader).name : "NULL") + " -> shader=" + (Object.op_Implicit((Object)(object)__result.shader) ? ((Object)__result.shader).name : "NULL") + " _GradientScale=" + DescribeFloat(__result, ShaderUtilities.ID_GradientScale) + " _FaceDilate=" + DescribeFloat(__result, ShaderUtilities.ID_FaceDilate) + " _OutlineWidth=" + DescribeFloat(__result, ShaderUtilities.ID_OutlineWidth) + " _UnderlayDilate=" + DescribeFloat(__result, ShaderUtilities.ID_UnderlayDilate) + " _MainTex=" + (Object.op_Implicit((Object)(object)__result.GetTexture(ShaderUtilities.ID_MainTex)) ? "ok" : "NULL"));
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(TMP_FontAsset), "Awake")]
		private static void PatchFontAwake(TMP_FontAsset __instance)
		{
			((TMP_Asset)__instance).material.SetFloat("_UnderlayDilate", 1f);
			((TMP_Asset)__instance).material.SetFloat("_UnderlayOffsetX", 0.1f);
			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.op_Implicit((Object)(object)fontBundle.Normal) && !__instance.fallbackFontAssetTable.Contains(fontBundle.Normal))
					{
						__instance.fallbackFontAssetTable.Add(fontBundle.Normal);
						num++;
					}
				}
				if (num > 0)
				{
					Plugin.LogInfo($"[{name}] font patched (Normal): +{num} fallback(s), total={__instance.fallbackFontAssetTable.Count}");
				}
			}
			else if (transmitRegex.IsMatch(name))
			{
				if (!Plugin.configTransmitIngameFont.Value)
				{
					DisableFont(__instance);
				}
				int num2 = 0;
				foreach (FontBundle fontBundle2 in fontBundles)
				{
					if (Object.op_Implicit((Object)(object)fontBundle2.Transmit) && !__instance.fallbackFontAssetTable.Contains(fontBundle2.Transmit))
					{
						__instance.fallbackFontAssetTable.Add(fontBundle2.Transmit);
						num2++;
					}
				}
				if (num2 > 0)
				{
					Plugin.LogInfo($"[{name}] font patched (Transmit): +{num2} fallback(s), total={__instance.fallbackFontAssetTable.Count}");
				}
			}
			else
			{
				Plugin.LogWarning("[" + name + "] not patched");
			}
		}

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

		[HarmonyPostfix]
		[HarmonyPatch(typeof(TMP_FontAsset), "SetupNewAtlasTexture")]
		private static void PatchSetupNewAtlasTexture(TMP_FontAsset __instance)
		{
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			if (__instance.atlasTextures == null || __instance.atlasTextures.Length == 0 || !Object.op_Implicit((Object)(object)__instance.atlasTextures[0]))
			{
				return;
			}
			FilterMode filterMode = ((Texture)__instance.atlasTextures[0]).filterMode;
			TextureWrapMode wrapMode = ((Texture)__instance.atlasTextures[0]).wrapMode;
			Texture2D[] atlasTextures = __instance.atlasTextures;
			foreach (Texture2D val in atlasTextures)
			{
				if (Object.op_Implicit((Object)(object)val))
				{
					((Texture)val).filterMode = filterMode;
					((Texture)val).wrapMode = wrapMode;
				}
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(TextMeshProUGUI), "Awake")]
		private static void PatchTextAwake(TextMeshProUGUI __instance)
		{
			if (!((Object)(object)((TMP_Text)__instance).font == (Object)null))
			{
				PatchFontAwake(((TMP_Text)__instance).font);
			}
		}

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

		public const string Name = "FontPatcher";

		public const string Version = "1.2.4";
	}
	[BepInPlugin("lekakid.lcfontpatcher", "FontPatcher", "1.2.4")]
	internal class Plugin : BaseUnityPlugin
	{
		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;

		public static Plugin Instance;

		private void Awake()
		{
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
			}
			configNormalIngameFont = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "UsingNormalIngameFont", true, "Using in-game default normal font");
			configTransmitIngameFont = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "UsingTransmitIngameFont", true, "Using in-game default normal font");
			configNormalRegexPattern = ((BaseUnityPlugin)this).Config.Bind<string>("Regex", "NormalFontNameRegex", "^(b|DialogueText|3270.*)$", "Normally, you don't neet to change it");
			configTransmitRegexPattern = ((BaseUnityPlugin)this).Config.Bind<string>("Regex", "TransmitFontNameRegex", "^edunline.*$", "Normally, you don't neet to change it");
			configFontAssetPath = ((BaseUnityPlugin)this).Config.Bind<string>("Path", "FontAssetsPath", "FontPatcher\\default", (ConfigDescription)null);
			configDebugLog = ((BaseUnityPlugin)this).Config.Bind<bool>("Debug", "Log", false, (ConfigDescription)null);
			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";
	}
}

plugins/LCChineseFont.dll

Decompiled 11 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 GameNetcodeStuff;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Events;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("Syrupdesu")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Replace in-game font with other things")]
[assembly: AssemblyFileVersion("1.2.5.0")]
[assembly: AssemblyInformationalVersion("1.2.5+75f2e050cf7b0992224c98e697cb3f4c2b11d57a")]
[assembly: AssemblyProduct("LCChineseFont")]
[assembly: AssemblyTitle("LCChineseFont")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.2.5.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;
		}
	}
}
internal class ResourcePath
{
	public const string NormalFont = "Normal";

	public const string TransmitFont = "Transmit";
}
[HarmonyPatch(typeof(HUDManager))]
internal class HUDManagerPatch
{
	[HarmonyPostfix]
	[HarmonyPatch("OnEnable")]
	private static void PostfixOnEnable()
	{
		((UnityEvent<string>)(object)HUDManager.Instance.chatTextField.onSubmit).AddListener((UnityAction<string>)OnSubmitChat);
	}

	[HarmonyPostfix]
	[HarmonyPatch("OnDisable")]
	private static void PrefixOnDisable()
	{
		((UnityEvent<string>)(object)HUDManager.Instance.chatTextField.onSubmit).RemoveListener((UnityAction<string>)OnSubmitChat);
	}

	private static void OnSubmitChat(string chatString)
	{
		//IL_0058: Unknown result type (might be due to invalid IL or missing references)
		//IL_006e: Unknown result type (might be due to invalid IL or missing references)
		PlayerControllerB localPlayerController = GameNetworkManager.Instance.localPlayerController;
		if (!string.IsNullOrEmpty(chatString) && chatString.Length < 50)
		{
			HUDManager.Instance.AddTextToChatOnServer(chatString, (int)localPlayerController.playerClientId);
		}
		for (int i = 0; i < StartOfRound.Instance.allPlayerScripts.Length; i++)
		{
			if (StartOfRound.Instance.allPlayerScripts[i].isPlayerControlled && Vector3.Distance(((Component)GameNetworkManager.Instance.localPlayerController).transform.position, ((Component)StartOfRound.Instance.allPlayerScripts[i]).transform.position) > 24.4f && (!GameNetworkManager.Instance.localPlayerController.holdingWalkieTalkie || !StartOfRound.Instance.allPlayerScripts[i].holdingWalkieTalkie))
			{
				HUDManager.Instance.playerCouldRecieveTextChatAnimator.SetTrigger("ping");
				break;
			}
		}
		localPlayerController.isTypingChat = false;
		HUDManager.Instance.chatTextField.text = "";
		EventSystem.current.SetSelectedGameObject((GameObject)null);
		HUDManager.Instance.PingHUDElement(HUDManager.Instance.Chat, 2f, 1f, 0.2f);
		((Behaviour)HUDManager.Instance.typingIndicator).enabled = false;
	}

	[HarmonyPrefix]
	[HarmonyPatch("SubmitChat_performed")]
	private static void PrefixSubmitChat_performed(ref bool __runOriginal)
	{
		__runOriginal = false;
	}
}
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()
		{
			normalRegex = new Regex(Plugin.configNormalRegexPattern.Value);
			transmitRegex = new Regex(Plugin.configTransmitRegexPattern.Value);
			try
			{
				string text = Path.Combine(Path.GetDirectoryName(((BaseUnityPlugin)Plugin.Instance).Config.ConfigFilePath), Plugin.configFontAssetPath.Value);
				Plugin.LogInfo("Font path: " + text);
				DirectoryInfo directoryInfo = new DirectoryInfo(text);
				if (!directoryInfo.Exists)
				{
					Plugin.LogError("Font directory not found: " + text);
					return;
				}
				FileInfo[] files = directoryInfo.GetFiles("*");
				Array.Sort(files, (FileInfo a, FileInfo b) => string.CompareOrdinal(a.Name, b.Name));
				int num = 0;
				int num2 = 0;
				FileInfo[] array = files;
				foreach (FileInfo fileInfo in array)
				{
					if (fileInfo.Extension.Equals(".manifest", StringComparison.OrdinalIgnoreCase) || fileInfo.Extension.Equals(".meta", StringComparison.OrdinalIgnoreCase))
					{
						continue;
					}
					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)";
							EnsureMainTexture(fontBundle.Normal);
							DiagnoseFontAsset(fontBundle.Normal, 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)";
							EnsureMainTexture(fontBundle.Transmit);
							DiagnoseFontAsset(fontBundle.Transmit, 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++;
					}
				}
				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());
			}
		}

		private static void EnsureMainTexture(TMP_FontAsset font)
		{
			if (Object.op_Implicit((Object)(object)((TMP_Asset)font).material) && !Object.op_Implicit((Object)(object)((TMP_Asset)font).material.GetTexture(ShaderUtilities.ID_MainTex)) && font.atlasTextures != null && font.atlasTextures.Length != 0 && Object.op_Implicit((Object)(object)font.atlasTextures[0]))
			{
				((TMP_Asset)font).material.mainTexture = (Texture)(object)font.atlasTextures[0];
				Plugin.LogInfo("[" + ((Object)font).name + "] material._MainTex was null; re-bound to atlasTextures[0]");
			}
		}

		private static void DiagnoseFontAsset(TMP_FontAsset font, string label)
		{
			//IL_0215: Unknown result type (might be due to invalid IL or missing references)
			//IL_01eb: Unknown result type (might be due to invalid IL or missing references)
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("[" + label + "] material=" + (Object.op_Implicit((Object)(object)((TMP_Asset)font).material) ? "ok" : "NULL"));
			if (Object.op_Implicit((Object)(object)((TMP_Asset)font).material))
			{
				Material material = ((TMP_Asset)font).material;
				stringBuilder.Append(", shader=" + (Object.op_Implicit((Object)(object)material.shader) ? ((Object)material.shader).name : "NULL"));
				stringBuilder.Append(", _MainTex=" + (Object.op_Implicit((Object)(object)material.GetTexture(ShaderUtilities.ID_MainTex)) ? "ok" : "NULL"));
				stringBuilder.Append(", _GradientScale=" + DescribeFloat(material, ShaderUtilities.ID_GradientScale));
				stringBuilder.Append(", _TextureWidth=" + DescribeFloat(material, ShaderUtilities.ID_TextureWidth));
				stringBuilder.Append(", _TextureHeight=" + DescribeFloat(material, ShaderUtilities.ID_TextureHeight));
				stringBuilder.Append(", _WeightNormal=" + DescribeFloat(material, ShaderUtilities.ID_WeightNormal));
				stringBuilder.Append(", _WeightBold=" + DescribeFloat(material, ShaderUtilities.ID_WeightBold));
				stringBuilder.Append(", _FaceDilate=" + DescribeFloat(material, ShaderUtilities.ID_FaceDilate));
				stringBuilder.Append(", _UnderlayDilate=" + DescribeFloat(material, ShaderUtilities.ID_UnderlayDilate));
			}
			stringBuilder.Append($", atlasTextures={((font.atlasTextures != null) ? font.atlasTextures.Length : 0)}");
			if (font.atlasTextures != null && font.atlasTextures.Length != 0 && Object.op_Implicit((Object)(object)font.atlasTextures[0]))
			{
				stringBuilder.Append($", atlasPage0={((Texture)font.atlasTextures[0]).width}x{((Texture)font.atlasTextures[0]).height}/{((Texture)font.atlasTextures[0]).filterMode}");
			}
			else
			{
				stringBuilder.Append(", atlasPage0=NULL");
			}
			stringBuilder.Append($", populationMode={font.atlasPopulationMode}");
			stringBuilder.Append(", sourceFontFile=" + (Object.op_Implicit((Object)(object)font.sourceFontFile) ? "ok" : "NULL"));
			Plugin.LogInfo(stringBuilder.ToString());
		}

		private static string DescribeFloat(Material m, int propertyId)
		{
			if (!m.HasProperty(propertyId))
			{
				return "absent";
			}
			return m.GetFloat(propertyId).ToString("0.##");
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(TMP_MaterialManager), "GetFallbackMaterial", new Type[]
		{
			typeof(Material),
			typeof(Material)
		})]
		private static void LogDerivedFallbackMaterial(Material sourceMaterial, Material targetMaterial, Material __result)
		{
			if (!((Object)(object)__result == (Object)null))
			{
				Plugin.LogInfo("[fallback material] source=" + ((Object.op_Implicit((Object)(object)sourceMaterial) && Object.op_Implicit((Object)(object)sourceMaterial.shader)) ? ((Object)sourceMaterial.shader).name : "NULL") + " target=" + ((Object.op_Implicit((Object)(object)targetMaterial) && Object.op_Implicit((Object)(object)targetMaterial.shader)) ? ((Object)targetMaterial.shader).name : "NULL") + " -> shader=" + (Object.op_Implicit((Object)(object)__result.shader) ? ((Object)__result.shader).name : "NULL") + " _GradientScale=" + DescribeFloat(__result, ShaderUtilities.ID_GradientScale) + " _FaceDilate=" + DescribeFloat(__result, ShaderUtilities.ID_FaceDilate) + " _OutlineWidth=" + DescribeFloat(__result, ShaderUtilities.ID_OutlineWidth) + " _UnderlayDilate=" + DescribeFloat(__result, ShaderUtilities.ID_UnderlayDilate) + " _MainTex=" + (Object.op_Implicit((Object)(object)__result.GetTexture(ShaderUtilities.ID_MainTex)) ? "ok" : "NULL"));
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(TMP_FontAsset), "Awake")]
		private static void PatchFontAwake(TMP_FontAsset __instance)
		{
			((TMP_Asset)__instance).material.SetFloat("_UnderlayDilate", 1f);
			((TMP_Asset)__instance).material.SetFloat("_UnderlayOffsetX", 0.1f);
			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.op_Implicit((Object)(object)fontBundle.Normal) && !__instance.fallbackFontAssetTable.Contains(fontBundle.Normal))
					{
						__instance.fallbackFontAssetTable.Add(fontBundle.Normal);
						num++;
					}
				}
				if (num > 0)
				{
					Plugin.LogInfo($"[{name}] font patched (Normal): +{num} fallback(s), total={__instance.fallbackFontAssetTable.Count}");
				}
			}
			else if (transmitRegex.IsMatch(name))
			{
				if (!Plugin.configTransmitIngameFont.Value)
				{
					DisableFont(__instance);
				}
				int num2 = 0;
				foreach (FontBundle fontBundle2 in fontBundles)
				{
					if (Object.op_Implicit((Object)(object)fontBundle2.Transmit) && !__instance.fallbackFontAssetTable.Contains(fontBundle2.Transmit))
					{
						__instance.fallbackFontAssetTable.Add(fontBundle2.Transmit);
						num2++;
					}
				}
				if (num2 > 0)
				{
					Plugin.LogInfo($"[{name}] font patched (Transmit): +{num2} fallback(s), total={__instance.fallbackFontAssetTable.Count}");
				}
			}
			else
			{
				Plugin.LogWarning("[" + name + "] not patched");
			}
		}

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

		[HarmonyPostfix]
		[HarmonyPatch(typeof(TMP_FontAsset), "SetupNewAtlasTexture")]
		private static void PatchSetupNewAtlasTexture(TMP_FontAsset __instance)
		{
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			if (__instance.atlasTextures == null || __instance.atlasTextures.Length == 0 || !Object.op_Implicit((Object)(object)__instance.atlasTextures[0]))
			{
				return;
			}
			FilterMode filterMode = ((Texture)__instance.atlasTextures[0]).filterMode;
			TextureWrapMode wrapMode = ((Texture)__instance.atlasTextures[0]).wrapMode;
			Texture2D[] atlasTextures = __instance.atlasTextures;
			foreach (Texture2D val in atlasTextures)
			{
				if (Object.op_Implicit((Object)(object)val))
				{
					((Texture)val).filterMode = filterMode;
					((Texture)val).wrapMode = wrapMode;
				}
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(TextMeshProUGUI), "Awake")]
		private static void PatchTextAwake(TextMeshProUGUI __instance)
		{
			if (!((Object)(object)((TMP_Text)__instance).font == (Object)null))
			{
				PatchFontAwake(((TMP_Text)__instance).font);
			}
		}

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

		public const string Name = "LCChineseFont";

		public const string Version = "1.2.5";
	}
	[BepInPlugin("syrupdesu.lcchinesefont", "LCChineseFont", "1.2.5")]
	internal class Plugin : BaseUnityPlugin
	{
		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;

		public static Plugin Instance;

		private void Awake()
		{
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
			}
			configNormalIngameFont = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "UsingNormalIngameFont", true, "Using in-game default normal font");
			configTransmitIngameFont = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "UsingTransmitIngameFont", true, "Using in-game default normal font");
			configNormalRegexPattern = ((BaseUnityPlugin)this).Config.Bind<string>("Regex", "NormalFontNameRegex", "^(b|DialogueText|3270.*)$", "Normally, you don't neet to change it");
			configTransmitRegexPattern = ((BaseUnityPlugin)this).Config.Bind<string>("Regex", "TransmitFontNameRegex", "^edunline.*$", "Normally, you don't neet to change it");
			configFontAssetPath = ((BaseUnityPlugin)this).Config.Bind<string>("Path", "FontAssetsPath", "FontPatcher\\default", (ConfigDescription)null);
			configDebugLog = ((BaseUnityPlugin)this).Config.Bind<bool>("Debug", "Log", false, (ConfigDescription)null);
			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 = "LCChineseFont";

		public const string PLUGIN_NAME = "LCChineseFont";

		public const string PLUGIN_VERSION = "1.2.5";
	}
}