1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-06-04 15:00:42 +00:00

d2dc6 refactor + unit test for it

This commit is contained in:
M. Sz 2021-02-07 18:34:10 +01:00
parent 227bc9fcb1
commit 6d098de778
2 changed files with 102 additions and 8 deletions

View File

@ -32,17 +32,42 @@ type DC6 struct {
Frames []*DC6Frame // size is Directions*FramesPerDirection
}
// Load uses restruct to read the binary dc6 data into structs then parses image data from the frame data.
// New creates a new, empty DC6
func New() *DC6 {
result := &DC6{
Version: 0,
Flags: 0,
Encoding: 0,
Termination: make([]byte, 4),
Directions: 0,
FramesPerDirection: 0,
FramePointers: make([]uint32, 0),
Frames: make([]*DC6Frame, 0),
}
return result
}
// Load loads a dc6 animation
func Load(data []byte) (*DC6, error) {
r := d2datautils.CreateStreamReader(data)
d := New()
err := d.Unmarshal(data)
if err != nil {
return nil, err
}
var dc DC6
return d, nil
}
// Unmarshal converts bite slice into DC6 structure
func (dc *DC6) Unmarshal(data []byte) error {
var err error
r := d2datautils.CreateStreamReader(data)
err = dc.loadHeader(r)
if err != nil {
return nil, err
return err
}
frameCount := int(dc.Directions * dc.FramesPerDirection)
@ -51,17 +76,17 @@ func Load(data []byte) (*DC6, error) {
for i := 0; i < frameCount; i++ {
dc.FramePointers[i], err = r.ReadUInt32()
if err != nil {
return nil, err
return err
}
}
dc.Frames = make([]*DC6Frame, frameCount)
if err := dc.loadFrames(r); err != nil {
return nil, err
return err
}
return &dc, nil
return nil
}
func (d *DC6) loadHeader(r *d2datautils.StreamReader) error {
@ -241,7 +266,7 @@ func (d *DC6) Clone() *DC6 {
for i := range d.Frames {
cloneFrame := *d.Frames[i]
clone.Frames = append(clone.Frames, &cloneFrame)
clone.Frames[i] = &cloneFrame
}
return &clone

View File

@ -0,0 +1,69 @@
package d2dc6
import (
"testing"
)
func TestDC6New(t *testing.T) {
dc6 := New()
if dc6 == nil {
t.Error("d2dc6.New() method returned nil")
}
}
func getExampleDC6() *DC6 {
exampleDC6 := &DC6{
Version: 6,
Flags: 1,
Encoding: 0,
Termination: []byte{238, 238, 238, 238},
Directions: 1,
FramesPerDirection: 1,
FramePointers: []uint32{56},
Frames: []*DC6Frame{
&DC6Frame{
Flipped: 0,
Width: 32,
Height: 26,
OffsetX: 45,
OffsetY: 24,
Unknown: 0,
NextBlock: 50,
Length: 10,
FrameData: []byte{2, 23, 34, 128, 53, 64, 39, 43, 123, 12},
Terminator: []byte{2, 8, 5},
},
},
}
return exampleDC6
}
func TestDC6Unmarshal(t *testing.T) {
exampleDC6 := getExampleDC6()
data := exampleDC6.Marshal()
extractedDC6, err := Load(data)
if err != nil {
t.Error(err)
}
if exampleDC6.Version != extractedDC6.Version ||
len(exampleDC6.Frames) != len(extractedDC6.Frames) ||
exampleDC6.Frames[0].NextBlock != extractedDC6.Frames[0].NextBlock {
t.Fatal("encoded and decoded DC6 isn't the same")
}
}
func TestDC6Clone(t *testing.T) {
exampleDC6 := getExampleDC6()
clonedDC6 := exampleDC6.Clone()
if exampleDC6.Termination[0] != clonedDC6.Termination[0] ||
len(exampleDC6.Frames) != len(clonedDC6.Frames) ||
exampleDC6.Frames[0].NextBlock != clonedDC6.Frames[0].NextBlock {
t.Fatal("cloned dc6 isn't equal to orginal")
}
}