Advertisement
Lanzr

iotaTools.lua

Jul 4th, 2024 (edited)
753
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.04 KB | None | 0 0
  1. require("hexMap")
  2. --[[
  3.     * this tools is read iota from focal_port to string
  4.     * if specify argument "dec" will be decompilation iota to hex code
  5.     * author : Lanzr
  6. ]]
  7. local completion = require "cc.shell.completion"
  8. local complete = completion.build(
  9.     { completion.choice, { "toStr", "dec", "append" } },
  10.     { completion.choice, { "overWrite" } }
  11. )
  12. shell.setCompletionFunction("iotaTools.lua", complete)
  13.  
  14. local cmd = arg[1]
  15. local param = arg[2]
  16. local dev = peripheral.find("focal_port")
  17. local g_force_mode  =false
  18.  
  19. local function tipWriter(left,right)
  20.     term.setTextColor(colors.orange)
  21.     write(left)
  22.     term.setTextColor(colors.yellow)
  23.     print(":"..right)
  24.     term.setTextColor(colors.white)
  25. end
  26. if cmd == nil then
  27.     local programName = arg[0] or fs.getName(shell.getRunningProgram())
  28.     print("Usage: " .. programName .. " <cmd> [Param]")
  29.     print("cmd can be :")
  30.     tipWriter("toStr", " get focal_port iota to string and save in \"data\" file")
  31.     tipWriter("dec", " get focal_port iota and decompile code based on hexMap and store it in \"dec_out\" file ")
  32.     tipWriter("append", " get focal_port iota and Generate mappings that are not included in the hexMap, store the results in a \"newHexMap\" file, and if append parameter \"overWrite\", the hexMap will be overwritten ")
  33.     return
  34. end
  35.  
  36. function table2str(t)
  37.     local function serialize(tbl)
  38.         local tmp = {}
  39.         for k, v in pairs(tbl) do
  40.             local k_type = type(k)
  41.             local v_type = type(v)
  42.             local key = (k_type == "string" and "[\"" .. k .. "\"]=") or (k_type == "number" and "")
  43.             local value = (v_type == "table" and serialize(v)) or (v_type == "boolean" and tostring(v)) or (v_type == "string" and "\"" .. v .. "\"") or (v_type == "number" and v)
  44.             tmp[#tmp + 1] = key and value and tostring(key) .. tostring(value) or nil
  45.         end
  46.         if table.maxn(tbl) == 0 then
  47.             return "\n[\"\"] = {" .. table.concat(tmp, ",") .. "},"
  48.         else
  49.             return "[" .. table.concat(tmp, " ") .. "\n]"
  50.         end
  51.     end
  52.     assert(type(t) == "table")
  53.     return serialize(t)
  54. end
  55.  
  56. local numMap = {
  57.     ["q"] = (function (sum)
  58.         return sum + 5
  59.     end),
  60.     ["e"] = (function (sum)
  61.         return sum + 10
  62.     end),
  63.     ["d"] = (function (sum)
  64.         return sum / 2
  65.     end),
  66.     ["w"] = (function (sum)
  67.         return sum + 1
  68.     end),
  69.     ["a"] = (function (sum)
  70.         return sum * 2
  71.     end)
  72. }
  73. local function getPatternNum(str)
  74.     str = string.gsub(str,"aqaa","")
  75.     local sum = 0
  76.     for char in str:gmatch(".") do
  77.         sum = numMap[char](sum)
  78.     end
  79.     return tostring(sum)
  80. end
  81. function decompilation()
  82.     local indentation_level = 0
  83.     anti_hexMap = {}
  84.     for cmd, iota in pairs(hexMap) do
  85.         anti_hexMap[iota["angles"]] = cmd
  86.     end
  87.     data = dev.readIota()
  88.     f = io.open("dec_out","w")
  89.     for index,iota  in pairs(data) do
  90.         local str = ""
  91.         local cmd = anti_hexMap[iota["angles"]]
  92.         if cmd == "}" then
  93.             indentation_level = indentation_level - 1
  94.         end
  95.         for i = 1, indentation_level, 1 do
  96.             str = str.."    "
  97.         end
  98.         if cmd ~= nil then
  99.             str = str..cmd
  100.         elseif string.match(iota["angles"],"^aqaa") ~= nil then
  101.             str = str..getPatternNum(iota["angles"])
  102.         else
  103.             str = str..iota["angles"]
  104.         end
  105.         f.write(f,str.."\n")
  106.         if cmd == "{" then
  107.             indentation_level = indentation_level + 1
  108.         end
  109.     end
  110.     f.close(f)
  111. end
  112.  
  113. function append()
  114.     local targetFile = "newHexMap"
  115.     if g_force_mode then
  116.         targetFile = "hexMap"
  117.     end
  118.     anti_hexMap = {}
  119.     for cmd, iota in pairs(hexMap) do
  120.         anti_hexMap[iota["angles"]] = cmd
  121.     end
  122.    
  123.     f = io.open("hexMap", "r+")
  124.     local hexMapFindLck =false
  125.     local text = ""
  126.     if f then
  127.         for line in f:lines() do
  128.              if not hexMapFindLck then
  129.                 local findHexMap = string.match(line, "^[ ]*hexMap[ ]*=")
  130.                 if findHexMap ~= nil then
  131.                     hexMapFindLck = true
  132.                 end
  133.             else
  134.                 local findBracket = string.match(line, "^[ ]*}[ ]*$")
  135.                 if findBracket then
  136.                     break
  137.                 end
  138.             end
  139.             text = text..line.."\n"
  140.         end
  141.         f.close(f)
  142.     end
  143.  
  144.     f = io.open(targetFile,"w")
  145.     f.write(f,text)
  146.     data = dev.readIota()
  147.     for index,iota  in pairs(data) do
  148.         local cmd = anti_hexMap[iota["angles"]]
  149.         if cmd == nil then
  150.             if string.match(iota["angles"],"^aqaa") == nil then
  151.                 local tmp = {}
  152.                 for k, v in pairs(iota) do
  153.                     local k_type = type(k)
  154.                     local v_type = type(v)
  155.                     local key = (k_type == "string" and "[\"" .. k .. "\"]=") or (k_type == "number" and "")
  156.                     local value = (v_type == "table" and serialize(v)) or (v_type == "boolean" and tostring(v)) or (v_type == "string" and "\"" .. v .. "\"") or (v_type == "number" and v)
  157.                     tmp[#tmp + 1] = key and value and tostring(key) .. tostring(value) or nil
  158.                 end
  159.                 local str =  "    [\"\"] = {" .. table.concat(tmp, ",") .. "},"
  160.                 f.write(f,str.."\n")
  161.             end
  162.         end
  163.     end
  164.     f.write(f,"}")
  165.     f.close(f)
  166. end
  167.  
  168. local function getIotaMap()
  169.     d = dev.readIota()
  170.     str = table2str(d)
  171.     f = io.open("data","w")
  172.     f.write(f,str)
  173.     f.close(f)
  174. end
  175.  
  176. local toolsMap = {
  177.     ["toStr"] = (function (cStr)
  178.         getIotaMap()
  179.     return true end),
  180.     ["dec"] = (function (cStr)
  181.         decompilation()
  182.     return true end),
  183.     ["append"] = (function (cStr)
  184.         if(param ~=nil)then
  185.             if(param == "overWrite")then
  186.                 g_force_mode = true
  187.             end
  188.         end
  189.         append()
  190.     return true end)
  191. }
  192. local function mainloop()
  193.    toolsMap[cmd]()
  194. end
  195.  
  196. mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement