Skip to content

Commit

Permalink
Refactored debugs and added console commands
Browse files Browse the repository at this point in the history
New console commands for printing settings:
- cpSettingsPrintVehicle
- cpSettingsPrintGenerator
- cpSettingsPrintJob
- cpSettingsPrintGlobal
  • Loading branch information
schwiti6190 committed Sep 14, 2023
1 parent 0366a34 commit db60d79
Show file tree
Hide file tree
Showing 10 changed files with 199 additions and 72 deletions.
27 changes: 26 additions & 1 deletion scripts/CpGlobalSettings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ function CpGlobalSettings:init()
g_messageCenter:subscribe(MessageType.SETTING_CHANGED[GameSettings.SETTING.USE_MILES], self.onUnitChanged, self)
g_messageCenter:subscribe(MessageType.SETTING_CHANGED[GameSettings.SETTING.USE_ACRE], self.onUnitChanged, self)
g_messageCenter:subscribe(MessageType.CP_DISTANCE_UNIT_CHANGED, self.onUnitChanged, self)

self:registerConsoleCommands()
end

function CpGlobalSettings:registerXmlSchema(schema,baseKey)
Expand Down Expand Up @@ -105,4 +107,27 @@ end

function CpGlobalSettings:debug(str,...)
CpUtil.debugFormat(CpDebug.DBG_HUD,"Global settings: "..str,...)
end
end

---------------------------------------------
--- Console Commands
---------------------------------------------

function CpGlobalSettings:registerConsoleCommands()
g_devHelper.consoleCommands:registerConsoleCommand("cpSettingsPrintGlobal",
"Prints the global settings or a given setting",
"consoleCommandPrintSetting", self)
end

