ws-game: archive/pak (Quake PACK / SiN SPAK read + write); containers/Archive: slice() lazy loader and collect() for simple archive writers (additive)

This commit is contained in:
4DBug committed 2026-10-31 13:52:17 -05:00
1 parent de4f14a343
commit d41d64219a
6 files changed
+329

No files matched your search

+12
View File
@@ -590,6 +590,18 @@ local list: { Entry } = {
return require("@formats/archive/gzip")
end,
},
{
name = "pak",
category = "archive",
path = "archive/pak",
extensions = { "pak", "sin" },
mime = { "application/x-pak" },
deps = { "core/Err", "core/Bytes", "core/Sniff", "containers/Archive" },
caps = { decode = true, encode = true, variants = { "quake", "sin" } },
load = function()
return require("@formats/archive/pak")
end,
},
{
name = "tar",
category = "archive",
+135
View File
@@ -0,0 +1,135 @@
--!strict
-- archive/pak: Quake / Quake II / Half-Life PAK archives ("PACK": 56-byte names, 64-byte directory entries) and the
-- SiN variant ("SPAK": 120-byte names), read into the standard Archive (entries read lazily: a copy of the stored
-- bytes) and written from any Archive value. Written from the format description in the Quake source release
-- (common.c: dpackheader_t / dpackfile_t) and the Quake specs; no code taken.
-- Not supported: Daikatana's compressed PAK entries.
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@formats/core/Err")
local Bytes = require("@formats/core/Bytes")
local Sniff = require("@formats/core/Sniff")
local Archive = require("@formats/containers/Archive")
-- ------------------------------------------------------------------------------------------------ contract
local Pak = {
name = "pak",
category = "archive",
extensions = { "pak", "sin" },
mime = { "application/x-pak" },
deps = { "core/Err", "core/Bytes", "core/Sniff", "containers/Archive" },
caps = { decode = true, encode = true, variants = { "quake", "sin" } },
}
-- ------------------------------------------------------------------------------------------------ types
export type DecodeOptions = { maxEntries: number? } -- default 1e6
export type EncodeOptions = { variant: ("quake" | "sin")? } -- default: the source variant, else "quake"
-- ------------------------------------------------------------------------------------------------ constants
local VARIANTS = { quake = { magic = "PACK", nameLen = 56 }, sin = { magic = "SPAK", nameLen = 120 } }
-- ------------------------------------------------------------------------------------------------ sniff
function Pak.sniff(r: Bytes.Reader): number
if r:remaining() < 12 then
return 0
end
return math.max(Sniff.magic(r, "PACK", 0, 0.9), Sniff.magic(r, "SPAK", 0, 0.9))
end
-- ------------------------------------------------------------------------------------------------ decode
local function encode(archive: any, opts: any?): (buffer?, Err.Err?)
return Pak.encode(archive, opts)
end
function Pak.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
local magic = r:string(4)
local dirOfs, dirLen = r:u32le(), r:u32le()
if r.err then
return nil, r.err
end
local variant = if magic == "PACK" then "quake" elseif magic == "SPAK" then "sin" else nil
if not variant then
return nil, Err.new(Err.CORRUPT, "bad magic", 0)
end
local nameLen = VARIANTS[variant].nameLen
local entrySize = nameLen + 8
local size = r.limit - r.base
if dirOfs + dirLen > size then
return nil, Err.new(Err.TRUNCATED, "directory past the end", 4)
end
if dirLen % entrySize ~= 0 then
return nil, Err.new(Err.CORRUPT, "directory size is not a multiple of " .. entrySize, 8)
end
local count = dirLen // entrySize
if count > (opts and opts.maxEntries or 1e6) then
return nil, Err.new(Err.LIMIT, "too many entries: " .. count)
end
local a = Archive.new("pak", encode)
local d = r:sub(dirOfs, dirLen)
for i = 0, count - 1 do
local name = d:fixed(nameLen)
local at, n = d:u32le(), d:u32le()
if d.err then
return nil, d.err
end
if at + n > size then
return nil, Err.new(Err.TRUNCATED, "entry " .. name .. " past the end", dirOfs + i * entrySize)
end
Archive.put(a, { name = name, size = n, dir = false, method = "store", offset = at, load = Archive.slice(r.buf, r.base + at, n) })
end
a.meta.source = { format = "pak", variant = variant, entries = count }
return a, nil
end
-- ------------------------------------------------------------------------------------------------ encode
function Pak.encode(archive: any, opts: EncodeOptions?): (buffer?, Err.Err?)
local items, err = Archive.collect(archive)
if not items then
return nil, err
end
local src = type(archive) == "table" and type(archive.meta) == "table" and archive.meta.source
local variant = opts and opts.variant or (if type(src) == "table" and src.format == "pak" then src.variant else nil) or "quake"
local v = VARIANTS[variant]
if not v then
return nil, Err.new(Err.ARG, "unknown variant " .. tostring(variant))
end
local files: { { name: string, data: buffer } } = {}
for _, it in items do
if not it.entry.dir then
local name = string.gsub(it.entry.name, "\\", "/")
if #name >= v.nameLen then
return nil, Err.new(Err.ARG, "name longer than " .. (v.nameLen - 1) .. " bytes: " .. name)
end
table.insert(files, { name = name, data = it.data })
end
end
local w = Bytes.writer()
w:string(v.magic)
w:u32le(0)
w:u32le(0)
local offsets: { number } = {}
for i, f in files do
offsets[i] = w:tell()
w:bytes(f.data)
end
local dirOfs = w:tell()
for i, f in files do
w:string(f.name)
w:fill(0, v.nameLen - #f.name)
w:u32le(offsets[i])
w:u32le(buffer.len(f.data))
end
w:setU32le(4, dirOfs)
w:setU32le(8, w:tell() - dirOfs)
return w:finish(), nil
end
-- ------------------------------------------------------------------------------------------------ extras
-- new(variant?) -> an empty PAK Archive: a:add(name, data); a:write()
function Pak.new(variant: ("quake" | "sin")?): any
local a = Archive.new("pak", encode)
a.meta.source = { format = "pak", variant = variant or "quake" }
return a
end
return Pak
+31
View File
@@ -152,6 +152,37 @@ function Archive.entries(value: any): { { entry: Types.ArchiveEntry, read: () ->
return out
end
-- slice(buf, at, n) -> a loader for Entry.load: a copy of buf[at, at + n) (stored entries of uncompressed archives)
function Archive.slice(buf: buffer, at: number, n: number): () -> (buffer?, Err.Err?)
return function(): (buffer?, Err.Err?)
local out = buffer.create(n)
buffer.copy(out, 0, buf, at, n)
return out, nil
end
end
-- collect(value) -> { { entry, data } } | nil, Err: every entry of an Archive-shaped value with its bytes read
-- (directories: data = an empty buffer), for writers of simple archives (PAK, WAD, GRP, VPK, ...)
function Archive.collect(value: any): ({ { entry: Types.ArchiveEntry, data: buffer } }?, Err.Err?)
local items = Archive.entries(value)
if not items then
return nil, Err.new(Err.ARG, "expected an Archive")
end
local out: { { entry: Types.ArchiveEntry, data: buffer } } = {}
for _, it in items do
local b: buffer = buffer.create(0)
if not it.entry.dir then
local d, err = it.read()
if not d then
return nil, err or Err.new(Err.CORRUPT, "entry " .. it.entry.name .. ": read failed")
end
b = d
end
table.insert(out, { entry = it.entry, data = b })
end
return out, nil
end
-- streamName(opts, ext) -> the entry name for a single-stream archive: opts.name without its compression extension
-- (".tgz" -> ".tar"), else "data"
function Archive.streamName(opts: any?, exts: { string }): string