henk2002

mon

Apr 29th, 2022 (edited)
518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.02 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. require("ae_auto_processor")
  37. require("chunkloaders")
  38.  
  39. data = {}
  40. current_draw = {}
  41. fullCheckInterval = 1
  42.  
  43. serviceList = {
  44.     AE_autocrafting = ae_auto_processor,
  45.     chunkloader = chunkloader
  46. }
  47.  
  48. _serviceSet = Set(serviceList)
  49.  
  50.  
  51. function main()
  52.  
  53.     local resetBColor, resetFColor = tty.gpu().getBackground(), tty.gpu().getForeground()
  54.  
  55.     setupData()
  56.  
  57.     local app = buildGui()
  58.     app:draw(true)
  59.  
  60.     -- Start some background tasks
  61.     local background = {}
  62.     table.insert(background, event.listen("key_up", function (key, address, char)
  63.         if char == string.byte('q') then
  64.             event.push('exit')
  65.         end
  66.     end))
  67.     table.insert(background, event.listen("redraw", function (key) app:draw() 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.             callService(mdata.service, mdata.key, mdata.value)
  72.         end
  73.     end))
  74.     table.insert(background, thread.create(failFast(mainloop)))
  75.     table.insert(background, thread.create(failFast(function() app:start() end)))
  76.  
  77.     -- Block until we receive the exit signal
  78.     local _, err = event.pull("exit")
  79.  
  80.     -- Cleanup
  81.     app:stop()
  82.  
  83.     for _, b in ipairs(background) do
  84.         if type(b) == 'table' and b.kill then
  85.             b:kill()
  86.         else
  87.             event.cancel(b)
  88.         end
  89.     end
  90.  
  91.     tty.gpu().setBackground(resetBColor)
  92.     tty.gpu().setForeground(resetFColor)
  93.     tty.clear()
  94.  
  95.     if err then
  96.         io.stderr:write(err)
  97.         os.exit(1)
  98.     else
  99.         os.exit(0)
  100.     end
  101. end
  102.  
  103. function log(...)
  104.     -- TODO: reserve a part of the screen for logs
  105.     for i, v in ipairs{...} do
  106.         if i > 1 then io.stderr:write(' ') end
  107.         io.stderr:write(tostring(v))
  108.     end
  109.     io.stderr:write('\n')
  110. end
  111.  
  112. function logRam(msg)
  113.     --free, total = computer.freeMemory(), computer.totalMemory()
  114.     --log(msg, 'RAM', (total - free) * 100 / total, '%')
  115. end
  116.  
  117. function pretty(x)
  118.     return serialization.serialize(x, true)
  119. end
  120.  
  121. function failFast(fn)
  122.     return function(...)
  123.         local res = table.pack(xpcall(fn, debug.traceback, ...))
  124.         if not res[1] then
  125.             event.push('exit', res[2])
  126.         end
  127.         return table.unpack(res, 2)
  128.     end
  129. end
  130.  
  131.  
  132.  
  133. function setupData()
  134.  
  135.     for k, _ in pairs(serviceList) do
  136.         print(k)
  137.         data[k] = {}
  138.         current_draw[k] = ServiceElement:new()
  139.     end
  140. end
  141.  
  142.  
  143. function callService(service, key, value)
  144.     if not _serviceSet[service] then
  145.         print("unkown service: " .. service)
  146.         return
  147.     end
  148.     serviceList[service](key, value)
  149. end
  150.  
  151. function updateall()
  152.     for _, v in pairs(serviceList) do
  153.         v("update", "update")
  154.     end
  155. end
  156.  
  157. -- Main loop --
  158.  
  159. function mainloop()
  160.     while true do
  161.         local e1, e2 = event.pull(fullCheckInterval, 'mainloop')
  162.         logRam('loop')
  163.         --log('AE2 loop in')
  164.         updateall()
  165.         --log('AE2 loop out')
  166.         event.push('redraw')
  167.     end
  168. end
  169.  
  170.  
  171.  
  172.  
  173. function yield(msg)
  174.     --local gpu = tty.gpu()
  175.     --local _, h = gpu.getViewport()
  176.     --gpu.set(1, h, msg)
  177.     os.sleep()
  178. end
  179.  
  180.  
  181.  
  182. function itemKey(item, withLabel)
  183.     local key = item.name .. '$' .. math.floor(item.damage)
  184.     if withLabel then
  185.         --log('using label for', item.label)
  186.         key = key .. '$' .. item.label
  187.     end
  188.     return key
  189. end
  190.  
  191.  
  192.  
  193. function equals(t1, t2)
  194.     if t1 == t2 then return true end
  195.     if type(t1) ~= type(t2) or type(t1) ~= 'table' then return false end
  196.  
  197.     for k1, v1 in pairs(t1) do
  198.         local v2 = t2[k1]
  199.         if not equals(v1, v2) then return false end
  200.     end
  201.  
  202.     for k2, _ in pairs(t2) do
  203.         if t1[k2] == nil then return false end
  204.     end
  205.  
  206.     return true
  207. end
  208.  
  209. function filter(array, predicate)
  210.     local res = {}
  211.     for _, v in ipairs(array) do
  212.         if predicate(v) then table.insert(res, v) end
  213.     end
  214.     return res
  215. end
  216.  
  217. function contains(haystack, needle)
  218.     if haystack == needle then return true end
  219.     if type(haystack) ~= type(needle) or type(haystack) ~= 'table' then return false end
  220.  
  221.     for k, v in pairs(needle) do
  222.         if not contains(haystack[k], v) then return false end
  223.     end
  224.  
  225.     return true
  226. end
  227.  
  228.  
  229.  
  230. function override(object, method, fn)
  231.     local super = object[method] or function() end
  232.     object[method] = function(...)
  233.         fn(super, ...)
  234.     end
  235. end
  236.  
  237. function numberValidator(str)
  238.     n = tonumber(str, 10)
  239.     return n and math.floor(n) == n
  240. end
  241.  
  242. function getStatus()
  243.     local cstatus = {}
  244.     for k, _ in pairs(Status) do
  245.         cstatus[k] = 0
  246.     end
  247.     for _, v in pairs(current_draw) do
  248.         cstatus[v.status] =  cstatus[v.status] + 1
  249.     end
  250.     return cstatus
  251. end
  252.  
  253. -- Stay close to the 16 Minecraft colors in order to work on gold GPU/screen
  254. local C_BACKGROUND = 0x3C3C3C
  255. local C_STATUS_BAR = 0xC3C3C3
  256. local C_STATUS_TEXT = 0x1E1E1E
  257. local C_STATUS_PRESSED = 0xFFFF00
  258. local C_BADGE = 0xD2D2D2
  259. local C_BADGE_ERR = 0xFF4900 --0xFFB6FF
  260. local C_BADGE_BUSY = 0x336DFF
  261. local C_BADGE_SELECTED = 0xFFAA00
  262. local C_BADGE_TEXT = 0x1E1E1E
  263. local C_INPUT = 0xFFFFFF
  264. local C_INPUT_TEXT = 0x1E1E1E
  265. local C_SCROLLBAR = C_BADGE_SELECTED
  266. local C_SCROLLBAR_BACKGROUND = 0xFFFFFF
  267.  
  268. function buildGui()
  269.     local app = GUI.application()
  270.     local statusBar = app:addChild(GUI.container(1, 1, app.width, 1))
  271.     local window = app:addChild(GUI.container(1, 1 + statusBar.height, app.width, app.height - statusBar.height))
  272.  
  273.     window:addChild(GUI.panel(1, 1, window.width, window.height, C_BACKGROUND))
  274.     local columns = math.floor(window.width / 60) + 1
  275.  
  276.     local statusView = window:addChild(GUI.layout(1, 1, window.width - 1, window.height, columns, 1))
  277.     for i = 1, columns do
  278.         statusView:setAlignment(i, 1, GUI.ALIGNMENT_HORIZONTAL_CENTER, GUI.ALIGNMENT_VERTICAL_TOP)
  279.         statusView:setMargin(i, 1, .5, 1)
  280.     end
  281.  
  282.     override(statusView, 'draw', function(super, self, ...)
  283.         self.children = {}
  284.         local added = 0
  285.         for service, element in pairs(current_draw) do
  286.             local badge = GUI.container(1, 1, math.floor(self.width / columns - 1), 4)
  287.             self:setPosition(1 + added % columns, 1, self:addChild(badge))
  288.             badge:addChild(GUI.panel(1, 1, badge.width, 4, element.bdgclr))
  289.             if tostring(element.line1) == "" then
  290.                 element.line1 = tostring(service)
  291.             end
  292.             badge:addChild(GUI.label(2, 2, badge.width, 1, C_BADGE_TEXT, tostring(element.line1)):setAlignment(GUI.ALIGNMENT_HORIZONTAL_CENTER, GUI.ALIGNMENT_VERTICAL_CENTER)) -- TODO: include the item icon ?
  293.             badge:addChild(GUI.label(2, 3, badge.width, 1, C_BADGE_TEXT, tostring(element.line2)):setAlignment(GUI.ALIGNMENT_HORIZONTAL_CENTER, GUI.ALIGNMENT_VERTICAL_CENTER))
  294.             badge:addChild(GUI.label(2, 4, badge.width, 1,C_BADGE_TEXT, tostring(element.line3)):setAlignment(GUI.ALIGNMENT_HORIZONTAL_CENTER, GUI.ALIGNMENT_VERTICAL_CENTER))
  295.             added = added + 1
  296.  
  297.         end
  298.  
  299.         super(self, ...)
  300.     end)
  301.  
  302.  
  303.  
  304.     -- Status bar
  305.     statusBar:addChild(GUI.panel(1, 1, statusBar.width, statusBar.height, C_STATUS_BAR))
  306.     local statusText = statusBar:addChild(GUI.text(2, 1, C_STATUS_TEXT, ''))
  307.     statusText.eventHandler = function(app, self)
  308.         status = getStatus()
  309.         self.text = string.format('Services: %d offline  %d healthy  %d busy  %d errored',
  310.             status[Status.OFFLINE], status[Status.ONLINE], status[Status.WORKING], status[Status.ERROR])
  311.     end
  312.     statusText.eventHandler(app, statusText)
  313.     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)
  314.         event.push('exit')
  315.     end
  316.  
  317.     return app
  318. end
  319.  
  320. function attachScrollbar(obj)
  321.     local width = (obj.width > 60) and 2 or 1
  322.     obj.width = obj.width - width
  323.     local bar = GUI.scrollBar(obj.x+obj.width, obj.y, width, obj.height, C_SCROLLBAR_BACKGROUND, C_SCROLLBAR,
  324.             0, 1, 0, 1, 4, false)
  325.     obj.parent:addChild(bar)
  326.     obj.scrollBar = bar
  327.  
  328.     override(obj, 'eventHandler', function (super, app, self, key, ...)
  329.         if key == 'scroll' then -- forward scrolls on the main object to the scrollbar
  330.             bar.eventHandler(app, bar, key, ...)
  331.         end
  332.         super(app, self, key, ...)
  333.     end)
  334.  
  335.     return bar
  336. end
  337.  
  338. -- Start the program
  339. main()
  340.  
Add Comment
Please, Sign In to add comment