Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- STORAGE_ROOT_DIR = "./"
- STORAGE_BIN_LIST_FILENAME = "bins.list"
- bins = {}
- main_bin = ""
- function logwarn(e)
- print("WARN> "..e)
- end
- function logerr(e)
- print("ERROR> "..e)
- end
- function logverb(e)
- print("VERBOSE> "..e)
- end
- --read bins
- for l in io.lines(STORAGE_ROOT_DIR..STORAGE_BIN_LIST_FILENAME) do
- local a,b,c = l:match("^([^;]+);([^;]+);([^;]+)$")
- if bins[b]~=nil then
- logwarn(("Duplicate bin alias detected: key '%s' already declared ('%s'), ignoring new value"):format(b,bins[b]))
- else
- bins[b]={type=a,id=b,val=c}
- end
- for id, dat in pairs(bins) do
- if dat.type=="main" then
- if main_bin~="" then
- logwarn(string.format("Duplicate main bin detected (%s); ignoring new value",main_bin))
- else
- main_bin = id
- end
- end
- end
- end
- function list_bins(all,type,blacklist,limit)
- local r = {}
- blacklist = blacklist or {}
- limit=limit or 1000000
- if limit==-1 then limit=1000000 end
- for id,dat in pairs(all) do
- if (not blacklist[id]) and (type==dat.type) then
- r[id] = dat
- end
- end
- return r
- end
- function pushItems(from,to,inslot,limit,toslot)
- local r = peripheral.call(from,"pushItems",to,inslot,limit,toslot)
- print(string.format("Moving %d items from '%s' to '%s'",r,from,to))
- return r
- end
- function getInvSize(inv)
- return peripheral.call(inv,"size")
- end
- function listInv(inv)
- return peripheral.call(inv,"list")
- end
- function itemDetails(inv,slot)
- return peripheral.call(inv,"getItem",slot).getMetadata()
- end
- function store_to(from,fromslot,to,limit)
- return pushItems(from.val,to.val,fromslot,limit)
- end
- function store_all_to(from,to,limit)
- local t=0
- limit=limit or 1000000
- if limit==-1 then limit=1000000 end
- for slot,item in pairs(listInv(from)) do
- t=t+pushItems(from.val,to.val,slot,limit-t)
- if t==limit then return limit end
- end
- return t
- end
- function store_to_any(from,fromslot,tolist,limit)
- local t=0
- limit=limit or 1000000
- if limit==-1 then limit=1000000 end
- local l={}
- for id,dat in pairs(tolist) do
- table.insert(l,{id=id,dat=dat})
- end
- for _,e in pairs(l) do
- local dat = e.dat
- t=t+pushItems(from.val,dat.val,fromslot,limit-t)
- if t==limit then return limit end
- end
- return t
- end
- function store_all_to_any(from,tolist,limit)
- limit=limit or 1000000
- if limit==-1 then limit=1000000 end
- local t=0
- for slot,item in pairs(listInv(from.val)) do
- t=t+store_to_any(from,slot,tolist,limit-t)
- if t==limit then return limit end
- end
- return t
- end
- function default_filter(txt)
- return function (det)
- local f = txt:lower()
- local a = det.name:lower():find(f)
- local b = det.displayName:lower():find(f)
- return a or b
- end
- end
- function fetch_from(from,to,filter,limit)
- if type(filter)~="function" then if filter then filter=function() return true end else filter=function() return false end end end
- local t=0
- limit=limit or 1000000
- if limit==-1 then limit=1000000 end
- for slot,item in pairs(listInv(from.val)) do
- if filter(itemDetails(from.val,slot)) then
- t=t+store_to(from,slot,to,limit-t)
- end
- if limit==t then return limit end
- end
- return t
- end
- function fetch_from_any(fromlist,to,filter,limit)
- if type(filter)~="function" then if filter then filter=function() return true end else filter=function() return false end end end
- local t=0
- limit=limit or 1000000
- if limit==-1 then limit=1000000 end
- for id,dat in pairs(fromlist) do
- t=t+fetch_from(dat,to,filter,limit-t)
- if limit==t then return limit end
- end
- return t
- end
- function tokens(str)
- local tok = {}
- local isStr = false
- local cur = ""
- for i=1,#str do
- local nc = str:sub(i,i)
- if nc=="'" then
- isStr = not isStr
- else
- if nc==' ' and not isStr then
- if cur~='' then
- table.insert(tok,cur)
- end
- cur=''
- else
- cur=cur..nc
- end
- end
- end
- table.insert(tok,cur)
- return tok
- end
- function exec_command(cmd)
- local tk = tokens(cmd)
- cmd = tk[1]
- if cmd=="storeall" or cmd=='s' then
- print(("Stored %d items."):format(store_all_to_any(bins[main_bin],list_bins(bins,"storage",false))))
- elseif cmd=="fetch" or cmd=='f' then
- print(("Fetched %d x '%s'."):format(fetch_from_any(list_bins(bins,"storage",false),bins[main_bin], default_filter(tk[2]), tonumber(tk[3] or -1)),tk[2]))
- elseif cmd=='moveto' or cmd=='m' then
- local where = tk[2]
- local what = tk[3]
- local limit = tonumber(tk[4])
- print(string.format("Moved %d x '%s' to '%s'.",fetch_from_any(list_bins(bins,"storage",{[where]=true}),bins[where],default_filter(what),limit),what,where))
- elseif cmd=='listbins' or cmd=='lb' then
- print("Listing bins:")
- for id,v in pairs(bins) do
- print(string.format("#'%s': pid=\"%s\" type='%s'",id,v.val,v.type))
- end
- elseif cmd=='main' then
- main_bin = tk[2]
- print("Set '"..tk[2].."' as main bin.")
- elseif cmd=='help' or cmd=='h' then
- print([[
- Help:
- storeall -> store all items from main bin to any storage
- moveto | m <where> <filter> <limit> -> move at most <limit >items that meet <filter> from any storage to <where>
- fetch | f <filter> <limit> -> same as "moveto main <filter> <limit>"
- main <bin> -> set <bin> as main bin
- listbins -> list all bins
- ]])
- elseif cmd=="exit" or cmd=="quit" or cmd=="q" then return false end
- return true
- end
- logverb(string.format("Main bin: %s",main_bin))
- logverb("Bins:")
- for id,v in pairs(bins) do
- logverb(string.format("%s: type: %s val: %s",id,v.type,v.val))
- end
- while exec_command(io.read('*l')) do end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement