[go: nahoru, domu]

Skip to content

Commit

Permalink
WIP- save states sort of working
Browse files Browse the repository at this point in the history
  • Loading branch information
jtothebell committed Aug 6, 2022
1 parent 5646c31 commit 33fcfef
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 58 deletions.
64 changes: 40 additions & 24 deletions platform/libretro/libretro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
Expand Down
20 changes: 1 addition & 19 deletions source/p8GlobalLuaFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 14 additions & 15 deletions source/vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 33fcfef

Please sign in to comment.