-------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -- -- file: export_UnitDefs.lua -- brief: Save the UnitDefs table to json for spreadsheeting -- author: Birdulon -- -- Copyright (C) 2020. -- Licensed under MIT or WTFPL, whichever you prefer. -- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- --local json = require "json" --VFS.Include("LuaUI/Widgets/json.lua") function widget:GetInfo() return { name = "Export UnitDefs", desc = "Exports UnitDefs table to a json when /exportUnitDefs is written in chat", author = "Birdulon", date = "June 2020", license = "MIT/WTFPL", layer = 0, enabled = true -- loaded by default? } end function widget:Update(dt) local filename = "SPR_" .. Engine.versionFull .. "_ZK_" .. Game.gameVersion .. "_unitdefs" local data = {} for unitID=1,#UnitDefs do data[unitID] = {} for k,v in UnitDefs[unitID]:pairs() do if k == "wDefs" then -- Skip as it's actually empty elseif k == "weapons" then data[unitID][k] = v for i=1,#v do if type(data[unitID].weapons[i].weaponDef) == "number" then local wepDef = WeaponDefs[data[unitID].weapons[i].weaponDef] for name,param in wepDef:pairs() do data[unitID].weapons[i][name] = param -- This flattens the structure a little bit, but I think it's a useful thing to do. end end end else data[unitID][k] = v end end end table.save(data, filename .. ".lua") Spring.Echo("game_message: UnitDefs table saved to " .. filename .. ".lua") -- if json then -- local file = io.open(filename .. ".json", "w") -- io.output(file) -- io.write(json.encode(data)) -- io.close(file) -- Spring.Echo("game_message: UnitDefs table saved to " .. filename .. ".json") -- end widgetHandler:RemoveWidget() end -- function widget:Initialize() -- end