henk2002

monitor_chunks

Apr 30th, 2022 (edited)
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.96 KB | None | 0 0
  1. --
  2. -- Copyright 2019 KaseiFR <kaseifr@gmail.com>
  3. --
  4. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  5. -- of this software and associated documentation files (the "Software"), to deal
  6. -- in the Software without restriction, including without limitation the rights
  7. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. -- copies of the Software, and to permit persons to whom the Software is
  9. -- furnished to do so, subject to the following conditions:
  10. --
  11. -- The above copyright notice and this permission notice shall be included in all
  12. -- copies or substantial portions of the Software.
  13. --
  14. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. -- SOFTWARE.
  21. --
  22.  
  23.  
  24. local component = require('component')
  25. local computer = require('computer')
  26. local coroutine = require('coroutine')
  27. local event = require('event')
  28. local filesystem = require('filesystem')
  29. local serialization = require('serialization')
  30. local thread = require('thread')
  31. local tty = require('tty')
  32. local unicode = require('unicode')
  33. local GUI = require('GUI')
  34. --local cuutil = require('cuutil')
  35. --local servicegui = require('servicegui')
  36.  
  37.  
  38. --data = {}
  39. --current_draw = {}
  40. --fullCheckInterval = 1
  41. --
  42. --serviceList = {
  43. -- AE_autocrafting = ae_auto_processor,
  44. -- chunkloader = chunkloader
  45. --}
  46. --
  47. --_serviceSet = Set(serviceList)
  48.  
  49. local chunkdata = {}
  50.  
  51. function main()
  52.  
  53. local resetBColor, resetFColor = tty.gpu().getBackground(), tty.gpu().getForeground()
  54.  
  55. local app = buildGui()
  56. app:draw(true)
  57.  
  58. -- Start some background tasks
  59. local background = {}
  60. table.insert(background, event.listen("key_up", function(key, address, char)
  61. if char == string.byte('q') then
  62. event.push('exit')
  63. end
  64. end))
  65. table.insert(background, event.listen("redraw", function(key)
  66. app:draw()
  67. end))
  68. table.insert(background, event.listen("net_msg", function(eventname, from, port, message)
  69. if port == 1122 then
  70. local mdata = serialization.unserialize(message)
  71. if mdata.service == "chunkloader" then
  72. updateLoaders(mdata.key, mdata.value)
  73. end
  74. end
  75. end))
  76. --table.insert(background, thread.create(failFast(mainloop)))
  77. table.insert(background, thread.create(failFast(function()
  78. app:start()
  79. end)))
  80.  
  81. -- Block until we receive the exit signal
  82. local _, err = event.pull("exit")
  83.  
  84. -- Cleanup
  85. app:stop()
  86.  
  87. for _, b in ipairs(background) do
  88. if type(b) == 'table' and b.kill then
  89. b:kill()
  90. else
  91. event.cancel(b)
  92. end
  93. end
  94.  
  95. tty.gpu().setBackground(resetBColor)
  96. tty.gpu().setForeground(resetFColor)
  97. tty.clear()
  98.  
  99. if err then
  100. io.stderr:write(err)
  101. os.exit(1)
  102. else
  103. os.exit(0)
  104. end
  105. end
  106.  
  107. function log(...)
  108. -- TODO: reserve a part of the screen for logs
  109. for i, v in ipairs { ... } do
  110. if i > 1 then
  111. io.stderr:write(' ')
  112. end
  113. io.stderr:write(tostring(v))
  114. end
  115. io.stderr:write('\n')
  116. end
  117.  
  118. function updateLoaders(key, value)
  119. chunkdata = value
  120. event.push('redraw')
  121. end
  122.  
  123. function logRam(msg)
  124. --free, total = computer.freeMemory(), computer.totalMemory()
  125. --log(msg, 'RAM', (total - free) * 100 / total, '%')
  126. end
  127.  
  128. function pretty(x)
  129. return serialization.serialize(x, true)
  130. end
  131.  
  132. function failFast(fn)
  133. return function(...)
  134. local res = table.pack(xpcall(fn, debug.traceback, ...))
  135. if not res[1] then
  136. event.push('exit', res[2])
  137. end
  138. return table.unpack(res, 2)
  139. end
  140. end
  141.  
  142. function yield(msg)
  143. --local gpu = tty.gpu()
  144. --local _, h = gpu.getViewport()
  145. --gpu.set(1, h, msg)
  146. os.sleep()
  147. end
  148.  
  149. function itemKey(item, withLabel)
  150. local key = item.name .. '$' .. math.floor(item.damage)
  151. if withLabel then
  152. --log('using label for', item.label)
  153. key = key .. '$' .. item.label
  154. end
  155. return key
  156. end
  157.  
  158. function equals(t1, t2)
  159. if t1 == t2 then
  160. return true
  161. end
  162. if type(t1) ~= type(t2) or type(t1) ~= 'table' then
  163. return false
  164. end
  165.  
  166. for k1, v1 in pairs(t1) do
  167. local v2 = t2[k1]
  168. if not equals(v1, v2) then
  169. return false
  170. end
  171. end
  172.  
  173. for k2, _ in pairs(t2) do
  174. if t1[k2] == nil then
  175. return false
  176. end
  177. end
  178.  
  179. return true
  180. end
  181.  
  182. function filter(array, predicate)
  183. local res = {}
  184. for _, v in ipairs(array) do
  185. if predicate(v) then
  186. table.insert(res, v)
  187. end
  188. end
  189. return res
  190. end
  191.  
  192. function contains(haystack, needle)
  193. if haystack == needle then
  194. return true
  195. end
  196. if type(haystack) ~= type(needle) or type(haystack) ~= 'table' then
  197. return false
  198. end
  199.  
  200. for k, v in pairs(needle) do
  201. if not contains(haystack[k], v) then
  202. return false
  203. end
  204. end
  205.  
  206. return true
  207. end
  208.  
  209. function override(object, method, fn)
  210. local super = object[method] or function()
  211. end
  212. object[method] = function(...)
  213. fn(super, ...)
  214. end
  215. end
  216.  
  217. function numberValidator(str)
  218. n = tonumber(str, 10)
  219. return n and math.floor(n) == n
  220. end
  221.  
  222.  
  223. -- Stay close to the 16 Minecraft colors in order to work on gold GPU/screen
  224. local C_BACKGROUND = 0x3C3C3C
  225. local C_STATUS_BAR = 0xC3C3C3
  226. local C_STATUS_TEXT = 0x1E1E1E
  227. local C_STATUS_PRESSED = 0xFFFF00
  228. local C_BADGE = 0xD2D2D2
  229. local C_BADGE_ERR = 0xFF4900 --0xFFB6FF
  230. local C_BADGE_BUSY = 0x336DFF
  231. local C_BADGE_SELECTED = 0xFFAA00
  232. local C_BADGE_TEXT = 0x1E1E1E
  233. local C_INPUT = 0xFFFFFF
  234. local C_INPUT_TEXT = 0x1E1E1E
  235. local C_SCROLLBAR = C_BADGE_SELECTED
  236. local C_SCROLLBAR_BACKGROUND = 0xFFFFFF
  237.  
  238. function buildGui()
  239. local app = GUI.application()
  240. local statusBar = app:addChild(GUI.container(1, 1, app.width, 1))
  241. local window = app:addChild(GUI.container(1, 1 + statusBar.height, app.width, app.height - statusBar.height))
  242.  
  243. window:addChild(GUI.panel(1, 1, window.width, window.height, C_BACKGROUND))
  244. local columns = math.floor(window.width / 8) + 1
  245. local rows = math.floor(window.height / 4)
  246.  
  247. local statusView = window:addChild(GUI.layout(1, 1, window.width - 7, window.height-1, columns, rows))
  248. for i = 1, columns do
  249. statusView:setAlignment(i, 1, GUI.ALIGNMENT_HORIZONTAL_CENTER, GUI.ALIGNMENT_VERTICAL_CENTER)
  250. statusView:setMargin(i, 1, 1, 1)
  251. end
  252. --statusView.showGrid = true
  253.  
  254. override(statusView, 'draw', function(super, self, ...)
  255. self.children = {}
  256.  
  257. local maxx, maxy, minx, miny = -math.huge, -math.huge, math.huge, math.huge
  258.  
  259. for _, v in pairs(chunkdata) do
  260. local x = math.tointeger(v.x)
  261. local y = math.tointeger(v.y)
  262. if x > maxx then
  263. maxx = x
  264. end
  265. if x < minx then
  266. minx = x
  267. end
  268. if y > maxy then
  269. maxy = y
  270. end
  271. if y < miny then
  272. miny = y
  273. end
  274. end
  275.  
  276. local totalcoloums = maxy - miny + 1
  277.  
  278. if totalcoloums > columns then
  279. print("error to many columns")
  280. end
  281.  
  282. local left_offset = math.floor((columns - totalcoloums) / 2)
  283. local yoffset = maxy + 1 + left_offset
  284. local xoffset = -minx + 2
  285.  
  286. for k, v in pairs(chunkdata) do
  287. local badge = GUI.container(1, 1, math.floor(self.width / columns - 1), 3)
  288. self:setPosition( (-1 * math.tointeger(v.y)) + yoffset, math.tointeger(v.x) + xoffset, self:addChild(badge))
  289. local badgecolor = v.status == 1 and 0x009900 or 0xff0000
  290. badge:addChild(GUI.panel(1, 1, badge.width, 3, badgecolor))
  291. local textcolor = v.y == "40" and 0x0066ff or C_BADGE_TEXT
  292. badge:addChild(GUI.label(2, 2, badge.width, 1, textcolor, v.x .. ":" .. v.y):setAlignment(GUI.ALIGNMENT_HORIZONTAL_CENTER, GUI.ALIGNMENT_VERTICAL_CENTER))
  293.  
  294. end
  295.  
  296.  
  297.  
  298. super(self, ...)
  299. end)
  300.  
  301.  
  302.  
  303. -- Status bar
  304. statusBar:addChild(GUI.panel(1, 1, statusBar.width, statusBar.height, C_STATUS_BAR))
  305. local statusText = statusBar:addChild(GUI.text(2, 1, C_STATUS_TEXT, ''))
  306. statusText.eventHandler = function(app, self)
  307. local good = 0
  308. local bad = 0
  309. for _,v in pairs(chunkdata) do
  310. if v.status == 1 then
  311. good = good + 1
  312. else
  313. bad = bad + 1
  314. end
  315. end
  316.  
  317. self.text = string.format('Chunkloaders online: %d/%d', good, good + bad)
  318. end
  319. statusText.eventHandler(app, statusText)
  320. statusBar:addChild(GUI.button(statusBar.width - 6, 1, 8, 1, C_STATUS_BAR, C_STATUS_TEXT, C_STATUS_BAR, C_STATUS_PRESSED, '[Exit]')).onTouch = function(app, object)
  321. event.push('exit')
  322. end
  323.  
  324. return app
  325. end
  326.  
  327. function attachScrollbar(obj)
  328. local width = (obj.width > 60) and 2 or 1
  329. obj.width = obj.width - width
  330. local bar = GUI.scrollBar(obj.x + obj.width, obj.y, width, obj.height, C_SCROLLBAR_BACKGROUND, C_SCROLLBAR,
  331. 0, 1, 0, 1, 4, false)
  332. obj.parent:addChild(bar)
  333. obj.scrollBar = bar
  334.  
  335. override(obj, 'eventHandler', function(super, app, self, key, ...)
  336. if key == 'scroll' then
  337. -- forward scrolls on the main object to the scrollbar
  338. bar.eventHandler(app, bar, key, ...)
  339. end
  340. super(app, self, key, ...)
  341. end)
  342.  
  343. return bar
  344. end
  345.  
  346. -- Start the program
  347. main()
  348.  
Add Comment
Please, Sign In to add comment