1112 lines
39 KiB
Luau
1112 lines
39 KiB
Luau
--!strict
|
|
-- Types: the standard value types each category decodes to and encodes from, plus small constructors. Every
|
|
-- top-level value has a `kind` ("image", "audio", "scene", ...). Shared enums are plain strings; buffers are tightly
|
|
-- packed, rows top to bottom, channels interleaved.
|
|
local Err = require("@rosetta/core/Err")
|
|
|
|
local Types = {}
|
|
|
|
export type Err = Err.Err
|
|
|
|
-- pixel formats: bytes per pixel. rgba8 is the default every decoder produces unless opts.native asks to keep the
|
|
-- source layout. yuv420p: Y plane (w*h) then U and V planes (ceil(w/2) * ceil(h/2) each).
|
|
export type PixelFormat = "rgba8" | "rgba16" | "rgbaf32" | "rgb8" | "gray8" | "gray16" | "graya8" | "alpha8" | "yuv420p"
|
|
local BPP: { [string]: number } = {
|
|
rgba8 = 4,
|
|
rgba16 = 8,
|
|
rgbaf32 = 16,
|
|
rgb8 = 3,
|
|
gray8 = 1,
|
|
gray16 = 2,
|
|
graya8 = 2,
|
|
alpha8 = 1,
|
|
}
|
|
Types.BPP = table.freeze(BPP) :: { [string]: number }
|
|
|
|
export type ImageMeta = {
|
|
colorSpace: string?, -- "srgb" (default), "linear", "display-p3", ...
|
|
gamma: number?,
|
|
icc: buffer?, -- the raw ICC profile
|
|
exif: buffer?, -- the raw EXIF block (TIFF header onwards)
|
|
dpi: { x: number, y: number }?,
|
|
palette: { { number } }?, -- { r, g, b, a } 0..255 per entry, for palette sources
|
|
text: { [string]: string }?, -- tEXt / comments
|
|
source: { format: string, bitDepth: number?, colorType: string?, [string]: any }?,
|
|
[string]: any,
|
|
}
|
|
|
|
-- one frame of an animation (GIF, APNG, WebP): the image is the frame's own rectangle, composited at x, y
|
|
export type Frame = {
|
|
image: Image,
|
|
delay: number, -- seconds
|
|
x: number,
|
|
y: number,
|
|
dispose: "none" | "background" | "previous",
|
|
blend: "source" | "over",
|
|
}
|
|
|
|
export type Image = {
|
|
kind: "image",
|
|
width: number,
|
|
height: number,
|
|
format: PixelFormat,
|
|
pixels: buffer,
|
|
frames: { Frame }?, -- animations: frame 1 is also width x height pixels (the composited first frame)
|
|
loops: number, -- play count: how many times the animation plays, 0 = forever (GIF stores repeats: plays - 1; APNG
|
|
-- num_plays and the WebP ANIM loop count are play counts)
|
|
meta: ImageMeta,
|
|
}
|
|
|
|
local Image = {}
|
|
function Image.new(width: number, height: number, format: PixelFormat?): Image
|
|
local f: PixelFormat = format or "rgba8"
|
|
local size
|
|
if f == "yuv420p" then
|
|
local cw, ch = (width + 1) // 2, (height + 1) // 2
|
|
size = width * height + 2 * cw * ch
|
|
else
|
|
size = width * height * (BPP[f] or 4)
|
|
end
|
|
return {
|
|
kind = "image",
|
|
width = width,
|
|
height = height,
|
|
format = f,
|
|
pixels = buffer.create(size),
|
|
frames = nil,
|
|
loops = 0,
|
|
meta = {},
|
|
}
|
|
end
|
|
function Image.bpp(format: string): number?
|
|
return BPP[format]
|
|
end
|
|
Types.Image = Image
|
|
|
|
export type Tags = { [string]: string } -- title, artist, album, date, genre, track, comment, ...
|
|
|
|
export type Decoder = {
|
|
sampleRate: number,
|
|
channels: number,
|
|
frames: number, -- total sample frames (estimate for VBR streams without an index), 0 = unknown
|
|
duration: number, -- seconds
|
|
bitrate: number, -- bits per second (average)
|
|
tags: Tags,
|
|
-- read: interleaved f32, frames read (0 at the end); out is reused when big enough
|
|
read: (self: any, n: number, out: buffer?) -> (buffer, number),
|
|
seek: (self: any, seconds: number) -> (),
|
|
position: (self: any) -> number, -- seconds
|
|
}
|
|
|
|
export type Audio = {
|
|
kind: "audio",
|
|
sampleRate: number,
|
|
channels: number,
|
|
frames: number,
|
|
samples: buffer, -- interleaved f32, frames * channels * 4 bytes
|
|
tags: Tags,
|
|
meta: { source: { format: string, [string]: any }?, [string]: any },
|
|
}
|
|
|
|
local Audio = {}
|
|
function Audio.new(sampleRate: number, channels: number, frames: number): Audio
|
|
return {
|
|
kind = "audio",
|
|
sampleRate = sampleRate,
|
|
channels = channels,
|
|
frames = frames,
|
|
samples = buffer.create(frames * channels * 4),
|
|
tags = {},
|
|
meta = {},
|
|
}
|
|
end
|
|
Types.Audio = Audio
|
|
|
|
export type Module = {
|
|
kind: "music",
|
|
title: string,
|
|
format: string, -- "mod", "s3m", "xm", "it" (codecs/audio/Tracker); "ahx", "hvl" (codecs/audio/Hively, own render)
|
|
channels: number,
|
|
patterns: { any },
|
|
order: { number },
|
|
instruments: { any },
|
|
samples: { any },
|
|
render: (self: any, opts: any?) -> Decoder, -- every music file is also audio
|
|
meta: { [string]: any },
|
|
}
|
|
|
|
export type MidiEvent = { tick: number, type: string, channel: number?, data: { number }?, meta: any? }
|
|
export type Midi = {
|
|
kind: "midi",
|
|
format: number, -- SMF 0 / 1 / 2
|
|
division: number, -- ticks per quarter note (or SMPTE, negative)
|
|
tracks: { { events: { MidiEvent }, name: string? } },
|
|
meta: { [string]: any },
|
|
render: ((self: any, opts: any?) -> Decoder)?, -- set by music/midi decode: the SoundFont synth as an audio Decoder
|
|
}
|
|
local Midi = {}
|
|
function Midi.new(division: number?): Midi
|
|
return { kind = "midi", format = 1, division = division or 480, tracks = {}, meta = {} }
|
|
end
|
|
Types.Midi = Midi
|
|
|
|
-- Times: Packet pts / dts / duration are integer ticks of the track's timescale (exact, for remuxing); VideoFrame pts
|
|
-- / duration and every `duration` / `seek` argument are seconds.
|
|
export type Track = {
|
|
id: number, -- the container's track number (Matroska TrackNumber, AVI stream index + 1, TS PID, ...)
|
|
kind: "video" | "audio" | "subtitle" | "data",
|
|
codec: string, -- our codec id: "vp8", "mjpeg", "mpeg1", "h264", "rawvideo"; "pcm_s16le", "mp3", "opus", "vorbis", ...
|
|
codecTag: string?, -- the container's own id ("V_VP8", "MJPG", stream_type "0x01", "avc1", ...)
|
|
timescale: number, -- ticks per second for pts / dts / duration
|
|
duration: number?, -- seconds
|
|
default: boolean?,
|
|
language: string?,
|
|
name: string?,
|
|
-- video
|
|
width: number?,
|
|
height: number?,
|
|
frameRate: number?, -- frames per second (nominal; 0 / nil when unknown)
|
|
pixelFormat: string?, -- raw video: "yuv420p", "rgb24", "bgr24", ...
|
|
-- audio
|
|
sampleRate: number?,
|
|
channels: number?,
|
|
bitsPerSample: number?,
|
|
codecDelay: number?, -- seconds of decoder output to drop at the start (encoder priming: Matroska CodecDelay, the
|
|
-- first edit's media time of an MP4 audio track)
|
|
playDuration: number?, -- seconds to play after the codec delay (an MP4 edit list's length: the end padding cut)
|
|
config: buffer?, -- codec configuration (CodecPrivate, avcC, OpusHead, Vorbis headers, AVI strf extra, ...)
|
|
[string]: any,
|
|
}
|
|
export type Packet = {
|
|
track: number, -- Track.id
|
|
pts: number, -- presentation time, ticks
|
|
dts: number?, -- decode time, ticks (nil = pts)
|
|
duration: number?, -- ticks
|
|
keyframe: boolean,
|
|
data: buffer, -- the packet's bytes (its own buffer)
|
|
}
|
|
export type Demuxer = {
|
|
kind: "demuxer",
|
|
format: string, -- the container format name
|
|
tracks: { Track },
|
|
duration: number, -- seconds (0 = unknown)
|
|
startTime: number, -- seconds: the first video / audio packet's presentation time (VideoFrame times count from it)
|
|
meta: { [string]: any }, -- title, tags, ...
|
|
packets: (self: any, track: number?) -> () -> Packet?,
|
|
read: (self: any, track: number?) -> Packet?,
|
|
-- seek: every track to its last keyframe at or before the time
|
|
seek: (self: any, seconds: number) -> (),
|
|
track: (self: any, kind: string) -> Track?, -- the first (default) track of a kind
|
|
}
|
|
export type VideoFrame = {
|
|
image: Image, -- yuv420p (the codecs' native output) or rgba8 (opts.rgba = true, MJPEG without yuv, frames-backed)
|
|
pts: number, -- seconds from the start of the stream (the demuxer's startTime)
|
|
duration: number, -- seconds
|
|
keyframe: boolean?,
|
|
index: number, -- 1-based, in output order
|
|
}
|
|
export type VideoDecoder = {
|
|
kind: "video",
|
|
format: string, -- the container format name ("mkv", "avi", "gif", ...)
|
|
codec: string, -- the video codec id ("vp8", "mjpeg", "frames" for animated images)
|
|
width: number,
|
|
height: number,
|
|
frameRate: number, -- frames per second (nominal)
|
|
frames: number, -- frame count when known (0 = unknown)
|
|
duration: number, -- seconds
|
|
track: Track?, -- the video track decoded
|
|
nextFrame: (self: any) -> (VideoFrame?, Err?), -- nil, nil at the end
|
|
toRGBA: (self: any, frame: VideoFrame | Image) -> Image, -- an rgba8 Image (the frame's own image if already rgba8)
|
|
seek: (self: any, seconds: number) -> (), -- the next frame is the first at or after the time (decoded from the keyframe before it)
|
|
reset: (self: any) -> (), -- back to the start
|
|
audio: Decoder?, -- the first audio track, decoded (nil if none or its codec is unavailable: see audioError)
|
|
audioError: Err?,
|
|
demuxer: Demuxer?, -- nil for frames-backed videos (animated images)
|
|
meta: { [string]: any },
|
|
}
|
|
-- a codec's decoder instance (codecs/video/Video registry entries create them): packets in, pictures out
|
|
export type VideoCodecDecoder = {
|
|
decode: (self: any, packet: Packet) -> Err?, -- feed one packet (decode order)
|
|
receive: (self: any) -> (Image?, number?, boolean?), -- the next picture, the pts (ticks) it carries, keyframe | nil
|
|
flush: (self: any) -> (), -- end of stream: pictures held for reordering become available to receive()
|
|
reset: (self: any) -> (), -- after a seek: drop references / held pictures
|
|
}
|
|
|
|
-- Indices are 1-based everywhere (node, mesh, material, texture, ...). Accessor data is tightly packed (no stride, no
|
|
-- padding, sparse accessors already applied). unorm / snorm types are glTF's normalized integers (value / 255,
|
|
-- max(value / 127, -1), ...). Positions are in scene units (meta.unit = metres per unit when known), meta.up is the
|
|
-- source's up axis ("y" for glTF / OBJ, "z" for STL / VOX / most CAD) - no format rotates the data on load.
|
|
export type Accessor = {
|
|
data: buffer, -- tightly packed, little endian
|
|
type: "f32" | "f64" | "u8" | "u16" | "u32" | "i8" | "i16" | "i32" | "unorm8" | "unorm16" | "snorm8" | "snorm16",
|
|
components: number, -- 1 (SCALAR) .. 4 (VEC4), 4 (MAT2), 9 (MAT3), 16 (MAT4)
|
|
count: number,
|
|
matrix: boolean?, -- components 4 / 9 / 16 are MAT2 / MAT3 / MAT4 (glTF), not VEC4
|
|
name: string?,
|
|
}
|
|
export type Primitive = {
|
|
attributes: { [string]: Accessor }, -- POSITION, NORMAL, TANGENT, TEXCOORD_n, COLOR_n, JOINTS_n, WEIGHTS_n; custom
|
|
-- attributes start with "_" (glTF convention: PLY extra vertex properties, ...)
|
|
indices: Accessor?,
|
|
-- "polygons": faces (u32, one size per face) splits indices into n-gons (OBJ / PLY / OFF with triangulate = false;
|
|
-- MeshOps.triangulate turns them into triangles)
|
|
mode: "triangles" | "lines" | "points" | "triangle_strip" | "triangle_fan" | "line_strip" | "line_loop" | "polygons",
|
|
faces: Accessor?,
|
|
material: number?, -- index into scene.materials (1-based)
|
|
targets: { { [string]: Accessor } }?, -- morph targets (POSITION / NORMAL / TANGENT deltas)
|
|
extras: { [string]: any }?,
|
|
}
|
|
export type Mesh = { name: string?, primitives: { Primitive }, weights: { number }?, extras: { [string]: any }? }
|
|
export type Sampler = {
|
|
wrapS: ("repeat" | "clamp" | "mirror")?, -- default "repeat"
|
|
wrapT: ("repeat" | "clamp" | "mirror")?,
|
|
filter: ("linear" | "nearest")?, -- magnification
|
|
minFilter: string?, -- "nearest" | "linear" | "nearest_mipmap_nearest" | ... (glTF names, lower case)
|
|
}
|
|
export type Texture = {
|
|
name: string?,
|
|
image: Image?, -- decoded (F.model.loadTextures or the caller), never by a model format
|
|
data: buffer?, -- the encoded image bytes (embedded / GLB / data: URI / resolved external file)
|
|
mime: string?, -- "image/png", "image/jpeg", "image/webp", "image/ktx2", ...
|
|
uri: string?, -- the external file name (relative to the model) when the data was not available or not embedded
|
|
sampler: Sampler?,
|
|
extras: { [string]: any }?,
|
|
}
|
|
export type TextureRef = {
|
|
texture: number, -- index into scene.textures (1-based)
|
|
texCoord: number?, -- TEXCOORD_n set (default 0)
|
|
scale: number?, -- normalTexture scale
|
|
strength: number?, -- occlusionTexture strength
|
|
-- KHR_texture_transform (texCoord overrides the ref's texCoord)
|
|
transform: { offset: { number }?, scale: { number }?, rotation: number?, texCoord: number? }?,
|
|
}
|
|
export type Material = {
|
|
name: string?,
|
|
baseColor: { number }, -- r, g, b, a linear 0..1
|
|
baseColorTexture: TextureRef?,
|
|
metallic: number,
|
|
roughness: number,
|
|
metallicRoughnessTexture: TextureRef?,
|
|
normalTexture: TextureRef?,
|
|
occlusionTexture: TextureRef?,
|
|
emissive: { number },
|
|
emissiveTexture: TextureRef?,
|
|
alphaMode: "opaque" | "mask" | "blend",
|
|
alphaCutoff: number,
|
|
doubleSided: boolean,
|
|
-- glTF material extensions by name (KHR_materials_emissive_strength = { emissiveStrength = 5 }, KHR_materials_unlit
|
|
-- = {}, ...), their fields as in glTF except textureInfo objects, which are TextureRefs
|
|
extensions: { [string]: any }?,
|
|
extras: { [string]: any }?, -- classic Phong (OBJ / FBX): ambient, diffuse, specular, shininess, ...
|
|
}
|
|
export type Node = {
|
|
name: string?,
|
|
children: { number }, -- node indices (1-based)
|
|
mesh: number?,
|
|
camera: number?,
|
|
light: number?,
|
|
skin: number?,
|
|
voxels: number?, -- index into scene.voxels (MagicaVoxel shapes; MeshOps.meshVoxels gives them meshes)
|
|
translation: { number }?, -- TRS, or matrix (column-major 4x4), not both
|
|
rotation: { number }?, -- quaternion x, y, z, w
|
|
scale: { number }?,
|
|
matrix: { number }?,
|
|
weights: { number }?, -- morph target weights
|
|
extras: { [string]: any }?,
|
|
}
|
|
export type Camera = { name: string?, type: "perspective" | "orthographic", yfov: number?, aspect: number?, xmag: number?, ymag: number?, znear: number, zfar: number? }
|
|
export type Light = { name: string?, type: "directional" | "point" | "spot", color: { number }, intensity: number, range: number?, innerCone: number?, outerCone: number? }
|
|
export type Skin = { name: string?, joints: { number }, inverseBindMatrices: Accessor?, skeleton: number? }
|
|
export type Channel = { node: number, path: "translation" | "rotation" | "scale" | "weights", input: Accessor, output: Accessor, interpolation: "LINEAR" | "STEP" | "CUBICSPLINE" }
|
|
export type Animation = { name: string?, channels: { Channel } }
|
|
|
|
-- a voxel grid (MagicaVoxel): one byte per cell, 0 = empty, 1..255 = palette index
|
|
export type VoxelMaterial = { type: string, [string]: any } -- "diffuse" | "metal" | "glass" | "emit" | "blend" | "media" | ..., MATL properties (numbers when numeric)
|
|
export type Voxels = {
|
|
kind: "voxels",
|
|
name: string?,
|
|
size: { number }, -- x, y, z cells
|
|
data: buffer, -- size x * y * z bytes, cell (x, y, z) at x + sx * (y + sy * z)
|
|
palette: { { number } }, -- palette[i] = { r, g, b, a } 0..255 (sRGB) for i = 1 .. 255 (shared between a file's models)
|
|
materials: { [number]: VoxelMaterial }?, -- by palette index
|
|
-- block grids (Minecraft schematics / regions): the exact block state of every cell, data / palette being its
|
|
-- colour view (block colours from a default table)
|
|
blocks: VoxelBlocks?,
|
|
-- true-colour grids (Qubicle): the exact 32-bit colour of every cell as r, g, b, a bytes (u32 little-endian,
|
|
-- a = 0 empty), data / palette being an indexed view of it
|
|
colors: buffer?,
|
|
}
|
|
export type VoxelBlocks = {
|
|
palette: { string }, -- block states ("minecraft:oak_stairs[facing=east,half=bottom]"), palette[1] is index 0
|
|
data: buffer, -- u16 little-endian per cell (same cell order as Voxels.data): 0-based index into palette
|
|
blockEntities: { any }?, -- the format's block entity records (NBT compounds; positions relative to the grid)
|
|
entities: { any }?, -- entity records (NBT compounds)
|
|
}
|
|
local Voxels = {}
|
|
function Voxels.new(sx: number, sy: number, sz: number, palette: { { number } }?): Voxels
|
|
local pal = palette
|
|
if not pal then
|
|
local p = table.create(255)
|
|
for i = 1, 255 do
|
|
p[i] = { i, i, i, 255 }
|
|
end
|
|
pal = p
|
|
end
|
|
return { kind = "voxels", size = { sx, sy, sz }, data = buffer.create(sx * sy * sz), palette = pal :: { { number } }, materials = nil }
|
|
end
|
|
function Voxels.get(v: Voxels, x: number, y: number, z: number): number
|
|
return buffer.readu8(v.data, x + v.size[1] * (y + v.size[2] * z))
|
|
end
|
|
function Voxels.set(v: Voxels, x: number, y: number, z: number, index: number)
|
|
buffer.writeu8(v.data, x + v.size[1] * (y + v.size[2] * z), index)
|
|
end
|
|
Types.Voxels = Voxels
|
|
|
|
export type Scene = {
|
|
kind: "scene",
|
|
nodes: { Node },
|
|
roots: { number }, -- top-level node indices
|
|
meshes: { Mesh },
|
|
materials: { Material },
|
|
textures: { Texture },
|
|
cameras: { Camera },
|
|
lights: { Light },
|
|
skins: { Skin },
|
|
animations: { Animation },
|
|
voxels: { Voxels }?, -- voxel grids (VOX), referenced by node.voxels
|
|
meta: { up: "y" | "z"?, unit: number?, source: { format: string, [string]: any }?, [string]: any },
|
|
}
|
|
|
|
local Scene = {}
|
|
function Scene.new(): Scene
|
|
return {
|
|
kind = "scene",
|
|
nodes = {},
|
|
roots = {},
|
|
meshes = {},
|
|
materials = {},
|
|
textures = {},
|
|
cameras = {},
|
|
lights = {},
|
|
skins = {},
|
|
animations = {},
|
|
voxels = nil,
|
|
meta = {},
|
|
}
|
|
end
|
|
local COMPONENT_SIZE: { [string]: number } = {
|
|
f32 = 4,
|
|
f64 = 8,
|
|
u8 = 1,
|
|
u16 = 2,
|
|
u32 = 4,
|
|
i8 = 1,
|
|
i16 = 2,
|
|
i32 = 4,
|
|
unorm8 = 1,
|
|
unorm16 = 2,
|
|
snorm8 = 1,
|
|
snorm16 = 2,
|
|
}
|
|
Types.COMPONENT_SIZE = table.freeze(COMPONENT_SIZE) :: { [string]: number }
|
|
function Scene.accessor(data: buffer?, type: any, components: number, count: number): Accessor
|
|
local d = data or buffer.create(count * components * (COMPONENT_SIZE[type] or 4))
|
|
return { data = d, type = type, components = components, count = count }
|
|
end
|
|
function Scene.material(name: string?): Material
|
|
return {
|
|
name = name,
|
|
baseColor = { 1, 1, 1, 1 },
|
|
metallic = 1,
|
|
roughness = 1,
|
|
emissive = { 0, 0, 0 },
|
|
alphaMode = "opaque",
|
|
alphaCutoff = 0.5,
|
|
doubleSided = false,
|
|
}
|
|
end
|
|
function Scene.fromPrimitive(prim: Primitive, name: string?): Scene
|
|
local s = Scene.new()
|
|
s.meshes[1] = { name = name, primitives = { prim } }
|
|
s.nodes[1] = { name = name, children = {}, mesh = 1 }
|
|
s.roots[1] = 1
|
|
return s
|
|
end
|
|
Types.Scene = Scene
|
|
|
|
function Scene.reader(acc: Accessor): ((i: number, c: number) -> number)?
|
|
local d, n, t = acc.data, acc.components, acc.type
|
|
local size = COMPONENT_SIZE[t]
|
|
if not size or type(d) ~= "buffer" then
|
|
return nil
|
|
end
|
|
local stride = n * size
|
|
local rb = buffer
|
|
if t == "f32" then
|
|
local f = rb.readf32
|
|
return function(i: number, c: number): number
|
|
return f(d, i * stride + c * 4)
|
|
end
|
|
elseif t == "f64" then
|
|
local f = rb.readf64
|
|
return function(i: number, c: number): number
|
|
return f(d, i * stride + c * 8)
|
|
end
|
|
elseif t == "u8" or t == "unorm8" then
|
|
local f, k = rb.readu8, if t == "unorm8" then 1 / 255 else 1
|
|
return function(i: number, c: number): number
|
|
return f(d, i * stride + c) * k
|
|
end
|
|
elseif t == "u16" or t == "unorm16" then
|
|
local f, k = rb.readu16, if t == "unorm16" then 1 / 65535 else 1
|
|
return function(i: number, c: number): number
|
|
return f(d, i * stride + c * 2) * k
|
|
end
|
|
elseif t == "u32" then
|
|
local f = rb.readu32
|
|
return function(i: number, c: number): number
|
|
return f(d, i * stride + c * 4)
|
|
end
|
|
elseif t == "i8" then
|
|
local f = rb.readi8
|
|
return function(i: number, c: number): number
|
|
return f(d, i * stride + c)
|
|
end
|
|
elseif t == "snorm8" then
|
|
local f = rb.readi8
|
|
return function(i: number, c: number): number
|
|
return math.max(f(d, i * stride + c) / 127, -1)
|
|
end
|
|
elseif t == "i16" then
|
|
local f = rb.readi16
|
|
return function(i: number, c: number): number
|
|
return f(d, i * stride + c * 2)
|
|
end
|
|
elseif t == "snorm16" then
|
|
local f = rb.readi16
|
|
return function(i: number, c: number): number
|
|
return math.max(f(d, i * stride + c * 2) / 32767, -1)
|
|
end
|
|
elseif t == "i32" then
|
|
local f = rb.readi32
|
|
return function(i: number, c: number): number
|
|
return f(d, i * stride + c * 4)
|
|
end
|
|
end
|
|
return nil
|
|
end
|
|
|
|
local IDENTITY = table.freeze({ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 })
|
|
Scene.IDENTITY = IDENTITY
|
|
function Scene.mul(a: { number }, b: { number }): { number }
|
|
local m = table.create(16, 0)
|
|
for col = 0, 3 do
|
|
for row = 0, 3 do
|
|
local s = 0
|
|
for k = 0, 3 do
|
|
s += a[k * 4 + row + 1] * b[col * 4 + k + 1]
|
|
end
|
|
m[col * 4 + row + 1] = s
|
|
end
|
|
end
|
|
return m
|
|
end
|
|
function Scene.nodeMatrix(node: Node): { number }
|
|
if node.matrix and #node.matrix == 16 then
|
|
return node.matrix
|
|
end
|
|
local t, r, sc = node.translation, node.rotation, node.scale
|
|
if not t and not r and not sc then
|
|
return IDENTITY
|
|
end
|
|
local tx, ty, tz = 0, 0, 0
|
|
if t then
|
|
tx, ty, tz = t[1] or 0, t[2] or 0, t[3] or 0
|
|
end
|
|
local x, y, z, w = 0, 0, 0, 1
|
|
if r then
|
|
x, y, z, w = r[1] or 0, r[2] or 0, r[3] or 0, r[4] or 1
|
|
end
|
|
local sx, sy, sz = 1, 1, 1
|
|
if sc then
|
|
sx, sy, sz = sc[1] or 1, sc[2] or 1, sc[3] or 1
|
|
end
|
|
local xx, yy, zz, xy, xz, yz, wx, wy, wz = x * x, y * y, z * z, x * y, x * z, y * z, w * x, w * y, w * z
|
|
return {
|
|
(1 - 2 * (yy + zz)) * sx, (2 * (xy + wz)) * sx, (2 * (xz - wy)) * sx, 0,
|
|
(2 * (xy - wz)) * sy, (1 - 2 * (xx + zz)) * sy, (2 * (yz + wx)) * sy, 0,
|
|
(2 * (xz + wy)) * sz, (2 * (yz - wx)) * sz, (1 - 2 * (xx + yy)) * sz, 0,
|
|
tx, ty, tz, 1,
|
|
}
|
|
end
|
|
export type Placed = { primitive: Primitive, matrix: { number }, node: number, mesh: number }
|
|
function Scene.flatten(scene: Scene): { Placed }
|
|
local out: { Placed } = {}
|
|
local nodes = scene.nodes or {}
|
|
local roots = scene.roots
|
|
if not roots or #roots == 0 then
|
|
local child = {}
|
|
for _, n in nodes do
|
|
for _, c in n.children or {} do
|
|
child[c] = true
|
|
end
|
|
end
|
|
roots = {}
|
|
for i in nodes do
|
|
if not child[i] then
|
|
table.insert(roots, i)
|
|
end
|
|
end
|
|
end
|
|
local seen: { [number]: boolean } = {}
|
|
local function visit(i: number, parent: { number })
|
|
local node = nodes[i]
|
|
if not node or seen[i] then
|
|
return
|
|
end
|
|
seen[i] = true
|
|
local m = Scene.nodeMatrix(node)
|
|
local world = if parent == IDENTITY then m elseif m == IDENTITY then parent else Scene.mul(parent, m)
|
|
local mesh = node.mesh and scene.meshes[node.mesh]
|
|
if mesh then
|
|
for _, p in mesh.primitives do
|
|
table.insert(out, { primitive = p, matrix = world, node = i, mesh = node.mesh :: number })
|
|
end
|
|
end
|
|
for _, c in node.children or {} do
|
|
visit(c, world)
|
|
end
|
|
end
|
|
for _, r in roots do
|
|
visit(r, IDENTITY)
|
|
end
|
|
return out
|
|
end
|
|
|
|
-- the STEP entity graph; B-rep bodies and tessellation come from the injected brep plug-in (Rosetta.plugins.brep)
|
|
-- complex (external mapping) instances: type = "COMPLEX", args = {}, parts = { { type, args } } in file order;
|
|
-- section = the DATA section index when a file has more than one (edition 3)
|
|
export type StepEntity = { id: number, type: string, args: { any }, parts: { { type: string, args: { any } } }?, section: number? }
|
|
export type Cad = {
|
|
kind: "cad",
|
|
schema: string, -- "AP203", "AP214", "AP242", ...
|
|
header: { [string]: any },
|
|
entities: { [number]: StepEntity },
|
|
meta: { [string]: any },
|
|
}
|
|
|
|
export type Font = {
|
|
kind: "font",
|
|
info: { family: string?, style: string?, fullName: string?, [string]: any },
|
|
unitsPerEm: number,
|
|
ascent: number,
|
|
descent: number,
|
|
lineGap: number,
|
|
glyphIndex: (self: any, codepoint: number) -> number, -- 0 = .notdef
|
|
advance: (self: any, glyph: number) -> number, -- font units
|
|
kern: (self: any, a: number, b: number) -> number,
|
|
outline: (self: any, glyph: number) -> { { op: string, [number]: number } }?, -- moveTo / lineTo / quadTo / cubicTo
|
|
rasterize: (self: any, glyph: number, px: number) -> Image?, -- alpha8
|
|
pages: { Image }?, -- bitmap fonts (BMFont)
|
|
bitmap: BitmapStrike?, -- bitmap fonts with per-glyph bitmaps (BDF, PCF, PSF, FON): codecs/font/Bitmap
|
|
meta: { [string]: any },
|
|
}
|
|
-- one bitmap glyph: a 1-bit (or grey) image in pixels with y down, placed relative to the pen on the baseline
|
|
export type BitmapGlyph = {
|
|
codepoint: number?, -- Unicode (nil for unencoded glyphs)
|
|
name: string?, -- glyph name (BDF STARTCHAR, PCF glyph names, PostScript names)
|
|
width: number, -- bitmap width / height in pixels
|
|
height: number,
|
|
left: number, -- x of the bitmap's left column relative to the pen
|
|
top: number, -- y of the bitmap's top row above the baseline (BDF: BBXoff + BBh)
|
|
advance: number, -- pen advance in pixels
|
|
pixels: buffer, -- u8 per pixel (width * height, rows top down): 0 = off, 255 = on (grey fonts use 1..254)
|
|
}
|
|
export type BitmapStrike = {
|
|
size: number, -- pixel size (em height in pixels)
|
|
ascent: number, -- pixels above the baseline
|
|
descent: number, -- pixels below the baseline (positive)
|
|
glyphs: { BitmapGlyph }, -- glyph index i + 1 is font glyph i (glyph 0 = the default / .notdef glyph)
|
|
default: number?, -- glyph index (0-based) used for unmapped code points
|
|
map: { [number]: number }?, -- extra code point -> glyph index (0-based) mappings (several code points on one glyph)
|
|
}
|
|
|
|
export type ArchiveEntry = { name: string, size: number, compressedSize: number?, method: string?, mtime: number?, dir: boolean }
|
|
export type Archive = {
|
|
kind: "archive",
|
|
format: string,
|
|
list: (self: any) -> { ArchiveEntry },
|
|
read: (self: any, name: string) -> (buffer?, Err?),
|
|
add: (self: any, name: string, data: buffer | string, opts: any?) -> (),
|
|
write: (self: any, opts: any?) -> (buffer?, Err?),
|
|
meta: { [string]: any },
|
|
}
|
|
|
|
-- data formats decode to plain Luau values; JSON null / YAML ~ / CBOR null is this sentinel (nil cannot sit in a
|
|
-- table). XML decodes to XmlNode trees. Byte strings (msgpack bin, CBOR bytes, plist data) are buffers.
|
|
local nullMeta = { __tostring = function()
|
|
return "null"
|
|
end }
|
|
Types.null = table.freeze(setmetatable({}, nullMeta))
|
|
|
|
-- Objects (maps) whose key order matters, and empty objects (which would otherwise read back as empty arrays), carry
|
|
-- an "object" metatable with their key order. Decoders mark empty objects always and every object when asked
|
|
-- (opts.ordered); encoders write an object's keys in that order (keys added later, or plain tables: sorted).
|
|
-- The key order is the __keys field of the object's own metatable, or (for a table that came with a metatable of its
|
|
-- own) an entry in a weak map.
|
|
local objectMeta: { [any]: { any } } = setmetatable({}, { __mode = "k" }) :: any -- object -> its key order
|
|
local function orderOf(v: any): { any }?
|
|
local mt: any = getmetatable(v)
|
|
if type(mt) == "table" then
|
|
local k: any = rawget(mt, "__keys")
|
|
if k ~= nil then
|
|
return k
|
|
end
|
|
end
|
|
return objectMeta[v]
|
|
end
|
|
local NOKEYS: { any } = table.freeze({}) :: any
|
|
function Types.object(t: { [any]: any }?, keys: { any }?): { [any]: any }
|
|
local o: { [any]: any } = t or {}
|
|
local k: { any } = keys or NOKEYS
|
|
local mt: any = getmetatable(o)
|
|
if mt == nil or (type(mt) == "table" and rawget(mt, "__keys") ~= nil) then
|
|
setmetatable(o, { __object = true, __keys = k })
|
|
else
|
|
objectMeta[o] = k
|
|
end
|
|
return o
|
|
end
|
|
function Types.isObject(v: any): boolean
|
|
return type(v) == "table" and orderOf(v) ~= nil
|
|
end
|
|
function Types.set(o: { [any]: any }, k: any, v: any)
|
|
local order = orderOf(o)
|
|
if order and o[k] == nil and v ~= nil then
|
|
if order == NOKEYS then
|
|
local mt: any = getmetatable(o)
|
|
if type(mt) == "table" and rawget(mt, "__keys") == NOKEYS then
|
|
mt.__keys = { k }
|
|
else
|
|
objectMeta[o] = { k }
|
|
end
|
|
else
|
|
table.insert(order, k)
|
|
end
|
|
end
|
|
o[k] = v
|
|
end
|
|
local function keyLess(a: any, b: any): boolean
|
|
local ta, tb = type(a), type(b)
|
|
if ta ~= tb then
|
|
return ta < tb
|
|
end
|
|
if ta == "number" or ta == "string" then
|
|
return a < b
|
|
end
|
|
return tostring(a) < tostring(b)
|
|
end
|
|
Types.keyLess = keyLess
|
|
|
|
function Types.isArray(t: { [any]: any }, emptyIsMap: boolean?): boolean
|
|
if Types.isObject(t) then
|
|
return false
|
|
end
|
|
local n = #t
|
|
if n == 0 then
|
|
return next(t) == nil and not emptyIsMap
|
|
end
|
|
if next(t, n) ~= nil then
|
|
return false
|
|
end
|
|
local c = 0
|
|
for _ in next, t do
|
|
c += 1
|
|
end
|
|
return c == n
|
|
end
|
|
function Types.keys(t: { [any]: any }): { any }
|
|
local order = orderOf(t)
|
|
local out, seen = {}, {}
|
|
local n = 0
|
|
if order then
|
|
for _, k in order do
|
|
if t[k] ~= nil and not seen[k] then
|
|
seen[k] = true
|
|
n += 1
|
|
out[n] = k
|
|
end
|
|
end
|
|
end
|
|
local rest: { any }? = nil
|
|
for k in next, t do
|
|
if not seen[k] then
|
|
if rest then
|
|
table.insert(rest, k)
|
|
else
|
|
rest = { k }
|
|
end
|
|
end
|
|
end
|
|
if rest then
|
|
table.sort(rest, keyLess)
|
|
table.move(rest, 1, #rest, n + 1, out)
|
|
end
|
|
return out
|
|
end
|
|
|
|
-- Dates and times (TOML datetimes, plist <date>, CBOR tags 0 / 1, msgpack timestamps). Fields present depend on the
|
|
-- kind: "datetime" (with offset: an instant), "datetime-local", "date-local", "time-local". offset is in minutes
|
|
-- east of UTC (nil = local time). tostring gives RFC 3339 ("1979-05-27T07:32:00.5-07:00", "07:32:00", ...).
|
|
export type Date = {
|
|
kind: "datetime" | "datetime-local" | "date-local" | "time-local",
|
|
year: number?,
|
|
month: number?,
|
|
day: number?,
|
|
hour: number?,
|
|
minute: number?,
|
|
second: number?,
|
|
nanosecond: number?, -- 0 .. 999999999
|
|
offset: number?, -- minutes east of UTC
|
|
}
|
|
local DateMeta = {}
|
|
local function p2(n: number): string
|
|
return string.format("%02d", n)
|
|
end
|
|
local function fmtDate(d: any): string
|
|
local s: string = ""
|
|
if d.year then
|
|
s = string.format("%04d-%02d-%02d", d.year, d.month or 1, d.day or 1)
|
|
end
|
|
if d.hour then
|
|
if s ~= "" then
|
|
s = s .. "T"
|
|
end
|
|
s ..= p2(d.hour) .. ":" .. p2(d.minute or 0) .. ":" .. p2(d.second or 0)
|
|
local ns = d.nanosecond or 0
|
|
if ns > 0 then
|
|
local f = string.format("%09d", ns)
|
|
s ..= "." .. (string.gsub(f, "0+$", ""))
|
|
end
|
|
if d.offset then
|
|
if d.offset == 0 then
|
|
s ..= "Z"
|
|
else
|
|
local o = math.abs(d.offset)
|
|
s ..= (if d.offset < 0 then "-" else "+") .. p2(o // 60) .. ":" .. p2(o % 60)
|
|
end
|
|
end
|
|
end
|
|
return s
|
|
end
|
|
DateMeta.__tostring = fmtDate
|
|
DateMeta.__eq = function(a: any, b: any): boolean
|
|
return fmtDate(a) == fmtDate(b) and a.kind == b.kind
|
|
end
|
|
local DateLib = {}
|
|
-- days since 1970-01-01 of a civil date (proleptic Gregorian; Howard Hinnant's algorithm)
|
|
local function daysFromCivil(y: number, m: number, d: number): number
|
|
y = if m <= 2 then y - 1 else y
|
|
local era = (if y >= 0 then y else y - 399) // 400
|
|
local yoe = y - era * 400
|
|
local mp = (m + 9) % 12
|
|
local doy = (153 * mp + 2) // 5 + d - 1
|
|
local doe = yoe * 365 + yoe // 4 - yoe // 100 + doy
|
|
return era * 146097 + doe - 719468
|
|
end
|
|
local function civilFromDays(z: number): (number, number, number)
|
|
z += 719468
|
|
local era = (if z >= 0 then z else z - 146096) // 146097
|
|
local doe = z - era * 146097
|
|
local yoe = (doe - doe // 1460 + doe // 36524 - doe // 146096) // 365
|
|
local y = yoe + era * 400
|
|
local doy = doe - (365 * yoe + yoe // 4 - yoe // 100)
|
|
local mp = (5 * doy + 2) // 153
|
|
local d = doy - (153 * mp + 2) // 5 + 1
|
|
local m = if mp < 10 then mp + 3 else mp - 9
|
|
return (if m <= 2 then y + 1 else y), m, d
|
|
end
|
|
DateLib.civilFromDays = civilFromDays -- days since 1970-01-01 -> year, month, day (proleptic Gregorian)
|
|
DateLib.daysFromCivil = daysFromCivil -- year, month, day -> days since 1970-01-01
|
|
local MDAYS = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
|
|
function DateLib.new(f: { [string]: any }): Date
|
|
local d: any = {}
|
|
for k, v in f do
|
|
d[k] = v
|
|
end
|
|
if d.kind == nil then
|
|
d.kind = if d.year and d.hour then (if d.offset then "datetime" else "datetime-local")
|
|
elseif d.year then "date-local"
|
|
else "time-local"
|
|
end
|
|
return setmetatable(d, DateMeta) :: any
|
|
end
|
|
function DateLib.is(v: any): boolean
|
|
return type(v) == "table" and getmetatable(v) == DateMeta
|
|
end
|
|
function DateLib.valid(d: Date): boolean
|
|
if d.year then
|
|
local y, m, dd = d.year, d.month or 0, d.day or 0
|
|
if y < 0 or y > 9999 or m < 1 or m > 12 or dd < 1 or dd > MDAYS[m] then
|
|
return false
|
|
end
|
|
if m == 2 and dd == 29 and not (y % 4 == 0 and (y % 100 ~= 0 or y % 400 == 0)) then
|
|
return false
|
|
end
|
|
end
|
|
if d.hour then
|
|
if d.hour > 23 or (d.minute or 0) > 59 or (d.second or 0) > 60 then
|
|
return false
|
|
end
|
|
end
|
|
if d.offset and math.abs(d.offset) >= 24 * 60 then
|
|
return false
|
|
end
|
|
return true
|
|
end
|
|
-- parse: RFC 3339 / ISO 8601 date-times and the TOML partial forms; lower-case t / z accepted
|
|
function DateLib.parse(s: string): Date?
|
|
local d: any = {}
|
|
local rest: string = s
|
|
local y, mo, dd, r1 = string.match(rest, "^(%d%d%d%d)%-(%d%d)%-(%d%d)(.*)$")
|
|
if y then
|
|
d.year, d.month, d.day = tonumber(y), tonumber(mo), tonumber(dd)
|
|
rest = r1 :: string
|
|
if rest == "" then
|
|
return if DateLib.valid(d) then DateLib.new(d) else nil
|
|
end
|
|
local sep = string.sub(rest, 1, 1)
|
|
if sep ~= "T" and sep ~= "t" and sep ~= " " then
|
|
return nil
|
|
end
|
|
rest = string.sub(rest, 2)
|
|
end
|
|
local h, mi, r2 = string.match(rest, "^(%d%d):(%d%d)(.*)$")
|
|
if not h then
|
|
return nil
|
|
end
|
|
d.hour, d.minute = tonumber(h), tonumber(mi)
|
|
rest = r2 :: string
|
|
local sec, r3 = string.match(rest, "^:(%d%d)(.*)$")
|
|
if not sec then
|
|
return nil
|
|
end
|
|
d.second = tonumber(sec)
|
|
rest = r3 :: string
|
|
local frac, r4 = string.match(rest, "^%.(%d+)(.*)$")
|
|
if frac then
|
|
d.nanosecond = tonumber(string.sub(frac .. "000000000", 1, 9))
|
|
rest = r4 :: string
|
|
else
|
|
d.nanosecond = 0
|
|
end
|
|
if rest ~= "" then
|
|
if not d.year then
|
|
return nil
|
|
end
|
|
if rest == "Z" or rest == "z" then
|
|
d.offset = 0
|
|
else
|
|
local sign, oh, om = string.match(rest, "^([%+%-])(%d%d):(%d%d)$")
|
|
if not sign then
|
|
return nil
|
|
end
|
|
local oh_n, om_n = tonumber(oh) :: number, tonumber(om) :: number
|
|
if oh_n > 23 or om_n > 59 then
|
|
return nil
|
|
end
|
|
d.offset = (oh_n * 60 + om_n) * (if sign == "-" then -1 else 1)
|
|
end
|
|
end
|
|
return if DateLib.valid(d) then DateLib.new(d) else nil
|
|
end
|
|
function DateLib.toUnix(d: Date): number
|
|
local days = if d.year then daysFromCivil(d.year, d.month or 1, d.day or 1) else 0
|
|
local s = days * 86400 + (d.hour or 0) * 3600 + (d.minute or 0) * 60 + (d.second or 0) - (d.offset or 0) * 60
|
|
return s + (d.nanosecond or 0) / 1e9
|
|
end
|
|
function DateLib.fromUnix(t: number, offset: number?): Date
|
|
local off = offset or 0
|
|
local whole = math.floor(t)
|
|
local ns = math.floor((t - whole) * 1e9 + 0.5)
|
|
if ns >= 1e9 then
|
|
whole += 1
|
|
ns -= 1e9
|
|
end
|
|
local lt = whole + off * 60
|
|
local days = lt // 86400
|
|
local secs = lt - days * 86400
|
|
local y, m, dd = civilFromDays(days)
|
|
return DateLib.new({
|
|
kind = "datetime",
|
|
year = y,
|
|
month = m,
|
|
day = dd,
|
|
hour = secs // 3600,
|
|
minute = (secs // 60) % 60,
|
|
second = secs % 60,
|
|
nanosecond = ns,
|
|
offset = off,
|
|
})
|
|
end
|
|
Types.Date = table.freeze(DateLib)
|
|
|
|
-- XML: an element, or (name starting with "#") a "#document" / "#comment" / "#cdata" / "#pi" / "#doctype" node with
|
|
-- its content in text. attrs is a Types.object (attribute order kept). Text children are plain strings.
|
|
export type XmlNode = {
|
|
name: string,
|
|
attrs: { [string]: string },
|
|
children: { XmlNode | string },
|
|
parent: XmlNode?,
|
|
text: string?, -- "#comment" / "#cdata" / "#pi" (the data after the target) / "#doctype" content
|
|
target: string?, -- "#pi": the target
|
|
ns: string?, -- the namespace URI (opts.namespaces)
|
|
[string]: any,
|
|
}
|
|
|
|
export type Paint = { type: "color", color: { number } } | { type: "linear" | "radial", stops: { { number } }, [string]: any }
|
|
export type VectorPath = {
|
|
d: { { op: string, [number]: number } }, -- absolute M / L / C / Q / Z commands
|
|
fill: Paint?,
|
|
fillRule: "nonzero" | "evenodd",
|
|
stroke: Paint?,
|
|
strokeWidth: number,
|
|
transform: { number }?, -- a, b, c, d, e, f
|
|
opacity: number,
|
|
group: number?, -- index into meta.groups (svg <g> nesting: opacity / clip-path per group)
|
|
clip: number?, -- index into meta.clips (a clip-path)
|
|
id: string?,
|
|
strokeDashArray: { number }?,
|
|
strokeDashOffset: number?,
|
|
strokeLineJoin: ("miter" | "round" | "bevel")?, -- default "miter"
|
|
strokeLineCap: ("butt" | "round" | "square")?, -- default "butt"
|
|
strokeMiterLimit: number?,
|
|
}
|
|
export type Vector = {
|
|
kind: "vector",
|
|
width: number,
|
|
height: number,
|
|
viewBox: { number }, -- x, y, w, h
|
|
paths: { VectorPath },
|
|
meta: { [string]: any },
|
|
}
|
|
local Vector = {}
|
|
function Vector.new(width: number, height: number): Vector
|
|
return { kind = "vector", width = width, height = height, viewBox = { 0, 0, width, height }, paths = {}, meta = {} }
|
|
end
|
|
Types.Vector = Vector
|
|
|
|
-- blend: a codecs/image/Blend mode name ("normal", "multiply", ...; groups also "passThrough"). Optional: children (a group:
|
|
-- its layers bottom to top; its image is 0 x 0), clip (clipped to the alpha of the layer below), mask (a grey mask in
|
|
-- canvas coordinates; outside its rectangle the mask is `default`).
|
|
export type LayerMask = { image: Image, x: number, y: number, default: number, disabled: boolean? }
|
|
export type Layer = { name: string, image: Image, x: number, y: number, opacity: number, blend: string, visible: boolean, children: { Layer }?, clip: boolean?, mask: LayerMask? }
|
|
export type Layered = {
|
|
kind: "layered",
|
|
width: number,
|
|
height: number,
|
|
layers: { Layer }, -- bottom to top (a tree: groups have children); animations: frame 1's layers
|
|
frames: { { layers: { Layer }, delay: number } }?, -- animations (Aseprite): every frame's layers, delay in seconds
|
|
meta: { [string]: any },
|
|
}
|
|
local Layered = {}
|
|
function Layered.new(width: number, height: number): Layered
|
|
return { kind = "layered", width = width, height = height, layers = {}, frames = nil, meta = {} }
|
|
end
|
|
function Layered.layer(name: string, image: Image, x: number?, y: number?): Layer
|
|
return { name = name, image = image, x = x or 0, y = y or 0, opacity = 1, blend = "normal", visible = true }
|
|
end
|
|
function Layered.group(name: string, children: { Layer }): Layer
|
|
return { name = name, image = Image.new(0, 0), x = 0, y = 0, opacity = 1, blend = "passThrough", visible = true, children = children }
|
|
end
|
|
Types.Layered = Layered
|
|
|
|
-- gids are Tiled's: 0 = empty, the tile's firstGid-relative index, flip / rotation flags in the top bits (kept).
|
|
-- Tilesets keep their image as a reference (imageSource: the path as written; image: an Image only if a caller
|
|
-- loads it). Extra Tiled fields sit next to these (offsetX / offsetY, parallax, tintColor, class, chunks of infinite
|
|
-- maps, objects' shapes, per-tile data, ...: see vector/tiled).
|
|
export type Tileset = { name: string, firstGid: number, tileWidth: number, tileHeight: number, columns: number, count: number, image: Image?, source: string?, [string]: any }
|
|
export type TileLayer = { name: string, type: "tiles" | "objects" | "image" | "group", width: number?, height: number?, data: { number }?, objects: { any }?, visible: boolean, opacity: number, properties: { [string]: any }, [string]: any }
|
|
export type TileMap = {
|
|
kind: "tilemap",
|
|
width: number,
|
|
height: number,
|
|
tileWidth: number,
|
|
tileHeight: number,
|
|
orientation: string,
|
|
tilesets: { Tileset },
|
|
layers: { TileLayer },
|
|
properties: { [string]: any },
|
|
meta: { [string]: any },
|
|
[string]: any,
|
|
}
|
|
local TileMap = {}
|
|
function TileMap.new(width: number, height: number, tileWidth: number, tileHeight: number): TileMap
|
|
return { kind = "tilemap", width = width, height = height, tileWidth = tileWidth, tileHeight = tileHeight, orientation = "orthogonal", tilesets = {}, layers = {}, properties = {}, meta = {} }
|
|
end
|
|
Types.TileMap = TileMap
|
|
|
|
export type Cue = { start: number, stop: number, text: string, style: string?, id: string?, [string]: any } -- seconds
|
|
export type Subtitles = { kind: "subtitles", cues: { Cue }, styles: { [string]: any }, meta: { [string]: any } }
|
|
local Subtitles = {}
|
|
function Subtitles.new(): Subtitles
|
|
return { kind = "subtitles", cues = {}, styles = {}, meta = {} }
|
|
end
|
|
Types.Subtitles = Subtitles
|
|
|
|
-- the playlist-luau shape
|
|
export type PlaylistEntry = {
|
|
url: string,
|
|
title: string?,
|
|
duration: number?,
|
|
playable: boolean,
|
|
reason: string?,
|
|
bandwidth: number?,
|
|
codecs: string?,
|
|
attributes: { [string]: string }?,
|
|
byteRange: { length: number, offset: number? }?,
|
|
}
|
|
export type Playlist = {
|
|
kind: "playlist",
|
|
format: string, -- "m3u", "pls", "hls", "xspf"
|
|
entries: { PlaylistEntry },
|
|
warnings: { string },
|
|
title: string?,
|
|
hls: { [string]: any }?,
|
|
meta: { [string]: any },
|
|
}
|
|
|
|
-- what every format folder's init.luau returns (checked by tools/lint.sh CONTRACT; see docs/STYLE.md)
|
|
export type Caps = { decode: boolean, encode: boolean, [string]: any }
|
|
export type Format = {
|
|
name: string,
|
|
category: string,
|
|
extensions: { string },
|
|
mime: { string },
|
|
deps: { string },
|
|
sniff: (r: any) -> number, -- confidence 0..1 from the first bytes (a core/Bytes Reader); must not consume / throw
|
|
decode: ((r: any, opts: any?) -> (any, Err?))?,
|
|
encode: ((value: any, opts: any?) -> (buffer?, Err?))?,
|
|
open: ((r: any, opts: any?) -> (any, Err?))?, -- streaming categories (audio / video)
|
|
caps: Caps,
|
|
}
|
|
|
|
-- the categories, in the order F.formats() lists them; kind = the `kind` of the values they decode to
|
|
Types.CATEGORIES = table.freeze({
|
|
image = "image",
|
|
audio = "audio",
|
|
video = "video",
|
|
model = "scene",
|
|
cad = "cad",
|
|
font = "font",
|
|
archive = "archive",
|
|
data = "data",
|
|
vector = "vector",
|
|
layered = "layered",
|
|
subtitle = "subtitles",
|
|
music = "music",
|
|
playlist = "playlist",
|
|
} :: { [string]: string })
|
|
|
|
return Types
|