-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathnodesearch.go
More file actions
646 lines (568 loc) · 13.2 KB
/
Copy pathnodesearch.go
File metadata and controls
646 lines (568 loc) · 13.2 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
package tetra3d
import (
"strings"
)
type NodeIterator interface {
ForEach(forEach func(node INode, index int) bool)
}
type NodeList []INode
func (n NodeList) ForEach(forEach func(node INode, index int) bool) {
for i, node := range n {
if !forEach(node, i) {
return
}
}
}
func (n *NodeList) Combine(other NodeIterator) {
other.ForEach(func(node INode, index int) bool {
n.Add(node)
return true
})
}
func (n NodeList) Count() int {
return len(n)
}
func (n NodeList) Capacity() int {
return cap(n)
}
func (n *NodeList) Add(node INode) {
(*n) = append((*n), node)
}
// Returns the element in the NodeSlice at the given index.
// The index loops around the bounds of the slice, such that NodeSlice.Get(-1)
// returns the last element in the slice.
func (n NodeList) Get(index int) INode {
if len(n) == 0 {
return nil
}
return n[index%len(n)]
}
func (n NodeList) First() INode {
if len(n) == 0 {
return nil
}
return n[0]
}
func (n NodeList) Last() INode {
if len(n) == 0 {
return nil
}
return n[len(n)-1]
}
func (n *NodeList) Clear() {
// clear(*n)
*n = (*n)[:0]
}
// Represents a set of options to control searching through a hierarchy of nodes recursively, evaluating filters to find
// nodes that fit criteria, and do so lazily (e.g. one at a time as necessary).
// The object can also process the results and output them to a NodeSlice (a slice of Nodes).
// Note that the filtering options do stack (e.g. if you specify `startingNode.Search().ByNames("Player").ByPropPair("hunger", 100)“,
// a node would have to be a descendant of `startingNode`, be named "Player", and have a property named "hunger" that has a value of 100
// to pass the filters and be considered "found").
type SearchOptions struct {
Start INode
noStartingNode bool
filterStack func(node INode) bool
}
func (s SearchOptions) ByNames(names ...string) SearchOptions {
last := s.filterStack
s.filterStack = func(node INode) bool {
add := false
name := node.Name()
for _, n := range names {
if name == n {
add = true
break
}
}
if add {
if last != nil {
return last(node)
} else {
return true
}
}
return false
}
return s
}
func (s SearchOptions) ByNamesPartial(names ...string) SearchOptions {
last := s.filterStack
s.filterStack = func(node INode) bool {
add := false
name := node.Name()
for _, n := range names {
if strings.Contains(name, n) {
add = true
break
}
}
if add {
if last != nil {
return last(node)
} else {
return true
}
}
return false
}
return s
}
func (s SearchOptions) ByParentNames(names ...string) SearchOptions {
last := s.filterStack
s.filterStack = func(node INode) bool {
add := false
if node.Parent() != nil {
name := node.Parent().Name()
for _, n := range names {
if name == n {
add = true
break
}
}
if add {
if last != nil {
return last(node)
} else {
return true
}
}
}
return false
}
return s
}
func (s SearchOptions) ByParentNamesPartial(names ...string) SearchOptions {
last := s.filterStack
s.filterStack = func(node INode) bool {
add := false
if node.Parent() != nil {
name := node.Parent().Name()
for _, n := range names {
if strings.Contains(name, n) {
add = true
break
}
}
if add {
if last != nil {
return last(node)
} else {
return true
}
}
}
return false
}
return s
}
func (s SearchOptions) ByPropNames(names ...string) SearchOptions {
last := s.filterStack
s.filterStack = func(node INode) bool {
add := false
props := node.Properties()
for _, n := range names {
if props.Has(n) {
add = true
break
}
}
if add {
if last != nil {
return last(node)
} else {
return true
}
}
return false
}
return s
}
func (s SearchOptions) ByParentPropNames(names ...string) SearchOptions {
last := s.filterStack
s.filterStack = func(node INode) bool {
add := false
if node.Parent() != nil {
props := node.Parent().Properties()
for _, n := range names {
if props.Has(n) {
add = true
break
}
}
if add {
if last != nil {
return last(node)
} else {
return true
}
}
}
return false
}
return s
}
func (s SearchOptions) ByPropValues(values ...any) SearchOptions {
last := s.filterStack
s.filterStack = func(node INode) bool {
add := false
props := node.Properties()
for _, value := range values {
for _, prop := range props.data {
if prop.Value == value {
add = true
break
}
}
if add {
break
}
}
if add {
if last != nil {
return last(node)
} else {
return true
}
}
return false
}
return s
}
func (s SearchOptions) ByParentPropValues(values ...any) SearchOptions {
last := s.filterStack
s.filterStack = func(node INode) bool {
add := false
if parent := node.Parent(); parent != nil {
props := parent.Properties()
for _, value := range values {
for _, prop := range props.data {
if prop.Value == value {
add = true
break
}
}
if add {
break
}
}
}
if add {
if last != nil {
return last(node)
} else {
return true
}
}
return false
}
return s
}
func (s SearchOptions) ByPropPair(name string, value any) SearchOptions {
last := s.filterStack
s.filterStack = func(node INode) bool {
if prop := node.Properties().GetByName(name); prop != nil && prop.Value == value {
if last != nil {
return last(node)
} else {
return true
}
}
return false
}
return s
}
func (s SearchOptions) ByParentPropPair(name string, value any) SearchOptions {
last := s.filterStack
s.filterStack = func(node INode) bool {
if node.Parent() != nil {
if prop := node.Parent().Properties().GetByName(name); prop != nil && prop.Value == value {
if last != nil {
return last(node)
} else {
return true
}
}
}
return false
}
return s
}
func (s SearchOptions) ByType(nt NodeType) SearchOptions {
last := s.filterStack
s.filterStack = func(node INode) bool {
if node.Type().Is(nt) {
if last != nil {
return last(node)
} else {
return true
}
}
return false
}
return s
}
func (s SearchOptions) ByParentType(nt NodeType) SearchOptions {
last := s.filterStack
s.filterStack = func(node INode) bool {
if node.Parent() != nil && node.Parent().Type().Is(nt) {
if last != nil {
return last(node)
} else {
return true
}
}
return false
}
return s
}
func (s SearchOptions) ByPropHasBitfieldValue(bitValue Bitfield) SearchOptions {
last := s.filterStack
s.filterStack = func(node INode) bool {
for _, prop := range node.Properties().data {
if prop.IsBitfield() && prop.AsBitfield().Contains(bitValue) {
if last != nil {
return last(node)
} else {
return true
}
}
}
return false
}
return s
}
func (s SearchOptions) ByParentPropHasBitfieldValue(bitValue Bitfield) SearchOptions {
last := s.filterStack
s.filterStack = func(node INode) bool {
if parent := node.Parent(); parent != nil {
for _, prop := range parent.Properties().data {
if prop.IsBitfield() && prop.AsBitfield().Contains(bitValue) {
if last != nil {
return last(node)
} else {
return true
}
}
}
}
return false
}
return s
}
func (s SearchOptions) ByPropHasBitfieldValueByName(bitfieldValueName string) SearchOptions {
last := s.filterStack
bitfieldValue := Bitfield(0)
s.filterStack = func(node INode) bool {
if bitfieldValue == 0 {
if lib := node.Library(); lib != nil {
bitfieldValue = lib.BitfieldValueByName(bitfieldValueName)
}
}
for _, prop := range node.Properties().data {
if prop.IsBitfield() {
if scene := node.Scene(); scene != nil && prop.AsBitfield().Contains(bitfieldValue) {
if last != nil {
return last(node)
} else {
return true
}
}
}
}
return false
}
return s
}
func (s SearchOptions) ByParentPropHasBitfieldValueByNames(bitfieldValueNames ...string) SearchOptions {
last := s.filterStack
lib := s.Start.Library()
if lib == nil {
return s
}
bitfieldNamesToValues := []Bitfield{}
for _, n := range bitfieldValueNames {
if v := lib.BitfieldValueByName(n); v > 0 {
bitfieldNamesToValues = append(bitfieldNamesToValues, v)
}
}
if len(bitfieldNamesToValues) == 0 {
// No applicable bitfield names found in the library,
// so no Node passes
s.filterStack = func(node INode) bool {
return false
}
}
s.filterStack = func(node INode) bool {
if parent := node.Parent(); parent != nil {
for _, prop := range parent.Properties().data {
for _, bf := range bitfieldNamesToValues {
if prop.IsBitfield() && prop.AsBitfield().Contains(bf) {
if last != nil {
return last(node)
} else {
return true
}
}
}
}
}
return false
}
return s
}
func (s SearchOptions) ByCustomFunc(customFunc func(INode) bool) SearchOptions {
if customFunc == nil {
return s
}
last := s.filterStack
s.filterStack = func(node INode) bool {
if customFunc(node) {
if last != nil {
return last(node)
} else {
return true
}
}
return false
}
return s
}
// Returns a copy of SearchOptions with the specified Nodes included as being precluded from testing and searching.
// Their descendants still are, though.
func (s SearchOptions) NotNode(node INode) SearchOptions {
last := s.filterStack
s.filterStack = func(n INode) bool {
if node == n {
return false
}
if last != nil {
return last(node)
}
return true
}
return s
}
// Returns a copy of SearchOptions with the specified Nodes included as being precluded from testing and searching.
// Their descendants still are, though.
func (s SearchOptions) NotNodes(nodes NodeIterator) SearchOptions {
last := s.filterStack
s.filterStack = func(node INode) bool {
found := false
nodes.ForEach(func(n INode, index int) bool {
if node == n {
found = true
return false
}
return true
})
if found {
return false
}
if last != nil {
return last(node)
}
return true
}
return s
}
// Returns a copy of SearchOptions with the option to preclude the starting node from being included in the iteration process.
// Its descendants still are, though.
func (s SearchOptions) WithStartingNode(withStartingNode bool) SearchOptions {
s.noStartingNode = !withStartingNode
return s
}
// Evaluates and returns all Nodes that pass the filter set in the form of a NodeList.
// If a NodeList `out` is passed in, it will be cleared and reused for this purpose to avoid memory allocation.
// Otherwise, a new one will be allocated for this purpose.
// If no nodes pass the filter set, the function returns a nil NodeList (slice).
func (s SearchOptions) GetNodeList() NodeList {
var out = NodeList{}
s.ForEach(func(node INode, index int) bool {
out = append(out, node)
return true
})
return out
}
// Evaluates and returns the first Node that passes the filter set.
// If no nodes pass the filter set, the function returns nil.
func (s SearchOptions) GetFirst() INode {
var out INode
s.ForEach(func(node INode, index int) bool {
out = node
return false
})
return out
}
// Evaluates and returns the first Node that passes the filter set.
// If no nodes pass the filter set, the function returns nil.
func (s SearchOptions) Get(index int) INode {
var out INode
i := 0
s.ForEach(func(node INode, forEachIndex int) bool {
if i == index {
out = node
return false
}
i++
return true
})
return out
}
// Evaluates and returns the last Node that passes the filter set.
// If no nodes pass the filter set, the function returns nil.
func (s SearchOptions) GetLast() INode {
var out INode
s.ForEach(func(node INode, index int) bool {
out = node
return true
})
return out
}
// Evaluates and returns the number of Nodes that pass the filter set.
// If no nodes pass the filter set, the function returns 0.
func (s SearchOptions) GetCount() int {
count := 0
s.ForEach(func(node INode, index int) bool {
count++
return true
})
return count
}
// Loops through each node starting with the root node specified when creating the SearchOptions
// and runs the specified function on the hierarchy if the node has passed the filter options.
// If the function returns true, the next Node in the hierarchy is returned; if not, the loop is ended prematurely.
func (s SearchOptions) ForEach(forEach func(node INode, index int) bool) {
if forEach == nil {
return
}
index := 0
if !s.noStartingNode {
if s.filterStack == nil || s.filterStack(s.Start) {
if !forEach(s.Start, index) {
return
}
index++
}
}
s.Start.ForEachChild(true, func(node INode, index int) bool {
if s.filterStack != nil && !s.filterStack(node) {
return true
}
if !forEach(node, index) {
return false
}
index++
return true
})
}
// Creates a SearchOptions object starting with the given Node.
// You create a SearchOptions object with Node.Search(), specify filters like ByNames(), and then iterate over all
// passing Nodes with SearchOptions.ForEach, or get a slice with GetSlice(). You can also get just the first or last
// with GetFirst() and GetLast().
func (n *Node) Search() SearchOptions {
return SearchOptions{
Start: n,
// filterStack: func(node INode) bool { return true},
}
}