Skip to content

Commit

Permalink
chore: [system]\runcode 🎨 Run formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
bitpredator committed Sep 26, 2024
1 parent 5fee496 commit 12d0f0b
Show file tree
Hide file tree
Showing 9 changed files with 433 additions and 423 deletions.
32 changes: 16 additions & 16 deletions server-data/resources/[system]/runcode/fxmanifest.lua
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
-- This resource is part of the default Cfx.re asset pack (cfx-server-data)
-- Altering or recreating for local use only is strongly discouraged.

version '1.0.0'
author 'Cfx.re <root@cfx.re>'
description 'Allows server owners to execute arbitrary server-side or client-side JavaScript/Lua code. *Consider only using this on development servers.'
repository 'https://github.com/citizenfx/cfx-server-data'
version("1.0.0")
author("Cfx.re <root@cfx.re>")
description("Allows server owners to execute arbitrary server-side or client-side JavaScript/Lua code. *Consider only using this on development servers.")
repository("https://github.com/citizenfx/cfx-server-data")

game 'common'
fx_version 'bodacious'
game("common")
fx_version("bodacious")

client_script 'runcode_cl.lua'
server_script 'runcode_sv.lua'
server_script 'runcode_web.lua'
client_script("runcode_cl.lua")
server_script("runcode_sv.lua")
server_script("runcode_web.lua")

shared_script 'runcode_shared.lua'
shared_script 'runcode.js'
shared_script("runcode_shared.lua")
shared_script("runcode.js")

client_script 'runcode_ui.lua'
client_script("runcode_ui.lua")

ui_page 'web/nui.html'
files {
'web/nui.html'
}
ui_page("web/nui.html")
files({
"web/nui.html",
})
17 changes: 9 additions & 8 deletions server-data/resources/[system]/runcode/runcode.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
exports('runJS', (snippet) => {
if (IsDuplicityVersion() && GetInvokingResource() !== GetCurrentResourceName()) {
return [ 'Invalid caller.', false ];
}
if (IsDuplicityVersion() && GetInvokingResource() !== GetCurrentResourceName()) {
return [ 'Invalid caller.', false ];
}

try {
return [ new Function(snippet)(), false ];
} catch (e) {
return [ false, e.toString() ];
}
try {
return [ new Function(snippet)(), false ];
}
catch (e) {
return [ false, e.toString() ];
}
});
24 changes: 12 additions & 12 deletions server-data/resources/[system]/runcode/runcode_cl.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
RegisterNetEvent('runcode:gotSnippet')
RegisterNetEvent("runcode:gotSnippet")

AddEventHandler('runcode:gotSnippet', function(id, lang, code)
local res, err = RunCode(lang, code)
AddEventHandler("runcode:gotSnippet", function(id, lang, code)
local res, err = RunCode(lang, code)

if not err then
if type(res) == 'vector3' then
res = json.encode({ table.unpack(res) })
elseif type(res) == 'table' then
res = json.encode(res)
end
end
if not err then
if type(res) == "vector3" then
res = json.encode({ table.unpack(res) })
elseif type(res) == "table" then
res = json.encode(res)
end
end

TriggerServerEvent('runcode:gotResult', id, res, err)
end)
TriggerServerEvent("runcode:gotResult", id, res, err)
end)
36 changes: 18 additions & 18 deletions server-data/resources/[system]/runcode/runcode_shared.lua
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
local runners = {}

function runners.lua(arg)
local code, err = load('return ' .. arg, '@runcode')
local code, err = load("return " .. arg, "@runcode")

-- if failed, try without return
if err then
code, err = load(arg, '@runcode')
end
-- if failed, try without return
if err then
code, err = load(arg, "@runcode")
end

if err then
print(err)
return nil, err
end
if err then
print(err)
return nil, err
end

local status, result = pcall(code)
print(result)
local status, result = pcall(code)
print(result)

if status then
return result
end
if status then
return result
end

return nil, result
return nil, result
end

function runners.js(arg)
return table.unpack(exports[GetCurrentResourceName()]:runJS(arg))
return table.unpack(exports[GetCurrentResourceName()]:runJS(arg))
end

