simPersistSet.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. #include "console/engineAPI.h"
  26. IMPLEMENT_CONOBJECT( SimPersistSet );
  27. ConsoleDocClass( SimPersistSet,
  28. "@brief A SimSet that can be safely persisted.\n\n"
  29. "Uses SimPersistIDs to reference objects in the set "
  30. "while persisted on disk. This allows the set to resolve "
  31. "its references no matter whether they are loaded before or "
  32. "after the set is created.\n\n"
  33. "Not intended for game development, for editors or internal use only.\n\n "
  34. "@internal");
  35. //-----------------------------------------------------------------------------
  36. SimPersistSet::SimPersistSet()
  37. : mIsResolvingPIDs( false )
  38. {
  39. VECTOR_SET_ASSOCIATION( mUnresolvedPIDs );
  40. }
  41. //-----------------------------------------------------------------------------
  42. bool SimPersistSet::processArguments( S32 argc, ConsoleValue *argv )
  43. {
  44. for( U32 i = 0; i < argc; ++ i )
  45. {
  46. // Parse the UUID.
  47. Torque::UUID uuid;
  48. if( !uuid.fromString( argv[ i ] ) )
  49. {
  50. Con::errorf( "SimPersistSet::processArguments - could not read UUID at index %i: %s", i, (const char*)argv[ i ] );
  51. continue;
  52. }
  53. // Find or create the respective persistent ID.
  54. SimPersistID* pid = SimPersistID::findOrCreate( uuid );
  55. if( pid->getObject() )
  56. {
  57. // There's already an object attached to this PID so just
  58. // add the object to the set.
  59. addObject( pid->getObject() );
  60. }
  61. else
  62. {
  63. // There not yet an object attached to the PID so push it
  64. // onto the stack to resolve it later.
  65. pid->incRefCount();
  66. mUnresolvedPIDs.push_back( pid );
  67. }
  68. }
  69. return true;
  70. }
  71. //-----------------------------------------------------------------------------
  72. void SimPersistSet::write( Stream& stream, U32 tabStop, U32 flags )
  73. {
  74. if( ( flags & SelectedOnly ) && !isSelected() )
  75. return;
  76. // If the selection is transient, we cannot really save it.
  77. // Just invoke the default SimObject::write and return.
  78. if( !getCanSave() )
  79. {
  80. Con::errorf( "SimPersistSet::write - transient set being saved: %d:%s (%s)",
  81. getId(), getClassName(), getName() );
  82. Parent::write( stream, tabStop, flags );
  83. return;
  84. }
  85. // If there are unresolved PIDs, give resolving them one last
  86. // chance before writing out the set.
  87. if( !mUnresolvedPIDs.empty() )
  88. resolvePIDs();
  89. // Write the set out.
  90. stream.writeTabs( tabStop );
  91. StringBuilder buffer;
  92. buffer.format( "new %s(%s", getClassName(), getName() ? getName() : "" );
  93. // Write the persistent IDs of all child objects into the set's
  94. // object constructor so we see them passed back to us through
  95. // processArguments when the object gets read in.
  96. const U32 numChildren = size();
  97. for( U32 i = 0; i < numChildren; ++ i )
  98. {
  99. SimObject* child = at( i );
  100. SimPersistID* pid = child->getPersistentId();
  101. AssertWarn( pid != NULL, "SimPersistSet::write - object without pid in persistent selection!" );
  102. if( !pid )
  103. continue;
  104. buffer.append( ',' );
  105. buffer.append( '"' );
  106. buffer.append( pid->getUUID().toString() );
  107. buffer.append( '"' );
  108. }
  109. buffer.append( ") {\r\n" );
  110. stream.write( buffer.length(), buffer.data() );
  111. // Write our object fields.
  112. writeFields( stream, tabStop + 1 );
  113. // Close our object definition.
  114. stream.writeTabs( tabStop );
  115. stream.write( 4, "};\r\n" );
  116. }
  117. //-----------------------------------------------------------------------------
  118. void SimPersistSet::resolvePIDs()
  119. {
  120. if( mIsResolvingPIDs )
  121. return;
  122. lock();
  123. mIsResolvingPIDs = true;
  124. for( U32 i = 0; i < mUnresolvedPIDs.size(); ++ i )
  125. if( mUnresolvedPIDs[ i ]->getObject() != NULL )
  126. {
  127. addObject( mUnresolvedPIDs[ i ]->getObject() );
  128. mUnresolvedPIDs[i]->decRefCount();
  129. mUnresolvedPIDs.erase( i );
  130. -- i;
  131. }
  132. mIsResolvingPIDs = false;
  133. unlock();
  134. }
  135. //-----------------------------------------------------------------------------
  136. void SimPersistSet::addObject( SimObject* object )
  137. {
  138. // If this set isn't transient, make sure the object has a valid
  139. // persistent ID.
  140. if( getCanSave() )
  141. object->getOrCreatePersistentId();
  142. Parent::addObject( object );
  143. }
  144. //=============================================================================
  145. // Console Methods.
  146. //=============================================================================
  147. // MARK: ---- Console Methods ----
  148. //-----------------------------------------------------------------------------
  149. DefineEngineMethod( SimPersistSet, resolvePersistentIds, void, (), , "() - Try to bind unresolved persistent IDs in the set." )
  150. {
  151. object->resolvePIDs();
  152. }