From c603eaafbc154ddf813c530c3adfc227760e4063 Mon Sep 17 00:00:00 2001 From: "M. Sz" Date: Thu, 4 Mar 2021 11:33:44 +0100 Subject: [PATCH] animation data: methods for edition animation data records count --- d2common/d2fileformats/d2animdata/animdata.go | 28 +++++++++++ .../d2fileformats/d2animdata/animdata_test.go | 49 ++++++++++++++++++- 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/d2common/d2fileformats/d2animdata/animdata.go b/d2common/d2fileformats/d2animdata/animdata.go index 9320c7d0..8e21c286 100644 --- a/d2common/d2fileformats/d2animdata/animdata.go +++ b/d2common/d2fileformats/d2animdata/animdata.go @@ -61,6 +61,34 @@ func (ad *AnimationData) GetRecordsCount() int { return len(ad.entries) } +func (ad *AnimationData) PushRecord(name string) { + ad.entries[name] = append( + ad.entries[name], + &AnimationDataRecord{ + name: name, + }, + ) +} + +func (ad *AnimationData) DeleteRecord(name string, recordIdx int) error { + newRecords := make([]*AnimationDataRecord, 0) + for n, i := range ad.entries[name] { + if n == recordIdx { + continue + } + + newRecords = append(newRecords, i) + } + + if len(ad.entries[name]) == len(newRecords) { + return fmt.Errorf("index %d not found", recordIdx) + } + + ad.entries[name] = newRecords + + return nil +} + // Load loads the data into an AnimationData struct //nolint:gocognit,funlen // can't reduce func Load(data []byte) (*AnimationData, error) { diff --git a/d2common/d2fileformats/d2animdata/animdata_test.go b/d2common/d2fileformats/d2animdata/animdata_test.go index 00a34479..9b414c7d 100644 --- a/d2common/d2fileformats/d2animdata/animdata_test.go +++ b/d2common/d2fileformats/d2animdata/animdata_test.go @@ -155,7 +155,7 @@ func TestAnimationDataRecord_FPS(t *testing.T) { } } -func TestAnimationDataRecord_Marshal(t *testing.T) { +func TestAnimationData_Marshal(t *testing.T) { file, fileErr := os.Open("testdata/AnimData.d2") if fileErr != nil { t.Error("cannot open test data file") @@ -209,3 +209,50 @@ func TestAnimationDataRecord_Marshal(t *testing.T) { } } } + +func TestAnimationData_DeleteRecord(t *testing.T) { + ad := &AnimationData{ + entries: map[string][]*AnimationDataRecord{ + "a": { + {name: "a", speed: 1, framesPerDirection: 1}, + {name: "a", speed: 2, framesPerDirection: 2}, + {name: "a", speed: 3, framesPerDirection: 3}, + }, + }, + } + + err := ad.DeleteRecord("a", 1) + + if err != nil { + t.Error(err) + } + + if len(ad.entries["a"]) != 2 { + t.Fatal("Delete record error") + } + + if ad.entries["a"][1].speed != 3 { + t.Fatal("Invalid index deleted") + } +} + +func TestAnimationData_PushRecord(t *testing.T) { + ad := &AnimationData{ + entries: map[string][]*AnimationDataRecord{ + "a": { + {name: "a", speed: 1, framesPerDirection: 1}, + {name: "a", speed: 2, framesPerDirection: 2}, + }, + }, + } + + ad.PushRecord("a") + + if len(ad.entries["a"]) != 3 { + t.Fatal("No record was pushed") + } + + if ad.entries["a"][2].name != "a" { + t.Fatal("unexpected name of new record was set") + } +}