SHOW:
|
|
- or go back to the newest paste.
| 1 | --[[ | |
| 2 | Server Core v0.2.1 | |
| 3 | Author: LeshaInc | |
| 4 | Date: 10 November 2015 16:22 | |
| 5 | - | Licenze: MIT |
| 5 | + | Licenze: Creative Commons «Attribution-NonCommercial-NoDerivatives» («Атрибуция — Некоммерческое использование — Без производных произведений») 4.0 Всемирная. |
| 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 | ||
| 48 | -- functions | |
| 49 | listeners[1] = {"key_down",function (_,_,k1,k2)
| |
| 50 | if keyboard.isControlDown() and keyboard.isKeyDown(46) then | |
| 51 | info("info","interrupted server.")
| |
| 52 | running = false | |
| 53 | end | |
| 54 | end} | |
| 55 | ||
| 56 | function getTime() | |
| 57 | local file = io.open('/tmp/' .. __name ..'.dt', 'w')
| |
| 58 | file:write('')
| |
| 59 | file:close() | |
| 60 | local lastmod = tonumber(string.sub(fs.lastModified('/tmp/' .. __name ..'.dt'), 1, -4)) + t_correction
| |
| 61 | ||
| 62 | return lastmod | |
| 63 | end | |
| 64 | function info(type_x,mesg) | |
| 65 | if gpu and _debug then | |
| 66 | for i=1,#info_types do | |
| 67 | if type_x == info_types[i][1] then | |
| 68 | gpu.setForeground(info_types[i][3]) | |
| 69 | local time_now = os.date("%d %b %H:%M:%S",getTime())
| |
| 70 | io.write(time_now) | |
| 71 | gpu.setForeground(text) | |
| 72 | io.write(" - " .. info_types[i][2] .. ": " .. mesg .. "\n")
| |
| 73 | return true | |
| 74 | end | |
| 75 | end | |
| 76 | return false | |
| 77 | end | |
| 78 | end | |
| 79 | function init() | |
| 80 | if component.isAvailable("gpu") then
| |
| 81 | gpu = component.gpu | |
| 82 | else | |
| 83 | gpu = nil | |
| 84 | end | |
| 85 | ||
| 86 | if #args < 1 then | |
| 87 | server_path = os.getenv("PWD")
| |
| 88 | else | |
| 89 | server_path = args[1] | |
| 90 | end | |
| 91 | ||
| 92 | if options.help or options.h then | |
| 93 | print("Server Core v0.1")
| |
| 94 | print("Usage:")
| |
| 95 | print(" sc [project path (default = ./)] [-d] [-h]")
| |
| 96 | print("Options:")
| |
| 97 | print(" -d,--nodebug = do not show information for debug")
| |
| 98 | print(" -h,--help = show help")
| |
| 99 | print(" -i,--ignore-servercore = ignore the check for file .servercore")
| |
| 100 | os.exit() | |
| 101 | end | |
| 102 | ||
| 103 | if options.nodebug or options.d then | |
| 104 | _debug = false | |
| 105 | end | |
| 106 | ||
| 107 | if not fs.exists(server_path) then | |
| 108 | info("err","path does not exist.")
| |
| 109 | os.exit() | |
| 110 | end | |
| 111 | ||
| 112 | if options["ignore-servercore"] or options.i then | |
| 113 | -- | |
| 114 | else | |
| 115 | if not fs.exists(fs.concat(fs.canonical(server_path), ".servercore")) then | |
| 116 | info("err",".servercore file not found.")
| |
| 117 | os.exit() | |
| 118 | end | |
| 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 | package.preload["server_api"] = function () return api end | |
| 129 | ||
| 130 | for file in fs.list(server_path) do | |
| 131 | if file ~= ".servercore" and fs.isDirectory(file) == false then | |
| 132 | local path_to_load = fs.concat(fs.canonical(server_path), file) | |
| 133 | info("info","loading module \"" .. path_to_load .. "\"...")
| |
| 134 | local ok,err = pcall(loadfile(path_to_load)) | |
| 135 | if not ok then | |
| 136 | info("err","error loading module \"" .. file .."\" - " .. err)
| |
| 137 | else | |
| 138 | info("info","loaded module \"" .. file .. "\".")
| |
| 139 | end | |
| 140 | end | |
| 141 | end | |
| 142 | ||
| 143 | info("ok",__name .. " is initialized.")
| |
| 144 | end | |
| 145 | function main() | |
| 146 | while running do | |
| 147 | local e = {event.pull()}
| |
| 148 | for i=1,#listeners do | |
| 149 | if listeners[i][1] == e[1] then | |
| 150 | local ok,err = pcall(listeners[i][2],e) | |
| 151 | if not ok then | |
| 152 | info("err","error occurred while processing the event " .. e[1] .. " - " .. err)
| |
| 153 | end | |
| 154 | end | |
| 155 | end | |
| 156 | end | |
| 157 | info("info",__name .. " is stopping...")
| |
| 158 | os.sleep(0.5) | |
| 159 | _G.sc = nil | |
| 160 | info("ok",__name .. " is stopped.")
| |
| 161 | end | |
| 162 | ||
| 163 | _G.sc.info = info | |
| 164 | _G.sc.getTime = getTime | |
| 165 | function _G.sc.on(event_name,handler) | |
| 166 | table.insert(listeners,{event_name,handler})
| |
| 167 | end | |
| 168 | function _G.sc.test() print("hello world") end
| |
| 169 | ||
| 170 | -- main | |
| 171 | init() | |
| 172 | main() |