Advertisement
billysback

RStack

Feb 1st, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.94 KB | None | 0 0
  1. local function split(str, pat)
  2.     local t = {}  -- NOTE: use {n = 0} in Lua-5.0
  3.     if str ~= nil then
  4.        local fpat = "(.-)" .. pat
  5.        local last_end = 1
  6.        local s, e, cap = str:find(fpat, 1)
  7.        while s do
  8.           if s ~= 1 or cap ~= "" then
  9.          table.insert(t,cap)
  10.           end
  11.           last_end = e+1
  12.           s, e, cap = str:find(fpat, last_end)
  13.        end
  14.        if last_end <= #str then
  15.           cap = str:sub(last_end)
  16.           table.insert(t, cap)
  17.        end
  18.     else
  19.         print("##SHIPPER ERROR failed to split ["..str.."] by:"..pat)
  20.     end
  21.     return t
  22. end
  23.  
  24. local function getFirst(line, char)
  25.     local index = -1
  26.     for i=1,string.len(line) do
  27.         local c = line:sub(i, i)
  28.         if c == char and index == -1 then index = i end
  29.     end
  30.     return index
  31. end
  32.  
  33. local function getLines(dir)
  34.     if fs.exists(dir) then
  35.         local file = fs.open(dir, "r")
  36.         local lines = {}
  37.         local line = file.readLine()
  38.         local i = 0
  39.         while line ~= nil do
  40.             lines[#lines + 1] = line
  41.             line = file.readLine()
  42.             i = i + 1
  43.             if i > 100 then
  44.                 i = 0
  45.                 sleep(0)
  46.             end
  47.         end
  48.         file.close()
  49.         return lines
  50.     else
  51.         return {}
  52.     end
  53. end
  54.  
  55. local function stackEvents(dir, fdir)
  56.     local lines = getLines(dir)
  57.     local que = {}
  58.     if #lines > 0 then
  59.         local j = 0
  60.         for i=1,#lines do
  61.             local line = lines[i]
  62.             j = j + 1
  63.             if j > 100 then
  64.                 j = 0
  65.                 sleep(0)
  66.             end
  67.             local index = getFirst(line, "@")
  68.             local n = tonumber(line:sub(1, index-1))
  69.             local ev = line:sub(index+1, -1)
  70.             local splt = split(ev, ";")
  71.             local event = splt[1]
  72.             local args = {}
  73.             if #splt > 1 then
  74.                 for i=2,#splt do
  75.                     local sp = split(splt[i], ",")
  76.                     local type = sp[1]
  77.                     local val = sp[2]
  78.                     if type == "number" then
  79.                         args[#args + 1] = tonumber(val)
  80.                     elseif type == "string" then
  81.                         args[#args + 1] = tostring(val)
  82.                     else
  83.                         print("INVALID TYPE:"..type.." NOT SUPPORTED")
  84.                     end
  85.                 end
  86.             end
  87.             local evnt = {event}
  88.             if #args > 0 then for i=1,#args do evnt[#evnt + 1] = args[i] end end
  89.             if type(n) == "number" then
  90.                 for i=1,n do
  91.                     que[#que + 1] = evnt
  92.                 end
  93.             else
  94.                 que[#que + 1] = evnt
  95.             end
  96.         end
  97.     end
  98.     if #que > 0 then
  99.         local j = 0
  100.         for i=1,#que do
  101.             j = j + 1
  102.             if j > 50 then
  103.                 j = 0
  104.                 sleep(0)
  105.             end
  106.             local event = que[i]
  107.             if #event == 1 then
  108.                 os.queueEvent( event[1] )
  109.             elseif #event == 2 then
  110.                 os.queueEvent( event[1] , event[2] )
  111.             elseif #event == 3 then
  112.                 os.queueEvent( event[1] , event[2] , event[3] )
  113.             elseif #event == 4 then
  114.                 os.queueEvent( event[1] , event[2] , event[3] , event[4])
  115.             end
  116.         end
  117.     end
  118.     if fdir ~= nil and fdir:gsub(" ", "") ~= "" then
  119.         os.run({}, fdir)
  120.     end
  121. end
  122.  
  123. local stuff = {...}
  124. local dir = ""
  125. for i=1,#stuff do if dir == "" then dir = stuff[i] else dir = dir.." "..stuff[i] end end
  126.  
  127. if dir == "" then
  128.     print("Please enter directory:")
  129.     dir = read()
  130. end
  131.  
  132. print("[Optional] Enter file directory to run")
  133. local fdir = read()
  134.  
  135. stackEvents(dir, fdir)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement