Advertisement
melzneni

inventorySender

Nov 9th, 2024 (edited)
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.38 KB | None | 0 0
  1. local function readFile(path)
  2. local file = fs.open(path, "r")
  3. local contents = file.readAll()
  4. file.close()
  5. return contents
  6. end
  7.  
  8. local function writeFile(path, data)
  9. local file = fs.open(path, "w")
  10. file.write(data)
  11. file.close()
  12. end
  13.  
  14. local function skipAfter(contents, pattern)
  15. local start = string.find(contents.data, pattern, contents.start, true)
  16. contents.start = start + string.len(pattern)
  17. end
  18.  
  19. local function readEncodedBlock(contents)
  20. if string.sub(contents.data, contents.start, contents.start) ~= "$" then
  21. return nil
  22. end
  23. local escaped = false
  24. local unescapedContent = ""
  25. for i = contents.start + 1, #contents.data do
  26. local c = contents.data:sub(i, i)
  27. if escaped then
  28. unescapedContent = unescapedContent .. c
  29. escaped = false
  30. elseif c == "\\" then
  31. escaped = true
  32. elseif c == "$" then
  33. contents.start = i + 1
  34. return unescapedContent
  35. else
  36. unescapedContent = unescapedContent .. c
  37. end
  38. end
  39. end
  40.  
  41. local thisFilePath = shell.getRunningProgram()
  42.  
  43. local contents = { data = readFile(thisFilePath), start = 0 }
  44.  
  45. skipAfter(contents, "===FI" .. "LES===")
  46. skipAfter(contents, "--[[")
  47.  
  48. while true do
  49. local fileName = readEncodedBlock(contents)
  50. if fileName == nil then
  51. break
  52. end
  53. local fileContents = readEncodedBlock(contents)
  54.  
  55. writeFile(fileName, fileContents)
  56. end
  57.  
  58. --===FILES===
  59. --[[$startup$$local simpleSocket = require("simpleSocket")
  60. local extendedPeripherals = require("extendedPeripherals")
  61.  
  62. local inventory = peripheral.find("inventory")
  63. local inventoryManager = peripheral.find("inventoryManager")
  64.  
  65. local inventorySide = extendedPeripherals.findPeripheralName("inventory")
  66.  
  67. local function packageItems(items)
  68. local packagedItems = {}
  69. for _, v in pairs(items) do
  70. local count = packagedItems\[v.name\]
  71. if count == nil then
  72. count = 0
  73. end
  74. packagedItems\[v.name\] = count + v.count
  75. end
  76. return packagedItems
  77. end
  78.  
  79. local function listItems(allItems, searchString)
  80. local matchingItems = {}
  81. for key, count in pairs(allItems) do
  82. if string.find(key, searchString, 1, true) ~= nil then
  83. matchingItems\[key\] = count
  84. end
  85. end
  86. return matchingItems
  87. end
  88.  
  89. local function listStorage(arguments)
  90. return listItems(packageItems(inventory.list()), arguments.searchString)
  91. end
  92.  
  93. local function listInventory(arguments)
  94. return listItems(packageItems(inventoryManager.list()), arguments.searchString)
  95. end
  96.  
  97. local function transferItems(arguments)
  98. if arguments.mode == "add" then
  99. return inventoryManager.addItemToPlayer(inventorySide, { name = arguments.name, count = arguments.count })
  100. else
  101. return inventoryManager.removeItemFromPlayer(inventorySide, { name = arguments.name, count = arguments.count })
  102. end
  103. end
  104.  
  105. local function space()
  106. local size = inventory.size()
  107. local used = 0
  108. for _, _ in pairs(inventory.list()) do
  109. used = used + 1
  110. end
  111. return { size, used }
  112. end
  113.  
  114. local inventoryServer = simpleSocket.openServerProtocol("inventorySender")
  115. inventoryServer:registerRequestHandler("listStorage", listStorage)
  116. inventoryServer:registerRequestHandler("listInventory", listInventory)
  117. inventoryServer:registerRequestHandler("transferItems", transferItems)
  118. inventoryServer:registerRequestHandler("space", space)
  119. inventoryServer:run()$$extendedPeripherals.lua$$local public = {}
  120.  
  121. function public.findPeripheralName(type)
  122. for _, name in pairs(peripheral.getNames()) do
  123. for _, peripheralType in pairs(table.pack(peripheral.getType(name))) do
  124. if peripheralType == type then
  125. return name
  126. end
  127. end
  128. end
  129. end
  130.  
  131. return public$$simpleSocket.lua$$local ServerProtocol = {}
  132. ServerProtocol.__index = ServerProtocol
  133. local ClientProtocol = {}
  134. ClientProtocol.__index = ClientProtocol
  135. local public = {}
  136.  
  137. local extendedPeripherals = require("extendedPeripherals")
  138. local storageLib = require("storage")
  139. local storage = storageLib.openGlobalStorage("inventorySender")
  140.  
  141. local function loadOrPromptUserName()
  142. if storage.data.userName == nil then
  143. term.write("Network username: ")
  144. storage.data.userName = io.read()
  145. storage:save()
  146. end
  147. return storage.data.userName
  148. end
  149.  
  150. local protocolPrefix = loadOrPromptUserName()
  151.  
  152. rednet.open(extendedPeripherals.findPeripheralName("modem"))
  153.  
  154. function public.openClientProtocol(protocolName)
  155. protocolName = protocolPrefix .. "_" .. protocolName
  156. local obj = setmetatable({ }, ClientProtocol)
  157. obj.isServer = false
  158. obj.protocol = protocolName
  159. return obj
  160. end
  161.  
  162. function public.openServerProtocol(protocolName)
  163. protocolName = protocolPrefix .. "_" .. protocolName
  164. local obj = setmetatable({}, ServerProtocol)
  165. rednet.host(protocolName, tostring(os.getComputerID()))
  166. obj.isServer = true
  167. obj.protocol = protocolName
  168. obj.requestHandlers = {}
  169. return obj
  170. end
  171.  
  172. local function checkProtocolType(protocol, expectServer)
  173. if protocol.isServer ~= expectServer then
  174. if expectServer then
  175. error("This operation can only be performed on a server")
  176. else
  177. error("This operation can only be performed on a client")
  178. end
  179. end
  180. end
  181.  
  182. function ServerProtocol:registerRequestHandler(actionName, handler)
  183. checkProtocolType(self, true)
  184. self.requestHandlers\[actionName\] = handler
  185. end
  186.  
  187. function ServerProtocol:run()
  188. checkProtocolType(self, true)
  189. while true do
  190. local client, request = rednet.receive(self.protocol)
  191. local action = request.action
  192. local handler = self.requestHandlers\[action\]
  193. if handler == nil then
  194. print("error: no handler registered")
  195. rednet.send(client, { action = action, error = true, message = "no handler registered for operation" }, self.protocol)
  196. else
  197. local success, result = pcall(handler, request.arguments)
  198. if success then
  199. print("success:", result)
  200. rednet.send(client, { action = action, error = false, result = result }, self.protocol)
  201. else
  202. print("error:", result)
  203. rednet.send(client, { action = action, error = true, message = result }, self.protocol)
  204. end
  205. end
  206. end
  207. end
  208.  
  209. function ClientProtocol:request(actionName, arguments)
  210. checkProtocolType(self, false)
  211. rednet.broadcast({ action = actionName, arguments = arguments }, self.protocol)
  212. while true do
  213. local serverId, response = rednet.receive(self.protocol, 20)
  214. if serverId == nil then
  215. error("Request expired")
  216. end
  217. if response.action == actionName then
  218. if response.error then
  219. error(response.message)
  220. else
  221. return response.result
  222. end
  223. end
  224. end
  225. end
  226.  
  227. return public$$storage.lua$$local public = {}
  228. local programStorageFolder = "programStorageFolder" .. "/" .. shell.getRunningProgram()
  229. local globalStorageFolder = "globalStorageFolder"
  230. local Storage = {}
  231. Storage.__index = Storage
  232.  
  233. if not fs.exists(programStorageFolder) then
  234. fs.makeDir(programStorageFolder)
  235. end
  236.  
  237. if not fs.exists(globalStorageFolder) then
  238. fs.makeDir(globalStorageFolder)
  239. end
  240.  
  241. local function loadStorage(path)
  242. local data = {}
  243. if fs.exists(path) then
  244. local f = fs.open(path, "r")
  245. data = textutils.unserialise(f.readAll())
  246. f.close()
  247. end
  248. return data
  249. end
  250.  
  251. local function saveStorage(path, data)
  252. local f = fs.open(path, "w")
  253. f.write(textutils.serialise(data, { compact = true }))
  254. f.close()
  255. end
  256.  
  257. function public.openStorage(name)
  258. local obj = setmetatable({}, Storage)
  259. obj.name = name
  260. obj.path = programStorageFolder .. "/" .. shell.getRunningProgram() .. "_" .. name .. ".data"
  261. obj.data = loadStorage(obj.path)
  262. return obj
  263. end
  264.  
  265. function public.openGlobalStorage(name)
  266. local obj = setmetatable({}, Storage)
  267. obj.name = name
  268. obj.path = globalStorageFolder .. "/" .. name .. ".data"
  269. obj.data = loadStorage(obj.path)
  270. return obj
  271. end
  272.  
  273. function Storage:save()
  274. saveStorage(self.path, self.data)
  275. end
  276.  
  277. return public$]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement