-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathproperty.go
212 lines (190 loc) · 4.63 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
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
package ofbx
import (
"fmt"
"io"
)
// PropertyType is a mapping of letter to data type
type PropertyType rune
// Property types block
const (
BOOL PropertyType = 'C'
INT16 PropertyType = 'Y'
LONG PropertyType = 'L'
INTEGER PropertyType = 'I'
STRING PropertyType = 'S'
RAWSTRING PropertyType = 'R'
FLOAT PropertyType = 'F'
DOUBLE PropertyType = 'D'
ArrayDOUBLE PropertyType = 'd'
ArrayINT PropertyType = 'i'
ArrayLONG PropertyType = 'l'
ArrayFLOAT PropertyType = 'f'
ArrayBOOL PropertyType = 'b'
ArrayBYTE PropertyType = 'c'
)
var (
propertyTypeSizes = map[PropertyType]int{
BOOL: 1,
INT16: 2,
DOUBLE: 8,
INTEGER: 4,
LONG: 8,
FLOAT: 4,
ArrayDOUBLE: 8,
ArrayINT: 4,
ArrayLONG: 8,
ArrayFLOAT: 4,
ArrayBOOL: 1,
ArrayBYTE: 1,
}
)
func (p *Property) stringValue() string {
switch p.Type {
case BOOL:
return fmt.Sprintf("%v", p.value.toBool())
case LONG:
return fmt.Sprintf("%d", p.value.toint64())
case INTEGER:
return fmt.Sprintf("%d", p.value.toInt32())
case STRING:
return p.value.String()
case RAWSTRING:
return p.value.String()
case FLOAT:
return fmt.Sprintf("%f", p.value.toFloat())
case DOUBLE:
return fmt.Sprintf("%f", p.value.toDouble())
case ArrayDOUBLE:
sli, err := parseArrayRawFloat64(p)
if err != nil {
return "Bad Format F64s " + err.Error()
}
return fmt.Sprintf("%v", sli)
case ArrayINT:
sli, err := parseArrayRawInt(p)
if err != nil {
return "Bad Format Ints " + err.Error()
}
return fmt.Sprintf("%v", sli)
case ArrayLONG:
sli, err := parseArrayRawInt64(p)
if err != nil {
return "Bad Format I64s " + err.Error()
}
return fmt.Sprintf("%v", sli)
case ArrayFLOAT:
sli, err := parseArrayRawFloat32(p)
if err != nil {
return "Bad Format F32s " + err.Error()
}
return fmt.Sprintf("%v", sli)
case ArrayBOOL:
return "Bool array not implemented"
case ArrayBYTE:
return "Byte array not implemented"
}
return "Error: Not a known property Type " + string(p.Type)
}
// Size returns the current property type's size
func (pt PropertyType) Size() int {
return propertyTypeSizes[pt]
}
// IsArray checks whether the property is an array
func (pt PropertyType) IsArray() bool {
switch pt {
case ArrayDOUBLE, ArrayFLOAT, ArrayINT, ArrayLONG, ArrayBOOL, ArrayBYTE:
return true
}
return false
}
// A Property is template class is used to ensure that the data of a FbxObject is strongly typed
type Property struct {
Count int
Type PropertyType
value *DataView
Encoding uint32
compressedLength uint32
}
func (p *Property) getValuesF32() ([]float32, error) {
return parseArrayRawFloat32(p)
}
func (p *Property) getValuesInt64() ([]int64, error) {
return parseArrayRawInt64(p)
}
func findChildren(element *Element, id string) []*Element {
iterables := element.Children
for idx, val := range iterables {
if val.ID.String() == id {
return iterables[idx:]
}
}
return []*Element{}
}
func assignSingleChildProperty(element *Element, id string, dv **DataView) bool {
prop := findSingleChildProperty(element, id)
if prop != nil {
*dv = prop.value
return true
}
return false
}
func findSingleChildProperty(element *Element, id string) *Property {
iterables := element.Children
for idx, val := range iterables {
if val.ID.String() == id {
if len(iterables[idx].Properties) > 0 {
return iterables[idx].Properties[0]
}
}
}
return nil
}
func findChildProperty(element *Element, id string) []*Property {
iterables := element.Children
for idx, val := range iterables {
if val.ID.String() == id {
return iterables[idx].Properties
}
}
return nil
}
func resolveProperty(obj Obj, name string) *Element {
elems := findChildren(obj.Element(), "Properties70")
if elems == nil {
return nil
}
elems = elems[0].Children
for _, elem := range elems {
if prop := elem.getProperty(0); prop != nil && prop.value.String() == name {
return elem
}
}
return nil
}
func isString(prop *Property) bool {
if prop == nil {
return false
}
return prop.Type == STRING
}
func isLong(prop *Property) bool {
if prop == nil {
return false
}
return prop.Type == LONG
}
func (p *Property) String() string {
return p.stringPrefix("")
}
func (p *Property) stringPrefix(prefix string) string {
p.value.Seek(0, io.SeekStart)
if p.value.Len() == 0 {
return ""
}
s := prefix + p.stringValue()
// s += ", proptype= " + fmt.Sprintf("%q", p.typ)
// s += "count=" + fmt.Sprintf("%d", p.count)
// s += ", encoding=" + fmt.Sprintf("%d", p.encoding)
// s += ", compressedLen=" + fmt.Sprintf("%d", p.compressedLength)
return s
}