Skip to content

Commit

Permalink
fix: import errors when an icon for a nonexistent tileset definition …
Browse files Browse the repository at this point in the history
…is trying to be referenced
Cammin committed May 12, 2024
1 parent dbded97 commit c12d26b
Showing 6 changed files with 34 additions and 10 deletions.
9 changes: 8 additions & 1 deletion Assets/LDtkUnity/Editor/Builders/LDtkBuilderEntity.cs
Original file line number Diff line number Diff line change
@@ -229,9 +229,16 @@ private string GetEntityImageAndRect(EntityInstance entityInstance, string asset
{
return null;
}

//todo while awaiting this fix, safely null check it https://github.com/deepnight/ldtk/issues/1107
TilesetDefinition tileset = tile.Tileset;
if (tileset == null)
{
return null;
}

LDtkRelativeGetterTilesetTexture textureGetter = new LDtkRelativeGetterTilesetTexture();
Texture2D tex = textureGetter.GetRelativeAsset(tile.Tileset, assetPath);
Texture2D tex = textureGetter.GetRelativeAsset(tileset, assetPath);
if (tex == null)
{
return null;
9 changes: 8 additions & 1 deletion Assets/LDtkUnity/Editor/ParsedField/Parsed/LDtkParsedTile.cs
Original file line number Diff line number Diff line change
@@ -38,8 +38,15 @@ public object ImportString(LDtkFieldParseContext ctx)
LDtkDebug.LogError("Couldn't parse point, importer and/or project was null");
return default;
}

//todo while awaiting this fix, just safely return null if a definition is not found https://github.com/deepnight/ldtk/issues/1107
TilesetDefinition tileset = tile.Tileset;
if (tileset == null)
{
return default;
}

Sprite sprite = ctx.Importer.GetAdditionalSprite(ctx.Project, tile.Tileset, tile.UnityRect);
Sprite sprite = ctx.Importer.GetAdditionalSprite(ctx.Project, tileset, tile.UnityRect);
return sprite;
}

Original file line number Diff line number Diff line change
@@ -206,10 +206,11 @@ public Sprite GetSpriteForTilesetRectangle(TilesetRectangle rectangle)

if (!_allSprites.TryGetValue(rectangle.TilesetUid, out var sprites))
{
//todo while awaiting this fix, just safely return null if a definition is not found https://github.com/deepnight/ldtk/issues/1107
TilesetDefinition tilesetDef = rectangle.Tileset;
if (tilesetDef == null)
{
_logger.LogError($"Problem getting sprite for TilesetRectangle def uid {rectangle.TilesetUid}: No definition exists?");
//_logger.LogError($"Problem getting sprite for TilesetRectangle def uid {rectangle.TilesetUid}: No definition exists?");
return null;
}

Original file line number Diff line number Diff line change
@@ -9,7 +9,8 @@ public partial class TilesetRectangle
/// Tileset used for this rectangle data. <br/>
/// Make sure to call <see cref="LDtkUidBank"/>.<see cref="LDtkUidBank.CacheUidData"/> first!
/// </value>
[IgnoreDataMember] public TilesetDefinition Tileset => LDtkUidBank.GetUidData<TilesetDefinition>(TilesetUid);
//todo while awaiting this fix, just safely not log an error if a definition is not found https://github.com/deepnight/ldtk/issues/1107
[IgnoreDataMember] public TilesetDefinition Tileset => LDtkUidBank.GetUidData<TilesetDefinition>(TilesetUid, true);

/// <value>
/// Rectangle of the tile in the Tileset atlas
9 changes: 6 additions & 3 deletions Assets/LDtkUnity/Runtime/Tools/Bank/LDtkDictionary.cs
Original file line number Diff line number Diff line change
@@ -33,14 +33,17 @@ protected void TryAdd(IEnumerable<TValue> values)
}
}

public TValue TryGet(TKey key)
public TValue TryGet(TKey key, bool silent = false)
{
if (ContainsKey(key))
{
return this[key];
}

LDtkDebug.LogError($"{typeof(TKey).Name} Dictionary<{typeof(TValue).Name}> does not contain a key for \"{key}\"");

if (!silent)
{
LDtkDebug.LogError($"{typeof(TKey).Name} Dictionary<{typeof(TValue).Name}> does not contain a key for \"{key}\"");
}
return default;
}
}
11 changes: 8 additions & 3 deletions Assets/LDtkUnity/Runtime/Tools/Bank/LDtkUidBank.cs
Original file line number Diff line number Diff line change
@@ -35,13 +35,14 @@ public static void CacheUidData(LdtkJson project)
Profiler.EndSample();
}

internal static T GetUidData<T>(long uid) where T : ILDtkUid
//todo while awaiting this fix, we can safely be silent for these cases. Once the bug is fixed, remove the silent param https://github.com/deepnight/ldtk/issues/1107
internal static T GetUidData<T>(long uid, bool silent = false) where T : ILDtkUid
{
Type requestedType = typeof(T);

if (_uids != null)
{
ILDtkUid tryGet = _uids.TryGet(uid);
ILDtkUid tryGet = _uids.TryGet(uid, silent);
if (tryGet != null)
{
Type type = tryGet.GetType();
@@ -53,7 +54,11 @@ internal static T GetUidData<T>(long uid) where T : ILDtkUid
return (T)tryGet;
}

LDtkDebug.LogError($"{nameof(LDtkUidBank)} Dictionary<{requestedType.Name}>'s dictionary entry was null");
if (!silent)
{
LDtkDebug.LogError($"{nameof(LDtkUidBank)} Dictionary<{requestedType.Name}>'s dictionary entry was null");
}

return default;
}

0 comments on commit c12d26b

Please sign in to comment.