Advertisement
joebodo

modified-strafe-CrazedProgrammer

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