Advertisement
joebodo

modified-strafe-CrazedProgrammer

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