Code: Select all
-- console.lua
--
local version = .05
function gadget:GetInfo()
return {
name = "Lua Console",
desc = "version " .. version,
author = "Alchemist",
date = "January 2011",
license = "MIT X11",
layer = 0,
enabled = true
}
end
local msgID = gadget:GetInfo().name .. ": "
--UNSYNCED-------------------------------------------------------------------
if (not gadgetHandler:IsSyncedCode()) then
local active = false
local command = ""
local hist = ""
local specialKeys = { [0] = ')', [1] = '!', [2] = '@', [3] = '#', [4] = '$', [5] = '%', [6] = '^', [7] = '&', [8] = '*', [9] = '(',
[32] = ' ', [39] = '\'', [39.1] = '\"', [44] = ',', [44.1] = '<', [45] = '-', [45.1] = '_',
[46] = '.', [46.1] = '>', [59] = ';', [59.1] = ':', [61] = '=', [61.1] = '+',
[91] = '[', [93] = ']', [91.1] = '{', [93.1] = '}', [96] = '`', [96.1] = '~'}
local actions = { "gameinfo", "controlunit", "sharedialog", "togglelos", "mouse2", "mousestate", "chat", "debug" }
function gadget:GamePreload()
Spring.SendMessage("Lua Console loaded... to Activate/Deactivate press CTRL")
end
function gadget:KeyPress(k, m, r, l)
handleKeys(k, m, l)
end
function handleKeys(key, m, l)
if (key == 306) then
if (active == false) then
active = true
Spring.SendMessage("---Lua Console ACTIVATED---")
for _, a in pairs(actions) do
Spring.SendCommands({"unbindAction " .. a})
end
else
active = false
Spring.SendMessage("---Lua Console deACTIVATED---")
--Spring.SendCommands({"keyreload"}) CRASH!!!
end
return false
end
if (active == true) then
if(key <= 122 and key >= 97) then -- A-Z and a-z
if(m.shift) then
command = command .. string.sub(string.upper(l), -1)
elseif(m.alt or m.ctrl) then
command = command .. string.sub(l, -1)
else
command = command .. l
end
elseif (key <= 57 and key >= 48) then -- 0-9
key = key - 48
if (m.shift) then
command = command .. specialKeys[key]
else
command = command .. key
end
elseif (key == 8) then -- Backspace
if(string.len(command) >= 1) then
command = string.sub(command, 1, -2)
end
elseif (key == 13) then -- Enter
eval_and_display (command)
hist = command .. '\n' .. hist
command = ""
elseif (specialKeys[key] == nil) then
else
if (m.shift) then
command = command .. specialKeys[key + .1]
else
command = command .. specialKeys[key]
end
end
end
end
GG.handleKeys = handleKeys
function eval_and_display (cmd)
local chunk = loadstring('return ' .. cmd)
if(chunk) then
local results = { pcall(chunk) }
if(not results[1]) then
Spring.SendLuaRulesMsg(msgID .. cmd)
else
table.remove(results, 1)
Spring.Echo("--> " .. tostring (results[1]))
end
end
end
function DrawScreen()
local vsx, vsy = gl.GetViewSizes()
local posx, posy = vsx * 0.25, vsy * 0.75
gl.Rect (posx - 5, posy + 13, posx + 502, posy + 10)
gl.Text(command, posx, posy, 12, "on")
gl.Text(hist, posx, (.9 * posy), 12, "on")
end
function gadget:KeyPress(k, m, r, l)
GG.handleKeys(k, m, l)
end
--SYNCED---------------------------------------------------------------------
else
function gadget:RecvLuaMsg(msg, player)
local cmd = string.match(msg, msgID .. "(.*)")
if cmd then
local chunk = loadstring('return ' .. cmd)
if(chunk) then
doline(chunk)
end
end
end
function doline(cmd)
local results = { pcall(cmd) }
if (not results[1]) then
Spring.Echo(results[2])
else
table.remove(results, 1)
Spring.Echo(unpack(results))
end
end
end
Heres a improved version, it gives better output and doesnt just suppres erros, tough its an uncought side effect i think.
Also i have these 2 problems, i dont know how to restrict gl.Text to some size and how to have it draw a gl.Rect inside gadget:KeyPress so it only draws when it gets activated.
Im fairly new to this whole graphics stuff.