From b49d8ce04d0d90ed3c3eb61472112c4c4f47f6b0 Mon Sep 17 00:00:00 2001 From: Jagget Date: Fri, 9 Feb 2024 23:03:59 -0500 Subject: [PATCH 1/2] Support loading font file from .dfmod --- Assets/Game/Addons/ModSupport/ModManager.cs | 6 +- .../Game/Addons/ModSupport/ModManager.cs.meta | 2 +- .../Game/UserInterface/DaggerfallFont.cs | 57 ++++++++++++++----- 3 files changed, 46 insertions(+), 19 deletions(-) diff --git a/Assets/Game/Addons/ModSupport/ModManager.cs b/Assets/Game/Addons/ModSupport/ModManager.cs index f5a8e3097b..b91780ab36 100644 --- a/Assets/Game/Addons/ModSupport/ModManager.cs +++ b/Assets/Game/Addons/ModSupport/ModManager.cs @@ -133,10 +133,10 @@ void Awake() { if (string.IsNullOrEmpty(ModDirectory)) ModDirectory = Path.Combine(Application.streamingAssetsPath, "Mods"); - } - void Start() - { + // Rest of the logic is moved from Start() method. + // There's nothing we cannot initialize right away. + SetupSingleton(); if (Instance == this) diff --git a/Assets/Game/Addons/ModSupport/ModManager.cs.meta b/Assets/Game/Addons/ModSupport/ModManager.cs.meta index 6a0a2e3ef9..fda3743894 100644 --- a/Assets/Game/Addons/ModSupport/ModManager.cs.meta +++ b/Assets/Game/Addons/ModSupport/ModManager.cs.meta @@ -5,7 +5,7 @@ licenseType: Free MonoImporter: serializedVersion: 2 defaultReferences: [] - executionOrder: 0 + executionOrder: -5 icon: {instanceID: 0} userData: assetBundleName: diff --git a/Assets/Scripts/Game/UserInterface/DaggerfallFont.cs b/Assets/Scripts/Game/UserInterface/DaggerfallFont.cs index d651a66af1..653215ba34 100644 --- a/Assets/Scripts/Game/UserInterface/DaggerfallFont.cs +++ b/Assets/Scripts/Game/UserInterface/DaggerfallFont.cs @@ -16,6 +16,7 @@ using System.Text; using DaggerfallConnect; using DaggerfallConnect.Arena2; +using DaggerfallWorkshop.Game.Utility.ModSupport; using DaggerfallWorkshop.Utility; using TMPro; @@ -265,6 +266,7 @@ public float DrawSDFGlyph(SDFGlyphInfo glyph, Vector2 position, Vector2 scale, C { Graphics.DrawTexture(targetRect, sdfFontInfo.Value.atlasTexture, glyph.rect, 0, 0, 0, 0, color, DaggerfallUI.Instance.SDFFontMaterial); } + return GetGlyphWidth(glyph, scale, GlyphSpacing); } @@ -640,10 +642,9 @@ float GetGlyphSpacing() } /// - /// Replace TMP font asset using a .ttf or .otf font in StreamingAssets/Fonts. - /// TODO: Support loading font file from .dfmod. + /// Replace TMP font asset using a .ttf or .otf font in StreamingAssets/Fonts or in dfmod asset /// - /// Filename of replacement font including .ttf extension. Font file must be present in StreamingAssets/Fonts to load. + /// Filename of replacement font including .ttf extension. Font file must be present in StreamingAssets/Fonts or in dfmod asset to load. /// Source TMP font for initial character table population. /// Replacement TMP font output. /// True is successfully created replacement TMP font. @@ -651,22 +652,48 @@ bool ReplaceTMPFontFromFile(string filename, TMP_FontAsset source, out TMP_FontA { const string ttfExt = ".ttf"; const string otfExt = ".otf"; + replacement = null; + + Font otfFontFromMods = null; + Font ttfFontFromMods = null; + + // Seek font replacement file from mods + if (ModManager.Instance != null) + { + if (!ModManager.Instance.TryGetAsset(filename + otfExt, false, out otfFontFromMods)) + { + ModManager.Instance.TryGetAsset(filename + ttfExt, false, out ttfFontFromMods); + } + } + + if (otfFontFromMods != null) + { + replacement = TMP_FontAsset.CreateFontAsset(otfFontFromMods, 45, 6, UnityEngine.TextCore.LowLevel.GlyphRenderMode.SDFAA, 4096, 4096, AtlasPopulationMode.Dynamic); + } + else if (ttfFontFromMods != null) + { + replacement = TMP_FontAsset.CreateFontAsset(ttfFontFromMods, 45, 6, UnityEngine.TextCore.LowLevel.GlyphRenderMode.SDFAA, 4096, 4096, AtlasPopulationMode.Dynamic); + } // Compose path to font file - string path = Path.Combine(Application.streamingAssetsPath, "Fonts", filename); + var path = Path.Combine(Application.streamingAssetsPath, "Fonts", filename); - // Check file exists - replacement = null; - if (File.Exists(path + ttfExt)) - path += ttfExt; - else if (File.Exists(path + otfExt)) - path += otfExt; - else - return false; + if (replacement == null) + { + // Check file exists + replacement = null; + if (File.Exists(path + ttfExt)) + path += ttfExt; + else if (File.Exists(path + otfExt)) + path += otfExt; + else + return false; + + // Create replacement TMP font asset from path + Font replacementFont = new Font(path); + replacement = TMP_FontAsset.CreateFontAsset(replacementFont, 45, 6, UnityEngine.TextCore.LowLevel.GlyphRenderMode.SDFAA, 4096, 4096, AtlasPopulationMode.Dynamic); + } - // Create replacement TMP font asset from path - Font font = new Font(path); - replacement = TMP_FontAsset.CreateFontAsset(font, 45, 6, UnityEngine.TextCore.LowLevel.GlyphRenderMode.SDFAA, 4096, 4096, AtlasPopulationMode.Dynamic); if (replacement == null) return false; From 30609369e49fb23186777937b14972d3eb493748 Mon Sep 17 00:00:00 2001 From: kaboissonneault Date: Sun, 14 Apr 2024 22:10:27 -0400 Subject: [PATCH 2/2] Removed comment from ModManager.Awake --- Assets/Game/Addons/ModSupport/ModManager.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/Assets/Game/Addons/ModSupport/ModManager.cs b/Assets/Game/Addons/ModSupport/ModManager.cs index b91780ab36..db99f790c7 100644 --- a/Assets/Game/Addons/ModSupport/ModManager.cs +++ b/Assets/Game/Addons/ModSupport/ModManager.cs @@ -134,9 +134,6 @@ void Awake() if (string.IsNullOrEmpty(ModDirectory)) ModDirectory = Path.Combine(Application.streamingAssetsPath, "Mods"); - // Rest of the logic is moved from Start() method. - // There's nothing we cannot initialize right away. - SetupSingleton(); if (Instance == this)