--!strict -- archive/unitypackage: Unity asset packages, a gzip'd tar of /asset, /asset.meta, /pathname -- (+ preview.png), read + write as an Archive keyed by the asset paths (Assets/...), each .meta next to its asset as -- in a Unity project. Supports: folders (meta only), lazy entries, guids kept (entry.guid) or made from the path. -- Not supported: pre-5.x packages without pathname files. -- ------------------------------------------------------------------------------------------------ requires local Err = require("@rosetta/core/Err") local Bytes = require("@rosetta/core/Bytes") local Md5 = require("@rosetta/core/Md5") local Deflate = require("@rosetta/codecs/compress/Deflate") local Archive = require("@rosetta/containers/Archive") local Tar = require("@rosetta/containers/Tar") -- ------------------------------------------------------------------------------------------------ contract local UnityPackage = { name = "unitypackage", category = "archive", extensions = { "unitypackage" }, mime = { "application/x-unitypackage" }, deps = { "core/Err", "core/Bytes", "core/Md5", "codecs/compress/Deflate", "containers/Archive", "containers/Tar" }, caps = { decode = true, encode = true }, } export type DecodeOptions = { maxEntries: number?, -- default 1e6 maxBytes: number?, -- inflated size, default 2^31 } export type EncodeOptions = { level: number?, -- deflate 0..9, default 6 mtime: number?, -- tar entry time, default 0 } -- ------------------------------------------------------------------------------------------------ sniff local function guidOf(name: string): (string?, string?) local n = string.gsub(name, "^%./", "") local guid, rest = string.match(n, "^(%x+)/?(.*)$") if guid and #guid == 32 then return string.lower(guid), rest end return nil, nil end function UnityPackage.sniff(r: Bytes.Reader): number if r:peek(3) ~= "\31\139\8" then return 0 end local head = Deflate.peek(r.buf, r.pos, r.limit - r.pos, 512) if not head or buffer.len(head) < 512 or not Tar.valid(head, 0) then return 0 end local z = 0 while z < 100 and buffer.readu8(head, z) ~= 0 do z += 1 end return if guidOf(buffer.readstring(head, 0, z)) then 1 else 0 -- ties with archive/gzip go to the extension end -- ------------------------------------------------------------------------------------------------ decode local function encode(archive: any, opts: any?): (buffer?, Err.Err?) return UnityPackage.encode(archive, opts) end type Asset = { asset: Tar.Entry?, meta: Tar.Entry?, pathname: Tar.Entry?, preview: Tar.Entry?, [string]: any } local function text(b: buffer, e: Tar.Entry): string return buffer.readstring(b, e.offset, e.size) end function UnityPackage.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?) local o: DecodeOptions = opts or {} local tar, err = Deflate.gunzip(r.buf, r.base, r.limit - r.base) if not tar then return nil, err end if buffer.len(tar) > (o.maxBytes or 2 ^ 31) then return nil, Err.new(Err.LIMIT, "inflated size over the byte limit") end local list, terr = Tar.read(tar, 0, buffer.len(tar), { maxEntries = o.maxEntries }) if not list then return nil, terr end local byGuid: { [string]: Asset } = {} local order: { string } = {} for _, e in list do local guid, rest = guidOf(e.name) if guid and not e.dir then local a = byGuid[guid] if not a then a = {} :: Asset byGuid[guid] = a table.insert(order, guid) end if rest == "asset" then a.asset = e elseif rest == "asset.meta" then a.meta = e elseif rest == "pathname" then a.pathname = e elseif rest == "preview.png" then a.preview = e end end end local arc = Archive.new("unitypackage", encode) local skipped = 0 for _, guid in order do local a = byGuid[guid] local pn = a.pathname if not pn then skipped += 1 else -- the first line is the path (Unity 5+ may add a second line, e.g. "00") local path = string.match(text(tar, pn), "^[^\r\n]*") or "" path = string.gsub(path, "\\", "/") if path ~= "" then local asset, meta = a.asset, a.meta local isDir = asset == nil Archive.put(arc, { name = path, size = if asset then asset.size else 0, dir = isDir, mtime = if asset then asset.mtime elseif meta then meta.mtime else nil, method = "deflate", guid = guid, load = if asset and asset.size > 0 then Archive.slice(tar, asset.offset, asset.size) else nil, preview = if a.preview then Archive.slice(tar, a.preview.offset, a.preview.size) else nil, }) if meta then Archive.put(arc, { name = path .. ".meta", size = meta.size, dir = false, mtime = meta.mtime, method = "deflate", guid = guid, load = if meta.size > 0 then Archive.slice(tar, meta.offset, meta.size) else nil, }) end end end end arc.meta.source = { format = "unitypackage", assets = #order - skipped, skipped = if skipped > 0 then skipped else nil } return arc, nil end -- ------------------------------------------------------------------------------------------------ encode local function metaGuid(b: buffer): string? local s = buffer.readstring(b, 0, math.min(buffer.len(b), 4096)) local g = string.match(s, "\nguid:%s*(%x+)") or string.match(s, "^guid:%s*(%x+)") if g and #g == 32 then return string.lower(g) end return nil end local function defaultMeta(guid: string, dir: boolean): string if dir then return "fileFormatVersion: 2\nguid: " .. guid .. "\nfolderAsset: yes\nDefaultImporter:\n externalObjects: {}\n userData: \n assetBundleName: \n assetBundleVariant: \n" end return "fileFormatVersion: 2\nguid: " .. guid .. "\nDefaultImporter:\n externalObjects: {}\n userData: \n assetBundleName: \n assetBundleVariant: \n" end function UnityPackage.encode(archive: any, opts: EncodeOptions?): (buffer?, Err.Err?) local o: EncodeOptions = opts or {} local items = Archive.entries(archive) if not items then return nil, Err.new(Err.ARG, "expected an Archive") end local byName: { [string]: any } = {} for _, it in items do byName[string.gsub(it.entry.name, "\\", "/")] = it end local mtime = o.mtime or 0 local list: { Tar.WriteEntry } = {} local used: { [string]: boolean } = {} for _, it in items do local e: any = it.entry local path = string.gsub((string.gsub(e.name, "\\", "/")), "/+$", "") local metaIt = byName[path .. ".meta"] local isMeta = string.sub(path, -5) == ".meta" and byName[string.sub(path, 1, -6)] ~= nil if path ~= "" and not isMeta then local data: buffer? = nil if not e.dir then local b, rerr = it.read() if not b then return nil, rerr end data = b end local metaData: buffer? = nil if metaIt then local mb, merr = metaIt.read() if not mb then return nil, merr end metaData = mb end local given: string? = if type(e.guid) == "string" and #e.guid == 32 and string.match(e.guid, "^%x+$") then string.lower(e.guid) else nil local g: string = given or (if metaData then metaGuid(metaData) else nil) or Md5.of(buffer.fromstring(path)) if used[g] then g = Md5.of(buffer.fromstring(path .. "\0" .. g)) end used[g] = true metaData = metaData or buffer.fromstring(defaultMeta(g, e.dir == true)) table.insert(list, { name = g .. "/", dir = true, mtime = mtime }) if data then table.insert(list, { name = g .. "/asset", data = data, mtime = mtime }) end table.insert(list, { name = g .. "/asset.meta", data = metaData, mtime = mtime }) table.insert(list, { name = g .. "/pathname", data = buffer.fromstring(path), mtime = mtime }) if type(e.preview) == "function" then local ok, pb = pcall(e.preview) if ok and type(pb) == "buffer" then table.insert(list, { name = g .. "/preview.png", data = pb, mtime = mtime }) end end end end local tar, terr = Tar.write(list, { format = "ustar" }) if not tar then return nil, terr end return Deflate.gzip(tar, { level = o.level or 6 }) end -- ------------------------------------------------------------------------------------------------ extras function UnityPackage.new(): any local a = Archive.new("unitypackage", encode) a.meta.source = { format = "unitypackage" } return a end return UnityPackage