gknova61

State

Dec 29th, 2012
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.56 KB | None | 0 0
  1. loaded = false
  2. dir = ""
  3. file = ""
  4. path = ""
  5. internal = {}
  6.  
  7. tArgs = {...}
  8. local function create()
  9.         local file = fs.open(path,"a")
  10.         file.close()
  11. end
  12.  
  13. function autoUpdate( fileName2,pastebinRaw )
  14.     while true do
  15.         local downloadedFile = http.request("http://pastebin.com/raw.php?i="..pastebinRaw)
  16.         local event, url, body = os.pullEvent()
  17.         if event == "http_success" then
  18.             _tBody = body.readAll()
  19.                 break
  20.         elseif event == "http_failed" then
  21.                 break
  22.         end
  23.     end
  24.     file = fs.open(fileName2, "r")
  25.     _fBody = file.readAll()
  26.     file.close()
  27.     local selection = 1
  28.     if _fBody ~= _tBody then
  29.     print("Update found!")
  30.     sleep(0.5)
  31.         fs.delete(fileName2)
  32.         file = fs.open(fileName2, "w")
  33.         file.write(_tBody)
  34.         file.close()
  35.         --shell.run
  36.         error()
  37.     end
  38. end
  39. autoUpdate("state","xQLT79bH")
  40.  
  41. local function load(directory,fileName)
  42.         dir = directory
  43.         file = fileName
  44.         if dir ~= nil then
  45.                 path = dir.."/"..file
  46.                 fs.makeDir(dir)
  47.                 create(path)
  48.                 local file = fs.open(path, "r")
  49.                 repeat
  50.                         line = file.readLine()
  51.                         if(line ~= nil) then
  52.                                 local asWords = line:gsub(":","")
  53.                                 local parts = {}
  54.                                 for word in asWords:gmatch("%w+") do table.insert(parts,word) end
  55.                                 internal[parts[1]] = parts[2]
  56.                         end
  57.                 until line == nil
  58.                 loaded = true
  59.                 file.close()
  60.         else
  61.                 path = file
  62.                 create(path)
  63.                 local file = fs.open(path, "r")
  64.                 repeat
  65.                         line = file.readLine()
  66.                         if(line ~= nil) then
  67.                                 local asWords = line:gsub(":","")
  68.                                 local parts = {}
  69.                                 for word in asWords:gmatch("%w+") do table.insert(parts,word) end
  70.                                 internal[parts[1]] = parts[2]
  71.                         end
  72.                 until line == nil
  73.                 loaded = true
  74.                 file.close()
  75.         end
  76. end
  77.  
  78. local function writeVal(key,value)
  79.         if(loaded == false) then
  80.                 return false
  81.         else
  82.                 local toWrite = value
  83.                 if(value == true) then toWrite = "true"
  84.                 elseif(value == false) then toWrite = "false" end
  85.                 internal[key] = toWrite
  86.                 return true
  87.         end
  88. end
  89.  
  90. local function readVal(key)
  91.         if(loaded == false) then
  92.                 return nil
  93.         end
  94.         toReturn = internal[key]
  95.         if(toReturn == "true") then return true
  96.         elseif(toReturn == "false") then return false
  97.         else return internal[key] end
  98. end
  99.  
  100. local function save()
  101.         if(loaded == true) then
  102.                 local file = fs.open(path,"w")
  103.                 for i,v in pairs(internal) do
  104.                         file.writeLine(i.." = "..v)
  105.                 end
  106.                 file.close()
  107.                 internal = {}
  108.                 loaded = false
  109.                 return true
  110.         else
  111.                 return false
  112.         end
  113. end
  114.  
  115. local function isEmpty()
  116.         if(loaded == false) then
  117.                 return nil
  118.         end
  119.         if(fs.getSize(path) == 0)then
  120.                 return true
  121.         else return false end
  122. end
  123.  
  124. local function printUsage()
  125.     print("Usage:")
  126.     print(shell.getRunningProgram().." <time-to-run> <interval> <side> <inputSide> (pulseTime)")
  127.     print("Parameters:")
  128.     print("[] = Required")
  129.     print("() = Optional")
  130.     return true
  131. end
  132.  
  133. local function errorCheck()
  134.     local sides = rs.getSides()
  135.     local tArg1Valid = false
  136.     local tArg2Valid = false
  137.     local tArg3Valid = false
  138.    
  139.     for i=1,#sides do
  140.         if tArgs[3] == sides[i] then
  141.         tArg2Valid = true
  142.         break
  143.         end
  144.     end
  145.    
  146.     for i=1,#sides do
  147.         if tArgs[4] == sides[i] then
  148.         tArg3Valid = true
  149.         break
  150.         end
  151.     end
  152.    
  153.     if tonumber(tArgs[2]) == nil then
  154.         error("Interval must be a number!")
  155.     end
  156.    
  157.     if tArg2Valid then
  158.     else
  159.         error("Invalid side!")
  160.     end
  161.    
  162.     if tArg3Valid then
  163.     elseif not tArg3Valid then
  164.         error("Invalid input side!")
  165.     end
  166. end
  167.  
  168. if #tArgs < 4 and not fs.exists("config") then
  169.     printUsage()
  170.     return false
  171. end
  172.  
  173. if tArgs[1] == "usage" then
  174.     printUsage()
  175.     return false
  176. end
  177.  
  178. errorCheck()
  179.  
  180. if noArgsGood == nil then
  181.     timeToRun = tonumber(tArgs[1])
  182.     interval = tonumber(tArgs[2])
  183.     side = tArgs[3]
  184.     inputStop = tArgs[4]
  185.     pulseTime = tonumber(tArgs[5])
  186.     load(nil,"config")
  187.     writeVal("interval",tostring(interval))
  188.     writeVal("side",side)
  189.     writeVal("inputStop",inputStop)
  190.     writeVal("pulseTime",tostring(pulseTime))
  191.     writeVal("timeToRun",tostring(timeToRun))
  192.     save()
  193. elseif noArgsGood then
  194.     load(nil,"config")
  195.     interval = tonumber(readVal("interval"))
  196.     side = readVal("side")
  197.     inputStop = readVal("inputStop")
  198.     pulseTime = tonumber(readVal("pulseTime"))
  199.     timeToRun = tonumber(readVal("timeToRun"))
  200.     save()
  201. end
  202.  
  203. if pulseTime == nil then
  204.     pulseTime = 0.2
  205. end
  206.  
  207. while true do
  208.   intervalTimer = os.startTimer(interval)
  209.   print("Declared variable intervalTimer")
  210.   local event = {os.pullEvent()}
  211.   if event[1] == "redstone" and rs.getInput(inputStop) then
  212.     rs.setOutput(side,true)
  213.     sleep(pulseTime)
  214.     rs.setOutput(side,false)
  215.   elseif event[1] == "redstone" and not rs.getInput(inputStop) then
  216.     if timeToRunTimer == nil then
  217.         timeToRunTimer = print(os.startTimer(timeToRun))
  218.     end
  219.     rs.setOutput(side,true)
  220.     sleep(pulseTime)
  221.     rs.setOutput(side,false)
  222.   elseif event[1] == "timer" then
  223.     if rs.getInput(inputStop) then
  224.         print("Getting front")
  225.         rs.setOutput(side,true)
  226.         sleep(pulseTime)
  227.         rs.setOutput(side,false)
  228.     elseif not rs.getInput(inputStop) then
  229.         print("not getting front")
  230.         if timeToRunTimer == nil then
  231.             timeToRunTimer = print(os.startTimer(timeToRun))
  232.         end
  233.         rs.setOutput(side,true)
  234.         sleep(pulseTime)
  235.         rs.setOutput(side,false)
  236.     end
  237.     if event[2] == intervalTimer then
  238.         print("doing redstone stuff")
  239.         rs.setOutput(side,true)
  240.         sleep(pulseTime)
  241.         rs.setOutput(side,false)
  242.     elseif event[2] == timeToRunTimer then
  243.         print("event[2] == timeToRunTimer")
  244.         local timeToRunTimer = nil
  245.         rs.setOutput(side,false)
  246.         while true do
  247.             local event = {os.pullEvent("redstone")}
  248.             if event[1] == "redstone" and rs.getInput(inputStop) then
  249.                 break
  250.             end
  251.         end
  252.     end
  253.   end
  254. end
Advertisement
Add Comment
Please, Sign In to add comment