Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Better network boot host, by PaymentionOption --
- VERSION = 1.0
- -----------
- -- NOTE:
- -- All OS' must have a file titled with their name and another file titled: <osname>_version containing the version of the OS.
- -- Variables --
- local tOS_List = {} -- Holds all of the OS'.
- -- ^^ Format: tOS_List[n] = { Name, Version, Path }
- ---------------
- -- Drawing Functions --
- -----------------------
- -- File functions --
- function LoadOS_List() -- Load all of the OS' and their respective information from the OS directory.
- if fs.exists( "OS_Directory" ) and fs.isDir( "OS_Directory" ) then -- If the OS directory exists.
- local tOS = fs.list( "OS_Directory" ) -- Get the contents of the OS directory.
- if #tOS % 2 == 0 then -- If the amount of files in the OS directory is even.
- local File = nil -- Setup a variable to hold a file instance.
- local FileVersion = nil -- Setup a variable to hold the version of an OS.
- for i=1, #tOS, 2 do -- Loop through all of the files in the OS directory.
- File = fs.open( "OS_Directory/" .. tOS[i+1], "r" ) -- Open the current OS version file.
- FileVersion = File.readAll() -- Get the version.
- File.close() -- Close the file instance.
- tOS_List[#tOS_List+1] = { Name = tOS[i], Version = tonumber( FileVersion ), Path = "OS_Directory/" .. tOS[i] }
- -- ^^ Add the OS to the table.
- end
- else -- If there are too many version files over OS' or vice versa.
- error( "OS Directory format error." ) -- Throw an error.
- end
- else -- If the OS directory does not exist.
- error( "OS Directory not found." ) -- Throw an error.
- end
- end
- function GetOS_FromFile( Index ) -- Gets the OS from the passed index and returns it as a string.
- local File = fs.open( tOS_List[Index].Path, "r" ) -- Open the OS file in read mode.
- local FileContents = File.readAll() -- Get the OS from the file into a string.
- File.close() -- Close the file.
- return FileContents -- Return the OS that was read from the file.
- end
- --------------------
- -- Networking functions --
- function OpenExistantModem() -- Opens any modem that is found on the computer. Returns false if no modem was found.
- local tSides = rs.getSides()
- for i=1, #tSides do
- if peripheral.isPresent( tSides[i] ) and peripheral.getType( tSides[i] ) == "modem" then
- rednet.open( tSides[i] )
- return true
- end
- end
- return false
- end
- function RespondToPings() -- Waits and responds to any ping received.
- local sender, message = rednet.receive() -- Wait forever for a message.
- if message == "PingingYou" then -- If the message was a ping.
- rednet.send( sender, "PingingYouBack" ) -- Respond to the ping to inform the user of our communication.
- return true, sender -- Return true and the sender that pinged us.
- else -- If the message was not a ping.
- return false, nil -- Return false and nil for the sender did not send a valid request.
- end
- end
- function SendOS_List( Reciever ) -- Sends the list of available OS'.
- local sOS_List = "" -- Setup a variable to hold all of the OS' in a string.
- for i=1, #tOS_List do -- Iterate through the OS List table.
- sOS_List = sOS_List .. tOS_List[i].Name .. "@#DS!" .. tostring( tOS_List[i].Version ) .. "@#DS!"
- -- ^^ Create the OS List string and build upon it very iteration.
- end
- rednet.send( Receiver, sOS_List ) -- Send the list to the intended receiver.
- end
- function SendOS( Receiver, Index ) -- Packages and sends the requested OS to the requester.
- local OS = GetOS_FromFile( Index ) -- Get the OS.
- if OS then -- If the OS is not nil.
- rednet.send( Receiver, tostring( OS ) ) -- Send the OS.
- return true -- Report a success.
- else -- If the OS is nil.
- return false -- Report a failure.
- end
- end
- --------------------------
- -- Main Loop --
- function MainLoop()
- local bPinged, Sender = RespondToPings()
- if bPinged then
- local sender, request = rednet.receive( 0.4 )
- -- Handle the request --
- if sender == Sender then
- if request == "AllPlease" then -- Get the list of OS'.
- SendOS_List( Sender )
- elseif string.find( request, "OS_Number_" ) then -- Request a specific OS.
- SendOS( Sender, tonumber( string.sub( request, 11, string.len( request ) ) ) )
- end
- else
- return
- end
- ------------------------
- else
- return
- end
- end
- ---------------
- -- The program --
- if not OpenExistantModem() then
- error( "No modem found." )
- end
- LoadOS_List() -- Load all of the OS' in the OS directory.
- while true do
- MainLoop()
- end
- -----------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement