Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FB_reviewed_dbg_command #358

Open
wants to merge 1 commit into
base: extra-dev-options
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 52 additions & 40 deletions LuaMenu/widgets/dbg_command_capture.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,58 +6,28 @@ function widget:GetInfo()
date = "",
license = "",
layer = 99999,
enabled = true
enabled = false,
}
end

VFS.Include("libs/json.lua")

--------------------------------------------------------------------------------
-- Local Variables
--------------------------------------------------------------------------------

local Configuration
local lobby

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")
Expand Down Expand Up @@ -87,7 +57,7 @@ function CaptureFunction(obj, fname, registerName)
}
end

function Disable()
local function Disable()
if captureFile then
captureFile:close()
captureFile = nil
Expand All @@ -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
72 changes: 39 additions & 33 deletions LuaMenu/widgets/dbg_command_replay.lua
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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)
Expand All @@ -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