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
|
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of NVIDIA CORPORATION nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Copyright (c) 2008-2018 NVIDIA Corporation. All rights reserved.
// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
#ifndef PX_XML_VISITOR_READER_H
#define PX_XML_VISITOR_READER_H
#include "PsArray.h"
#include "PsUtilities.h"
#include "RepXMetaDataPropertyVisitor.h"
#include "SnPxStreamOperators.h"
#include "SnXmlMemoryPoolStreams.h"
#include "SnXmlReader.h"
#include "SnXmlImpl.h"
#include "SnXmlMemoryAllocator.h"
#include "SnXmlStringToType.h"
namespace physx { namespace Sn {
inline PxU32 findEnumByName( const char* inName, const PxU32ToName* inTable )
{
for ( PxU32 idx = 0; inTable[idx].mName != NULL; ++idx )
{
if ( physx::shdfnd::stricmp( inTable[idx].mName, inName ) == 0 )
return inTable[idx].mValue;
}
return 0;
}
PX_INLINE void stringToFlagsType( const char* strData, XmlMemoryAllocator& alloc, PxU32& ioType, const PxU32ToName* inTable )
{
if ( inTable == NULL )
return;
ioType = 0;
if ( strData && *strData)
{
//Destructively parse the string to get out the different flags.
char* theValue = const_cast<char*>( copyStr( &alloc, strData ) );
char* theMarker = theValue;
char* theNext = theValue;
while( theNext && *theNext )
{
++theNext;
if( *theNext == '|' )
{
*theNext = 0;
++theNext;
ioType |= static_cast< PxU32 > ( findEnumByName( theMarker, inTable ) );
theMarker = theNext;
}
}
if ( theMarker && *theMarker )
ioType |= static_cast< PxU32 > ( findEnumByName( theMarker, inTable ) );
alloc.deallocate( reinterpret_cast<PxU8*>( theValue ) );
}
}
template<typename TDataType>
PX_INLINE void stringToEnumType( const char* strData, TDataType& ioType, const PxU32ToName* inTable )
{
ioType = static_cast<TDataType>( findEnumByName( strData, inTable ) );
}
template<typename TDataType>
PX_INLINE bool readProperty( XmlReader& inReader, const char* pname, TDataType& ioType )
{
const char* value;
if ( inReader.read( pname, value ) )
{
stringToType( value, ioType );
return true;
}
return false;
}
template<typename TObjType>
inline TObjType* findReferencedObject( PxCollection& collection, PxSerialObjectId id)
{
PX_ASSERT(id > 0);
TObjType* outObject = static_cast<TObjType*>(const_cast<PxBase*>(collection.find(id)));
if (outObject == NULL)
{
Ps::getFoundation().error(PxErrorCode::eINVALID_PARAMETER, __FILE__, __LINE__,
"PxSerialization::createCollectionFromXml: "
"Reference to ID %d cannot be resolved. Make sure externalRefs collection is specified if required and "
"check Xml file for completeness.",
id);
}
return outObject;
}
template<typename TObjType>
inline bool readReference( XmlReader& inReader, PxCollection& collection, TObjType*& outObject )
{
PxSerialObjectId theId;
const char* theValue = inReader.getCurrentItemValue();
strto( theId, theValue );
if( theId == 0)
{
// the NULL pointer is a valid pointer if the input id is 0
outObject = NULL;
return true;
}
else
{
outObject = findReferencedObject<TObjType>(collection, theId);
return outObject != NULL;
}
}
template<typename TObjType>
inline bool readReference( XmlReader& inReader, PxCollection& inCollection, const char* pname, TObjType*& outObject )
{
outObject = NULL;
PxSerialObjectId theId = 0;
if (readProperty ( inReader, pname, theId ) && theId )
{
outObject = findReferencedObject<TObjType>(inCollection, theId);
}
// the NULL pointer is a valid pointer if the input id is 0
return (outObject != NULL) || 0 == theId;
}
template<typename TEnumType, typename TStorageType>
inline bool readFlagsProperty( XmlReader& reader, XmlMemoryAllocator& allocator, const char* pname, const PxU32ToName* inConversions, PxFlags<TEnumType,TStorageType>& outFlags )
{
const char* value;
if ( reader.read( pname, value ) )
{
PxU32 tempValue = 0;
stringToFlagsType( value, allocator, tempValue, inConversions );
outFlags = PxFlags<TEnumType,TStorageType>(Ps::to16(tempValue) );
return true;
}
return false;
}
template<typename TObjType, typename TReaderType, typename TInfoType>
inline void readComplexObj( TReaderType& oldVisitor, TObjType* inObj, TInfoType& info);
template<typename TObjType, typename TReaderType>
inline void readComplexObj( TReaderType& oldVisitor, TObjType* inObj);
template<typename TReaderType, typename TGeomType>
inline PxGeometry* parseGeometry( TReaderType& reader, TGeomType& /*inGeom*/)
{
PxAllocatorCallback& inAllocator = reader.mAllocator.getAllocator();
TGeomType* shape = PX_PLACEMENT_NEW((inAllocator.allocate(sizeof(TGeomType), "parseGeometry", __FILE__, __LINE__ )), TGeomType);
PxClassInfoTraits<TGeomType> info;
readComplexObj( reader, shape);
return shape;
}
template<typename TReaderType>
inline void parseShape( TReaderType& visitor, PxGeometry*& outResult, Ps::Array<PxMaterial*>& outMaterials)
{
XmlReader& theReader( visitor.mReader );
PxCollection& collection = visitor.mCollection;
visitor.pushCurrentContext();
if ( visitor.gotoTopName() )
{
visitor.pushCurrentContext();
if ( visitor.gotoChild( "Materials" ) )
{
for( bool matSuccess = visitor.gotoFirstChild(); matSuccess;
matSuccess = visitor.gotoNextSibling() )
{
PxMaterial* material = NULL;
if(!readReference<PxMaterial>( theReader, collection, material ))
visitor.mHadError = true;
if ( material )
outMaterials.pushBack( material );
}
}
visitor.popCurrentContext();
visitor.pushCurrentContext();
PxPlaneGeometry plane;
PxHeightFieldGeometry heightField;
PxSphereGeometry sphere;
PxTriangleMeshGeometry mesh;
PxConvexMeshGeometry convex;
PxBoxGeometry box;
PxCapsuleGeometry capsule;
if ( visitor.gotoChild( "Geometry" ) )
{
if ( visitor.gotoFirstChild() )
{
const char* geomTypeName = visitor.getCurrentItemName();
if ( physx::shdfnd::stricmp( geomTypeName, "PxSphereGeometry" ) == 0 ) outResult = parseGeometry(visitor, sphere);
else if ( physx::shdfnd::stricmp( geomTypeName, "PxPlaneGeometry" ) == 0 ) outResult = parseGeometry(visitor, plane);
else if ( physx::shdfnd::stricmp( geomTypeName, "PxCapsuleGeometry" ) == 0 ) outResult = parseGeometry(visitor, capsule);
else if ( physx::shdfnd::stricmp( geomTypeName, "PxBoxGeometry" ) == 0 ) outResult = parseGeometry(visitor, box);
else if ( physx::shdfnd::stricmp( geomTypeName, "PxConvexMeshGeometry" ) == 0 ) outResult = parseGeometry(visitor, convex);
else if ( physx::shdfnd::stricmp( geomTypeName, "PxTriangleMeshGeometry" ) == 0 ) outResult = parseGeometry(visitor, mesh);
else if ( physx::shdfnd::stricmp( geomTypeName, "PxHeightFieldGeometry" ) == 0 ) outResult = parseGeometry(visitor, heightField);
else
PX_ASSERT( false );
}
}
visitor.popCurrentContext();
}
visitor.popCurrentContext();
return;
}
template<typename TReaderType, typename TObjType>
inline void readShapesProperty( TReaderType& visitor, TObjType* inObj, const PxRigidActorShapeCollection* inProp = NULL, bool isSharedShape = false )
{
PX_UNUSED(isSharedShape);
PX_UNUSED(inProp);
XmlReader& theReader( visitor.mReader );
PxCollection& collection( visitor.mCollection );
visitor.pushCurrentContext();
if ( visitor.gotoTopName() )
{
//uggh working around the shape collection api.
//read out materials and geometry
for ( bool success = visitor.gotoFirstChild(); success;
success = visitor.gotoNextSibling() )
{
if( 0 == physx::shdfnd::stricmp( visitor.getCurrentItemName(), "PxShapeRef" ) )
{
PxShape* shape = NULL;
if(!readReference<PxShape>( theReader, collection, shape ))
visitor.mHadError = true;
if(shape)
inObj->attachShape( *shape );
}
else
{
Ps::Array<PxMaterial*> materials;
PxGeometry* geometry = NULL;
parseShape( visitor, geometry, materials);
PxShape* theShape = NULL;
if ( materials.size() )
{
theShape = visitor.mArgs.physics.createShape( *geometry, materials.begin(), Ps::to16(materials.size()), true );
if ( theShape )
{
readComplexObj( visitor, theShape );
if(theShape)
{
inObj->attachShape(*theShape);
collection.add( *theShape );
}
}
}
switch(geometry->getType())
{
case PxGeometryType::eSPHERE :
static_cast<PxSphereGeometry*>(geometry)->~PxSphereGeometry();
break;
case PxGeometryType::ePLANE :
static_cast<PxPlaneGeometry*>(geometry)->~PxPlaneGeometry();
break;
case PxGeometryType::eCAPSULE :
static_cast<PxCapsuleGeometry*>(geometry)->~PxCapsuleGeometry();
break;
case PxGeometryType::eBOX :
static_cast<PxBoxGeometry*>(geometry)->~PxBoxGeometry();
break;
case PxGeometryType::eCONVEXMESH :
static_cast<PxConvexMeshGeometry*>(geometry)->~PxConvexMeshGeometry();
break;
case PxGeometryType::eTRIANGLEMESH :
static_cast<PxTriangleMeshGeometry*>(geometry)->~PxTriangleMeshGeometry();
break;
case PxGeometryType::eHEIGHTFIELD :
static_cast<PxHeightFieldGeometry*>(geometry)->~PxHeightFieldGeometry();
break;
case PxGeometryType::eGEOMETRY_COUNT:
case PxGeometryType::eINVALID:
PX_ASSERT(0);
}
visitor.mAllocator.getAllocator().deallocate(geometry);
}
}
}
visitor.popCurrentContext();
}
struct ReaderNameStackEntry : NameStackEntry
{
bool mValid;
ReaderNameStackEntry( const char* nm, bool valid ) : NameStackEntry(nm), mValid(valid) {}
};
typedef PxProfileArray<ReaderNameStackEntry> TReaderNameStack;
template<typename TObjType>
struct RepXVisitorReaderBase
{
protected:
RepXVisitorReaderBase<TObjType>& operator=(const RepXVisitorReaderBase<TObjType>&);
public:
TReaderNameStack& mNames;
PxProfileArray<PxU32>& mContexts;
PxRepXInstantiationArgs mArgs;
XmlReader& mReader;
TObjType* mObj;
XmlMemoryAllocator& mAllocator;
PxCollection& mCollection;
bool mValid;
bool& mHadError;
RepXVisitorReaderBase( TReaderNameStack& names, PxProfileArray<PxU32>& contexts, const PxRepXInstantiationArgs& args, XmlReader& reader, TObjType* obj
, XmlMemoryAllocator& alloc, PxCollection& collection, bool& hadError )
: mNames( names )
, mContexts( contexts )
, mArgs( args )
, mReader( reader )
, mObj( obj )
, mAllocator( alloc )
, mCollection( collection )
, mValid( true )
, mHadError(hadError)
{
}
RepXVisitorReaderBase( const RepXVisitorReaderBase& other )
: mNames( other.mNames )
, mContexts( other.mContexts )
, mArgs( other.mArgs )
, mReader( other.mReader )
, mObj( other.mObj )
, mAllocator( other.mAllocator )
, mCollection( other.mCollection )
, mValid( other.mValid )
, mHadError( other.mHadError )
{
}
void pushName( const char* name )
{
gotoTopName();
mNames.pushBack( ReaderNameStackEntry( name, mValid ) );
}
void pushBracketedName( const char* name ) { pushName( name ); }
void popName()
{
if ( mNames.size() )
{
if ( mNames.back().mOpen && mNames.back().mValid )
mReader.leaveChild();
mNames.popBack();
}
mValid =true;
if ( mNames.size() && mNames.back().mValid == false )
mValid = false;
}
void pushCurrentContext()
{
mContexts.pushBack( static_cast<PxU32>( mNames.size() ) );
}
void popCurrentContext()
{
if ( mContexts.size() )
{
PxU32 depth = mContexts.back();
PX_ASSERT( mNames.size() >= depth );
while( mNames.size() > depth )
popName();
mContexts.popBack();
}
}
bool updateLastEntryAfterOpen()
{
mNames.back().mValid = mValid;
mNames.back().mOpen = mValid;
return mValid;
}
bool gotoTopName()
{
if ( mNames.size() && mNames.back().mOpen == false )
{
if ( mValid )
mValid = mReader.gotoChild( mNames.back().mName );
updateLastEntryAfterOpen();
}
return mValid;
}
bool isValid() const { return mValid; }
bool gotoChild( const char* name )
{
pushName( name );
return gotoTopName();
}
bool gotoFirstChild()
{
pushName( "__child" );
if ( mValid ) mValid = mReader.gotoFirstChild();
return updateLastEntryAfterOpen();
}
bool gotoNextSibling()
{
bool retval = mValid;
if ( mValid ) retval = mReader.gotoNextSibling();
return retval;
}
const char* getCurrentItemName() { if (mValid ) return mReader.getCurrentItemName(); return ""; }
const char* topName() const
{
if ( mNames.size() ) return mNames.back().mName;
PX_ASSERT( false );
return "bad__repx__name";
}
const char* getCurrentValue()
{
const char* value = NULL;
if ( isValid() && mReader.read( topName(), value ) )
return value;
return NULL;
}
template<typename TDataType>
bool readProperty(TDataType& outType)
{
const char* value = getCurrentValue();
if ( value && *value )
{
stringToType( value, outType );
return true;
}
return false;
}
template<typename TDataType>
bool readExtendedIndexProperty(TDataType& outType)
{
const char* value = mReader.getCurrentItemValue();
if ( value && *value )
{
stringToType( value, outType );
return true;
}
return false;
}
template<typename TRefType>
bool readReference(TRefType*& outRef)
{
return physx::Sn::readReference<TRefType>( mReader, mCollection, topName(), outRef );
}
inline bool readProperty(const char*& outProp )
{
outProp = "";
const char* value = getCurrentValue();
if ( value && *value && mArgs.stringTable )
{
outProp = mArgs.stringTable->allocateStr( value );
return true;
}
return false;
}
inline bool readProperty(PxConvexMesh*& outProp )
{
return readReference<PxConvexMesh>( outProp );
}
inline bool readProperty(PxTriangleMesh*& outProp )
{
return readReference<PxTriangleMesh>( outProp );
}
inline bool readProperty(PxBVH33TriangleMesh*& outProp )
{
return readReference<PxBVH33TriangleMesh>( outProp );
}
inline bool readProperty(PxBVH34TriangleMesh*& outProp )
{
return readReference<PxBVH34TriangleMesh>( outProp );
}
inline bool readProperty(PxHeightField*& outProp )
{
return readReference<PxHeightField>( outProp );
}
inline bool readProperty( PxRigidActor *& outProp )
{
return readReference<PxRigidActor>( outProp );
}
template<typename TAccessorType>
void simpleProperty( PxU32 /*key*/, TAccessorType& inProp )
{
typedef typename TAccessorType::prop_type TPropertyType;
TPropertyType value;
if ( readProperty( value ) )
inProp.set( mObj, value );
}
template<typename TAccessorType>
void enumProperty( PxU32 /*key*/, TAccessorType& inProp, const PxU32ToName* inConversions )
{
typedef typename TAccessorType::prop_type TPropertyType;
const char* strVal = getCurrentValue();
if ( strVal && *strVal )
{
TPropertyType pval;
stringToEnumType( strVal, pval, inConversions );
inProp.set( mObj, pval );
}
}
template<typename TAccessorType>
void flagsProperty( PxU32 /*key*/, const TAccessorType& inProp, const PxU32ToName* inConversions )
{
typedef typename TAccessorType::prop_type TPropertyType;
typedef typename TPropertyType::InternalType TInternalType;
const char* strVal = getCurrentValue();
if ( strVal && *strVal )
{
PxU32 tempValue = 0;
stringToFlagsType( strVal, mAllocator, tempValue, inConversions );
inProp.set( mObj, TPropertyType(TInternalType( tempValue )));
}
}
template<typename TAccessorType, typename TInfoType>
void complexProperty( PxU32* /*key*/, const TAccessorType& inProp, TInfoType& inInfo )
{
typedef typename TAccessorType::prop_type TPropertyType;
if ( gotoTopName() )
{
TPropertyType propVal = inProp.get( mObj );
readComplexObj( *this, &propVal, inInfo );
inProp.set( mObj, propVal );
}
}
template<typename TAccessorType, typename TInfoType>
void bufferCollectionProperty( PxU32* /*key*/, const TAccessorType& inProp, TInfoType& inInfo )
{
typedef typename TAccessorType::prop_type TPropertyType;
Ps::InlineArray<TPropertyType,5> theData;
this->pushCurrentContext();
if ( this->gotoTopName() )
{
for ( bool success = this->gotoFirstChild(); success;
success = this->gotoNextSibling() )
{
TPropertyType propVal;
readComplexObj( *this, &propVal, inInfo );
theData.pushBack(propVal);
}
}
this->popCurrentContext();
inProp.set( mObj, theData.begin(), theData.size() );
}
template<typename TAccessorType, typename TInfoType>
void extendedIndexedProperty( PxU32* /*key*/, const TAccessorType& inProp, TInfoType& inInfo )
{
typedef typename TAccessorType::prop_type TPropertyType;
this->pushCurrentContext();
if ( this->gotoTopName() )
{
PxU32 index = 0;
for ( bool success = this->gotoFirstChild(); success;
success = this->gotoNextSibling() )
{
TPropertyType propVal;
readComplexObj( *this, &propVal, inInfo );
inProp.set(mObj, index, propVal);
++index;
}
}
this->popCurrentContext();
}
template<typename TAccessorType, typename TInfoType>
void PxFixedSizeLookupTableProperty( PxU32* /*key*/, const TAccessorType& inProp, TInfoType& inInfo )
{
typedef typename TAccessorType::prop_type TPropertyType;
const_cast<TAccessorType&>(inProp).clear( mObj );
this->pushCurrentContext();
if ( this->gotoTopName() )
{
for ( bool success = this->gotoFirstChild(); success;
success = this->gotoNextSibling() )
{
TPropertyType propXVal;
readComplexObj( *this, &propXVal, inInfo );
if(this->gotoNextSibling())
{
TPropertyType propYVal;
readComplexObj( *this, &propYVal, inInfo );
const_cast<TAccessorType&>(inProp).addPair(mObj, propXVal, propYVal);
}
}
}
this->popCurrentContext();
}
void handleShapes( const PxRigidActorShapeCollection& inProp )
{
physx::Sn::readShapesProperty( *this, mObj, &inProp );
}
};
template<typename TObjType>
struct RepXVisitorReader : public RepXVisitorReaderBase<TObjType>
{
RepXVisitorReader( TReaderNameStack& names, PxProfileArray<PxU32>& contexts, const PxRepXInstantiationArgs& args, XmlReader& reader, TObjType* obj
, XmlMemoryAllocator& alloc, PxCollection& collection, bool& ret)
: RepXVisitorReaderBase<TObjType>( names, contexts, args, reader, obj, alloc, collection, ret)
{
}
RepXVisitorReader( const RepXVisitorReader<TObjType>& other )
: RepXVisitorReaderBase<TObjType>( other )
{
}
};
// Specialized template to load dynamic rigid, to determine the kinematic state first
template<>
struct RepXVisitorReader<PxRigidDynamic> : public RepXVisitorReaderBase<PxRigidDynamic>
{
RepXVisitorReader( TReaderNameStack& names, PxProfileArray<PxU32>& contexts, const PxRepXInstantiationArgs& args, XmlReader& reader, PxRigidDynamic* obj
, XmlMemoryAllocator& alloc, PxCollection& collection, bool& ret)
: RepXVisitorReaderBase<PxRigidDynamic>( names, contexts, args, reader, obj, alloc, collection, ret)
{
}
RepXVisitorReader( const RepXVisitorReader<PxRigidDynamic>& other )
: RepXVisitorReaderBase<PxRigidDynamic>( other )
{
}
void handleShapes( const PxRigidActorShapeCollection& inProp )
{
// Need to read the parental actor to check if actor is kinematic
// in that case we need to apply the kinematic flag before a shape is set
XmlReaderWriter* parentReader = static_cast<XmlReaderWriter*>(mReader.getParentReader());
if(mObj)
{
const char* value;
if (parentReader->read( "RigidBodyFlags", value ))
{
if(strstr(value, "eKINEMATIC"))
{
mObj->setRigidBodyFlag(PxRigidBodyFlag::eKINEMATIC, true);
}
}
}
physx::Sn::readShapesProperty( *this, mObj, &inProp );
parentReader->release();
}
template<typename TAccessorType>
void simpleProperty( PxU32 /*key*/, TAccessorType& inProp )
{
typedef typename TAccessorType::prop_type TPropertyType;
TPropertyType value;
if (readProperty(value))
{
// If the rigid body is kinematic, we cannot set the LinearVelocity or AngularVelocity
const bool kinematic = (mObj->getRigidBodyFlags() & PxRigidBodyFlag::eKINEMATIC);
if(kinematic && (inProp.mProperty.mKey == PxPropertyInfoName::PxRigidBody_LinearVelocity || inProp.mProperty.mKey == PxPropertyInfoName::PxRigidBody_AngularVelocity))
return;
inProp.set(mObj, value );
}
}
private:
RepXVisitorReader<PxRigidDynamic>& operator=(const RepXVisitorReader<PxRigidDynamic>&);
};
template<>
struct RepXVisitorReader<PxShape> : public RepXVisitorReaderBase<PxShape>
{
RepXVisitorReader( TReaderNameStack& names, PxProfileArray<PxU32>& contexts, const PxRepXInstantiationArgs& args, XmlReader& reader, PxShape* obj
, XmlMemoryAllocator& alloc, PxCollection& collection, bool& ret )
: RepXVisitorReaderBase<PxShape>( names, contexts, args, reader, obj, alloc, collection, ret )
{
}
RepXVisitorReader( const RepXVisitorReader<PxShape>& other )
: RepXVisitorReaderBase<PxShape>( other )
{
}
void handleShapeMaterials( const PxShapeMaterialsProperty& ) //these were handled during construction.
{
}
void handleGeometryProperty( const PxShapeGeometryProperty& )
{
}
private:
RepXVisitorReader<PxShape>& operator=(const RepXVisitorReader<PxShape>&);
};
template<>
struct RepXVisitorReader<PxArticulationLink> : public RepXVisitorReaderBase<PxArticulationLink>
{
RepXVisitorReader( TReaderNameStack& names, PxProfileArray<PxU32>& contexts, const PxRepXInstantiationArgs& args, XmlReader& reader, PxArticulationLink* obj
, XmlMemoryAllocator& alloc, PxCollection& collection, bool& ret )
: RepXVisitorReaderBase<PxArticulationLink>( names, contexts, args, reader, obj, alloc, collection, ret )
{
}
RepXVisitorReader( const RepXVisitorReader<PxArticulationLink>& other )
: RepXVisitorReaderBase<PxArticulationLink>( other )
{
}
void handleIncomingJoint( const TIncomingJointPropType& prop )
{
pushName( "Joint" );
if ( gotoTopName() )
{
PxArticulationJoint* theJoint( prop.get( mObj ) );
readComplexObj( *this, theJoint );
//Add joint to PxCollection, since PxArticulation requires PxArticulationLink and joint.
mCollection.add( *theJoint );
}
popName();
}
private:
RepXVisitorReader<PxArticulationLink>& operator=(const RepXVisitorReader<PxArticulationLink>&);
};
inline void readProperty( RepXVisitorReaderBase<PxArticulation>& inSerializer, PxArticulation* inObj, const PxArticulationLinkCollectionProp& /*inProp*/)
{
PxProfileAllocatorWrapper theWrapper( inSerializer.mAllocator.getAllocator() );
PxCollection& collection( inSerializer.mCollection );
TArticulationLinkLinkMap linkRemapMap( theWrapper );
inSerializer.pushCurrentContext();
if( inSerializer.gotoTopName() )
{
for ( bool links = inSerializer.gotoFirstChild();
links != false;
links = inSerializer.gotoNextSibling() )
{
//Need enough information to create the link...
PxSerialObjectId theParentPtr = 0;
const PxArticulationLink* theParentLink = NULL;
if ( inSerializer.mReader.read( "Parent", theParentPtr ) )
{
const TArticulationLinkLinkMap::Entry* theRemappedParent( linkRemapMap.find( theParentPtr ) );
//If we have a valid at write time, we had better have a valid parent at read time.
PX_ASSERT( theRemappedParent );
theParentLink = theRemappedParent->second;
}
PxArticulationLink* newLink = inObj->createLink( const_cast<PxArticulationLink*>( theParentLink ), PxTransform(PxIdentity) );
PxSerialObjectId theIdPtr = 0;
inSerializer.mReader.read( "Id", theIdPtr );
linkRemapMap.insert( theIdPtr, newLink );
readComplexObj( inSerializer, newLink );
//Add link to PxCollection, since PxArticulation requires PxArticulationLink and joint.
collection.add( *newLink, theIdPtr );
}
}
inSerializer.popCurrentContext();
}
template<>
struct RepXVisitorReader<PxArticulation> : public RepXVisitorReaderBase<PxArticulation>
{
RepXVisitorReader( TReaderNameStack& names, PxProfileArray<PxU32>& contexts, const PxRepXInstantiationArgs& args, XmlReader& reader, PxArticulation* obj
, XmlMemoryAllocator& alloc, PxCollection& collection, bool& ret)
: RepXVisitorReaderBase<PxArticulation>( names, contexts, args, reader, obj, alloc, collection, ret)
{
}
RepXVisitorReader( const RepXVisitorReader<PxArticulation>& other )
: RepXVisitorReaderBase<PxArticulation>( other )
{
}
void handleArticulationLinks( const PxArticulationLinkCollectionProp& inProp )
{
physx::Sn::readProperty( *this, mObj, inProp );
}
};
template<typename TObjType, typename TInfoType>
inline bool readAllProperties( PxRepXInstantiationArgs args, TReaderNameStack& names, PxProfileArray<PxU32>& contexts, XmlReader& reader, TObjType* obj, XmlMemoryAllocator& alloc, PxCollection& collection, TInfoType& info )
{
bool hadError = false;
RepXVisitorReader<TObjType> theReader( names, contexts, args, reader, obj, alloc, collection, hadError);
RepXPropertyFilter<RepXVisitorReader<TObjType> > theOp( theReader );
info.visitBaseProperties( theOp );
info.visitInstanceProperties( theOp );
return !hadError;
}
template<typename TObjType>
inline bool readAllProperties( PxRepXInstantiationArgs args, XmlReader& reader, TObjType* obj, XmlMemoryAllocator& alloc, PxCollection& collection )
{
PxProfileAllocatorWrapper wrapper( alloc.getAllocator() );
TReaderNameStack names( wrapper );
PxProfileArray<PxU32> contexts( wrapper );
PxClassInfoTraits<TObjType> info;
return readAllProperties( args, names, contexts, reader, obj, alloc, collection, info.Info );
}
template<typename TObjType, typename TReaderType, typename TInfoType>
inline void readComplexObj( TReaderType& oldVisitor, TObjType* inObj, TInfoType& info)
{
if(!readAllProperties( oldVisitor.mArgs, oldVisitor.mNames, oldVisitor.mContexts, oldVisitor.mReader, inObj, oldVisitor.mAllocator, oldVisitor.mCollection, info ))
oldVisitor.mHadError = true;
}
template<typename TObjType, typename TReaderType, typename TInfoType>
inline void readComplexObj( TReaderType& oldVisitor, TObjType* inObj, const TInfoType& info)
{
if(!readAllProperties( oldVisitor.mArgs, oldVisitor.mNames, oldVisitor.mContexts, oldVisitor.mReader, inObj, oldVisitor.mAllocator, oldVisitor.mCollection, info ))
oldVisitor.mHadError = true;
}
template<typename TObjType, typename TReaderType>
inline void readComplexObj( TReaderType& oldVisitor, TObjType* inObj, const PxUnknownClassInfo& /*info*/)
{
const char* value = oldVisitor.mReader.getCurrentItemValue();
if ( value && *value )
{
stringToType( value, *inObj );
return;
}
oldVisitor.mHadError = true;
}
template<typename TObjType, typename TReaderType>
inline void readComplexObj( TReaderType& oldVisitor, TObjType* inObj)
{
PxClassInfoTraits<TObjType> info;
if(!readAllProperties( oldVisitor.mArgs, oldVisitor.mNames, oldVisitor.mContexts, oldVisitor.mReader, inObj, oldVisitor.mAllocator, oldVisitor.mCollection, info.Info ))
oldVisitor.mHadError = true;
}
} }
#endif
|