diff --git a/LuaMenu/widgets/dbg_command_capture.lua b/LuaMenu/widgets/dbg_command_capture.lua index c70f87a91..23c533c90 100644 --- a/LuaMenu/widgets/dbg_command_capture.lua +++ b/LuaMenu/widgets/dbg_command_capture.lua @@ -6,12 +6,16 @@ function widget:GetInfo() date = "", license = "", layer = 99999, - enabled = true + enabled = false, } end VFS.Include("libs/json.lua") +-------------------------------------------------------------------------------- +-- Local Variables +-------------------------------------------------------------------------------- + local Configuration local lobby @@ -19,45 +23,11 @@ local captured = {} local captureFile local enabled = false -function widget:Initialize() - lobby = WG.LibLobby.lobby - WG.Delay(function() - Configuration = WG.Chobby.Configuration - SetState(Configuration.captureServerCommands) - - Configuration:AddListener("OnConfigurationChange", - function(listener, key, value) - if key == "captureServerCommands" then - SetState(value) - end - end - ) - end, 0.1) -end - -function widget:Shutdown() - Disable() -end - -function SetState(value) - if enabled == value then - return - end - enabled = value - - if enabled then - Spring.Echo("===Command capture initialized===") - -- TODO: For some reason we can't measure both of these commands - -- If we try, log information will be done for _OnCommandReceived twice (some lua inheritance magic again?) - CaptureFunction(lobby, "CommandReceived", "Interface:CommandReceived") - -- CaptureFunction(lobby, "_OnCommandReceived", "Interface:_OnCommandReceived") - else - Spring.Echo("===Command capture disabled===") - Disable() - end -end +-------------------------------------------------------------------------------- +-- Local Functions +-------------------------------------------------------------------------------- -function CaptureFunction(obj, fname, registerName) +local function CaptureFunction(obj, fname, registerName) Spring.Echo("Capturing function [" .. tostring(fname) .. "] as " .. tostring(registerName)) if captureFile == nil then captureFile = io.open("commands.log", "a") @@ -87,7 +57,7 @@ function CaptureFunction(obj, fname, registerName) } end -function Disable() +local function Disable() if captureFile then captureFile:close() captureFile = nil @@ -97,3 +67,45 @@ function Disable() p.obj[p.fname] = p.orig end end + +local function SetState(value) + if enabled == value then + return + end + enabled = value + + if enabled then + Spring.Echo("===Command capture initialized===") + -- TODO: For some reason we can't measure both of these commands + -- If we try, log information will be done for _OnCommandReceived twice (some lua inheritance magic again?) + CaptureFunction(lobby, "CommandReceived", "Interface:CommandReceived") + -- CaptureFunction(lobby, "_OnCommandReceived", "Interface:_OnCommandReceived") + else + Spring.Echo("===Command capture disabled===") + Disable() + end +end + +-------------------------------------------------------------------------------- +-- Widget Interface +-------------------------------------------------------------------------------- + +function widget:Initialize() + lobby = WG.LibLobby.lobby + Configuration = WG.Chobby.Configuration + Configuration:SetConfigValue("replayServerCommands", false) + SetState(false) + WG.Delay(function() + Configuration:AddListener("OnConfigurationChange", + function(listener, key, value) + if key == "captureServerCommands" then + SetState(value) + end + end + ) + end, 0.1) +end + +function widget:Shutdown() + Disable() +end \ No newline at end of file diff --git a/LuaMenu/widgets/dbg_command_replay.lua b/LuaMenu/widgets/dbg_command_replay.lua index c2b26b11b..e547061c7 100644 --- a/LuaMenu/widgets/dbg_command_replay.lua +++ b/LuaMenu/widgets/dbg_command_replay.lua @@ -1,7 +1,3 @@ --- Seems unwise to make this a GUI setting, even if it's Dev-only... --- I wonder if there's a good way to have local overrides without it being tracked by Git. -local AUTO_QUIT_ON_FINISH = false - function widget:GetInfo() return { name = "Command replay", @@ -10,48 +6,36 @@ function widget:GetInfo() date = "", license = "", layer = 99999, - enabled = true + enabled = false, } end VFS.Include("libs/json.lua") -local Configuration -local lobby - -local enabled = false +-------------------------------------------------------------------------------- +-- Local Variables +-------------------------------------------------------------------------------- -function widget:Initialize() - lobby = WG.LibLobby.lobby -end - -function widget:Initialize() - lobby = WG.LibLobby.lobby - WG.Delay(function() - Configuration = WG.Chobby.Configuration - SetState(Configuration.replayServerCommands) +-- Seems unwise to make this a GUI setting, even if it's Dev-only... +-- I wonder if there's a good way to have local overrides without it being tracked by Git. +local AUTO_QUIT_ON_FINISH = false - Configuration:AddListener("OnConfigurationChange", - function(listener, key, value) - if key == "replayServerCommands" then - SetState(value) - end - end - ) - end, 0.1) -end +local Configuration, lobby +local enabled = false -function SetState(value) +-------------------------------------------------------------------------------- +-- Local Functions +-------------------------------------------------------------------------------- +local function SetState(value) if enabled == value then return end enabled = value if enabled then - Spring.Echo("===Command replay starting...===") - - cmds = json.decode(VFS.LoadFile("commands.json")) - Spring.Echo("Total commands: " .. tostring(#cmds)) + Spring.Log(LOG_SECTION, Log.Debug, "===Command replay starting...===") + local cmds = json.decode(VFS.LoadFile("commands.json")) + Spring.Log(LOG_SECTION, Log.Debug, "Total commands: ", #cmds) for i, v in ipairs(cmds) do lobby:CommandReceived(v) @@ -61,6 +45,28 @@ function SetState(value) Spring.Quit() end else - Spring.Echo("===Command capture disabled===") + Spring.Log(LOG_SECTION, Log.Notice, "===Command replay disabled===") end end + +-------------------------------------------------------------------------------- +-- Widget Interface +-------------------------------------------------------------------------------- + +function widget:Initialize() + lobby = WG.LibLobby.lobby + Configuration = WG.Chobby.Configuration + Configuration:SetConfigValue("replayServerCommands", false) + SetState(false) + WG.Delay(function() + Configuration:AddListener("OnConfigurationChange", + function(listener, key, value) + if key == "replayServerCommands" then + SetState(value) + end + end + ) + end, 0.1) +end + +