From 73562b691b94a37e0dd2cbfedae2e2586effe8f7 Mon Sep 17 00:00:00 2001 From: Dwergi Date: Mon, 23 Sep 2024 08:33:04 +0300 Subject: [PATCH] BitmapFont now always loads from TitleContainer. (#946) Fixes KNI web builds when using BitmapFont (#944) --- .../BitmapFonts/BitmapFont.cs | 14 +++++++++---- .../BitmapFonts/BitmapFontFileReader.cs | 21 ++++++++++++++++--- .../BitmapFonts/BitmapFontFileReaderTests.cs | 15 +++++++------ 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/source/MonoGame.Extended/BitmapFonts/BitmapFont.cs b/source/MonoGame.Extended/BitmapFonts/BitmapFont.cs index b982c571..b174b569 100644 --- a/source/MonoGame.Extended/BitmapFonts/BitmapFont.cs +++ b/source/MonoGame.Extended/BitmapFonts/BitmapFont.cs @@ -364,13 +364,19 @@ public sealed class BitmapFont public static BitmapFont FromFile(GraphicsDevice graphicsDevice, string path) { - using FileStream stream = File.OpenRead(path); - return FromStream(graphicsDevice, stream); + using Stream stream = TitleContainer.OpenStream(path); + return FromStream(graphicsDevice, stream, path); } + [Obsolete("Use the FromStream() overload that takes an explicit name.")] public static BitmapFont FromStream(GraphicsDevice graphicsDevice, FileStream stream) { - var bmfFile = BitmapFontFileReader.Read(stream); + return FromStream(graphicsDevice, stream, stream.Name); + } + + public static BitmapFont FromStream(GraphicsDevice graphicsDevice, Stream stream, string name) + { + var bmfFile = BitmapFontFileReader.Read(stream, name); // Load page textures Dictionary pages = new Dictionary(); @@ -379,7 +385,7 @@ public sealed class BitmapFont if (!pages.ContainsKey(bmfFile.Pages[i])) { string texturePath = Path.Combine(Path.GetDirectoryName(bmfFile.Path), bmfFile.Pages[i]); - using (Stream textureStream = File.OpenRead(texturePath)) + using (Stream textureStream = TitleContainer.OpenStream(texturePath)) { Texture2D texture = Texture2D.FromStream(graphicsDevice, textureStream); pages.Add(bmfFile.Pages[i], texture); diff --git a/source/MonoGame.Extended/Content/BitmapFonts/BitmapFontFileReader.cs b/source/MonoGame.Extended/Content/BitmapFonts/BitmapFontFileReader.cs index 6f578477..bb9a444c 100644 --- a/source/MonoGame.Extended/Content/BitmapFonts/BitmapFontFileReader.cs +++ b/source/MonoGame.Extended/Content/BitmapFonts/BitmapFontFileReader.cs @@ -28,18 +28,33 @@ public static class BitmapFontFileReader public static BitmapFontFileContent Read(string path) { using var stream = File.OpenRead(path); - return Read(stream); + return Read(stream, path); } /// /// Reads the content of the font file at the path specified. /// - /// A containing the font file contents to read. + /// A containing the font file contents to read. /// A instance containing the results of the read operation. /// /// Thrown if the header for the file contents does not match a known header format. /// + [Obsolete("Use the overload that takes an explicit name parameter.")] public static BitmapFontFileContent Read(FileStream stream) + { + return Read(stream, stream.Name); + } + + /// + /// Reads the content of the font file at the path specified. + /// + /// A containing the font file contents to read. + /// The name or path that uniquely identifies this . + /// A instance containing the results of the read operation. + /// + /// Thrown if the header for the file contents does not match a known header format. + /// + public static BitmapFontFileContent Read(Stream stream, string name) { long position = stream.Position; var sig = stream.ReadByte(); @@ -60,7 +75,7 @@ public static class BitmapFontFileReader _ => throw new InvalidOperationException("This does not appear to be a valid BMFont file!") }; - bmfFile.Path = stream.Name; + bmfFile.Path = name; return bmfFile; } diff --git a/tests/MonoGame.Extended.Tests/BitmapFonts/BitmapFontFileReaderTests.cs b/tests/MonoGame.Extended.Tests/BitmapFonts/BitmapFontFileReaderTests.cs index 72db9eff..626deb3a 100644 --- a/tests/MonoGame.Extended.Tests/BitmapFonts/BitmapFontFileReaderTests.cs +++ b/tests/MonoGame.Extended.Tests/BitmapFonts/BitmapFontFileReaderTests.cs @@ -102,8 +102,9 @@ public class BitmapFontFileReaderTests [Fact] public void Read_BinaryFile_Test() { - using FileStream stream = File.OpenRead("BitmapFonts/files/bmfont/test-font-binary.fnt"); - var actual = BitmapFontFileReader.Read(stream); + string path = "BitmapFonts/files/bmfont/test-font-binary.fnt"; + using FileStream stream = File.OpenRead(path); + var actual = BitmapFontFileReader.Read(stream, path); Assert.Equal(_expected.Header, actual.Header); Assert.Equal(_expected.Info, actual.Info); Assert.Equal(_expected.Common, actual.Common); @@ -116,8 +117,9 @@ public class BitmapFontFileReaderTests [Fact] public void Read_XmlFile_Test() { - using FileStream stream = File.OpenRead("BitmapFonts/files/bmfont/test-font-xml.fnt"); - var actual = BitmapFontFileReader.Read(stream); + string path = "BitmapFonts/files/bmfont/test-font-xml.fnt"; + using FileStream stream = File.OpenRead(path); + var actual = BitmapFontFileReader.Read(stream, path); Assert.Equal(_expected.Header, actual.Header); Assert.Equal(_expected.Info, actual.Info); Assert.Equal(_expected.Common, actual.Common); @@ -130,8 +132,9 @@ public class BitmapFontFileReaderTests [Fact] public void Read_Text_Test() { - using FileStream stream = File.OpenRead("BitmapFonts/files/bmfont/test-font-text.fnt"); - var actual = BitmapFontFileReader.Read(stream); + string path = "BitmapFonts/files/bmfont/test-font-text.fnt"; + using FileStream stream = File.OpenRead(path); + var actual = BitmapFontFileReader.Read(stream, path); Assert.Equal(_expected.Header, actual.Header); Assert.Equal(_expected.Info, actual.Info); Assert.Equal(_expected.Common, actual.Common);