Skip to content

Commit

Permalink
slight dictionary optimizing
Browse files Browse the repository at this point in the history
  • Loading branch information
Cammin committed May 21, 2024
1 parent bbdc1b2 commit e885208
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ private bool ReformatRectMetaData(List<TilesetRectangle> srcRects)

int jsonTileCount = srcRects.Count;

//it can pre allocate if the list is empty during a first-time import
if (_sprites.IsNullOrEmpty())
{
_sprites = new List<LDtkSpriteRect>(jsonTileCount);
}

// trim metas off the end of the list to match the new src count.
// LDtk handles this in the exact same way where if the tile count decreased, then any old tiles are complete
if (_sprites.Count > jsonTileCount)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,23 @@ internal Dictionary<int, string> CustomDataToDictionary()
Dictionary<int,string> dict = new Dictionary<int, string>(CustomData.Length);
foreach (TileCustomMetadata metadata in CustomData)
{
if (!dict.ContainsKey(metadata.TileId))
{
dict.Add(metadata.TileId, metadata.Data);
continue;
}
dict[metadata.TileId] = metadata.Data;
dict.Add(metadata.TileId, metadata.Data);
}
return dict;
}
internal Dictionary<int, List<string>> EnumTagsToDictionary()
{
Dictionary<int, List<string>> dict = new Dictionary<int, List<string>>();
Dictionary<int, List<string>> dict = new Dictionary<int, List<string>>(EnumTags.Length);
foreach (EnumTagValue tagValue in EnumTags)
{
foreach (int tileId in tagValue.TileIds)
{
if (!dict.ContainsKey(tileId))
if (!dict.TryGetValue(tileId, out List<string> list))
{
dict.Add(tileId, new List<string>());
list = new List<string>(1);
dict.Add(tileId, list);
}
dict[tileId].Add(tagValue.EnumValueId);
list.Add(tagValue.EnumValueId);
}
}
return dict;
Expand Down

0 comments on commit e885208

Please sign in to comment.