From fd8eead241fbb8c80bb74df4942ece1a955fece9 Mon Sep 17 00:00:00 2001
From: Christopher Whitley <103014489+AristurtleDev@users.noreply.github.com>
Date: Sun, 24 Aug 2025 03:20:29 -0400
Subject: [PATCH] Fix/textureatlas import error (#1017)
* Ensure that location of PNG is resolved relative to the JSON file directory in TexturePacker processor
* Bump version to 5.0.2
* Update tests
---
Directory.Build.props | 2 +-
README.md | 2 +-
.../TexturePackerJsonImporter.cs | 6 ++---
.../TextureAtlases/TexturePackerProcessor.cs | 18 ++++++++-------
...TexturePackerJsonImporterProcessorTests.cs | 22 +++++++++----------
5 files changed, 26 insertions(+), 24 deletions(-)
diff --git a/Directory.Build.props b/Directory.Build.props
index 857873dd..bb05b778 100644
--- a/Directory.Build.props
+++ b/Directory.Build.props
@@ -7,7 +7,7 @@
- 5.0.1
+ 5.0.2
diff --git a/README.md b/README.md
index ef85c0fd..a07105fd 100644
--- a/README.md
+++ b/README.md
@@ -11,7 +11,7 @@ MonoGame.Extended is a set of utilities (in the form of libraries/tools) to [Mon
Code is distributed as NuGet packages in the form of libraries (`.dll` files). You can easily install the NuGet packages into your existing MonoGame project using the NuGet Package Manager UI in Visual Studio or by using the command line interface (CLI) in a terminal.
```sh
-dotnet add package MonoGame.Extended --version 5.0.1
+dotnet add package MonoGame.Extended --version 5.0.2
```
### Using the Content Pipeline Extensions
diff --git a/source/MonoGame.Extended.Content.Pipeline/TextureAtlases/TexturePackerJsonImporter.cs b/source/MonoGame.Extended.Content.Pipeline/TextureAtlases/TexturePackerJsonImporter.cs
index c89c9f50..e6998e7d 100644
--- a/source/MonoGame.Extended.Content.Pipeline/TextureAtlases/TexturePackerJsonImporter.cs
+++ b/source/MonoGame.Extended.Content.Pipeline/TextureAtlases/TexturePackerJsonImporter.cs
@@ -8,9 +8,9 @@ using MonoGame.Extended.Content.TexturePacker;
namespace MonoGame.Extended.Content.Pipeline.TextureAtlases
{
[ContentImporter(".json", DefaultProcessor = "TexturePackerProcessor", DisplayName = "TexturePacker JSON Importer - MonoGame.Extended")]
- public class TexturePackerJsonImporter : ContentImporter
+ public class TexturePackerJsonImporter : ContentImporter>
{
- public override TexturePackerFileContent Import(string filename, ContentImporterContext context)
+ public override ContentImporterResult Import(string filename, ContentImporterContext context)
{
var tpFile = TexturePackerFileReader.Read(filename);
@@ -27,7 +27,7 @@ namespace MonoGame.Extended.Content.Pipeline.TextureAtlases
}
}
- return tpFile;
+ return new ContentImporterResult(filename, tpFile);
}
}
}
diff --git a/source/MonoGame.Extended.Content.Pipeline/TextureAtlases/TexturePackerProcessor.cs b/source/MonoGame.Extended.Content.Pipeline/TextureAtlases/TexturePackerProcessor.cs
index 99b0c16e..bb633d7a 100644
--- a/source/MonoGame.Extended.Content.Pipeline/TextureAtlases/TexturePackerProcessor.cs
+++ b/source/MonoGame.Extended.Content.Pipeline/TextureAtlases/TexturePackerProcessor.cs
@@ -2,6 +2,7 @@
// Licensed under the MIT license.
// See LICENSE file in the project root for full license information.
+using System.IO;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;
@@ -10,25 +11,26 @@ using MonoGame.Extended.Content.TexturePacker;
namespace MonoGame.Extended.Content.Pipeline.TextureAtlases;
[ContentProcessor(DisplayName = "TexturePacker Processor - MonoGame.Extended")]
-public class TexturePackerProcessor : ContentProcessor
+public class TexturePackerProcessor : ContentProcessor, TexturePackerProcessorResult>
{
- public override TexturePackerProcessorResult Process(TexturePackerFileContent input, ContentProcessorContext context)
+ public override TexturePackerProcessorResult Process(ContentImporterResult input, ContentProcessorContext context)
{
- if (input.Meta.Image != null)
+ if (input.Data.Meta.Image != null)
{
// Validates the texture exists and can be processed (fails build if missing)
- var externalRef = new ExternalReference(input.Meta.Image);
+ var externalRef = new ExternalReference(input.Data.Meta.Image);
context.BuildAndLoadAsset(externalRef, nameof(TextureProcessor));
}
- else if (input.Meta.DataFormat == "monogame-extended")
+ else if (input.Data.Meta.DataFormat == "monogame-extended")
{
- foreach (var texture in input.Textures)
+ foreach (var texture in input.Data.Textures)
{
- var externalRef = new ExternalReference(texture.FileName);
+ string texturePath = Path.Combine(Path.GetDirectoryName(input.FilePath), texture.FileName);
+ var externalRef = new ExternalReference(texturePath);
context.BuildAndLoadAsset(externalRef, nameof(TextureProcessor));
}
}
- return new TexturePackerProcessorResult(input);
+ return new TexturePackerProcessorResult(input.Data);
}
}
diff --git a/tests/MonoGame.Extended.Content.Pipeline.Tests/TexturePackerJsonImporterProcessorTests.cs b/tests/MonoGame.Extended.Content.Pipeline.Tests/TexturePackerJsonImporterProcessorTests.cs
index 7675d4ec..229c2775 100644
--- a/tests/MonoGame.Extended.Content.Pipeline.Tests/TexturePackerJsonImporterProcessorTests.cs
+++ b/tests/MonoGame.Extended.Content.Pipeline.Tests/TexturePackerJsonImporterProcessorTests.cs
@@ -12,19 +12,19 @@ namespace MonoGame.Extended.Content.Pipeline.Tests
{
var filePath = PathExtensions.GetApplicationFullPath(@"TestData/test-tileset.json");
var importer = new TexturePackerJsonImporter();
- var data = importer.Import(filePath, Substitute.For());
+ var importResult = importer.Import(filePath, Substitute.For());
- Assert.NotNull(data);
+ Assert.NotNull(importResult);
// Check meta.image contains image name
- Assert.Equal("test-tileset.png", data.Meta.Image);
+ Assert.Equal("test-tileset.png", importResult.Data.Meta.Image);
// Check regions count and textures
- Assert.Equal(9, data.Regions.Count);
- Assert.Null(data.Textures); // only used by new MonoGame.Extended JSON format
+ Assert.Equal(9, importResult.Data.Regions.Count);
+ Assert.Null(importResult.Data.Textures); // only used by new MonoGame.Extended JSON format
// Check first region
- var firstRegion = data.Regions[0];
+ var firstRegion = importResult.Data.Regions[0];
Assert.Equal("1.png", firstRegion.FileName);
Assert.Equal(2, firstRegion.Frame.X);
Assert.Equal(2, firstRegion.Frame.Y);
@@ -54,16 +54,16 @@ namespace MonoGame.Extended.Content.Pipeline.Tests
{
var filePath = PathExtensions.GetApplicationFullPath(@"TestData/test-atlas.json");
var importer = new TexturePackerJsonImporter();
- var data = importer.Import(filePath, Substitute.For());
+ var importResult = importer.Import(filePath, Substitute.For());
// Regions must be null (only used by old generic json format)
- Assert.Null(data.Regions);
+ Assert.Null(importResult.Data.Regions);
// Textures contains 1 texture
- Assert.NotNull(data.Textures);
- Assert.Single(data.Textures);
+ Assert.NotNull(importResult.Data.Textures);
+ Assert.Single(importResult.Data.Textures);
- var texture = data.Textures[0];
+ var texture = importResult.Data.Textures[0];
Assert.Equal("test-atlas-texture.png", texture.FileName);
// The first texture contains frames