Merge branch 'translator-text' of https://github.com/misternebula/quantum-space-buddies into translator-text

This commit is contained in:
Mister_Nebula 2022-06-30 19:33:32 +01:00
commit 2de81eb580

View File

@ -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<MonoBehaviour>();
var allFonts = Resources.LoadAll<Font>("fonts/english - latin");
var allFonts = Resources.LoadAll("fonts/english - latin", typeof(Font));
foreach (var item in everyBehaviour)
foreach (var monoBehaviour in prefab.GetComponentsInChildren<MonoBehaviour>(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);
}
}
}