-
Notifications
You must be signed in to change notification settings - Fork 2
/
node.go
124 lines (107 loc) · 2.2 KB
/
node.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
package fbx
import (
"fmt"
"strings"
)
type Node struct {
EndOffset uint64
NumProperties uint64
PropertyListLen uint64
NameLen uint8
Name string
Properties []*Property
NestedNodes []*Node
}
func (node *Node) IsEmpty() bool {
if node.EndOffset == 0 &&
node.NumProperties == 0 &&
node.PropertyListLen == 0 &&
node.NameLen == 0 {
return true
}
return false
}
func (node *Node) FilterName(name string) []*Node {
return node.Filter(FilterName(name))
}
func (node *Node) Filter(f NodeFilter) (nodes []*Node) {
if f(node) {
nodes = append(nodes, node)
}
for _, sub := range node.NestedNodes {
subNodes := sub.Filter(f)
nodes = append(nodes, subNodes...)
}
return
}
func (node *Node) Int32Slice(name string) ([]int32, bool) {
nodes := node.FilterName(name)
if len(nodes) != 1 {
return nil, false
}
properties := nodes[0].Properties
if len(properties) != 1 {
return nil, false
}
return properties[0].AsInt32Slice()
}
func (node *Node) Float64Slice(name string) ([]float64, bool) {
nodes := node.FilterName(name)
if len(nodes) != 1 {
return nil, false
}
properties := nodes[0].Properties
if len(properties) != 1 {
return nil, false
}
return properties[0].AsFloat64Slice()
}
func (n *Node) String() string {
b := strings.Builder{}
b.WriteString(n.Name)
b.WriteString(":")
if len(n.Properties) > 0 {
b.WriteString(fmt.Sprint("", n.Properties, ""))
}
if len(n.NestedNodes) > 0 {
b.WriteString(fmt.Sprint("{", n.NestedNodes, "}"))
}
b.WriteString("\n")
return b.String()
}
func (node *Node) GetNode(name string) *Node {
for _, n := range node.NestedNodes {
if n.Name == name {
return n
}
}
return nil
}
func (node *Node) GetInt32Slice(name string) []int32 {
nn := node.GetNode(name)
if nn == nil {
return nil
}
if len(nn.Properties) < 1 {
return nil
}
if nn.Properties[0].TypeCode != 'i' {
return nil
}
a, _ := nn.Properties[0].AsInt32Slice()
return a
}
func (node *Node) GetFloat64Slice(name string) []float64 {
nn := node.GetNode(name)
if nn == nil {
return nil
}
if len(nn.Properties) < 1 {
return nil
}
if nn.Properties[0].TypeCode != 'd' {
return nil
}
a, _ := nn.Properties[0].AsFloat64Slice()
return a
}