From d7e69e1349201b59d7e109f942f6d1d35d09a049 Mon Sep 17 00:00:00 2001 From: IrishBruse Date: Sun, 15 Jan 2023 03:14:58 +0000 Subject: [PATCH] Fix path crash --- LDtk.Example/LDtkMonogameGame.cs | 1 + LDtk/JsonPartials/LDtkFile.cs | 29 ++++++++++++----------------- LDtk/JsonPartials/LDtkLevel.cs | 3 +++ LDtk/JsonPartials/LDtkWorld.cs | 1 + LDtk/Renderer/LDtkRenderer.cs | 7 +++---- 5 files changed, 20 insertions(+), 21 deletions(-) diff --git a/LDtk.Example/LDtkMonogameGame.cs b/LDtk.Example/LDtkMonogameGame.cs index e43d0de9..ab0b995e 100644 --- a/LDtk.Example/LDtkMonogameGame.cs +++ b/LDtk.Example/LDtkMonogameGame.cs @@ -14,6 +14,7 @@ namespace LDtkMonogameExample; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; + public class LDtkMonogameGame : Game { // LDtk stuff diff --git a/LDtk/JsonPartials/LDtkFile.cs b/LDtk/JsonPartials/LDtkFile.cs index 89bf47fc..dce6ef6d 100644 --- a/LDtk/JsonPartials/LDtkFile.cs +++ b/LDtk/JsonPartials/LDtkFile.cs @@ -64,27 +64,22 @@ public LDtkWorld LoadWorld(Guid iid) { foreach (LDtkWorld world in Worlds) { - if (world.Iid == iid) + if (world.Iid != iid) { - world.FilePath = FilePath; - if (world.Levels != null) + continue; + } + world.FilePath = FilePath; + foreach (LDtkLevel level in world.Levels) + { + if (level.ExternalRelPath != null) { - foreach (LDtkLevel level in world.Levels) - { - if (level.ExternalRelPath != null) - { - level.FilePath = Path.Join(Path.GetDirectoryName(FilePath), level.ExternalRelPath); - } - else - { - level.FilePath = FilePath; - } - } + level.FilePath = Path.Join(Path.GetDirectoryName(FilePath), level.ExternalRelPath); } - - world.Content = Content; - return world; + level.WorldFilePath = world.FilePath; } + + world.Content = Content; + return world; } return null; } diff --git a/LDtk/JsonPartials/LDtkLevel.cs b/LDtk/JsonPartials/LDtkLevel.cs index 69bceb81..47af0cbd 100644 --- a/LDtk/JsonPartials/LDtkLevel.cs +++ b/LDtk/JsonPartials/LDtkLevel.cs @@ -16,6 +16,9 @@ public partial class LDtkLevel /// The absolute filepath to the level [JsonIgnore] public string FilePath { get; set; } + /// The absolute filepath to the world + [JsonIgnore] public string WorldFilePath { get; set; } + /// World Position of the level in pixels [JsonIgnore] public Point Position => new(WorldX, WorldY); diff --git a/LDtk/JsonPartials/LDtkWorld.cs b/LDtk/JsonPartials/LDtkWorld.cs index b2cd9ce8..97b168a2 100644 --- a/LDtk/JsonPartials/LDtkWorld.cs +++ b/LDtk/JsonPartials/LDtkWorld.cs @@ -126,6 +126,7 @@ private LDtkLevel LoadLevel(LDtkLevel rawLevel) } level.ExternalRelPath = rawLevel.ExternalRelPath; + level.WorldFilePath = FilePath; level.Loaded = true; } else diff --git a/LDtk/Renderer/LDtkRenderer.cs b/LDtk/Renderer/LDtkRenderer.cs index 412018da..2c47be9f 100644 --- a/LDtk/Renderer/LDtkRenderer.cs +++ b/LDtk/Renderer/LDtkRenderer.cs @@ -112,7 +112,6 @@ private Texture2D[] RenderLayers(LDtkLevel level) SpriteEffects mirror = (SpriteEffects)tile.F; SpriteBatch.Draw(texture, position, rect, Color.White, 0, Vector2.Zero, 1f, mirror, 0); } - break; case LayerType.AutoLayer: @@ -127,7 +126,6 @@ private Texture2D[] RenderLayers(LDtkLevel level) SpriteBatch.Draw(texture, position, rect, Color.White, 0, Vector2.Zero, 1f, mirror, 0); } } - break; case LayerType.Entities: @@ -163,8 +161,9 @@ private Texture2D GetTexture(LDtkLevel level, string path) { if (!string.IsNullOrWhiteSpace(level.FilePath)) { - string filePath = Path.GetDirectoryName(level.FilePath); - return Texture2D.FromFile(graphicsDevice, Path.GetFullPath(Path.Combine(filePath, "../", path))); + string filePath = Path.GetDirectoryName(level.WorldFilePath); + string absolutePath = Path.GetFullPath(Path.Combine(filePath, path)); + return Texture2D.FromFile(graphicsDevice, absolutePath); } return Texture2D.FromFile(graphicsDevice, Path.Combine("Content", path)); }