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

+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