2020-06-15 22:39:05 +00:00
|
|
|
using System.IO;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
2020-09-28 08:02:17 +00:00
|
|
|
using ConformalDecals.Util;
|
2020-06-15 22:39:05 +00:00
|
|
|
using TMPro;
|
2020-06-19 17:36:13 +00:00
|
|
|
using UniLinq;
|
2020-06-15 22:39:05 +00:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace ConformalDecals.Text {
|
2020-09-28 01:26:55 +00:00
|
|
|
/// KSP database loader for KSPFont files which contain TextMeshPro font assets
|
2020-10-04 21:40:07 +00:00
|
|
|
[DatabaseLoaderAttrib(new[] {"decalfont"})]
|
2020-06-15 22:39:05 +00:00
|
|
|
public class FontLoader : DatabaseLoader<GameDatabase.TextureInfo> {
|
2020-06-19 17:36:13 +00:00
|
|
|
private const string FallbackName = "NotoSans-Regular SDF";
|
2020-11-13 09:55:42 +00:00
|
|
|
private static TMP_FontAsset _fallbackFont;
|
2020-06-19 17:36:13 +00:00
|
|
|
|
2020-06-15 22:39:05 +00:00
|
|
|
public override IEnumerator Load(UrlDir.UrlFile urlFile, FileInfo fileInfo) {
|
2020-11-13 09:55:42 +00:00
|
|
|
if (_fallbackFont == null) {
|
|
|
|
_fallbackFont = Resources.FindObjectsOfTypeAll<TMP_FontAsset>().First(o => o.name == FallbackName);
|
|
|
|
if (_fallbackFont == null) Logging.LogError($"Could not find fallback font '{FallbackName}'");
|
2020-06-19 17:36:13 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 08:02:17 +00:00
|
|
|
Logging.Log($"Loading font file '{urlFile.fullPath}'");
|
2020-06-15 22:39:05 +00:00
|
|
|
var bundle = AssetBundle.LoadFromFile(urlFile.fullPath);
|
|
|
|
if (!bundle) {
|
2020-09-28 08:02:17 +00:00
|
|
|
Logging.Log($"Could not load font asset {urlFile.fullPath}");
|
2020-06-15 22:39:05 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
var loadedFonts = bundle.LoadAllAssets<TMP_FontAsset>();
|
|
|
|
foreach (var font in loadedFonts) {
|
2020-09-28 08:02:17 +00:00
|
|
|
Logging.Log($"Adding font {font.name}");
|
2020-11-13 09:55:42 +00:00
|
|
|
font.fallbackFontAssets.Add(_fallbackFont);
|
2020-06-15 22:39:05 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-19 17:36:13 +00:00
|
|
|
|
2020-06-15 22:39:05 +00:00
|
|
|
yield break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|