Ledger Nano X - The secure hardware wallet
SHARE
TWEET

altar_exe.lua

a guest Apr 9th, 2020 250 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local c = require('component')
  2. local io = require("io")
  3. local serialization = require('serialization')
  4. local ser = serialization.serialize
  5. local deser = serialization.unserialize
  6. local event = require('event')
  7. local term = require('term')
  8.  
  9. local WEST = 4
  10. local SOUTH = 3
  11. local NORTH = 2
  12. local BOTTOM = 0
  13.  
  14. local active = 'none'
  15. local function debug(tbl)
  16.     if (tbl[1] == nil) then
  17.         for k,v in pairs(tbl) do
  18.             if (type(v) == 'table') then
  19.                 debug(v)
  20.             else
  21.                 print (k,v)
  22.             end
  23.         end
  24.     else
  25.         for i,v in ipairs(tbl) do
  26.             if (type(v) == 'table') then
  27.                 debug(v)
  28.             else
  29.                 print (i,v)
  30.             end
  31.         end
  32.     end
  33. end
  34.  
  35. function split(s, delimiter)
  36.     result = {};
  37.     for match in (s..delimiter):gmatch("(.-)"..delimiter) do
  38.         table.insert(result, match);
  39.     end
  40.     return result;
  41. end
  42.  
  43. local function getRedstoneIOAddresses()
  44.   local tmp = {}
  45.   for addr in pairs(c.list('redstone')) do
  46.     table.insert(tmp, addr)
  47.   end
  48.   return tmp
  49. end
  50.  
  51. local rs_list = getRedstoneIOAddresses()
  52. table.sort(rs_list)
  53.  
  54. local SKIP = rs_list[1]
  55.  
  56. local args = {...}
  57.  
  58. local function activate()
  59.   local rs = c.proxy(SKIP)
  60.   rs.setOutput(SOUTH, 1)
  61.   os.sleep(5)
  62.   rs.setOutput(SOUTH, 0)
  63. end
  64.  
  65. local function dropRelays(number)
  66.   local rs = c.proxy(SKIP)
  67.   for i=1, number do
  68.     rs.setOutput(BOTTOM, 1)
  69.     os.sleep(0.5)
  70.     rs.setOutput(BOTTOM, 0)
  71.     os.sleep(0.5)
  72.   end
  73. end
  74.  
  75.  
  76. local function requestStorageUnit(addr)
  77.   local rs = c.proxy(addr)
  78.   rs.setOutput(NORTH, 1)
  79.   os.sleep(1)
  80.   rs.setOutput(NORTH, 0)
  81. end
  82.  
  83. local Altar = {
  84.   _isUp = false
  85. }
  86. function Altar:up()
  87.   for _,addr in ipairs(rs_list) do
  88.     local rs = c.proxy(addr)
  89.     if (rs.address ~= SKIP) then
  90.       rs.setOutput(SOUTH, 1)
  91.     end
  92.   end
  93.   self._isUp = true
  94. end
  95.  
  96.  
  97.  
  98. function Altar:down()
  99.   for _,addr in ipairs(rs_list) do
  100.     local rs = c.proxy(addr)
  101.     if (rs.address ~= SKIP) then
  102.       rs.setOutput(SOUTH, 0)
  103.     end
  104.   end
  105.   self._isUp = false
  106. end
  107.  
  108. function Altar:isUp()
  109.   return (c.proxy(rs_list[2]).getOutput(SOUTH) > 0)
  110. end
  111.  
  112. function Altar:clear()
  113.     local rs = c.proxy(SKIP)
  114.     rs.setOutput(WEST, 1)
  115.     os.sleep(2)
  116.     rs.setOutput(WEST, 0)
  117.     active = 'none'
  118.   end
  119.  
  120. local Patterns = {
  121.   _patterns = {},
  122.   _size = 0
  123. }
  124.  
  125. function Patterns:load()
  126.   local file = io.open('patterns', 'r')
  127.   local txt = file:read('*all')
  128.   self._patterns = deser(txt)
  129.   file:close()
  130.   print ('loaded: ' ,self._patterns)
  131.   if self._patterns == nil then
  132.     self._patterns = {}
  133.   end
  134.   if (self._size == 0) then
  135.     self._size = self:count()
  136.   end  
  137. end
  138.  
  139. function Patterns:save()
  140.   local file = io.open('patterns', 'w')
  141.   file:write(ser(self._patterns))
  142.   file:close()
  143. end
  144.  
  145. function Patterns:add(key, addr)
  146.   self._size = self._size + 1
  147.   table.insert(self._patterns, {key = key, address = addr, active = false})
  148.   self:save()
  149. end
  150.  
  151. function Patterns:store(key)
  152.     self:load()
  153.     if (self._size == #rs_list) then
  154.       print('Pattern size exceeded. Exiting now')
  155.       return
  156.     end
  157.     local rs = c.proxy(rs_list[self._size + 1])
  158.     requestStorageUnit(rs.address)
  159.     print('add new item to store: ', key, addr)
  160.     self:add(key, rs.address)    
  161.     if (Altar:isUp()) then
  162.       Altar:down()
  163.     end
  164.     activate()
  165. end
  166.  
  167. function Patterns:remove(key)
  168.   self:load()
  169.   local addr = nil
  170.   for i,v in ipairs(self._patterns) do
  171.     if (v.key == key) then
  172.     self._patterns[i] = nil
  173.     end
  174.   end
  175.   self._size = self._size - 1
  176.   self:save()
  177. end
  178.  
  179. function Patterns:getAddr(key)
  180.     self:load()
  181.     local current = nil
  182.     local addr = nil
  183.     local index = nil
  184.     for i,v in ipairs(self._patterns) do
  185.       if (v.key == key) then
  186.         addr = v.address
  187.         index = i
  188.       end
  189.     end
  190.     return addr
  191. end
  192.  
  193. function Patterns:get(key)
  194.   Altar:clear()
  195.   local addr = self:getAddr(key)
  196.   if (Altar:isUp()) then
  197.     Altar:down()
  198.   end
  199.  
  200.   requestStorageUnit(addr)
  201.   activate()
  202.   dropRelays(8)
  203.   os.sleep(8)
  204.   requestStorageUnit(addr)
  205.   activate()
  206.   Altar:up()
  207.   active = key  
  208. end
  209.  
  210. function Patterns:count()
  211.   local count = 0
  212.   for _ in ipairs(self._patterns) do
  213.     count = count + 1
  214.   end
  215.   return count
  216. end
  217.  
  218. local Commands = {
  219.  running = false,
  220.  history = { },
  221.  hints = { }
  222. }
  223.  
  224. function Commands:hint(p)
  225.  
  226.   for k in pairs(self) do
  227.     if (p) then
  228.       print(k)
  229.     end
  230.     table.insert(self.hints, k)
  231.   end
  232.  
  233. end
  234.  
  235. function Commands:print(...)
  236.   local arg = {...}
  237.   local tmp = {}
  238.   if (arg[1] == 'clear') then
  239.     term.clear()
  240.   end
  241.   if (arg[1] == 'skip') then
  242.     tmp = SKIP
  243.   elseif (arg[1] == 'altar') then
  244.     tmp = Altar
  245.   elseif (arg[1] == 'active') then
  246.     tmp = active
  247.   elseif (arg[1] == 'patterns') then
  248.     Patterns:load()
  249.     tmp = Patterns
  250.   else
  251.     tmp = arg[1]
  252.   end
  253.   if (type(tmp) == 'table') then
  254.     debug(tmp)
  255.   else
  256.     print(tmp)
  257.   end
  258. end
  259.  
  260. function Commands:up()
  261.   print('Raising altar..')
  262.   Altar:up()
  263.   print('...done!')
  264. end
  265.  
  266. function Commands:on()
  267.   print('Running app..')
  268.   self.running = true
  269.   if (not Altar:isUp()) then
  270.     Altar:up()
  271.   end
  272.   while (self.running) do
  273.     local function filter(name, ...)
  274.       if (name == 'interrupted') then
  275.         return true
  276.       end
  277.       if name ~= "key_up" and name ~= "key_down" and name ~= "touch" then
  278.         return false
  279.       end
  280.       return true
  281.     end
  282.     local name, _, x, y = event.pullFiltered(filter)
  283.     if (name == 'interrupted') then
  284.       print('Interrupted..')
  285.       self.running = false
  286.       print('stopping now')
  287.     end
  288.     self:hint()
  289.     local input = term.read(self.history, self.hints)
  290.     local cmd = split(input, ' ')
  291.     table.insert(self.history, input)
  292.     self[cmd[1]](self, cmd[2], cmd[3], cmd[4])
  293.   end
  294.   print('...exited!')
  295. end
  296.  
  297. function Commands:down()
  298.   print('Lowering altar..')
  299.   Altar:down()
  300.   print('...done!')
  301. end
  302.  
  303. function Commands:off()
  304.   print('stopping..')
  305.   self.running = false
  306.   print('...done!')
  307. end
  308.  
  309. function Commands:clear()
  310.     print('Clearing altar..')
  311.     Altar:clear()
  312.     print('...done!')
  313. end
  314.  
  315. function Commands:add(...)
  316.   local arg = {...}
  317.   print('Adding new pattern: ', arg[1])
  318.   Patterns:store(arg[1])
  319. end
  320.  
  321. function Commands:insert(...)
  322.   local arg = {...}
  323.   print('Adding new pattern: ', arg[1])
  324.   Patterns:store(arg[1])
  325. end
  326.  
  327. function Commands:remove(...)
  328.   local arg = {...}
  329.   print('Removing pattern: ', arg[1])
  330.   Patterns:remove(arg[1])
  331. end
  332.  
  333. function Commands:delete(...)
  334.   local arg = {...}
  335.   print('Removing pattern: ', arg[1])
  336.   Patterns:remove(arg[1])
  337. end
  338.  
  339. function Commands:get(...)
  340.   local arg = {...}
  341.   print('Loading pattern: ', arg[1])
  342.   Patterns:get(arg[1])
  343.   print('...done!')
  344. end
  345.  
  346. function Commands:load(...)
  347.   local arg = {...}
  348.   print('Loading pattern: ', arg[1])
  349.   Patterns:get(arg[1])
  350.   print('...done!')
  351. end
  352.  
  353. function Commands:drop(...)
  354.     local arg = {...}
  355.     print('Dropping ' .. arg[1] .. ' relays')
  356.     dropRelays(arg[1])
  357.     print('...done!')
  358. end
  359.  
  360. function Commands:list(...)
  361.     local arg = {...}
  362.     if (arg[1] == 'constellations') then
  363.       Patterns:load()
  364.       for _,v in ipairs(Patterns._patterns) do print(v.key) end
  365.     else
  366.       debug(Commands)
  367.     end
  368. end
  369.  
  370. function Commands:admin(...)
  371.     local arg = {...}
  372.     if (arg[1] == 'pattern') then
  373.       if (arg[2] == 'reset') then
  374.         Patterns:load()
  375.         local addr = Patterns:getAddr(arg[3])
  376.         if (Altar:isUp()) then
  377.             Altar:down()
  378.         end
  379.        
  380.         requestStorageUnit(addr)
  381.         activate()
  382.       end
  383.     end
  384. end
  385.  
  386. Commands[args[1]](Commands, args[2], args[3], args[4])
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top