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 extendedPeripherals = require("extendedPeripherals")
- local inventory = peripheral.find("inventory")
- local inventoryManager = peripheral.find("inventoryManager")
- local inventorySide = extendedPeripherals.findPeripheralName("inventory")
- local function packageItems(items)
- local packagedItems = {}
- for _, v in pairs(items) do
- local count = packagedItems\[v.name\]
- if count == nil then
- count = 0
- end
- packagedItems\[v.name\] = count + v.count
- end
- return packagedItems
- end
- local function listItems(allItems, searchString)
- local matchingItems = {}
- for key, count in pairs(allItems) do
- if string.find(key, searchString, 1, true) ~= nil then
- matchingItems\[key\] = count
- end
- end
- return matchingItems
- end
- local function listStorage(arguments)
- return listItems(packageItems(inventory.list()), arguments.searchString)
- end
- local function listInventory(arguments)
- return listItems(packageItems(inventoryManager.list()), arguments.searchString)
- end
- local function transferItems(arguments)
- if arguments.mode == "add" then
- return inventoryManager.addItemToPlayer(inventorySide, { name = arguments.name, count = arguments.count })
- else
- return inventoryManager.removeItemFromPlayer(inventorySide, { name = arguments.name, count = arguments.count })
- end
- end
- local function space()
- local size = inventory.size()
- local used = 0
- for _, _ in pairs(inventory.list()) do
- used = used + 1
- end
- return { size, used }
- end
- local inventoryServer = simpleSocket.openServerProtocol("inventorySender")
- inventoryServer:registerRequestHandler("listStorage", listStorage)
- inventoryServer:registerRequestHandler("listInventory", listInventory)
- inventoryServer:registerRequestHandler("transferItems", transferItems)
- inventoryServer:registerRequestHandler("space", space)
- inventoryServer:run()$$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$$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$]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement