Advertisement
Guest User

ServerCore v0.1

a guest
Nov 10th, 2015
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.55 KB | None | 0 0
  1. --[[
  2.     Server Core v0.1
  3.     Author: LeshaInc
  4.     Date: 9 November 2015
  5.     Licenze: MIT
  6. ]]
  7.  
  8. --[[
  9.     Info codes:
  10.     1 - Server started
  11.     2 - Server stopped
  12. ]]
  13.  
  14. -- settings
  15. local __debug    = false
  16. local __name     = "server"
  17. local __timezone = 0
  18. local __correct  = 0
  19. local __port     = 256
  20.  
  21. -- apis
  22. local component = require("component")
  23. local un  = require("unicode")
  24. local fs  = require("filesystem")
  25. local event = require("event")
  26. local keyboard = require("keyboard")
  27. local fs = require("filesystem")
  28. local shell = require("shell")
  29.  
  30. -- other vars
  31. local info_types = {
  32.     {"err","Error",0xFF0000},
  33.     {"ok","OK",0x00AA00},
  34.     {"warn","Warning",0xFF6600},
  35.     {"info","Info",0x62B1F6},
  36. }
  37. local text = 0xEEEEEE
  38. local tz = __timezone + __correct
  39. local t_correction = tz * 3600
  40. local running = true
  41. local listeners = {}
  42. local args, options = shell.parse(...)
  43. local server_path = ""
  44. _G.sc = {}
  45.  
  46. -- components
  47. local gpu
  48. local m
  49.  
  50. -- functions
  51. listeners[1] = {"key_down",function (_,_,k1,k2)
  52.     if keyboard.isControlDown() and keyboard.isKeyDown(46) then
  53.         info("info","interrupted server.")
  54.         running = false
  55.     end
  56. end}
  57.  
  58. function getTime()
  59.     local file = io.open('/tmp/' .. __name ..'.dt', 'w')
  60.     file:write('')
  61.     file:close()
  62.     local lastmod = tonumber(string.sub(fs.lastModified('/tmp/' .. __name ..'.dt'), 1, -4)) + t_correction
  63.  
  64.     return lastmod
  65. end
  66. function info(type_x,mesg)
  67.     if gpu then
  68.         for i=1,#info_types do
  69.             if type_x == info_types[i][1] then
  70.                 gpu.setForeground(info_types[i][3])
  71.                 local time_now = os.date("%d %b %H:%M:%S",getTime())
  72.                 io.write(time_now)
  73.                 gpu.setForeground(text)
  74.                 io.write(" - " .. info_types[i][2] .. ": " .. mesg .. "\n")
  75.                 return true
  76.             end
  77.         end
  78.         return false
  79.     end
  80. end
  81. function init()
  82.     if component.isAvailable("gpu") then
  83.         gpu = component.gpu
  84.     else
  85.         gpu = nil
  86.     end
  87.    
  88.     if component.isAvailable("modem") then
  89.         m = component.modem
  90.     else
  91.         info("err","modem is required for the server.")
  92.         os.exit()
  93.     end
  94.    
  95.     if #args < 1 then
  96.         server_path = os.getenv("PWD")
  97.     else
  98.         server_path = args[1]
  99.     end
  100.    
  101.     if options.help or options.h  then
  102.         print("Server Core v0.1")
  103.         print("Usage:")
  104.         print("  sc [project path (default = ./)] [-d] [-h]")
  105.         print("Options:")
  106.         print("  -d,--debug = show information for debug")
  107.         print("  -h,--help  = show help")
  108.         os.exit()
  109.     end
  110.    
  111.     if not fs.exists(server_path) then
  112.         info("err","path does not exist.")
  113.         os.exit()
  114.     end
  115.    
  116.     if not fs.exists(fs.concat(fs.canonical(server_path), ".servercore")) then
  117.         info("err",".servercore file not found.")
  118.         os.exit()
  119.     end
  120.    
  121.     if not fs.isDirectory(server_path) then
  122.         info("err","path must be a folder.")
  123.         os.exit()
  124.     end
  125.    
  126.     info("info",__name .. " is initializing...")
  127.    
  128.     m.open(__port)
  129.     m.broadcast(__port,"client","1")
  130.    
  131.     package.preload["server_api"] = function () return api end
  132.    
  133.     for file in fs.list(server_path) do
  134.         if file ~= ".servercore" then
  135.             local path_to_load = fs.concat(fs.canonical(server_path), file)
  136.             info("info","loading module \"" .. path_to_load .. "\"...")
  137.             local ok,err = pcall(loadfile(path_to_load))
  138.             if not ok then
  139.                 info("err","error loading module \"" .. file .."\" - " .. err)
  140.             else
  141.                 info("info","loaded module \"" .. file .. "\".")
  142.             end
  143.         end
  144.     end
  145.    
  146.     info("ok",__name .. " is initialized.")
  147. end
  148. function main()
  149.     while running do
  150.         local e = {event.pull()}
  151.         for i=1,#listeners do
  152.             if listeners[i][1] == e[1] then
  153.                 local ok,err = pcall(listeners[i][2],e)
  154.                 if not ok then
  155.                     info("err","error occurred while processing the event " .. e[1] .. " - " .. err)
  156.                 end
  157.             end
  158.         end
  159.     end
  160.     info("info",__name .. " is stopping...")
  161.     os.sleep(0.5)
  162.     m.broadcast(__port,"client","2")
  163.     _G.sc = nil
  164.     info("ok",__name .. " is stopped.")
  165. end
  166.  
  167. _G.sc.info = info
  168. _G.sc.getTime = getTime
  169. function _G.sc.on(event_name,handler)
  170.     table.insert(listeners,{event_name,handler})
  171. end
  172. function _G.sc.test() print("hello world") end
  173.  
  174. -- main
  175. init()
  176. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement