From 5acb88335085097b9f75908fc0756c5f38f20564 Mon Sep 17 00:00:00 2001 From: Pawel Kolodziejski Date: Sun, 3 Aug 2008 13:02:45 +0000 Subject: [PATCH] fixed save/restore lua state --- engine/lua/lrestore.cpp | 32 ++++++++++++++++++++------------ engine/lua/lsave.cpp | 23 +++++++++++++++-------- engine/lua/lstate.cpp | 1 - engine/main.cpp | 1 + engine/savegame.cpp | 7 +++++++ engine/savegame.h | 1 + 6 files changed, 44 insertions(+), 21 deletions(-) diff --git a/engine/lua/lrestore.cpp b/engine/lua/lrestore.cpp index 61ce49f8b0a..12d66734c16 100644 --- a/engine/lua/lrestore.cpp +++ b/engine/lua/lrestore.cpp @@ -1,3 +1,9 @@ +#include "common/endian.h" +#include "common/debug.h" + +#include "engine/engine.h" +#include "engine/savegame.h" + #include "engine/lua/ltask.h" #include "engine/lua/lauxlib.h" #include "engine/lua/lmem.h" @@ -9,9 +15,6 @@ #include "engine/lua/lstring.h" #include "engine/lua/lua.h" -#include "common/endian.h" -#include "common/debug.h" - RestoreCallback restoreCallbackPtr = NULL; static void restoreObjectValue(TObject *object, RestoreSint32 restoreSint32, RestoreUint32 restoreUint32) { @@ -251,32 +254,35 @@ void lua_Restore(RestoreStream restoreStream, RestoreSint32 restoreSint32, Resto maxStringsLength = restoreSint32(); char *tempStringBuffer = (char *)luaM_malloc(maxStringsLength); + //printf("1: %d\n", g_engine->_savedState->getBufferPos()); + int32 i; for (i = 0; i < arrayStringsCount; i++) { arraysObj->idObj.low = restoreSint32(); arraysObj->idObj.hi = restoreSint32(); int32 constIndex = restoreSint32(); - lua_Type tag = (lua_Type)restoreSint32(); - PointerId ptr; - ptr.low = restoreUint32(); - ptr.hi = restoreUint32(); TaggedString *tempString; if (constIndex != -1) { + TObject obj; + restoreObjectValue(&obj, restoreSint32, restoreUint32); int32 length = restoreSint32(); restoreStream(tempStringBuffer, length); tempString = luaS_newlstr(tempStringBuffer, length); - tempString->u.s.globalval.ttype = tag; - tempString->u.s.globalval.value.ts = (TaggedString *)makePointerFromId(ptr); + tempString->u.s.globalval = obj; } else { + PointerId ptr; + lua_Type tag = (lua_Type)restoreSint32(); + ptr.low = restoreUint32(); + ptr.hi = restoreUint32(); if (tag == 0) tempString = luaS_createudata((void *)makePointerFromId(ptr), LUA_ANYTAG); else tempString = luaS_createudata((void *)makePointerFromId(ptr), tag); if (restoreCallbackPtr) { - PointerId ptr = makeIdFromPointer(tempString->u.s.globalval.value.ts); - ptr = restoreCallbackPtr(tempString->u.s.globalval.ttype, ptr, restoreSint32); - tempString->u.s.globalval.value.ts = (TaggedString *)makePointerFromId(ptr); + PointerId ptr = makeIdFromPointer(tempString->u.d.v); + ptr = restoreCallbackPtr(tempString->u.d.tag, ptr, restoreSint32); + tempString->u.d.v = makePointerFromId(ptr); } } tempString->constindex = constIndex; @@ -285,6 +291,8 @@ void lua_Restore(RestoreStream restoreStream, RestoreSint32 restoreSint32, Resto } luaM_free(tempStringBuffer); + //printf("2: %d\n", g_engine->_savedState->getBufferPos()); + int32 l; Closure *tempClosure; arraysObj = (ArrayIDObj *)luaM_malloc(sizeof(ArrayIDObj) * arrayClosuresCount); diff --git a/engine/lua/lsave.cpp b/engine/lua/lsave.cpp index 811c94679b0..8e2502d1f33 100644 --- a/engine/lua/lsave.cpp +++ b/engine/lua/lsave.cpp @@ -1,3 +1,9 @@ +#include "engine/savegame.h" +#include "engine/engine.h" + +#include "common/endian.h" +#include "common/debug.h" + #include "engine/lua/ltask.h" #include "engine/lua/lauxlib.h" #include "engine/lua/lmem.h" @@ -9,9 +15,6 @@ #include "engine/lua/lstring.h" #include "engine/lua/lua.h" -#include "common/endian.h" -#include "common/debug.h" - PointerId makeIdFromPointer(void *ptr) { PointerId pointer; @@ -218,6 +221,8 @@ void lua_Save(SaveStream saveStream, SaveSint32 saveSint32, SaveUint32 saveUint3 // save maximum length for string saveSint32(maxStringLength); + //printf("1: %d\n", g_engine->_savedState->getBufferPos()); + // save hash tables for strings and user data TaggedString *tempString; for (i = 0; i < NUM_HASHS; i++) { @@ -234,16 +239,18 @@ void lua_Save(SaveStream saveStream, SaveSint32 saveSint32, SaveUint32 saveUint3 saveStream(tempString->str, tempString->u.s.len); } else { if (saveCallbackPtr) { - PointerId ptr = makeIdFromPointer(tempString->u.s.globalval.value.ts); - ptr = saveCallbackPtr(tempString->u.s.globalval.ttype, ptr, saveSint32); - tempString->u.s.globalval.value.ts = (TaggedString *)makePointerFromId(ptr); + PointerId ptr = makeIdFromPointer(tempString->u.d.v); + ptr = saveCallbackPtr(tempString->u.d.tag, ptr, saveSint32); + tempString->u.d.v = makePointerFromId(ptr); } - saveObjectValue(&tempString->u.s.globalval, saveSint32, saveUint32); + saveObjectValue((TObject *)&tempString->u.d, saveSint32, saveUint32); } } } } - + + //printf("2: %d\n", g_engine->_savedState->getBufferPos()); + Closure *tempClosure = (Closure *)L->rootcl.next; while (tempClosure) { saveUint32(makeIdFromPointer(tempClosure).low); diff --git a/engine/lua/lstate.cpp b/engine/lua/lstate.cpp index 10f1bea5879..e36bdfee3ef 100644 --- a/engine/lua/lstate.cpp +++ b/engine/lua/lstate.cpp @@ -94,7 +94,6 @@ void lua_close() { luaM_free(L->Mbuffer); luaM_free(L); L = NULL; - lua_iolibclose(); #ifdef LUA_DEBUG printf("total de blocos: %ld\n", numblocks); printf("total de memoria: %ld\n", totalmem); diff --git a/engine/main.cpp b/engine/main.cpp index 1e1f55829e7..15e4164906b 100644 --- a/engine/main.cpp +++ b/engine/main.cpp @@ -166,6 +166,7 @@ void quit() { if (g_lua_initialized) { lua_removelibslists(); lua_close(); + lua_iolibclose(); g_lua_initialized = false; } if (g_registry) { diff --git a/engine/savegame.cpp b/engine/savegame.cpp index 9150ad0b002..7061dbc5a8d 100644 --- a/engine/savegame.cpp +++ b/engine/savegame.cpp @@ -108,6 +108,13 @@ void SaveGame::endSection() { _currentSection = 0; } +uint32 SaveGame::getBufferPos() { + if (_saving) + return _sectionSize; + else + return _sectionPtr; +} + void SaveGame::read(void *data, int size) { if (_saving) error("SaveGame::readBlock called when storing a savegame!"); diff --git a/engine/savegame.h b/engine/savegame.h index 7f9ea8dd440..0b85ac2e06f 100644 --- a/engine/savegame.h +++ b/engine/savegame.h @@ -51,6 +51,7 @@ public: uint32 beginSection(uint32 sectionTag); void endSection(); + uint32 getBufferPos(); void read(void *data, int size); void write(const void *data, int size); uint32 readLEUint32();