Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = { ... }
- local bExtract = false -- Extract or Pack.
- local sDestination = ""
- local bRunning = true
- local sSplitString = "!dnf32Dsf!"
- -- Extra methods --
- function printUsage()
- print( "Usage: nxp <extract/pack> <.nxpFile/directory>" )
- print( " <outputDirectory/.nxpOutputFile>" )
- end
- -- Compatibility: Lua-5.1; Borrowed from: http://lua-users.org/wiki/SplitJoin
- function split(str, pat)
- local t = {} -- NOTE: use {n = 0} in Lua-5.0
- local fpat = "(.-)" .. pat
- local last_end = 1
- local s, e, cap = str:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(t,cap)
- end
- last_end = e+1
- s, e, cap = str:find(fpat, last_end)
- end
- if last_end <= #str then
- cap = str:sub(last_end)
- table.insert(t, cap)
- end
- return t
- end
- -------------------
- function determineMethod( sArg )
- if sArg == "extract" then
- return true
- elseif sArg == "pack" then
- return false
- else
- return nil
- end
- end
- function checkFileExistance()
- if tArgs[1] == "extract" then
- if fs.exists( tArgs[2] ) then
- if not fs.exists( tArgs[3] ) then
- sDestination = tArgs[3]
- return true
- else
- error( "Cannot extract there." )
- end
- else
- error( "Zip to be extracted doesn't exist." )
- end
- else
- if fs.exists( tArgs[2] ) and fs.isDir( tArgs[2] ) then
- if not fs.exists( tArgs[3] ) then
- sDestination = tArgs[3] .. ".nxp"
- return true
- else
- error( "Cannot transfer package to an existant file." )
- end
- else
- error( "Cannot package " .. tArgs[2] .. "." )
- end
- end
- end
- if #tArgs < 3 then
- printUsage()
- bRunning = false
- end
- -- NexPack specific functions.
- -- Takes a string of a package and splits it into a table that it returns.
- -- Format: tPackage[n] = { Name, Contents }
- function readPackageIntoTable( sPackage )
- local tPackage = split( sPackage, sSplitString )
- local tPackage_Appended = {}
- local nIndex = 1
- for i=1, #tPackage, 2 do
- tPackage_Appended[nIndex] = { Name = tPackage[i], Contents = tPackage[i+1] }
- nIndex = nIndex + 1
- end
- return tPackage_Appended
- end
- -- Takes a unpackaged table and a directory to write the contents of the table into files in the directory passed.
- function writePackageToDirectory( tPacked, sDirectory )
- local file = nil
- for index,value in ipairs( tPacked ) do
- file = fs.open( sDirectory .. "/" .. tPacked[index].Name, "w" )
- file.write( tPacked[index].Contents )
- file.close()
- end
- end
- if bRunning then
- bExtract = determineMethod( tArgs[1] )
- if bExtract ~= nil then
- -- Extract the selected package.
- if bExtract then
- -- Make sure all paths and files are in their correct places and or states.
- if checkFileExistance() then
- -- Create the directory we'll be extracting this package to.
- fs.makeDir( tArgs[3] )
- -- Get the package in a string format for splitting.
- local package = fs.open( tArgs[2], "r" )
- local packageContents = package.readAll()
- package.close()
- -- Get the package contents into a table for writing into separate files.
- local tPackageContents = readPackageIntoTable( packageContents )
- -- Format: tPackageContents[n] = { Name, Contents }
- -- Write the package table into separate files.
- writePackageToDirectory( tPackageContents, tArgs[3] )
- end
- -- Package the selected directory.
- else
- -- Make sure all paths and files are in their correct places or states.
- if checkFileExistance() then
- local sPackage = sSplitString -- The final string of the package.
- local sDir = tArgs[2]
- local tContents = fs.list( sDir )
- local tFiles = {} -- The files will have their full path in their name!
- -- Seperate all objects in the selected directory so only the files are packaged.
- for index, value in ipairs( tContents ) do
- if not fs.isDir( sDir .. "/" .. value ) then
- table.insert( tFiles, sDir .. "/" .. value ) -- The fiels will have their full path in their name!
- end
- end
- -- Read the files into the proper format within a file.
- local file = nil
- local fileContents = ""
- for index,value in ipairs( tFiles ) do
- file = fs.open( value, "r" )
- fileContents = file.readAll()
- file.close()
- sPackage = sPackage .. fs.getName( value ) .. sSplitString .. fileContents .. sSplitString
- end
- file = fs.open( sDestination, "w" )
- file.write( sPackage )
- file.close()
- end
- end
- else
- printUsage()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement