[Lua] Run Code Ingame

Started by Anonymous, April 07, 2018, 07:07:54 PM

Previous topic - Next topic

Anonymous

Commands:

/s <code> (for server-side Lua expressions)

/c <code> (for client-side Lua expressions)

/s2 <code> (for server-side Lua statements)

/c2 <code> (for client-side Lua statements)



Server-side Code:


addCommandHandler("s",function(commandName,code,client) -- expressions
if code == '' then
outputChatBox(client.player.name..", you must type some server code to run! Syntax /s <code>", CHAT_TYPE_INFO)
else
local returns = { pcall(load("return "..code)) }
local status = table.remove(returns, 1)
if not status then
local _error = returns[1]
outputChatBox(client.player.name.."'s server code had an error!", CHAT_TYPE_INFO)
outputChatBox("Code: "..code, CHAT_TYPE_INFO)
outputChatBox("Error: ".._error, CHAT_TYPE_INFO)
else
outputChatBox(client.player.name.." ran server code: "..code, CHAT_TYPE_INFO)
local argsStr = getArgsString(table.unpack(returns))
outputChatBox("Returns: "..argsStr, CHAT_TYPE_INFO)
end
end
end)
addCommandHandler("s2",function(commandName,code,client) -- statements
if code == '' then
outputChatBox(client.player.name..", you must type some server code to run! Syntax /s2 <code>", CHAT_TYPE_INFO)
else
local status, _error = pcall(load(code))
if not status then
outputChatBox(client.player.name.."'s server code had an error!", CHAT_TYPE_INFO)
outputChatBox("Code: "..code, CHAT_TYPE_INFO)
outputChatBox("Error: ".._error, CHAT_TYPE_INFO)
else
outputChatBox(client.player.name.." ran server code: "..code, CHAT_TYPE_INFO)
end
end
end)

function ocb(...)
outputChatBox(getArgsString(...), CHAT_TYPE_INFO)
end

function getArgsString(...)
local arg = {...}
    local Values, Value, Type = {}
    for i,v in ipairs(arg) do
        Type = type(v)
        if     Type == "number"   then Value = v
        elseif Type == "boolean"  then Value = (v) and "true" or "false"
        elseif Type == "string"   then Value = [[']]..replace(replace(v, [[]], [[\]]), [[']], [[']])..[[']]
        elseif Type == "table"    then Value = getTableString(v)
        elseif Type == "function" then Value = "function"
        elseif Type == "userdata" then Value = "userdata: "..tostring(v)
        elseif Type == "nil"      then Value = "nil"
        else                           Value = (Type) and ""..Type.."" or "unknown data type"
        end
        table.insert(Values, Value)
    end
    return (Values[1]) and table.concat(Values, ", ") or "nil"
end

function replace(String, LookFor, ReplaceWith)
    if not String or not LookFor or not ReplaceWith then return end
    String, LookFor, ReplaceWith = tostring(String), tostring(LookFor), tostring(ReplaceWith)
   
    local NewString = ""
    local OccuranceStartPos = string.find(String, LookFor, 1, true)
    if not OccuranceStartPos then return String end
   
    if OccuranceStartPos > 1 then
        NewString = NewString..string.sub(String, 1, OccuranceStartPos - 1)
    end
   
    local OccuranceEndPos
    while OccuranceStartPos do
        OccuranceEndPos = OccuranceStartPos + (string.len(LookFor) - 1)
        NewString = NewString..ReplaceWith
        OccuranceStartPos = string.find(String, LookFor, OccuranceEndPos + 1, true)
        if (OccuranceStartPos or 0) - 1 > OccuranceEndPos then
            NewString = NewString..string.sub(String, OccuranceEndPos + 1, OccuranceStartPos - 1)
        end
    end
   
    if OccuranceEndPos < string.len(String) then
        NewString = NewString..string.sub(String, OccuranceEndPos + 1)
    end
   
    return NewString
end

