From 20b391ca6ff670aad944cd75013c43bab3cac130 Mon Sep 17 00:00:00 2001 From: Fireball <32598847+FIr3baL@users.noreply.github.com> Date: Sat, 15 Apr 2023 22:58:50 +0200 Subject: [PATCH] =?UTF-8?q?dbg=5Fcommand=5Fcapture.lua=20-=20little=20rest?= =?UTF-8?q?ructured=20(sections)=20-=20default=20=3D=20disabled=20-=20disa?= =?UTF-8?q?ble=20Configuration.captureServerCommands=20on=20startup=20-=20?= =?UTF-8?q?made=20global=20functions=20local=20(SetState,=20Disable)=20-?= =?UTF-8?q?=20changed=20order=20of=20functions=20in=20code=20listing=20(it?= =?UTF-8?q?=C2=B4s=20a=20direct=20consequence=20of=20having=20local=20func?= =?UTF-8?q?tions,=20they=20need=20to=20be=20defined=20before=20usage)=20-?= =?UTF-8?q?=20changed=20Spring.Echo=20to=20Spring.Log?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit dbg_command_replay.lua - deleted duplicate function widget:Initialize() - little restructured (sections) - default = disabled - disable Configuration.replayServerCommands on startup - made global functions local (SetState) - changed order of functions in code listing (it´s a direct consequence of having local functions, they need to be defined before usage) - changed Spring.Echo to Spring.Log - made global var cmds local --- LuaMenu/widgets/dbg_command_capture.lua | 92 ++++++++++++++----------- LuaMenu/widgets/dbg_command_replay.lua | 72 ++++++++++--------- 2 files changed, 91 insertions(+), 73 deletions(-) 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 + +