simObjectMemento.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 "platform/platform.h"
  23. #include "console/simObjectMemento.h"
  24. #include "console/simObject.h"
  25. #include "console/simDatablock.h"
  26. #include "core/stream/memStream.h"
  27. SimObjectMemento::SimObjectMemento()
  28. : mState( NULL ),
  29. mIsDatablock( false )
  30. {
  31. }
  32. SimObjectMemento::~SimObjectMemento()
  33. {
  34. dFree( mState );
  35. }
  36. void SimObjectMemento::save( SimObject *object )
  37. {
  38. // Cleanup any existing state data.
  39. dFree( mState );
  40. mObjectName = String::EmptyString;
  41. // Use a stream to save the state.
  42. MemStream stream( 256 );
  43. U32 writeFlags = 0;
  44. SimDataBlock* db = dynamic_cast<SimDataBlock*>(object);
  45. if( !db )
  46. stream.write( sizeof( "return " ) - 1, "return " );
  47. else
  48. {
  49. mIsDatablock = true;
  50. // Cull the datablock name from the output so that
  51. // we can easily replace it in case the datablock's name
  52. // is already taken when we call restore(). We can't use the same
  53. // setup as with non-datablock classes as the return semantics
  54. // are not the same.
  55. writeFlags |= SimObject::NoName;
  56. }
  57. object->write( stream, 0, writeFlags );
  58. stream.write( (UTF8)0 );
  59. // Steal the data away from the stream.
  60. mState = (UTF8*)stream.takeBuffer();
  61. mObjectName = object->getName();
  62. }
  63. SimObject *SimObjectMemento::restore() const
  64. {
  65. // Make sure we have data to restore.
  66. if ( !mState )
  67. return NULL;
  68. // TODO: We could potentially make this faster by
  69. // caching the CodeBlock generated from the string
  70. SimObject* object;
  71. if( !mIsDatablock )
  72. {
  73. // Set the redefine behavior to automatically giving
  74. // the new objects unique names. This will restore the
  75. // old names if they are still available or give reasonable
  76. // approximations if not.
  77. const char* oldRedefineBehavior = Con::getVariable( "$Con::redefineBehavior" );
  78. Con::setVariable( "$Con::redefineBehavior", "renameNew" );
  79. // Read the object.
  80. const UTF8* result = Con::evaluate( mState );
  81. // Restore the redefine behavior.
  82. Con::setVariable( "$Con::redefineBehavior", oldRedefineBehavior );
  83. if ( !result || !result[ 0 ] )
  84. return NULL;
  85. // Look up the object.
  86. U32 objectId = dAtoi( result );
  87. object = Sim::findObject( objectId );
  88. }
  89. else
  90. {
  91. String objectName = mObjectName;
  92. // For datablocks, it's getting a little complicated. Datablock definitions cannot be used
  93. // as expressions and thus we can't get to the datablock object we create by using the
  94. // Con::evaluate() return value. Instead, we need to rely on the object name. However, if
  95. // the name is already taken and needs to be changed, we need to manually do that. To complicate
  96. // this further, we cannot rely on automatic renaming since then we don't know by what name
  97. // the newly created object actually goes. So what we do is we alter the source text snapshot
  98. // and substitute a name in case the old object name is actually taken now.
  99. char* tempBuffer;
  100. if( !Sim::findObject( objectName ) )
  101. tempBuffer = mState;
  102. else
  103. {
  104. String uniqueName = Sim::getUniqueName( objectName );
  105. U32 uniqueNameLen = uniqueName.length();
  106. char* pLeftParen = dStrchr( mState, '(' );
  107. if( pLeftParen == NULL )
  108. return NULL;
  109. U32 numCharsToLeftParen = pLeftParen - mState;
  110. dsize_t tempBufferLen = dStrlen(mState) + uniqueNameLen + 1;
  111. tempBuffer = ( char* ) dMalloc( tempBufferLen );
  112. dMemcpy( tempBuffer, mState, numCharsToLeftParen );
  113. dMemcpy( &tempBuffer[ numCharsToLeftParen ], uniqueName, uniqueNameLen );
  114. dStrcpy( &tempBuffer[ numCharsToLeftParen + uniqueNameLen ], &mState[ numCharsToLeftParen ], tempBufferLen - numCharsToLeftParen - uniqueNameLen );
  115. }
  116. Con::evaluate( tempBuffer );
  117. if( tempBuffer != mState )
  118. dFree( tempBuffer );
  119. if( objectName == String::EmptyString )
  120. return NULL;
  121. object = Sim::findObject( objectName );
  122. }
  123. return object;
  124. }