Decompiled source of JaggyFontFix v1.2.2

JaggyFontFix/JaggyFontFix.dll

Decompiled 2 weeks ago
using System;
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 BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using JaggyFontFix;
using Microsoft.CodeAnalysis;
using On.RoR2;
using On.RoR2.UI;
using RoR2;
using RoR2.UI;
using TMPro;
using UnityEngine;
using UnityEngine.TextCore.LowLevel;

[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("JaggyFontFix")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+a47ea43a847a6217f4c3a39e61d2f7f4293938bf")]
[assembly: AssemblyProduct("JaggyFontFix")]
[assembly: AssemblyTitle("JaggyFontFix")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.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;
		}
	}
}
public static class FontAssets
{
	public static AssetBundle mainBundle;

	public const string bundleName = "zh_font";

	public static string AssetBundlePath => Path.Combine(Path.GetDirectoryName(global::JaggyFontFix.JaggyFontFix.PInfo.Location), "zh_font");

	public static void Init()
	{
		mainBundle = AssetBundle.LoadFromFile(AssetBundlePath);
		Log.Info(((Object)mainBundle).name + " bundle loaded.");
	}
}
namespace JaggyFontFix
{
	[BepInPlugin("xykle.JaggyFontFix", "JaggyFontFix", "1.2.2")]
	public class JaggyFontFix : BaseUnityPlugin
	{
		public const string PluginGUID = "xykle.JaggyFontFix";

		public const string PluginAuthor = "xykle";

		public const string PluginName = "JaggyFontFix";

		public const string PluginVersion = "1.2.2";

		private TMP_FontAsset fontAsset;

		public static PluginInfo PInfo { get; private set; }

		public static ConfigEntry<UseFontName> UseFontNameConfig { get; set; }

		public static ConfigEntry<string> CustomFontFileNameConfig { get; set; }

		private void Init()
		{
			PInfo = ((BaseUnityPlugin)this).Info;
			Log.Init(((BaseUnityPlugin)this).Logger);
			FontAssets.Init();
			ConfigurationInit();
		}

		private void ConfigurationInit()
		{
			UseFontNameConfig = ((BaseUnityPlugin)this).Config.Bind<UseFontName>("Font Settings", "Font Name", UseFontName.NotoSans, "Set which font will use in game. If you want use custom font, set this to \"Custom\".");
			CustomFontFileNameConfig = ((BaseUnityPlugin)this).Config.Bind<string>("Font Settings", "Custom Font File Name", "font.ttf", "Set custom font file name that you wanna use in game. Only work when \"Font Name\" set to \"Custom\".\nYou NEED to put your font file under this plugin folder, path to font file is not supported.\nFile extension is optional.");
		}

		public void Awake()
		{
			Init();
			GenerateFontAsset();
			SubcribeEvents();
		}

		private void OnDestory()
		{
			UnSubcribeEvents();
		}

		private void SubcribeEvents()
		{
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Expected O, but got Unknown
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Expected O, but got Unknown
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Expected O, but got Unknown
			HGTextMeshProUGUI.Awake += new hook_Awake(ReplaceHGTextFont);
			LanguageTextMeshController.CacheComponents += new hook_CacheComponents(ReplaceTmpControllerFont);
			Run.InstantiateUi += new hook_InstantiateUi(ReplaceFontOnStageBegin);
		}

		private void UnSubcribeEvents()
		{
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Expected O, but got Unknown
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Expected O, but got Unknown
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Expected O, but got Unknown
			HGTextMeshProUGUI.Awake -= new hook_Awake(ReplaceHGTextFont);
			LanguageTextMeshController.CacheComponents -= new hook_CacheComponents(ReplaceTmpControllerFont);
			Run.InstantiateUi -= new hook_InstantiateUi(ReplaceFontOnStageBegin);
		}

		private Font LoadFont(UseFontName fontName)
		{
			Font val = null;
			return (Font)(fontName switch
			{
				UseFontName.TaipeiSans => FontAssets.mainBundle.LoadAsset<Font>("TaipeiSansTCBeta-Regular"), 
				UseFontName.Cubic11 => FontAssets.mainBundle.LoadAsset<Font>("Cubic_11"), 
				UseFontName.Custom => FontFromFile(CustomFontFileNameConfig.Value), 
				_ => FontAssets.mainBundle.LoadAsset<Font>("NotoSansCJKsc-Regular"), 
			});
		}

