steed/stead/dbg.lua

226 lines
5.6 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-06-16 08:05:23 +03:00
function disp_obj()
local v = obj{
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
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
local i,o
local rc=''
for i,o in pairs(w) do
if rc ~='' then rc = rc..'^' end
2010-06-25 12:31:54 +03:00
if isCode(o) then o = stead.string.format("code [[%s]]", stead.functions[o].code); end
2010-06-16 22:31:15 +03:00
rc = stead.cat(rc, stead.par(' ', 'Key:'..tostring(i),
2010-06-15 22:51:46 +03:00
'Val:'..tostring(deref(o))));
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,
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!"'),
obj { nam = 'Back', dsc = '^{Back}', act = code [[ back() ]]},
2010-06-16 08:05:23 +03:00
new [[ disp_obj() ]],
}
2010-06-15 21:58:52 +03:00
}
dump_object = room {
nam = "Dump object",
debug = true,
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())]]},
obj{nam = 'Lifes', dsc = '^{Dump lifes}', act = code[[ return dump_obj(game.lifes)]]},
obj{nam = 'Ways', dsc = '^{Dump ways}', act = code[[ return dump_obj(ways(dbg_here()))]]},
obj{nam = 'Back', dsc = '^{Back}', act = code [[ return back() ]] },
2010-06-16 08:05:23 +03:00
new[[ disp_obj() ]]}
2010-06-14 15:55:29 +03:00
}
2010-06-14 14:41:55 +03:00
choose_location = dlg {
debug = 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)
local k,v
objs(s):zap();
for k,v in pairs(_G) do
if isRoom(v) and not v.debug then
local n = call(v, 'nam');
2010-06-14 14:54:21 +03:00
put(phr(n, true, [[return goto("]]..k..[[")]]), s);
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-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)
local k,v
objs(s):zap();
for k,v in pairs(_G) do
if isObject(v) and not isRoom(v) and not isPlayer(v) and not v.debug and not have(v) and not isStatus(v) then
local n = call(v, 'nam');
2010-06-14 14:54:21 +03:00
put(phr(n, true, k..':enable(); return take("'..k..'")'), s);
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-06-14 14:41:55 +03:00
nam = 'Drop object',
dsc = 'Select object to drop.',
gen = function(s)
local k,v
objs(s):zap();
for k,v in pairs(_G) do
if isObject(v) and not isRoom(v) and not isPlayer(v) and not v.debug and have(v) then
local n = call(v, 'nam');
2010-06-25 12:31:54 +03:00
put (phr(k, true, 'drop("'..k..'","'..deref(dbg_here())..'")'), s)
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-06-15 21:58:52 +03:00
function dbg_exit()
2010-06-25 12:31:54 +03:00
local r
if game.version < "1.2.0" then
r = call(dbg_here(), 'dsc');
end
2010-06-15 21:58:52 +03:00
return par ('^^', back(), r);
end
2010-06-14 14:41:55 +03:00
debug_dlg = dlg {
2010-06-14 14:54:21 +03:00
debug = true,
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-16 08:15:00 +03:00
-- new [[ disp_obj ]]
2010-06-14 14:41:55 +03:00
},
};
debug_tool = menu {
debug = true,
2010-06-14 14:54:21 +03:00
forcedsc = true,
2010-06-14 14:41:55 +03:00
nam = txtb('debug'),
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();
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-06-23 15:09:33 +03:00
input.key = stead.hook(input.key,
2010-06-15 21:58:52 +03:00
function(f, s, down, key, ...)
if not here().debug and down and key == 'f7' then return 'use_debug' end
2010-06-16 08:05:23 +03:00
return f(s, down, key, unpack(arg))
2010-06-15 21:58:52 +03:00
end)
2010-06-14 14:41:55 +03:00
putf('debug_tool', me());