Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- 0.01
- Started a new thing!
- ]]
- local version = 0.01
- local allItems = {}
- local chestCache = {}
- local modules = {}
- local chests = {}
- local manipFuncs = {}
- local dynaStore = false
- local dataLocation = "/data/"
- local modulesLocation = "/modules/"
- local customFileName = "customization"
- local dynaName = "dynaStore"
- local itemSaveName = "items"
- local chestSaveName = "chests"
- local manip = false
- local custom = fs.exists(dataLocation..customFileName)
- local running = false
- local function copy(a,b)
- assert(type(a) == "table","copy: first input is not a table.")
- for k,v in pairs(a) do
- if type(v) == "table" then
- b[k] = {}
- copy(a[k],b[k])
- else
- b[k] = v
- end
- end
- end
- local function ao(wrt,h)
- h.writeLine(wrt)
- end
- local function saveDyna()
- local h = fs.open(dataLocation..dynaName,"w")
- if h then
- h.write(textutils.serialize(dynaStore))
- h.close()
- end
- end
- local function loadDyna()
- local h = fs.open(dataLocation..dynaName,"r")
- if h then
- dynaStore = textutils.unserialize(h.readAll())
- h.close()
- else
- dynaStore = false
- end
- end
- local function writeCustomization(file)
- local h = fs.open(file,"w")
- ao("return {",h)
- ao(" manipulators = {},",h)
- 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)
- ao(" moduleLocation = \"/modules/\",",h)
- ao(" prefix = \"i\",",h)
- ao("}",h)
- h.flush()
- h.close()
- end
- local function moveCustomization(a)
- printError("Warning: Customization file error, moving it to a new location then creating another customization file.")
- local b = a:find(":")
- local c = a:sub(b+1)
- local d = c:match("%d+")
- print("This program will mark the line (line "..d..") with a comment.")
- d = tonumber(d)
- assert(type(d) == "number","This should not happen. Please send a signal flare to fatboychummy (" .. tostring( d ) .. ")")
- local fileName = dataLocation.."BAD-"..customFileName
- local h1 = fs.open(dataLocation..customFileName,"r")
- local h2 = fs.open(fileName,"w")
- local i = 1
- local cur = h1.readLine()
- while cur do
- if i == d then
- cur = cur.."--"..string.rep("!",15)
- end
- h2.writeLine(cur)
- cur = h1.readLine()
- i = i + 1
- end
- h1.close()
- h2.close()
- print("File moved",fileName)
- fs.delete(dataLocation..customFileName)
- writeCustomization(dataLocation..customFileName)
- print("All done")
- end
- local function setup()
- writeCustomization(dataLocation..customFileName)
- end
- if not custom then
- setup()
- print("System is set up.")
- return 0
- else
- ok,custom = pcall(dofile,dataLocation..customFileName)
- if not ok or not custom or custom == {} then
- moveCustomization(custom)
- return 0
- end
- custom.modules = {}
- end
- do
- loadDyna()
- if not dynaStore then loadDyna() end
- if not dynaStore then dynaStore = {} end
- local tmods = fs.list(modulesLocation)
- for i = 1,#tmods do
- local h = fs.open(modulesLocation..tmods[i],"r")
- h.readLine()
- local no = h.readLine()
- h.close()
- custom.modules[tmods[i]] = textutils.unserialize(no)
- if not dynaStore[tmods[i]] then
- dynaStore[tmods[i]] = {}
- end
- end
- end
- local function getChests()
- local a = peripheral.getNames()
- chests = {}
- for i = 1,#a do
- if a[i]:find("chest") or a[i]:find("shulker") then
- chests[#chests+1] = a[i]
- end
- end
- end
- local tofind = {
- "getEnder",
- "tell",
- "getName",
- "getInventory",
- }
- assert(#custom.manipulators ~= 0,"Manipulators table is empty.")
- for i = 1,#custom.manipulators do
- local c = custom.manipulators[i]
- local cur = peripheral.wrap(c)
- if cur then
- for o = 1,#tofind do
- if not manipFuncs[tofind[o]] and cur[tofind[o]] then
- manipFuncs[tofind[o]] = cur[tofind[o]]
- print("Found",tofind[o])
- end
- end
- else
- printError("Warning: Peripheral "..c.." does not exist.")
- end
- end
- for i = 1,#tofind do
- if manipFuncs[tofind[i]] == nil then
- error("Failed to find the function:",tofind[i].."... Make sure the proper modules are installed.")
- end
- end
- local name = manipFuncs.getName()
- local inv = manipFuncs.getInventory()
- local ender = manipFuncs.getEnder()
- local mods = {}
- copy(custom.modules,mods)
- local tell = function(stuff)
- local lines = {}
- if stuff:len() > 100 then
- local o = 1
- for i = 1,stuff:len(),100 do
- lines[o] = stuff:sub(i,i+99)
- o = o + 1
- end
- else
- lines[1] = stuff
- end
- for i = 1,#lines do
- local a,b = pcall(manipFuncs.tell,lines[i])
- if not a then
- printError("Your chat-recorder is either missing or not bound properly.")
- term.setTextColor(colors.yellow)
- print(b)
- term.setTextColor(colors.white)
- end
- end
- end
- local function parseModules(str)
- for k,v in pairs(mods) do
- print("Checking module:",k)
- for i = 1,#v do
- if str == v[i] then
- return k,v
- end
- end
- end
- return false
- end
- local function runModule(mod,tab)
- getChests()
- local a = dofile(modulesLocation..mod)
- if mod == "help.lua" then
- a(tab,chests,inv,tell,dynaStore[mod],mods)
- elseif mod == "debug.lua" then
- a(tab,chests,inv,tell,dynaStore,mods,chestCache,allItems)
- else
- a(tab,chests,inv,tell,dynaStore[mod],chestCache,allItems)
- end
- saveDyna()
- end
- local function parse(tab)
- print("Parsing...")
- for i = 1,#tab do
- tab[i] = tab[i]:lower()
- end
- local module = parseModules(tab[1])
- print("parsed:",tab[1]..", got",module)
- if module then
- return module,tab
- else
- print("Module does not exist!")
- tell("Module does not exist!")
- end
- return false
- end
- local function main()
- tell("Ready.")
- while true do
- local ev = {os.pullEvent(custom.preferredDetectionMethod)}
- if ev[2] == name then
- if ev[3] == custom.prefix then
- if string.lower(ev[4][1]) == "interrupt" then
- print("Interrupting process.")
- os.queueEvent("INTERRUPT")
- else
- local mod,tab = parse(ev[4])
- if running and mod then
- tell("Module " .. tostring(mod) .. " is currently running.")
- else
- if mod then
- os.queueEvent("RUN_MODULE",mod,tab)
- end
- end
- end
- end
- end
- print("---")
- end
- end
- local function co()
- while true do
- local ev = {os.pullEvent("RUN_MODULE")}
- parallel.waitForAny(
- function ()
- runModule(ev[2],ev[3])
- end,
- function ()
- os.pullEvent("INTERRUPT")
- end)
- os.queueEvent("MODULE_COMPLETE")
- end
- end
- local function co2()
- while true do
- local ev = {os.pullEvent()}
- if ev[1] == "RUN_MODULE" then
- running = ev[2]
- elseif ev[1] == "MODULE_COMPLETE" then
- running = false
- end
- end
- end
- local function cache()
- local function save()
- local h = fs.open(dataLocation..itemSaveName,"w")
- if h then
- h.write("return " .. textutils.serialize(allItems))
- h.close()
- else
- printError("Failed to save items to file.")
- end
- h = fs.open(dataLocation..chestSaveName,"w")
- if h then
- h.write("return " .. textutils.serialize(chestCache))
- h.close()
- else
- printError("Failed to save chest data to file.")
- end
- end
- local function load()
- local function dof(loc)
- local ok,err = pcall(dofile,loc)
- if ok then
- return false,err
- else
- printError("Failed to load items from file "..tostring(loc).." due to "..err)
- end
- return true,{}
- end
- local fail1 = false
- local fail2 = false
- fail1, allItems = dof(dataLocation..itemSaveName)
- fail2, chestCache = dof(dataLocation..chestSaveName)
- return not (fail1 or fail2), (fail1 and allItems) or (fail2 and chestCache) or "yeet"
- end
- local function newItem(item)
- local meta = item.getMetadata()
- if meta then
- if not allItems[meta.name] then
- allItems[meta.name] = {}
- end
- allItems[meta.name][meta.damage] = {
- Display = meta.displayName,
- count = 0,
- }
- print("New item added to cache: " .. meta.displayName)
- end
- end
- local function countItem(item,dmg,c)
- allItems[item][dmg].count = allItems[item][dmg].count + c
- end
- local function zero()
- for k,v in pairs(allItems) do
- for k2,v2 in pairs(v) do
- v2.count = 0
- end
- end
- end
- local a,b = load()
- if a then
- print("Successfully loaded itemdata and chestdata")
- while true do
- print("Scanning inventory.")
- local a = os.clock()
- getChests()
- zero()
- for i = 1,#chests do
- local cCName = chests[i]
- chestCache[cCName] = {}
- local cChest = peripheral.wrap(cCName)
- if cChest then
- local inv = cChest.list()
- for o = 1,cChest.size() do
- if inv[o] then
- local cName = inv[o].name
- local cDam = inv[o].damage
- if not allItems[cName] or ( allItems[cName] and not allItems[cName][cDam] ) then
- newItem(cChest.getItem(o))
- end
- chestCache[cCName][o] = {name = cName,damage = cDam, count = inv[o].count}
- countItem(cName,cDam,inv[o].count)
- else
- chestCache[cCName][o] = false
- end
- end
- end
- end
- b = os.clock()
- print("Done.","Took",b-a,"second(s).")
- print("Saving")
- a = os.clock()
- save()
- b = os.clock()
- print("Done.","Took",b-a,"second(s).")
- os.sleep(custom.cacheRefreshTime or 10)
- end
- else
- printError(b)
- if not fs.exists(dataLocation..itemSaveName) then
- print("A file is missing, it may be our problem.")
- local h = fs.open(dataLocation..itemSaveName,"w")
- h.writeLine("return {}")
- h.close()
- print("File "..dataLocation..itemSaveName.." has been created.")
- end
- if not fs.exists(dataLocation..chestSaveName) then
- print("A file is missing, it may be our problem.")
- local h = fs.open(dataLocation..chestSaveName,"w")
- h.writeLine("return {}")
- h.close()
- print("File "..dataLocation..chestSaveName.." has been created.")
- end
- print("Sleeping 5, then rebooting.")
- os.sleep(5)
- os.reboot()
- end
- end
- local a,err = pcall(parallel.waitForAny,main,co,co2,cache)
- if not a then
- if err == "Terminated" then
- tell("Modu has been terminated")
- return
- end
- tell("Modu has stopped with error "..err.."... Rebooting.")
- os.sleep(2)
- os.reboot()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement