Skip to content

Commit 0038a1e

Browse files
Merge pull request #2579 from Jagget/Support-loading-font-file-from-.dfmod
Support loading font file from .dfmod
2 parents 6d62f17 + 3060936 commit 0038a1e

File tree

3 files changed

+43
-19
lines changed

3 files changed

+43
-19
lines changed

Assets/Game/Addons/ModSupport/ModManager.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,7 @@ void Awake()
133133
{
134134
if (string.IsNullOrEmpty(ModDirectory))
135135
ModDirectory = Path.Combine(Application.streamingAssetsPath, "Mods");
136-
}
137136

138-
void Start()
139-
{
140137
SetupSingleton();
141138

142139
if (Instance == this)

Assets/Game/Addons/ModSupport/ModManager.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/Game/UserInterface/DaggerfallFont.cs

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System.Text;
1717
using DaggerfallConnect;
1818
using DaggerfallConnect.Arena2;
19+
using DaggerfallWorkshop.Game.Utility.ModSupport;
1920
using DaggerfallWorkshop.Utility;
2021
using TMPro;
2122

@@ -265,6 +266,7 @@ public float DrawSDFGlyph(SDFGlyphInfo glyph, Vector2 position, Vector2 scale, C
265266
{
266267
Graphics.DrawTexture(targetRect, sdfFontInfo.Value.atlasTexture, glyph.rect, 0, 0, 0, 0, color, DaggerfallUI.Instance.SDFFontMaterial);
267268
}
269+
268270
return GetGlyphWidth(glyph, scale, GlyphSpacing);
269271
}
270272

@@ -640,33 +642,58 @@ float GetGlyphSpacing()
640642
}
641643

642644
/// <summary>
643-
/// Replace TMP font asset using a .ttf or .otf font in StreamingAssets/Fonts.
644-
/// TODO: Support loading font file from .dfmod.
645+
/// Replace TMP font asset using a .ttf or .otf font in StreamingAssets/Fonts or in dfmod asset
645646
/// </summary>
646-
/// <param name="filename">Filename of replacement font including .ttf extension. Font file must be present in StreamingAssets/Fonts to load.</param>
647+
/// <param name="filename">Filename of replacement font including .ttf extension. Font file must be present in StreamingAssets/Fonts or in dfmod asset to load.</param>
647648
/// <param name="source">Source TMP font for initial character table population.</param>
648649
/// <param name="replacement">Replacement TMP font output.</param>
649650
/// <returns>True is successfully created replacement TMP font.</returns>
650651
bool ReplaceTMPFontFromFile(string filename, TMP_FontAsset source, out TMP_FontAsset replacement)
651652
{
652653
const string ttfExt = ".ttf";
653654
const string otfExt = ".otf";
655+
replacement = null;
656+
657+
Font otfFontFromMods = null;
658+
Font ttfFontFromMods = null;
659+
660+
// Seek font replacement file from mods
661+
if (ModManager.Instance != null)
662+
{
663+
if (!ModManager.Instance.TryGetAsset(filename + otfExt, false, out otfFontFromMods))
664+
{
665+
ModManager.Instance.TryGetAsset(filename + ttfExt, false, out ttfFontFromMods);
666+
}
667+
}
668+
669+
if (otfFontFromMods != null)
670+
{
671+
replacement = TMP_FontAsset.CreateFontAsset(otfFontFromMods, 45, 6, UnityEngine.TextCore.LowLevel.GlyphRenderMode.SDFAA, 4096, 4096, AtlasPopulationMode.Dynamic);
672+
}
673+
else if (ttfFontFromMods != null)
674+
{
675+
replacement = TMP_FontAsset.CreateFontAsset(ttfFontFromMods, 45, 6, UnityEngine.TextCore.LowLevel.GlyphRenderMode.SDFAA, 4096, 4096, AtlasPopulationMode.Dynamic);
676+
}
654677

655678
// Compose path to font file
656-
string path = Path.Combine(Application.streamingAssetsPath, "Fonts", filename);
679+
var path = Path.Combine(Application.streamingAssetsPath, "Fonts", filename);
657680

658-
// Check file exists
659-
replacement = null;
660-
if (File.Exists(path + ttfExt))
661-
path += ttfExt;
662-
else if (File.Exists(path + otfExt))
663-
path += otfExt;
664-
else
665-
return false;
681+
if (replacement == null)
682+
{
683+
// Check file exists
684+
replacement = null;
685+
if (File.Exists(path + ttfExt))
686+
path += ttfExt;
687+
else if (File.Exists(path + otfExt))
688+
path += otfExt;
689+
else
690+
return false;
691+
692+
// Create replacement TMP font asset from path
693+
Font replacementFont = new Font(path);
694+
replacement = TMP_FontAsset.CreateFontAsset(replacementFont, 45, 6, UnityEngine.TextCore.LowLevel.GlyphRenderMode.SDFAA, 4096, 4096, AtlasPopulationMode.Dynamic);
695+
}
666696

667-
// Create replacement TMP font asset from path
668-
Font font = new Font(path);
669-
replacement = TMP_FontAsset.CreateFontAsset(font, 45, 6, UnityEngine.TextCore.LowLevel.GlyphRenderMode.SDFAA, 4096, 4096, AtlasPopulationMode.Dynamic);
670697
if (replacement == null)
671698
return false;
672699

0 commit comments

Comments
 (0)