Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function readFile(path)
- local file = fs.open(path, "r")
- local contents = file.readAll()
- file.close()
- return contents
- end
- local function writeFile(path, data)
- local file = fs.open(path, "w")
- file.write(data)
- file.close()
- end
- local function skipAfter(contents, pattern)
- local start = string.find(contents.data, pattern, contents.start, true)
- contents.start = start + string.len(pattern)
- end
- local function readEncodedBlock(contents)
- if string.sub(contents.data, contents.start, contents.start) ~= "$" then
- return nil
- end
- local escaped = false
- local unescapedContent = ""
- for i = contents.start + 1, #contents.data do
- local c = contents.data:sub(i, i)
- if escaped then
- unescapedContent = unescapedContent .. c
- escaped = false
- elseif c == "\\" then
- escaped = true
- elseif c == "$" then
- contents.start = i + 1
- return unescapedContent
- else
- unescapedContent = unescapedContent .. c
- end
- end
- end
- local thisFilePath = shell.getRunningProgram()
- local contents = { data = readFile(thisFilePath), start = 0 }
- skipAfter(contents, "===FI" .. "LES===")
- skipAfter(contents, "--[[")
- while true do
- local fileName = readEncodedBlock(contents)
- if fileName == nil then
- break
- end
- local fileContents = readEncodedBlock(contents)
- writeFile(fileName, fileContents)
- end
- --===FILES===
- --[[$startup$$local simpleSocket = require("simpleSocket")
- local inventorySender = simpleSocket.openClientProtocol("inventorySender")
- local MODE_RECEIVE = 0
- local MODE_SEND = 1
- local function listItems(searchString, mode)
- local response
- if mode == MODE_RECEIVE then
- response = inventorySender:request("listStorage", { searchString = searchString })
- else
- response = inventorySender:request("listInventory", { searchString = searchString })
- end
- local inventory = {}
- local c = 0
- for key, count in pairs(response) do
- table.insert(inventory, { key = key, count = count })
- c = c + 1
- end
- table.sort(inventory, function(iv)
- return iv.key
- end)
- return inventory, c
- end
- local function clearTerminal()
- term.clear()
- term.setCursorPos(1, 1)
- end
- local function transferItems(name, amount, mode)
- if mode == MODE_RECEIVE then
- return inventorySender:request("transferItems", { mode = "add", name = name, count = amount })
- else
- return inventorySender:request("transferItems", { mode = "remove", name = name, count = amount })
- end
- end
- local function trimName(name)
- local i = string.find(name, ":", 1, true)
- if i == nil then
- return name
- else
- return string.sub(name, i + 1, #name)
- end
- end
- local mode = MODE_RECEIVE
- local MAX_ITEM_COUNT = 10
- clearTerminal()
- while true do
- if mode == MODE_RECEIVE then
- term.write("receive > ")
- else
- term.write("send > ")
- end
- local searchTerm = io.read()
- if searchTerm == "" then
- mode = 1 - mode
- clearTerminal()
- elseif searchTerm == "@space" then
- local space = inventorySender:request("space")
- local size = space\[1\]
- local used = space\[2\]
- clearTerminal()
- print(tostring(used) .. "/" .. tostring(size))
- else
- local selectables, numItems = listItems(searchTerm, mode)
- if numItems ~= 0 then
- for i = 1, math.min(numItems, MAX_ITEM_COUNT) do
- print(tostring(i) .. ":", trimName(selectables\[i\].key), tostring(selectables\[i\].count))
- end
- term.write("select > ")
- local indexPrompt = io.read()
- if indexPrompt == "" then
- clearTerminal()
- else
- local selectedIndex = tonumber(indexPrompt)
- if selectedIndex >= 1 and selectedIndex <= MAX_ITEM_COUNT then
- local selectedEntry = selectables\[selectedIndex\]
- clearTerminal()
- print(selectedEntry.key)
- print(tostring(selectedEntry.count) .. " available")
- term.write("amount > ")
- local amount = tonumber(io.read())
- local amountTransferred = transferItems(selectedEntry.key, amount, mode)
- clearTerminal()
- print("transferred " .. tostring(amountTransferred) .. " items")
- else
- clearTerminal()
- end
- end
- else
- clearTerminal()
- print("no result")
- end
- end
- end
- $$simpleSocket.lua$$local ServerProtocol = {}
- ServerProtocol.__index = ServerProtocol
- local ClientProtocol = {}
- ClientProtocol.__index = ClientProtocol
- local public = {}
- local extendedPeripherals = require("extendedPeripherals")
- local storageLib = require("storage")
- local storage = storageLib.openGlobalStorage("inventorySender")
- local function loadOrPromptUserName()
- if storage.data.userName == nil then
- term.write("Network username: ")
- storage.data.userName = io.read()
- storage:save()
- end
- return storage.data.userName
- end
- local protocolPrefix = loadOrPromptUserName()
- rednet.open(extendedPeripherals.findPeripheralName("modem"))
- function public.openClientProtocol(protocolName)
- protocolName = protocolPrefix .. "_" .. protocolName
- local obj = setmetatable({ }, ClientProtocol)
- obj.isServer = false
- obj.protocol = protocolName
- return obj
- end
- function public.openServerProtocol(protocolName)
- protocolName = protocolPrefix .. "_" .. protocolName
- local obj = setmetatable({}, ServerProtocol)
- rednet.host(protocolName, tostring(os.getComputerID()))
- obj.isServer = true
- obj.protocol = protocolName
- obj.requestHandlers = {}
- return obj
- end
- local function checkProtocolType(protocol, expectServer)
- if protocol.isServer ~= expectServer then
- if expectServer then
- error("This operation can only be performed on a server")
- else
- error("This operation can only be performed on a client")
- end
- end
- end
- function ServerProtocol:registerRequestHandler(actionName, handler)
- checkProtocolType(self, true)
- self.requestHandlers\[actionName\] = handler
- end
- function ServerProtocol:run()
- checkProtocolType(self, true)
- while true do
- local client, request = rednet.receive(self.protocol)
- local action = request.action
- local handler = self.requestHandlers\[action\]
- if handler == nil then
- print("error: no handler registered")
- rednet.send(client, { action = action, error = true, message = "no handler registered for operation" }, self.protocol)
- else
- local success, result = pcall(handler, request.arguments)
- if success then
- print("success:", result)
- rednet.send(client, { action = action, error = false, result = result }, self.protocol)
- else
- print("error:", result)
- rednet.send(client, { action = action, error = true, message = result }, self.protocol)
- end
- end
- end
- end
- function ClientProtocol:request(actionName, arguments)
- checkProtocolType(self, false)
- rednet.broadcast({ action = actionName, arguments = arguments }, self.protocol)
- while true do
- local serverId, response = rednet.receive(self.protocol, 20)
- if serverId == nil then
- error("Request expired")
- end
- if response.action == actionName then
- if response.error then
- error(response.message)
- else
- return response.result
- end
- end
- end
- end
- return public$$storage.lua$$local public = {}
- local programStorageFolder = "programStorageFolder" .. "/" .. shell.getRunningProgram()
- local globalStorageFolder = "globalStorageFolder"
- local Storage = {}
- Storage.__index = Storage
- if not fs.exists(programStorageFolder) then
- fs.makeDir(programStorageFolder)
- end
- if not fs.exists(globalStorageFolder) then
- fs.makeDir(globalStorageFolder)
- end
- local function loadStorage(path)
- local data = {}
- if fs.exists(path) then
- local f = fs.open(path, "r")
- data = textutils.unserialise(f.readAll())
- f.close()
- end
- return data
- end
- local function saveStorage(path, data)
- local f = fs.open(path, "w")
- f.write(textutils.serialise(data, { compact = true }))
- f.close()
- end
- function public.openStorage(name)
- local obj = setmetatable({}, Storage)
- obj.name = name
- obj.path = programStorageFolder .. "/" .. shell.getRunningProgram() .. "_" .. name .. ".data"
- obj.data = loadStorage(obj.path)
- return obj
- end
- function public.openGlobalStorage(name)
- local obj = setmetatable({}, Storage)
- obj.name = name
- obj.path = globalStorageFolder .. "/" .. name .. ".data"
- obj.data = loadStorage(obj.path)
- return obj
- end
- function Storage:save()
- saveStorage(self.path, self.data)
- end
- return public$$extendedPeripherals.lua$$local public = {}
- function public.findPeripheralName(type)
- for _, name in pairs(peripheral.getNames()) do
- for _, peripheralType in pairs(table.pack(peripheral.getType(name))) do
- if peripheralType == type then
- return name
- end
- end
- end
- end
- return public$]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement