Advertisement
CrazedProgrammer

Strafe

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