quantum-space-buddies/QSB/PlayerBodySetup/Remote/FontReplacer.cs

50 lines
1.3 KiB
C#
Raw Normal View History

2022-06-30 15:50:42 +00:00
using QSB.Utility;
using System.Linq;
using System.Reflection;
using UnityEngine;
namespace QSB.PlayerBodySetup.Remote;
public static class FontReplacer
{
public static void ReplaceFonts(GameObject prefab)
{
2022-06-30 18:22:03 +00:00
var allFonts = Resources.LoadAll<Font>("fonts/english - latin");
2022-06-30 15:50:42 +00:00
2022-06-30 18:22:03 +00:00
foreach (var monoBehaviour in prefab.GetComponentsInChildren<MonoBehaviour>(true))
2022-06-30 15:50:42 +00:00
{
2022-06-30 18:22:03 +00:00
var publicFields = monoBehaviour
.GetType()
.GetFields(BindingFlags.Public | BindingFlags.Instance);
var privateSerializedFields = monoBehaviour
2022-06-30 15:50:42 +00:00
.GetType()
.GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
2022-06-30 18:22:03 +00:00
.Where(x => x.IsDefined(typeof(SerializeField)));
2022-06-30 15:50:42 +00:00
2022-06-30 18:22:03 +00:00
foreach (var field in publicFields.Concat(privateSerializedFields))
2022-06-30 15:50:42 +00:00
{
2022-06-30 18:22:03 +00:00
if (field.FieldType != typeof(Font))
2022-06-30 15:50:42 +00:00
{
2022-06-30 18:22:03 +00:00
continue;
}
2022-06-30 15:50:42 +00:00
2022-06-30 18:22:03 +00:00
var existingFont = (Font)field.GetValue(monoBehaviour);
if (existingFont == null)
{
continue;
}
2022-06-30 15:50:42 +00:00
2022-06-30 18:22:03 +00:00
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;
2022-06-30 15:50:42 +00:00
}
2022-06-30 18:22:03 +00:00
field.SetValue(monoBehaviour, replacementFont);
2022-06-30 15:50:42 +00:00
}
}
}
}