foxygit / fake-08-xbox360 Log in
commit 33fcfef00dd8556ef95132522ad09a8d3f7b79af
Author:     Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Sat Aug 6 16:11:07 2022 -0600
Commit:     Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Sat Aug 6 16:11:07 2022 -0600

    WIP- save states sort of working
---
 platform/libretro/libretro.cpp | 64 ++++++++++++++++++++++++++----------------
 source/p8GlobalLuaFunctions.h  | 20 +------------
 source/vm.cpp                  | 29 +++++++++----------
 3 files changed, 55 insertions(+), 58 deletions(-)

diff --git a/platform/libretro/libretro.cpp b/platform/libretro/libretro.cpp
index ef40013..ab86e7b 100644
--- a/platform/libretro/libretro.cpp
+++ b/platform/libretro/libretro.cpp
@@ -84,7 +84,6 @@ EXPORT void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb;

 EXPORT void retro_init()
 {
-    printf("********** retro init\n");
     //called once. do setup (create host and vm?)
     _host = new Host();

@@ -118,7 +117,6 @@ EXPORT void retro_init()

 EXPORT void retro_deinit()
 {
-    printf("**********r retro deinit\n");
     //delete things created in init
     _vm->CloseCart();
     _host->oneTimeCleanup();
@@ -290,52 +288,70 @@ EXPORT void retro_run()
     frame++;
 }

+//lua memory is 2 MB in size
+//https://www.lexaloffle.com/dl/docs/pico-8_manual.html
+//section 6.7: Memory
+//we can probably make this smaller since we ignore unmodified globals...
+#define LUASTATEBUFFSIZE 1024*1024*2

 EXPORT size_t retro_serialize_size()
 {
-    //lua memory is 2 MB in size
-    //https://www.lexaloffle.com/dl/docs/pico-8_manual.html
-    //section 6.7: Memory
-    printf("********** getting serialize size\n");
-    return sizeof(PicoRam) + sizeof(audioState_t) + 1024*1024*2;
+    return sizeof(PicoRam) + sizeof(audioState_t) + LUASTATEBUFFSIZE;
 }

 EXPORT bool retro_serialize(void *data, size_t size)
 {
-    printf("********** retro_serialize\n");
     const int expectedSize = retro_serialize_size();
     if (size > expectedSize) {
-        printf("********** size > expected size\n");
         size = expectedSize;
     }
     else if (size < expectedSize) {
-        printf("********** size < expected size\n");
         return false;
     }

+    char luaStateBuffer[LUASTATEBUFFSIZE];
+    memset(luaStateBuffer, 0, LUASTATEBUFFSIZE);

-    saveState_t* saveState = (saveState_t*)data;
+    size_t offset = 0;
+    size_t luaStateSize = _vm->serializeLuaState(luaStateBuffer);

-    memcpy(&saveState->memory, _memory->data, sizeof(PicoRam));
-    memcpy(&saveState->audioState, _audio->getAudioState(), sizeof(audioState_t));
-    std::string luaState = _vm->serializeLuaState();
-    printf("serialized luaState: %s\n", luaState.c_str());
-    memcpy(&saveState->luaStateStr, luaState.c_str(), luaState.length());
+    memcpy(((char*)data + offset), &luaStateSize, sizeof(size_t));
+    offset += sizeof(size_t);

+    memcpy(((char*)data + offset), luaStateBuffer, luaStateSize);
+    offset += luaStateSize;
+
+    memcpy(((char*)data + offset), _memory->data, sizeof(PicoRam));
+    offset += sizeof(PicoRam);
+
+    memcpy(((char*)data + offset), _audio->getAudioState(), sizeof(audioState_t));
+    offset += sizeof(audioState_t);

     return true;
 }

 EXPORT bool retro_unserialize(const void *data, size_t size)
 {
-    printf("********** retro_unserialize\n");
-    saveState_t* saveState = (saveState_t*)data;
-
-    memcpy(_memory->data, &saveState->memory, sizeof(PicoRam));
-    memcpy(_audio->getAudioState(), &saveState->audioState, sizeof(audioState_t));
-    printf("serialized luaState: %s\n",  saveState->luaStateStr);
-    std::string luaStateStr = saveState->luaStateStr;
-    _vm->deserializeLuaState(luaStateStr);
+    char luaStateBuffer[LUASTATEBUFFSIZE];
+    memset(luaStateBuffer, 0, LUASTATEBUFFSIZE);
+
+    size_t offset = 0;
+    size_t luaStateSize;
+    memcpy(&luaStateSize, ((char*)data + offset),  sizeof(size_t));
+    offset += sizeof(size_t);
+
+    memcpy(luaStateBuffer, ((char*)data + offset), luaStateSize);
+    offset += luaStateSize;
+    _vm->deserializeLuaState(luaStateBuffer, luaStateSize);
+
+    memcpy(_memory->data, ((char*)data + offset), sizeof(PicoRam));
+    offset += sizeof(PicoRam);
+
+    memcpy(_audio->getAudioState(), ((char*)data + offset), sizeof(audioState_t));
+    offset += sizeof(audioState_t);
+
+
+

     return true;
 }
diff --git a/source/p8GlobalLuaFunctions.h b/source/p8GlobalLuaFunctions.h
index 54d7e5c..d3793da 100644
--- a/source/p8GlobalLuaFunctions.h
+++ b/source/p8GlobalLuaFunctions.h
@@ -305,35 +305,17 @@ end

 eris.persist_all = function()
   local new_symbols = {}
-  printh("persist_all")
   for k,v in pairs(_G) do
     if eris.original_G[k] != v then
-       printh("adding key: " .. k .. ": " .. tostr(v))
        new_symbols[k] = v
     end
   end

-  local result = eris.persist(eris.perm, new_symbols)
-  printh("result: ")
-  for i=1, #result do
-    printh(tostr(i).."  "..tostr(ord(result, i)))
-  end
-  printh("end of result")
-
-  return result;
+  return eris.persist(eris.perm, new_symbols)
 end

 eris.restore_all = function(persisted)
-  printh("persisted: ")
-  for i=1, #persisted do
-    printh(tostr(i).."  "..tostr(ord(persisted, i)))
-  end
-  printh("end of persisted")
-  printh("restore_all- iterating symbols first")
   local new_symbols = eris.unpersist(eris.unperm, persisted)
-  for k,v in pairs(new_symbols) do
-    printh("restoring key: " .. k .. ": " .. tostr(v))
-  end
   for k,v in pairs(new_symbols) do
     _G[k] = v
   end
diff --git a/source/vm.cpp b/source/vm.cpp
index be2bcb1..b90072d 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -274,6 +274,20 @@ bool Vm::loadCart(Cart* cart) {
         return false;
     }

+    // Push the eris.init_persist_all function on the top of the lua stack (or nil if it doesn't exist)
+    // we call this function to establish the default global state of things not to save in the save state
+    // needs to be called after globals are loaded but before the cart is run, or _init is called
+    lua_getglobal(_luaState, "eris");
+	lua_getfield(_luaState, -1, "init_persist_all");
+
+    if (lua_pcall(_luaState, 0, 0, 0)){
+        Logger_Write("Error setting up lua persistence: %s\n", lua_tostring(_luaState, -1));
+        lua_pop(_luaState, 1);
+        return false;
+    }
+
+    //pop the eris.init_persist_all fuction off the stack now that we're done with it
+    lua_pop(_luaState, 1);

     int loadedCart = luaL_loadstring(_luaState, cart->LuaString.c_str());
     if (loadedCart != LUA_OK) {
@@ -333,22 +347,7 @@ bool Vm::loadCart(Cart* cart) {
     //pop the _init fuction off the stack now that we're done with it
     lua_pop(_luaState, 0);

-    // Push the eris.init_persist_all function on the top of the lua stack (or nil if it doesn't exist)
-    printf("********trying to call eris.init_persist_all\n");
-    lua_getglobal(_luaState, "eris");
-	lua_getfield(_luaState, -1, "init_persist_all");

-    if (lua_pcall(_luaState, 0, 0, 0)){
-        printf("********error trying to call eris.init_persist_all\n");
-        printf("Error setting up lua persistence: %s\n", lua_tostring(_luaState, -1));
-        Logger_Write("Error setting up lua persistence: %s\n", lua_tostring(_luaState, -1));
-        lua_pop(_luaState, 1);
-        return false;
-    }
-
-    //pop the eris.init_persist_all fuction off the stack now that we're done with it
-    lua_pop(_luaState, 1);
-    printf("********succeeded calling eris.init_persist_all\n");

     //check for update, mark correct target fps
     lua_getglobal(_luaState, "_update60");