Advertisement
fatboychummy

modu

Oct 14th, 2018
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.71 KB | None | 0 0
  1. --[[
  2.   0.01
  3.   Started a new thing!
  4. ]]
  5.  
  6.  
  7. local version = 0.01
  8. local allItems = {}
  9. local chestCache = {}
  10. local modules = {}
  11. local chests = {}
  12. local manipFuncs = {}
  13. local dynaStore = false
  14. local dataLocation = "/data/"
  15. local modulesLocation = "/modules/"
  16. local customFileName = "customization"
  17. local dynaName = "dynaStore"
  18. local itemSaveName = "items"
  19. local chestSaveName = "chests"
  20. local manip = false
  21. local custom = fs.exists(dataLocation..customFileName)
  22. local running = false
  23.  
  24.  
  25. local function copy(a,b)
  26.   assert(type(a) == "table","copy: first input is not a table.")
  27.   for k,v in pairs(a) do
  28.     if type(v) == "table" then
  29.       b[k] = {}
  30.       copy(a[k],b[k])
  31.     else
  32.       b[k] = v
  33.     end
  34.   end
  35. end
  36.  
  37. local function ao(wrt,h)
  38.   h.writeLine(wrt)
  39. end
  40.  
  41. local function saveDyna()
  42.   local h = fs.open(dataLocation..dynaName,"w")
  43.   if h then
  44.     h.write(textutils.serialize(dynaStore))
  45.     h.close()
  46.   end
  47. end
  48.  
  49. local function loadDyna()
  50.   local h = fs.open(dataLocation..dynaName,"r")
  51.   if h then
  52.     dynaStore = textutils.unserialize(h.readAll())
  53.     h.close()
  54.   else
  55.     dynaStore = false
  56.   end
  57. end
  58.  
  59. local function writeCustomization(file)
  60.   local h = fs.open(file,"w")
  61.   ao("return {",h)
  62.   ao("  manipulators = {},",h)
  63.   ao("  preferredDetectionMethod = \"command\", --The event created by whatever chat recorder you are using.  It is seriously recommended you don't change this (until further notice).",h)
  64.   ao("  moduleLocation = \"/modules/\",",h)
  65.   ao("  prefix = \"i\",",h)
  66.   ao("}",h)
  67.   h.flush()
  68.   h.close()
  69. end
  70.  
  71. local function moveCustomization(a)
  72.   printError("Warning: Customization file error, moving it to a new location then creating another customization file.")
  73.   local b = a:find(":")
  74.   local c = a:sub(b+1)
  75.   local d = c:match("%d+")
  76.   print("This program will mark the line (line "..d..") with a comment.")
  77.   d = tonumber(d)
  78.   assert(type(d) == "number","This should not happen. Please send a signal flare to fatboychummy (" .. tostring( d ) .. ")")
  79.   local fileName = dataLocation.."BAD-"..customFileName
  80.   local h1 = fs.open(dataLocation..customFileName,"r")
  81.   local h2 = fs.open(fileName,"w")
  82.   local i = 1
  83.   local cur = h1.readLine()
  84.   while cur do
  85.     if i == d then
  86.       cur = cur.."--"..string.rep("!",15)
  87.     end
  88.     h2.writeLine(cur)
  89.     cur = h1.readLine()
  90.     i = i + 1
  91.   end
  92.   h1.close()
  93.   h2.close()
  94.   print("File moved",fileName)
  95.   fs.delete(dataLocation..customFileName)
  96.   writeCustomization(dataLocation..customFileName)
  97.   print("All done")
  98. end
  99.  
  100.  
  101.  
  102. local function setup()
  103.   writeCustomization(dataLocation..customFileName)
  104. end
  105.  
  106. if not custom then
  107.   setup()
  108.   print("System is set up.")
  109.   return 0
  110. else
  111.   ok,custom = pcall(dofile,dataLocation..customFileName)
  112.   if not ok or not custom or custom == {} then
  113.     moveCustomization(custom)
  114.     return 0
  115.   end
  116.   custom.modules = {}
  117. end
  118.  
  119.  
  120. do
  121.   loadDyna()
  122.   if not dynaStore then loadDyna() end
  123.   if not dynaStore then dynaStore = {} end
  124.   local tmods = fs.list(modulesLocation)
  125.   for i = 1,#tmods do
  126.     local h = fs.open(modulesLocation..tmods[i],"r")
  127.     h.readLine()
  128.     local no = h.readLine()
  129.     h.close()
  130.     custom.modules[tmods[i]] = textutils.unserialize(no)
  131.     if not dynaStore[tmods[i]] then
  132.       dynaStore[tmods[i]] = {}
  133.     end
  134.   end
  135. end
  136.  
  137.  
  138. local function getChests()
  139.   local a = peripheral.getNames()
  140.   chests = {}
  141.   for i = 1,#a do
  142.     if a[i]:find("chest") or a[i]:find("shulker") then
  143.       chests[#chests+1] = a[i]
  144.     end
  145.   end
  146. end
  147.  
  148. local tofind = {
  149.   "getEnder",
  150.   "tell",
  151.   "getName",
  152.   "getInventory",
  153. }
  154.  
  155. assert(#custom.manipulators ~= 0,"Manipulators table is empty.")
  156.  
  157. for i = 1,#custom.manipulators do
  158.   local c = custom.manipulators[i]
  159.   local cur = peripheral.wrap(c)
  160.   if cur then
  161.     for o = 1,#tofind do
  162.       if not manipFuncs[tofind[o]] and cur[tofind[o]] then
  163.         manipFuncs[tofind[o]] = cur[tofind[o]]
  164.         print("Found",tofind[o])
  165.       end
  166.     end
  167.   else
  168.     printError("Warning: Peripheral "..c.." does not exist.")
  169.   end
  170. end
  171. for i = 1,#tofind do
  172.   if manipFuncs[tofind[i]] == nil then
  173.     error("Failed to find the function:",tofind[i].."...  Make sure the proper modules are installed.")
  174.   end
  175. end
  176.  
  177.  
  178. local name = manipFuncs.getName()
  179. local inv = manipFuncs.getInventory()
  180. local ender = manipFuncs.getEnder()
  181.  
  182. local mods = {}
  183. copy(custom.modules,mods)
  184.  
  185.  
  186. local tell = function(stuff)
  187.   local lines = {}
  188.   if stuff:len() > 100 then
  189.     local o = 1
  190.     for i = 1,stuff:len(),100 do
  191.       lines[o] = stuff:sub(i,i+99)
  192.       o = o + 1
  193.     end
  194.   else
  195.     lines[1] = stuff
  196.   end
  197.   for i = 1,#lines do
  198.     local a,b = pcall(manipFuncs.tell,lines[i])
  199.     if not a then
  200.       printError("Your chat-recorder is either missing or not bound properly.")
  201.       term.setTextColor(colors.yellow)
  202.       print(b)
  203.       term.setTextColor(colors.white)
  204.     end
  205.   end
  206.  
  207. end
  208.  
  209.  
  210.  
  211. local function parseModules(str)
  212.   for k,v in pairs(mods) do
  213.     print("Checking module:",k)
  214.     for i = 1,#v do
  215.       if str == v[i] then
  216.         return k,v
  217.       end
  218.     end
  219.   end
  220.   return false
  221. end
  222.  
  223.  
  224. local function runModule(mod,tab)
  225.   getChests()
  226.   local a = dofile(modulesLocation..mod)
  227.   if mod == "help.lua" then
  228.     a(tab,chests,inv,tell,dynaStore[mod],mods)
  229.   elseif mod == "debug.lua" then
  230.     a(tab,chests,inv,tell,dynaStore,mods,chestCache,allItems)
  231.   else
  232.     a(tab,chests,inv,tell,dynaStore[mod],chestCache,allItems)
  233.   end
  234.   saveDyna()
  235. end
  236.  
  237.  
  238. local function parse(tab)
  239.   print("Parsing...")
  240.   for i = 1,#tab do
  241.     tab[i] = tab[i]:lower()
  242.   end
  243.   local module = parseModules(tab[1])
  244.   print("parsed:",tab[1]..", got",module)
  245.   if module then
  246.     return module,tab
  247.   else
  248.     print("Module does not exist!")
  249.     tell("Module does not exist!")
  250.   end
  251.   return false
  252. end
  253.  
  254.  
  255. local function main()
  256.   tell("Ready.")
  257.   while true do
  258.     local ev = {os.pullEvent(custom.preferredDetectionMethod)}
  259.     if ev[2] == name then
  260.       if ev[3] == custom.prefix then
  261.         if string.lower(ev[4][1]) == "interrupt" then
  262.           print("Interrupting process.")
  263.           os.queueEvent("INTERRUPT")
  264.         else
  265.           local mod,tab = parse(ev[4])
  266.           if running and mod then
  267.             tell("Module " .. tostring(mod) .. " is currently running.")
  268.           else
  269.             if mod then
  270.               os.queueEvent("RUN_MODULE",mod,tab)
  271.             end
  272.           end
  273.         end
  274.       end
  275.     end
  276.     print("---")
  277.   end
  278. end
  279.  
  280. local function co()
  281.   while true do
  282.     local ev = {os.pullEvent("RUN_MODULE")}
  283.     parallel.waitForAny(
  284.       function ()
  285.         runModule(ev[2],ev[3])
  286.       end,
  287.       function ()
  288.         os.pullEvent("INTERRUPT")
  289.       end)
  290.     os.queueEvent("MODULE_COMPLETE")
  291.   end
  292. end
  293.  
  294. local function co2()
  295.   while true do
  296.     local ev = {os.pullEvent()}
  297.     if ev[1] == "RUN_MODULE" then
  298.       running = ev[2]
  299.     elseif ev[1] == "MODULE_COMPLETE" then
  300.       running = false
  301.     end
  302.   end
  303. end
  304.  
  305. local function cache()
  306.   local function save()
  307.     local h = fs.open(dataLocation..itemSaveName,"w")
  308.     if h then
  309.       h.write("return " .. textutils.serialize(allItems))
  310.       h.close()
  311.     else
  312.       printError("Failed to save items to file.")
  313.     end
  314.     h = fs.open(dataLocation..chestSaveName,"w")
  315.     if h then
  316.       h.write("return " .. textutils.serialize(chestCache))
  317.       h.close()
  318.     else
  319.       printError("Failed to save chest data to file.")
  320.     end
  321.   end
  322.  
  323.   local function load()
  324.     local function dof(loc)
  325.       local ok,err = pcall(dofile,loc)
  326.       if ok then
  327.         return false,err
  328.       else
  329.         printError("Failed to load items from file "..tostring(loc).." due to "..err)
  330.       end
  331.       return true,{}
  332.     end
  333.     local fail1 = false
  334.     local fail2 = false
  335.     fail1, allItems = dof(dataLocation..itemSaveName)
  336.     fail2, chestCache = dof(dataLocation..chestSaveName)
  337.  
  338.     return not (fail1 or fail2), (fail1 and allItems) or (fail2 and chestCache) or "yeet"
  339.   end
  340.  
  341.   local function newItem(item)
  342.     local meta = item.getMetadata()
  343.     if meta then
  344.       if not allItems[meta.name] then
  345.         allItems[meta.name] = {}
  346.       end
  347.       allItems[meta.name][meta.damage] = {
  348.         Display = meta.displayName,
  349.         count = 0,
  350.       }
  351.       print("New item added to cache: " .. meta.displayName)
  352.     end
  353.   end
  354.  
  355.   local function countItem(item,dmg,c)
  356.     allItems[item][dmg].count = allItems[item][dmg].count + c
  357.   end
  358.  
  359.   local function zero()
  360.     for k,v in pairs(allItems) do
  361.       for k2,v2 in pairs(v) do
  362.         v2.count = 0
  363.       end
  364.     end
  365.   end
  366.  
  367.   local a,b = load()
  368.   if a then
  369.     print("Successfully loaded itemdata and chestdata")
  370.     while true do
  371.       print("Scanning inventory.")
  372.       local a = os.clock()
  373.       getChests()
  374.       zero()
  375.       for i = 1,#chests do
  376.         local cCName = chests[i]
  377.         chestCache[cCName] = {}
  378.         local cChest = peripheral.wrap(cCName)
  379.         if cChest then
  380.           local inv = cChest.list()
  381.           for o = 1,cChest.size() do
  382.             if inv[o] then
  383.               local cName = inv[o].name
  384.               local cDam = inv[o].damage
  385.               if not allItems[cName] or ( allItems[cName] and not allItems[cName][cDam] ) then
  386.                 newItem(cChest.getItem(o))
  387.               end
  388.               chestCache[cCName][o] = {name = cName,damage = cDam, count = inv[o].count}
  389.               countItem(cName,cDam,inv[o].count)
  390.             else
  391.               chestCache[cCName][o] = false
  392.             end
  393.           end
  394.         end
  395.       end
  396.       b = os.clock()
  397.       print("Done.","Took",b-a,"second(s).")
  398.       print("Saving")
  399.       a = os.clock()
  400.       save()
  401.       b = os.clock()
  402.       print("Done.","Took",b-a,"second(s).")
  403.       os.sleep(custom.cacheRefreshTime or 10)
  404.     end
  405.   else
  406.     printError(b)
  407.     if not fs.exists(dataLocation..itemSaveName) then
  408.       print("A file is missing, it may be our problem.")
  409.       local h = fs.open(dataLocation..itemSaveName,"w")
  410.       h.writeLine("return {}")
  411.       h.close()
  412.       print("File "..dataLocation..itemSaveName.." has been created.")
  413.     end
  414.     if not fs.exists(dataLocation..chestSaveName) then
  415.       print("A file is missing, it may be our problem.")
  416.       local h = fs.open(dataLocation..chestSaveName,"w")
  417.       h.writeLine("return {}")
  418.       h.close()
  419.       print("File "..dataLocation..chestSaveName.." has been created.")
  420.     end
  421.     print("Sleeping 5, then rebooting.")
  422.     os.sleep(5)
  423.     os.reboot()
  424.   end
  425.  
  426. end
  427.  
  428.  
  429.  
  430. local a,err = pcall(parallel.waitForAny,main,co,co2,cache)
  431.  
  432. if not a then
  433.   if err == "Terminated" then
  434.     tell("Modu has been terminated")
  435.     return
  436.   end
  437.   tell("Modu has stopped with error "..err.."... Rebooting.")
  438.   os.sleep(2)
  439.   os.reboot()
  440. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement