Skip to content

Commit

Permalink
Fix path crash
Browse files Browse the repository at this point in the history
  • Loading branch information
IrishBruse committed Jan 15, 2023
1 parent f31f9db commit d7e69e1
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 21 deletions.
1 change: 1 addition & 0 deletions LDtk.Example/LDtkMonogameGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace LDtkMonogameExample;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;


public class LDtkMonogameGame : Game
{
// LDtk stuff
Expand Down
29 changes: 12 additions & 17 deletions LDtk/JsonPartials/LDtkFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
3 changes: 3 additions & 0 deletions LDtk/JsonPartials/LDtkLevel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public partial class LDtkLevel
/// <summary> The absolute filepath to the level </summary>
[JsonIgnore] public string FilePath { get; set; }

/// <summary> The absolute filepath to the world </summary>
[JsonIgnore] public string WorldFilePath { get; set; }

/// <summary> World Position of the level in pixels </summary>
[JsonIgnore] public Point Position => new(WorldX, WorldY);

Expand Down
1 change: 1 addition & 0 deletions LDtk/JsonPartials/LDtkWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ private LDtkLevel LoadLevel(LDtkLevel rawLevel)
}

level.ExternalRelPath = rawLevel.ExternalRelPath;
level.WorldFilePath = FilePath;
level.Loaded = true;
}
else
Expand Down
7 changes: 3 additions & 4 deletions LDtk/Renderer/LDtkRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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));
}
Expand Down

0 comments on commit d7e69e1

Please # to comment.