Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BitmapFont now always loads from TitleContainer. #946

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions source/MonoGame.Extended/BitmapFonts/BitmapFont.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,13 +364,19 @@ public void Reset()

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<string, Texture2D> pages = new Dictionary<string, Texture2D>();
Expand All @@ -379,7 +385,7 @@ public static BitmapFont FromStream(GraphicsDevice graphicsDevice, FileStream st
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>
/// Reads the content of the font file at the path specified.
/// </summary>
/// <param name="stream">A <see cref="FileStream"/> containing the font file contents to read.</param>
/// <param name="stream">A <see cref="Stream"/> containing the font file contents to read.</param>
/// <returns>A <see cref="BitmapFontFileContent"/> instance containing the results of the read operation.</returns>
/// <exception cref="InvalidOperationException">
/// Thrown if the header for the file contents does not match a known header format.
/// </exception>
[Obsolete("Use the overload that takes an explicit name parameter.")]
public static BitmapFontFileContent Read(FileStream stream)
{
return Read(stream, stream.Name);
}

/// <summary>
/// Reads the content of the font file at the path specified.
/// </summary>
/// <param name="stream">A <see cref="Stream"/> containing the font file contents to read.</param>
/// <param name="name">The name or path that uniquely identifies this <see cref="BitmapFontFileContent"/>.</param>
/// <returns>A <see cref="BitmapFontFileContent"/> instance containing the results of the read operation.</returns>
/// <exception cref="InvalidOperationException">
/// Thrown if the header for the file contents does not match a known header format.
/// </exception>
public static BitmapFontFileContent Read(Stream stream, string name)
{
long position = stream.Position;
var sig = stream.ReadByte();
Expand All @@ -60,7 +75,7 @@ public static BitmapFontFileContent Read(FileStream stream)
_ => throw new InvalidOperationException("This does not appear to be a valid BMFont file!")
};

bmfFile.Path = stream.Name;
bmfFile.Path = name;

return bmfFile;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,9 @@ private static BitmapFontFileContent CreateExpected()
[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);
Expand All @@ -116,8 +117,9 @@ public void Read_BinaryFile_Test()
[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);
Expand All @@ -130,8 +132,9 @@ public void Read_XmlFile_Test()
[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);
Expand Down
Loading