| 74 | } |
| 75 | |
| 76 | SimObject *SimObjectMemento::restore() const |
| 77 | { |
| 78 | // Make sure we have data to restore. |
| 79 | if ( !mState ) |
| 80 | return NULL; |
| 81 | |
| 82 | // TODO: We could potentially make this faster by |
| 83 | // caching the CodeBlock generated from the string |
| 84 | |
| 85 | SimObject* object; |
| 86 | if( !mIsDatablock ) |
| 87 | { |
| 88 | // Set the redefine behavior to automatically giving |
| 89 | // the new objects unique names. This will restore the |
| 90 | // old names if they are still available or give reasonable |
| 91 | // approximations if not. |
| 92 | |
| 93 | const char* oldRedefineBehavior = Con::getVariable( "$Con::redefineBehavior" ); |
| 94 | Con::setVariable( "$Con::redefineBehavior", "renameNew" ); |
| 95 | |
| 96 | // Read the object. |
| 97 | |
| 98 | const UTF8* result = Con::evaluate( mState ); |
| 99 | |
| 100 | // Restore the redefine behavior. |
| 101 | |
| 102 | Con::setVariable( "$Con::redefineBehavior", oldRedefineBehavior ); |
| 103 | |
| 104 | if ( !result || !result[ 0 ] ) |
| 105 | return NULL; |
| 106 | |
| 107 | // Look up the object. |
| 108 | |
| 109 | U32 objectId = dAtoi( result ); |
| 110 | object = Sim::findObject( objectId ); |
| 111 | } |
| 112 | else |
| 113 | { |
| 114 | String objectName = mObjectName; |
| 115 | |
| 116 | // For datablocks, it's getting a little complicated. Datablock definitions cannot be used |
| 117 | // as expressions and thus we can't get to the datablock object we create by using the |
| 118 | // Con::evaluate() return value. Instead, we need to rely on the object name. However, if |
| 119 | // the name is already taken and needs to be changed, we need to manually do that. To complicate |
| 120 | // this further, we cannot rely on automatic renaming since then we don't know by what name |
| 121 | // the newly created object actually goes. So what we do is we alter the source text snapshot |
| 122 | // and substitute a name in case the old object name is actually taken now. |
| 123 | |
| 124 | char* tempBuffer; |
| 125 | if( !Sim::findObject( objectName ) ) |
| 126 | tempBuffer = mState; |
| 127 | else |
| 128 | { |
| 129 | String uniqueName = Sim::getUniqueName( objectName ); |
| 130 | U32 uniqueNameLen = uniqueName.length(); |
| 131 | |
| 132 | char* pLeftParen = dStrchr( mState, '(' ); |
| 133 | if( pLeftParen == NULL ) |
nothing calls this directly
no test coverage detected