From 84c87b2eb808a5ff0d72640aebf20045a1e9060f Mon Sep 17 00:00:00 2001 From: "M. Sz" Date: Mon, 1 Feb 2021 12:53:32 +0100 Subject: [PATCH 1/2] data encoding: DC6 --- d2common/d2fileformats/d2dc6/dc6.go | 35 +++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/d2common/d2fileformats/d2dc6/dc6.go b/d2common/d2fileformats/d2dc6/dc6.go index 0ea28329..e903f022 100644 --- a/d2common/d2fileformats/d2dc6/dc6.go +++ b/d2common/d2fileformats/d2dc6/dc6.go @@ -146,6 +146,41 @@ func (d *DC6) loadFrames(r *d2datautils.StreamReader) error { return nil } +// Marshal encodes dc6 animation back into byte slice +func (d *DC6) Marshal() []byte { + sw := d2datautils.CreateStreamWriter() + + // Encode header + sw.PushInt32(d.Version) + sw.PushUint32(d.Flags) + sw.PushUint32(d.Encoding) + + sw.PushBytes(d.Termination...) + + sw.PushUint32(d.Directions) + sw.PushUint32(d.FramesPerDirection) + + // load frames + for _, i := range d.FramePointers { + sw.PushUint32(i) + } + + for i := range d.Frames { + sw.PushUint32(d.Frames[i].Flipped) + sw.PushUint32(d.Frames[i].Width) + sw.PushUint32(d.Frames[i].Height) + sw.PushInt32(d.Frames[i].OffsetX) + sw.PushInt32(d.Frames[i].OffsetY) + sw.PushUint32(d.Frames[i].Unknown) + sw.PushUint32(d.Frames[i].NextBlock) + sw.PushUint32(d.Frames[i].Length) + sw.PushBytes(d.Frames[i].FrameData...) + sw.PushBytes(d.Frames[i].Terminator...) + } + + return sw.GetBytes() +} + // DecodeFrame decodes the given frame to an indexed color texture func (d *DC6) DecodeFrame(frameIndex int) []byte { frame := d.Frames[frameIndex] From 2ebb36eba8604ff9af6ba9a90e3bf9963361748d Mon Sep 17 00:00:00 2001 From: "M. Sz" Date: Mon, 1 Feb 2021 20:56:23 +0100 Subject: [PATCH 2/2] fixed stream-writer's test bug --- d2common/d2datautils/stream_writer_test.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/d2common/d2datautils/stream_writer_test.go b/d2common/d2datautils/stream_writer_test.go index 126425b4..1f7dd95c 100644 --- a/d2common/d2datautils/stream_writer_test.go +++ b/d2common/d2datautils/stream_writer_test.go @@ -8,14 +8,12 @@ func TestStreamWriterByte(t *testing.T) { sr := CreateStreamWriter() data := []byte{0x12, 0x34, 0x56, 0x78} - for _, d := range data { - sr.PushByte(d) - } + sr.PushBytes(data...) output := sr.GetBytes() for i, d := range data { if output[i] != d { - t.Fatalf("sr.PushByte() pushed %X, but wrote %X instead", d, output[i]) + t.Fatalf("sr.PushBytes() pushed %X, but wrote %X instead", d, output[i]) } } }