Advertisement
Snusmumriken

Boot.lua

Jun 1st, 2017
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.66 KB | None | 0 0
  1. -- boot.lua script loading module
  2. -- pathes block
  3. local path = (arg[0]:match'(.*)[\\/]'):gsub('\\', '/')
  4. local mpath = arg[1] and (arg[1]:match'(.*)[\\/]'):gsub('\\', '/')
  5.  
  6. local lpath = {
  7.     path..'/?.lua',
  8.     path..'/?/init.lua',
  9. }
  10.  
  11. local cpath = {
  12.     path..'/?.dll',
  13.     path..'/?/?.dll',
  14.     path..'/?/core.dll'
  15. }
  16.  
  17. if mpath then
  18.     lpath[#lpath + 1] = mpath..'/?.lua'
  19.     lpath[#lpath + 1] = mpath..'/?/init.lua'
  20.     cpath[#cpath + 1] = mpath..'/?.dll'
  21.     cpath[#cpath + 1] = mpath..'/?/?.dll'
  22.     cpath[#cpath + 1] = mpath..'/?/core.dll'
  23. else
  24.     mpath = path
  25. end
  26.  
  27. package.path = table.concat(lpath, ';')
  28. package.cpath = table.concat(cpath, ';')
  29. local lenv = os.getenv('LUA_PATH')
  30. local cenv = os.getenv('LUA_CPATH')
  31. if lenv then package.path = package.path..';'..lenv end
  32. if cenv then package.cpath = package.cpath..';'..cenv end
  33.  
  34. local args = {}
  35. args[-1] = path
  36. args[0]  = mpath..'/main.lua'
  37.  
  38. for i = 1, math.huge do
  39.     if not arg[i] then break end
  40.     args[i] = arg[i]
  41. end
  42.  
  43. local function error_printer(msg, layer)
  44.     local msg = debug.traceback("Error: " .. tostring(msg), 1 + (layer or 1)):gsub("\n[^\n]+$", "")
  45.     local res = {}
  46.     for line in (msg..'\n'):gmatch('(.-)\n') do
  47.         if line:find('boot.lua') then
  48.             if res[#res] and res[#res]:find('%[C%]: in function') then
  49.                 --res[#res] = nil
  50.             end
  51.             --break
  52.         end
  53.         res[#res + 1] = line
  54.     end
  55.     print(table.concat(res, '\n'))
  56. end
  57.  
  58. node = {}
  59. node.time = require'socket'.gettime
  60. node.sleep = require'socket'.sleep
  61. node.sleepdelay = .1
  62. node.path = mpath or path
  63.  
  64. local function endslash(path)
  65.     return path:sub(#path - 1) == "/" and path or path..'/'
  66. end
  67.  
  68. local function isAbsolute(path)
  69.     path = path:gsub('\\', '/')
  70.     return path:find('/') == 1 or path:find('%a:') == 1
  71. end
  72.  
  73. local function toAbsolute(path, global)
  74.     return isAbsolute(path) and path or endslash(global)..path
  75. end
  76.  
  77. local function readfile(path, mode, prefix)
  78.     mode = mode or 'rb'
  79.     path = toAbsolute(path, prefix)
  80.     local file, err = io.open(path, mode)
  81.     if not file then return nil, err end
  82.     local data = file:read'*all'
  83.     file:close()
  84.     return data
  85. end
  86.  
  87. local function writefile(data, path, mode, prefix)
  88.     mode = mode or 'wb'
  89.     path = toAbsolute(path, prefix)
  90.     local file, err = io.open(path, mode)
  91.     if not file then return nil, err end
  92.     file:write(data)
  93.     return file:close()
  94. end
  95.  
  96. function node.readfile(path, mode)
  97.     readfile(path, mode, node.path)
  98. end
  99.  
  100. function node.writefile(data, path, mode)
  101.     writefile(data, path, mode, node.path)
  102. end
  103.  
  104. function node.run()
  105.     require('main')
  106.     if not node then error('Node not found') end
  107.     if node.load then node.load(args) end
  108.    
  109.     if node.update then
  110.         while true do
  111.             node.update()
  112.             node.sleep(node.sleepdelay or .1)
  113.         end
  114.     end
  115. end
  116.  
  117. local protected = {}
  118. protected.loaded = {}
  119. for k, v in pairs(package.loaded) do
  120.     protected.loaded[k] = true
  121. end
  122.  
  123. protected.preload = {}
  124. for k, v in pairs(package.preload) do
  125.     protected.preload[k] = true
  126. end
  127.  
  128. protected.loaders = {}
  129. for k, v in pairs(package.loaders) do
  130.     protected.loaders[k] = true
  131. end
  132.  
  133. protected._G = {}
  134. for k, v in pairs(_G) do
  135.     protected._G[k] = true
  136. end
  137.  
  138. function node.reload()
  139.  
  140.     for k, v in pairs(package.loaded) do
  141.         if not protected.loaded[k] then package.loaded[k] = nil end
  142.     end
  143.    
  144.     for k, v in pairs(package.preload) do
  145.         if not protected.preload[k] then package.loaded[k] = nil end
  146.     end
  147.    
  148.     for k, v in pairs(package.loaders) do
  149.         if not protected.loaders[k] then package.loaded[k] = nil end
  150.     end
  151.    
  152.     for k, v in pairs(_G) do
  153.         if not protected._G[k] then _G[k] = nil end
  154.     end
  155.    
  156.     print(xpcall(node.run, error_printer))
  157. end
  158.  
  159.  
  160. local result = xpcall(node.run, error_printer)
  161.  
  162. if not result then debug.debug() end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement