#nullable enable using System; using System.Collections.Generic; using System.Runtime.CompilerServices; using ReLogic.Content; using ReLogic.Graphics; using Terraria; using Terraria.GameContent; using Terraria.ModLoader; namespace CalamityMod.Fonts { public sealed class FontAssetSystem : ModSystem { public static readonly Dictionary?> Fonts = new() { { "MouseText", FontAssets.MouseText }, { "ItemStack", FontAssets.ItemStack }, { "DeathText", FontAssets.DeathText }, { "CombatText", FontAssets.CombatText[0] }, { "CombatTextCrit", FontAssets.CombatText[1] } }; public static Asset MouseText => FontAssets.MouseText; public static Asset ItemStack => FontAssets.ItemStack; public static Asset DeathText => FontAssets.DeathText; public static Asset CombatText => FontAssets.CombatText[0]; public static Asset 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? Wingdings => ExpectFont(nameof(Wingdings)); public static Asset? CodebreakerDialog => ExpectFont(nameof(CodebreakerDialog)); public static Asset? Impact => ExpectFont(nameof(Impact)); public static Asset? Flexure => ExpectFont(nameof(Flexure)); public override void OnModLoad() { 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("Fonts/Wingdings"), fallbackFont: null); AddFont("CodebreakerDialog", Mod.Assets.Request("Fonts/CodebreakerDialog"), FontAssets.MouseText); AddFont("Impact", Mod.Assets.Request("Fonts/Impact"), FontAssets.CombatText[1]); AddFont("Flexure", Mod.Assets.Request("Fonts/Flexure"), FontAssets.CombatText[1]); } private static void AddFont(string key, Asset newFont, Asset? fallbackFont) { var font = OperatingSystem.IsWindows() ? newFont : fallbackFont; Fonts.Add(key, font); } public static Asset? ExpectFont(string fontName) { if (!Fonts.TryGetValue(fontName, out var font) || font is null) { return null; } font.Wait(); return font; } } }