-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHclStructuredGraphParser.vb
More file actions
1838 lines (1592 loc) · 98.7 KB
/
HclStructuredGraphParser.vb
File metadata and controls
1838 lines (1592 loc) · 98.7 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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
' Version Uploaded of Fo4Library 3.2.0
Option Strict On
Option Explicit On
' =============================================================================
' ESTADO: DEBUG / EN REVISIÓN — NO CERRADO
' -----------------------------------------------------------------------------
' Parseo de estructuras HCL (Havok Cloth): SimClothData, collidables, capsules,
' operadores (MoveParticles, Simulate, CopyVertices, etc.), cloth states.
' Llamado desde HclClothPackageParser_Class.
'
' PENDIENTES CONOCIDOS:
' - Todos los offsets de campos determinados empíricamente para FO4 64-bit.
' No verificados contra Havok SDK, pero todos confirmados con DumpStructuralAnalysis
' en CasualDress.nif.
' - hclSimClothData layout verificado (ver HkxObjectGraphParser.vb para offsets):
' +0x038: Particles (hkVector4 xyz=pos w=invMass)
' +0x048: FixedParticles (uint16 indices)
' +0x058: TriangleIndices (uint16 triplets)
' +0x068: m_unknown68 (54 elems, tipo desconocido — ReadByteArray con stride 1 probablemente incorrecto)
' +0x088: m_unknown88 (uint32 SIN fixups = bone indices para collidables) ← Field88UInt32
' +0x098: m_collidableTransforms (5×hkMatrix4=64B embedded) ← Field98Matrices
' +0x0A8: m_collidables (GLOBAL fixups → hclCollidable) ← offset CORRECTO
' +0x0B8: m_staticConstraintSets (GLOBAL fixups)
' +0x0D8: m_simClothPoses (GLOBAL fixups)
' - BUG: .Name = ResolveLocalString(+0x030) lee el ptr de hkArray (m_collidableTransformIndices)
' como string — es semánticamente incorrecto. Devuelve vacío cuando count=0 (todos los samples
' conocidos), pero retornaría garbage para arrays no vacíos. La clase no tiene m_name serializado.
' - hclCollidable: ShapeObject resuelto vía GLOBAL fixup en +0x88. VERIFICADO.
' hclCollidable.m_transform: hkMatrix4 column-major en +0x020 (4×hkVector4). VERIFICADO.
' - Operadores de simulación: campos internos parcialmente mapeados.
' - Sin soporte para Skyrim SSE (PointerSize=4).
' =============================================================================
Imports System.Collections.Generic
Imports System.Linq
Public NotInheritable Class HclStructuredGraphParser_Class
Public Shared Function ParseSimClothData(graph As HkxObjectGraph_Class, source As HkxVirtualObjectGraph_Class) As HclSimClothDataDetail_Class
If IsNothing(graph) OrElse IsNothing(source) Then Return Nothing
If Not source.ClassName.Equals("hclSimClothData", StringComparison.OrdinalIgnoreCase) Then Return Nothing
Dim collidableObjects = graph.ReadObjectReferenceArray(source.RelativeOffset + &HA8)
Dim constraintObjects = graph.ReadObjectReferenceArray(source.RelativeOffset + &HB8)
Dim defaultPoseObjects = graph.ReadObjectReferenceArray(source.RelativeOffset + &HD8)
Dim result As New HclSimClothDataDetail_Class With {
.SourceObject = source,
.Name = String.Empty, ' hclSimClothData no tiene m_name serializado; +0x030 es hkArray ptr (m_collidableTransformIndices), no string
.Field38Vectors = ReadVector4Array(graph, graph.ReadArrayHeader(source.RelativeOffset + &H38)),
.Field48UInt16 = ReadUInt16Array(graph, graph.ReadArrayHeader(source.RelativeOffset + &H48)),
.Field58UInt16 = ReadUInt16Array(graph, graph.ReadArrayHeader(source.RelativeOffset + &H58)),
.Field68Bytes = ReadByteArray(graph, graph.ReadArrayHeader(source.RelativeOffset + &H68)),
.Field88UInt32 = ReadUInt32Array(graph, graph.ReadArrayHeader(source.RelativeOffset + &H88)),
.Field98Matrices = ReadMatrix4Array(graph, graph.ReadArrayHeader(source.RelativeOffset + &H98)),
.Collidables = collidableObjects,
.ConstraintSets = constraintObjects,
.DefaultClothPoses = defaultPoseObjects,
.FieldF8UInt32 = ReadUInt32Array(graph, graph.ReadArrayHeader(source.RelativeOffset + &HF8)),
.Field108Bytes = ReadByteArray(graph, graph.ReadArrayHeader(source.RelativeOffset + &H108)),
.Field118Pairs = ReadUInt32PairArray(graph, graph.ReadArrayHeader(source.RelativeOffset + &H118))
}
result.ParticleDatas.AddRange(ParseSimParticleData(result.Field38Vectors))
result.FixedParticleIndices.AddRange(result.Field48UInt16.Select(Function(value) CInt(value)))
result.Triangles.AddRange(ReadUInt16TriangleArray(result.Field58UInt16))
result.StaticCollisionMasks.AddRange(result.FieldF8UInt32)
result.PinchDetectionFlags.AddRange(result.Field108Bytes)
result.CollidableDetails.AddRange(collidableObjects.Select(Function(obj) ParseCollidable(graph, obj)).Where(Function(detail) Not IsNothing(detail)))
result.DefaultClothPoseDetails.AddRange(defaultPoseObjects.Select(Function(obj) graph.ParseSimClothPose(obj)).Where(Function(detail) Not IsNothing(detail)))
result.ConstraintDetails.AddRange(constraintObjects.Select(Function(obj) ParseConstraintObject(graph, obj)).Where(Function(detail) Not IsNothing(detail)))
Return result
End Function
Public Shared Function ParseClothState(graph As HkxObjectGraph_Class, source As HkxVirtualObjectGraph_Class) As HclClothStateDetail_Class
If IsNothing(graph) OrElse IsNothing(source) Then Return Nothing
If Not source.ClassName.Equals("hclClothState", StringComparison.OrdinalIgnoreCase) Then Return Nothing
Dim result As New HclClothStateDetail_Class With {
.SourceObject = source,
.Name = graph.ResolveLocalString(source.RelativeOffset + &H10),
.Field18UInt32 = ReadUInt32Array(graph, graph.ReadArrayHeader(source.RelativeOffset + &H18)),
.Field28Vectors = ReadVector4Array(graph, graph.ReadArrayHeader(source.RelativeOffset + &H28)),
.Field38Structs = ReadRawStructArray(graph, graph.ReadArrayHeader(source.RelativeOffset + &H38), 32),
.Field48Vectors = ReadVector4Array(graph, graph.ReadArrayHeader(source.RelativeOffset + &H48)),
.FieldB0Structs = ReadRawStructArray(graph, graph.ReadArrayHeader(source.RelativeOffset + &HB0), 72),
.FieldC0Bytes = ReadByteArray(graph, graph.ReadArrayHeader(source.RelativeOffset + &HC0)),
.FieldD8Bytes = ReadByteArray(graph, graph.ReadArrayHeader(source.RelativeOffset + &HD8)),
.FieldF0Bytes = ReadByteArray(graph, graph.ReadArrayHeader(source.RelativeOffset + &HF0)),
.Field108Bytes = ReadByteArray(graph, graph.ReadArrayHeader(source.RelativeOffset + &H108)),
.Field120Bytes = ReadByteArray(graph, graph.ReadArrayHeader(source.RelativeOffset + &H120)),
.Field138Bytes = ReadByteArray(graph, graph.ReadArrayHeader(source.RelativeOffset + &H138))
}
result.OperatorIndices.AddRange(result.Field18UInt32.Select(Function(value) CInt(value)))
result.BufferAccesses.AddRange(ParseStateBufferAccessArray(graph, graph.ReadArrayHeader(source.RelativeOffset + &H28)))
result.AuxiliaryBufferAccesses.AddRange(ParseStateBufferAccessArray(graph, graph.ReadArrayHeader(source.RelativeOffset + &H48)))
result.TransformAccessContainers.AddRange(ParseStateTransformAccessContainerArray(graph, graph.ReadArrayHeader(source.RelativeOffset + &H38)))
For Each container In result.TransformAccessContainers
result.TransformSetAccesses.AddRange(container.Accesses)
Next
Return result
End Function
Private Shared Function ParseStateBufferAccessArray(graph As HkxObjectGraph_Class, field As HkxObjectArrayHeader_Class) As List(Of HclClothStateBufferAccessDetail_Class)
Dim result As New List(Of HclClothStateBufferAccessDetail_Class)
For Each raw In ReadRawStructArray(graph, field, 16)
Dim access = ParseStateBufferAccess(raw)
If Not IsNothing(access) Then result.Add(access)
Next
Return result
End Function
Private Shared Function ParseStateBufferAccess(raw As HkxRawStructGraph_Class) As HclClothStateBufferAccessDetail_Class
If IsNothing(raw) Then Return Nothing
Dim result As New HclClothStateBufferAccessDetail_Class With {
.EntryIndex = raw.EntryIndex,
.EntryRelativeOffset = raw.EntryRelativeOffset,
.RawStruct = raw,
.Word0 = If(raw.UInt32Values.Count > 0, raw.UInt32Values(0), 0UI),
.Word1 = If(raw.UInt32Values.Count > 1, raw.UInt32Values(1), 0UI),
.Word2 = If(raw.UInt32Values.Count > 2, raw.UInt32Values(2), 0UI),
.Word3 = If(raw.UInt32Values.Count > 3, raw.UInt32Values(3), 0UI)
}
result.BufferIndex = CInt(result.Word0)
result.AccessCode = CInt(result.Word1)
result.AccessCodeLowByte = result.AccessCode And &HFF
result.AccessCodeHighByte = (result.AccessCode >> 8) And &HFF
Return result
End Function
Private Shared Function ParseStateTransformAccessContainerArray(graph As HkxObjectGraph_Class, field As HkxObjectArrayHeader_Class) As List(Of HclClothStateTransformAccessContainerDetail_Class)
Dim result As New List(Of HclClothStateTransformAccessContainerDetail_Class)
For Each raw In ReadRawStructArray(graph, field, 32)
Dim container = ParseStateTransformAccessContainer(graph, raw)
If Not IsNothing(container) Then result.Add(container)
Next
Return result
End Function
Private Shared Function ParseStateTransformAccessContainer(graph As HkxObjectGraph_Class, raw As HkxRawStructGraph_Class) As HclClothStateTransformAccessContainerDetail_Class
If IsNothing(graph) OrElse IsNothing(raw) Then Return Nothing
Dim nestedHeader = graph.ReadArrayHeader(raw.EntryRelativeOffset + &H10)
Dim result As New HclClothStateTransformAccessContainerDetail_Class With {
.EntryIndex = raw.EntryIndex,
.EntryRelativeOffset = raw.EntryRelativeOffset,
.RawStruct = raw,
.NestedAccessHeader = nestedHeader
}
result.HeaderUInt32.AddRange(raw.UInt32Values.Take(4))
For Each nestedRaw In ReadRawStructArray(graph, nestedHeader, 72)
Dim access = ParseStateTransformSetAccess(graph, nestedRaw)
If Not IsNothing(access) Then result.Accesses.Add(access)
Next
Return result
End Function
Private Shared Function ParseStateTransformSetAccess(graph As HkxObjectGraph_Class, raw As HkxRawStructGraph_Class) As HclClothStateTransformSetAccessDetail_Class
If IsNothing(graph) OrElse IsNothing(raw) Then Return Nothing
Dim result As New HclClothStateTransformSetAccessDetail_Class With {
.EntryIndex = raw.EntryIndex,
.EntryRelativeOffset = raw.EntryRelativeOffset,
.RawStruct = raw
}
For subIndex = 0 To 2
Dim componentAccess = ParseStateTransformComponentAccess(graph, raw, subIndex)
If Not IsNothing(componentAccess) Then result.ComponentAccesses.Add(componentAccess)
Next
result.HasAnyMaskData = result.ComponentAccesses.Any(Function(access) access.MaskBytes.Any(Function(value) value <> 0))
Return result
End Function
Private Shared Function ParseStateTransformComponentAccess(graph As HkxObjectGraph_Class, raw As HkxRawStructGraph_Class, subIndex As Integer) As HclClothStateTransformComponentAccessDetail_Class
If IsNothing(graph) OrElse IsNothing(raw) Then Return Nothing
If subIndex < 0 OrElse subIndex > 2 Then Return Nothing
Dim wordBase = subIndex * 6
Dim headerOffset = raw.EntryRelativeOffset + (subIndex * 24)
Dim header = graph.ReadArrayHeader(headerOffset)
Dim result As New HclClothStateTransformComponentAccessDetail_Class With {
.SubIndex = subIndex,
.HeaderRelativeOffset = headerOffset,
.ArrayHeader = header,
.MaskBytes = ReadByteArray(graph, header),
.MaskCount = header.Count,
.CapacityAndFlags = header.CapacityAndFlags,
.TransformCount = If(raw.UInt32Values.Count > wordBase + 4, CInt(raw.UInt32Values(wordBase + 4)), 0),
.ReservedValue = If(raw.UInt32Values.Count > wordBase + 5, raw.UInt32Values(wordBase + 5), 0UI)
}
result.MaskIndices.AddRange(DecodeMaskIndices(result.MaskBytes))
Return result
End Function
Public Shared Function ParseBufferDefinition(graph As HkxObjectGraph_Class, source As HkxVirtualObjectGraph_Class) As HclBufferDefinitionDetail_Class
If IsNothing(graph) OrElse IsNothing(source) Then Return Nothing
If Not source.ClassName.Equals("hclBufferDefinition", StringComparison.OrdinalIgnoreCase) Then Return Nothing
Dim payloadUInt32 = ReadPayloadUInt32(graph, source, &H20)
Return New HclBufferDefinitionDetail_Class With {
.SourceObject = source,
.Name = graph.ResolveLocalString(source.RelativeOffset + &H10),
.PayloadRelativeOffset = source.RelativeOffset + &H20,
.PayloadBytes = ReadPayloadBytes(graph, source, &H20),
.PayloadUInt32 = payloadUInt32,
.ParticleCount = If(payloadUInt32.Count > 0, CInt(payloadUInt32(0)), 0),
.TriangleCount = If(payloadUInt32.Count > 1, CInt(payloadUInt32(1)), 0)
}
End Function
Public Shared Function ParseScratchBufferDefinition(graph As HkxObjectGraph_Class, source As HkxVirtualObjectGraph_Class) As HclScratchBufferDefinitionDetail_Class
If IsNothing(graph) OrElse IsNothing(source) Then Return Nothing
If Not source.ClassName.Equals("hclScratchBufferDefinition", StringComparison.OrdinalIgnoreCase) Then Return Nothing
Dim payloadUInt32 = ReadPayloadUInt32(graph, source, &H20)
Return New HclScratchBufferDefinitionDetail_Class With {
.SourceObject = source,
.Name = graph.ResolveLocalString(source.RelativeOffset + &H10),
.PayloadRelativeOffset = source.RelativeOffset + &H20,
.PayloadBytes = ReadPayloadBytes(graph, source, &H20),
.PayloadUInt32 = payloadUInt32,
.ParticleCount = If(payloadUInt32.Count > 0, CInt(payloadUInt32(0)), 0),
.TriangleCount = If(payloadUInt32.Count > 1, CInt(payloadUInt32(1)), 0)
}
End Function
Public Shared Function ParseMoveParticlesOperator(graph As HkxObjectGraph_Class, source As HkxVirtualObjectGraph_Class) As HclMoveParticlesOperatorDetail_Class
If IsNothing(graph) OrElse IsNothing(source) Then Return Nothing
If Not source.ClassName.Equals("hclMoveParticlesOperator", StringComparison.OrdinalIgnoreCase) Then Return Nothing
Return New HclMoveParticlesOperatorDetail_Class With {
.SourceObject = source,
.Name = graph.ResolveLocalString(source.RelativeOffset + &H10),
.HeaderUInt32 = ReadUInt32Block(graph, source.RelativeOffset + &H18, 2),
.Pairs = ReadVertexParticlePairs(graph, graph.ReadArrayHeader(source.RelativeOffset + &H20))
}
End Function
Public Shared Function ParseSimulateOperator(graph As HkxObjectGraph_Class, source As HkxVirtualObjectGraph_Class) As HclSimulateOperatorDetail_Class
If IsNothing(graph) OrElse IsNothing(source) Then Return Nothing
If Not source.ClassName.Equals("hclSimulateOperator", StringComparison.OrdinalIgnoreCase) Then Return Nothing
Dim header = ReadUInt32Block(graph, source.RelativeOffset + &H18, 6)
Return New HclSimulateOperatorDetail_Class With {
.SourceObject = source,
.Name = graph.ResolveLocalString(source.RelativeOffset + &H10),
.HeaderUInt32 = header,
.SubstepCount = If(header.Count > 3, CInt(header(3)), 0),
.SolveIterationCount = If(header.Count > 4, CInt(header(4)), 0),
.Configs = ReadUInt32ConfigArray(graph, graph.ReadArrayHeader(source.RelativeOffset + &H30))
}
End Function
Public Shared Function ParseCopyVerticesOperator(graph As HkxObjectGraph_Class, source As HkxVirtualObjectGraph_Class) As HclCopyVerticesOperatorDetail_Class
If IsNothing(graph) OrElse IsNothing(source) Then Return Nothing
If Not source.ClassName.Equals("hclCopyVerticesOperator", StringComparison.OrdinalIgnoreCase) Then Return Nothing
Dim payloadBytes = ReadPayloadBytes(graph, source, &H20)
Dim payloadUInt32 = ReadPayloadUInt32(graph, source, &H20)
Return New HclCopyVerticesOperatorDetail_Class With {
.SourceObject = source,
.Name = graph.ResolveLocalString(source.RelativeOffset + &H10),
.HeaderUInt32 = ReadUInt32Block(graph, source.RelativeOffset + &H18, 2),
.PayloadRelativeOffset = source.RelativeOffset + &H20,
.PayloadBytes = payloadBytes,
.PayloadUInt32 = payloadUInt32,
.ElementCount = If(payloadUInt32.Count > 2, CInt(payloadUInt32(2)), 0),
.PayloadAsciiTag = ExtractPrintableAscii(payloadBytes)
}
End Function
Public Shared Function ParseGatherAllVerticesOperator(graph As HkxObjectGraph_Class, source As HkxVirtualObjectGraph_Class) As HclGatherAllVerticesOperatorDetail_Class
If IsNothing(graph) OrElse IsNothing(source) Then Return Nothing
If Not source.ClassName.Equals("hclGatherAllVerticesOperator", StringComparison.OrdinalIgnoreCase) Then Return Nothing
Dim payloadBytes = ReadPayloadBytes(graph, source, &H20)
Dim payloadUInt32 = ReadPayloadUInt32(graph, source, &H20)
Return New HclGatherAllVerticesOperatorDetail_Class With {
.SourceObject = source,
.Name = graph.ResolveLocalString(source.RelativeOffset + &H10),
.HeaderUInt32 = ReadUInt32Block(graph, source.RelativeOffset + &H18, 2),
.PayloadRelativeOffset = source.RelativeOffset + &H20,
.PayloadBytes = payloadBytes,
.PayloadUInt32 = payloadUInt32,
.ElementCount = If(payloadUInt32.Count > 2, CInt(payloadUInt32(2)), 0),
.GatheredVertexIndices = DecodePackedUInt16List(payloadUInt32.Skip(12), If(payloadUInt32.Count > 2, CInt(payloadUInt32(2)), 0)),
.PayloadAsciiTag = ExtractPrintableAscii(payloadBytes)
}
End Function
Private Shared Function DecodePackedUInt16List(words As IEnumerable(Of UInteger), takeCount As Integer) As List(Of UShort)
Dim result As New List(Of UShort)
If IsNothing(words) OrElse takeCount <= 0 Then Return result
For Each word In words
If result.Count < takeCount Then result.Add(CUShort(word And &HFFFFUI))
If result.Count < takeCount Then result.Add(CUShort((word >> 16) And &HFFFFUI))
If result.Count >= takeCount Then Exit For
Next
Return result
End Function
Public Shared Function ParseCapsuleShape(graph As HkxObjectGraph_Class, source As HkxVirtualObjectGraph_Class) As HclCapsuleShapeDetail_Class
If IsNothing(graph) OrElse IsNothing(source) Then Return Nothing
Dim className = If(source.ClassName, String.Empty)
If Not className.Equals("hclCapsuleShape", StringComparison.OrdinalIgnoreCase) AndAlso
Not className.Equals("hclTaperedCapsuleShape", StringComparison.OrdinalIgnoreCase) Then Return Nothing
Dim isTapered = className.Equals("hclTaperedCapsuleShape", StringComparison.OrdinalIgnoreCase)
Dim vectorCount = Math.Max(0, (source.Size - &H10) \ 16)
Dim vectors = ReadVector4Block(graph, source.RelativeOffset + &H10, vectorCount)
Dim endpointA = If(vectorCount > 1, vectors(1), Nothing)
Dim endpointB = If(vectorCount > 2, vectors(2), Nothing)
Dim extraVector0 = If(isTapered AndAlso vectorCount > 8, vectors(8), Nothing)
Dim extraVector1 = If(isTapered AndAlso vectorCount > 9, vectors(9), Nothing)
Dim segmentLength = 0.0F
If endpointA IsNot Nothing AndAlso endpointB IsNot Nothing Then
Dim dx = endpointA.X - endpointB.X
Dim dy = endpointA.Y - endpointB.Y
Dim dz = endpointA.Z - endpointB.Z
segmentLength = CSng(Math.Sqrt((dx * dx) + (dy * dy) + (dz * dz)))
End If
If isTapered AndAlso vectorCount > 5 Then
segmentLength = vectors(5).X
End If
Dim radiusA = If(Not isTapered AndAlso vectorCount > 4, vectors(4).X, If(extraVector0 IsNot Nothing, extraVector0.X, 0.0F))
Dim radiusB = If(Not isTapered AndAlso vectorCount > 4, vectors(4).X, If(extraVector0 IsNot Nothing, extraVector0.Y, 0.0F))
Dim taperFactor = 0.0F
If segmentLength > 0.000001F Then taperFactor = Math.Abs(radiusB - radiusA) / segmentLength
Dim taperCosine = If(isTapered AndAlso extraVector1 IsNot Nothing, extraVector1.X, CSng(Math.Sqrt(Math.Max(0.0R, 1.0R - (taperFactor * taperFactor)))))
Return New HclCapsuleShapeDetail_Class With {
.SourceObject = source,
.ShapeClassName = className,
.Vectors = vectors,
.EndpointA = endpointA,
.EndpointB = endpointB,
.AxisHint = If(vectorCount > 3, vectors(3), Nothing),
.ParameterVector = If(vectorCount > 4, vectors(4), Nothing),
.Radius = radiusA,
.AuxiliaryRadius = radiusB,
.SegmentLength = segmentLength,
.TaperFactor = taperFactor,
.TaperCosine = taperCosine,
.ExtraScalar0 = If(isTapered AndAlso vectorCount > 5, vectors(5).X, 0.0F),
.ExtraScalar1 = If(isTapered AndAlso vectorCount > 6, vectors(6).X, 0.0F),
.ExtraScalar2 = If(isTapered AndAlso vectorCount > 7, vectors(7).X, 0.0F),
.ExtraVector0 = extraVector0,
.ExtraVector1 = extraVector1
}
End Function
Public Shared Function ParseCollidable(graph As HkxObjectGraph_Class, source As HkxVirtualObjectGraph_Class) As HclCollidableDetail_Class
If IsNothing(graph) OrElse IsNothing(source) Then Return Nothing
If Not source.ClassName.Equals("hclCollidable", StringComparison.OrdinalIgnoreCase) Then Return Nothing
Dim payloadBytes = ReadPayloadBytes(graph, source, &H18)
Dim payloadVectors = ReadVector4Block(graph, source.RelativeOffset + &H18, If(IsNothing(payloadBytes), 0, payloadBytes.Length \ 16))
Return New HclCollidableDetail_Class With {
.SourceObject = source,
.Name = graph.ResolveLocalString(source.RelativeOffset + &H10),
.ShapeObject = graph.ResolveGlobalObject(source.RelativeOffset + &H88),
.ShapeDetail = ParseCapsuleShape(graph, graph.ResolveGlobalObject(source.RelativeOffset + &H88)),
.PayloadRelativeOffset = source.RelativeOffset + &H18,
.PayloadBytes = payloadBytes,
.PayloadUInt32 = ReadPayloadUInt32(graph, source, &H18),
.PayloadVectors = payloadVectors,
.TransformMatrix = CreateMatrix4FromVectorRows(payloadVectors, 0),
.LinearVelocity = If(payloadVectors.Count > 4, payloadVectors(4), Nothing),
.AngularVelocity = If(payloadVectors.Count > 5, payloadVectors(5), Nothing),
.ParameterVector = If(payloadVectors.Count > 6, payloadVectors(6), Nothing)
}
End Function
Public Shared Function ParseStandardLinkConstraintSet(graph As HkxObjectGraph_Class, source As HkxVirtualObjectGraph_Class) As HclStandardLinkConstraintSetDetail_Class
If IsNothing(graph) OrElse IsNothing(source) Then Return Nothing
If Not source.ClassName.Equals("hclStandardLinkConstraintSet", StringComparison.OrdinalIgnoreCase) Then Return Nothing
Dim rawLinks = ReadRawStructArray(graph, graph.ReadArrayHeader(source.RelativeOffset + &H20), 12)
Dim result As New HclStandardLinkConstraintSetDetail_Class With {
.SourceObject = source,
.Name = graph.ResolveLocalString(source.RelativeOffset + &H10),
.Links = rawLinks
}
result.LinkDetails.AddRange(ParseDistanceConstraints(rawLinks))
Return result
End Function
Public Shared Function ParseStretchLinkConstraintSet(graph As HkxObjectGraph_Class, source As HkxVirtualObjectGraph_Class) As HclStretchLinkConstraintSetDetail_Class
If IsNothing(graph) OrElse IsNothing(source) Then Return Nothing
If Not source.ClassName.Equals("hclStretchLinkConstraintSet", StringComparison.OrdinalIgnoreCase) Then Return Nothing
Dim rawLinks = ReadRawStructArray(graph, graph.ReadArrayHeader(source.RelativeOffset + &H20), 12)
Dim result As New HclStretchLinkConstraintSetDetail_Class With {
.SourceObject = source,
.Name = graph.ResolveLocalString(source.RelativeOffset + &H10),
.Links = rawLinks
}
result.LinkDetails.AddRange(ParseDistanceConstraints(rawLinks))
Return result
End Function
Public Shared Function ParseBendStiffnessConstraintSet(graph As HkxObjectGraph_Class, source As HkxVirtualObjectGraph_Class) As HclBendStiffnessConstraintSetDetail_Class
If IsNothing(graph) OrElse IsNothing(source) Then Return Nothing
If Not source.ClassName.Equals("hclBendStiffnessConstraintSet", StringComparison.OrdinalIgnoreCase) Then Return Nothing
Dim rawLinks = ReadRawStructArray(graph, graph.ReadArrayHeader(source.RelativeOffset + &H20), 32)
Dim result As New HclBendStiffnessConstraintSetDetail_Class With {
.SourceObject = source,
.Name = graph.ResolveLocalString(source.RelativeOffset + &H10),
.Links = rawLinks
}
result.LinkDetails.AddRange(ParseBendConstraints(rawLinks))
Return result
End Function
Public Shared Function ParseLocalRangeConstraintSet(graph As HkxObjectGraph_Class, source As HkxVirtualObjectGraph_Class) As HclLocalRangeConstraintSetDetail_Class
If IsNothing(graph) OrElse IsNothing(source) Then Return Nothing
If Not source.ClassName.Equals("hclLocalRangeConstraintSet", StringComparison.OrdinalIgnoreCase) Then Return Nothing
Dim rawConstraints = ReadRawStructArray(graph, graph.ReadArrayHeader(source.RelativeOffset + &H20), 16)
Dim result As New HclLocalRangeConstraintSetDetail_Class With {
.SourceObject = source,
.Name = graph.ResolveLocalString(source.RelativeOffset + &H10),
.Constraints = rawConstraints
}
result.ConstraintDetails.AddRange(ParseLocalRangeConstraints(rawConstraints))
result.UniformMaximumDistance = ResolveUniformParameter(result.ConstraintDetails.Select(Function(item) item.MaximumDistance))
result.UniformMaximumNormalDistance = ResolveUniformParameter(result.ConstraintDetails.Select(Function(item) item.MaximumNormalDistance))
result.UniformMinimumNormalDistance = ResolveUniformParameter(result.ConstraintDetails.Select(Function(item) item.MinimumNormalDistance))
result.DistinctParticleCount = result.ConstraintDetails.Select(Function(item) CInt(item.ParticleIndex)).Distinct().Count()
result.DistinctReferenceVertexCount = result.ConstraintDetails.Select(Function(item) CInt(item.ReferenceVertexIndex)).Distinct().Count()
result.ParticleReferenceIdentityCount = result.ConstraintDetails.Where(Function(item) item.ParticleIndex = item.ReferenceVertexIndex).Count()
Return result
End Function
Public Shared Function ParseVolumeConstraintMx(graph As HkxObjectGraph_Class, source As HkxVirtualObjectGraph_Class) As HclVolumeConstraintMxDetail_Class
If IsNothing(graph) OrElse IsNothing(source) Then Return Nothing
If Not source.ClassName.Equals("hclVolumeConstraintMx", StringComparison.OrdinalIgnoreCase) Then Return Nothing
Dim result As New HclVolumeConstraintMxDetail_Class With {
.SourceObject = source,
.Name = graph.ResolveLocalString(source.RelativeOffset + &H10),
.Field20RawStructs = ReadRawStructArray(graph, graph.ReadArrayHeader(source.RelativeOffset + &H20), 352),
.Field30RawStructs = ReadRawStructArray(graph, graph.ReadArrayHeader(source.RelativeOffset + &H30), 32),
.Field40RawStructs = ReadRawStructArray(graph, graph.ReadArrayHeader(source.RelativeOffset + &H40), 352),
.Field50RawStructs = ReadRawStructArray(graph, graph.ReadArrayHeader(source.RelativeOffset + &H50), 32),
.Field20VectorBlocks = ReadVectorStructArray(graph, graph.ReadArrayHeader(source.RelativeOffset + &H20), 22),
.Field30VectorBlocks = ReadVectorStructArray(graph, graph.ReadArrayHeader(source.RelativeOffset + &H30), 2),
.Field40VectorBlocks = ReadVectorStructArray(graph, graph.ReadArrayHeader(source.RelativeOffset + &H40), 22),
.Field50VectorBlocks = ReadVectorStructArray(graph, graph.ReadArrayHeader(source.RelativeOffset + &H50), 2)
}
result.Field20Batches.AddRange(ParseVolumeConstraintBatches(result.Field20RawStructs, result.Field20VectorBlocks))
result.Field30Entries.AddRange(ParseVolumeConstraintVectorEntries(result.Field30VectorBlocks))
result.Field40Batches.AddRange(ParseVolumeConstraintBatches(result.Field40RawStructs, result.Field40VectorBlocks))
result.Field50Entries.AddRange(ParseVolumeConstraintVectorEntries(result.Field50VectorBlocks))
result.Field20QuadSlots.AddRange(result.Field20Batches.SelectMany(Function(batch) batch.QuadSlots))
result.Field40QuadSlots.AddRange(result.Field40Batches.SelectMany(Function(batch) batch.QuadSlots))
result.Field40BridgeSlots.AddRange(ParseVolumeConstraintBridgeSlots(result.Field40QuadSlots, result.Field20QuadSlots))
result.Field20BridgeSourceQuadSlots.AddRange(BuildVolumeBridgeSourceQuadSlots(result.Field40BridgeSlots))
result.Field40BridgeSourceChain.AddRange(BuildVolumeBridgeSourceChain(result.Field40BridgeSlots))
result.Field20NonBridgeQuadSlots.AddRange(BuildVolumeNonBridgeQuadSlots(result.Field20QuadSlots, result.Field20BridgeSourceQuadSlots))
result.Field30ParameterValues.AddRange(ExtractVolumeConstraintParameterValues(result.Field30Entries))
result.Field50ParameterValues.AddRange(ExtractVolumeConstraintParameterValues(result.Field50Entries))
result.Field50ToField30PivotMatches.AddRange(BuildVolumeConstraintPivotMatches(result.Field50Entries, result.Field30Entries))
result.Field40TerminalQuadSlots.AddRange(BuildVolumeTerminalQuadSlots(result.Field40QuadSlots, result.Field40BridgeSlots))
result.Field30UniformParameter = ResolveUniformParameter(result.Field30ParameterValues)
result.Field50UniformParameter = ResolveUniformParameter(result.Field50ParameterValues)
result.Field50PivotReuseOffset = ResolvePivotReuseOffset(result.Field50ToField30PivotMatches)
result.Field50PivotReuseCount = result.Field50ToField30PivotMatches.Count
result.Field20MidVectorsLookZeroish = result.Field20Batches.All(Function(batch) batch Is Nothing OrElse batch.MidVectorsLookZeroish)
result.Field40MidVectorsLookZeroish = result.Field40Batches.All(Function(batch) batch Is Nothing OrElse batch.MidVectorsLookZeroish)
result.Field20BatchUniformParameter = ResolveUniformParameter(result.Field20Batches.Where(Function(batch) batch IsNot Nothing AndAlso batch.UniformLaneParameter.HasValue).Select(Function(batch) batch.UniformLaneParameter.Value))
result.Field40BatchUniformParameter = ResolveUniformParameter(result.Field40Batches.Where(Function(batch) batch IsNot Nothing AndAlso batch.UniformLaneParameter.HasValue).Select(Function(batch) batch.UniformLaneParameter.Value))
result.Field20LaneParametersUniformAcrossBatches = result.Field20BatchUniformParameter.HasValue
result.Field40LaneParametersUniformAcrossBatches = result.Field40BatchUniformParameter.HasValue
result.Field20BatchParameterMatchesField30Parameter = result.Field20BatchUniformParameter.HasValue AndAlso result.Field30UniformParameter.HasValue AndAlso Math.Abs(CDbl(result.Field20BatchUniformParameter.Value - result.Field30UniformParameter.Value)) <= 0.0001R
result.Field40BatchParameterMatchesField50Parameter = result.Field40BatchUniformParameter.HasValue AndAlso result.Field50UniformParameter.HasValue AndAlso Math.Abs(CDbl(result.Field40BatchUniformParameter.Value - result.Field50UniformParameter.Value)) <= 0.0001R
result.Field20AndField40ParametersDistinct = result.Field20BatchUniformParameter.HasValue AndAlso result.Field40BatchUniformParameter.HasValue AndAlso Math.Abs(CDbl(result.Field20BatchUniformParameter.Value - result.Field40BatchUniformParameter.Value)) > 0.0001R
result.HasDistinctParameterGroups = result.Field20AndField40ParametersDistinct AndAlso result.Field20BatchParameterMatchesField30Parameter AndAlso result.Field40BatchParameterMatchesField50Parameter
result.Field40BridgeCountMatchesField50Count = (result.Field40BridgeSlots.Count > 0 AndAlso result.Field40BridgeSlots.Count = result.Field50Entries.Count)
result.Field40BridgeSlotsExact = result.Field40BridgeSlots.All(Function(slot) slot IsNot Nothing AndAlso slot.SharedParticlesFirst.Count = 2 AndAlso slot.SharedParticlesSecond.Count = 2 AndAlso slot.BridgeParticles.Count = 6)
result.Field40BridgeFormsSequentialChain = ResolveVolumeBridgeSequentialChain(result.Field40BridgeSlots)
Dim terminalExtension = ResolveVolumeTerminalBridgeExtension(result.Field40TerminalQuadSlots, result.Field40BridgeSlots)
result.Field40TerminalExtendsBridgeChain = terminalExtension.Item1
result.Field40TerminalSharedParticleCount = terminalExtension.Item2
result.Field40TerminalAddedParticleCount = terminalExtension.Item3
result.Field40BridgeSourceChainCount = result.Field40BridgeSourceChain.Count
result.Field40NonZeroQuadCount = result.Field40QuadSlots.Where(Function(slot) slot IsNot Nothing AndAlso Not slot.IsAllZero).Count()
result.Field40ExactBridgeCount = result.Field40BridgeSlots.Count
result.Field50PivotTailStartIndex = ResolvePivotTailStartIndex(result.Field50ToField30PivotMatches)
result.Field50MatchesField30Tail = result.Field50PivotTailStartIndex.HasValue AndAlso (result.Field50PivotTailStartIndex.Value + result.Field50PivotReuseCount = result.Field30Entries.Count)
result.Field20NonZeroQuadCount = result.Field20QuadSlots.Where(Function(slot) slot IsNot Nothing AndAlso Not slot.IsAllZero).Count()
result.Field20NonZeroQuadCountMatchesField30Count = (result.Field20NonZeroQuadCount > 0 AndAlso result.Field20NonZeroQuadCount = result.Field30Entries.Count)
result.Field40NonZeroQuadCountMatchesField50Count = (result.Field40NonZeroQuadCount > 0 AndAlso result.Field40NonZeroQuadCount = result.Field50Entries.Count)
result.Field30LeadEntryCount = If(result.Field50PivotTailStartIndex.HasValue, result.Field50PivotTailStartIndex.Value, 0)
result.Field30TailEntryCount = result.Field30Entries.Count - result.Field30LeadEntryCount
result.Field30LeadEntries.AddRange(result.Field30Entries.Where(Function(entry) entry IsNot Nothing AndAlso entry.EntryIndex < result.Field30LeadEntryCount))
result.Field30TailEntries.AddRange(result.Field30Entries.Where(Function(entry) entry IsNot Nothing AndAlso entry.EntryIndex >= result.Field30LeadEntryCount))
result.Field50TailSourceEntries.AddRange(result.Field30TailEntries.Where(Function(entry) result.Field50ToField30PivotMatches.Any(Function(match) match.MatchedEntryIndex = entry.EntryIndex)))
result.Field40TerminalQuadCount = Math.Max(0, result.Field40NonZeroQuadCount - result.Field40ExactBridgeCount)
result.Field20ExtraActiveQuadCount = Math.Max(0, result.Field20NonZeroQuadCount - result.Field30Entries.Count)
result.Field20BridgeSourceQuadCount = result.Field20BridgeSourceQuadSlots.Count
result.Field20NonBridgeQuadCount = result.Field20NonBridgeQuadSlots.Count
result.Field50TailSourceEntryCount = result.Field50TailSourceEntries.Count
result.Field20BridgeSourceAndNonBridgePartitionMatchesActiveQuads = (result.Field20BridgeSourceQuadCount + result.Field20NonBridgeQuadCount = result.Field20NonZeroQuadCount)
result.Field40BridgeAndTerminalPartitionMatchesActiveQuads = (result.Field40ExactBridgeCount + result.Field40TerminalQuadCount = result.Field40NonZeroQuadCount)
result.Field40BridgeSourceChainMatchesField20BridgeSourceCount = (result.Field40BridgeSourceChainCount = result.Field20BridgeSourceQuadCount)
result.Field30LeadCountMatchesField20ExtraActiveQuadCount = (result.Field30LeadEntryCount = result.Field20ExtraActiveQuadCount)
result.Field30TailCountMatchesField40BridgeSourceChainCount = (result.Field30TailEntryCount = result.Field40BridgeSourceChainCount)
result.Field50EntryCountMatchesField40BridgeSourceChainCount = (result.Field50Entries.Count = result.Field40BridgeSourceChainCount)
result.Field50TailSourceCountMatchesField50EntryCount = (result.Field50TailSourceEntryCount = result.Field50Entries.Count)
result.Field50TailSourceCountMatchesField30TailEntryCount = (result.Field50TailSourceEntryCount = result.Field30TailEntryCount)
Return result
End Function
Private Shared Function ParseVolumeConstraintBatches(rawStructs As IEnumerable(Of HkxRawStructGraph_Class),
vectorBlocks As IEnumerable(Of HkxVectorStructBlockGraph_Class)) As List(Of HclVolumeConstraintBatch_Class)
Dim result As New List(Of HclVolumeConstraintBatch_Class)
If IsNothing(rawStructs) Then Return result
Dim vectorByEntry As New Dictionary(Of Integer, HkxVectorStructBlockGraph_Class)
If Not IsNothing(vectorBlocks) Then
For Each block In vectorBlocks
If IsNothing(block) Then Continue For
vectorByEntry(block.EntryIndex) = block
Next
End If
For Each raw In rawStructs
If IsNothing(raw) Then Continue For
Dim block As HkxVectorStructBlockGraph_Class = Nothing
vectorByEntry.TryGetValue(raw.EntryIndex, block)
Dim batch As New HclVolumeConstraintBatch_Class With {
.EntryIndex = raw.EntryIndex,
.RawStruct = raw,
.VectorBlock = block
}
If block IsNot Nothing AndAlso block.Vectors IsNot Nothing Then
batch.AllVectors.AddRange(block.Vectors)
batch.PreQuadVectors.AddRange(block.Vectors.Take(16))
batch.MidVectors.AddRange(block.Vectors.Skip(16).Take(2))
batch.PostQuadVectors.AddRange(block.Vectors.Skip(18).Take(4))
End If
batch.QuadSlots.AddRange(ParseVolumeConstraintQuadSlots({raw}))
PopulateVolumeConstraintBatchLanes(batch)
batch.MidVectorsLookZeroish = batch.MidVectors.All(Function(v) v Is Nothing OrElse (Math.Abs(CDbl(v.X)) <= 0.0001R AndAlso Math.Abs(CDbl(v.Y)) <= 0.0001R AndAlso Math.Abs(CDbl(v.Z)) <= 0.0001R AndAlso Math.Abs(CDbl(v.W)) <= 0.0001R))
batch.UniformLaneParameter = ResolveUniformParameter(batch.Lanes.Where(Function(l) l?.ParameterVector IsNot Nothing).Select(Function(l) CSng(l.ParameterVector.Y)))
batch.LaneParameterIsUniform = batch.UniformLaneParameter.HasValue
result.Add(batch)
Next
Return result
End Function
Private Shared Sub PopulateVolumeConstraintBatchLanes(batch As HclVolumeConstraintBatch_Class)
If IsNothing(batch) Then Return
batch.Lanes.Clear()
For laneIndex = 0 To 3
Dim lane As New HclVolumeConstraintLane_Class With {
.LaneIndex = laneIndex,
.QuadSlot = If(laneIndex < batch.QuadSlots.Count, batch.QuadSlots(laneIndex), Nothing),
.ParameterVector = If(laneIndex < batch.PostQuadVectors.Count, batch.PostQuadVectors(laneIndex), Nothing)
}
lane.CoefficientVectors.AddRange(batch.PreQuadVectors.Skip(laneIndex * 4).Take(4))
batch.Lanes.Add(lane)
Next
End Sub
Private Shared Function ParseVolumeConstraintQuadSlots(items As IEnumerable(Of HkxRawStructGraph_Class)) As List(Of HclVolumeConstraintQuadSlot_Class)
Dim result As New List(Of HclVolumeConstraintQuadSlot_Class)
If IsNothing(items) Then Return result
For Each raw In items
If IsNothing(raw?.RawBytes) OrElse raw.RawBytes.Length < 288 Then Continue For
For slotIndex = 0 To 3
Dim byteOffset = 256 + (slotIndex * 8)
If byteOffset + 7 >= raw.RawBytes.Length Then Exit For
Dim quad As New HclVolumeConstraintQuadSlot_Class With {
.RawStructEntryIndex = raw.EntryIndex,
.SlotIndex = slotIndex,
.ByteOffset = byteOffset,
.ParticleA = BitConverter.ToUInt16(raw.RawBytes, byteOffset),
.ParticleB = BitConverter.ToUInt16(raw.RawBytes, byteOffset + 2),
.ParticleC = BitConverter.ToUInt16(raw.RawBytes, byteOffset + 4),
.ParticleD = BitConverter.ToUInt16(raw.RawBytes, byteOffset + 6)
}
quad.Particles.AddRange(New Integer() {quad.ParticleA, quad.ParticleB, quad.ParticleC, quad.ParticleD})
quad.IsAllZero = (quad.ParticleA = 0 AndAlso quad.ParticleB = 0 AndAlso quad.ParticleC = 0 AndAlso quad.ParticleD = 0)
result.Add(quad)
Next
Next
Return result
End Function
Private Shared Function ParseVolumeConstraintBridgeSlots(subsetSlots As IEnumerable(Of HclVolumeConstraintQuadSlot_Class),
referenceSlots As IEnumerable(Of HclVolumeConstraintQuadSlot_Class)) As List(Of HclVolumeConstraintBridgeSlot_Class)
Dim result As New List(Of HclVolumeConstraintBridgeSlot_Class)
If IsNothing(subsetSlots) OrElse IsNothing(referenceSlots) Then Return result
Dim references = referenceSlots.ToList()
For Each slot In subsetSlots
If IsNothing(slot) OrElse slot.Particles.Count = 0 Then Continue For
Dim overlaps = references.
Select(Function(reference)
If IsNothing(reference) Then Return Nothing
Dim sharedParticles = slot.Particles.Intersect(reference.Particles).ToList()
Return New With { .Slot = reference, .SharedParticles = sharedParticles, .SharedCount = sharedParticles.Count }
End Function).
Where(Function(match) match IsNot Nothing AndAlso match.SharedCount > 0).
OrderByDescending(Function(match) match.SharedCount).
ThenBy(Function(match) match.Slot.RawStructEntryIndex).
ThenBy(Function(match) match.Slot.SlotIndex).
ToList()
Dim bridgeMatches = overlaps.Where(Function(match) match.SharedCount = 2).Take(2).ToList()
If bridgeMatches.Count < 2 Then Continue For
Dim bridge As New HclVolumeConstraintBridgeSlot_Class With {
.TargetSlot = slot,
.FirstSourceSlot = bridgeMatches(0).Slot,
.SecondSourceSlot = bridgeMatches(1).Slot
}
bridge.SharedParticlesFirst.AddRange(bridgeMatches(0).SharedParticles)
bridge.SharedParticlesSecond.AddRange(bridgeMatches(1).SharedParticles)
bridge.OuterParticlesFirst.AddRange(bridgeMatches(0).Slot.Particles.Except(bridgeMatches(0).SharedParticles))
bridge.OuterParticlesSecond.AddRange(bridgeMatches(1).Slot.Particles.Except(bridgeMatches(1).SharedParticles))
bridge.BridgeParticles.AddRange(bridgeMatches(0).Slot.Particles.Union(bridgeMatches(1).Slot.Particles).Distinct())
result.Add(bridge)
Next
Return result
End Function
Private Shared Function BuildVolumeTerminalQuadSlots(activeSlots As IEnumerable(Of HclVolumeConstraintQuadSlot_Class),
bridgeSlots As IEnumerable(Of HclVolumeConstraintBridgeSlot_Class)) As List(Of HclVolumeConstraintQuadSlot_Class)
Dim result As New List(Of HclVolumeConstraintQuadSlot_Class)
If IsNothing(activeSlots) Then Return result
Dim bridgeTargets As New HashSet(Of String)(StringComparer.OrdinalIgnoreCase)
If Not IsNothing(bridgeSlots) Then
For Each bridge In bridgeSlots
If bridge?.TargetSlot Is Nothing Then Continue For
bridgeTargets.Add(CreateVolumeConstraintQuadSlotKey(bridge.TargetSlot))
Next
End If
For Each slot In activeSlots
If slot Is Nothing OrElse slot.IsAllZero Then Continue For
Dim key = CreateVolumeConstraintQuadSlotKey(slot)
If bridgeTargets.Contains(key) Then Continue For
result.Add(slot)
Next
Return result
End Function
Private Shared Function BuildVolumeBridgeSourceQuadSlots(bridgeSlots As IEnumerable(Of HclVolumeConstraintBridgeSlot_Class)) As List(Of HclVolumeConstraintQuadSlot_Class)
Dim result As New List(Of HclVolumeConstraintQuadSlot_Class)
If IsNothing(bridgeSlots) Then Return result
Dim seen As New HashSet(Of String)(StringComparer.OrdinalIgnoreCase)
For Each bridge In bridgeSlots
If bridge Is Nothing Then Continue For
For Each slot In New HclVolumeConstraintQuadSlot_Class() {bridge.FirstSourceSlot, bridge.SecondSourceSlot}
If slot Is Nothing OrElse slot.IsAllZero Then Continue For
Dim key = CreateVolumeConstraintQuadSlotKey(slot)
If seen.Add(key) Then result.Add(slot)
Next
Next
Return result
End Function
Private Shared Function BuildVolumeBridgeSourceChain(bridgeSlots As IEnumerable(Of HclVolumeConstraintBridgeSlot_Class)) As List(Of HclVolumeConstraintQuadSlot_Class)
Dim result As New List(Of HclVolumeConstraintQuadSlot_Class)
If IsNothing(bridgeSlots) Then Return result
Dim ordered = bridgeSlots.
Where(Function(slot) slot?.TargetSlot IsNot Nothing AndAlso slot.FirstSourceSlot IsNot Nothing AndAlso slot.SecondSourceSlot IsNot Nothing).
OrderBy(Function(slot) slot.TargetSlot.RawStructEntryIndex).
ThenBy(Function(slot) slot.TargetSlot.SlotIndex).
ThenBy(Function(slot) slot.TargetSlot.ByteOffset).
ToList()
If ordered.Count = 0 Then Return result
If ordered.Count = 1 Then
result.Add(ordered(0).FirstSourceSlot)
result.Add(ordered(0).SecondSourceSlot)
Return result
End If
Dim nextKeys = New HashSet(Of String)(StringComparer.OrdinalIgnoreCase) From {
CreateVolumeConstraintQuadSlotKey(ordered(1).FirstSourceSlot),
CreateVolumeConstraintQuadSlotKey(ordered(1).SecondSourceSlot)
}
Dim firstSlot = ordered(0).FirstSourceSlot
Dim secondSlot = ordered(0).SecondSourceSlot
If nextKeys.Contains(CreateVolumeConstraintQuadSlotKey(firstSlot)) AndAlso Not nextKeys.Contains(CreateVolumeConstraintQuadSlotKey(secondSlot)) Then
result.Add(secondSlot)
result.Add(firstSlot)
Else
result.Add(firstSlot)
result.Add(secondSlot)
End If
For i = 1 To ordered.Count - 1
Dim tailKey = CreateVolumeConstraintQuadSlotKey(result(result.Count - 1))
Dim leftSlot = ordered(i).FirstSourceSlot
Dim rightSlot = ordered(i).SecondSourceSlot
Dim leftKey = CreateVolumeConstraintQuadSlotKey(leftSlot)
Dim rightKey = CreateVolumeConstraintQuadSlotKey(rightSlot)
If StringComparer.OrdinalIgnoreCase.Equals(leftKey, tailKey) Then
result.Add(rightSlot)
ElseIf StringComparer.OrdinalIgnoreCase.Equals(rightKey, tailKey) Then
result.Add(leftSlot)
Else
result.Clear()
Return result
End If
Next
Return result
End Function
Private Shared Function BuildVolumeNonBridgeQuadSlots(activeSlots As IEnumerable(Of HclVolumeConstraintQuadSlot_Class),
bridgeSourceSlots As IEnumerable(Of HclVolumeConstraintQuadSlot_Class)) As List(Of HclVolumeConstraintQuadSlot_Class)
Dim result As New List(Of HclVolumeConstraintQuadSlot_Class)
If IsNothing(activeSlots) Then Return result
Dim bridgeKeys As New HashSet(Of String)(StringComparer.OrdinalIgnoreCase)
If Not IsNothing(bridgeSourceSlots) Then
For Each slot In bridgeSourceSlots
If slot Is Nothing OrElse slot.IsAllZero Then Continue For
bridgeKeys.Add(CreateVolumeConstraintQuadSlotKey(slot))
Next
End If
For Each slot In activeSlots
If slot Is Nothing OrElse slot.IsAllZero Then Continue For
Dim key = CreateVolumeConstraintQuadSlotKey(slot)
If bridgeKeys.Contains(key) Then Continue For
result.Add(slot)
Next
Return result
End Function
Private Shared Function CreateVolumeConstraintQuadSlotKey(slot As HclVolumeConstraintQuadSlot_Class) As String
If slot Is Nothing Then Return String.Empty
Return $"{slot.RawStructEntryIndex}:{slot.SlotIndex}:{slot.ByteOffset}"
End Function
Private Shared Function ParseVolumeConstraintVectorEntries(items As IEnumerable(Of HkxVectorStructBlockGraph_Class)) As List(Of HclVolumeConstraintVectorEntry_Class)
Dim result As New List(Of HclVolumeConstraintVectorEntry_Class)
If IsNothing(items) Then Return result
For Each item In items
If IsNothing(item) Then Continue For
result.Add(New HclVolumeConstraintVectorEntry_Class With {
.EntryIndex = item.EntryIndex,
.Pivot = If(item.Vectors.Count > 0, item.Vectors(0), Nothing),
.Parameters = If(item.Vectors.Count > 1, item.Vectors(1), Nothing)
})
Next
Return result
End Function
Private Shared Function ExtractVolumeConstraintParameterValues(entries As IEnumerable(Of HclVolumeConstraintVectorEntry_Class)) As IEnumerable(Of Single)
If IsNothing(entries) Then Return Enumerable.Empty(Of Single)()
Return entries.
Where(Function(entry) entry?.Parameters IsNot Nothing).
Select(Function(entry) entry.Parameters.Y).
Distinct().
OrderBy(Function(value) value).
ToList()
End Function
Private Shared Function BuildVolumeConstraintPivotMatches(subsetEntries As IEnumerable(Of HclVolumeConstraintVectorEntry_Class),
referenceEntries As IEnumerable(Of HclVolumeConstraintVectorEntry_Class)) As IEnumerable(Of HclVolumeConstraintPivotMatch_Class)
Dim result As New List(Of HclVolumeConstraintPivotMatch_Class)
If IsNothing(subsetEntries) OrElse IsNothing(referenceEntries) Then Return result
Dim references = referenceEntries.Where(Function(entry) entry?.Pivot IsNot Nothing).ToList()
For Each entry In subsetEntries.Where(Function(item) item?.Pivot IsNot Nothing)
Dim matchIndex = references.FindIndex(Function(candidate) VolumeConstraintVectorsAlmostEqual(entry.Pivot, candidate.Pivot, 0.001F))
If matchIndex < 0 Then Continue For
result.Add(New HclVolumeConstraintPivotMatch_Class With {
.EntryIndex = entry.EntryIndex,
.MatchedEntryIndex = references(matchIndex).EntryIndex
})
Next
Return result
End Function
Private Shared Function ResolveUniformParameter(values As IEnumerable(Of Single)) As Single?
If IsNothing(values) Then Return Nothing
Dim distinctValues = values.Distinct().ToList()
If distinctValues.Count <> 1 Then Return Nothing
Return distinctValues(0)
End Function
Private Shared Function ResolvePivotTailStartIndex(matches As IEnumerable(Of HclVolumeConstraintPivotMatch_Class)) As Integer?
If IsNothing(matches) Then Return Nothing
Dim ordered = matches.Select(Function(match) match.MatchedEntryIndex).Distinct().OrderBy(Function(value) value).ToList()
If ordered.Count = 0 Then Return Nothing
For i = 1 To ordered.Count - 1
If ordered(i) <> ordered(i - 1) + 1 Then Return Nothing
Next
Return ordered(0)
End Function
Private Shared Function ResolvePivotReuseOffset(matches As IEnumerable(Of HclVolumeConstraintPivotMatch_Class)) As Integer?
If IsNothing(matches) Then Return Nothing
Dim deltas = matches.Select(Function(match) match.MatchedEntryIndex - match.EntryIndex).Distinct().ToList()
If deltas.Count <> 1 Then Return Nothing
Return deltas(0)
End Function
Private Shared Function ResolveVolumeBridgeSequentialChain(bridgeSlots As IEnumerable(Of HclVolumeConstraintBridgeSlot_Class)) As Boolean
If IsNothing(bridgeSlots) Then Return False
Dim ordered = bridgeSlots.
Where(Function(slot) slot?.TargetSlot IsNot Nothing AndAlso slot.FirstSourceSlot IsNot Nothing AndAlso slot.SecondSourceSlot IsNot Nothing).
OrderBy(Function(slot) slot.TargetSlot.RawStructEntryIndex).
ThenBy(Function(slot) slot.TargetSlot.SlotIndex).
ThenBy(Function(slot) slot.TargetSlot.ByteOffset).
ToList()
If ordered.Count = 0 Then Return False
If ordered.Count = 1 Then Return True
Dim nextKeys = New HashSet(Of String)(StringComparer.OrdinalIgnoreCase) From {
CreateVolumeConstraintQuadSlotKey(ordered(1).FirstSourceSlot),
CreateVolumeConstraintQuadSlotKey(ordered(1).SecondSourceSlot)
}
Dim chain As New List(Of String)
Dim firstKey = CreateVolumeConstraintQuadSlotKey(ordered(0).FirstSourceSlot)
Dim secondKey = CreateVolumeConstraintQuadSlotKey(ordered(0).SecondSourceSlot)
If nextKeys.Contains(firstKey) AndAlso Not nextKeys.Contains(secondKey) Then
chain.Add(secondKey)
chain.Add(firstKey)
Else
chain.Add(firstKey)
chain.Add(secondKey)
End If
For i = 1 To ordered.Count - 1
Dim tail = chain(chain.Count - 1)
Dim leftKey = CreateVolumeConstraintQuadSlotKey(ordered(i).FirstSourceSlot)
Dim rightKey = CreateVolumeConstraintQuadSlotKey(ordered(i).SecondSourceSlot)
If StringComparer.OrdinalIgnoreCase.Equals(leftKey, tail) Then
chain.Add(rightKey)
ElseIf StringComparer.OrdinalIgnoreCase.Equals(rightKey, tail) Then
chain.Add(leftKey)
Else
Return False
End If
Next
Return chain.Count = ordered.Count + 1
End Function
Private Shared Function ResolveVolumeTerminalBridgeExtension(terminalSlots As IEnumerable(Of HclVolumeConstraintQuadSlot_Class),
bridgeSlots As IEnumerable(Of HclVolumeConstraintBridgeSlot_Class)) As Tuple(Of Boolean, Integer, Integer)
Dim terminals = If(terminalSlots, Enumerable.Empty(Of HclVolumeConstraintQuadSlot_Class)()).Where(Function(slot) slot IsNot Nothing AndAlso Not slot.IsAllZero).ToList()
Dim ordered = If(bridgeSlots, Enumerable.Empty(Of HclVolumeConstraintBridgeSlot_Class)()).
Where(Function(slot) slot?.TargetSlot IsNot Nothing AndAlso slot.FirstSourceSlot IsNot Nothing AndAlso slot.SecondSourceSlot IsNot Nothing).
OrderBy(Function(slot) slot.TargetSlot.RawStructEntryIndex).
ThenBy(Function(slot) slot.TargetSlot.SlotIndex).
ThenBy(Function(slot) slot.TargetSlot.ByteOffset).
ToList()
If terminals.Count <> 1 OrElse ordered.Count = 0 Then Return Tuple.Create(False, 0, 0)
Dim chain As New List(Of HclVolumeConstraintQuadSlot_Class)
chain.Add(ordered(0).FirstSourceSlot)
chain.Add(ordered(0).SecondSourceSlot)
For i = 1 To ordered.Count - 1
Dim tailKey = CreateVolumeConstraintQuadSlotKey(chain(chain.Count - 1))
Dim leftSlot = ordered(i).FirstSourceSlot
Dim rightSlot = ordered(i).SecondSourceSlot
Dim leftKey = CreateVolumeConstraintQuadSlotKey(leftSlot)
Dim rightKey = CreateVolumeConstraintQuadSlotKey(rightSlot)
If StringComparer.OrdinalIgnoreCase.Equals(leftKey, tailKey) Then
chain.Add(rightSlot)
ElseIf StringComparer.OrdinalIgnoreCase.Equals(rightKey, tailKey) Then
chain.Add(leftSlot)
Else
Return Tuple.Create(False, 0, 0)
End If
Next
Dim lastSource = chain(chain.Count - 1)
Dim terminal = terminals(0)
Dim sharedCount = terminal.Particles.Intersect(lastSource.Particles).Count()
Dim added = terminal.Particles.Except(lastSource.Particles).Count()
Return Tuple.Create(sharedCount = 2 AndAlso added = 2, sharedCount, added)
End Function
Private Shared Function VolumeConstraintVectorsAlmostEqual(left As HkxVector4Graph_Class,
right As HkxVector4Graph_Class,
tolerance As Single) As Boolean
If IsNothing(left) OrElse IsNothing(right) Then Return False
Return Math.Abs(left.X - right.X) <= tolerance AndAlso
Math.Abs(left.Y - right.Y) <= tolerance AndAlso
Math.Abs(left.Z - right.Z) <= tolerance AndAlso
Math.Abs(left.W - right.W) <= tolerance
End Function
Private Shared Function ParseSimParticleData(values As IEnumerable(Of HkxVector4Graph_Class)) As List(Of HclSimParticleDataGraph_Class)
Dim result As New List(Of HclSimParticleDataGraph_Class)
If IsNothing(values) Then Return result
Dim entryIndex = 0
For Each value In values
If IsNothing(value) Then Continue For
result.Add(New HclSimParticleDataGraph_Class With {
.EntryIndex = entryIndex,
.Mass = value.X,
.InverseMass = value.Y,
.Radius = value.Z,
.Friction = value.W
})
entryIndex += 1
Next
Return result
End Function
Private Shared Function ParseDistanceConstraints(rawLinks As IEnumerable(Of HkxRawStructGraph_Class)) As List(Of HclDistanceConstraintGraph_Class)
Dim result As New List(Of HclDistanceConstraintGraph_Class)
If IsNothing(rawLinks) Then Return result
For Each raw In rawLinks
If IsNothing(raw) OrElse IsNothing(raw.RawBytes) OrElse raw.RawBytes.Length < 12 Then Continue For
result.Add(New HclDistanceConstraintGraph_Class With {
.EntryIndex = raw.EntryIndex,
.RawStruct = raw,
.ParticleA = BitConverter.ToUInt16(raw.RawBytes, 0),
.ParticleB = BitConverter.ToUInt16(raw.RawBytes, 2),
.RestLength = BitConverter.ToSingle(raw.RawBytes, 4),
.Stiffness = BitConverter.ToSingle(raw.RawBytes, 8)
})
Next
Return result
End Function
Private Shared Function ParseBendConstraints(rawLinks As IEnumerable(Of HkxRawStructGraph_Class)) As List(Of HclBendConstraintGraph_Class)
Dim result As New List(Of HclBendConstraintGraph_Class)
If IsNothing(rawLinks) Then Return result
For Each raw In rawLinks
If IsNothing(raw) OrElse IsNothing(raw.RawBytes) OrElse raw.RawBytes.Length < 32 Then Continue For
result.Add(New HclBendConstraintGraph_Class With {
.EntryIndex = raw.EntryIndex,
.RawStruct = raw,
.WeightA = BitConverter.ToSingle(raw.RawBytes, 0),
.WeightB = BitConverter.ToSingle(raw.RawBytes, 4),
.WeightC = BitConverter.ToSingle(raw.RawBytes, 8),
.WeightD = BitConverter.ToSingle(raw.RawBytes, 12),
.BendStiffness = BitConverter.ToSingle(raw.RawBytes, 16),
.RestCurvature = BitConverter.ToSingle(raw.RawBytes, 20),
.ParticleA = BitConverter.ToUInt16(raw.RawBytes, 24),