1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-06-20 06:05:23 +00:00

Replaced restruct with StreamReader (#574)

This commit is contained in:
Intyre 2020-07-11 05:02:45 +02:00 committed by GitHub
parent 9e58b134e5
commit aecd047cf3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 30 deletions

View File

@ -1,40 +1,66 @@
package d2dc6 package d2dc6
import ( import (
"encoding/binary" "github.com/OpenDiablo2/OpenDiablo2/d2common"
"github.com/go-restruct/restruct"
) )
// DC6 represents a DC6 file. // DC6 represents a DC6 file.
type DC6 struct { type DC6 struct {
// Header Version int32
Version int32 `struct:"int32"` Flags uint32
Flags uint32 `struct:"uint32"` Encoding uint32
Encoding uint32 `struct:"uint32"` Termination []byte // 4 bytes
Termination []byte `struct:"[4]byte"` Directions uint32
Directions uint32 `struct:"uint32"` FramesPerDirection uint32
FramesPerDirection uint32 `struct:"uint32"` FramePointers []uint32 // size is Directions*FramesPerDirection
Frames []*DC6Frame // size is Directions*FramesPerDirection
FramePointers []uint32 `struct:"[]uint32,size=Directions*FramesPerDirection"`
Frames []*DC6Frame `struct-size:"Directions*FramesPerDirection"`
} }
// Load uses restruct to read the binary dc6 data into structs then parses image data from the frame data. // Load uses restruct to read the binary dc6 data into structs then parses image data from the frame data.
func Load(data []byte) (*DC6, error) { func Load(data []byte) (*DC6, error) {
result := &DC6{} const (
terminationSize = 4
terminatorSize = 3
)
restruct.EnableExprBeta() r := d2common.CreateStreamReader(data)
err := restruct.Unpack(data, binary.LittleEndian, &result)
if err != nil { var dc DC6
return nil, err dc.Version = r.GetInt32()
dc.Flags = r.GetUInt32()
dc.Encoding = r.GetUInt32()
dc.Termination = r.ReadBytes(terminationSize)
dc.Directions = r.GetUInt32()
dc.FramesPerDirection = r.GetUInt32()
frameCount := int(dc.Directions * dc.FramesPerDirection)
dc.FramePointers = make([]uint32, frameCount)
for i := 0; i < frameCount; i++ {
dc.FramePointers[i] = r.GetUInt32()
} }
return result, err dc.Frames = make([]*DC6Frame, frameCount)
for i := 0; i < frameCount; i++ {
frame := &DC6Frame{
Flipped: r.GetUInt32(),
Width: r.GetUInt32(),
Height: r.GetUInt32(),
OffsetX: r.GetInt32(),
OffsetY: r.GetInt32(),
Unknown: r.GetUInt32(),
NextBlock: r.GetUInt32(),
Length: r.GetUInt32(),
}
frame.FrameData = r.ReadBytes(int(frame.Length))
frame.Terminator = r.ReadBytes(terminatorSize)
dc.Frames[i] = frame
}
return &dc, nil
} }
// Decodes the given frame to an indexed color texture // DecodeFrame decodes the given frame to an indexed color texture
func (d *DC6) DecodeFrame(frameIndex int) []byte { func (d *DC6) DecodeFrame(frameIndex int) []byte {
frame := d.Frames[frameIndex] frame := d.Frames[frameIndex]

View File

@ -2,14 +2,14 @@ package d2dc6
// DC6Frame represents a single frame in a DC6. // DC6Frame represents a single frame in a DC6.
type DC6Frame struct { type DC6Frame struct {
Flipped uint32 `struct:"uint32"` Flipped uint32
Width uint32 `struct:"uint32"` Width uint32
Height uint32 `struct:"uint32"` Height uint32
OffsetX int32 `struct:"int32"` OffsetX int32
OffsetY int32 `struct:"int32"` OffsetY int32
Unknown uint32 `struct:"uint32"` Unknown uint32
NextBlock uint32 `struct:"uint32"` NextBlock uint32
Length uint32 `struct:"uint32,sizeof=FrameData"` Length uint32
FrameData []byte FrameData []byte // size is the value of Length
Terminator []byte `struct:"[3]byte"` Terminator []byte // 3 bytes
} }