diff --git a/stead/snapshots.lua b/stead/snapshots.lua index 514127e..2723cc8 100644 --- a/stead/snapshots.lua +++ b/stead/snapshots.lua @@ -3,14 +3,6 @@ game._snapshots = {} function make_snapshot(nr) if not tonumber(nr) then nr = 0 end local h = { }; - local function save_object(key, value, h) - savevar(h, value, key, false); - return true; - end - local function save_var(key, value, h) - savevar(h, value, key, isForSave(key, value, _G)) - end - h.txt = '' h.write = function(s, ...) local i @@ -19,10 +11,7 @@ function make_snapshot(nr) end end local old = game._snapshots; game._snapshots = nil - for_each_object(save_object, h); - save_object('game', game, h); - for_everything(save_var, h); - clearvar(_G); + do_savegame(game, h); game._snapshots = old game._snapshots[nr] = h.txt; end @@ -36,10 +25,7 @@ function restore_snapshot(nr) if not tonumber(nr) then nr = 0 end local ss = game._snapshots if not ss[nr] then return nil, true end -- nothing todo - stead:init(); - game.lifes:zap(); - dofile('main.lua'); - game:ini() + gamefile("main.lua"); stead.pclr(); -- if type(init) == 'function' then -- no hooks here!!! -- init(); -- end diff --git a/stead/stead.lua b/stead/stead.lua index eb6e2f6..6c3a14e 100644 --- a/stead/stead.lua +++ b/stead/stead.lua @@ -1634,18 +1634,40 @@ function savevar (h, v, n, need) h:write("\n") end +function gamefile(file) + stead:init(); + game.lifes:zap(); + dofile(file); + game:ini(); + if file == 'main.lua' then -- legacy??? + file = nil + end + game._script = file; + return goto(here()) +end - -function game_save(self, name, file) - local h; +function do_savegame(s, h) local function save_object(key, value, h) savevar(h, value, key, false); return true; end - local function save_var(key, value, h) savevar(h, value, key, isForSave(key, value, _G)) end + if s._script then + h:write(stead.string.format("gamefile(%q)\n", + s._script)) + end + save_object('allocator', allocator, h); -- always first! + for_each_object(save_object, h); + save_object('game', self, h); + for_everything(save_var, h); +-- save_object('_G', _G, h); + clearvar(_G); +end + +function game_save(self, name, file) + local h; if file ~= nil then file:write(name..".pl = '"..deref(self.pl).."'\n"); @@ -1668,12 +1690,7 @@ function game_save(self, name, file) if type(n) == 'string' and n ~= "" then h:write("-- $Name: "..n:gsub("\n","\\n").."$\n"); end - save_object('allocator', allocator, h); -- always first! - for_each_object(save_object, h); - save_object('game', self, h); - for_everything(save_var, h); --- save_object('_G', _G, h); - clearvar(_G); + do_savegame(self, h); h:flush(); h:close(); game.autosave = false; -- we have only one try for autosave