steed/stead/dbg.lua

361 lines
8.3 KiB
Lua
Raw Normal View History

2010-06-14 14:41:55 +03:00
-- add this: reuire "dbg"
-- in your project
-- for debug tools
2010-06-14 15:55:29 +03:00
require "input"
2010-06-14 16:19:37 +03:00
2010-11-04 13:38:25 +02:00
function _xref_escape(n)
local delim = ':'
if stead.api_version >= "1.2.2" then
delim = stead.delim;
end
if xact then
2010-11-04 14:46:25 +02:00
n = n:gsub("\\?[\\"..delim.."]", { [delim] = "\\"..delim } )
2010-11-04 13:38:25 +02:00
end
return n
end
2010-07-12 22:17:11 +03:00
function ordered_n(t)
local ordered = {};
local i,v, max;
max = 0;
for i,v in pairs(t) do
2010-07-15 16:57:03 +03:00
local o = { k = i; v = v };
stead.table.insert(ordered, o);
2010-07-12 22:17:11 +03:00
max = max + 1;
end
stead.table.sort(ordered, function(a, b)
2010-07-15 16:57:03 +03:00
if isObject(a.v) and not isObject(b.v) then
2010-07-12 22:17:11 +03:00
return true
end
2010-07-15 16:57:03 +03:00
if not isObject(a.v) and isObject(b.v) then
2010-07-12 22:17:11 +03:00
return false
end
2010-07-15 16:57:03 +03:00
if isObject(a.v) and isObject(b.v) then
local n = call(a.v, 'nam');
local m = call(b.v, 'nam');
2010-12-04 13:34:54 +02:00
if type(n) ~= 'string' and type(m) ~= 'string' then
return false
end
2010-07-14 16:56:12 +03:00
if type(n) ~= 'string' then
2010-07-14 16:55:22 +03:00
return true
end
2010-07-14 16:56:12 +03:00
if type(m) ~= 'string' then
2010-07-14 16:55:22 +03:00
return false
end
2010-07-12 22:17:11 +03:00
return n < m;
end
return false
end)
ordered.i = 1;
ordered.max = max;
return ordered;
end
function snext(t, k)
local v
if not k then
k = ordered_n(t);
end
if k.i > k.max then
return nil
end
v = k[k.i]
k.i = k.i + 1
2010-07-15 16:57:03 +03:00
return k, v.v, v.k;
2010-07-12 22:17:11 +03:00
end
function spairs(s, var)
return snext, s, nil;
end
2010-06-16 08:05:23 +03:00
function disp_obj()
2010-07-20 10:24:48 +03:00
local v = obj {
2010-06-16 08:05:23 +03:00
nam = 'disp',
2010-06-25 12:31:54 +03:00
act = true,
2010-06-16 08:05:23 +03:00
dsc = function(s)
local r = s._txt
s._txt = nil;
return r
2010-07-20 10:24:48 +03:00
end;
save = function(self, name, h, need)
if need then
h:write(stead.string.format("%s = disp_obj();\n", name));
end
savemembers(h, self, name, false);
2010-06-16 08:05:23 +03:00
end
}
return v;
end
2010-06-15 21:58:52 +03:00
dump_obj = function(w)
w = ref(w)
if type(w) ~= 'table' then
2010-06-16 08:05:23 +03:00
seen('disp')._txt = '^^No such object.';
return true
2010-06-15 21:58:52 +03:00
end
2010-12-04 13:34:54 +02:00
local i,o,n
2010-06-15 21:58:52 +03:00
local rc=''
for i,o in pairs(w) do
2010-07-12 22:17:11 +03:00
local t = stead.tostring(o);
if t == i then
t = tostring(o);
end
if t then
if rc ~='' then rc = rc..'^' end
local n = '';
2010-07-20 08:18:14 +03:00
if type(o) ~= 'function' and isObject(ref(o)) then
2010-07-12 22:17:11 +03:00
n = call(ref(o), 'nam');
2010-12-04 13:34:54 +02:00
if type(n) ~= 'string' then n = '' else n = ' : '..n; end
2010-07-12 22:17:11 +03:00
end
rc = stead.cat(rc, stead.par(' ', tostring(i)..' : '..t..n));
end
end
seen('disp')._txt = stead.cat('^^', rc)
return true;
end
dump_globals = function()
local i,o
local rc=''
if type(variables) ~= 'table' then
return
end
for i,o in ipairs(variables) do
local v = _G[o];
local t = stead.tostring(v);
if t then
if rc ~='' then rc = rc..'^' end
rc = stead.cat(rc, stead.par(' ', tostring(o)..' : '..t));
end
2010-06-15 21:58:52 +03:00
end
2010-06-16 22:31:15 +03:00
seen('disp')._txt = stead.cat('^^', rc)
2010-06-16 08:05:23 +03:00
return true;
2010-06-15 21:58:52 +03:00
end
2010-06-25 12:31:54 +03:00
dbg_here = function()
return debug_tool._here
end
2010-06-14 16:19:37 +03:00
list_objects = function()
local i,o
2010-06-25 12:31:54 +03:00
local rc = stead.par(' ', 'Room:'..tostring(deref(dbg_here())),
'Nam:'..tostring(call(dbg_here(),'nam')));
for i,o in opairs(objs(dbg_here())) do
2010-06-15 21:58:52 +03:00
rc = rc..'^';
o = ref(o)
2010-06-16 22:31:15 +03:00
rc = stead.cat(rc, stead.par(' ', 'Id:'..tostring(o.id),
2010-06-15 21:58:52 +03:00
'Obj:'..tostring(deref(o)),
'Nam:'..tostring(call(o, 'nam')),
'Disabled:'..tostring(isDisabled(o))));
end
2010-06-16 08:15:00 +03:00
-- seen('disp')._txt = rc
2010-06-15 21:58:52 +03:00
return rc
end
list_inv = function()
local i,o
local rc=''
for i,o in opairs(inv()) do
if rc ~='' then rc = rc..'^' end
2010-06-14 16:19:37 +03:00
o = ref(o)
2010-06-16 22:31:15 +03:00
rc = stead.cat(rc, stead.par(' ', 'Id:'..tostring(o.id), 'Obj:'..tostring(deref(o)),
2010-06-15 21:58:52 +03:00
'Nam:'..tostring(call(o, 'nam')),
'Disabled:'..tostring(isDisabled(o)),
'Taken:'..tostring(taken(o))));
2010-06-14 16:19:37 +03:00
end
2010-06-15 21:58:52 +03:00
if rc == '' then return end
2010-06-16 08:15:00 +03:00
-- seen('disp')._txt = rc
2010-06-14 16:19:37 +03:00
return rc
end
2010-06-14 15:55:29 +03:00
execute_cmd = room {
nam = "Execute Lua code",
debug = true,
2010-07-22 14:45:59 +03:00
system_type = true,
2010-06-14 15:55:29 +03:00
forcedsc = true,
dsc = "Enter Lua code here to exec.",
inp_enter = function(s)
if type(s.obj[1]._txt) == 'string' then
local f = loadstring(s.obj[1]._txt);
if f then
2010-06-16 22:31:15 +03:00
seen('disp')._txt = stead.cat('^^', f());
2010-06-16 08:05:23 +03:00
return true
2010-06-14 15:55:29 +03:00
end
2010-06-16 08:05:23 +03:00
seen('disp')._txt = "^^Error in exec.";
return true
2010-06-14 15:55:29 +03:00
end
return back();
end,
2010-06-25 12:31:54 +03:00
obj = { inp('inp', '{Enter cmd}: ', 'return "Hello World!"'),
2010-07-20 10:24:48 +03:00
obj { nam = 'Back', dsc = '^{Back}', act = code [[ back() ]] },
disp_obj(),
2010-06-16 08:05:23 +03:00
}
2010-06-15 21:58:52 +03:00
}
dump_object = room {
nam = "Dump object",
debug = true,
2010-07-22 14:45:59 +03:00
system_type = true,
2010-06-15 21:58:52 +03:00
forcedsc = true,
dsc = "Enter object name here to dump.",
inp_enter = function(s)
2010-06-25 12:31:54 +03:00
local w = s.obj[1]._txt
if type(w) == 'string' then
if not ref(w) then w = objs(dbg_here()):srch(w); end
return dump_obj(w);
2010-06-15 21:58:52 +03:00
end
return back();
end,
2010-06-25 12:31:54 +03:00
obj = { inp('inp', '{Enter object}: ', 'main'),
obj{nam = 'Here', dsc = '^{Dump here}', act = code[[ return dump_obj(dbg_here())]]},
obj{nam = 'Player',dsc = '^{Dump player}', act = code[[ return dump_obj(me())]]},
2010-07-26 22:37:40 +03:00
obj{nam = 'Lifes', dsc = '^{Dump lifes}', act = code[[ return dump_obj(debug_tool.lifes)]]},
2010-06-25 12:31:54 +03:00
obj{nam = 'Ways', dsc = '^{Dump ways}', act = code[[ return dump_obj(ways(dbg_here()))]]},
2010-07-12 22:17:11 +03:00
obj{nam = 'Globals', dsc = '^{Dump globals}', act = code [[return dump_globals()]] },
2010-06-25 12:31:54 +03:00
obj{nam = 'Back', dsc = '^{Back}', act = code [[ return back() ]] },
2010-07-20 10:24:48 +03:00
disp_obj() }
2010-06-14 15:55:29 +03:00
}
2010-06-14 14:41:55 +03:00
choose_location = dlg {
debug = true,
2010-07-22 13:39:48 +03:00
system_type = true,
2010-06-14 14:54:21 +03:00
forcedsc = true,
2010-06-14 14:41:55 +03:00
nam = 'Go to',
dsc = 'Select location.',
gen = function(s)
2010-07-15 16:57:03 +03:00
local k,v,kk
2010-06-14 14:41:55 +03:00
objs(s):zap();
2010-07-15 16:57:03 +03:00
for k,v,kk in spairs(_G) do
2010-06-14 14:41:55 +03:00
if isRoom(v) and not v.debug then
2010-07-12 22:17:11 +03:00
local n = tostring(call(v, 'nam'));
2010-07-15 16:57:03 +03:00
local o = kk;
2010-07-12 22:17:11 +03:00
if type(o) == 'string' then
n = n..' : '..o;
2010-11-04 13:38:25 +02:00
n = _xref_escape(n);
2010-07-23 13:06:25 +03:00
put(phr(n, true, [[timer:set(debug_tool._timer); game.lifes:cat(debug_tool.lifes); return goto(]]..o..[[)]]), s);
2010-07-12 22:17:11 +03:00
end
2010-06-14 14:41:55 +03:00
end
end
2010-06-14 15:55:29 +03:00
put (phr('Back',true, 'return back()'), s)
2010-06-14 14:41:55 +03:00
end
}
choose_object = dlg {
debug = true,
2010-07-22 13:39:48 +03:00
system_type = true,
2010-06-14 14:54:21 +03:00
forcedsc = true,
2010-06-14 14:41:55 +03:00
nam = 'Get object',
dsc = 'Select object to get.',
gen = function(s)
2010-07-15 16:57:03 +03:00
local k,v,kk
2010-06-14 14:41:55 +03:00
objs(s):zap();
2010-07-15 16:57:03 +03:00
for k,v,kk in spairs(_G) do
2010-07-12 22:17:11 +03:00
if isObject(v) and not isPhrase(v) and not isRoom(v) and not isPlayer(v) and not v.debug and not have(v) and not isStatus(v) then
local n = tostring(call(v, 'nam'));
2010-07-15 16:57:03 +03:00
local o = kk;
2010-07-12 22:17:11 +03:00
if type(o) == 'string' then
n = n..' : '..o;
2010-11-04 13:38:25 +02:00
n = _xref_escape(n);
2010-07-13 07:37:05 +03:00
put(phr(n, true, o..':enable(); return take('..o..')'), s);
2010-07-12 22:17:11 +03:00
end
2010-06-14 14:41:55 +03:00
end
end
2010-06-14 15:55:29 +03:00
put (phr('Back',true, 'return back()'), s)
2010-06-14 14:41:55 +03:00
end
}
drop_object = dlg {
debug = true,
2010-06-14 14:54:21 +03:00
forcedsc = true,
2010-07-22 13:39:48 +03:00
system_type = true,
2010-06-14 14:41:55 +03:00
nam = 'Drop object',
dsc = 'Select object to drop.',
gen = function(s)
local k,v
objs(s):zap();
2010-07-12 22:17:11 +03:00
for k,v in ipairs(inv()) do
v = ref(v);
if not v.debug then
local n = tostring(call(v, 'nam'));
local o = deref(v);
if type(o) == 'string' then
n = n..' : '..o;
2010-11-04 13:38:25 +02:00
n = _xref_escape(n);
2010-07-13 07:37:05 +03:00
put (phr(n, true, o..':enable(); drop('..o..','..deref(dbg_here())..')'), s)
2010-07-12 22:17:11 +03:00
end
2010-06-14 14:41:55 +03:00
end
end
2010-06-14 15:55:29 +03:00
put (phr('Back', true, 'return back()'), s)
2010-06-14 14:41:55 +03:00
end
}
2010-07-20 10:24:48 +03:00
2010-06-15 21:58:52 +03:00
function dbg_exit()
2010-06-25 12:31:54 +03:00
local r
if stead.api_version < "1.2.0" then
2010-06-25 12:31:54 +03:00
r = call(dbg_here(), 'dsc');
end
2010-07-20 10:24:48 +03:00
game.lifes:cat(debug_tool.lifes);
2010-07-23 13:06:25 +03:00
timer:set(debug_tool._timer);
2010-06-15 21:58:52 +03:00
return par ('^^', back(), r);
end
2010-07-20 10:24:48 +03:00
2010-06-14 14:41:55 +03:00
debug_dlg = dlg {
2010-06-14 14:54:21 +03:00
debug = true,
2010-07-22 13:39:48 +03:00
system_type = true,
2010-06-14 14:54:21 +03:00
forcedsc = true,
2010-06-14 14:41:55 +03:00
nam = 'Debug Tool',
dsc = 'Select tool.',
obj = {
2010-06-14 15:55:29 +03:00
phr('Go to location...', true, [[pon(); choose_location:gen(); return goto('choose_location')]]),
phr('Get object...', true, [[pon(); choose_object:gen(); return goto('choose_object')]]),
phr('Put object...', true, [[pon(); drop_object:gen(); return goto('drop_object')]]),
2010-06-14 16:19:37 +03:00
phr('Current scene...', true, [[pon(); return list_objects();]]),
2010-06-15 21:58:52 +03:00
phr('Inventory...', true, [[pon(); return list_inv();]]),
2010-06-15 22:51:46 +03:00
phr('Dump object...', true, [[pon(); return goto(dump_object);]]),
phr('Exec Lua string...', true, [[pon(); return goto('execute_cmd')]]),
2010-06-15 21:58:52 +03:00
phr('Exit',true , [[pon(); return dbg_exit()]]),
2010-06-14 14:41:55 +03:00
},
};
debug_tool = menu {
debug = true,
2010-07-22 13:39:48 +03:00
system_type = true,
2010-06-14 14:54:21 +03:00
forcedsc = true,
2010-06-14 14:41:55 +03:00
nam = txtb('debug'),
2010-07-20 10:24:48 +03:00
lifes = list {},
2010-06-14 14:41:55 +03:00
inv = function(s)
2010-06-25 12:31:54 +03:00
if here().debug then
return nil, true --nothing todo
end
debug_dlg.__from__ = here();
2010-07-23 13:06:25 +03:00
s._timer = timer:get();
timer:stop();
2010-07-20 10:24:48 +03:00
s.lifes:zap();
s.lifes:cat(game.lifes);
game.lifes:zap();
2010-06-25 12:31:54 +03:00
s._here = here();
2010-06-14 14:41:55 +03:00
me().where = 'debug_dlg'; -- force to go
2010-06-25 12:31:54 +03:00
return goto(self.where);
2010-06-15 21:58:52 +03:00
end,
2010-06-14 14:41:55 +03:00
};
2010-06-15 21:58:52 +03:00
2010-06-23 15:09:33 +03:00
game.action = stead.hook(game.action,
2010-06-15 21:58:52 +03:00
function (f, s, cmd, ...)
if cmd == 'use_debug' then
return debug_tool:inv()
end
2010-06-16 08:05:23 +03:00
return f(s, cmd, unpack(arg))
2010-06-15 21:58:52 +03:00
end)
2010-07-22 13:39:48 +03:00
stead.module_init(function()
input.key = stead.hook(input.key,
function(f, s, down, key, ...)
if not here().debug and down and key == 'f7' then return 'use_debug' end
return f(s, down, key, unpack(arg))
end)
2010-07-23 09:55:31 +03:00
putf('debug_tool', me());
2010-06-15 21:58:52 +03:00
end)
2010-07-06 13:15:27 +03:00
-- vim:ts=4