Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- boot.lua script loading module
- -- pathes block
- local path = (arg[0]:match'(.*)[\\/]'):gsub('\\', '/')
- local mpath = arg[1] and (arg[1]:match'(.*)[\\/]'):gsub('\\', '/')
- local lpath = {
- path..'/?.lua',
- path..'/?/init.lua',
- }
- local cpath = {
- path..'/?.dll',
- path..'/?/?.dll',
- path..'/?/core.dll'
- }
- if mpath then
- lpath[#lpath + 1] = mpath..'/?.lua'
- lpath[#lpath + 1] = mpath..'/?/init.lua'
- cpath[#cpath + 1] = mpath..'/?.dll'
- cpath[#cpath + 1] = mpath..'/?/?.dll'
- cpath[#cpath + 1] = mpath..'/?/core.dll'
- else
- mpath = path
- end
- package.path = table.concat(lpath, ';')
- package.cpath = table.concat(cpath, ';')
- local lenv = os.getenv('LUA_PATH')
- local cenv = os.getenv('LUA_CPATH')
- if lenv then package.path = package.path..';'..lenv end
- if cenv then package.cpath = package.cpath..';'..cenv end
- local args = {}
- args[-1] = path
- args[0] = mpath..'/main.lua'
- for i = 1, math.huge do
- if not arg[i] then break end
- args[i] = arg[i]
- end
- local function error_printer(msg, layer)
- local msg = debug.traceback("Error: " .. tostring(msg), 1 + (layer or 1)):gsub("\n[^\n]+$", "")
- local res = {}
- for line in (msg..'\n'):gmatch('(.-)\n') do
- if line:find('boot.lua') then
- if res[#res] and res[#res]:find('%[C%]: in function') then
- --res[#res] = nil
- end
- --break
- end
- res[#res + 1] = line
- end
- print(table.concat(res, '\n'))
- end
- node = {}
- node.time = require'socket'.gettime
- node.sleep = require'socket'.sleep
- node.sleepdelay = .1
- node.path = mpath or path
- local function endslash(path)
- return path:sub(#path - 1) == "/" and path or path..'/'
- end
- local function isAbsolute(path)
- path = path:gsub('\\', '/')
- return path:find('/') == 1 or path:find('%a:') == 1
- end
- local function toAbsolute(path, global)
- return isAbsolute(path) and path or endslash(global)..path
- end
- local function readfile(path, mode, prefix)
- mode = mode or 'rb'
- path = toAbsolute(path, prefix)
- local file, err = io.open(path, mode)
- if not file then return nil, err end
- local data = file:read'*all'
- file:close()
- return data
- end
- local function writefile(data, path, mode, prefix)
- mode = mode or 'wb'
- path = toAbsolute(path, prefix)
- local file, err = io.open(path, mode)
- if not file then return nil, err end
- file:write(data)
- return file:close()
- end
- function node.readfile(path, mode)
- readfile(path, mode, node.path)
- end
- function node.writefile(data, path, mode)
- writefile(data, path, mode, node.path)
- end
- function node.run()
- require('main')
- if not node then error('Node not found') end
- if node.load then node.load(args) end
- if node.update then
- while true do
- node.update()
- node.sleep(node.sleepdelay or .1)
- end
- end
- end
- local protected = {}
- protected.loaded = {}
- for k, v in pairs(package.loaded) do
- protected.loaded[k] = true
- end
- protected.preload = {}
- for k, v in pairs(package.preload) do
- protected.preload[k] = true
- end
- protected.loaders = {}
- for k, v in pairs(package.loaders) do
- protected.loaders[k] = true
- end
- protected._G = {}
- for k, v in pairs(_G) do
- protected._G[k] = true
- end
- function node.reload()
- for k, v in pairs(package.loaded) do
- if not protected.loaded[k] then package.loaded[k] = nil end
- end
- for k, v in pairs(package.preload) do
- if not protected.preload[k] then package.loaded[k] = nil end
- end
- for k, v in pairs(package.loaders) do
- if not protected.loaders[k] then package.loaded[k] = nil end
- end
- for k, v in pairs(_G) do
- if not protected._G[k] then _G[k] = nil end
- end
- print(xpcall(node.run, error_printer))
- end
- local result = xpcall(node.run, error_printer)
- if not result then debug.debug() end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement