From c603eaafbc154ddf813c530c3adfc227760e4063 Mon Sep 17 00:00:00 2001 From: "M. Sz" Date: Thu, 4 Mar 2021 11:33:44 +0100 Subject: [PATCH 1/4] 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") + } +} From 2e2d086d713cb38271a563c17e81180061a59091 Mon Sep 17 00:00:00 2001 From: "M. Sz" Date: Thu, 4 Mar 2021 11:37:16 +0100 Subject: [PATCH 2/4] animation data: lintfix --- d2common/d2fileformats/d2animdata/animdata.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/d2common/d2fileformats/d2animdata/animdata.go b/d2common/d2fileformats/d2animdata/animdata.go index 8e21c286..8a50f6c0 100644 --- a/d2common/d2fileformats/d2animdata/animdata.go +++ b/d2common/d2fileformats/d2animdata/animdata.go @@ -61,6 +61,7 @@ func (ad *AnimationData) GetRecordsCount() int { return len(ad.entries) } +// PushRecord adds a new record to entry named 'name' func (ad *AnimationData) PushRecord(name string) { ad.entries[name] = append( ad.entries[name], @@ -70,8 +71,10 @@ func (ad *AnimationData) PushRecord(name string) { ) } +// DeleteRecord teletes specified index from specified entry func (ad *AnimationData) DeleteRecord(name string, recordIdx int) error { newRecords := make([]*AnimationDataRecord, 0) + for n, i := range ad.entries[name] { if n == recordIdx { continue From 98539befe354b997706109253f01420f031db56d Mon Sep 17 00:00:00 2001 From: "M. Sz" Date: Thu, 4 Mar 2021 11:52:08 +0100 Subject: [PATCH 3/4] animation data: Add/Delete entry methods --- d2common/d2fileformats/d2animdata/animdata.go | 22 ++++++++++++++++ .../d2fileformats/d2animdata/animdata_test.go | 26 +++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/d2common/d2fileformats/d2animdata/animdata.go b/d2common/d2fileformats/d2animdata/animdata.go index 8a50f6c0..3129b5ac 100644 --- a/d2common/d2fileformats/d2animdata/animdata.go +++ b/d2common/d2fileformats/d2animdata/animdata.go @@ -92,6 +92,28 @@ func (ad *AnimationData) DeleteRecord(name string, recordIdx int) error { return nil } +func (ad *AnimationData) AddEntry(name string) error { + _, found := ad.entries[name] + if found { + return fmt.Errorf("entry of name %s already exist", name) + } + + ad.entries[name] = make([]*AnimationDataRecord, 0) + + return nil +} + +func (ad *AnimationData) DeleteEntry(name string) error { + _, found := ad.entries[name] + if !found { + return fmt.Errorf("entry named %s doesn't exist", name) + } + + delete(ad.entries, name) + + 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 9b414c7d..08ed60d0 100644 --- a/d2common/d2fileformats/d2animdata/animdata_test.go +++ b/d2common/d2fileformats/d2animdata/animdata_test.go @@ -256,3 +256,29 @@ func TestAnimationData_PushRecord(t *testing.T) { t.Fatal("unexpected name of new record was set") } } + +func TestAnimationData_AddEntry(t *testing.T) { + ad := &AnimationData{ + entries: make(map[string][]*AnimationDataRecord), + } + + ad.AddEntry("a") + + if _, found := ad.entries["a"]; !found { + t.Fatal("entry wasn't added") + } +} + +func TestAnimationData_DeleteEntry(t *testing.T) { + ad := &AnimationData{ + entries: map[string][]*AnimationDataRecord{ + "a": {{}, {}}, + }, + } + + ad.DeleteEntry("a") + + if _, found := ad.entries["a"]; found { + t.Fatal("Entry wasn't deleted") + } +} From fb7279f6b0fbe61bd029feaf7433d13c055c2561 Mon Sep 17 00:00:00 2001 From: "M. Sz" Date: Thu, 4 Mar 2021 11:55:33 +0100 Subject: [PATCH 4/4] animation data: lintfix --- d2common/d2fileformats/d2animdata/animdata.go | 2 ++ d2common/d2fileformats/d2animdata/animdata_test.go | 10 ++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/d2common/d2fileformats/d2animdata/animdata.go b/d2common/d2fileformats/d2animdata/animdata.go index 3129b5ac..07e05cd1 100644 --- a/d2common/d2fileformats/d2animdata/animdata.go +++ b/d2common/d2fileformats/d2animdata/animdata.go @@ -92,6 +92,7 @@ func (ad *AnimationData) DeleteRecord(name string, recordIdx int) error { return nil } +// AddEntry adds a new animation entry with name given func (ad *AnimationData) AddEntry(name string) error { _, found := ad.entries[name] if found { @@ -103,6 +104,7 @@ func (ad *AnimationData) AddEntry(name string) error { return nil } +// DeleteEntry deltees entry with specified name func (ad *AnimationData) DeleteEntry(name string) error { _, found := ad.entries[name] if !found { diff --git a/d2common/d2fileformats/d2animdata/animdata_test.go b/d2common/d2fileformats/d2animdata/animdata_test.go index 08ed60d0..8d1b2061 100644 --- a/d2common/d2fileformats/d2animdata/animdata_test.go +++ b/d2common/d2fileformats/d2animdata/animdata_test.go @@ -262,7 +262,10 @@ func TestAnimationData_AddEntry(t *testing.T) { entries: make(map[string][]*AnimationDataRecord), } - ad.AddEntry("a") + err := ad.AddEntry("a") + if err != nil { + t.Error(err) + } if _, found := ad.entries["a"]; !found { t.Fatal("entry wasn't added") @@ -276,7 +279,10 @@ func TestAnimationData_DeleteEntry(t *testing.T) { }, } - ad.DeleteEntry("a") + err := ad.DeleteEntry("a") + if err != nil { + t.Error(err) + } if _, found := ad.entries["a"]; found { t.Fatal("Entry wasn't deleted")