Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --pastebin get 2WLg1AWL theinsektAPIs/installerAPI
- --theinsektAPIs/installerAPI contains functions for using installfiles
- --experimental
- --only tested a little
- --may stop working when testing new stuff
- --programs
- --installer and updateinstaller:
- --do: pastebin run fjSV9kHj
- --some result codes
- --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
- --How an installfile works
- --Each line are run in order. Empty lines are skipped. Invalid commands should cause an error.
- --valid commands
- --examle lines:
- --
- --pastebin <code> <path>
- --installfile <code>
- --comment fgfhdfhdhf jnjn
- function loadLines(path)
- if type(path)~="string" then error("Error (loadLines): path should be a string",2) end
- 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)
- if type(line)~="string" then error("Error (getWords): line should be a string",2) end
- 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]
- --download and run installfile
- return standardInstall(shellObj,installed,code)
- 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 standardDownload(shellObj,installed,path,code,false)
- 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={
- [-3]="file wasn't downloaded",
- [-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 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
- --should have checked path and code before calling this function
- --like in downloadHelp2
- 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
- --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
- --returns -3 if the file wasn't downloaded
- --positive number are good, negative are bad
- --d should be a single file download function, takes params path, code and dextra
- function downloadHelp(shellObj,installed, isInstallfile, d, path, code, dextra)
- --check parameters
- if type(isInstallfile)~="boolean" and type(isInstallfile)~="nil" then
- error("Error: the parameter called isInstallfile, should be true, false or nil.",2)
- end
- if type(installed)~="table" then error("Error: the parameter called installed, should be a table.",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
- if type(d)~="function" then error("Error: the parameter called d, should be a function",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
- --remove if already exists NEW 2015-06-06
- if fs.exists(path) then fs.delete(path) end
- --download file
- local resCode = d(shellObj, path, code, dextra)
- --check rescode
- if resCode < 0 then return resCode end
- --check that file was downloaded NEW 2015-06-06
- if not fs.exists(path) then return -3 end
- --mark as installed
- markAsInstalled(installed, path,code)
- --do stuff depending on if downloaded installfile
- if isInstallfile then
- return doInstallFile(shellObj, installed, path)
- else
- return resCode
- end
- end
- function markAsInstalled(installed, path,code)
- --check parameters
- if type(installed)~="table" then error("Error: the parameter called installed, should be a table.",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
- --do the marking
- installed[path]=code
- --return result code
- return 2
- end
- --downloads the given file and then returns 2
- function pastebin(shellObj, path,code,overwrite)
- if overwrite then
- fs.delete(path)
- end
- shellObj.run("pastebin","get", code, path)
- 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
- --downloads the file with the given code from pastebin to the given path
- function standardDownload(shellObj,installed,path,code,isInstallfile)
- return downloadHelp(shellObj,installed,isInstallfile,pastebin,path,code,true)
- end
- function standardInstall(shellObj,installed,code)
- local path="installfiles/"..code
- return standardDownload(shellObj,installed,path,code,true)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement