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)
|
2014-01-22 03:59:51 -08:00
|
|
|
-- Ensure a command was given..
|
2022-11-04 10:44:34 -04:00
|
|
|
if str == nil or string.len(str) == 0 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
|
|
|
|
2014-01-22 03:59:51 -08:00
|
|
|
-- For safety measures we will nuke the os table..
|
2022-12-03 09:20:16 -05:00
|
|
|
local oldOs = os
|
2024-08-20 14:10:54 -04:00
|
|
|
|
|
|
|
|
-- TODO: Intential assignment to remove access to os during calls
|
|
|
|
|
---@diagnostic disable-next-line: assign-type-mismatch
|
2020-02-19 21:40:57 -08:00
|
|
|
os = nil
|
2020-03-19 18:41:42 -07:00
|
|
|
|
2023-08-28 21:02:28 -04:00
|
|
|
-- Define 'player' and 'target' inside the string for use by the caller
|
|
|
|
|
local definePlayer = 'local player = GetPlayerByName(\'' .. player:getName() .. '\'); '
|
|
|
|
|
local defineTarget = 'local target = player:getCursorTarget(); '
|
2020-07-05 20:09:21 +03:00
|
|
|
|
2014-01-22 03:59:51 -08:00
|
|
|
-- Ensure the command compiles / is valid..
|
2022-12-03 09:20:16 -05:00
|
|
|
local scriptObj, err0 = loadstring(definePlayer .. defineTarget .. str)
|
2022-11-04 10:44:34 -04:00
|
|
|
if scriptObj == nil then
|
2023-12-09 23:54:00 +00:00
|
|
|
player:printToPlayer('Failed to load the given string.')
|
2024-08-25 12:01:53 -04:00
|
|
|
|
|
|
|
|
if err0 then
|
|
|
|
|
player:printToPlayer(err0)
|
|
|
|
|
end
|
|
|
|
|
|
2022-12-03 09:20:16 -05:00
|
|
|
os = oldOs
|
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
|
|
|
|
2014-01-22 03:59:51 -08:00
|
|
|
-- Execute the string..
|
2022-11-04 13:16:24 -04:00
|
|
|
local successfullyExecuted, errorMessage = pcall(scriptObj)
|
|
|
|
|
if not successfullyExecuted then
|
2023-12-09 23:54:00 +00:00
|
|
|
player:printToPlayer('Error calling: ' .. str .. '\n' .. errorMessage)
|
2014-01-22 03:59:51 -08:00
|
|
|
end
|
2020-03-19 18:41:42 -07:00
|
|
|
|
2014-01-22 03:59:51 -08:00
|
|
|
-- Restore the os table..
|
2022-12-03 09:20:16 -05:00
|
|
|
os = oldOs
|
2020-03-19 18:41:42 -07:00
|
|
|
end
|
2023-09-02 07:01:35 -04:00
|
|
|
|
|
|
|
|
return commandObj
|