Skip to content

Commit d7e69e1

Browse files
committed
Fix path crash
1 parent f31f9db commit d7e69e1

File tree

5 files changed

+20
-21
lines changed

5 files changed

+20
-21
lines changed

LDtk.Example/LDtkMonogameGame.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace LDtkMonogameExample;
1414
using Microsoft.Xna.Framework.Graphics;
1515
using Microsoft.Xna.Framework.Input;
1616

17+
1718
public class LDtkMonogameGame : Game
1819
{
1920
// LDtk stuff

LDtk/JsonPartials/LDtkFile.cs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,27 +64,22 @@ public LDtkWorld LoadWorld(Guid iid)
6464
{
6565
foreach (LDtkWorld world in Worlds)
6666
{
67-
if (world.Iid == iid)
67+
if (world.Iid != iid)
6868
{
69-
world.FilePath = FilePath;
70-
if (world.Levels != null)
69+
continue;
70+
}
71+
world.FilePath = FilePath;
72+
foreach (LDtkLevel level in world.Levels)
73+
{
74+
if (level.ExternalRelPath != null)
7175
{
72-
foreach (LDtkLevel level in world.Levels)
73-
{
74-
if (level.ExternalRelPath != null)
75-
{
76-
level.FilePath = Path.Join(Path.GetDirectoryName(FilePath), level.ExternalRelPath);
77-
}
78-
else
79-
{
80-
level.FilePath = FilePath;
81-
}
82-
}
76+
level.FilePath = Path.Join(Path.GetDirectoryName(FilePath), level.ExternalRelPath);
8377
}
84-
85-
world.Content = Content;
86-
return world;
78+
level.WorldFilePath = world.FilePath;
8779
}
80+
81+
world.Content = Content;
82+
return world;
8883
}
8984
return null;
9085
}

LDtk/JsonPartials/LDtkLevel.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public partial class LDtkLevel
1616
/// <summary> The absolute filepath to the level </summary>
1717
[JsonIgnore] public string FilePath { get; set; }
1818

19+
/// <summary> The absolute filepath to the world </summary>
20+
[JsonIgnore] public string WorldFilePath { get; set; }
21+
1922
/// <summary> World Position of the level in pixels </summary>
2023
[JsonIgnore] public Point Position => new(WorldX, WorldY);
2124

LDtk/JsonPartials/LDtkWorld.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ private LDtkLevel LoadLevel(LDtkLevel rawLevel)
126126
}
127127

128128
level.ExternalRelPath = rawLevel.ExternalRelPath;
129+
level.WorldFilePath = FilePath;
129130
level.Loaded = true;
130131
}
131132
else

LDtk/Renderer/LDtkRenderer.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ private Texture2D[] RenderLayers(LDtkLevel level)
112112
SpriteEffects mirror = (SpriteEffects)tile.F;
113113
SpriteBatch.Draw(texture, position, rect, Color.White, 0, Vector2.Zero, 1f, mirror, 0);
114114
}
115-
116115
break;
117116

118117
case LayerType.AutoLayer:
@@ -127,7 +126,6 @@ private Texture2D[] RenderLayers(LDtkLevel level)
127126
SpriteBatch.Draw(texture, position, rect, Color.White, 0, Vector2.Zero, 1f, mirror, 0);
128127
}
129128
}
130-
131129
break;
132130

133131
case LayerType.Entities:
@@ -163,8 +161,9 @@ private Texture2D GetTexture(LDtkLevel level, string path)
163161
{
164162
if (!string.IsNullOrWhiteSpace(level.FilePath))
165163
{
166-
string filePath = Path.GetDirectoryName(level.FilePath);
167-
return Texture2D.FromFile(graphicsDevice, Path.GetFullPath(Path.Combine(filePath, "../", path)));
164+
string filePath = Path.GetDirectoryName(level.WorldFilePath);
165+
string absolutePath = Path.GetFullPath(Path.Combine(filePath, path));
166+
return Texture2D.FromFile(graphicsDevice, absolutePath);
168167
}
169168
return Texture2D.FromFile(graphicsDevice, Path.Combine("Content", path));
170169
}

0 commit comments

Comments
 (0)