function RunCode(lang, str)
return runners[lang](str)
end
return runners[lang](str)
end
56 changes: 28 additions & 28 deletions server-data/resources/[system]/runcode/runcode_sv.lua
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
function GetPrivs(source)
return {
canServer = IsPlayerAceAllowed(source, 'command.run'),
canClient = IsPlayerAceAllowed(source, 'command.crun'),
canSelf = IsPlayerAceAllowed(source, 'runcode.self'),
}
return {
canServer = IsPlayerAceAllowed(source, "command.run"),
canClient = IsPlayerAceAllowed(source, "command.crun"),
canSelf = IsPlayerAceAllowed(source, "runcode.self"),
}
end

RegisterCommand('run', function(_, _, rawCommand)
local _, _ = RunCode('lua', rawCommand:sub(4))
RegisterCommand("run", function(_, _, rawCommand)
local _, _ = RunCode("lua", rawCommand:sub(4))
end, true)

RegisterCommand('crun', function(source, _, rawCommand)
if not source then
return
end
RegisterCommand("crun", function(source, _, rawCommand)
if not source then
return
end

TriggerClientEvent('runcode:gotSnippet', source, -1, 'lua', rawCommand:sub(5))
TriggerClientEvent("runcode:gotSnippet", source, -1, "lua", rawCommand:sub(5))
end, true)

RegisterCommand('runcode', function(source)
if not source then
return
end
RegisterCommand("runcode", function(source)
if not source then
return
end

local df = LoadResourceFile(GetCurrentResourceName(), 'data.json')
local saveData = {}
local df = LoadResourceFile(GetCurrentResourceName(), "data.json")
local saveData = {}

if df then
saveData = json.decode(df)
end
if df then
saveData = json.decode(df)
end

local p = GetPrivs(source)
local p = GetPrivs(source)

if not p.canServer and not p.canClient and not p.canSelf then
return
end
if not p.canServer and not p.canClient and not p.canSelf then
return
end

p.saveData = saveData
p.saveData = saveData

TriggerClientEvent('runcode:openUi', source, p)
end, true)
TriggerClientEvent("runcode:openUi", source, p)
end, true)
36 changes: 18 additions & 18 deletions server-data/resources/[system]/runcode/runcode_ui.lua
Original file line number Diff line number Diff line change
@@ -1,56 +1,56 @@
local openData

RegisterNetEvent('runcode:openUi')
RegisterNetEvent("runcode:openUi")

AddEventHandler('runcode:openUi', function(options)
AddEventHandler("runcode:openUi", function(options)
openData = {
type = 'open',
type = "open",
options = options,
url = 'http://' .. GetCurrentServerEndpoint() .. '/' .. GetCurrentResourceName() .. '/',
res = GetCurrentResourceName()
url = "http://" .. GetCurrentServerEndpoint() .. "/" .. GetCurrentResourceName() .. "/",
res = GetCurrentResourceName(),
}

SendNuiMessage(json.encode(openData))
end)

RegisterNUICallback('getOpenData', function(_, cb)
RegisterNUICallback("getOpenData", function(_, cb)
cb(openData)
end)

RegisterNUICallback('doOk', function(_, cb)
RegisterNUICallback("doOk", function(_, cb)
SendNuiMessage(json.encode({
type = 'ok'
type = "ok",
}))

SetNuiFocus(true, true)

cb('ok')
cb("ok")
end)

RegisterNUICallback('doClose', function(_, cb)
RegisterNUICallback("doClose", function(_, cb)
SendNuiMessage(json.encode({
type = 'close'
type = "close",
}))

SetNuiFocus(false, false)

cb('ok')
cb("ok")
end)

local rcCbs = {}
local id = 1

RegisterNUICallback('runCodeInBand', function(args, cb)
RegisterNUICallback("runCodeInBand", function(args, cb)
id = id + 1

rcCbs[id] = cb

TriggerServerEvent('runcode:runInBand', id, args)
TriggerServerEvent("runcode:runInBand", id, args)
end)

RegisterNetEvent('runcode:inBandResult')
RegisterNetEvent("runcode:inBandResult")

AddEventHandler('runcode:inBandResult', function(id, result)
AddEventHandler("runcode:inBandResult", function(id, result)
if rcCbs[id] then
local cb = rcCbs[id]
rcCbs[id] = nil
Expand All @@ -59,8 +59,8 @@ AddEventHandler('runcode:inBandResult', function(id, result)
end
end)

AddEventHandler('onResourceStop', function(resourceName)
AddEventHandler("onResourceStop", function(resourceName)
if resourceName == GetCurrentResourceName() then
SetNuiFocus(false, false)
end
end)
end)
Loading

0 comments on commit 12d0f0b

Please sign in to comment.