From 0d4350185723b89b723f0214d3992ca1ef620086 Mon Sep 17 00:00:00 2001 From: grilledham Date: Wed, 6 Feb 2019 21:20:08 +0000 Subject: [PATCH] added _G.dump and dump commands --- features/gui/debug/command.lua | 127 +++++++++++++++++++++++++++++++++ features/gui/debug/model.lua | 1 + 2 files changed, 128 insertions(+) diff --git a/features/gui/debug/command.lua b/features/gui/debug/command.lua index 24a39949..1d4a74d1 100644 --- a/features/gui/debug/command.lua +++ b/features/gui/debug/command.lua @@ -1,6 +1,12 @@ local DebugView = require 'features.gui.debug.main_view' +local Model = require 'features.gui.debug.model' local Command = require 'utils.command' +local loadstring = loadstring +local pcall = pcall +local dump = Model.dump +local log = log + Command.add( 'debug', { @@ -11,3 +17,124 @@ Command.add( DebugView.open_dubug(player) end ) + +Command.add( + 'dump', + { + arguments = {'str'}, + capture_excess_arguments = true, + allowed_by_server = true, + debug_only = true, + description = 'dumps value to player.print' + }, + function(args, player) + local p + if player then + p = player.print + else + p = print + end + + local func, err = loadstring('return ' .. args.str) + + if not func then + p(err) + return + end + + local suc, value = pcall(func) + + if not suc then + if value then + local i = value:find('\n') + if i then + p(value:sub(1, i)) + return + end + + i = value:find('%s') + if i then + p(value:sub(i + 1)) + end + end + + return + end + + p(dump(value)) + end +) + +Command.add( + 'dump-log', + { + arguments = {'str'}, + capture_excess_arguments = true, + allowed_by_server = true, + debug_only = true, + description = 'dumps value to log' + }, + function(args, player) + local p + if player then + p = player.print + else + p = print + end + + local func, err = loadstring('return ' .. args.str) + + if not func then + p(err) + return + end + + local suc, value = pcall(func) + + if not suc then + p(value) + return + end + + log(dump(value)) + end +) + +Command.add( + 'dump-file', + { + arguments = {'str'}, + capture_excess_arguments = true, + allowed_by_server = true, + debug_only = true, + description = 'dumps value to dump.lua' + }, + function(args, player) + local p + local player_index + if player then + p = player.print + player_index = player.index + else + p = print + player_index = 0 + end + + local func, err = loadstring('return ' .. args.str) + + if not func then + p(err) + return + end + + local suc, value = pcall(func) + + if not suc then + p(value) + return + end + + value = dump(value) + game.write_file('dump.lua', value, false, player_index) + end +) diff --git a/features/gui/debug/model.lua b/features/gui/debug/model.lua index fb636718..90be49de 100644 --- a/features/gui/debug/model.lua +++ b/features/gui/debug/model.lua @@ -87,6 +87,7 @@ function Public.dump(data) return inspect(data, inspect_options) end local dump = Public.dump +_G.dump = dump function Public.dump_ignore_builder(ignore) local function process(item)