Don't like ads? PRO users don't see any ads ;-)
Guest

NXP

By: PaymentOption on Sep 18th, 2012  |  syntax: Lua  |  size: 4.49 KB  |  hits: 25  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. local tArgs = { ... }
  2. local bExtract = false -- Extract or Pack.
  3. local sDestination = ""
  4.  
  5. local bRunning = true
  6. local sSplitString = "!dnf32Dsf!"
  7.  
  8. -- Extra methods --
  9. function printUsage()
  10.         print( "Usage: nxp <extract/pack> <.nxpFile/directory>" )
  11.         print( "              <outputDirectory/.nxpOutputFile>" )
  12. end
  13.  
  14. -- Compatibility: Lua-5.1; Borrowed from: http://lua-users.org/wiki/SplitJoin
  15. function split(str, pat)
  16.    local t = {}  -- NOTE: use {n = 0} in Lua-5.0
  17.    local fpat = "(.-)" .. pat
  18.    local last_end = 1
  19.    local s, e, cap = str:find(fpat, 1)
  20.    while s do
  21.       if s ~= 1 or cap ~= "" then
  22.          table.insert(t,cap)
  23.       end
  24.       last_end = e+1
  25.       s, e, cap = str:find(fpat, last_end)
  26.    end
  27.    if last_end <= #str then
  28.       cap = str:sub(last_end)
  29.       table.insert(t, cap)
  30.    end
  31.    return t
  32. end
  33. -------------------
  34.  
  35. function determineMethod( sArg )
  36.         if sArg == "extract" then
  37.                 return true
  38.         elseif sArg == "pack" then
  39.                 return false
  40.         else
  41.                 return nil
  42.         end
  43. end
  44.  
  45. function checkFileExistance()
  46.         if tArgs[1] == "extract" then
  47.                 if fs.exists( tArgs[2] ) then
  48.                         if not fs.exists( tArgs[3] ) then
  49.                                 sDestination = tArgs[3]
  50.                                 return true
  51.                         else
  52.                                 error( "Cannot extract there." )
  53.                         end
  54.                 else
  55.                         error( "Zip to be extracted doesn't exist." )
  56.                 end
  57.         else
  58.                 if fs.exists( tArgs[2] ) and fs.isDir( tArgs[2] ) then
  59.                         if not fs.exists( tArgs[3] ) then
  60.                                 sDestination = tArgs[3] .. ".nxp"
  61.                                 return true
  62.                         else
  63.                                 error( "Cannot transfer package to an existant file." )
  64.                         end
  65.                 else
  66.                         error( "Cannot package " .. tArgs[2] .. "." )
  67.                 end
  68.         end
  69. end
  70.  
  71. if #tArgs < 3 then
  72.         printUsage()
  73.         bRunning = false
  74. end
  75.  
  76. -- NexPack specific functions.
  77. -- Takes a string of a package and splits it into a table that it returns.
  78. -- Format: tPackage[n] = { Name, Contents }
  79. function readPackageIntoTable( sPackage )
  80.         local tPackage = split( sPackage, sSplitString )
  81.         local tPackage_Appended = {}
  82.         local nIndex = 1
  83.        
  84.         for i=1, #tPackage, 2 do
  85.                 tPackage_Appended[nIndex] = { Name = tPackage[i], Contents = tPackage[i+1] }
  86.                 nIndex = nIndex + 1
  87.         end
  88.        
  89.         return tPackage_Appended
  90. end
  91.  
  92. -- Takes a unpackaged table and a directory to write the contents of the table into files in the directory passed.
  93. function writePackageToDirectory( tPacked, sDirectory )
  94.         local file = nil
  95.        
  96.         for index,value in ipairs( tPacked ) do
  97.                 file = fs.open( sDirectory .. "/" .. tPacked[index].Name, "w" )
  98.                 file.write( tPacked[index].Contents )
  99.                 file.close()
  100.         end
  101. end
  102.  
  103. if bRunning then
  104.         bExtract = determineMethod( tArgs[1] )
  105.         if bExtract ~= nil then
  106.                 -- Extract the selected package.
  107.                 if bExtract then
  108.                         -- Make sure all paths and files are in their correct places and or states.
  109.                         if checkFileExistance() then
  110.                                 -- Create the directory we'll be extracting this package to.
  111.                                 fs.makeDir( tArgs[3] )
  112.                                
  113.                                 -- Get the package in a string format for splitting.
  114.                                 local package = fs.open( tArgs[2], "r" )
  115.                                 local packageContents = package.readAll()
  116.                                 package.close()
  117.                                
  118.                                 -- Get the package contents into a table for writing into separate files.
  119.                                 local tPackageContents = readPackageIntoTable( packageContents )
  120.                                 -- Format: tPackageContents[n] = { Name, Contents }
  121.                                 -- Write the package table into separate files.
  122.                                 writePackageToDirectory( tPackageContents, tArgs[3] )
  123.                         end
  124.                 -- Package the selected directory.
  125.                 else
  126.                         -- Make sure all paths and files are in their correct places or states.
  127.                         if checkFileExistance() then
  128.                                 local sPackage = sSplitString -- The final string of the package.
  129.                                 local sDir = tArgs[2]
  130.                                 local tContents = fs.list( sDir )
  131.                                 local tFiles = {} -- The files will have their full path in their name!
  132.                                
  133.                                 -- Seperate all objects in the selected directory so only the files are packaged.
  134.                                 for index, value in ipairs( tContents ) do
  135.                                         if not fs.isDir( sDir .. "/" .. value ) then
  136.                                                 table.insert( tFiles, sDir .. "/" .. value ) -- The fiels will have their full path in their name!
  137.                                         end
  138.                                 end
  139.                                
  140.                                 -- Read the files into the proper format within a file.
  141.                                 local file = nil
  142.                                 local fileContents = ""
  143.                                 for index,value in ipairs( tFiles ) do
  144.                                         file = fs.open( value, "r" )
  145.                                         fileContents = file.readAll()
  146.                                         file.close()
  147.                                        
  148.                                         sPackage = sPackage .. fs.getName( value ) .. sSplitString .. fileContents .. sSplitString
  149.                                 end
  150.                                
  151.                                 file = fs.open( sDestination, "w" )
  152.                                 file.write( sPackage )
  153.                                 file.close()
  154.                         end
  155.                 end
  156.         else
  157.                 printUsage()
  158.         end
  159. end