Advertisement
melzneni

inventoryTablet

Nov 9th, 2024 (edited)
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.59 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.  
  61. local inventorySender = simpleSocket.openClientProtocol("inventorySender")
  62.  
  63. local MODE_RECEIVE = 0
  64. local MODE_SEND = 1
  65.  
  66. local function listItems(searchString, mode)
  67. local response
  68. if mode == MODE_RECEIVE then
  69. response = inventorySender:request("listStorage", { searchString = searchString })
  70. else
  71. response = inventorySender:request("listInventory", { searchString = searchString })
  72. end
  73. local inventory = {}
  74. local c = 0
  75. for key, count in pairs(response) do
  76. table.insert(inventory, { key = key, count = count })
  77. c = c + 1
  78. end
  79. table.sort(inventory, function(iv)
  80. return iv.key
  81. end)
  82. return inventory, c
  83. end
  84.  
  85. local function clearTerminal()
  86. term.clear()
  87. term.setCursorPos(1, 1)
  88. end
  89.  
  90. local function transferItems(name, amount, mode)
  91. if mode == MODE_RECEIVE then
  92. return inventorySender:request("transferItems", { mode = "add", name = name, count = amount })
  93. else
  94. return inventorySender:request("transferItems", { mode = "remove", name = name, count = amount })
  95. end
  96. end
  97.  
  98. local function trimName(name)
  99. local i = string.find(name, ":", 1, true)
  100. if i == nil then
  101. return name
  102. else
  103. return string.sub(name, i + 1, #name)
  104. end
  105. end
  106.  
  107. local mode = MODE_RECEIVE
  108. local MAX_ITEM_COUNT = 10
  109. clearTerminal()
  110. while true do
  111. if mode == MODE_RECEIVE then
  112. term.write("receive > ")
  113. else
  114. term.write("send > ")
  115. end
  116.  
  117. local searchTerm = io.read()
  118. if searchTerm == "" then
  119. mode = 1 - mode
  120. clearTerminal()
  121. elseif searchTerm == "@space" then
  122. local space = inventorySender:request("space")
  123. local size = space\[1\]
  124. local used = space\[2\]
  125. clearTerminal()
  126. print(tostring(used) .. "/" .. tostring(size))
  127. else
  128. local selectables, numItems = listItems(searchTerm, mode)
  129. if numItems ~= 0 then
  130. for i = 1, math.min(numItems, MAX_ITEM_COUNT) do
  131. print(tostring(i) .. ":", trimName(selectables\[i\].key), tostring(selectables\[i\].count))
  132. end
  133. term.write("select > ")
  134. local indexPrompt = io.read()
  135. if indexPrompt == "" then
  136. clearTerminal()
  137. else
  138. local selectedIndex = tonumber(indexPrompt)
  139. if selectedIndex >= 1 and selectedIndex <= MAX_ITEM_COUNT then
  140. local selectedEntry = selectables\[selectedIndex\]
  141. clearTerminal()
  142. print(selectedEntry.key)
  143. print(tostring(selectedEntry.count) .. " available")
  144. term.write("amount > ")
  145. local amount = tonumber(io.read())
  146. local amountTransferred = transferItems(selectedEntry.key, amount, mode)
  147. clearTerminal()
  148. print("transferred " .. tostring(amountTransferred) .. " items")
  149. else
  150. clearTerminal()
  151. end
  152. end
  153. else
  154. clearTerminal()
  155. print("no result")
  156. end
  157. end
  158. end
  159.  
  160. $$simpleSocket.lua$$local ServerProtocol = {}
  161. ServerProtocol.__index = ServerProtocol
  162. local ClientProtocol = {}
  163. ClientProtocol.__index = ClientProtocol
  164. local public = {}
  165.  
  166. local extendedPeripherals = require("extendedPeripherals")
  167. local storageLib = require("storage")
  168. local storage = storageLib.openGlobalStorage("inventorySender")
  169.  
  170. local function loadOrPromptUserName()
  171. if storage.data.userName == nil then
  172. term.write("Network username: ")
  173. storage.data.userName = io.read()
  174. storage:save()
  175. end
  176. return storage.data.userName
  177. end
  178.  
  179. local protocolPrefix = loadOrPromptUserName()
  180.  
  181. rednet.open(extendedPeripherals.findPeripheralName("modem"))
  182.  
  183. function public.openClientProtocol(protocolName)
  184. protocolName = protocolPrefix .. "_" .. protocolName
  185. local obj = setmetatable({ }, ClientProtocol)
  186. obj.isServer = false
  187. obj.protocol = protocolName
  188. return obj
  189. end
  190.  
  191. function public.openServerProtocol(protocolName)
  192. protocolName = protocolPrefix .. "_" .. protocolName
  193. local obj = setmetatable({}, ServerProtocol)
  194. rednet.host(protocolName, tostring(os.getComputerID()))
  195. obj.isServer = true
  196. obj.protocol = protocolName
  197. obj.requestHandlers = {}
  198. return obj
  199. end
  200.  
  201. local function checkProtocolType(protocol, expectServer)
  202. if protocol.isServer ~= expectServer then
  203. if expectServer then
  204. error("This operation can only be performed on a server")
  205. else
  206. error("This operation can only be performed on a client")
  207. end
  208. end
  209. end
  210.  
  211. function ServerProtocol:registerRequestHandler(actionName, handler)
  212. checkProtocolType(self, true)
  213. self.requestHandlers\[actionName\] = handler
  214. end
  215.  
  216. function ServerProtocol:run()
  217. checkProtocolType(self, true)
  218. while true do
  219. local client, request = rednet.receive(self.protocol)
  220. local action = request.action
  221. local handler = self.requestHandlers\[action\]
  222. if handler == nil then
  223. print("error: no handler registered")
  224. rednet.send(client, { action = action, error = true, message = "no handler registered for operation" }, self.protocol)
  225. else
  226. local success, result = pcall(handler, request.arguments)
  227. if success then
  228. print("success:", result)
  229. rednet.send(client, { action = action, error = false, result = result }, self.protocol)
  230. else
  231. print("error:", result)
  232. rednet.send(client, { action = action, error = true, message = result }, self.protocol)
  233. end
  234. end
  235. end
  236. end
  237.  
  238. function ClientProtocol:request(actionName, arguments)
  239. checkProtocolType(self, false)
  240. rednet.broadcast({ action = actionName, arguments = arguments }, self.protocol)
  241. while true do
  242. local serverId, response = rednet.receive(self.protocol, 20)
  243. if serverId == nil then
  244. error("Request expired")
  245. end
  246. if response.action == actionName then
  247. if response.error then
  248. error(response.message)
  249. else
  250. return response.result
  251. end
  252. end
  253. end
  254. end
  255.  
  256. return public$$storage.lua$$local public = {}
  257. local programStorageFolder = "programStorageFolder" .. "/" .. shell.getRunningProgram()
  258. local globalStorageFolder = "globalStorageFolder"
  259. local Storage = {}
  260. Storage.__index = Storage
  261.  
  262. if not fs.exists(programStorageFolder) then
  263. fs.makeDir(programStorageFolder)
  264. end
  265.  
  266. if not fs.exists(globalStorageFolder) then
  267. fs.makeDir(globalStorageFolder)
  268. end
  269.  
  270. local function loadStorage(path)
  271. local data = {}
  272. if fs.exists(path) then
  273. local f = fs.open(path, "r")
  274. data = textutils.unserialise(f.readAll())
  275. f.close()
  276. end
  277. return data
  278. end
  279.  
  280. local function saveStorage(path, data)
  281. local f = fs.open(path, "w")
  282. f.write(textutils.serialise(data, { compact = true }))
  283. f.close()
  284. end
  285.  
  286. function public.openStorage(name)
  287. local obj = setmetatable({}, Storage)
  288. obj.name = name
  289. obj.path = programStorageFolder .. "/" .. shell.getRunningProgram() .. "_" .. name .. ".data"
  290. obj.data = loadStorage(obj.path)
  291. return obj
  292. end
  293.  
  294. function public.openGlobalStorage(name)
  295. local obj = setmetatable({}, Storage)
  296. obj.name = name
  297. obj.path = globalStorageFolder .. "/" .. name .. ".data"
  298. obj.data = loadStorage(obj.path)
  299. return obj
  300. end
  301.  
  302. function Storage:save()
  303. saveStorage(self.path, self.data)
  304. end
  305.  
  306. return public$$extendedPeripherals.lua$$local public = {}
  307.  
  308. function public.findPeripheralName(type)
  309. for _, name in pairs(peripheral.getNames()) do
  310. for _, peripheralType in pairs(table.pack(peripheral.getType(name))) do
  311. if peripheralType == type then
  312. return name
  313. end
  314. end
  315. end
  316. end
  317.  
  318. return public$]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement