Advertisement
CrazedProgrammer

Strafe

Apr 18th, 2015 (edited)
10,943
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.09 KB | None | 0 0
  1. -- Strafe version 1.0 by CrazedProgrammer
  2. -- This program needs the Surface API 1.5.3 in the strafedata directory.
  3. -- If the Surface API 1.5.3 doesn't exist it will try to download it.
  4. -- You can find info and documentation on these pages:
  5. --
  6. -- You may use this in your ComputerCraft OSes and modify it without asking.
  7. -- However, you may not publish this program under your name without asking me.
  8. -- If you have any suggestions, bug reports or questions then please send an email to:
  9. -- crazedprogrammer@gmail.com
  10.  
  11. function split(pString, pPattern)
  12.   local Table = {}  -- NOTE: use {n = 0} in Lua-5.0
  13.   local fpat = "(.-)" .. pPattern
  14.   local last_end = 1
  15.   local s, e, cap = pString:find(fpat, 1)
  16.   while s do
  17.     if s ~= 1 or cap ~= "" then
  18.       table.insert(Table,cap)
  19.     end
  20.     last_end = e+1
  21.     s, e, cap = pString:find(fpat, last_end)
  22.   end
  23.   if last_end <= #pString then
  24.     cap = pString:sub(last_end)
  25.     table.insert(Table, cap)
  26.   end
  27.   return Table
  28. end
  29.  
  30. function install(game)
  31.   if not fs.isDir(dir.."strafedata/"..game) then
  32.     fs.delete(dir.."strafedata/"..game)
  33.     fs.makeDir(dir.."strafedata/"..game)
  34.   end
  35.   local _d = shell.dir()
  36.   local i = split(online[game].install, "|")
  37.   term.setTextColor(colors.white)
  38.   term.setBackgroundColor(colors.black)
  39.   term.setCursorPos(1, 1)
  40.   term.clear()
  41.   shell.run("cd /"..dir.."strafedata/"..game)
  42.   for k,v in pairs(i) do
  43.     shell.run(({v:gsub("?", dir.."strafedata/"..game)})[1])
  44.   end
  45.   shell.run("cd /".._d)
  46.   online[game].banner = online[game].banner:saveString()
  47.   local f = fs.open(dir.."strafedata/"..game..".tab", "w")
  48.   f.write(textutils.serialize(online[game]))
  49.   f.close()
  50.   online[game].banner = surface.loadString(online[game].banner)
  51.   timer = os.startTimer(0)
  52.   updateGames()
  53.   updateOnline()
  54. end
  55.  
  56. function updateGames()
  57.   games = { }
  58.   for k,v in pairs(fs.list(dir.."strafedata")) do
  59.     if v:sub(#v - 3, #v) == ".tab" then
  60.       local name = v:sub(1, #v - 4)
  61.       local f = fs.open(dir.."strafedata/"..v, "r")
  62.       games[name] = textutils.unserialize(f.readAll())
  63.       games[name]["banner"] = surface.loadString(games[name]["banner"])
  64.       f.close()
  65.     end
  66.   end
  67. end
  68.  
  69. function updateOnline()
  70.   http.request("https://pastebin.com/raw/m9YK1tke")
  71. end
  72.  
  73. function updateSize()
  74.   width, height = term.getSize()
  75.   rows = math.floor((width - 1) / 25)
  76.   offset = math.floor((width - rows * 25 + 1) / 2)
  77.   surf = surface.create(width, height)
  78. end
  79.  
  80. function update()
  81.   draw()
  82. end
  83.  
  84. function draw()
  85.   surf:clear(nil, colors.white, colors.black)
  86.   if state == 1 then
  87.     drawGames()
  88.   elseif state == 2 then
  89.     drawOnline()
  90.   end
  91.   surf:drawLine(1, 1, width, 1, " ", colors.lightGray, colors.black)
  92.   if width >= 43 then
  93.     surf:drawText(1, 1, "Strafe")
  94.     surf:drawText(9, 1, "Installed Games  Download Games", nil, colors.blue)
  95.   else
  96.     surf:drawText(1, 1, "Installed  Download", nil, colors.blue)
  97.   end
  98.   surf:drawPixel(width, 1, "X", colors.red, colors.white)
  99.   surf:drawPixel(width, 2, "^")
  100.   surf:drawPixel(width, height, "v")
  101.   surf:render()
  102. end
  103.  
  104. function drawGames()
  105.   local i = 0
  106.   for k,v in pairs(games) do
  107.     surf:drawSurface((i % rows) * 25 + 1 + offset, math.floor(i / rows) * 6 + 3 - gamesOffset, v.banner)
  108.     surf:drawText((i % rows) * 25 + 1 + offset, math.floor(i / rows) * 6 + 7 - gamesOffset, "Play Delete", colors.black, colors.white)
  109.     i = i + 1
  110.   end
  111. end
  112.  
  113. function drawOnline()
  114.   local i = 0
  115.   for k,v in pairs(online) do
  116.     surf:drawSurface((i % rows) * 25 + 1 + offset, math.floor(i / rows) * 6 + 3 - onlineOffset, v.banner)
  117.     local str = "Download"
  118.     if games[k] then
  119.       if games[k].version == v.version then
  120.         str = "Installed"
  121.       else
  122.         str = "Update"
  123.       end
  124.     end
  125.     surf:drawText((i % rows) * 25 + 1 + offset, math.floor(i / rows) * 6 + 7 - onlineOffset, str, colors.black, colors.white)
  126.     i = i + 1
  127.   end
  128. end
  129.  
  130. function onClick(x, y)
  131.   if y == 1 then
  132.     if x == width then
  133.       term.setTextColor(colors.white)
  134.       term.setBackgroundColor(colors.black)
  135.       term.setCursorPos(1, 1)
  136.       term.clear()
  137.       running = false
  138.     elseif width >= 43 then
  139.       if x >= 9 and x <= 23 then
  140.         updateGames()
  141.         state = 1
  142.       elseif x >= 26 and x <= 39 then
  143.         updateOnline()
  144.         state = 2
  145.       end
  146.     else
  147.       if x >= 1 and x <= 9 then
  148.         updateGames()
  149.         state = 1
  150.       elseif x >= 12 and x <= 19 then
  151.         updateOnline()
  152.         state = 2
  153.       end
  154.     end
  155.   elseif x == width and y == 2 then
  156.     if state == 1 and gamesOffset > 0 then
  157.       gamesOffset = gamesOffset - 6
  158.     elseif onlineOffset > 0 then
  159.       onlineOffset = onlineOffset - 6
  160.     end
  161.   elseif x == width and y == height then
  162.     if state == 1 then
  163.       gamesOffset = gamesOffset + 6
  164.     else
  165.       onlineOffset = onlineOffset + 6
  166.     end
  167.   elseif y > 1 and state == 1 then
  168.     local id = math.floor((y + gamesOffset - 2) / 6) * rows
  169.     if x - offset + 1 <= rows * 25 then
  170.       id = id + math.floor((x - offset) / 25)
  171.     end
  172.     local xx = (x - offset) % 25
  173.     local yy = (y - 2) % 6
  174.     local i = 0
  175.     for k,v in pairs(games) do
  176.       if id == i then
  177.         if xx >= 1 and xx <= 4 and yy == 5 then
  178.           local d = shell.dir()
  179.           shell.run("cd /"..dir.."strafedata/"..k.."/"..v.launchdir)
  180.           term.setTextColor(colors.white)
  181.           term.setBackgroundColor(colors.black)
  182.           term.setCursorPos(1, 1)
  183.           term.clear()
  184.           shell.run(v.launch)
  185.           shell.run("cd /"..d)
  186.           timer = os.startTimer(0)
  187.           updateSize()
  188.         elseif xx >= 6 and xx <= 11 and yy == 5 then
  189.           fs.delete(dir.."strafedata/"..k)
  190.           fs.delete(dir.."strafedata/"..k..".tab")
  191.           updateGames()
  192.         end
  193.       end
  194.       i = i + 1
  195.     end
  196.   elseif y > 1 and state == 2 then
  197.     local id = math.floor((y + onlineOffset - 2) / 6) * rows
  198.     if x - offset + 1 <= rows * 25 then
  199.       id = id + math.floor((x - offset) / 25)
  200.     end
  201.     local xx = (x - offset) % 25
  202.     local yy = (y - 2) % 6
  203.     local i = 0
  204.     for k,v in pairs(online) do
  205.       if id == i then
  206.         if xx >= 1 and xx <= 8 and yy == 5 and not games[k] then
  207.           install(k)
  208.         elseif xx >= 1 and xx <= 6 and yy == 5 and games[k] then
  209.           if games[k].version ~= v.version then
  210.             fs.delete(dir.."strafedata/"..k)
  211.             fs.delete(dir.."strafedata/"..k..".tab")
  212.             install(k)
  213.           end
  214.         end
  215.       end
  216.       i = i + 1
  217.     end
  218.   end
  219. end
  220.  
  221. dir = fs.getDir(shell.getRunningProgram()).."/"
  222. if not fs.isDir(dir.."strafedata") then
  223.   fs.delete(dir.."strafedata")
  224.   fs.makeDir(dir.."strafedata")
  225. end
  226. if not fs.exists(dir.."strafedata/surface") or fs.isDir(dir.."strafedata/surface") then
  227.   fs.delete(dir.."strafedata/surface")
  228.   local d = shell.dir()
  229.   shell.run("cd /"..dir.."strafedata")
  230.   shell.run("pastebin get J2Y288mW surface")
  231.   shell.run("cd /"..d)
  232. end
  233. os.loadAPI(dir.."strafedata/surface")
  234. updateSize()
  235. local _off = math.floor(width / 2) surf:drawText(1, 1, "Made by CrazedProgrammer") local cp = surface.loadString("_00100010208f208f208f208f208f208f208f208f208f208f208f208f208f208f208f208f208f20ff20ff20ff20ff20ff20ff20ff20ff20ff20ff20ff20ff20ff20ff207f208f20ff20ff200f200f20ff200f200f20ff20ff20ff20ff20ff20ff20ff207f208f20ff200f20ff20ff20ff200f20ff200f20ff20ff20ff20ff20ff20ff207f208f20ff200f20ff20ff20ff200f200f20ff20ff20ff20ff20ff20ff20ff207f208f20ff200f20ff20ff20ff200f20ff20ff20ff20ff20ff20ff20ff20ff207f208f20ff20ff200f200f20ff200f20ff20ff20ff200f200f200f20ff20ff207f208f20ff20ff20ff20ff20ff20ff20ff20ff20ff20ff20ff20ff20ff20ff207f208f207f207f207f207f207f207f207f207f207f207f207f207f207f207f207f20_720_720_720_720_720_720_72077207720_720_720_720_720_720_720_720_720_720_720_720_720_7207720772077207720_720_720_720_720_720_720872087208720872087208720872087208720872087208720872087208720872087208720872087208720872087208720872087208720872087208720872077208720772077207720772077208720772077208720772077208720b72087207720872087208720872087208720872087208720872087208720872087208720772077207720772077207720772077207720772077207720772077207720772077") for i=1,16,1 do local surf2 = surface.create(i, i) surf2:drawSurfaceScaled(1, 1, i, i, cp) surf:fillRect(1, 2, width, height, nil, colors.black) surf:drawSurfaceRotated(_off, 10, i / 2, i / 2, 4 - i / 4, surf2) surf:render() os.sleep(0) end for i=1,4,1 do surf:fillRect(1, 2, width, height, nil, colors.black) surf:drawSurface(_off - 7, 3, cp) surf:drawLine(_off - 7, i * 4, _off + i * 4 - 11, 3, nil, colors.white) surf:drawLine(_off - 7, 1 + i * 4, _off + i * 4 - 10, 3, nil, colors.white) surf:drawLine(_off - 7, 2 + i * 4, _off + i * 4 - 9, 3, nil, colors.white) surf:render() os.sleep(0) end for i=1,4,1 do surf:fillRect(1, 2, width, height, nil, colors.black) surf:drawSurface(_off - 7, 3, cp) surf:drawLine(_off + i * 4 - 11, 18, _off + 8, i * 4, nil, colors.white) surf:drawLine(_off + i * 4 - 10, 18, _off + 8, 1 + i * 4, nil, colors.white) surf:drawLine(_off + i * 4 - 9, 18, _off + 8, 2 + i * 4, nil, colors.white) surf:render() os.sleep(0) end
  236. games = { }
  237. online = { }
  238. updateGames()
  239. state = 1
  240. gamesOffset = 0
  241. onlineOffset = 0
  242. running = true
  243. timer = os.startTimer(0)
  244. while running do
  245.   local e = {os.pullEvent()}
  246.   if e[1] == "timer" and e[2] == timer then
  247.     timer = os.startTimer(0)
  248.     update()
  249.   elseif e[1] == "mouse_click" then
  250.     onClick(e[3], e[4])
  251.   elseif e[1] == "mouse_scroll"then
  252.     if e[2] == -1 then
  253.       if state == 1 and gamesOffset + e[2] * 6 >= 0 then
  254.         gamesOffset = gamesOffset + e[2] * 6
  255.       elseif state == 2 and onlineOffset > 0 then
  256.         onlineOffset = onlineOffset + e[2] * 6
  257.       end
  258.     else
  259.       if state == 1 then
  260.         gamesOffset = gamesOffset + e[2] * 6
  261.       else
  262.         onlineOffset = onlineOffset + e[2] * 6
  263.       end
  264.     end
  265.   elseif e[1] == "term_resize" then
  266.     updateSize()
  267.   elseif e[1] == "http_success" and e[2] == "https://pastebin.com/raw/m9YK1tke" then
  268.     online = textutils.unserialize(e[3].readAll())
  269.     e[3].close()
  270.     for k,v in pairs(online) do
  271.       v.banner = surface.loadString(v.banner)
  272.     end
  273.   end
  274. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement