ZNZNCOOP

Program_Downloader(0.1)

Dec 6th, 2014
218
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. -- Programming by NEO
  4. -- Program Downloader v0.1
  5. -- http://computercraft.ru
  6. function pastebin(adress)
  7. pastebinPOST= " http://pastebin.com/raw.php?i="
  8. if (adress == nil) then error('Adrress is not exists') end
  9. pastebinPOSTE= pastebinPOST..adress
  10. h= http.post(pastebinPOSTE)
  11. lines= {}
  12. while true do
  13.    q= h.readLine()
  14.    if (q == nil) then break end
  15.    lines[#lines+1]= q
  16. end
  17.    return lines
  18. end
  19.  
  20. function parseD(adress)
  21.    lines= pastebin(adress)
  22.    tokens= {}
  23.    s= {}
  24.    c= 0
  25.    for i=1,#lines do
  26.       for q=1,#lines[i] do
  27.          c= c+1
  28.          s[c]= string.sub(lines[i],q,q)
  29.       end
  30.    end
  31.     temp= ''
  32.     forS= false
  33.     i= 0
  34.     while i<#s do
  35.        i= i+1
  36.        forS= false
  37.        if (s[i] == ';') then
  38.           forS= true
  39.           tokens[#tokens+1]= temp
  40.           temp= ''
  41.        end
  42.        if (s[i] == '=') then
  43.           forS= true
  44.           tokens[#tokens+1]= temp
  45.           tokens[#tokens+1]= '='
  46.           temp= ''
  47.        end
  48.        if (forS~= true) then
  49.           if (s[i+1] == nil) then
  50.              temp= temp..s[i]
  51.              tokens[#tokens+1]= temp
  52.           end
  53.           temp= temp..s[i]
  54.        end
  55.    end
  56.  
  57.    name_list= {}
  58.    id_list= {}
  59.    i= 0
  60.    while i<#tokens do
  61.     i= i+1
  62.       if (tokens[i+1] == '=') then
  63.          name_list[#name_list+1]= tokens[i]
  64.          id_list[#id_list+1]= tokens[i+2]
  65.          i= i+2
  66.       end
  67.  
  68.    end
  69.    return name_list,id_list
  70. end
  71.  
  72. function drawA(arr)
  73.    for i=1,#arr do
  74.     print(arr[i])
  75.    end
  76. end
  77.  
  78. function download(index)
  79.    lines= pastebin(PROGRAMS_ADRESS[index])
  80.    path= fs.open(PROGRAMS_NAME[index],'w')
  81.    for i=1,#lines do
  82.       path.writeLine(lines[i])
  83.    end
  84.    path.close()
  85.    while true do
  86.       term.clear()
  87.       term.setBackgroundColor(8192)
  88.       term.setCursorPos(15,10)
  89.       print('File '..PROGRAMS_NAME[index] ..' downloaded')
  90.       term.setCursorPos(15,11)
  91.       print('Press Space,to return')
  92.       event,a= os.pullEvent()
  93.       if (event == 'key' and a == 57) then break end
  94.    end
  95. end
  96.  
  97. function drawAll()
  98.     term.clear()
  99.     term.setCursorPos(1,1)
  100.     term.setBackgroundColor(8)
  101.     for i=min,max do
  102.        if (i == count) then
  103.           print('                  <'..PROGRAMS_NAME[i]..'>')
  104.        else
  105.          --if (max-count>=2) then
  106.           print("                   "..PROGRAMS_NAME[i])
  107.        end
  108.     end
  109. end
  110.  
  111. function load()
  112.    PROGRAMS_NAME,PROGRAMS_ADRESS= parseD(ADRESS_LIST)
  113.    table.insert(PROGRAMS_NAME,#PROGRAMS_NAME+1,"Update")
  114.    table.insert(PROGRAMS_NAME,#PROGRAMS_NAME+1,"Re-list")
  115.    table.insert(PROGRAMS_NAME,#PROGRAMS_NAME+1,"Exit")
  116.    PROGRAMS_DESCRIPTION= pastebin(ADRESS_DISCRIPTION)
  117.    table.insert(PROGRAMS_DESCRIPTION,#PROGRAMS_NAME-2,"Udpate Program")
  118.    table.insert(PROGRAMS_DESCRIPTION,#PROGRAMS_NAME-1,"Reload Programs List")
  119.    table.insert(PROGRAMS_DESCRIPTION,#PROGRAMS_NAME,"Exit Program")
  120.  
  121. end
  122.  
  123. function TextBox()
  124.    X,Y= term.getSize()
  125.    term.setCursorPos(1,Y-1)
  126.    print("Loading...")
  127.    sleep(1)
  128. end
  129.  
  130. function description(index)
  131.    
  132.    while true do
  133.       term.clear()
  134.       term.setBackgroundColor(4096)
  135.       term.setCursorPos(1,1)
  136.       print(PROGRAMS_DESCRIPTION[index])
  137.       print('Press Space,to return')
  138.       event,a= os.pullEvent()
  139.       if (event == 'key' and a == 57) then break end
  140.    end
  141. end
  142.  
  143. function update()
  144.    fs.delete('pd')
  145.    shell.run('pastebin','get','VhdQt94A','pd')
  146.    shell.run('pd')
  147.    error()
  148. end
  149.  
  150. X,Y= term.getSize()
  151.  
  152. ADRESS_LIST= "xZHMdRXP" -- не менять
  153. ADRESS_DISCRIPTION= "5Ha4HQXx"
  154. if (ADRESS_LIST == "")  then error("Adrress = nil") end
  155. term.clear()
  156. term.setCursorPos(1,1)
  157. PROGRAMS_NAME= {}
  158. PROGRAMS_ADRESS= {}
  159. PROGRAMS_DESCRIPTION= {}
  160. load()
  161. count= 1
  162. if (Y>#PROGRAMS_NAME) then max= #PROGRAMS_NAME else max= Y end
  163. min= 1
  164. while true do
  165.    drawAll()
  166.    event,a= os.pullEvent()
  167.    if (event == 'key') then
  168.       if (a == 32) then description(count) end
  169.       if (a == 200) then count= count-1 min= min-1 max= max-1 end
  170.       if (a == 208) then count= count+1 max= max+1 max= max+1 end
  171.       if (a == 28 and max-count>2) then download(count) end
  172.       if (a == 28 and max-count == 2) then update() end
  173.       if (a == 28 and max-count == 1) then load() TextBox() end
  174.       if (a == 28 and max-count == 0) then  
  175.          term.clear()
  176.      term.setCursorPos(1,1)
  177.          error()
  178.       end
  179.    end
  180.    if (min <= 1) then min= 1 end
  181.    if (max > #PROGRAMS_NAME or max < #PROGRAMS_NAME) then max= #PROGRAMS_NAME end
  182.    if (count< min) then count= min end
  183.    if (count > max) then count= max end
  184. end
RAW Paste Data