Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- do --Readable XT downloader
- --Configs
- local inert=function()end; --A function that does nothing. Use it to disable prints or read
- local cprint=print; --Logs an info message
- local aprint=print; --Prints an asking message, that the user must see to know what to input
- local cerr=printError; --Prints an error in red, if possible
- local cread=read; --Receives user input. If it returns "yes", answer is positive
- local xtpath="/xt"; --Where xt should be loaded and downloaded
- local autoupdate=true; --Wether updates should always be checked, or just when necessary
- local lastver=false;
- local lastverpaste=false;
- local function checkForUpdate()
- if http then
- cprint("Checking for updates...");
- local lastverpage=http.get("http://www.pastebin.com/raw.php?i=BHpUQ8Hn");
- if lastverpage then
- lastver=tonumber(lastverpage.readLine());
- lastverpaste=lastverpage.readLine();
- lastverpage.close();
- if not lastver or not lastverpaste then
- cerr("Update paste corrupted");
- lastver=false;
- lastverpaste=false;
- end
- else
- cerr("Cant check for updates");
- end
- else
- cerr("Cant check for updates, http disabled");
- end
- end
- local myver=false;
- local asktodelete=false;
- local loadedver=false;
- local mybestver=false;
- local function getMyVersion()
- if fs.isDir(xtpath) then
- cprint("\""..xtpath.."\" is a directory, not XT api");
- myver=false;
- asktodelete=true;
- elseif fs.exists(xtpath) then
- local handle=fs.open(xtpath,"r");
- local fline=handle.readLine();
- handle.close();
- if fline:sub(1,6)=="--XT V" and fline:sub(10)==" by negamartin" and tonumber(fline:sub(7,9)) then
- myver=tonumber(fline:sub(7,9));
- cprint("\""..xtpath.."\" is XT api V"..myver);
- else
- myver=false;
- asktodelete=true;
- cprint("\""..xtpath.."\" is a non-XT file");
- end
- else
- cprint("\""..xtpath.."\" does not exist");
- myver=false;
- end
- if xt then
- loadedver=xt.version;
- if type(loadedver)=="number" then
- cprint("XT V"..loadedver.." is already loaded");
- else
- loadedver=false;
- end
- else
- loadedver=false;
- end
- if myver then
- if loadedver then
- mybestver=math.max(myver,loadedver);
- else mybestver=myver; end
- else mybestver=loadedver; end
- end
- local function downloadXT()
- if lastver and lastverpaste then
- if http then
- cprint("Downloading XT V"..lastver.."...");
- local xtpage=http.get("http://www.pastebin.com/raw.php?i="..lastverpaste);
- if xtpage then
- local handle=fs.open(xtpath,"w");
- handle.write(xtpage.readAll());
- handle.close();
- xtpage.close();
- myver=lastver;
- return true;
- else
- cerr("Cant download XT");
- end
- else
- cerr("Cant download XT, http disabled");
- end
- end
- return false;
- end
- getMyVersion();
- if autoupdate or not mybestver then
- checkForUpdate();
- if lastver then
- if mybestver then
- if mybestver<lastver then
- local doit=true;
- if asktodelete then
- aprint("A non-XT file has been found in \""..xtpath.."\"");
- aprint("Overwrite it? (\"yes\" or \"no\")");
- if cread()=="yes" then
- fs.delete(xtpath);
- else
- aprint("Remove the non-XT file if you wish to update");
- doit=false;
- end
- end
- if doit then
- if downloadXT() then
- cprint("Updated XT to V"..lastver);
- else
- cprint("Couldn't download XT, using offline version");
- end
- end
- else
- cprint("XT up to date");
- end
- else
- if asktodelete then
- aprint("A non-XT file has been found in \""..xtpath.."\"");
- aprint("Overwrite it? (\"yes\" or \"no\")");
- if cread()=="yes" then
- fs.delete(xtpath);
- else
- aprint("Move the non-XT file and try again");
- error();
- end
- end
- if downloadXT() then
- cprint("XT V"..lastver.." downloaded");
- else
- cerr("Couldn't download xt");
- error();
- end
- end
- else
- if mybestver then
- cprint("Couldn't update, using offline version");
- else
- cerr("Couldn't download xt, and no offline version");
- error();
- end
- end
- end
- if loadedver then
- if myver and myver>loadedver then
- dofile(xtpath);
- end
- else
- dofile(xtpath);
- end
- end
Add Comment
Please, Sign In to add comment