formats-luau foundation: build (darklua + token passes) for Roblox and LuaJIT, compat prelude, core, Types, host adapters, API + registry, lint, tests on both runtimes; absorb deflate (codecs/Deflate + new compressor) and png (image/png + new encoder)

This commit is contained in:
4DBug committed 2026-10-31 08:18:01 -05:00
commit cdcb5b0b77
226 files changed
+8409

No files matched your search

+149
View File
@@ -0,0 +1,149 @@
--!strict
-- host/Love: LÖVE 11+. Files through love.filesystem (the game folder and the save directory), falling back to io for
-- paths outside them; converts the standard types to LÖVE objects:
-- host.toImageData(img) -> love ImageData (rgba8)
-- host.fromImageData(imageData) -> Image (rgba8)
-- host.toSoundData(audio) -> love SoundData (an Audio, or a Decoder decoded to the end; 16-bit)
local Err = require("@formats/core/Err")
local Types = require("@formats/Types")
local Host = {}
Host.name = "love"
Host.hasFiles = true
local function love(): any
return (_G :: any).love
end
local function io(): any
return (_G :: any).io
end
-- the FFI, when the build runs on LuaJIT (always, under LÖVE); called through _G so the build leaves it alone
local function ffi(): any
local req = (_G :: any).require
local ok, m = pcall(req, "ffi")
return if ok then m else nil
end
function Host.readFile(path: string): (buffer?, Err.Err?)
local fs = love().filesystem
if fs.getInfo(path, "file") then
local s, msg = fs.read(path)
if type(s) ~= "string" then
return nil, Err.new(Err.IO, "cannot read " .. path .. ": " .. tostring(msg))
end
return buffer.fromstring(s)
end
local f, msg = io().open(path, "rb")
if not f then
return nil, Err.new(Err.IO, "cannot open " .. path .. ": " .. tostring(msg))
end
local s = f:read("*a")
f:close()
return buffer.fromstring(s)
end
-- writeFile: relative paths go to the save directory (love.filesystem.write), absolute ones through io
function Host.writeFile(path: string, data: buffer): (boolean, Err.Err?)
local s = buffer.tostring(data)
if string.sub(path, 1, 1) ~= "/" and not string.find(path, "^%a:[/\\]") then
local ok, msg = love().filesystem.write(path, s)
if not ok then
return false, Err.new(Err.IO, "cannot write " .. path .. ": " .. tostring(msg))
end
return true
end
local f, msg = io().open(path, "wb")
if not f then
return false, Err.new(Err.IO, "cannot create " .. path .. ": " .. tostring(msg))
end
f:write(s)
f:close()
return true
end
function Host.exists(path: string): boolean
if love().filesystem.getInfo(path, "file") then
return true
end
local f = io().open(path, "rb")
if f then
f:close()
return true
end
return false
end
-- toImageData(img) -> ImageData | nil, Err
function Host.toImageData(img: any): (any, Err.Err?)
if img.format ~= "rgba8" then
return nil, Err.new(Err.ARG, "toImageData: needs rgba8 pixels, got " .. tostring(img.format))
end
local n = img.width * img.height * 4
local F = ffi()
local B = buffer :: any
if F and B.toffi then
local id = love().image.newImageData(img.width, img.height, "rgba8")
F.copy(id:getFFIPointer(), B.toffi(img.pixels), n)
return id
end
local bytes = love().data.newByteData(buffer.readstring(img.pixels, 0, n))
return love().image.newImageData(img.width, img.height, "rgba8", bytes)
end
-- fromImageData(imageData) -> Image (rgba8 copy)
function Host.fromImageData(id: any): Types.Image
local w, h = id:getDimensions()
local img = Types.Image.new(w, h, "rgba8")
local F = ffi()
local B = buffer :: any
if F and B.toffi and id:getFormat() == "rgba8" then
F.copy(B.toffi(img.pixels), id:getFFIPointer(), w * h * 4)
return img
end
local s = id:getString()
buffer.writestring(img.pixels, 0, s, math.min(#s, w * h * 4))
return img
end
-- toSoundData(audio) -> SoundData (16-bit). audio: an Audio, or a Decoder (decoded to the end).
function Host.toSoundData(a: any): (any, Err.Err?)
local samples, frames, channels, rate = a.samples, a.frames, a.channels, a.sampleRate
if samples == nil then
-- a Decoder: read everything
local parts, total = {}, 0
while true do
local out, n = a:read(65536)
if 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
samples = buffer.create(total * channels * 4)
local o = 0
for _, p in parts do
buffer.copy(samples, o, p)
o += buffer.len(p)
end
frames = total
end
local sd = love().sound.newSoundData(math.max(frames, 1), rate, 16, channels)
local count = frames * channels
local F = ffi()
if F then
local p = F.cast("int16_t*", sd:getFFIPointer())
for i = 0, count - 1 do
local v = math.clamp(buffer.readf32(samples, i * 4), -1, 1)
p[i] = math.floor(v * 32767 + 0.5)
end
else
for i = 0, count - 1 do
sd:setSample(i, math.clamp(buffer.readf32(samples, i * 4), -1, 1))
end
end
return sd
end
return Host
+48
View File
@@ -0,0 +1,48 @@
--!strict
-- host/LuaJIT: plain LuaJIT (or any Lua with io): files through io.open.
local Err = require("@formats/core/Err")
local Host = {}
Host.name = "luajit"
Host.hasFiles = true
local function io(): any
return (_G :: any).io
end
function Host.readFile(path: string): (buffer?, Err.Err?)
local f, msg = io().open(path, "rb")
if not f then
return nil, Err.new(Err.IO, "cannot open " .. path .. ": " .. tostring(msg))
end
local s = f:read("*a")
f:close()
if type(s) ~= "string" then
return nil, Err.new(Err.IO, "cannot read " .. path)
end
return buffer.fromstring(s), nil
end
function Host.writeFile(path: string, data: buffer): (boolean, Err.Err?)
local f, msg = io().open(path, "wb")
if not f then
return false, Err.new(Err.IO, "cannot create " .. path .. ": " .. tostring(msg))
end
local ok, werr = f:write(buffer.tostring(data))
f:close()
if not ok then
return false, Err.new(Err.IO, "cannot write " .. path .. ": " .. tostring(werr))
end
return true, nil
end
function Host.exists(path: string): boolean
local f = io().open(path, "rb")
if f then
f:close()
return true
end
return false
end
return Host
+21
View File
@@ -0,0 +1,21 @@
--!strict
-- host/Luau: the Luau CLI (and any embedding without io): no file system. Sources are buffers or strings.
local Err = require("@formats/core/Err")
local Host = {}
Host.name = "luau"
Host.hasFiles = false
function Host.readFile(path: string): (buffer?, Err.Err?)
return nil, Err.new(Err.IO, "no file system on this host (luau): pass a buffer or a string, not the path " .. path)
end
function Host.writeFile(path: string, _data: buffer): (boolean, Err.Err?)
return false, Err.new(Err.IO, "no file system on this host (luau): cannot write " .. path)
end
function Host.exists(_path: string): boolean
return false
end
return Host
+40
View File
@@ -0,0 +1,40 @@
--!strict
-- host/Roblox: no file system (sources are buffers or strings, e.g. from HttpService or an asset); converts the
-- standard types to engine objects:
-- host.toEditableImage(img) -> EditableImage (rgba8; other formats are converted by the caller first)
local Err = require("@formats/core/Err")
local Host = {}
Host.name = "roblox"
Host.hasFiles = false
function Host.readFile(path: string): (buffer?, Err.Err?)
return nil, Err.new(Err.IO, "Roblox has no file system: pass a buffer or a string, not the path " .. path)
end
function Host.writeFile(path: string, _data: buffer): (boolean, Err.Err?)
return false, Err.new(Err.IO, "Roblox has no file system: cannot write " .. path .. " (use F.save(value) for a buffer)")
end
function Host.exists(_path: string): boolean
return false
end
-- toEditableImage(img) -> EditableImage | nil, Err. img.format must be "rgba8".
function Host.toEditableImage(img: any): (any, Err.Err?)
if img.format ~= "rgba8" then
return nil, Err.new(Err.ARG, "toEditableImage: needs rgba8 pixels, got " .. tostring(img.format))
end
local AssetService = (game :: any):GetService("AssetService")
local size = Vector2.new(img.width, img.height)
local ok, ei = pcall(function()
return AssetService:CreateEditableImage({ Size = size })
end)
if not ok or not ei then
return nil, Err.new(Err.IO, "CreateEditableImage failed: " .. tostring(ei))
end
ei:WritePixelsBuffer(Vector2.zero, size, img.pixels)
return ei
end
return Host
+43
View File
@@ -0,0 +1,43 @@
--!strict
-- host: the runtime adapter, detected once at load time. Roblox (no file system; toEditableImage), LÖVE
-- (love.filesystem + io; toImageData / toSoundData), plain LuaJIT (io), the Luau CLI (no file system).
--
-- local host = require("@formats/host")
-- print(host.name) -- "roblox" | "love" | "luajit" | "luau"
-- local buf, err = host.readFile("a.png") -- every host has readFile / writeFile / exists (failing with Err.IO
-- -- where there is no file system)
local Err = require("@formats/core/Err")
export type Host = {
name: string,
hasFiles: boolean,
readFile: (path: string) -> (buffer?, Err.Err?),
writeFile: (path: string, data: buffer) -> (boolean, Err.Err?),
exists: (path: string) -> boolean,
[string]: any,
}
local G = _G :: any
local name
if typeof(game) == "Instance" then
name = "roblox"
elseif G.love ~= nil and type(G.love) == "table" and G.love.filesystem ~= nil then
name = "love"
elseif G.jit ~= nil or G.io ~= nil then
name = "luajit"
else
name = "luau"
end
local host: Host
if name == "roblox" then
host = require("@formats/host/Roblox")
elseif name == "love" then
host = require("@formats/host/Love")
elseif name == "luajit" then
host = require("@formats/host/LuaJIT")
else
host = require("@formats/host/Luau")
end
local _ = Err
return host