420 lines
13 KiB
Luau
420 lines
13 KiB
Luau
--!strict
|
|
-- Formats: read, write and convert file formats in pure Luau, on Roblox and on LuaJIT / LÖVE (through tools/build.py).
|
|
-- Every format of a category maps onto one standard type (src/Formats/Types.luau): a PNG and a WebP both load as an
|
|
-- Image. Formats are found through the generated Registry: by content (each format's sniff), then by extension.
|
|
--
|
|
-- local F = require(Formats) -- Roblox: the ModuleScript; LuaJIT: require("formats"); luau: "@formats"
|
|
-- local img, err = F.load(src, opts?) -- src: buffer | string | path (hosts with files); err: core/Err
|
|
-- local img = F.image.load(src) -- the same, restricted to one category (F.audio, F.model, ...)
|
|
-- local dec = F.audio.open(src) -- streaming decoders (audio / video)
|
|
-- local buf = F.save(img, nil, { format = "png" }) -- a buffer; without format: the source format
|
|
-- F.save(img, "out.png") -- a file (the format from the extension), on hosts with files
|
|
-- local s = F.saveString(img, { format = "png" })
|
|
-- local buf = F.convert(src, "png", opts?) -- load + save
|
|
-- F.formats() -- the capability list (also docs/FORMATS.md)
|
|
--
|
|
-- Never throws on bad data or bad arguments: every function returns nil, Err (Err.code, Err.message, tostring(err)).
|
|
local Err = require("@formats/core/Err")
|
|
local Bytes = require("@formats/core/Bytes")
|
|
local Input = require("@formats/core/Input")
|
|
local Types = require("@formats/Types")
|
|
local Registry = require("@formats/Registry")
|
|
local host = require("@formats/host")
|
|
|
|
local F = {}
|
|
|
|
F.version = "0.1.0"
|
|
F.Err = Err
|
|
F.Types = Types
|
|
F.Bytes = Bytes
|
|
F.null = Types.null
|
|
F.host = host
|
|
F.plugins = {} :: { [string]: any } -- injected optional dependencies (plugins.brep for cad/step)
|
|
|
|
-- value.kind -> category
|
|
local KIND_CATEGORY: { [string]: string } = {
|
|
image = "image",
|
|
audio = "audio",
|
|
scene = "model",
|
|
cad = "cad",
|
|
font = "font",
|
|
archive = "archive",
|
|
vector = "vector",
|
|
tilemap = "vector",
|
|
layered = "layered",
|
|
subtitles = "subtitle",
|
|
music = "music",
|
|
midi = "music",
|
|
playlist = "playlist",
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ registry access
|
|
local loaded: { [string]: any } = {}
|
|
|
|
local function module(entry: Registry.Entry): (any, Err.Err?)
|
|
local m = loaded[entry.path]
|
|
if m then
|
|
return m
|
|
end
|
|
local ok, res = pcall(entry.load)
|
|
if not ok then
|
|
return nil, Err.new(Err.UNSUPPORTED, "format " .. entry.name .. " failed to load: " .. tostring(res))
|
|
end
|
|
loaded[entry.path] = res
|
|
return res
|
|
end
|
|
|
|
local function inCategory(entry: Registry.Entry, category: string?): boolean
|
|
return category == nil or entry.category == category
|
|
end
|
|
|
|
-- find(nameOrExt, category?) -> the entry whose name, then whose extension, matches
|
|
local function find(key: string, category: string?): Registry.Entry?
|
|
local k = string.lower(key)
|
|
for _, e in Registry do
|
|
if e.name == k and inCategory(e, category) then
|
|
return e
|
|
end
|
|
end
|
|
for _, e in Registry do
|
|
if inCategory(e, category) and table.find(e.extensions, k) then
|
|
return e
|
|
end
|
|
end
|
|
return nil
|
|
end
|
|
|
|
-- detect(buf, name?, category?, format?) -> entry | nil, Err: opts.format, else the best sniff, else the extension
|
|
local function detect(buf: buffer, name: string?, category: string?, format: string?): (Registry.Entry?, Err.Err?)
|
|
if format then
|
|
local e = find(format, category)
|
|
if not e then
|
|
return nil, Err.new(Err.UNKNOWN_FORMAT, "no format named " .. format .. (if category then " in " .. category else ""))
|
|
end
|
|
return e
|
|
end
|
|
local ext = Input.extension(name)
|
|
local best: Registry.Entry? = nil
|
|
local bestScore = 0
|
|
for _, e in Registry do
|
|
if inCategory(e, category) and (e.caps.decode or e.caps.open) then
|
|
local m = module(e)
|
|
if m and m.sniff then
|
|
local ok, score = pcall(m.sniff, Bytes.reader(buf))
|
|
if ok and type(score) == "number" and score > 0 then
|
|
if ext and table.find(e.extensions, ext) then
|
|
score += 0.01 -- ties go to the extension
|
|
end
|
|
if score > bestScore then
|
|
best, bestScore = e, score
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
if best then
|
|
return best
|
|
end
|
|
if ext then
|
|
local e = find(ext, category)
|
|
if e then
|
|
return e
|
|
end
|
|
end
|
|
return nil, Err.new(Err.UNKNOWN_FORMAT, "unrecognised data" .. (if name then " (" .. name .. ")" else "") .. (if category then " for " .. category else ""))
|
|
end
|
|
|
|
local function tag(value: any, formatName: string)
|
|
if type(value) == "table" then
|
|
local meta = value.meta
|
|
if type(meta) == "table" then
|
|
if meta.source == nil then
|
|
meta.source = { format = formatName }
|
|
elseif type(meta.source) == "table" and meta.source.format == nil then
|
|
meta.source.format = formatName
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
-- full decode through a streaming decoder (audio formats with open but no decode)
|
|
local function drain(dec: any): any
|
|
local channels = dec.channels
|
|
local parts, total = {}, 0
|
|
while true do
|
|
local out, n = dec:read(65536)
|
|
if not out or n == 0 then
|
|
break
|
|
end
|
|
local copy = buffer.create(n * channels * 4)
|
|
buffer.copy(copy, 0, out, 0, n * channels * 4)
|
|
table.insert(parts, copy)
|
|
total += n
|
|
end
|
|
local a = Types.Audio.new(dec.sampleRate, channels, total)
|
|
local o = 0
|
|
for _, p in parts do
|
|
buffer.copy(a.samples, o, p)
|
|
o += buffer.len(p)
|
|
end
|
|
a.tags = dec.tags or {}
|
|
return a
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ API
|
|
export type Options = { [string]: any } -- format (name or extension), category, path, data, name, + format options
|
|
|
|
local load: (src: any, opts: Options?) -> (any, Err.Err?)
|
|
|
|
-- single-stream archives (gzip / zst / xz / bz2 / br: meta.stream) whose one entry sniffs as another archive (a
|
|
-- .tar.gz) load as that inner archive, with meta.compression = the outer format. opts.unwrap = false keeps the outer
|
|
-- one; an explicit opts.format also does.
|
|
local function unwrap(value: any, formatName: string, o: Options): any
|
|
if o.unwrap == false or o.format ~= nil or (o._depth or 0) >= 3 then
|
|
return value
|
|
end
|
|
if type(value) ~= "table" or value.kind ~= "archive" or type(value.meta) ~= "table" or not value.meta.stream then
|
|
return value
|
|
end
|
|
local ok, list = pcall(value.list, value)
|
|
if not ok or type(list) ~= "table" or #list ~= 1 then
|
|
return value
|
|
end
|
|
local rok, inner = pcall(value.read, value, list[1].name)
|
|
if not rok or type(inner) ~= "buffer" then
|
|
return value
|
|
end
|
|
local e = detect(inner, nil, "archive", nil)
|
|
if not e then
|
|
return value
|
|
end
|
|
local io = table.clone(o)
|
|
io.category = "archive"
|
|
io.name = list[1].name
|
|
io._depth = (o._depth or 0) + 1
|
|
local v = load(inner, io)
|
|
if type(v) ~= "table" or type(v.meta) ~= "table" then
|
|
return value
|
|
end
|
|
v.meta.compression = formatName
|
|
return v
|
|
end
|
|
|
|
-- load(src, opts?) -> value | nil, Err
|
|
function F.load(src: any, opts: Options?): (any, Err.Err?)
|
|
local o = opts or {}
|
|
local buf, ierr, name = Input.resolve(src, o, host)
|
|
if not buf then
|
|
return nil, ierr
|
|
end
|
|
if name and o.name == nil then
|
|
o = table.clone(o)
|
|
o.name = name -- formats may use the file name (single-stream archives name their entry after it)
|
|
end
|
|
local entry, derr = detect(buf, name, o.category, o.format)
|
|
if not entry then
|
|
return nil, derr
|
|
end
|
|
local m, merr = module(entry)
|
|
if not m then
|
|
return nil, merr
|
|
end
|
|
local value, err
|
|
if m.decode then
|
|
local ok, v, e = pcall(m.decode, Bytes.reader(buf), o)
|
|
if not ok then
|
|
return nil, Err.wrap(Err.new(Err.CORRUPT, "decoder threw: " .. tostring(v)), entry.name)
|
|
end
|
|
value, err = v, e
|
|
elseif m.open then
|
|
local ok, dec, e = pcall(m.open, Bytes.reader(buf), o)
|
|
if not ok then
|
|
return nil, Err.wrap(Err.new(Err.CORRUPT, "decoder threw: " .. tostring(dec)), entry.name)
|
|
end
|
|
if dec then
|
|
value = drain(dec)
|
|
end
|
|
err = e
|
|
else
|
|
return nil, Err.wrap(Err.new(Err.UNSUPPORTED, "no decoder"), entry.name)
|
|
end
|
|
if value == nil then
|
|
return nil, Err.wrap(err or Err.new(Err.CORRUPT, "decode failed"), entry.name)
|
|
end
|
|
tag(value, entry.name)
|
|
return unwrap(value, entry.name, o)
|
|
end
|
|
load = F.load
|
|
|
|
-- open(src, opts?) -> a streaming decoder (audio Decoder / VideoDecoder / Demuxer) | nil, Err
|
|
function F.open(src: any, opts: Options?): (any, Err.Err?)
|
|
local o = opts or {}
|
|
local buf, ierr, name = Input.resolve(src, o, host)
|
|
if not buf then
|
|
return nil, ierr
|
|
end
|
|
local entry, derr = detect(buf, name, o.category, o.format)
|
|
if not entry then
|
|
return nil, derr
|
|
end
|
|
local m, merr = module(entry)
|
|
if not m then
|
|
return nil, merr
|
|
end
|
|
if not m.open then
|
|
return nil, Err.wrap(Err.new(Err.UNSUPPORTED, "no streaming decoder (use load)"), entry.name)
|
|
end
|
|
local ok, dec, e = pcall(m.open, Bytes.reader(buf), o)
|
|
if not ok then
|
|
return nil, Err.wrap(Err.new(Err.CORRUPT, "decoder threw: " .. tostring(dec)), entry.name)
|
|
end
|
|
if dec == nil then
|
|
return nil, Err.wrap(e or Err.new(Err.CORRUPT, "open failed"), entry.name)
|
|
end
|
|
return dec
|
|
end
|
|
|
|
-- the writable format for a value: opts.format, the destination's extension, or the source format
|
|
local function encoderFor(value: any, dest: string?, o: Options): (Registry.Entry?, any, Err.Err?)
|
|
local category = o.category
|
|
if category == nil and type(value) == "table" and type(value.kind) == "string" then
|
|
category = KIND_CATEGORY[value.kind]
|
|
end
|
|
local key = o.format or Input.extension(dest)
|
|
if key == nil and type(value) == "table" and type(value.meta) == "table" and type(value.meta.source) == "table" then
|
|
key = value.meta.source.format
|
|
end
|
|
if key == nil then
|
|
return nil, nil, Err.new(Err.ARG, "save: no format (pass opts.format or a destination with an extension)")
|
|
end
|
|
local entry = find(key, category)
|
|
if not entry then
|
|
return nil, nil, Err.new(Err.UNKNOWN_FORMAT, "no format named " .. key .. (if category then " for " .. category else ""))
|
|
end
|
|
local m, merr = module(entry)
|
|
if not m then
|
|
return nil, nil, merr
|
|
end
|
|
if not m.encode then
|
|
local alt = nil
|
|
for _, e in Registry do
|
|
if e.category == entry.category and e.caps.encode then
|
|
alt = e.name
|
|
break
|
|
end
|
|
end
|
|
return nil, nil, Err.wrap(Err.new(Err.UNSUPPORTED, "read-only format" .. (if alt then "; try " .. alt else "")), entry.name)
|
|
end
|
|
return entry, m, nil
|
|
end
|
|
|
|
-- save(value, dest?, opts?) -> buffer (no dest) | true (written to dest) | nil, Err
|
|
function F.save(value: any, dest: string?, opts: Options?): (any, Err.Err?)
|
|
local o = opts or {}
|
|
local entry, m, err = encoderFor(value, dest, o)
|
|
if not entry then
|
|
return nil, err
|
|
end
|
|
local ok, buf, e = pcall(m.encode, value, o)
|
|
if not ok then
|
|
return nil, Err.wrap(Err.new(Err.ARG, "encoder threw: " .. tostring(buf)), entry.name)
|
|
end
|
|
if buf == nil then
|
|
return nil, Err.wrap(e or Err.new(Err.ARG, "encode failed"), entry.name)
|
|
end
|
|
if dest then
|
|
local wok, werr = host.writeFile(dest, buf)
|
|
if not wok then
|
|
return nil, werr
|
|
end
|
|
return true
|
|
end
|
|
return buf
|
|
end
|
|
|
|
-- saveString(value, opts?) -> string | nil, Err
|
|
function F.saveString(value: any, opts: Options?): (string?, Err.Err?)
|
|
local buf, err = F.save(value, nil, opts)
|
|
if not buf then
|
|
return nil, err
|
|
end
|
|
return buffer.tostring(buf)
|
|
end
|
|
|
|
-- convert(src, format, opts?) -> buffer (or true with opts.out = a path) | nil, Err. opts go to both load and save.
|
|
function F.convert(src: any, format: string, opts: Options?): (any, Err.Err?)
|
|
local o = opts or {}
|
|
local value, err = F.load(src, o)
|
|
if value == nil then
|
|
return nil, err
|
|
end
|
|
local so = table.clone(o)
|
|
so.format = format
|
|
so.category = nil
|
|
return F.save(value, o.out, so)
|
|
end
|
|
|
|
-- detect(src, opts?) -> format name | nil, Err
|
|
function F.detect(src: any, opts: Options?): (string?, Err.Err?)
|
|
local o = opts or {}
|
|
local buf, ierr, name = Input.resolve(src, o, host)
|
|
if not buf then
|
|
return nil, ierr
|
|
end
|
|
local entry, err = detect(buf, name, o.category, o.format)
|
|
if not entry then
|
|
return nil, err
|
|
end
|
|
return entry.name
|
|
end
|
|
|
|
export type FormatInfo = { name: string, category: string, extensions: { string }, mime: { string }, caps: { [string]: any } }
|
|
|
|
-- formats(category?) -> { FormatInfo } (no format module is loaded)
|
|
function F.formats(category: string?): { FormatInfo }
|
|
local out = {}
|
|
for _, e in Registry do
|
|
if inCategory(e, category) then
|
|
table.insert(out, { name = e.name, category = e.category, extensions = e.extensions, mime = e.mime, caps = e.caps })
|
|
end
|
|
end
|
|
return out
|
|
end
|
|
|
|
-- format(name) -> the format module itself (its contract table: sniff / decode / encode / extras such as png.info)
|
|
function F.format(name: string): (any, Err.Err?)
|
|
local e = find(name)
|
|
if not e then
|
|
return nil, Err.new(Err.UNKNOWN_FORMAT, "no format named " .. name)
|
|
end
|
|
return module(e)
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ category facades
|
|
-- F.image.load / save / open / formats, F.audio..., F.model... : the same calls restricted to one category
|
|
local function with(opts: Options?, category: string): Options
|
|
local o = if opts then table.clone(opts) else {}
|
|
o.category = category
|
|
return o
|
|
end
|
|
for category in Types.CATEGORIES do
|
|
local cat: string = category
|
|
F[cat] = {
|
|
load = function(src: any, opts: Options?): (any, Err.Err?)
|
|
return F.load(src, with(opts, cat))
|
|
end,
|
|
open = function(src: any, opts: Options?): (any, Err.Err?)
|
|
return F.open(src, with(opts, cat))
|
|
end,
|
|
save = function(value: any, dest: string?, opts: Options?): (any, Err.Err?)
|
|
return F.save(value, dest, with(opts, cat))
|
|
end,
|
|
formats = function(): { FormatInfo }
|
|
return F.formats(cat)
|
|
end,
|
|
}
|
|
end
|
|
F.data.null = Types.null
|
|
|
|
return F
|