simPersistSet.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "console/simPersistSet.h"
  23. #include "console/simPersistID.h"
  24. #include "console/consoleTypes.h"
  25. IMPLEMENT_CONOBJECT( SimPersistSet );
  26. ConsoleDocClass( SimPersistSet,
  27. "@brief A SimSet that can be safely persisted.\n\n"
  28. "Uses SimPersistIDs to reference objects in the set "
  29. "while persisted on disk. This allows the set to resolve "
  30. "its references no matter whether they are loaded before or "
  31. "after the set is created.\n\n"
  32. "Not intended for game development, for editors or internal use only.\n\n "
  33. "@internal");
  34. //-----------------------------------------------------------------------------
  35. SimPersistSet::SimPersistSet()
  36. : mIsResolvingPIDs( false )
  37. {
  38. VECTOR_SET_ASSOCIATION( mUnresolvedPIDs );
  39. }
  40. //-----------------------------------------------------------------------------
  41. bool SimPersistSet::processArguments( S32 argc, const char** argv )
  42. {
  43. for( U32 i = 0; i < argc; ++ i )
  44. {
  45. // Parse the UUID.
  46. Torque::UUID uuid;
  47. if( !uuid.fromString( argv[ i ] ) )
  48. {
  49. Con::errorf( "SimPersistSet::processArguments - could not read UUID at index %i: %s", i, argv[ i ] );
  50. continue;
  51. }
  52. // Find or create the respective persistent ID.
  53. SimPersistID* pid = SimPersistID::findOrCreate( uuid );
  54. if( pid->getObject() )
  55. {
  56. // There's already an object attached to this PID so just
  57. // add the object to the set.
  58. addObject( pid->getObject() );
  59. }
  60. else
  61. {
  62. // There not yet an object attached to the PID so push it
  63. // onto the stack to resolve it later.
  64. pid->incRefCount();
  65. mUnresolvedPIDs.push_back( pid );
  66. }
  67. }
  68. return true;
  69. }
  70. //-----------------------------------------------------------------------------
  71. void SimPersistSet::write( Stream& stream, U32 tabStop, U32 flags )
  72. {
  73. if( ( flags & SelectedOnly ) && !isSelected() )
  74. return;
  75. // If the selection is transient, we cannot really save it.
  76. // Just invoke the default SimObject::write and return.
  77. if( !getCanSave() )
  78. {
  79. Con::errorf( "SimPersistSet::write - transient set being saved: %d:%s (%s)",
  80. getId(), getClassName(), getName() );
  81. Parent::write( stream, tabStop, flags );
  82. return;
  83. }
  84. // If there are unresolved PIDs, give resolving them one last
  85. // chance before writing out the set.
  86. if( !mUnresolvedPIDs.empty() )
  87. resolvePIDs();
  88. // Write the set out.
  89. stream.writeTabs( tabStop );
  90. StringBuilder buffer;
  91. buffer.format( "new %s(%s", getClassName(), getName() ? getName() : "" );
  92. // Write the persistent IDs of all child objects into the set's
  93. // object constructor so we see them passed back to us through
  94. // processArguments when the object gets read in.
  95. const U32 numChildren = size();
  96. for( U32 i = 0; i < numChildren; ++ i )
  97. {
  98. SimObject* child = at( i );
  99. SimPersistID* pid = child->getPersistentId();
  100. AssertWarn( pid != NULL, "SimPersistSet::write - object without pid in persistent selection!" );
  101. if( !pid )
  102. continue;
  103. buffer.append( ',' );
  104. buffer.append( '"' );
  105. buffer.append( pid->getUUID().toString() );
  106. buffer.append( '"' );
  107. }
  108. buffer.append( ") {\r\n" );
  109. stream.write( buffer.length(), buffer.data() );
  110. // Write our object fields.
  111. writeFields( stream, tabStop + 1 );
  112. // Close our object definition.
  113. stream.writeTabs( tabStop );
  114. stream.write( 4, "};\r\n" );
  115. }
  116. //-----------------------------------------------------------------------------
  117. void SimPersistSet::resolvePIDs()
  118. {
  119. if( mIsResolvingPIDs )
  120. return;
  121. lock();
  122. mIsResolvingPIDs = true;
  123. for( U32 i = 0; i < mUnresolvedPIDs.size(); ++ i )
  124. if( mUnresolvedPIDs[ i ]->getObject() != NULL )
  125. {
  126. addObject( mUnresolvedPIDs[ i ]->getObject() );
  127. mUnresolvedPIDs[i]->decRefCount();
  128. mUnresolvedPIDs.erase( i );
  129. -- i;
  130. }
  131. mIsResolvingPIDs = false;
  132. unlock();
  133. }
  134. //-----------------------------------------------------------------------------
  135. void SimPersistSet::addObject( SimObject* object )
  136. {
  137. // If this set isn't transient, make sure the object has a valid
  138. // persistent ID.
  139. if( getCanSave() )
  140. object->getOrCreatePersistentId();
  141. Parent::addObject( object );
  142. }
  143. //=============================================================================
  144. // Console Methods.
  145. //=============================================================================
  146. // MARK: ---- Console Methods ----
  147. //-----------------------------------------------------------------------------
  148. ConsoleMethod( SimPersistSet, resolvePersistentIds, void, 2, 2, "() - Try to bind unresolved persistent IDs in the set." )
  149. {
  150. object->resolvePIDs();
  151. }