simObjectMemento.cpp 5.3 KB

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