From b7985271ac19bb7c2656ec57c79ff7ecbea8e8d4 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Thu, 30 Jun 2022 11:22:03 -0700 Subject: [PATCH] improve FontReplacer a bit --- QSB/PlayerBodySetup/Remote/FontReplacer.cs | 63 +++++++++------------- 1 file changed, 26 insertions(+), 37 deletions(-) diff --git a/QSB/PlayerBodySetup/Remote/FontReplacer.cs b/QSB/PlayerBodySetup/Remote/FontReplacer.cs index 829b9667..bf974239 100644 --- a/QSB/PlayerBodySetup/Remote/FontReplacer.cs +++ b/QSB/PlayerBodySetup/Remote/FontReplacer.cs @@ -1,10 +1,6 @@ using QSB.Utility; -using System; -using System.Collections.Generic; using System.Linq; using System.Reflection; -using System.Text; -using System.Threading.Tasks; using UnityEngine; namespace QSB.PlayerBodySetup.Remote; @@ -13,47 +9,40 @@ public static class FontReplacer { public static void ReplaceFonts(GameObject prefab) { - var everyBehaviour = prefab.GetComponentsInChildren(); + var allFonts = Resources.LoadAll("fonts/english - latin"); - var allFonts = Resources.LoadAll("fonts/english - latin", typeof(Font)); - - foreach (var item in everyBehaviour) + foreach (var monoBehaviour in prefab.GetComponentsInChildren(true)) { - var publicFields = item.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance); - - var privateSerializedFields = item + var publicFields = monoBehaviour + .GetType() + .GetFields(BindingFlags.Public | BindingFlags.Instance); + var privateSerializedFields = monoBehaviour .GetType() .GetFields(BindingFlags.NonPublic | BindingFlags.Instance) - .Where(x => x.IsDefined(typeof(SerializeField), false)) - .ToArray(); + .Where(x => x.IsDefined(typeof(SerializeField))); - // concatenate the two arrays - var final = new FieldInfo[publicFields.Length + privateSerializedFields.Length]; - publicFields.CopyTo(final, 0); - privateSerializedFields.CopyTo(final, publicFields.Length); - - foreach (var field in final) + foreach (var field in publicFields.Concat(privateSerializedFields)) { - if (field.FieldType == typeof(Font)) + if (field.FieldType != typeof(Font)) { - var existingFont = (Font)field.GetValue(item); - - if (existingFont == null) - { - continue; - } - - var fontName = existingFont.name; - var replacementFont = allFonts.FirstOrDefault(x => x.name == fontName); - - if (replacementFont == null) - { - DebugLog.ToConsole($"Warning - Couldn't find replacement font for {fontName}.", OWML.Common.MessageType.Warning); - continue; - } - - field.SetValue(item, replacementFont); + continue; } + + var existingFont = (Font)field.GetValue(monoBehaviour); + if (existingFont == null) + { + continue; + } + + var fontName = existingFont.name; + var replacementFont = allFonts.FirstOrDefault(x => x.name == fontName); + if (replacementFont == null) + { + DebugLog.ToConsole($"Warning - Couldn't find replacement font for {fontName}.", OWML.Common.MessageType.Warning); + continue; + } + + field.SetValue(monoBehaviour, replacementFont); } } }