Advertisement
wirthe16

2drpggamelauncher

Oct 21st, 2017
750
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.08 KB | None | 0 0
  1. local term = require("term")
  2. local shell = require("shell")
  3. local fs = require("filesystem")
  4. local component = require("component")
  5. local gpu = component.gpu
  6.  
  7. local args = {"update"}
  8. local guthubfolder = "https://raw.githubusercontent.com/DarthWirthe/Wirthe16_2D_RPG_Game_Wraith_Onslaught/"
  9. local ghdir = guthubfolder.."main/games/testgame/"
  10. local localpath = "/games/testgame/"
  11. local libdir = guthubfolder.."main/lib/"
  12. local libpath = "/lib/"
  13.  
  14. local data, delete = {},{}
  15.  
  16. gpu.setBackground(0x000000)
  17. gpu.setForeground(0xFFFFFF)
  18. term.clear()
  19. local t={}
  20.  
  21. t.list = {
  22. "*Загрузчик файлов для игры Wraith Onslaught*",
  23. "Для загрузки файлов необходима интернет карта.",
  24. "Системные требования игры Wraith Onslaught:",
  25. "Монитор и видеокарта 3-го уровня,",
  26. "2Мб оперативной памяти,",
  27. "(Память уровня 3.5 - 2шт.)",
  28. "500Кб свободного места на ",
  29. "жёстком диске, мышь, клавиатура.",
  30. "Потребление энергии компьютером:",
  31. "400 - 1200 EU/сек.",
  32. }
  33.  
  34. for f = 1, #t.list do
  35.     term.setCursor(2,7+f)
  36.     print(t.list[f])
  37. end
  38.  
  39. function t.downloadFromGit(gitdir,file)
  40.     return shell.execute("wget "..ghdir..gitdir.." "..file.." -fQ")
  41. end
  42.  
  43. function t.getCurrentVersion()
  44.     local st
  45.         if fs.exists(localpath.."data/version.data") then
  46.         st = loadfile(localpath.."data/version.data")()
  47.         return st:sub(1,1):byte()
  48.         end
  49.     return 1
  50. end
  51.  
  52. function t.setCurrentVersion(ver)
  53.     local file = io.open(localpath.."data/version.data","w")
  54.     file:write('return "'..string.char(ver)..'"')
  55.     file:close()
  56. end
  57.  
  58. function t.getNewestVersion()
  59.     local file, data = "/temp-005b83cf"
  60.     t.downloadFromGit("newest_version",file)
  61.     data = loadfile(file)()
  62.     fs.remove(file)
  63.     return data
  64. end
  65.  
  66. function t.getFileList(version)
  67.     local file, data1, data2 = "/temp-005b83d0"
  68.     t.downloadFromGit("upd"..version,file)
  69.     data1, data2 = loadfile(file)()
  70.     fs.remove(file)
  71.     return data1, data2
  72. end
  73.  
  74. function t.nlog(text)
  75.     gpu.setForeground(0xFFFFFF)
  76.     term.setCursor(2,2)
  77.     io.write(text..string.rep(" ",50))
  78. end
  79.  
  80. local new
  81.  
  82. function t.getUpdateList()
  83.     if component.isAvailable("internet") then
  84.     t.nlog("Проверка обновлений.")
  85.     local current, flist, dlist = t.getCurrentVersion()
  86.     new = t.getNewestVersion()
  87.         local function addToData(input)
  88.         local n = true
  89.             for f = 1, #data do
  90.                 if data[f] == input then
  91.                 n = false
  92.                 break
  93.                 end
  94.             end
  95.             for f = 1, #delete do
  96.                 if delete[f] == input then
  97.                 table.remove(delete,f)
  98.                 end
  99.             end
  100.             if n then
  101.             table.insert(data,input)
  102.             end
  103.         end
  104.         local function addToDelete(input)
  105.         local n = true
  106.             for f = 1, #delete do
  107.                 if delete[f] == input then
  108.                 n = false
  109.                 break
  110.                 end
  111.             end
  112.             for f = 1, #data do
  113.                 if data[f] == input then
  114.                 table.remove(data,f)
  115.                 end
  116.             end
  117.             if n then
  118.             table.insert(delete,input)
  119.             end
  120.         end
  121.         if new > current then
  122.             t.nlog("Текущая версия: "..current.." Новая версия: "..new.." Получение списка файлов.")
  123.             for f = current + 1, new do
  124.             flist, dlist = t.getFileList(f)
  125.                 for e = 1, #flist do
  126.                 addToData(flist[e])
  127.                 end
  128.                 for e = 1, #dlist do
  129.                 addToDelete(dlist[e])
  130.                 end
  131.             end
  132.         end
  133.     end
  134. end
  135.  
  136. function t.pbar(l,n1,n2,x,y)
  137.     term.setCursor(x,y)
  138.     io.write(string.rep("─",l))
  139.     term.setCursor(x,y+1)
  140.     io.write(string.rep("█",math.floor(n1*l/n2)))
  141.     term.setCursor(x,y+2)
  142.     io.write(string.rep("─",l))
  143. end
  144.  
  145. t.getUpdateList()
  146.  
  147. for i = 1, #delete do
  148.     if fs.exists(localpath..delete[i]) then
  149.     fs.remove(localpath..delete[i])
  150.     end
  151. end
  152.  
  153. local att
  154.  
  155. for i = 1, #data do
  156.     att = 1
  157.     term.setCursor(2,3)
  158.     io.write(math.floor(i*100/#data).."%   ")
  159.     t.pbar(80,i,#data,2,4)
  160.     t.nlog("Проверка "..data[i])
  161.     t.d1,t.d2 = ghdir, localpath
  162.     if data[i]:sub(1,2) == "G_" then t.d1,t.d2 = libdir, libpath end
  163.     t.d3 = t.d2..data[i]
  164.         if not fs.exists(t.d3) or fs.size(t.d3) == 0 or args[1] == "update" then
  165.         if not component.isAvailable("internet") then term.clear(); error("Для запуска нужна интернет-карта!") end
  166.         t.nlog("Скачивание "..data[i])
  167.         fs.makeDirectory(fs.path(t.d3) or "")
  168.             while att < 100 do
  169.             t.e = shell.execute("wget "..t.d1..data[i].." "..t.d3.." -fQ")
  170.             if fs.size(t.d3) == 0 or t.e == nil then
  171.             fs.remove(t.d3)  
  172.             att = att + 1
  173.             t.nlog("Ошибка запроса. Попытка "..att.."/30")
  174.             os.sleep(1)
  175.             else
  176.             t.nlog("Успешно!")
  177.             break
  178.             end
  179.             if att == 30 then att = nil; break end
  180.             end
  181.         end
  182.     if not att then term.clear(); t.nlog("Нет соединения с github.com."); break end
  183.     os.sleep()
  184. end
  185. if att or ( #data == 0 and #delete == 0 ) then
  186.     t.setCurrentVersion(new)
  187.     term.clear()
  188.     t.nlog("Загрузка завершена. Директория исполняемого файла: /games/testgame/game.lua")
  189.     os.sleep(2)
  190.     data,delete,t,att=nil
  191.     shell.execute("/games/testgame/game.lua")
  192. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement