Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds in simple generic persist interface. #1897

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions data/core/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -689,10 +689,7 @@ function core.init()
EXEDIR = common.normalize_volume(EXEDIR)
end

core.window = renwindow._restore()
if core.window == nil then
core.window = renwindow.create("")
end
core.window = system.persist("WINDOW") or renwindow.create("")
do
local session = load_session()
if session.window_mode == "normal" then
Expand All @@ -708,19 +705,21 @@ function core.init()
local project_dir = core.recent_projects[1] or "."
local project_dir_explicit = false
local files = {}
for i = 2, #ARGS do
local arg_filename = strip_trailing_slash(ARGS[i])
local info = system.get_file_info(arg_filename) or {}
if info.type == "dir" then
project_dir = arg_filename
project_dir_explicit = true
else
-- on macOS we can get an argument like "-psn_0_52353" that we just ignore.
if not ARGS[i]:match("^-psn") then
local file_abs = core.project_absolute_path(arg_filename)
if file_abs then
table.insert(files, file_abs)
project_dir = file_abs:match("^(.+)[/\\].+$")
if not system.persist("RESTARTED") then
for i = 2, #ARGS do
local arg_filename = strip_trailing_slash(ARGS[i])
local info = system.get_file_info(arg_filename) or {}
if info.type == "dir" then
project_dir = arg_filename
project_dir_explicit = true
else
-- on macOS we can get an argument like "-psn_0_52353" that we just ignore.
if not ARGS[i]:match("^-psn") then
local file_abs = core.project_absolute_path(arg_filename)
if file_abs then
table.insert(files, file_abs)
project_dir = file_abs:match("^(.+)[/\\].+$")
end
end
end
end
Expand Down Expand Up @@ -928,7 +927,8 @@ end
function core.restart()
quit_with_function(function()
core.restart_request = true
core.window:_persist()
system.persist("RESTARTED", true)
system.persist("WINDOW", core.window)
end)
end

Expand Down Expand Up @@ -1361,6 +1361,7 @@ function core.step()
did_keymap = res or did_keymap
end
core.redraw = true
if core.restart_request or core.quit_request then return false end
end

local width, height = core.window:get_size()
Expand Down
29 changes: 2 additions & 27 deletions src/api/renwindow.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#include "lua.h"
#include <SDL.h>

static RenWindow *persistant_window = NULL;

static void init_window_icon(SDL_Window *window) {
#if !defined(_WIN32) && !defined(__APPLE__)
#include "../resources/icons/icon.inl"
Expand Down Expand Up @@ -59,12 +57,11 @@ static int f_renwin_create(lua_State *L) {

static int f_renwin_gc(lua_State *L) {
RenWindow *window_renderer = *(RenWindow**)luaL_checkudata(L, 1, API_TYPE_RENWINDOW);
if (window_renderer != persistant_window)
ren_destroy(window_renderer);

ren_destroy(window_renderer);
return 0;
}


static int f_renwin_get_size(lua_State *L) {
RenWindow *window_renderer = *(RenWindow**)luaL_checkudata(L, 1, API_TYPE_RENWINDOW);
int w, h;
Expand All @@ -74,33 +71,11 @@ static int f_renwin_get_size(lua_State *L) {
return 2;
}

static int f_renwin_persist(lua_State *L) {
RenWindow *window_renderer = *(RenWindow**)luaL_checkudata(L, 1, API_TYPE_RENWINDOW);

persistant_window = window_renderer;
return 0;
}

static int f_renwin_restore(lua_State *L) {
if (!persistant_window) {
lua_pushnil(L);
}
else {
RenWindow **window_renderer = (RenWindow**)lua_newuserdata(L, sizeof(RenWindow*));
luaL_setmetatable(L, API_TYPE_RENWINDOW);

*window_renderer = persistant_window;
}

return 1;
}

static const luaL_Reg renwindow_lib[] = {
{ "create", f_renwin_create },
{ "__gc", f_renwin_gc },
{ "get_size", f_renwin_get_size },
{ "_persist", f_renwin_persist },
{ "_restore", f_renwin_restore },
{NULL, NULL}
};

Expand Down
48 changes: 48 additions & 0 deletions src/api/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,53 @@ static int f_setenv(lua_State* L) {
}


static void transfer_lua(lua_State* from, lua_State* to) {
switch (lua_type(from, -1)) {
case LUA_TLIGHTUSERDATA: lua_pushlightuserdata(to, lua_touserdata(from, -1)); break;
case LUA_TNIL: lua_pushnil(to); break;
case LUA_TBOOLEAN: lua_pushboolean(to, lua_toboolean(from, -1)); break;
case LUA_TSTRING: {
size_t len;
const char* buffer = lua_tolstring(from, -1, &len);
lua_pushlstring(to, buffer, len);
} break;
case LUA_TNUMBER: {
if (lua_isinteger(from, -1))
lua_pushinteger(to, lua_tointeger(from, -1));
else
lua_pushnumber(to, lua_tonumber(from, -1));
} break;
case LUA_TUSERDATA: { // Strip userdata of metadata, and store it.
void* ud = lua_newuserdata(to, lua_rawlen(from, -1));
memcpy(ud, lua_touserdata(from, -1), lua_rawlen(from, -1));
luaL_getmetafield(from, -1, "__name");
luaL_newmetatable(to, lua_tostring(from, -1));
lua_setmetatable(to, -2);
lua_pop(from, 1);
lua_pushnil(from);
lua_setmetatable(from, -2);
} break;
}
}


static int f_persist(lua_State* L) {
static lua_State* pL = NULL;
if (!pL)
pL = luaL_newstate();
lua_pushstring(pL, luaL_checkstring(L, 1));
if (lua_gettop(L) == 2) { // Save
transfer_lua(L, pL);
lua_rawset(pL, LUA_REGISTRYINDEX);
return 0;
} else { // Load
lua_rawget(pL, LUA_REGISTRYINDEX);
transfer_lua(pL, L);
return 1;
}
}


static const luaL_Reg lib[] = {
{ "poll_event", f_poll_event },
{ "wait_event", f_wait_event },
Expand Down Expand Up @@ -1285,6 +1332,7 @@ static const luaL_Reg lib[] = {
{ "get_fs_type", f_get_fs_type },
{ "text_input", f_text_input },
{ "setenv", f_setenv },
{ "persist", f_persist },
{ NULL, NULL }
};

Expand Down
1 change: 0 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ int main(int argc, char **argv) {
luaL_openlibs(L);
api_load_libs(L);


lua_newtable(L);
for (int i = 0; i < argc; i++) {
lua_pushstring(L, argv[i]);
Expand Down