-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtokenize.go
257 lines (232 loc) · 6.05 KB
/
tokenize.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package ofbx
import (
"bufio"
"encoding/binary"
"io"
"strconv"
"github.com/pkg/errors"
)
// Header is a magic set of ints
type Header struct {
Reserved [2]uint8
Version uint32
}
func (h Header) String() string {
b := []byte{}
for _, n := range h.Reserved {
b = append(b, byte(n))
}
s := string(b)
s += " " + strconv.Itoa(int(h.Version))
return s
}
// Cursor is a rapper for a reader
type Cursor struct {
*bufio.Reader
cr *CountReader
}
// ReadSoFar returns how much of the data has been read
func (c *Cursor) ReadSoFar() int {
return c.cr.ReadSoFar - c.Reader.Buffered()
}
func (c *Cursor) readShortString() (string, error) {
length, err := c.ReadByte()
if err != nil {
return "", err
}
//fmt.Print("slength is ", length)
byt := make([]byte, int(length))
_, err = io.ReadFull(c, byt)
if err != nil {
return "", err
}
return string(byt), nil
}
func (c *Cursor) readLongString() (string, error) {
var length uint32
err := binary.Read(c, binary.LittleEndian, &length)
if err != nil {
return "", err
}
byt := make([]byte, length)
_, err = io.ReadFull(c, byt)
if err != nil {
return "", err
}
//fmt.Print(" |S Prop had ", length, "|")
return string(byt), nil
}
func (c *Cursor) readElementOffset(version uint16) (uint64, error) {
if version >= 7500 {
var i uint64
err := binary.Read(c, binary.LittleEndian, &i)
return i, err
}
var i uint32
err := binary.Read(c, binary.LittleEndian, &i)
return uint64(i), err
}
func (c *Cursor) readBytes(length int) []byte {
tempArr := make([]byte, length)
_, err := io.ReadFull(c, tempArr)
if err != nil {
//fmt.Println(err)
}
return tempArr
}
func (c *Cursor) readProperty() (*Property, error) {
if _, err := c.Peek(1); err != nil {
return nil, errors.New("Reading Past End")
}
prop := Property{}
typ, _, err := c.ReadRune()
if err != nil {
//fmt.Println(err)
}
prop.Type = PropertyType(typ)
var val string
//fmt.Println("Got property type:", string(prop.typ))
switch prop.Type {
case 'S':
val, err = c.readLongString()
case 'Y':
val = string(c.readBytes(2))
case 'C':
val = string(c.readBytes(1))
case 'I':
val = string(c.readBytes(4))
case 'F':
val = string(c.readBytes(4))
case 'D':
val = string(c.readBytes(8))
case 'L':
val = string(c.readBytes(8))
case 'R':
tmp := c.readBytes(4)
length := int(binary.LittleEndian.Uint32(tmp))
val = string(append(tmp, c.readBytes(length)...))
case 'b', 'f', 'd', 'l', 'i':
unCompressedLength := c.readBytes(4)
encoding := c.readBytes(4)
compressedLength := c.readBytes(4)
length := int(binary.LittleEndian.Uint32(compressedLength))
if int(binary.LittleEndian.Uint32(encoding)) == 0 {
elemCount := int(binary.LittleEndian.Uint32(unCompressedLength))
switch prop.Type {
case 'f', 'i':
length = elemCount * 4
case 'd', 'l':
length = elemCount * 8
}
}
prop.Encoding = binary.LittleEndian.Uint32(encoding)
prop.compressedLength = binary.LittleEndian.Uint32(compressedLength)
prop.Count = int(binary.LittleEndian.Uint32(unCompressedLength))
//fmt.Println("prop lengths", unCompressedLength, compressedLength, "props encoding", encoding)
val = string(c.readBytes(length))
default:
return nil, errors.New("Did not know this property:" + string(prop.Type))
}
if err != nil {
//fmt.Println(err)
}
prop.value = NewDataView(val)
return &prop, nil
}
func (c *Cursor) readElement(version uint16) (*Element, error) {
v, _ := c.Peek(12)
footer := true
for _, b := range v {
if b != 0 {
footer = false
break
}
}
if footer {
// Note we don't actually read the footer contents yet,
// as far as we know the footer holds no useful information
//fmt.Println("Returning footer")
return nil, nil
}
endOffset, err := c.readElementOffset(version)
if err != nil {
return nil, err
}
//fmt.Println("Obtained element end offset", endOffset)
propCt, err := c.readElementOffset(version)
if err != nil {
return nil, err
}
//fmt.Println("Obtained element prop count", propCt)
_, err = c.readElementOffset(version)
if err != nil {
return nil, err
}
//fmt.Println("Obtained property list length", prop_list_length)
id, err := c.readShortString()
if err != nil {
return nil, err
}
//fmt.Println("Read short string", id)
element := Element{}
element.ID = NewDataView(id)
element.Properties = make([]*Property, propCt)
for i := uint64(0); i < propCt; i++ {
element.Properties[i], err = c.readProperty()
}
if uint64(c.ReadSoFar()) >= endOffset {
//fmt.Println("NO Sentinel sizes ", c.ReadSoFar(), endOffset)
return &element, nil
}
blockSentinelLength := 13
if version >= 7500 {
blockSentinelLength = 25
}
//fmt.Print("sizes pre children ", c.ReadSoFar(), endOffset, uint64(blockSentinelLength))
for uint64(c.ReadSoFar()) < endOffset-uint64(blockSentinelLength) {
child, err := c.readElement(version)
if err != nil {
return nil, errors.Wrap(err, "ReadingChild element failed")
}
element.Children = append(element.Children, child)
if uint64(c.ReadSoFar()) > endOffset {
//fmt.Println("Read past where we were supposed to!!", c.ReadSoFar(), endOffset)
}
}
if uint64(c.ReadSoFar()) > endOffset {
//fmt.Println("Read past where we were supposed to!!", c.ReadSoFar(), endOffset)
}
c.Discard(blockSentinelLength)
//fmt.Println("With Sentinel", uint64(c.ReadSoFar()), "versus", endOffset)
return &element, nil
}
func tokenize(r io.Reader) (*Element, error) {
countReader := NewCountReader(r)
r2 := bufio.NewReader(countReader)
cursor := &Cursor{r2, countReader}
//fmt.Println("initial stats: ", r2.Buffered(), cursor.ReadSoFar())
ok := isBinary(cursor)
if !ok {
return nil, errors.New("Non-binary FBX")
}
var header Header
err := binary.Read(cursor, binary.LittleEndian, &header)
if err != nil {
return nil, err
}
//fmt.Println(header)
//fmt.Println("Header:", header)
root := &Element{}
for {
//fmt.Println("Reading element")
child, err := cursor.readElement(uint16(header.Version))
if err != nil {
//fmt.Println("Read element failure", err)
return nil, err
}
if child == nil {
return root, nil
}
root.Children = append(root.Children, child)
}
}