negamartin

Readable XT2 downloader

Sep 13th, 2015
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.26 KB | None | 0 0
  1. do      --Readable XT downloader
  2.     --Configs
  3.     local inert=function()end;  --A function that does nothing. Use it to disable prints or read
  4.     local cprint=print;         --Logs an info message
  5.     local aprint=print;         --Prints an asking message, that the user must see to know what to input
  6.     local cerr=printError;      --Prints an error in red, if possible
  7.     local cread=read;           --Receives user input. If it returns "yes", answer is positive
  8.     local xtpath="/xt";         --Where xt should be loaded and downloaded
  9.     local autoupdate=true;      --Wether updates should always be checked, or just when necessary
  10.    
  11.     local lastver=false;
  12.     local lastverpaste=false;
  13.     local function checkForUpdate()
  14.         if http then
  15.             cprint("Checking for updates...");
  16.             local lastverpage=http.get("http://www.pastebin.com/raw.php?i=BHpUQ8Hn");
  17.             if lastverpage then
  18.                 lastver=tonumber(lastverpage.readLine());
  19.                 lastverpaste=lastverpage.readLine();
  20.                 lastverpage.close();
  21.                 if not lastver or not lastverpaste then
  22.                     cerr("Update paste corrupted");
  23.                     lastver=false;
  24.                     lastverpaste=false;
  25.                 end
  26.             else
  27.                 cerr("Cant check for updates");
  28.             end
  29.         else
  30.             cerr("Cant check for updates, http disabled");
  31.         end
  32.     end
  33.    
  34.     local myver=false;
  35.     local asktodelete=false;
  36.     local loadedver=false;
  37.     local mybestver=false;
  38.     local function getMyVersion()
  39.         if fs.isDir(xtpath) then
  40.             cprint("\""..xtpath.."\" is a directory, not XT api");
  41.             myver=false;
  42.             asktodelete=true;
  43.         elseif fs.exists(xtpath) then
  44.             local handle=fs.open(xtpath,"r");
  45.             local fline=handle.readLine();
  46.             handle.close();
  47.             if fline:sub(1,6)=="--XT V" and fline:sub(10)==" by negamartin" and tonumber(fline:sub(7,9)) then
  48.                 myver=tonumber(fline:sub(7,9));
  49.                 cprint("\""..xtpath.."\" is XT api V"..myver);
  50.             else
  51.                 myver=false;
  52.                 asktodelete=true;
  53.                 cprint("\""..xtpath.."\" is a non-XT file");
  54.             end
  55.         else
  56.             cprint("\""..xtpath.."\" does not exist");
  57.             myver=false;
  58.         end
  59.         if xt then
  60.             loadedver=xt.version;
  61.             if type(loadedver)=="number" then
  62.                 cprint("XT V"..loadedver.." is already loaded");
  63.             else
  64.                 loadedver=false;
  65.             end
  66.         else
  67.             loadedver=false;
  68.         end
  69.         if myver then
  70.             if loadedver then
  71.                 mybestver=math.max(myver,loadedver);
  72.             else mybestver=myver; end
  73.         else mybestver=loadedver; end
  74.     end
  75.    
  76.     local function downloadXT()
  77.         if lastver and lastverpaste then
  78.             if http then
  79.                 cprint("Downloading XT V"..lastver.."...");
  80.                 local xtpage=http.get("http://www.pastebin.com/raw.php?i="..lastverpaste);
  81.                 if xtpage then
  82.                     local handle=fs.open(xtpath,"w");
  83.                     handle.write(xtpage.readAll());
  84.                     handle.close();
  85.                     xtpage.close();
  86.                     myver=lastver;
  87.                     return true;
  88.                 else
  89.                     cerr("Cant download XT");
  90.                 end
  91.             else
  92.                 cerr("Cant download XT, http disabled");
  93.             end
  94.         end
  95.         return false;
  96.     end
  97.    
  98.     getMyVersion();
  99.     if autoupdate or not mybestver then
  100.         checkForUpdate();
  101.         if lastver then
  102.             if mybestver then
  103.                 if mybestver<lastver then
  104.                     local doit=true;
  105.                     if asktodelete then
  106.                         aprint("A non-XT file has been found in \""..xtpath.."\"");
  107.                         aprint("Overwrite it? (\"yes\" or \"no\")");
  108.                         if cread()=="yes" then
  109.                             fs.delete(xtpath);
  110.                         else
  111.                             aprint("Remove the non-XT file if you wish to update");
  112.                             doit=false;
  113.                         end
  114.                     end
  115.                     if doit then
  116.                         if downloadXT() then
  117.                             cprint("Updated XT to V"..lastver);
  118.                         else
  119.                             cprint("Couldn't download XT, using offline version");
  120.                         end
  121.                     end
  122.                 else
  123.                     cprint("XT up to date");
  124.                 end
  125.             else
  126.                 if asktodelete then
  127.                     aprint("A non-XT file has been found in \""..xtpath.."\"");
  128.                     aprint("Overwrite it? (\"yes\" or \"no\")");
  129.                     if cread()=="yes" then
  130.                         fs.delete(xtpath);
  131.                     else
  132.                         aprint("Move the non-XT file and try again");
  133.                         error();
  134.                     end
  135.                 end
  136.                 if downloadXT() then
  137.                     cprint("XT V"..lastver.." downloaded");
  138.                 else
  139.                     cerr("Couldn't download xt");
  140.                     error();
  141.                 end
  142.             end
  143.         else
  144.             if mybestver then
  145.                 cprint("Couldn't update, using offline version");
  146.             else
  147.                 cerr("Couldn't download xt, and no offline version");
  148.                 error();
  149.             end
  150.         end
  151.     end
  152.     if loadedver then
  153.         if myver and myver>loadedver then
  154.             dofile(xtpath);
  155.         end
  156.     else
  157.         dofile(xtpath);
  158.     end
  159. end
Add Comment
Please, Sign In to add comment