		private Font FontFromFile(string fileName)
		{
			//IL_00db: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e1: Expected O, but got Unknown
			string text = Path.Combine(Path.GetDirectoryName(PInfo.Location), fileName);
			if (!fileName.Contains(".ttf") && !fileName.Contains(".otf"))
			{
				if (File.Exists(text + ".ttf"))
				{
					text += ".ttf";
				}
				else
				{
					if (!File.Exists(text + ".otf"))
					{
						Log.Warning("Custom font file not found! Is there any typo or not support format? (Only .ttf and .otf are supported.) Input name: " + fileName);
						return FontAssets.mainBundle.LoadAsset<Font>("NotoSansCJKsc-Regular");
					}
					text += ".otf";
				}
			}
			if (!File.Exists(text))
			{
				Log.Warning("Custom font file not found! Is there any typo? Input name: " + fileName);
				return FontAssets.mainBundle.LoadAsset<Font>("NotoSansCJKsc-Regular");
			}
			Font result = new Font(text);
			Log.Info("Loading \"" + text + "\".");
			return result;
		}

		private GameObject ReplaceFontOnStageBegin(orig_InstantiateUi orig, Run self, Transform uiRoot)
		{
			GameObject result = orig.Invoke(self, uiRoot);
			int num = ReplaceAllFont();
			Log.Info($"New stage begun, replace {num} font(s) on scene.");
			return result;
		}

		private void ReplaceTmpControllerFont(orig_CacheComponents orig, LanguageTextMeshController self)
		{
			orig.Invoke(self);
			if (!((Object)(object)self.textMeshPro == (Object)null))
			{
				self.textMeshPro.font = fontAsset;
			}
		}

		private void ReplaceHGTextFont(orig_Awake orig, HGTextMeshProUGUI self)
		{
			orig.Invoke(self);
			((TMP_Text)self).font = fontAsset;
			((TextMeshProUGUI)self).UpdateFontAsset();
		}

		private int ReplaceAllFont()
		{
			TMP_Text[] array = Object.FindObjectsByType<TMP_Text>((FindObjectsInactive)1, (FindObjectsSortMode)0);
			int num = 0;
			TMP_Text[] array2 = array;
			foreach (TMP_Text val in array2)
			{
				if (!((Object)(object)val.font == (Object)(object)fontAsset))
				{
					val.font = fontAsset;
					num++;
				}
			}
			return num;
		}

		private void GenerateFontAsset()
		{
			fontAsset = null;
			Font val = LoadFont(UseFontNameConfig.Value);
			Log.Info(((Object)val).name + " is loaded.");
			fontAsset = TMP_FontAsset.CreateFontAsset(val, 90, 9, (GlyphRenderMode)4165, 4096, 2048, (AtlasPopulationMode)1, true);
			Material material = ((TMP_Asset)fontAsset).material;
			material.SetFloat("_UnderlayOffsetX", 0.3f);
			material.SetFloat("_UnderlayOffsetY", -0.3f);
			material.SetFloat("_UnderlayDilate", 0.5f);
			material.SetFloat("_UnderlaySoftness", 0.1f);
			material.EnableKeyword("UNDERLAY_ON");
			((TMP_Asset)fontAsset).material = material;
		}
	}
	public enum UseFontName
	{
		NotoSans,
		TaipeiSans,
		Cubic11,
		Custom
	}
	internal static class Log
	{
		private static ManualLogSource _logSource;

		internal static void Init(ManualLogSource logSource)
		{
			_logSource = logSource;
		}

		internal static void Debug(object data)
		{
			_logSource.LogDebug(data);
		}

		internal static void Error(object data)
		{
			_logSource.LogError(data);
		}

		internal static void Fatal(object data)
		{
			_logSource.LogFatal(data);
		}

		internal static void Info(object data)
		{
			_logSource.LogInfo(data);
		}

		internal static void Message(object data)
		{
			_logSource.LogMessage(data);
		}

		internal static void Warning(object data)
		{
			_logSource.LogWarning(data);
		}
	}
}