2021-01-18 13:34:00 -05:00
|
|
|
-----------------------------------
|
2016-07-29 08:35:15 -06:00
|
|
|
-- func: exec
|
2014-01-22 03:59:51 -08:00
|
|
|
-- desc: Allows you to execute a Lua string directly from chat.
|
2021-01-18 13:34:00 -05:00
|
|
|
-----------------------------------
|
2024-08-25 10:54:45 -04:00
|
|
|
---@type TCommand
|
2023-09-02 07:01:35 -04:00
|
|
|
local commandObj = {}
|
2014-01-22 03:59:51 -08:00
|
|
|
|
2023-09-02 07:01:35 -04:00
|
|
|
commandObj.cmdprops =
|
2014-01-22 03:59:51 -08:00
|
|
|
{
|
2015-02-20 00:12:53 -07:00
|
|
|
permission = 4,
|
2023-08-28 21:02:28 -04:00
|
|
|
parameters = 's'
|
2020-02-19 21:40:57 -08:00
|
|
|
}
|
2014-01-22 03:59:51 -08:00
|
|
|
|
2023-09-02 07:01:35 -04:00
|
|
|
local function error(player, msg)
|
2023-12-09 23:54:00 +00:00
|
|
|
player:printToPlayer(msg)
|
|
|
|
|
player:printToPlayer('!exec <Lua string>')
|
2020-02-19 21:40:57 -08:00
|
|
|
end
|
2017-08-04 13:50:26 -04:00
|
|
|
|
2023-09-02 07:01:35 -04:00
|
|
|
commandObj.onTrigger = function(player, str)
|
2026-05-28 09:34:57 +01:00
|
|
|
if
|
|
|
|
|
not str or
|
|
|
|
|
str == ''
|
|
|
|
|
then
|
2023-08-28 21:02:28 -04:00
|
|
|
error(player, 'You must enter a string to execute.')
|
2020-02-19 21:40:57 -08:00
|
|
|
return
|
2014-01-22 03:59:51 -08:00
|
|
|
end
|
2020-03-19 18:41:42 -07:00
|
|
|
|
2026-05-28 09:34:57 +01:00
|
|
|
--
|
|
|
|
|
-- Set up a safe environment for us to execute the incoming Lua string in
|
|
|
|
|
--
|
2024-08-20 14:10:54 -04:00
|
|
|
|
2026-05-28 09:34:57 +01:00
|
|
|
-- Capture things from the current or global scope we might want in the sandbox.
|
|
|
|
|
local safeEnv =
|
|
|
|
|
{
|
|
|
|
|
player = player,
|
|
|
|
|
target = player:getCursorTarget()
|
|
|
|
|
}
|
2020-03-19 18:41:42 -07:00
|
|
|
|
2026-05-28 09:34:57 +01:00
|
|
|
setmetatable(safeEnv, {
|
|
|
|
|
__index = function(_, key)
|
|
|
|
|
-- Block the use of powerful libraries
|
|
|
|
|
if
|
|
|
|
|
key == 'os' or
|
|
|
|
|
key == 'io' or
|
|
|
|
|
key == 'debug' or
|
|
|
|
|
key == 'require'
|
|
|
|
|
then
|
|
|
|
|
player:printToPlayer('Trying to access forbidden global!')
|
|
|
|
|
return nil
|
|
|
|
|
end
|
2020-07-05 20:09:21 +03:00
|
|
|
|
2026-05-28 09:34:57 +01:00
|
|
|
-- Otherwise, let them through
|
|
|
|
|
return _G[key]
|
2024-08-25 12:01:53 -04:00
|
|
|
end
|
2026-05-28 09:34:57 +01:00
|
|
|
})
|
2024-08-25 12:01:53 -04:00
|
|
|
|
2026-05-28 09:34:57 +01:00
|
|
|
local scriptObj, compileErr = loadstring(str)
|
|
|
|
|
if not scriptObj then
|
|
|
|
|
player:printToPlayer(fmt('Failed to load string: {}', tostring(compileErr)))
|
2020-02-19 21:40:57 -08:00
|
|
|
return
|
2014-01-22 03:59:51 -08:00
|
|
|
end
|
2020-03-19 18:41:42 -07:00
|
|
|
|
2026-05-28 09:34:57 +01:00
|
|
|
-- Bind the sandboxed environment to the compiled function.
|
|
|
|
|
setfenv(scriptObj, safeEnv)
|
2020-03-19 18:41:42 -07:00
|
|
|
|
2026-05-28 09:34:57 +01:00
|
|
|
local success, execErr = pcall(scriptObj)
|
|
|
|
|
if not success then
|
|
|
|
|
player:printToPlayer(fmt('Error executing: {}', tostring(execErr)))
|
|
|
|
|
end
|
2020-03-19 18:41:42 -07:00
|
|
|
end
|
2023-09-02 07:01:35 -04:00
|
|
|
|
|
|
|
|
return commandObj
|