From b78dca52c2ad010857df1ff30331552630fe2912 Mon Sep 17 00:00:00 2001 From: Intyre Date: Wed, 23 Dec 2020 10:39:58 +0100 Subject: [PATCH] Refactor StreamWriter --- d2common/d2datautils/stream_writer.go | 65 ++++++++++++++------------- 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/d2common/d2datautils/stream_writer.go b/d2common/d2datautils/stream_writer.go index 10870a73..849cbdb2 100644 --- a/d2common/d2datautils/stream_writer.go +++ b/d2common/d2datautils/stream_writer.go @@ -2,10 +2,6 @@ package d2datautils import "bytes" -const ( - byteMask = 0xFF -) - // StreamWriter allows you to create a byte array by streaming in writes of various sizes type StreamWriter struct { data *bytes.Buffer @@ -20,41 +16,40 @@ func CreateStreamWriter() *StreamWriter { return result } +// GetBytes returns the the byte slice of the underlying data +func (v *StreamWriter) GetBytes() []byte { + return v.data.Bytes() +} + // PushByte writes a byte to the stream func (v *StreamWriter) PushByte(val byte) { v.data.WriteByte(val) } -// PushUint16 writes an uint16 word to the stream -func (v *StreamWriter) PushUint16(val uint16) { - for count := 0; count < bytesPerInt16; count++ { - shift := count * bitsPerByte - v.data.WriteByte(byte(val>>shift) & byteMask) - } -} - // PushInt16 writes a int16 word to the stream func (v *StreamWriter) PushInt16(val int16) { - for count := 0; count < bytesPerInt16; count++ { - shift := count * bitsPerByte - v.data.WriteByte(byte(val>>shift) & byteMask) - } + v.PushUint16(uint16(val)) +} + +// PushUint16 writes an uint16 word to the stream +//nolint +func (v *StreamWriter) PushUint16(val uint16) { + v.data.WriteByte(byte(val)) + v.data.WriteByte(byte(val >> 8)) +} + +// PushInt32 writes a int32 dword to the stream +func (v *StreamWriter) PushInt32(val int32) { + v.PushUint32(uint32(val)) } // PushUint32 writes a uint32 dword to the stream +//nolint func (v *StreamWriter) PushUint32(val uint32) { - for count := 0; count < bytesPerInt32; count++ { - shift := count * bitsPerByte - v.data.WriteByte(byte(val>>shift) & byteMask) - } -} - -// PushUint64 writes a uint64 qword to the stream -func (v *StreamWriter) PushUint64(val uint64) { - for count := 0; count < bytesPerInt64; count++ { - shift := count * bitsPerByte - v.data.WriteByte(byte(val>>shift) & byteMask) - } + v.data.WriteByte(byte(val)) + v.data.WriteByte(byte(val >> 8)) + v.data.WriteByte(byte(val >> 16)) + v.data.WriteByte(byte(val >> 24)) } // PushInt64 writes a uint64 qword to the stream @@ -62,7 +57,15 @@ func (v *StreamWriter) PushInt64(val int64) { v.PushUint64(uint64(val)) } -// GetBytes returns the the byte slice of the underlying data -func (v *StreamWriter) GetBytes() []byte { - return v.data.Bytes() +// PushUint64 writes a uint64 qword to the stream +//nolint +func (v *StreamWriter) PushUint64(val uint64) { + v.data.WriteByte(byte(val)) + v.data.WriteByte(byte(val >> 8)) + v.data.WriteByte(byte(val >> 16)) + v.data.WriteByte(byte(val >> 24)) + v.data.WriteByte(byte(val >> 32)) + v.data.WriteByte(byte(val >> 40)) + v.data.WriteByte(byte(val >> 48)) + v.data.WriteByte(byte(val >> 56)) }