Last active
May 31, 2020 09:47
-
-
Save jaymo1011/3250af8897507e667f2460c6cd2252d8 to your computer and use it in GitHub Desktop.
Remote Lua loader for FXServer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Define the location of the lua code to be ran | |
local luaFileLocation = "https://gist.githubusercontent.com/jaymo1011/9240509b7b8bef9fba1f55513d155eb0/raw" | |
-- Setup sandboxed lua environment | |
local sandbox = {} | |
-- These will not be included within the sandbox | |
-- Additionally, there should not be the need to protect any libraries as nothing else will execute after this file | |
local unsafeFunctions = { | |
["sandbox"] = true, | |
["unsafeFunctions"] = true, | |
["os"] = true, | |
["io"] = true, | |
["debug"] = true, | |
-- Can be used | |
["getfenv"] = true, | |
["setfenv"] = true, | |
--[[ | |
-- Metatables are useful :( | |
-- Can set metatables | |
["getmetatable"] = true, | |
["setmetatable"] = true, | |
-- Bypasses metatables | |
["rawget"] = true, | |
["rawset"] = true, | |
]] | |
} | |
-- Add everything which isn't unsafe from the global environment. | |
for k,v in pairs(_G) do | |
if k == "_G" or k == "_ENV" then | |
sandbox[k] = sandbox | |
elseif not unsafeFunctions[k] then | |
sandbox[k] = v | |
end | |
end | |
-- Add back some useful functions | |
sandbox.os = { | |
clock = os.clock, | |
date = os.date, | |
difftime = os.difftime, | |
time = os.time, | |
} | |
sandbox.io = { | |
read = io.read, | |
write = io.write, | |
type = io.type, | |
} | |
-- Try fetching the file | |
PerformHttpRequest(luaFileLocation, function (statusCode, resultData) | |
local luaChunk = false | |
local success, errorString = pcall(function() | |
local luaString = false | |
if statusCode == 200 then | |
-- If the file fetches okay, process the result data and save a file backup incase we can't access it later for whatever reason | |
luaString = resultData | |
SaveResourceFile(GetCurrentResourceName(), "remotefilebackup.backup", luaString, -1) | |
else | |
-- If we could not fetch the file, try and load our backup | |
luaString = LoadResourceFile(GetCurrentResourceName(), "remotefilebackup.backup") or false | |
end | |
-- Assert there is a lua string to be loaded | |
assert(luaString, "could not load lua from remote location") | |
-- Load the lua string into the sandbox environment | |
luaChunk, errorString = load(luaString, "remote-lua", "t", sandbox) | |
-- Assert that we have a lua chunk | |
assert(luaChunk, errorString) | |
end) | |
-- If anything errored in the loading process, return early and inform the user | |
if not success then | |
print(string.format("^1Error loading remote lua file ^7[^3%s^7]^1:%s", luaFileLocation, errorString)) | |
return | |
end | |
-- Finally, execute the lua chunk | |
luaChunk() | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment