Advertisement
Guest User

ServerCore v0.2

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