BombBloke

MSC ASTC to CAB

Mar 8th, 2021 (edited)
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.43 KB | None | 0 0
  1. -- Only mostly shitty MSC ASTC exporter.
  2.  
  3. -- Repackages the texture data into Unity
  4. -- CABs so's AssetStudio can parse it.
  5.  
  6. -- Processes all files and subfolders it can find.
  7. -- *.png / *.jpg / *.tga will end up with *.cab next to them.
  8.  
  9. require "lfs"
  10.  
  11. local function processImg(infile, entry)
  12.     local input = io.open("c281abfcf2c9ab349a5adca88b286ec0", "rb")
  13.  
  14.     local output = io.open(infile:sub(1, #infile - 3) .. "cab", "wb")
  15.  
  16.     output:write(input:read(4096))
  17.    
  18.     if #entry > 12 then entry = entry:sub(1, 12) end
  19.     if #entry < 12 then entry = entry .. string.rep(" ", 12 - #entry) end
  20.    
  21.     output:write("\12\00\00\00" .. entry)
  22.    
  23.     input:read(16)
  24.     output:write(input:read(8))
  25.    
  26.     local input2 = io.open(infile, "rb")
  27.  
  28.     local size = lfs.attributes(infile).size - 16
  29.  
  30.     input2:read(4)
  31.     local texType = 44 + input2:read(1):byte()
  32.     input2:read(2)
  33.     local wB = input2:read(2)
  34.     local width = wB:byte(1) + wB:byte(2) * 256
  35.     input2:read(1)
  36.     local hB = input2:read(2)
  37.     local height = hB:byte(1) + hB:byte(2) * 256
  38.     input2:read(4)
  39.  
  40.     output:write(wB .. "\0\0" .. hB .. "\0\0")
  41.  
  42.     input:read(8)
  43.  
  44.     --[[
  45.     0x30 48 ASTC_4x4    ASTC (4x4 pixel block in 128 bits) compressed RGB(A) texture format.
  46.     0x31 49 ASTC_5x5    ASTC (5x5 pixel block in 128 bits) compressed RGB(A) texture format.
  47.     0x32 50 ASTC_6x6    ASTC (6x6 pixel block in 128 bits) compressed RGB(A) texture format.
  48.     ASTC_8x8    ASTC (8x8 pixel block in 128 bits) compressed RGB(A) texture format.
  49.     ASTC_10x10  ASTC (10x10 pixel block in 128 bits) compressed RGB(A) texture format.
  50.     ASTC_12x12  ASTC (12x12 pixel block in 128 bits) compressed RGB(A) texture format.
  51.     ]]--
  52.  
  53.     output:write(input:read(4) .. string.char(texType))
  54.  
  55.     input:read(1)
  56.  
  57.     output:write(input:read(59))
  58.  
  59.     input:read(size)
  60.     output:write(input2:read(size))
  61.  
  62.     output:write(input:read(4198508) or "something fucked up here")
  63.  
  64.     input:close()
  65.     input2:close()
  66.     output:close()
  67. end
  68.  
  69. local function scanDir(dir)
  70.     for entry in lfs.dir(dir) do if entry ~= "." and entry ~= ".." then
  71.         local attrib = lfs.attributes(dir .. "/" .. entry)
  72.         local ext = entry:sub(-3):lower()
  73.        
  74.         if not attrib then print("Failed to access attribute data for " .. dir .. "/" .. entry) end
  75.        
  76.         if attrib.mode == "directory" then
  77.             scanDir(dir .. "/" .. entry)
  78.         elseif ext == "png" or ext == "jpg" or ext == "tga" or ext == "bmp" then
  79.             processImg(dir .. "/" .. entry, entry:sub(1, #entry - 4))
  80.         end
  81.     end end
  82. end
  83.  
  84. return scanDir(lfs.currentdir())
Add Comment
Please, Sign In to add comment