function CpGlobalSettings:consoleCommandPrintSetting(name)
if name == nil then
CpUtil.info("%d Global settings printed", tostring(self.settings))
return
end
local num = tonumber(name)
if num then
CpUtil.info(tostring(self.settings[num]))
return
end
CpUtil.info(tostring(self[name]))
end
15 changes: 12 additions & 3 deletions scripts/CpSettingsUtil.lua
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,17 @@ end
---@param class table class to save the data
---@param filePath string
function CpSettingsUtil.loadSettingsFromSetup(class, filePath)
local xmlFile = XMLFile.load("settingSetupXml", filePath, CpSettingsUtil.setupXmlSchema)
class.settings = {}
local xmlFile = XMLFile.load("settingSetupXml", filePath, CpSettingsUtil.setupXmlSchema)
class.settings = {}
setmetatable(class.settings, { __tostring = function(self)
--- Adds tostring function to print all settings
for i, s in ipairs(self) do
CpUtil.info("%2d: %s", i, tostring(s))
end
return tostring(#self)
end})
class.settingsBySubTitle = {}
local uniqueID = 0
local uniqueID = 0
local autoUpdateGui = xmlFile:getValue("Settings#autoUpdateGui")
local setupKey = xmlFile:getValue("Settings#prefixText")
local pageTitle = xmlFile:getValue("Settings#title")
Expand Down Expand Up @@ -256,7 +263,9 @@ end
---@param class table
---@param settings table
function CpSettingsUtil.cloneSettingsTable(class, settings, ...)
local mt = getmetatable(settings)
class.settings = {}
setmetatable(class.settings, mt)
for _, setting in ipairs(settings) do
local settingClone = setting:clone(...)
table.insert(class.settings, settingClone)
Expand Down
104 changes: 44 additions & 60 deletions scripts/CpUtil.lua
Original file line number Diff line number Diff line change
Expand Up @@ -109,30 +109,6 @@ function CpUtil.getVariable(variableName)
return f and f() or nil
end

function CpUtil.internalPrint(printFunc, infoString, ...)
CpUtil.try(
function (...)
printFunc(string.format('%s %s', infoString, string.format(...)))
end,
...)
end

-- convenience debug function that expects string.format() arguments,
-- CpUtil.debugVehicle( CpDebug.DBG_TURN, "fill level is %.1f, mode = %d", fillLevel, mode )
---@param channel number
function CpUtil.debugFormat(channel, ...)
if CpDebug and CpDebug:isChannelActive(channel) then
local updateLoopIndex = g_updateLoopIndex and g_updateLoopIndex or 0
local timestamp = getDate( ":%S")
channel = channel or 0
CpUtil.try(
function (...)
print(string.format('%s [dbg%d lp%d] %s', timestamp, channel, updateLoopIndex, string.format(...)))
end,
...)
end
end

--- (Safely) get the name of a vehicle or implement.
---@param object table vehicle or implement
function CpUtil.getName(object)
Expand All @@ -149,6 +125,35 @@ function CpUtil.getName(object)
return object.getName and object:getName() .. '/' .. helperName or 'Unknown'
end

function CpUtil.internalPrint(tag, ...)
CpUtil.try(
function (...)
local updateLoopIndex = g_updateLoopIndex and g_updateLoopIndex or 0
local timestamp = getDate( ":%S")
local infoString = string.format('%s [%s lp%d]', timestamp, tag or "---", updateLoopIndex)
print(string.format('%s %s', infoString, string.format(...)))
end, ...)
end

function CpUtil.internalPrintVehicle(vehicle, tag, str, ...)
str = string.format("%s: %s", CpUtil.getName(vehicle), str)
CpUtil.internalPrint(tag, str, ...)
end

function CpUtil.internalPrintImplement(implement, tag, str, ...)
str = string.format("%s: %s", CpUtil.getName(implement), str)
CpUtil.internalPrintVehicle(implement.rootVehicle, tag, str, ...)
end

-- convenience debug function that expects string.format() arguments,
-- CpUtil.debugVehicle( CpDebug.DBG_TURN, "fill level is %.1f, mode = %d", fillLevel, mode )
---@param channel number
function CpUtil.debugFormat(channel, ...)
if CpDebug and CpDebug:isChannelActive(channel) then
CpUtil.internalPrint("dbg"..channel, ...)
end
end

-- convenience debug function to show the vehicle name and expects string.format() arguments,
-- CpUtil.debugVehicle( CpDebug.DBG_TURN, vehicle, "fill level is %.1f, mode = %d", fillLevel, mode )
---@param channel number
Expand All @@ -157,14 +162,7 @@ function CpUtil.debugVehicle(channel, vehicle, ...)
local rootVehicle = vehicle and vehicle.rootVehicle
local active = rootVehicle == nil or rootVehicle.getCpSettings == nil or CpUtil.isVehicleDebugActive(rootVehicle)
if CpDebug and active and CpDebug:isChannelActive(channel) then
local updateLoopIndex = g_updateLoopIndex and g_updateLoopIndex or 0
local timestamp = getDate( ":%S")
channel = channel or 0
CpUtil.try(
function (...)
print(string.format('%s [dbg%d lp%d] %s: %s', timestamp, channel, updateLoopIndex, CpUtil.getName(vehicle), string.format( ... )))
end,
...)
CpUtil.internalPrintVehicle(vehicle, "dbg"..channel, ...)
end
end

Expand All @@ -183,45 +181,31 @@ function CpUtil.isVehicleDebugActive(vehicle)
end

function CpUtil.info(...)
local updateLoopIndex = g_updateLoopIndex and g_updateLoopIndex or 0
local timestamp = getDate( ":%S")
CpUtil.try(
function (...)
print(string.format('%s [info lp%d] %s', timestamp, updateLoopIndex, string.format(...)))
end,
...)
CpUtil.internalPrint("info", ...)
end

function CpUtil.infoVehicle(vehicle, ...)
local updateLoopIndex = g_updateLoopIndex and g_updateLoopIndex or 0
local timestamp = getDate( ":%S")
CpUtil.try(
function (...)
print(string.format('%s [info lp%d] %s: %s', timestamp, updateLoopIndex, CpUtil.getName(vehicle), string.format( ... )))
end,
...)
CpUtil.internalPrintVehicle(vehicle, "info", ...)
end

function CpUtil.infoImplement(implement, ...)
local updateLoopIndex = g_updateLoopIndex and g_updateLoopIndex or 0
local timestamp = getDate( ":%S")
CpUtil.try(
function (...)
local rootVehicle = implement.rootVehicle or implement
print(string.format('%s [info lp%d] %s(%s): %s', timestamp, updateLoopIndex, CpUtil.getName(rootVehicle), CpUtil.getName(implement), string.format( ... )))
end,
...)
CpUtil.internalPrintImplement(implement, "info", ...)
end

--- The same as CpUtil.info(), but also uses printCallstack()
function CpUtil.error(...)
printCallstack()
local updateLoopIndex = g_updateLoopIndex and g_updateLoopIndex or 0
local timestamp = getDate( ":%S")
if printError == nil then
printError = print
end
CpUtil.internalPrint(printError, string.format('%s [error lp%d]', timestamp, updateLoopIndex), ...)
CpUtil.internalPrint("error", ...)
end

function CpUtil.errorVehicle(vehicle, ...)
printCallstack()
CpUtil.internalPrintVehicle( vehicle, "error", ...)
end

function CpUtil.errorImplement(implement, ...)
printCallstack()
CpUtil.internalPrintImplement(implement, "error", ...)
end

--- Create a node at x, z, direction according to yRotation.
Expand Down
9 changes: 8 additions & 1 deletion scripts/ai/jobs/CpAIJob.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
--- Basic cp job.
--- Every cp job should be derived from this job.
---@class CpAIJob : AIJob
---@field jobTypeIndex number
CpAIJob = {
name = "",
jobName = "",
Expand All @@ -9,7 +10,13 @@ CpAIJob = {
local AIJobCp_mt = Class(CpAIJob, AIJob)

function CpAIJob.new(isServer, customMt)
local self = AIJob.new(isServer, customMt or AIJobCp_mt)
local mt = customMt or AIJobCp_mt
mt.__tostring = function(_self)
CpUtil.info("Job: %s", g_currentMission.aiJobTypeManager:getJobTypeByIndex(_self.jobTypeIndex).name)
return string.format("%d Job parameters", tostring(_self.cpJobParameters))
end

local self = AIJob.new(isServer, mt)
self.isDirectStart = false
self:setupTasks(isServer)

Expand Down
4 changes: 1 addition & 3 deletions scripts/ai/jobs/CpJobParameters.lua
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,7 @@ end


function CpJobParameters:__tostring()
for i, setting in ipairs(self.settings) do
CpUtil.info("%s", tostring(setting))
end
return tostring(self.settings)
end

--- Are the setting values roughly equal.
Expand Down
18 changes: 18 additions & 0 deletions scripts/ai/parameters/AIParameterSetting.lua
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,24 @@ function AIParameterSetting:debug(str, ...)
end
end

function AIParameterSetting:info(str, ...)
local name = string.format("%s: ", self.name)
if self.vehicle == nil then
CpUtil.info(name..str, ...)
else
CpUtil.infoVehicle(self.vehicle, name..str, ...)
end
end

function AIParameterSetting:error(str, ...)
local name = string.format("%s: ", self.name)
if self.vehicle == nil then
CpUtil.error(name..str, ...)
else
CpUtil.errorVehicle(self.vehicle, name..str, ...)
end
end

function AIParameterSetting:getDebugString()
return tostring(self)
end
3 changes: 1 addition & 2 deletions scripts/ai/parameters/AIParameterSettingList.lua
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,7 @@ end
function AIParameterSettingList:getClosestIxFromSetup(ix)
local value = self.data.values[ix]
if value == nil then
self:debug("Setting value bugged, ix: %s", tostring(ix))
printCallstack()
self:error("Setting value bugged, ix: %s", tostring(ix))
return 1
end
-- find the value requested
Expand Down
19 changes: 18 additions & 1 deletion scripts/specializations/CpAIWorker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,11 @@ end

function CpAIWorker.registerConsoleCommands()
g_devHelper.consoleCommands:registerConsoleCommand("cpVehicleOnWorkStartTest",
"Raise the field work start event.", "consoleCommandRaiseWorkStart", CpAIWorker)
"Raise the field work start event.",
"consoleCommandRaiseWorkStart", CpAIWorker)
g_devHelper.consoleCommands:registerConsoleCommand("cpSettingsPrintJob",
"Prints the current job and job parameters",
"consoleCommandPrintCurrentSelectedJob", CpAIWorker)
--- TODO: Adding functions to execute the lowering, raising and fieldwork end events.
end

Expand All @@ -563,3 +567,16 @@ function CpAIWorker:consoleCommandRaiseWorkStart()
c:delete()
end
end

function CpAIWorker:consoleCommandPrintCurrentSelectedJob()
local vehicle = g_currentMission.controlledVehicle
if not vehicle or vehicle.getCpStartableJob == nil then
CpUtil.info("Not entered a valid vehicle!")
return
end
local job = vehicle:getCpStartableJob()
if not job then
CpUtil.infoVehicle(vehicle, "No valid job found!")
end
CpUtil.infoVehicle(vehicle, tostring(job))
end
37 changes: 36 additions & 1 deletion scripts/specializations/CpCourseGeneratorSettings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ function CpCourseGeneratorSettings.initSpecialization()
CpSettingsUtil.registerXmlSchema(schema,
"vehicles.vehicle(?)"..CpCourseGeneratorSettings.KEY..CpCourseGeneratorSettings.VINE_SETTINGS_KEY.."(?)")
CpCourseGeneratorSettings.loadSettingsSetup()

CpCourseGeneratorSettings.registerConsoleCommands()
end


Expand Down Expand Up @@ -234,4 +236,37 @@ function CpCourseGeneratorSettings:generateWorkWidthSettingValuesAndTexts(settin
table.insert(texts, i)
end
return values, texts
end
end

---------------------------------------------
--- Console Commands
---------------------------------------------

function CpCourseGeneratorSettings.registerConsoleCommands()
g_devHelper.consoleCommands:registerConsoleCommand("cpSettingsPrintGenerator",
"Prints the course generator settings or a given setting",
"consoleCommandPrintSetting", CpCourseGeneratorSettings)
end

function CpCourseGeneratorSettings:consoleCommandPrintSetting(name)
local vehicle = g_currentMission.controlledVehicle
if not vehicle then
CpUtil.info("Not entered a valid vehicle!")
return
end
local spec = vehicle.spec_cpCourseGeneratorSettings
if not spec then
CpUtil.infoVehicle(vehicle, "has no course generator settings!")
return
end
if name == nil then
CpUtil.infoVehicle(vehicle,"%d Course generator settings printed", tostring(spec.settings))
return
end
local num = tonumber(name)
if num then
CpUtil.infoVehicle(vehicle, tostring(spec.settings[num]))
return
end
CpUtil.infoVehicle(vehicle, tostring(spec[name]))
end
Loading

0 comments on commit db60d79

Please sign in to comment.