save logic changed, MAY BE BROKEN. fixups

This commit is contained in:
p.kosyh 2010-06-20 09:19:33 +00:00
parent 390ab5aebd
commit 95b192d51e
2 changed files with 72 additions and 36 deletions

View file

@ -72,7 +72,7 @@ function callpop()
stead.cctx[stead.call_top] = nil; stead.cctx[stead.call_top] = nil;
stead.call_top = stead.call_top - 1; stead.call_top = stead.call_top - 1;
if stead.call_top < 0 then if stead.call_top < 0 then
error "callpush/callpop mismatch" error ("callpush/callpop mismatch")
end end
end end
@ -363,7 +363,8 @@ end
function obj_save(self, name, h, need) function obj_save(self, name, h, need)
local dsc; local dsc;
if need then if need then
h:write(name.." = obj {nam = '"..tostring(self.nam).."'}\n"); error ("Object "..name.." can not be saved!");
return
end end
savemembers(h, self, name, need); savemembers(h, self, name, need);
end end
@ -389,7 +390,7 @@ end
function obj(v) function obj(v)
if v.nam == nil then if v.nam == nil then
error ("No object name in constructor."); error ("No object name in constructor.", 2);
end end
v.object_type = true; v.object_type = true;
@ -465,6 +466,7 @@ function list_check(self)
for i,v,ii in opairs(self) do for i,v,ii in opairs(self) do
local o = ref(v); local o = ref(v);
if not o then -- isObject(o) then -- compat if not o then -- isObject(o) then -- compat
error ("No object: "..tostring(v))
return false return false
end end
if deref(v) then if deref(v) then
@ -494,7 +496,7 @@ function list_add(self, name, pos)
if self:look(nam) then if self:look(nam) then
return nil return nil
end end
self.__modifyed__ = true; self.__modified__ = true;
if tonumber(pos) then if tonumber(pos) then
stead.table.insert(self, tonumber(pos), nam); stead.table.insert(self, tonumber(pos), nam);
self[tonumber(pos)] = nam; -- for spare lists self[tonumber(pos)] = nam; -- for spare lists
@ -511,7 +513,7 @@ function list_set(self, name, pos)
return nil return nil
end end
nam = deref(name); nam = deref(name);
self.__modifyed__ = true; self.__modified__ = true;
self[i] = nam; -- for spare lists self[i] = nam; -- for spare lists
return true return true
end end
@ -527,7 +529,7 @@ function list_find(self, name)
end end
function list_save(self, name, h, need) function list_save(self, name, h, need)
if self.__modifyed__ then if self.__modifyed__ or self.__modified__ then -- compat
h:write(name.." = list({});\n"); h:write(name.." = list({});\n");
need = true; need = true;
end end
@ -582,7 +584,7 @@ function list_zap(self)
for n,o,ii in opairs(self) do for n,o,ii in opairs(self) do
self[ii] = nil; self[ii] = nil;
end end
self.__modifyed__ = true self.__modified__ = true
return self return self
end end
@ -605,7 +607,7 @@ function list_del(self, name)
if n == nil then if n == nil then
return nil; return nil;
end end
self.__modifyed__ = true self.__modified__ = true
v = stead.table.remove(self, n); v = stead.table.remove(self, n);
if not v then if not v then
v = self[n]; v = self[n];
@ -637,7 +639,7 @@ end
function call(v, n, ...) function call(v, n, ...)
if type(v) ~= 'table' then if type(v) ~= 'table' then
error ("Call on non table object:"..n); error ("Call on non table object:"..n, 2);
end end
if v[n] == nil then if v[n] == nil then
return nil,nil; return nil,nil;
@ -658,12 +660,12 @@ function call(v, n, ...)
if type(v[n]) == 'boolean' then if type(v[n]) == 'boolean' then
return v[n] return v[n]
end end
error ("Method not string nor function:"..tostring(n)); error ("Method not string nor function:"..tostring(n), 2);
end end
function call_bool(v, n, ...) function call_bool(v, n, ...)
if type(v) ~= 'table' then if type(v) ~= 'table' then
error ("Call bool on non table object:"..n); error ("Call bool on non table object:"..n, 2);
end end
if v[n] == nil then if v[n] == nil then
@ -726,14 +728,15 @@ end
function room_save(self, name, h, need) function room_save(self, name, h, need)
local dsc; local dsc;
if need then if need then
h:write(name.." = room {nam = '"..tostring(self.nam).."'}\n"); error ("Room "..name.." can not be saved!");
return
end end
savemembers(h, self, name, need); savemembers(h, self, name, need);
end end
function room(v) --constructor function room(v) --constructor
if v.nam == nil then if v.nam == nil then
error "No room name in constructor."; error ("No room name in constructor.", 2);
end end
if v.scene == nil then if v.scene == nil then
v.scene = room_scene; v.scene = room_scene;
@ -1101,14 +1104,14 @@ function go(self, where, back)
return nil,ret(false) return nil,ret(false)
end end
if not isRoom(ref(where)) then if not isRoom(ref(where)) then
error ("Trying to go nowhere: "..where); error ("Trying to go nowhere: "..where, 2);
end end
if not isRoom(ref(self.where)) then if not isRoom(ref(self.where)) then
error ("Trying to go from nowhere: "..self.where); error ("Trying to go from nowhere: "..self.where, 2);
end end
if stead.in_entered_call or stead.in_onexit_call then if stead.in_entered_call or stead.in_onexit_call then
error ("Do not use goto from onexit/entered action! Use exit/enter action instead:" .. self.where); error ("Do not use goto from onexit/entered action! Use exit/enter action instead:" .. self.where, 2);
end end
local v, r; local v, r;
@ -1188,7 +1191,7 @@ end
function player(v) function player(v)
if v.nam == nil then if v.nam == nil then
error "No player name in constructor."; error ("No player name in constructor.", 2);
end end
if v.where == nil then if v.where == nil then
v.where = 'main'; v.where = 'main';
@ -1317,6 +1320,7 @@ function do_ini(self)
for_each(game, "game", check_list, isList) for_each(game, "game", check_list, isList)
stead.fixups:apply();
for_each_object(call_ini); for_each_object(call_ini);
me():tag(); me():tag();
@ -1342,7 +1346,7 @@ end
function game(v) function game(v)
if v.nam == nil then if v.nam == nil then
error "No game name in constructor."; error ("No game name in constructor.", 2);
end end
if v.pl == nil then if v.pl == nil then
v.pl = 'player'; v.pl = 'player';
@ -1450,9 +1454,11 @@ end
function savevar (h, v, n, need) function savevar (h, v, n, need)
local r,f local r,f
if v == nil or type(v)=="userdata" or if v == nil or type(v)=="userdata" or
type(v)=="function" then type(v)=="function" then
-- if need then
-- error ("Variable "..n.." can not be saved!");
-- end
return return
end end
@ -1469,6 +1475,14 @@ function savevar (h, v, n, need)
end end
if type(v) == "table" then if type(v) == "table" then
if type(v.key_name) == 'string' and v.key_name ~= n and
'_G["'..v.key_name..'"]' ~= n then -- just xref
if need then
local w = stead.string.format("%s = ref(%q)", n, v.key_name);
h:write(stead.string.format("%s = stead.fixups:add(%q)\n", n, w));
end
return
end
if v.__visited__ ~= nil then if v.__visited__ ~= nil then
return return
end end
@ -1880,24 +1894,24 @@ allocator = obj {
new = function(s, n) new = function(s, n)
local v = ref(n); local v = ref(n);
if type(v) ~= 'table' or type(n) ~= 'string' then if type(v) ~= 'table' or type(n) ~= 'string' then
error "Error in new."; error ("Error in new.", 2);
end end
v.save = allocator_save; v.save = allocator_save;
v.constructor = n; v.constructor = n;
stead.table.insert(s.objects, v); stead.table.insert(s.objects, v);
v.key_name = 'allocator.objects['..stead.table.maxn(s.objects)..']'; v.key_name = 'allocator["objects"]['..stead.table.maxn(s.objects)..']';
return v return v
end, end,
objects = { objects = {
save = function(self, name, h, need) save = function(self, name, h, need)
savemembers(h, self, name, true); savemembers(h, self, name, true);
end end,
}, },
}; };
function new(str) function new(str)
if type(str) ~= 'string' then if type(str) ~= 'string' then
error("Non string constructor in new."); error("Non string constructor in new.", 2);
end end
return allocator:new(str); return allocator:new(str);
end end
@ -1906,6 +1920,25 @@ function delete(v)
allocator:delete(v); allocator:delete(v);
end end
stead.fixups = {
fix = {},
apply = function(s)
local i,v
for i,v in ipairs(s.fix) do
local f = loadstring(v);
if type(f) ~= 'function' then
error ("Error while fixup:" .. tostring(v))
end
f();
end
s.fix = {}
end,
add = function(s, str)
stead.table.insert(s.fix, str)
return nil
end
}
timer = obj { -- timer calls stead.timer callback timer = obj { -- timer calls stead.timer callback
nam = 'timer', nam = 'timer',
ini = function(s) ini = function(s)
@ -1983,7 +2016,7 @@ end
function vobj(key, name, dsc, w) function vobj(key, name, dsc, w)
if not tonumber(key) then if not tonumber(key) then
error ("vobj key must be number!"); error ("vobj key must be number!", 2);
end end
return obj{ key = key, nam = name, dsc = dsc, where = deref(w), act = vobj_act, used = vobj_used, save = vobj_save, obj = list({}) }; return obj{ key = key, nam = name, dsc = dsc, where = deref(w), act = vobj_act, used = vobj_used, save = vobj_save, obj = list({}) };
end end
@ -2051,7 +2084,7 @@ end
function taketo(obj, wh, pos) function taketo(obj, wh, pos)
local o = remove(obj, wh); local o = remove(obj, wh);
if not isObject(o) then if not isObject(o) then
error "Trying to take wrong object."; error ("Trying to take wrong object.", 2);
end end
inv():add(obj, pos); inv():add(obj, pos);
o._taken = true o._taken = true
@ -2070,7 +2103,7 @@ function putto(obj, w, pos)
local wh local wh
local o = ref(obj); local o = ref(obj);
if not isObject(o) then if not isObject(o) then
error "Trying to put wrong object."; error ("Trying to put wrong object.", 2);
end end
if not w then if not w then
wh = deref(here()); wh = deref(here());
@ -2098,7 +2131,7 @@ end
function drop(obj, w) function drop(obj, w)
local o = put(obj, w); local o = put(obj, w);
if not isObject(o) then if not isObject(o) then
error "Trying to drop wrong object:"; error ("Trying to drop wrong object.", 2);
end end
inv():del(obj); inv():del(obj);
o._taken = false o._taken = false
@ -2108,7 +2141,7 @@ end
function dropf(obj) function dropf(obj)
local o = putf(obj); local o = putf(obj);
if not isObject(o) then if not isObject(o) then
error "Trying to dropf wrong object:"; error ("Trying to dropf wrong object.", 2);
end end
inv():del(obj); inv():del(obj);
o._taken = false o._taken = false
@ -2276,7 +2309,7 @@ end
function change_pl(p) function change_pl(p)
local o = ref(p); local o = ref(p);
if type(deref(p)) ~= 'string' or not o then if type(deref(p)) ~= 'string' or not o then
error "Wrong player name in change_pl..."; error ("Wrong player name in change_pl...", 2);
end end
game.pl = deref(p); game.pl = deref(p);
return goto(o.where); return goto(o.where);
@ -2319,6 +2352,9 @@ function enable_all(o)
end end
function isForSave(k, v, s) -- k - key, v - value, s -- parent table function isForSave(k, v, s) -- k - key, v - value, s -- parent table
if type(v) == 'function' or type(v) == 'userdata' then
return false
end
return stead.string.find(k, '_') == 1 or stead.string.match(k,'^%u') return stead.string.find(k, '_') == 1 or stead.string.match(k,'^%u')
end end
@ -2347,7 +2383,7 @@ function check_version(v)
return return
end end
if stead.version < v then if stead.version < v then
error ([[The game requires instead engine of version ]] ..v.. [[ or higher. http://instead.googlecode.com]]) error ([[The game requires instead engine of version ]] ..v.. [[ or higher. http://instead.googlecode.com]], 2)
end end
game.version = v game.version = v
if v >= "1.2.0" then if v >= "1.2.0" then

View file

@ -1,9 +1,9 @@
isForSave = hook(isForSave, isForSave = hook(isForSave,
function (f, k, v, s, ...) -- k - key, v - value, s -- parent table function (f, k, v, s, ...) -- k - key, v - value, s -- parent table
local i,v local i,o
if type(s.var) == 'table' then if type(s.var) == 'table' then
for i,v in ipairs(s.var) do for i,o in ipairs(s.var) do
if v == k then if o == k then
return true return true
end end
end end