feat: actually simplify font loading
This commit is contained in:
1 parent
98057b8b16
commit
5e49e8df44
3 files changed
+9
-57
No files matched your search
@@ -1,10 +1,5 @@
|
||||
#nullable enable
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using ReLogic.Content;
|
||||
using ReLogic.Content;
|
||||
using ReLogic.Graphics;
|
||||
using Terraria;
|
||||
using Terraria.GameContent;
|
||||
using Terraria.ModLoader;
|
||||
|
||||
@@ -12,14 +7,6 @@ namespace CalamityMod.Fonts
|
||||
{
|
||||
public sealed class FontAssetSystem : ModSystem
|
||||
{
|
||||
public static readonly Dictionary<string, Asset<DynamicSpriteFont>?> Fonts = new() {
|
||||
{ "MouseText", FontAssets.MouseText },
|
||||
{ "ItemStack", FontAssets.ItemStack },
|
||||
{ "DeathText", FontAssets.DeathText },
|
||||
{ "CombatText", FontAssets.CombatText[0] },
|
||||
{ "CombatTextCrit", FontAssets.CombatText[1] }
|
||||
};
|
||||
|
||||
public static Asset<DynamicSpriteFont> MouseText => FontAssets.MouseText;
|
||||
|
||||
public static Asset<DynamicSpriteFont> ItemStack => FontAssets.ItemStack;
|
||||
@@ -30,47 +17,17 @@ namespace CalamityMod.Fonts
|
||||
|
||||
public static Asset<DynamicSpriteFont> CombatTextCrit => FontAssets.CombatText[1];
|
||||
|
||||
// TODO: We could implement pretty simple caching here with the `field`
|
||||
// keyword, but not everyone has a newer SDK installed. Wait until tML
|
||||
// updates to .NET 10.
|
||||
public static Asset<DynamicSpriteFont>? Wingdings => ExpectFont(nameof(Wingdings));
|
||||
public static Asset<DynamicSpriteFont> Wingdings => field ??= GetFont("Fonts/Wingdings");
|
||||
|
||||
public static Asset<DynamicSpriteFont>? CodebreakerDialog => ExpectFont(nameof(CodebreakerDialog));
|
||||
public static Asset<DynamicSpriteFont> CodebreakerDialog => field ??= GetFont("Fonts/CodebreakerDialog");
|
||||
|
||||
public static Asset<DynamicSpriteFont>? Impact => ExpectFont(nameof(Impact));
|
||||
public static Asset<DynamicSpriteFont> Impact => field ??= GetFont("Fonts/Impact");
|
||||
|
||||
public static Asset<DynamicSpriteFont>? Flexure => ExpectFont(nameof(Flexure));
|
||||
public static Asset<DynamicSpriteFont> Flexure => field ??= GetFont("Fonts/Flexure");
|
||||
|
||||
public override void OnModLoad()
|
||||
private static Asset<DynamicSpriteFont> GetFont(string path)
|
||||
{
|
||||
if (Main.dedServ)
|
||||
return;
|
||||
|
||||
// Intentionally does not have a fallback due to it already having null checks where it is utilized to prevent them from appearing
|
||||
AddFont("Wingdings", Mod.Assets.Request<DynamicSpriteFont>("Fonts/Wingdings"), fallbackFont: null);
|
||||
AddFont("CodebreakerDialog", Mod.Assets.Request<DynamicSpriteFont>("Fonts/CodebreakerDialog"), FontAssets.MouseText);
|
||||
AddFont("Impact", Mod.Assets.Request<DynamicSpriteFont>("Fonts/Impact"), FontAssets.CombatText[1]);
|
||||
AddFont("Flexure", Mod.Assets.Request<DynamicSpriteFont>("Fonts/Flexure"), FontAssets.CombatText[1]);
|
||||
}
|
||||
|
||||
private static void AddFont(string key, Asset<DynamicSpriteFont> newFont, Asset<DynamicSpriteFont>? fallbackFont)
|
||||
{
|
||||
var font = OperatingSystem.IsWindows()
|
||||
? newFont
|
||||
: fallbackFont;
|
||||
|
||||
Fonts.Add(key, font);
|
||||
}
|
||||
|
||||
public static Asset<DynamicSpriteFont>? ExpectFont(string fontName)
|
||||
{
|
||||
if (!Fonts.TryGetValue(fontName, out var font) || font is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
font.Wait();
|
||||
return font;
|
||||
return ModContent.GetInstance<CalamityMod>().Assets.Request<DynamicSpriteFont>(path, AssetRequestMode.ImmediateLoad);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user