TextureAtlasJsonConverter.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using System.IO;
  3. using System.Text.Json;
  4. using System.Text.Json.Serialization;
  5. using Microsoft.Xna.Framework.Content;
  6. using Microsoft.Xna.Framework.Graphics;
  7. using MonoGame.Extended.Content;
  8. using MonoGame.Extended.Content.TexturePacker;
  9. using MonoGame.Extended.Graphics;
  10. namespace MonoGame.Extended.Serialization.Json
  11. {
  12. public class TextureAtlasJsonConverter : JsonConverter<Texture2DAtlas>
  13. {
  14. private readonly ContentManager _contentManager;
  15. private readonly string _path;
  16. public TextureAtlasJsonConverter(ContentManager contentManager, string path)
  17. {
  18. _contentManager = contentManager;
  19. _path = path;
  20. }
  21. /// <inheritdoc />
  22. public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(Texture2DAtlas);
  23. public override Texture2DAtlas Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
  24. {
  25. if (reader.TokenType == JsonTokenType.String)
  26. {
  27. // TODO: (Aristurtle 05/20/2024) What is this for? It's just an if block that throws an exception. Need
  28. // to investigate.
  29. var textureAtlasAssetName = reader.GetString();
  30. var contentPath = GetContentPath(textureAtlasAssetName);
  31. var texturePackerFile = _contentManager.Load<TexturePackerFileContent>(contentPath, new JsonContentLoader());
  32. var texture = _contentManager.Load<Texture2D>(texturePackerFile.Meta.Image);
  33. //return TextureAtlas.Create(texturePackerFile.Metadata.Image, texture );
  34. throw new NotImplementedException();
  35. }
  36. else
  37. {
  38. var metadata = JsonSerializer.Deserialize<InlineTextureAtlas>(ref reader, options);
  39. // TODO: When we get to .NET Standard 2.1 it would be more robust to use
  40. // [Path.GetRelativePath](https://docs.microsoft.com/en-us/dotnet/api/system.io.path.getrelativepath?view=netstandard-2.1)
  41. var textureName = Path.GetFileNameWithoutExtension(metadata.Texture);
  42. var textureDirectory = Path.GetDirectoryName(metadata.Texture);
  43. var directory = Path.GetDirectoryName(_path);
  44. var relativePath = Path.Combine(_contentManager.RootDirectory, directory, textureDirectory, textureName);
  45. var resolvedAssetName = Path.GetFullPath(relativePath);
  46. Texture2D texture;
  47. try
  48. {
  49. texture = _contentManager.Load<Texture2D>(resolvedAssetName);
  50. }
  51. catch (Exception ex)
  52. {
  53. if (textureDirectory == null || textureDirectory == "")
  54. texture = _contentManager.Load<Texture2D>(textureName);
  55. else
  56. texture = _contentManager.Load<Texture2D>(textureDirectory + "/" + textureName);
  57. }
  58. return Texture2DAtlas.Create(resolvedAssetName, texture, metadata.RegionWidth, metadata.RegionHeight);
  59. }
  60. }
  61. /// <inheritdoc />
  62. public override void Write(Utf8JsonWriter writer, Texture2DAtlas value, JsonSerializerOptions options) { }
  63. // ReSharper disable once ClassNeverInstantiated.Local
  64. private class InlineTextureAtlas
  65. {
  66. public string Texture { get; set; }
  67. public int RegionWidth { get; set; }
  68. public int RegionHeight { get; set; }
  69. }
  70. private string GetContentPath(string relativePath)
  71. {
  72. var directory = Path.GetDirectoryName(_path);
  73. return Path.Combine(directory, relativePath);
  74. }
  75. }
  76. }