Skip to content

Commit 7facef9

Browse files
Merge pull request #36 from shabbywu/fix/tag-userdata-parse-error
fix: fix failed to parse userdata of the first tag Closes #30
2 parents 87df2bb + 32247fb commit 7facef9

3 files changed

Lines changed: 29 additions & 13 deletions

File tree

source/AsepriteDotNet/IO/AsepriteFileLoader.cs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,12 @@ private static AsepriteTag[] ReadTags(AsepriteBinaryReader reader)
173173

174174
if (currentUserData is not null)
175175
{
176-
currentUserData.Text = text;
177-
currentUserData.Color = color;
178-
179-
if (lastReadChunkType == ASE_CHUNK_TAGS)
176+
if (lastReadChunkType != ASE_CHUNK_TAGS)
180177
{
178+
currentUserData.Text = text;
179+
currentUserData.Color = color;
180+
}
181+
else {
181182
// Tags are a special case. User data for tags comes all together
182183
// (one next to the other) after the tags chunk, in the same order:
183184
//
@@ -189,7 +190,12 @@ private static AsepriteTag[] ReadTags(AsepriteBinaryReader reader)
189190
//
190191
// So here we expect that the next user data chunk will correspond to the next tag
191192
// int he tags collection
192-
tagIterator++;
193+
//
194+
// However, `currentUserData` is actually the one of last tags after read last chunk of ASE_CHUNK_TAGS
195+
// So here we fix the `currentUserData` to the right one
196+
currentUserData = tags[tagIterator++].UserData;
197+
currentUserData.Text = text;
198+
currentUserData.Color = color;
193199

194200
if (tagIterator < tags.Count)
195201
{
@@ -510,10 +516,12 @@ private static AsepriteFile LoadFile(string fileName, AsepriteBinaryReader reade
510516
}
511517
else if (currentUserData is not null)
512518
{
513-
currentUserData.Text = text;
514-
currentUserData.Color = color;
515-
516-
if (lastReadChunkType == ASE_CHUNK_TAGS)
519+
if (lastReadChunkType != ASE_CHUNK_TAGS)
520+
{
521+
currentUserData.Text = text;
522+
currentUserData.Color = color;
523+
}
524+
else
517525
{
518526
// Tags are a special case. User data for tags comes all together
519527
// (one next to the other) after the tags chunk, in the same order:
@@ -526,7 +534,12 @@ private static AsepriteFile LoadFile(string fileName, AsepriteBinaryReader reade
526534
//
527535
// So here we expect that the next user data chunk will correspond to the next tag
528536
// int he tags collection
529-
tagIterator++;
537+
//
538+
// However, `currentUserData` is actually the one of last tags after read last chunk of ASE_CHUNK_TAGS
539+
// So here we fix the `currentUserData` to the right one
540+
currentUserData = tags[tagIterator++].UserData;
541+
currentUserData.Text = text;
542+
currentUserData.Color = color;
530543

531544
if (tagIterator < tags.Count)
532545
{
71 Bytes
Binary file not shown.

tests/AsepriteDotNet.Tests/IO/AsepriteFileLoaderTests.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,11 @@ public void AsepriteFileReader_ReadFileTest()
7272
Assert.Equal(1, doc.Layers[9].ChildLevel);
7373

7474
// Validate Tags
75-
Assert.Equal(3, doc.Tags.Length);
75+
Assert.Equal(4, doc.Tags.Length);
7676
Assert.Equal("tag0to2forward", doc.Tags[0].Name);
7777
Assert.Equal(0, doc.Tags[0].From);
7878
Assert.Equal(2, doc.Tags[0].To);
79+
Assert.Equal("tag-1-user-data", doc.Tags[0].UserData.Text);
7980
Assert.Equal(new Rgba32(0, 0, 0, 255), doc.Tags[0].Color);
8081
Assert.Equal(AsepriteLoopDirection.Forward, doc.Tags[0].LoopDirection);
8182
Assert.Equal("tag3pingpong", doc.Tags[1].Name);
@@ -84,6 +85,7 @@ public void AsepriteFileReader_ReadFileTest()
8485
Assert.Equal(new Rgba32(11, 255, 230, 255), doc.Tags[2].Color);
8586
Assert.Equal(new Rgba32(11, 255, 230, 255), doc.Tags[2].UserData.Color);
8687
Assert.Equal("tag-4-user-data", doc.Tags[2].UserData.Text);
88+
Assert.False(doc.Tags[3].UserData.HasText);
8789

8890
// Validate Frames
8991
Assert.Equal(7, doc.Frames.Length);
@@ -416,10 +418,11 @@ public void AsepriteFileReader_ReadTagsTest()
416418
string path = GetPath("read-test.aseprite");
417419
AsepriteTag[] tags = AsepriteFileLoader.ReadTags(path);
418420

419-
Assert.Equal(3, tags.Length);
421+
Assert.Equal(4, tags.Length);
420422
Assert.Equal("tag0to2forward", tags[0].Name);
421423
Assert.Equal(0, tags[0].From);
422424
Assert.Equal(2, tags[0].To);
425+
Assert.Equal("tag-1-user-data", tags[0].UserData.Text);
423426
Assert.Equal(new Rgba32(0, 0, 0, 255), tags[0].Color);
424427
Assert.Equal(AsepriteLoopDirection.Forward, tags[0].LoopDirection);
425428
Assert.Equal("tag3pingpong", tags[1].Name);
@@ -428,7 +431,7 @@ public void AsepriteFileReader_ReadTagsTest()
428431
Assert.Equal(new Rgba32(11, 255, 230, 255), tags[2].Color);
429432
Assert.Equal(new Rgba32(11, 255, 230, 255), tags[2].UserData.Color);
430433
Assert.Equal("tag-4-user-data", tags[2].UserData.Text);
431-
434+
Assert.False(tags[3].UserData.HasText);
432435
}
433436

434437
// There was an issue where slice data was read incorrectly. This test was put in place to ensure that

0 commit comments

Comments
 (0)