Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --theinsektAPIs/installerAPI
- --a program used to install/download computercraft programs
- --incomplete erroneous example
- --os.loadAPI("dah_installer")
- --doInstallFile(shell, {}, "")
- --experimental
- --only tested a little
- --may stop working when testing new stuff
- function loadLines(path)
- if not fs.exists(path) then
- return nil
- end
- local h=fs.open(path, "r")
- if h==nil then
- return nil
- end
- local res={}
- while(true) do
- local line=h.readLine()
- if line==nil then break end
- res[#res+1]=line
- end
- h.close()
- return res
- end
- function getWords(line)
- local words={}
- --"%S+"
- --"%a+"
- for word in string.gmatch(line, "%S+") do
- words[#words+1]=word
- end
- return words
- end
- --do the commands and add to the installed
- --table if something was installed
- --installed[path]=code
- --should have the same code if it's the same file
- --that way the same install file can be referenced twice
- commands={
- installfile=function(shellObj,installed,words)
- --get parameters
- local command=words[1]
- local code=words[2]
- local path="installfiles/"..code
- --local path=words[3]
- --download the install file, and mark it as installed
- local res=pastebin2(shellObj,installed, command, code, path)
- if res~=2 then --if res==1 or res==-1
- return res
- end
- --do installs in install file
- return doInstallFile(shellObj,installed,path)
- end,
- pastebin=function(shellObj,installed,words)
- --get parameters
- local command=words[1]
- local code=words[2]
- local path=words[3]
- --download the file and mark it as installed
- return pastebin2(shellObj,installed, command,code,path)
- end,
- ["comment"]=function(shellObj,installed,words)
- return 0
- end,
- }
- --returns 0 if the line is empty or should be ignored
- --returns -2 if unrecognised command
- --returns 1 if already installed
- --returns 2 if installed
- --returns -1 if two different files have the same path
- --positive number are good, negative are bad
- resCodeMeanings={
- [-2]="unrecognised command",
- [-1]="two different files have the same path",
- [0]="line was empty or ignored",
- [1]="skipped because already installed",
- [2]="performed installation",
- }
- function getRescodeMeaning(resCode)
- local res=resCodeMeanings[resCode]
- if res==nil then
- return "unrecognized resCode"
- end
- return res
- end
- --returns 1 if already installed
- --returns 2 if installed
- --returns -1 if two different files have the same path
- --positive number are good, negative are bad
- function pastebin2(shellObj, installed, command,code,path)
- --check parameters
- if type(installed)~="table" then error("Error: the parameter called installed, should be a table.",2) end
- if type(command)~="string" then error("Error: the parameter called command, should be a string",2) end
- if type(code)~="string" then error("Error: the parameter called code, should be a string",2) end
- if type(path)~="string" then error("Error: the parameter called path, should be a string",2) end
- --check if already installed
- if installed[path]~=nil then
- if installed[path]==code then
- --return have already installed code
- return 1
- else
- --have already installed a different file
- --with the same path
- return -1
- end
- end
- --download file (install)
- pastebin(shellObj,path,code,true)
- --update installed
- installed[path]=code
- --return did install code
- return 2
- end
- --returns 0 if the line is empty or should be ignored
- --returns -2 if unrecognised command
- --returns 1 if already installed
- --returns 2 if installed
- --returns -1 if two different files have the same path
- --positive number are good, negative are bad
- function doInstallLine(shellObj,installed,line)
- --get the words on the line
- local words=getWords(line)
- --
- return doInstallWords(shellObj,installed,words)
- end
- --install using words from command line
- function doInstallWords(shellObj,installed,words)
- --DEBUG
- --for k,v in ipairs(words) do
- -- print(k,": ",v)
- --end
- --if line has no commands return 0
- if words==nil or #words==0 then
- return 0
- end
- --get the command on the line
- local command=words[1]
- --get the command function
- local f=commands[command]
- --if that command function does not exist return -2
- if f==nil then
- return -2
- end
- --run the command function
- return f(shellObj, installed, words)
- end
- function doInstallFile(shellObj, installed, path)
- --check arguments
- if installed==nil then installed={} end
- if type(path)~="string" then error("Error: path should be a string",2) end
- if type(installed)~="table" then error("Error: installed should be a table or nil",2) end
- --load lines from install file
- local lines=loadLines(path)
- --for each line
- for lineNr,line in ipairs(lines) do
- local resCode = doInstallLine(shellObj, installed, line)
- --throw errors here depending on the if the resCode is an error
- if resCode < 0 then
- local resCodeString="resCode "..tostring(resCode).." ("..installerAPI.getRescodeMeaning(resCode)..")"
- error("Error: "..resCodeString.." in file "..path.." on line "..tostring(lineNr))
- end
- end
- return 2
- end
- function pastebin(shellObj,path,code,overwrite)
- if overwrite then
- fs.delete(path)
- end
- shellObj.run("pastebin","get", code, path)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement