-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbyte_stream_out.go
137 lines (111 loc) · 3.15 KB
/
byte_stream_out.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package nex
import (
"github.com/PretendoNetwork/nex-go/v2/types"
crunch "github.com/superwhiskers/crunch/v3"
)
// ByteStreamOut is an abstraction of github.com/superwhiskers/crunch with nex type support
type ByteStreamOut struct {
*crunch.Buffer
LibraryVersions *LibraryVersions
Settings *ByteStreamSettings
}
// StringLengthSize returns the expected size of String length fields
func (bso *ByteStreamOut) StringLengthSize() int {
size := 2
if bso.Settings != nil {
size = bso.Settings.StringLengthSize
}
return size
}
// PIDSize returns the size of PID types
func (bso *ByteStreamOut) PIDSize() int {
size := 4
if bso.Settings != nil {
size = bso.Settings.PIDSize
}
return size
}
// UseStructureHeader determines if Structure headers should be used
func (bso *ByteStreamOut) UseStructureHeader() bool {
useStructureHeader := false
if bso.Settings != nil {
useStructureHeader = bso.Settings.UseStructureHeader
}
return useStructureHeader
}
// CopyNew returns a copy of the StreamOut but with a blank internal buffer. Returns as types.Writable
func (bso *ByteStreamOut) CopyNew() types.Writable {
return NewByteStreamOut(bso.LibraryVersions, bso.Settings)
}
// Writes the input data to the end of the StreamOut
func (bso *ByteStreamOut) Write(data []byte) {
bso.Grow(int64(len(data)))
bso.WriteBytesNext(data)
}
// WriteUInt8 writes a uint8
func (bso *ByteStreamOut) WriteUInt8(u8 uint8) {
bso.Grow(1)
bso.WriteByteNext(byte(u8))
}
// WriteUInt16LE writes a uint16 as LE
func (bso *ByteStreamOut) WriteUInt16LE(u16 uint16) {
bso.Grow(2)
bso.WriteU16LENext([]uint16{u16})
}
// WriteUInt32LE writes a uint32 as LE
func (bso *ByteStreamOut) WriteUInt32LE(u32 uint32) {
bso.Grow(4)
bso.WriteU32LENext([]uint32{u32})
}
// WriteUInt64LE writes a uint64 as LE
func (bso *ByteStreamOut) WriteUInt64LE(u64 uint64) {
bso.Grow(8)
bso.WriteU64LENext([]uint64{u64})
}
// WriteInt8 writes a int8
func (bso *ByteStreamOut) WriteInt8(s8 int8) {
bso.Grow(1)
bso.WriteByteNext(byte(s8))
}
// WriteInt16LE writes a uint16 as LE
func (bso *ByteStreamOut) WriteInt16LE(s16 int16) {
bso.Grow(2)
bso.WriteU16LENext([]uint16{uint16(s16)})
}
// WriteInt32LE writes a int32 as LE
func (bso *ByteStreamOut) WriteInt32LE(s32 int32) {
bso.Grow(4)
bso.WriteU32LENext([]uint32{uint32(s32)})
}
// WriteInt64LE writes a int64 as LE
func (bso *ByteStreamOut) WriteInt64LE(s64 int64) {
bso.Grow(8)
bso.WriteU64LENext([]uint64{uint64(s64)})
}
// WriteFloat32LE writes a float32 as LE
func (bso *ByteStreamOut) WriteFloat32LE(f32 float32) {
bso.Grow(4)
bso.WriteF32LENext([]float32{f32})
}
// WriteFloat64LE writes a float64 as LE
func (bso *ByteStreamOut) WriteFloat64LE(f64 float64) {
bso.Grow(8)
bso.WriteF64LENext([]float64{f64})
}
// WriteBool writes a bool
func (bso *ByteStreamOut) WriteBool(b bool) {
var bVar uint8
if b {
bVar = 1
}
bso.Grow(1)
bso.WriteByteNext(byte(bVar))
}
// NewByteStreamOut returns a new NEX writable byte stream
func NewByteStreamOut(libraryVersions *LibraryVersions, settings *ByteStreamSettings) *ByteStreamOut {
return &ByteStreamOut{
Buffer: crunch.NewBuffer(),
LibraryVersions: libraryVersions,
Settings: settings,
}
}