function getTableString(Table)
    local String, IntKey, Element = {}, (isTableIntKey(Table)) and true or false
    table.insert(String, '{')
    for i,v in _G[((IntKey) and 'i' or '')..'pairs'](Table) do
        Element = { i, v }
        for j,w in ipairs(Element) do
            if j == 1 then table.insert(String, ((#String > 1) and ',' or '')..((IntKey) and '' or '[')) end
           
            if not IntKey or j == 2 then
                Type = type(w)
                if Type == 'number' then table.insert(String, w)
                elseif Type == 'boolean' then table.insert(String, ((w) and 'true' or 'false'))
                elseif Type == 'string' then table.insert(String, [[']]..replace(replace(w, [[]], [[\]]), [[']], [[']])..[[']])
                elseif Type == 'table' then table.insert(String, getTableString(w))
                elseif Type == 'function' then table.insert(String, 'function')
                elseif Type == 'userdata' then table.insert(String, 'userdata: '..tostring(w))
                elseif Type == 'nil' then table.insert(String, 'nil')
                else table.insert(String, 'unknown data type')
                end
            end
           
            if j == 1 then table.insert(String, ((IntKey) and '' or ']=')) end
        end
    end
    table.insert(String, '}')
    return table.concat(String)
end

function isTableIntKey(Table)
    local b = 0
    for i,v in pairs(Table) do b = b + 1 end
    for a=1, b do
        if Table[a] == nil then return false end
    end
    return true
end

Client-side code:


addCommandHandler("c",function(commandName,code) -- expressions
if code == '' then
outputChatBox(localPlayer.name..", you must type some client code to run! Syntax /c <code>", CHAT_TYPE_INFO)
else
local returns = { pcall(load("return "..code)) }
local status = table.remove(returns, 1)
if not status then
local _error = returns[1]
outputChatBox(localPlayer.name.."'s client code had an error!", CHAT_TYPE_INFO)
outputChatBox("Code: "..code, CHAT_TYPE_INFO)
outputChatBox("Error: ".._error, CHAT_TYPE_INFO)
else
outputChatBox(localPlayer.name.." ran client code: "..code, CHAT_TYPE_INFO)
local argsStr = getArgsString(table.unpack(returns))
outputChatBox("Returns: "..argsStr, CHAT_TYPE_INFO)
end
end
end)
addCommandHandler("c2",function(commandName,code) -- statements
if code == '' then
outputChatBox(localPlayer.name..", you must type some client code to run! Syntax /c2 <code>", CHAT_TYPE_INFO)
else
local status, _error = pcall(load(code))
if not status then
outputChatBox(localPlayer.name.."'s client code had an error!", CHAT_TYPE_INFO)
outputChatBox("Code: "..code, CHAT_TYPE_INFO)
outputChatBox("Error: ".._error, CHAT_TYPE_INFO)
else
outputChatBox(localPlayer.name.." ran client code: "..code, CHAT_TYPE_INFO)
end
end
end)

function ocb(...)
outputChatBox(getArgsString(...), CHAT_TYPE_INFO)
end

function getArgsString(...)
local arg = {...}
    local Values, Value, Type = {}
    for i,v in ipairs(arg) do
        Type = type(v)
        if     Type == "number"   then Value = v
        elseif Type == "boolean"  then Value = (v) and "true" or "false"
        elseif Type == "string"   then Value = [[']]..replace(replace(v, [[]], [[\]]), [[']], [[']])..[[']]
        elseif Type == "table"    then Value = getTableString(v)
        elseif Type == "function" then Value = "function"
        elseif Type == "userdata" then Value = "userdata: "..tostring(v)
        elseif Type == "nil"      then Value = "nil"
        else                           Value = (Type) and ""..Type.."" or "unknown data type"
        end
        table.insert(Values, Value)
    end
    return (Values[1]) and table.concat(Values, ", ") or "nil"
end

function replace(String, LookFor, ReplaceWith)
    if not String or not LookFor or not ReplaceWith then return end
    String, LookFor, ReplaceWith = tostring(String), tostring(LookFor), tostring(ReplaceWith)
   
    local NewString = ""
    local OccuranceStartPos = string.find(String, LookFor, 1, true)
    if not OccuranceStartPos then return String end
   
    if OccuranceStartPos > 1 then
        NewString = NewString..string.sub(String, 1, OccuranceStartPos - 1)
    end
   
    local OccuranceEndPos
    while OccuranceStartPos do
        OccuranceEndPos = OccuranceStartPos + (string.len(LookFor) - 1)
        NewString = NewString..ReplaceWith
        OccuranceStartPos = string.find(String, LookFor, OccuranceEndPos + 1, true)
        if (OccuranceStartPos or 0) - 1 > OccuranceEndPos then
            NewString = NewString..string.sub(String, OccuranceEndPos + 1, OccuranceStartPos - 1)
        end
    end
   
    if OccuranceEndPos < string.len(String) then
        NewString = NewString..string.sub(String, OccuranceEndPos + 1)
    end
   
    return NewString
end

function getTableString(Table)
    local String, IntKey, Element = {}, (isTableIntKey(Table)) and true or false
    table.insert(String, '{')
    for i,v in _G[((IntKey) and 'i' or '')..'pairs'](Table) do
        Element = { i, v }
        for j,w in ipairs(Element) do
            if j == 1 then table.insert(String, ((#String > 1) and ',' or '')..((IntKey) and '' or '[')) end
           
            if not IntKey or j == 2 then
                Type = type(w)
                if Type == 'number' then table.insert(String, w)
                elseif Type == 'boolean' then table.insert(String, ((w) and 'true' or 'false'))
                elseif Type == 'string' then table.insert(String, [[']]..replace(replace(w, [[]], [[\]]), [[']], [[']])..[[']])
                elseif Type == 'table' then table.insert(String, getTableString(w))
                elseif Type == 'function' then table.insert(String, 'function')
                elseif Type == 'userdata' then table.insert(String, 'userdata: '..tostring(w))
                elseif Type == 'nil' then table.insert(String, 'nil')
                else table.insert(String, 'unknown data type')
                end
            end
           
            if j == 1 then table.insert(String, ((IntKey) and '' or ']=')) end
        end
    end
    table.insert(String, '}')
    return table.concat(String)
end

function isTableIntKey(Table)
    local b = 0
    for i,v in pairs(Table) do b = b + 1 end
    for a=1, b do
        if Table[a] == nil then return false end
    end
    return true
end