negamartin

Readable non-HTTP XT2 extractor

Oct 2nd, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 31.19 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 should see
  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.     local inlineXT=[[--XT V2.0 by negamartin
  11.     setfenv(1,setmetatable({},{__index=getfenv(1)}))local xt={}xt.version=2.0;function xt.escapeString(c,d)local e=""for f in c:gmatch(".")do e=e..(d[f]or f) end;return e end;function xt.descapeString(g,h,j)local k=""local l=false;if j then for f in g:gmatch(".")do if l then k=k..h[f]l=false else if f==j then l=true else k=k..f end end end else for f in g:gmatch(".")do if l then k=k..l[f]l=false else l=h[f]if not l then k=k..f end end end end;return k end;local m=string.char(30)local n=string.char(31)xt.safeEscaper={["\\"]="\\\\",["\n"]="\\n",["\r"]="\\r",[m]="\\"..m,[n]="\\"..n}xt.safeDescaper={["\\"]={["\\"]="\\",["n"]="\n",["r"]="\r",[m]=m,[n]=n},[m]={},[n]={}}for i=128,255 do if i==138 then xt.safeEscaper[string.char(138)]=n.."n"xt.safeDescaper[n]["n"]=string.char(138)elseif i==141 then xt.safeEscaper[string.char(141)]=n.."r"xt.safeDescaper[n]["r"]=string.char(141)else xt.safeEscaper[string.char(i)]=m..string.char(i-128)xt.safeDescaper[m][string.char(i-128)]=string.char(i)end end;function xt.safeString(o)return xt.escapeString(o,xt.safeEscaper)end;function xt.unsafeString(q)return xt.descapeString(q,xt.safeDescaper)end;function xt.isUnsafe(r)for f in r:gmatch(".")do local b=f:byte()if b==10 or b==13 or b>127 then return true end end;return false end;xt.nativeFuncs={}xt.serializeEscaper={[","]="|.",["="]="|-",["{"]="|[",["}"]="|]",["|"]="||"}xt.serializeDescaper={["."]=",",["-"]="=",["["]="{",["]"]="}",["|"]="|"}function xt.stringify(s,t,u,v,w,x)local y=type(s)local z;if y=="string"then local A="s"if xt.isUnsafe(s)then s=xt.safeString(s)A="u"end;z=A..xt.escapeString(s,xt.serializeEscaper)elseif y=="number"then z="n"..tostring(s)elseif y=="boolean"then if s then z="bt"else z="bf"end elseif y=="table"then if u[s]then z="p"..u[s]else u[0]=u[0]+1;u[s]=u[0]z="{"for B,C in pairs(s)do local D,E;D=xt.stringify(B,t+1,u,v,w,x)E=xt.stringify(C,t+1,u,v,w,x)z=z..D.."="..E..","end;if w and type(getmetatable(s))=="table"then z=z.."}:"..xt.stringify(getmetatable(s),t+1,u,v,w,x)else z=z.."}"end end elseif y=="function"then if v then local F,dump=pcall(string.dump,s)if F then z="f"..xt.escapeString(xt.safeString(dump),xt.serializeEscaper)elseif x then local G=false;G=xt.nativeFuncs[s]if G then local H=_G or type(getfenv)=="function"and getfenv(1)for I in G:gmatch("[^%.]+")do if type(H)~="table"then H=false;break end;H=H[I]if type(H)~="table"and type(H)~="function"then H=false;break end end;if not H then xt.nativeFuncs[s]=nil;G=false end end;if not G then local J={}function inspectTab(K)for B,C in pairs(K)do if type(B)=="string"then if C==s then return B elseif type(C)=="table"then if not J[C]then J[C]=true;local L=inspectTab(C)if L then return B.."."..L end end end end end end;G=inspectTab(_G)xt.nativeFuncs[s]=G end;if G then z="j"..G else error("Could not serialize native function ["..tostring(s).."], nested "..t-1 .." tables deep",t+1)end else error("Serializing native functions is not enabled",t+1)end else error("Serializing functions is not enabled",t+1)end elseif y=="nil"then z="l"else error("Cannot serialize type "..tostring(y)..", nested "..t-1 .." tables",t+1)end;return z end;function xt.valuefy(r,t,u,v,w,x)local A=r:sub(1,1)local z=r:sub(2)..""local s;if A=="s"then s=xt.descapeString(z,xt.serializeDescaper,"|")elseif A=="u"then s=xt.unsafeString(xt.descapeString(z,xt.serializeDescaper,"|"))elseif A=="n"then s=tonumber(z)elseif A=="b"then s=z=="t"elseif A=="{"then s={}u[0]=u[0]+1;u[u[0] ]=s;local M=1;local N=1;local O=0;local P=false;local Q=false;for f in z:gmatch(".")do O=O+1;if f=="{"then M=M+1 elseif f=="}"then M=M-1 elseif f=="="and M==1 then P=O elseif f==","and M==1 then local R,S;R=xt.valuefy(z:sub(N,P-1).."",t+1,u,v,w,x)S=xt.valuefy(z:sub(P+1,O-1).."",t+1,u,v,w,x)s[R]=S;N=O+1 elseif M==0 and f==":"then if w then Q=xt.valuefy(z:sub(O+1).."",t+1,u,v,w,x)break else error("Unserializing metatables is not enabled",t+1)end end end;if Q then setmetatable(s,Q)end elseif A=="p"then s=u[tonumber(z)]elseif A=="f"then if v then s=loadstring(xt.unsafeString(xt.descapeString(z,xt.serializeDescaper,"|")))else error("Unserializing functions is not enabled",t+1)end elseif A=="j"then if x then local T=_G or type(getfenv)=="function"and getfenv(1)for I in z:gmatch("[^%.]+")do if type(T)~="table"then T=false;break end;T=T[I]if type(T)~="table"and type(T)~="function"then T=false;break end end;if type(T)=="function"and not pcall(string.dump,T)then s=T else error("Could not unserialize native function["..z.."], nested "..t-1 .." tables",t+1)end else error("Unserializing native functions is not enabled",t+1)end elseif A=="l"then else error("Unknown prefix "..tostring(A)..", nested "..t-1 .." tables",t+1)end;return s end;function xt.serialize(s,U,V,dn)return xt.stringify(s,1,{[0]=0},U,V,dn)end;function xt.unserialize(r,U,V,dn)return xt.valuefy(r,1,{[0]=0},U,V,dn)end;function xt.fserialize(s,U,V,dn)return xt.stringify(s,1,{[0]=0},not U,not V,not dn)end;function xt.funserialize(r,U,V,dn)return xt.valuefy(r,1,{[0]=0},not U,not V,not dn)end;function xt.setUser(W,t)if type(W)~="string"then error("User must be a string",2)elseif W=="global"then error("User cant be \"global\"",2)end;local X=getfenv(t or 2)if rawget(X,"___Xt__UsEr___")then rawset(X,"___Xt__UsEr___",W)else setfenv(t or 2,setmetatable({___Xt__UsEr___=W},{__index=X,__newindex=X}))end end;function xt.loadValue(W,Y)if fs.isDir("/xtdata")then if type(W)~="string"then error("Attempt to load value with invalid user (maybe call xt.setUser?)",2)end;W="/xtdata/"..W.."/"if fs.isDir(W)then if fs.exists(W..Y)and not fs.isDir(W..Y)then local Z=fs.open(W..Y,"r")local s=Z.readLine()Z.close()return xt.unserialize(s)end end end end;function xt.saveValue(W,Y,s)if type(W)~="string"then error("Attempt to save value with invalid user (maybe call xt.setUser?)",2)end;local z=xt.serialize(s)if not fs.isDir("/xtdata")then fs.delete("/xtdata")fs.makeDir("/xtdata")end;if not fs.isDir("/xtdata/"..W)then fs.delete("/xtdata/"..W)fs.makeDir("/xtdata/"..W)end;if fs.isDir("/xtdata/"..W.."/"..Y)then fs.delete("/xtdata/"..W.."/"..Y)end;local _=fs.open("/xtdata/"..W.."/"..Y,"w")_.writeLine(z)_.close()end;function xt.deleteValue(Y,W)if W==nil then W=getfenv(2).___Xt__UsEr___ elseif W==true then W="global"end;if type(W)~="string"then error("Attempt to delete value with invalid user (maybe call xt.setUser?)",2)end;xt.values[W][Y]=nil;local a0=#xt.values[W]==0;if fs.isDir("/xtdata")then W="/xtdata/"..W.."/"if fs.isDir(W)then fs.delete(W..Y)end;if a0 then fs.delete(W)end end;if a0 then xt.values[W]=nil end end;xt.values={global={}}setmetatable(xt,{__newindex=function(a1,B,C)if xt.values.global[B]~=nil then if C==nil then xt.deleteValue(B,"global")elseif xt.values.global[B]~=C then xt.saveValue("global",B,C)xt.values.global[B]=C end;return end;local W=getfenv(2).___Xt__UsEr___;if xt.values[W]then if xt.values[W][B]~=nil then if C==nil then xt.deleteValue(B,W)elseif xt.values[W][B]~=C then xt.saveValue(W,B,C)xt.values[W][B]=C end;return end end;rawset(xt,B,C)end,__index=function(a1,B)if xt.values.global[B]~=nil then return xt.values.global[B]end;local W=getfenv(2).___Xt__UsEr___;if xt.values[W]and xt.values[W][B]~=nil then return xt.values[W][B]end end})function xt.addValue(Y,s,global,W)if W==nil then W=getfenv(2).___Xt__UsEr___ end;if type(W)~="string"then error("Attempt to add value with invalid user (maybe call xt.setUser?)",2)end;if global then W="global"end;if type(Y)~="string"then error("Value name must be a string",2)elseif W~="global"and xt.values.global[Y]~=nil then error("A global value named \""..Y.."\" is already registered",2)elseif rawget(xt,Y)~=nil then error("xt."..tostring(Y).." already exists as a non-value key",2)end;if type(xt.values[W])~="table"then xt.values[W]={}end;local a2=xt.loadValue(W,Y)if a2==nil then if s~=nil then xt.saveValue(W,Y,s)xt.values[W][Y]=s end;return true else xt.values[W][Y]=a2;return false end end;function xt.save(Y)local W=getfenv(2).___Xt__UsEr___;if type(W)~="string"then error("Attempt to save value with invalid user (maybe call xt.setUser?)",2)elseif type(Y)~="string"then error("Name should be a string",2)end;if xt.values.global[Y]~=nil then xt.saveValue("global",Y,xt.values.global[Y])elseif xt.values[W][Y]~=nil then xt.saveValue(W,Y,xt.values[W][Y])end end;xt.stackgroup={}xt.priorities={}xt.stacknames={}function xt.addStack(a3,a4,a5,Y,a6)a6=a6 or 0;if a3[Y]then error("Stack "..Y.." already exists",2)end;local a7=false;for i,p in ipairs(a4)do if p<a6 then a7=i;break end end;a7=a7 or#a4+1;table.insert(a4,a7,a6)table.insert(a5,a7,Y)a3[Y]={}return true end;function xt.pushStack(a3,a8,a9)local aa=a3[a8]if aa then aa[#aa+1]=a9 else error("Attempt to push unexisting stack "..tostring(a8),2)end;return true end;function xt.popStack(a3,a8)local aa=a3[a8]if not aa then error("Attempt to pop unexisting stack "..tostring(a8),2)end;local _=aa[#aa]aa[#aa]=nil;return _ end;function xt.peekStack(a3,a8)local aa=a3[a8]if not aa then error("Attempt to peek unexisting stack "..tostring(a8),2)end;return aa[#aa]end;function xt.peekAllStacks(a3,a5)local i=1;return function()while true do local Y=a5[i]if Y==nil then return end;local aa=a3[Y]i=i+1;if#aa~=0 then return Y,aa[#aa]end end end end;function xt.callStackgroup(a3,a5,...)for i=1,#a5 do local aa=a3[a5[i]]if#aa~=0 then aa[#aa](...)end end end;function xt.addStackgroup(Y)local a3={}local a4={}local a5={}xt.stackgroup[Y]=a3;xt.priorities[Y]=a4;xt.stacknames[Y]=a5;xt["add"..Y]=function(ab,p)return xt.addStack(a3,a4,a5,ab,p)end;xt["push"..Y]=function(ab,_)return xt.pushStack(a3,ab,_)end;xt["pop"..Y]=function(ab)return xt.popStack(a3,ab)end;xt["peek"..Y]=function(ab)return xt.peekStack(a3,ab)end;xt["peekAll"..Y]=function()return xt.peekAllStacks(a3,a5)end;xt["call"..Y]=function(...)return xt.callStackgroup(a3,a5,...)end;xt["check"..Y]=function(ab,ac)if a3[ab]==nil then return xt.addStack(a3,a4,a5,ab,ac)end;return false end;xt["fpush"..Y]=function(ab,_,ac)if a3[ab]==nil then xt.addStack(a3,a4,a5,ab,ac)end;return xt.pushStack(a3,ab,_)end;xt["delete"..Y]=function(ab)if a3[ab]then for i,ad in ipairs(a5)do if ad==ab then table.remove(a4,i)table.remove(a5,i)break end end;a3[ab]=nil end end end;function xt.enableRestoration(ae,af)ae=ae or shell.getRunningProgram()if type(ae)~="string"then error("Expected program pathname",2)elseif not fs.exists(ae)or fs.isDir(ae)then error("Pathname must be a file",2)elseif af and af~="p"and af~="b"and af~="a"and af~="i"then error("Invalid old startup handling, "..tostring(af),2)end;xt.disableRestoration()if fs.exists("/startup")then if fs.isDir("/startup")then af="i"else if not af then print("What do you wish to do with the previous startup program?")print("[P] Run it alongside this program on startup")print("[B] Run it before this program on startup")print("[A] Run it after this program on startup")print("[I] Don't run it at all, just restore it after")while true do local ag,B=os.pullEvent("key")if B==keys.p then af="p"break elseif B==keys.b then af="b"break elseif B==keys.a then af="a"break elseif B==keys.i then af="i"break end end;sleep(0)end end;if fs.exists("/xt_active_startup")then local i=1;while true do if not fs.exists("/xt_startup_backup_"..i)then break end;i=i+1 end;fs.move("/xt_active_startup","/xt_startup_backup_"..i)end;fs.move("/startup","/xt_active_startup")else af="i"end;local ah;if af=="p"then ah=[[parallel.waitForAll(function()shell.run("/xt_active_startup")end,function()shell.run("]]..ae..[[")end)]]elseif af=="b"then ah=[[local ok,err=pcall(shell.run,"/xt_active_startup")if not ok then printError(tostring(err))sleep(2) end shell.run("]]..ae..[[")]]elseif af=="a"then ah=[[local ok,err=pcall(shell.run,"]]..ae..[[")if not ok then printError(tostring(err))sleep(2) end shell.run("/xt_active_startup")]]elseif af=="i"then ah=[[shell.run("]]..ae..[[");]]else error("what?")end;local Z=fs.open("/startup","w")Z.writeLine("--XT's restore-on-startup program")Z.writeLine("--DO NOT CHANGE OR REMOVE THESE COMMENTS")Z.writeLine("--Running "..ae)if af=="i"then Z.writeLine("--and nothing else")else Z.writeLine("--and /xt_active_startup")end;Z.writeLine(ah)Z.close()if not fs.exists("/restore")then local Z=fs.open("/restore","w")Z.writeLine("--XT's easy startup disabler program")Z.writeLine("--DO NOT CHANGE OR REMOVE THESE COMMENTS")Z.writeLine([[if type(xt)=="table" and type(xt.disableRestoration)=="function" then xt.disableRestoration(); else printError("Failed to restore"); end] ])Z.close()end;return true end;function xt.disableRestoration()local ai=false;if fs.exists("/startup")and not fs.isDir("/startup")then local Z=fs.open("/startup","r")local aj=Z.readLine()local ak=Z.readLine()Z.close()if aj=="--XT's restore-on-startup program"and ak=="--DO NOT CHANGE OR REMOVE THESE COMMENTS"then fs.delete("/startup")ai=true end end;if fs.exists("/restore")and not fs.isDir("/restore")then local Z=fs.open("/restore","r")local aj=Z.readLine()local ak=Z.readLine()Z.close()if aj=="--XT's easy startup disabler program"and ak=="--DO NOT CHANGE OR REMOVE THESE COMMENTS"then fs.delete("/restore")end end;if fs.exists("/xt_active_startup")and not fs.exists("/startup")then fs.move("/xt_active_startup","/startup")end;return ai end;xt.fc={south=0,west=1,north=2,east=3,[0]="south",[1]="west",[2]="north",[3]="east",["z+"]=0,["x-"]=1,["z-"]=2,["x+"]=3,zplus=0,xminus=1,zminus=2,xplus=3}function xt.invertFace(al)return(al+2)%4 end;function xt.getFX(am)am=am or xt.f;if am==nil then error("F must be a number",2)elseif am==1 then return-1 elseif am==3 then return 1 else return 0 end end;function xt.getFZ(am)am=am or xt.f;if am==nil then error("F must be a number",2)elseif am==0 then return 1 elseif am==2 then return-1 else return 0 end end;if turtle then ___Xt__UsEr___="xt"xt.addValue("x",0,true)xt.addValue("y",0,true)xt.addValue("z",0,true)xt.addValue("f",0,true)xt.addStackgroup("Premove")xt.addStackgroup("Preturn")xt.addStackgroup("Refuel")xt.addPremove("err",0)xt.pushPremove("err",function()error("Attempt to move without reloading stacks (maybe call xt.setTurtleUser?)")end)xt.addPreturn("err",0)xt.pushPreturn("err",function()error("Attempt to turn without reloading stacks (maybe call xt.setTurtleUser?)")end)xt.addRefuel("err",0)xt.pushRefuel("err",function()error("Refueled without reloading stacks (maybe call xt.setTurtleUser?)")end)xt.addValue("moveData",{false,0})function xt.doMove(an,ao)if an=="fw"then if xt.getFX()==0 then xt.z=xt.z+xt.getFZ()*ao else xt.x=xt.x+xt.getFX()*ao end elseif an=="bk"then if xt.getFX()==0 then xt.z=xt.z-xt.getFZ()*ao else xt.x=xt.x-xt.getFX()*ao end elseif an=="up"then xt.y=xt.y+ao elseif an=="dn"then xt.y=xt.y-ao end end;function xt.premoveSaving(an)if xt.moveData[1]then xt.correctPosition()end;xt.moveData[2]=xt.getFuelLevel()xt.moveData[1]=an;xt.save("moveData")end;function xt.postmoveSaving(an,F)if F then xt.doMove(an,1)end;xt.moveData[1]=false;xt.save("moveData")end;function xt.preturnSaving(an)if an=="r"then xt.f=(xt.f+1)%4 elseif an=="l"then xt.f=(xt.f-1)%4 else error("suspicious...")end end;function xt.preturnChecking(an)xt.wasTurning=xt.wasTurning+1 end;function xt.postturnChecking(an)xt.wasTurning=xt.wasTurning-1 end;xt.fuelUpdated=false;function xt.postmoveFuel(an,F)if F and xt.fuelUpdated then xt.fuelUpdated=xt.fuelUpdated-1 end end;function xt.refuelFuel()xt.fuelUpdated=false end;function xt.reloadStacks(ap,aq)xt.stackgroup={}xt.priorities={}xt.stacknames={}xt.addStackgroup("Premove")xt.addStackgroup("Forcemove")xt.addStackgroup("Postmove")xt.addStackgroup("Preturn")xt.addStackgroup("Postturn")xt.addStackgroup("Refuel")if not ap then xt.addPremove("pos_save",100)xt.pushPremove("pos_save",xt.premoveSaving)xt.addPostmove("pos_save",100)xt.pushPostmove("pos_save",xt.postmoveSaving)xt.addPreturn("face_save",100)xt.pushPreturn("face_save",xt.preturnSaving)end;if aq then xt.addValue("wasTurning",0)xt.addPreturn("face_check",100)xt.pushPreturn("face_check",xt.preturnChecking)xt.addPostturn("face_check",100)xt.pushPostturn("face_check",xt.postturnChecking)else xt.deleteValue("wasTurning")end;xt.addPostmove("quick_fuel",90)xt.pushPostmove("quick_fuel",xt.postmoveFuel)xt.addRefuel("quick_fuel",90)xt.pushRefuel("quick_fuel",xt.refuelFuel)end;function xt.setTurtleUser(W,ap,aq)xt.setUser(W,3)xt.reloadStacks(ap,aq)end;function xt.move(ar,as,an,at,au,av,...)ar=ar or 1;if ar<0 then return xt.move(-ar,at,au,as,an,av,...)end;for i=1,ar do xt.callPremove(an,...)local F=true;if av then while not as()do xt.callForcemove(an,...)end else F=as()end;xt.callPostmove(an,F,...)if not F then return false end end;return true end;function xt.turn(ar,aw,an,at,au,...)ar=ar or 1;if ar<0 then return xt.turn(-ar,at,au,aw,an,...)end;for i=1,ar do xt.callPreturn(an,...)aw()xt.callPostturn(an,...)end;return true end;function xt.fw(ar,...)return xt.move(ar,xt.native.forward,"fw",xt.native.back,"bk",false,...)end;function xt.bk(ar,...)return xt.move(ar,xt.native.back,"bk",xt.native.forward,"fw",false,...)end;function xt.up(ar,...)return xt.move(ar,xt.native.up,"up",xt.native.down,"dn",false,...)end;function xt.dn(ar,...)return xt.move(ar,xt.native.down,"dn",xt.native.up,"up",false,...)end;function xt.ffw(ar,...)return xt.move(ar,xt.native.forward,"fw",xt.native.back,"bk",true,...)end;function xt.fbk(ar,...)return xt.move(ar,xt.native.back,"bk",xt.native.forward,"fw",true,...)end;function xt.fup(ar,...)return xt.move(ar,xt.native.up,"up",xt.native.down,"dn",true,...)end;function xt.fdn(ar,...)return xt.move(ar,xt.native.down,"dn",xt.native.up,"up",true,...)end;function xt.left(ar,...)return xt.turn(ar,xt.native.turnLeft,"l",xt.native.turnRight,"r",...)end;function xt.right(ar,...)return xt.turn(ar,xt.native.turnRight,"r",xt.native.turnLeft,"l",...)end;function xt.quickTurn(ax)ax=ax%4;if ax==0 then elseif ax==1 then xt.right()elseif ax==2 then xt.right(2)elseif ax==3 then xt.left()else error("what?")end;return true end;function xt.face(ay)if ay==nil then return false,"Not enough arguments"end;return xt.quickTurn(ay-xt.f)end;function xt.gotoX(az)if az>xt.x+0.1 then xt.face(3)elseif az<xt.x-0.1 then xt.face(1)else return true end;local aA=math.abs(az-xt.x)return xt.fw(aA)end;function xt.gotoZ(aB)if aB>xt.z+0.1 then xt.face(0)elseif aB<xt.z-0.1 then xt.face(2)else return true end;local aA=math.abs(aB-xt.z)return xt.fw(aA)end;function xt.gotoXZ(az,aB)local aC=xt.getFX()local aD=xt.getFZ()if aD==1 and aB>xt.z or aD==-1 and aB<xt.z or aC==1 and az<xt.x or aC==-1 and az>xt.x then xt.gotoZ(aB)xt.gotoX(az)else xt.gotoX(az)xt.gotoZ(aB)end end;function xt.gotoY(aE)local aF;if aE>xt.y then aF=xt.up elseif aE<xt.y then aF=xt.dn else return true end;local aA=math.abs(aE-xt.y)return aF(aA)end;function xt.gotoXZY(az,aE,aB)xt.gotoXZ(az,aB)xt.gotoY(aE)end;function xt.gotoYXZ(az,aE,aB)xt.gotoY(aE)xt.gotoXZ(az,aB)end;function xt.fgotoX(az)if az>xt.x+0.1 then xt.face(3)elseif az<xt.x-0.1 then xt.face(1)else return true end;local aA=math.abs(az-xt.x)return xt.ffw(aA)end;function xt.fgotoZ(aB)if aB>xt.z+0.1 then xt.face(0)elseif aB<xt.z-0.1 then xt.face(2)else return true end;local aA=math.abs(aB-xt.z)return xt.ffw(aA)end;function xt.fgotoXZ(az,aB)local aC=xt.getFX()local aD=xt.getFZ()if aD==1 and aB>xt.z or aD==-1 and aB<xt.z or aC==1 and az<xt.x or aC==-1 and az>xt.x then xt.fgotoZ(aB)xt.fgotoX(az)else xt.fgotoX(az)xt.fgotoZ(aB)end end;function xt.fgotoY(aE)local aF;if aE>xt.y then aF=xt.fup elseif aE<xt.y then aF=xt.fdn else return true end;local aA=math.abs(aE-xt.y)return aF(aA)end;function xt.fgotoXZY(az,aE,aB)xt.fgotoXZ(az,aB)xt.fgotoY(aE)end;function xt.fgotoYXZ(az,aE,aB)xt.fgotoY(aE)xt.fgotoXZ(az,aB)end;function xt.navigate(aG,aH)local aI=true;if aG=="x"then if aH<xt.x then xt.face(xt.fc.xminus)aI=false elseif aH>xt.x then xt.face(xt.fc.xplus)aI=true else return end elseif aG=="z"then if aH<xt.z then xt.face(xt.fc.zminus)aI=false elseif aH>xt.z then xt.face(xt.fc.zplus)aI=true else return end else error("Invalid navigate coord",2)end;while aI and xt[aG]-0.1<aH or not aI and xt[aG]-0.1>aH do while xt.dn()do end;while not xt.fw()do while not xt.up()do while not xt.bk()do xt.dn()end end end end end;function xt.nav(aG,aH)local aJ;xt.correctPosition()if aG=="x"then if aH>xt.x then xt.face(xt.fc.xplus)aJ=aH-xt.x elseif aH<xt.x then xt.face(xt.fc.xminus)aJ=xt.x-aH else return true end elseif aG=="z"then if aH>xt.z then xt.face(xt.fc.zplus)aJ=aH-xt.z elseif aH<xt.z then xt.face(xt.fc.zminus)aJ=xt.z-aH else return true end else error("Invalid coord "..tostring(aG),2)end;local aK=1;while aK<=aJ do while xt.dn()do end;while not xt.fw()do while not xt.up()do while not xt.bk()do while not xt.dn()do sleep(0.2)end end;aK=aK-1 end end;aK=aK+1 end;return true end;function xt.navXZ(az,aB)local aC=xt.getFX()local aD=xt.getFZ()if aD==1 and aB>xt.z or aD==-1 and aB<xt.z or aC==1 and az<xt.x or aC==-1 and az>xt.x then xt.navigate("z",aB)xt.navigate("x",az)else xt.navigate("x",az)xt.navigate("z",aB)end end;function xt.locateGPS()local aL;local aM;local aN;aL,aM,aN=gps.locate()if aL==nil or aM==nil or aN==nil then return false end;xt.x=aL;xt.z=aN;xt.y=aM;return true end;function xt.orientGPS(forward,back,aO)if xt.getFuelLevel()~="unlimited"then if xt.getFuel()<2 then return false,"Not enough fuel"end end;if aO==nil then aO=false end;if forward==nil then forward=xt.fw end;if back==nil then back=xt.bk end;local aL;local aN;if not xt.locate()then return false,"Could not reach GPS"end;aL=xt.x;aN=xt.z;if not forward()then if aO then return false,"Could not go forward"else return xt.calibrateGPS(back,forward,true)end end;if not xt.locate()then back()return false,"Could not reach GPS"end;local aP=xt.x-aL;local aQ=xt.z-aN;if aO then aP=-aP;aQ=-aQ end;if aQ==1 then xt.f=0 elseif aQ==-1 then xt.f=2 elseif aP==1 then xt.f=3 elseif aP==-1 then xt.f=1 end;back()xt.wasTurning=0;return true end;function xt.orientTorch(K,an)local aR;local aS=K.metadata;local aT=K.name;if aT=="minecraft:torch"or aT=="minecraft:redstone_torch"or aT=="minecraft:lever"or aT=="minecraft:stone_button"or aT=="minecraft:wooden_button"then if aS==1 then aR=xt.fc.xplus elseif aS==2 then aR=xt.fc.xminus elseif aS==3 then aR=xt.fc.zplus elseif aS==4 then aR=xt.fc.zminus else return false end elseif aT=="minecraft:ladder"or aT=="minecraft:wall_sign"then if aS==5 then aR=xt.fc.xplus elseif aS==4 then aR=xt.fc.xminus elseif aS==3 then aR=xt.fc.zplus elseif aS==2 then aR=xt.fc.zminus else return false end else return false end;if an=="front"then xt.f=aR elseif an=="back"then xt.f=(aR+2)%4 elseif an=="left"then xt.f=(aR+1)%4 elseif an=="right"then xt.f=(aR-1)%4 else return false end;xt.wasTurning=0;return true end;xt.addValue("cantOrientOn",{["minecraft:glass"]={[0]=true},["ComputerCraft:CC-Peripheral"]={[1]=true}})function xt.canOrientOn(K)if not K then return false end;if xt.cantOrientOn[K.name]then return not xt.cantOrientOn[K.name][K.metadata or K.damage]else return true end end;function xt.setCantOrientOn(K)if not xt.cantOrientOn[K.name]then xt.cantOrientOn[K.name]={}end;xt.cantOrientOn[K.name][K.metadata or K.damage]=true;xt.save("cantOrientOn")end;function xt.orientDigging()local aU=xt.s;local aV=false;for i=1,16 do local K=xt.getItemDetail(i)if K then if K.name=="minecraft:torch"or K.name=="minecraft:redstone_torch"or K.name=="minecraft:ladder"then aV=i;break end end end;if aV then local aW=xt.f;local aX=false;for i=1,4 do local K=xt.inspect()if K then if xt.canOrientOn(K)then aX=K;break else if not xt.dig()then xt.face(aW)return false,"Found undiggable block"end end end;xt.right()end;xt.checkForcemove("justdig",0)xt.pushForcemove("justdig",xt.forceDig)function finish()xt.popForcemove("justdig")xt.sel(aU)end;function tryBlock()xt.fup()xt.sel(aV)if xt.placeDn()then return true else xt.fdn()return false end end;if aX then for i=1,3 do xt.right()if not xt.dig()then if xt.detect()then xt.face(aW)finish()return false,"Found undiggable block"end end end;xt.right()if not xt.digDn()and xt.detectDn()then xt.face(aW)finish()return end;if tryBlock()then local aY=xt.f;if xt.orientTorch(xt.inspectDn(),"back")then xt.fdn()xt.quickTurn(aW-aY)finish()return true end else xt.setCantOrientOn(aX)end end;for i=1,16 do if i~=aV and xt.getItemCount(i)~=0 then local K=xt.getItemDetail(i)if xt.canOrientOn(K)then xt.dig()xt.sel(i)if xt.place()then if not xt.digDn()and xt.detectDn()then xt.dig()xt.face(aW)finish()return false,"Found undiggable block"end;if tryBlock()then local aY=xt.f;if xt.orientTorch(xt.inspectDn(),"back")then xt.fdn()xt.sel(i)xt.dig()xt.quickTurn(aW-aY)finish()return true end else xt.setCantOrientOn(K)end;xt.sel(i)xt.dig()else xt.setCantOrientOn(K)end end end end;finish()return false,"Could not place orienting item"else return false,"Could not find orienting item"end end;function xt.refuel(ar,...)local F,aZ;if ar then F,aZ=xt.native.refuel(ar)else F,aZ=xt.native.refuel()end;if F then for ab,_ in xt.peekAllRefuel()do _(...)end end;return F,aZ end;function xt.getFuel()if xt.fuelUpdated then return xt.fuelUpdated else return xt.getFuelLevel()end end;function xt.getFuelLevel()xt.fuelUpdated=xt.native.getFuelLevel()return xt.fuelUpdated end;function xt.getCurSlot()xt.s=xt.native.getSelectedSlot()return xt.s end;function xt.sel(a_)xt.native.select(a_)xt.s=a_;return true end;function xt.restoreTurtle()if type(turtle.native)~="table"then return false end;for B,C in pairs(turtle.native)do turtle[B]=C end;turtle.xt_replaced=nil;return true end;function xt.unloadXT()xt.restoreTurtle()_G.xt=nil;return"Bye Bye"end;function xt.inspectBase(b0)local F,K=b0()if F then return K else return false,K end end;function xt.inspect()return xt.inspectBase(xt.native.inspect)end;function xt.inspectDn()return xt.inspectBase(xt.native.inspectDown)end;function xt.inspectUp()return xt.inspectBase(xt.native.inspectUp)end;function xt.addDir(Y)if type(Y)~="string"then error("name must be a string",2)end;local aF=xt[Y]if type(aF)~="function"then error("No "..Y.."() function in XT api",2)end;local b1=xt[Y.."Up"]if type(b1)~="function"then error("No "..Y.."Up() function in XT api",2)end;local b2=xt[Y.."Dn"]if type(b2)~="function"then error("No "..Y.."Dn() function in XT api",2)end;xt[Y.."Dir"]=function(an,...)if an=="fw"then return aF(...)elseif an=="up"then return b1(...)elseif an=="dn"then return b2(...)elseif an=="bk"then xt.right(2)local F=aF(...)xt.right(2)return F end end end;if turtle.xt_replaced then xt.restoreTurtle()end;for B,C in pairs(turtle)do if B=="forward"then xt.forward=xt.fw elseif B=="back"then xt.back=xt.bk elseif B=="up"then elseif B=="down"then xt.down=xt.dn elseif B=="turnLeft"then xt.turnLeft=xt.left elseif B=="turnRight"then xt.turnRight=xt.right elseif B=="select"then xt.select=xt.sel elseif B=="inspect"then elseif B=="inspectUp"then elseif B=="inspectDown"then xt.inspectDown=xt.inspectDn elseif B=="attack"then xt.attack=C;xt.atk=C;xt.attackDir=xt.atkDir elseif B=="attackUp"then xt.attackUp=C;xt.atkUp=C elseif B=="attackDown"then xt.attackDown=C;xt.atkDn=C elseif B=="digDown"then xt.digDown=C;xt.digDn=C elseif B=="placeDown"then xt.placeDown=C;xt.placeDn=C elseif B=="detectDown"then xt.detectDown=C;xt.detectDn=C elseif B=="compareDown"then xt.compareDown=C;xt.compareDn=C elseif B=="dropDown"then xt.dropDown=C;xt.dropDn=C elseif B=="suckDown"then xt.suckDown=C;xt.suckDn=C elseif B=="refuel"then elseif B=="getFuelLevel"then elseif B=="getSelectedSlot"then xt.getSelectedSlot=xt.getCurSlot else xt[B]=C end end;xt.go=xt.fw;xt.goUp=xt.up;xt.goDn=xt.dn;xt.fgo=xt.ffw;xt.fgoUp=xt.fup;xt.fgoDn=xt.fdn;xt.addDir("go")xt.addDir("fgo")xt.addDir("atk")xt.addDir("attack")xt.addDir("dig")xt.addDir("place")xt.addDir("detect")xt.addDir("inspect")xt.addDir("compare")xt.addDir("drop")xt.addDir("suck")xt.forceDig=xt.digDir;xt.forceAtk=xt.atkDir;xt.forceAttack=xt.attackDir;turtle.forward=xt.fw;turtle.back=xt.bk;turtle.up=xt.up;turtle.down=xt.dn;turtle.turnLeft=xt.left;turtle.turnRight=xt.right;turtle.refuel=xt.refuel;turtle.getFuelLevel=xt.getFuelLevel;turtle.select=xt.sel;turtle.xt_replaced=true;xt.getCurSlot()function xt.shouldReorient()if xt.wasTurning==nil then error("Orientation checking is not enabled",2)end;return xt.wasTurning>0 end;function xt.shouldRelocate()return not not xt.moveData[1]end;function xt.correctPosition()if xt.shouldRelocate()then local b=xt.detect()local b3=xt.getFuelLevel()if b3=="unlimited"then if not b then xt.doMove(xt.moveData[1],1)end else if xt.moveData[2]-b3==1 then xt.doMove(xt.moveData[1],1)end end;xt.moveData[1]=false;xt.save("moveData")end end;xt.correctPosition()end;_G.xt=xt
  12.     ]]                          --XT to extract. Comment is on 1 line so that other xt downloaders can recognize it
  13.    
  14.     local lastver=false;
  15.     local lastverpaste=false;
  16.     local function checkForUpdate()
  17.         lastver=2.0;
  18.     end
  19.    
  20.     local myver=false;
  21.     local asktodelete=false;
  22.     local loadedver=false;
  23.     local mybestver=false;
  24.     local function getMyVersion()
  25.         if fs.isDir(xtpath) then
  26.             cprint("\""..xtpath.."\" is a directory, not XT api");
  27.             myver=false;
  28.             asktodelete=true;
  29.         elseif fs.exists(xtpath) then
  30.             local handle=fs.open(xtpath,"r");
  31.             local fline=handle.readLine();
  32.             handle.close();
  33.             if fline:sub(1,6)=="--XT V" and fline:sub(10)==" by negamartin" and tonumber(fline:sub(7,9)) then
  34.                 myver=tonumber(fline:sub(7,9));
  35.                 cprint("\""..xtpath.."\" is XT api V"..myver);
  36.             else
  37.                 myver=false;
  38.                 asktodelete=true;
  39.                 cprint("\""..xtpath.."\" is a non-XT file");
  40.             end
  41.         else
  42.             cprint("\""..xtpath.."\" does not exist");
  43.             myver=false;
  44.         end
  45.         if xt then
  46.             loadedver=xt.version;
  47.             print("XT V"..loadedver.." is already loaded");
  48.         else
  49.             loadedver=false;
  50.         end
  51.         if myver then
  52.             if loadedver then
  53.                 mybestver=math.max(myver,loadedver);
  54.             else mybestver=myver; end
  55.         else mybestver=loadedver; end
  56.     end
  57.    
  58.     local function downloadXT()
  59.         local handle=fs.open(xtpath,"w");
  60.         handle.write(inlineXT);
  61.         handle.close();
  62.         return true;
  63.     end
  64.    
  65.     getMyVersion();
  66.     if autoupdate or not mybestver then
  67.         checkForUpdate();
  68.         if lastver then
  69.             if mybestver then
  70.                 if mybestver<lastver then
  71.                     local doit=true;
  72.                     if asktodelete then
  73.                         aprint("A non-XT file has been found in \""..xtpath.."\"");
  74.                         aprint("Overwrite it? (\"yes\" or \"no\")");
  75.                         if cread()=="yes" then
  76.                             fs.delete(xtpath);
  77.                         else
  78.                             cprint("Remove the non-XT file if you wish to update");
  79.                             doit=false;
  80.                         end
  81.                     end
  82.                     if doit then
  83.                         if downloadXT() then
  84.                             cprint("Updated XT to V"..lastver);
  85.                         else
  86.                             cprint("Couldn't extract XT, using previous version");
  87.                         end
  88.                     end
  89.                 else
  90.                     cprint("XT up to date");
  91.                 end
  92.             else
  93.                 if asktodelete then
  94.                     aprint("A non-XT file has been found in \""..xtpath.."\"");
  95.                     aprint("Overwrite it? (\"yes\" or \"no\")");
  96.                     if cread()=="yes" then
  97.                         fs.delete(xtpath);
  98.                     else
  99.                         aprint("Move the non-XT file and try again");
  100.                         error();
  101.                     end
  102.                 end
  103.                 if downloadXT() then
  104.                     cprint("XT V"..lastver.." extracted");
  105.                 else
  106.                     cerr("Couldn't extract xt");
  107.                     error();
  108.                 end
  109.             end
  110.         else
  111.             if mybestver then
  112.                 cprint("Couldn't extract, using previous version");
  113.             else
  114.                 cerr("Couldn't extract xt, and no previous version");
  115.                 error();
  116.             end
  117.         end
  118.     end
  119.     if loadedver then
  120.         if myver and myver>loadedver then
  121.             dofile(xtpath);
  122.         else
  123.             xt.correctPosition();
  124.         end
  125.     else
  126.         dofile(xtpath);
  127.     end
  128. end
Advertisement
Add Comment
Please, Sign In to add comment