steed/stead/snapshots.lua

68 lines
1.4 KiB
Lua
Raw Normal View History

2010-06-19 13:14:25 +03:00
game._snapshots = {}
function make_snapshot(nr)
if not tonumber(nr) then nr = 0 end
local h = { };
2010-07-06 11:14:42 +03:00
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
2010-06-19 13:14:25 +03:00
h.txt = ''
h.write = function(s, ...)
local i
for i = 1, stead.table.maxn(arg) do
s.txt = s.txt .. tostring(arg[i]);
end
end
local old = game._snapshots; game._snapshots = nil
for_each_object(save_object, h);
save_object('game', game, h);
2010-07-06 11:14:42 +03:00
for_everything(save_var, h);
2010-06-19 13:14:25 +03:00
clearvar(_G);
game._snapshots = old
game._snapshots[nr] = h.txt;
end
2010-07-06 12:18:25 +03:00
function isSnapshot(nr)
if not tonumber(nr) then nr = 0 end
return (game._snapshots[nr] ~= nil)
end
2010-06-19 13:14:25 +03:00
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
2010-06-22 09:13:43 +03:00
stead:init();
2010-06-19 13:14:25 +03:00
game.lifes:zap();
dofile('main.lua');
2010-07-06 11:14:42 +03:00
game:ini()
-- if type(init) == 'function' then -- no hooks here!!!
-- init();
-- end
2010-06-19 13:14:25 +03:00
local f, err = loadstring(ss[nr]);
if not f then return end
local i,r = f();
game._snapshots = ss
if r then
return nil, false
end
2010-07-06 11:14:42 +03:00
i = do_ini(game, true);
2010-06-19 13:14:25 +03:00
RAW_TEXT = true
2010-07-06 11:14:42 +03:00
delete_snapshot(nr);
if cctx() then
pr(i)
end
2010-06-19 13:14:25 +03:00
return i;
end
function delete_snapshot(nr)
if not tonumber(nr) then nr = 0 end
game._snapshots[nr] = nil
end
2010-07-06 11:14:42 +03:00
-- vim:ts=4