-
Notifications
You must be signed in to change notification settings - Fork 1
/
property.go
139 lines (118 loc) · 3.19 KB
/
property.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
138
139
package main
import (
"bytes"
"encoding/binary"
"fmt"
"io"
)
// Property contains the byte data and type of property to a specific node
type Property struct {
TypeCode byte
Data []byte
}
// NewPropertyInt32 creates a property that holds data for an Int32
func NewPropertyInt32(p int32) *Property {
buf := new(bytes.Buffer)
binary.Write(buf, binary.LittleEndian, p)
return &Property{
TypeCode: 'I',
Data: buf.Bytes(),
}
}
// NewPropertyInt64 creates a property that holds data for an Int64
func NewPropertyInt64(p int64) *Property {
buf := new(bytes.Buffer)
binary.Write(buf, binary.LittleEndian, p)
return &Property{
TypeCode: 'L',
Data: buf.Bytes(),
}
}
// NewPropertyString creates a property that holds data for a string
func NewPropertyString(s string) *Property {
return &Property{
TypeCode: 'S',
Data: []byte(s),
}
}
// Size returns how much space the property would take up in an FBX
func (p *Property) Size() uint64 {
size := uint64(len(p.Data)) + 1 // +1 comes from the typecode byte
// Strings and byte arrays have 4 bytes that represent the size of the
// string/[]byte
if p.TypeCode == 'S' || p.TypeCode == 'R' {
size += 4
}
return size
}
func (p Property) Write(w io.Writer) error {
_, err := w.Write([]byte{p.TypeCode})
if err != nil {
return err
}
if p.TypeCode == 'S' || p.TypeCode == 'R' {
err = binary.Write(w, binary.LittleEndian, uint32(len(p.Data)))
if err != nil {
return err
}
}
_, err = w.Write(p.Data)
return err
}
// AsString interprets the byte data as a string
func (p *Property) AsString() string {
return string(p.Data)
}
// AsBytes just returns the raw bytes found in the property
func (p *Property) AsBytes() []byte {
return p.Data
}
// AsInt8 interprets the first byte of our data as an 8bit integer
func (p *Property) AsInt8() int8 {
var data int8
buf := bytes.NewReader(p.Data)
/*err :=*/ binary.Read(buf, binary.LittleEndian, &data)
return data
}
// AsInt16 interprets the data buffer as a 16bit integer
func (p *Property) AsInt16() int16 {
var data int16
buf := bytes.NewReader(p.Data)
/*err :=*/ binary.Read(buf, binary.LittleEndian, &data)
return data
}
// AsInt32 interprets the data buffer as a 32bit integer
func (p *Property) AsInt32() int32 {
var data int32
buf := bytes.NewReader(p.Data)
/*err :=*/ binary.Read(buf, binary.LittleEndian, &data)
return data
}
// AsInt64 interprets the data buffer as a 64bit integer
func (p *Property) AsInt64() int64 {
var data int64
buf := bytes.NewReader(p.Data)
/*err :=*/ binary.Read(buf, binary.LittleEndian, &data)
return data
}
// AsFloat32 interprets the data buffer as a 32bit float
func (p *Property) AsFloat32() float32 {
var data float32
buf := bytes.NewReader(p.Data)
/*err :=*/ binary.Read(buf, binary.LittleEndian, &data)
return data
}
// AsFloat64 interprets the data buffer as a 64bit float
func (p *Property) AsFloat64() float64 {
var data float64
buf := bytes.NewReader(p.Data)
/*err :=*/ binary.Read(buf, binary.LittleEndian, &data)
return data
}
// AsBool interprets the first byte of data in the buffer as a boolean
func (p *Property) AsBool() bool {
return p.Data[0] != 0
}
func (p *Property) String() string {
return fmt.Sprintf("%v", p.Data)
}