From 69383384bf8b81a65f939e8739a5c15504acd984 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Wed, 16 Feb 2005 02:42:05 +0000 Subject: [PATCH] add LuaData::Serialize, LuaData::LoadFromString; fix stack overflow --- stepmania/src/LuaReference.cpp | 22 +++++++++++++++++----- stepmania/src/LuaReference.h | 4 ++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/stepmania/src/LuaReference.cpp b/stepmania/src/LuaReference.cpp index 9f21c80b5d..067ef0b066 100644 --- a/stepmania/src/LuaReference.cpp +++ b/stepmania/src/LuaReference.cpp @@ -125,7 +125,7 @@ void LuaExpression::Register() this->SetFromStack(); } -void LuaData::BeforeReset() +CString LuaData::Serialize() const { /* Call Serialize(t), where t is our referenced object. */ lua_pushstring( LUA->L, "Serialize" ); @@ -144,23 +144,35 @@ void LuaData::BeforeReset() if( pString == NULL ) FAIL_M( "Serialize() didn't return a string" ); - m_sSerializedData = pString; + CString sRet = pString; + lua_pop( LUA->L, 1 ); + + return sRet; } -void LuaData::Register() +void LuaData::LoadFromString( const CString &s ) { /* Restore the serialized data by evaluating the serialized data. */ CString sError; - if( !LUA->RunScript( m_sSerializedData, "serialization", sError, 1 ) ) + if( !LUA->RunScript( s, "serialization", sError, 1 ) ) { /* Serialize() should never return an invalid script. Drop the failed * script into the log (it may be too big to pass to FAIL_M) and fail. */ - LOG->Warn( "Unserialization of \"%s\" failed: %s", m_sSerializedData.c_str(), sError.c_str() ); + LOG->Warn( "Unserialization of \"%s\" failed: %s", s.c_str(), sError.c_str() ); FAIL_M( "Unserialization failed" ); } this->SetFromStack(); +} +void LuaData::BeforeReset() +{ + m_sSerializedData = Serialize(); +} + +void LuaData::Register() +{ + LoadFromString( m_sSerializedData ); m_sSerializedData.clear(); } diff --git a/stepmania/src/LuaReference.h b/stepmania/src/LuaReference.h index 03bf8047ce..4b79481ac1 100644 --- a/stepmania/src/LuaReference.h +++ b/stepmania/src/LuaReference.h @@ -65,6 +65,10 @@ private: * The object will be saved and restored across Lua resets. */ class LuaData: public LuaReference { +public: + virtual CString Serialize() const; + virtual void LoadFromString( const CString &s ); + protected: virtual void BeforeReset(); virtual void Register();