AssossaGPB

ServerFrames Vr.3

Oct 15th, 2014
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.63 KB | None | 0 0
  1. local bedrockPath='/' 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 = shell.getRunningProgram() end
  2.  
  3. local program = Bedrock:Initialise()
  4.  
  5. --Run the code for when the sevrer starts up
  6. function Start()
  7.     --Get Conf
  8.     local file = fs.open("data/config","r")
  9.     conf = textutils.unserialize(file.readAll())
  10.     file.close()
  11.  
  12.     --Use Config
  13.     httpPro = false
  14.    
  15.     --Setup Running Variable
  16.     running = true
  17.  
  18.     if conf.templates == "http" then
  19.         httpPro = true
  20.     end
  21.  
  22.     --Load Server Code
  23.     local s,e = pcall(os.loadAPI, "data/code")
  24.  
  25.     if not s then --If Code Errored
  26.         print("Failure Loading Server Code.")
  27.         print("Please Debug Your Code.")
  28.         print("----")
  29.         print("Error:")
  30.         print(e)
  31.         error()
  32.     end
  33.  
  34.     if code.init == nil then
  35.         print("No Init Function Found In Server Code.")
  36.         error()
  37.     end
  38.  
  39.     if code.packet == nil then
  40.         print("No Packet Function Found In Server Code.")
  41.         error()
  42.     end
  43.      
  44.     --Wrap Modem
  45.     mod = peripheral.wrap(conf["side"])
  46.     mod.open(100)
  47.      
  48.     --Initialize Server Code
  49.     code.init()
  50. end
  51.  
  52. --Get Html Page Function
  53. local function getPage(page)
  54.   if page == nil then
  55.         page = "index"
  56.   end
  57.  
  58.   local file2 = fs.open("pages/"..page,"r")
  59.   local out = ""
  60.  
  61.   if file2 == nil then
  62.         out = "404 Page Not Found"
  63.   else
  64.         out = file2.readAll()
  65.   end
  66.  
  67.   file2.close()
  68.   return out
  69. end
  70.  
  71. --Send Packet Function
  72. local function send(to,data)
  73.   local pack = {}
  74.   local mod = peripheral.wrap("left")
  75.  
  76.   pack.to = to
  77.   pack.data = data
  78.   pack.protocol = "returnData"
  79.  
  80.   pack = textutils.serialize(pack)
  81.  
  82.   mod.transmit(100,100,pack)
  83. end
  84.  
  85. Start() --Initialize the server
  86.  
  87. program:RegisterEvent('modem_message',function(self, event, side, sc, rc, m, d)
  88.     if running then
  89.         if httpPro then
  90.             local pro = ""
  91.             if m.protocol ~= nil then
  92.                 pro = m.protocol
  93.             else
  94.                 pro = "http"
  95.             end
  96.            
  97.             if pro == "http" then
  98.                 if m.data.page then
  99.                     send(m.from,getPage(m.data.page))
  100.                 else
  101.                     pcall(send,m.from,getPage(m.data))
  102.                 end
  103.             end
  104.         end
  105.  
  106.         code.packet(m)
  107.     end
  108. end)
  109.  
  110. program:Run(function()
  111.    
  112. end)
Advertisement
Add Comment
Please, Sign In to add comment