Advertisement
Guest User

server

a guest
Dec 18th, 2014
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.67 KB | None | 0 0
  1. local pkg = {
  2.   Views = {
  3.     [ "main.view" ] = "{BackgroundColour=blue, Children={2.0={Name=Credits, Text=Original by nitrogenfingers, Revamped by KeeganDeathman, TextColour=white, Type=Label, Width=100%, Y=3.0, Align=Center}, 1.0={Name=Master, Text=Master Server, TextColour=white, Type=Label, Width=100%, Align=Center}}}",
  4.   },
  5.   gui = "local bedrockPath='Build/' if OneOS then OneOS.LoadAPI('/System/API/Bedrock.lua', false)elseif fs.exists(bedrockPath..'/Bedrock')then os.loadAPI(bedrockPath..'/Bedrock')else if http then print('Downloading Bedrock...')local h=http.get('http://pastebin.com/raw.php?i=0MgKNqpN')if h then local f=fs.open(bedrockPath..'/Bedrock','w')f.write(h.readAll())f.close()h.close()os.loadAPI(bedrockPath..'/Bedrock')else error('Failed to download Bedrock. Is your internet working?') end else error('This program needs to download Bedrock to work. Please enable HTTP.') end end if Bedrock then Bedrock.BasePath = bedrockPath Bedrock.ProgramPath = '' end!@#&!@#&local program = Bedrock:Initialise()!@#&!@#&routers = {}!@#&servers = {}!@#&!@#&function addConnector(newid, parentid, connections)!@#&  if #connections==0 then return false end!@#&  for i=1,#connections do!@#&    local child = connections[i]!@#&    !@#&    --We remove extras here--!@#&    for j=1,#child.children do!@#&      if child.children[j].id == newid then!@#&        table.remove(child.children, j)!@#&        print(' removed.')!@#&      end!@#&    end!@#&    !@#&    if child.id == parentid then!@#&      table.insert(child.children, {!@#&        id = newid,!@#&        children = {}!@#&      })!@#&    !@#&      return true!@#&    end!@#&    !@#&    addConnector(newid, parentid, child.children)!@#&  end!@#&  return false!@#&end!@#&!@#&function removeConnector(oldid, connections)!@#&  if #connections==0 then return false end!@#&  for i=1,#connections do!@#&    local child = connections[i]!@#&    if child.id == oldid then!@#&      table.remove(connections, i)!@#&      return true!@#&    end!@#&    if removeConnector(oldid, child.children) then return true end!@#&  end!@#&  return false!@#&end!@#&!@#&function findConnector(existingid, connections)!@#&  if #connections==0 then return '' end!@#&  for i=1,#connections do!@#&    local child = connections[i]!@#&  if child.id == existingid then return child.id end!@#&  local path = findConnector(existingid, child.children)!@#&  if path ~= '' then return child.id..'-'..path end!@#&  end!@#&  return '';!@#&end!@#&!@#&function findTopConnector(existingid)!@#&  --print('Finding top connector, out of '..#routers..' routers')!@#&  --print('Checking for :'..existingid)!@#&  for i=1,#routers do!@#&    local route = routers[i]!@#& --print('At router '..i..': ID = '..route.id)!@#&   if route.id == existingid then !@#&   return route.id, '' !@#&  end!@#&    local path = findConnector(existingid, route.children) !@#&  if path ~= '' then return route.id, path end!@#&  end!@#&  --This should never happen--!@#&  print('A network error has occurred.')!@#&  return -1, ''!@#&end!@#&!@#&!@#&function processRequest(senderid, request)!@#&--Identical to the procedures found in routers!@#&  if request == '$HELLO' then!@#&  !@#&    rednet.send(senderid, '$HELLO')!@#&    return!@#&  elseif request == '$ADDME' then!@#&  table.insert(routers, {!@#&   id = senderid,!@#&      children = {}!@#& })!@#&  rednet.send(senderid, '$ACK')!@#&    return!@#&  end!@#&!@#&--If it's a client request, we funnel it down to the nearest router!@#&  if string.find(request, '$CLIENT') then!@#&    request = string.gsub(request, '$CLIENT', '')!@#&   local values = split(request, '-')!@#&  local routeid, dest = findTopConnector(tonumber(values[1]))!@#& if dest~='' then !@#&     request = dest..'-'..values[2]!@#&    else request = values[2] end!@#&    rednet.send(routeid, request)!@#&!@#&--If it's a server request, we forward it to the right server!@#&  elseif string.find(request, '$SERVER') then!@#&    local sent = false!@#&    request = string.gsub(request, '$SERVER', '')!@#&  for i=1,#servers do!@#&   local f, l = string.find(request, servers[i].name)!@#&      if f==1 then!@#&      request = string.sub(request, l+1)!@#&      rednet.send(servers[i].id, request)!@#&     !@#&        sent = true!@#&     break;!@#&    end!@#&   end!@#&    if not sent then  end!@#&--If it's for the master server we process it ourselves!@#&  elseif string.find(request, '$MASTER') then!@#&    request = string.gsub(request, '$MASTER', '')!@#&   !@#&    if string.find(request, '$ADDROUTER')==1 then!@#&     request = string.gsub(request, '$ADDROUTER', '')!@#&    local values = split(request, '-')!@#&      addConnector(tonumber(values[1]), tonumber(values[2]), routers)!@#&     local routeid, dest = findTopConnector(tonumber(values[1]))!@#&     rednet.send(routeid, dest..'-'..'$ACK')!@#&   elseif string.find(request, '$ADDSERVER')==1 then!@#&     request = string.gsub(request, '$ADDSERVER', '')!@#&    local values = split(request, '-')!@#&      --for i=1,#values do end!@#&    table.insert(servers, {!@#&       id = tonumber(values[1]),!@#&       name = values[2]!@#&      })!@#&      rednet.send(senderid, '$ACK')!@#& end!@#&--And, if it's adressed at no one we ignore it out of hand.!@#&  else!@#&  end!@#&end!@#&!@#&function split(str, pat)!@#&  local t = { }!@#&  local fpat = '(.-)'..pat!@#&  local last_end = 1!@#&  local s, e, cap = str:find(fpat, 1)!@#&  while s do!@#&    if s ~= 1 or cap ~= '' then!@#&      table.insert(t,cap)!@#&    end!@#&    last_end = e+1!@#&    s, e, cap = str:find(fpat, last_end)!@#&  end!@#&  if last_end <= #str then!@#&    cap = str:sub(last_end)!@#&    table.insert(t, cap)!@#&  end!@#&  return t!@#&end!@#&!@#&function tablelength(T)!@#&  local count = 0!@#&  for _ in pairs(T) do count = count + 1 end!@#&  return count!@#&end!@#&!@#&function main()!@#& local requestid, request = rednet.receive()!@#& term.setCursorPos(1,7)!@#&  term.clearLine()!@#&    print(\\\"Servers: \\\"..table.getn(servers))!@#&   term.setCursorPos(1,5)!@#&  term.clearLine()!@#&    print(\\\"Routers: \\\"..table.getn(routers))!@#&   term.setCursorPos(1,9)!@#&  term.clearLine()!@#&    print(\\\"Latest Request: \\\"..request)!@#&    processRequest(requestid, request)!@#&end!@#&!@#&program:Run(function()!@#& program:LoadView('main')!@#&    rednet.open('top')!@#&  while true do!@#&       main()!@#&  end!@#&end)",
  6. } local function makeFile(_path, _content) local file = fs.open(_path, "w") _content = _content:gsub("!@".."#&", "%\n") _content = textutils.unserialize(_content) file.write(_content) file.close() end local function makeFolder(_path, _content) fs.makeDir(_path) for k,v in pairs(_content) do if type(v) == "table" then makeFolder(_path .. "/" .. k, v) else makeFile(_path .. "/" .. k, v) end end end local sDest = shell.resolve( "masterserver" ) or "/" if sDest == "root" then sDest = "/" end local tPackage = pkg makeFolder(sDest, tPackage) print("Package Extracted to '" .. sDest .. "'!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement