Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat(artifacts): read Godot PCK packs
Add a Godot Engine PCK artifact reader for standalone packs and packs
embedded behind an executable trailer (formats 2 and 3). Members are
uncompressed byte ranges verified against their declared MD5 on read.

Encrypted directories, sparse bundles, patch removal entries, unknown
flags and format 1 fail closed as unavailable; encrypted members are
reported without being read. inspect-artifact and extract-artifact
recognize standalone packs by their GDPC magic.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
  • Loading branch information
0xthearchitect and claude committed Oct 14, 2026
commit 6edfe0a1b9d07a7646e2bbdb78691dd7b09558a8
6 changes: 5 additions & 1 deletion src/application/BinaryTargetResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
hasBinaryPlistSignature,
MAX_XML_PLIST_PROBE_BYTES,
} from "../domain/apple/appleArchiveFormat.js";
import { hasGodotPckHeader } from "../domain/godotPckFormat.js";
import {
hasZipSignature,
isZipPackageFormat,
Expand Down Expand Up @@ -316,6 +317,8 @@ const detectArtifactFormat = async (
if (lower.endsWith(".app")) return "app-pack";
return zipPackageFormatForPath(lower) ?? "zip";
}
if (hasGodotPckHeader(magic.subarray(0, observed.bytesRead)))
return "godot-pck";
// Binary plists, including keyed archives, often use other extensions.
if (hasBinaryPlistSignature(magic.subarray(0, observed.bytesRead)))
return "plist";
Expand Down Expand Up @@ -395,7 +398,8 @@ const isArchiveFormat = (
| "raw-binary"
>,
): format is Extract<BinaryTarget, { kind: "archive" }>["format"] =>
isZipPackageFormat(format) || ["asar", "dmg", "pkg"].includes(format);
isZipPackageFormat(format) ||
["asar", "godot-pck", "dmg", "pkg"].includes(format);

const sha256Handle = async (
handle: FileHandle,
Expand Down
7 changes: 6 additions & 1 deletion src/artifacts/ArtifactReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,12 @@ export interface ArtifactTraversalOptions {

/** Read-only adapter over one directory, archive, or virtual container. */
export interface ArtifactReader {
readonly format: "directory" | ZipPackageFormat | "asar" | "file";
readonly format:
| "directory"
| ZipPackageFormat
| "asar"
| "godot-pck"
| "file";
entries(
signal?: AbortSignal,
options?: ArtifactTraversalOptions,
Expand Down
234 changes: 234 additions & 0 deletions src/artifacts/GodotPckArtifactReader.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
import { createHash } from "node:crypto";
import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import { text } from "node:stream/consumers";

import { describe, expect, it } from "vitest";

import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js";

import type { ArtifactEntry } from "./ArtifactReader.js";
import { GodotPckArtifactReader } from "./GodotPckArtifactReader.js";

interface Member {
readonly path: string;
readonly data: string;
readonly flags?: number;
readonly md5?: Buffer;
readonly size?: bigint;
}

const u32 = (value: number): Buffer => {
const bytes = Buffer.alloc(4);
bytes.writeUInt32LE(value);
return bytes;
};
const u64 = (value: bigint | number): Buffer => {
const bytes = Buffer.alloc(8);
bytes.writeBigUInt64LE(BigInt(value));
return bytes;
};
const pad4 = (bytes: Buffer): Buffer =>
Buffer.concat([bytes, Buffer.alloc((4 - (bytes.length % 4)) % 4)]);

/**
* Build a pack with the layout Godot's exporter writes (and GDRE Tools
* reproduces): format 2 places the directory after a 96-byte header and
* members after it; format 3 stores members first and a pack-relative
* directory offset.
*/
const buildPck = (
version: 2 | 3,
members: readonly Member[],
options: { packFlags?: number; fileCount?: number } = {},
): Buffer => {
const data: Buffer[] = [];
const offsets: number[] = [];
let dataBytes = 0;
for (const member of members) {
offsets.push(dataBytes);
const bytes = pad4(Buffer.from(member.data));
data.push(bytes);
dataBytes += bytes.length;
}
const directory = Buffer.concat([
u32(options.fileCount ?? members.length),
...members.flatMap((member, index) => {
const path = pad4(Buffer.from(member.path));
return [
u32(path.length),
path,
u64(offsets[index] ?? 0),
u64(member.size ?? BigInt(Buffer.byteLength(member.data))),
member.md5 ?? createHash("md5").update(member.data).digest(),
u32(member.flags ?? 0),
];
}),
]);
const headerBytes = 96;
const fileBase = version === 2 ? headerBytes + directory.length : headerBytes;
const header = Buffer.concat([
Buffer.from("GDPC"),
u32(version),
u32(4),
u32(6),
u32(1),
u32(options.packFlags ?? (version === 3 ? 2 : 0)),
u64(fileBase),
u64(version === 3 ? headerBytes + dataBytes : 0),
]);
const paddedHeader = Buffer.concat([
header,
Buffer.alloc(headerBytes - header.length),
]);
return version === 2
? Buffer.concat([paddedHeader, directory, ...data])
: Buffer.concat([paddedHeader, ...data, directory]);
};

const embed = (pack: Buffer): Buffer =>
Buffer.concat([
Buffer.from("\x7fELF fake engine bytes"),
pack,
u64(pack.length),
Buffer.from("GDPC"),
]);

const writePck = async (bytes: Buffer): Promise<string> => {
const path = join(await createTestTempDirectory("rea-godot-pck-"), "game");
await writeFile(path, bytes);
return path;
};

const list = async (
reader: GodotPckArtifactReader,
): Promise<ArtifactEntry[]> => {
const entries: ArtifactEntry[] = [];
for await (const entry of reader.entries()) entries.push(entry);
return entries;
};

const withReader = async <T>(
bytes: Buffer,
use: (reader: GodotPckArtifactReader) => Promise<T>,
): Promise<T> => {
const reader = new GodotPckArtifactReader(await writePck(bytes));
try {
return await use(reader);
} finally {
await reader.close();
}
};

const members: readonly Member[] = [
{ path: "res://scenes/main.gd", data: "extends Node\n" },
{ path: "project.binary", data: "" },
{ path: "data/é.json", data: '{"session":"Session1"}' },
];

describe("GodotPckArtifactReader", () => {
it.each([
["format 3 standalone", buildPck(3, members), false],
["format 2 standalone", buildPck(2, members), false],
["format 3 embedded", embed(buildPck(3, members)), true],
[
"format 2 embedded with relative file base",
embed(buildPck(2, members, { packFlags: 2 })),
true,
],
])(
"lists and reads every member of a %s pack",
async (_, bytes, embedded) => {
await withReader(bytes, async (reader) => {
const entries = await list(reader);
expect(entries.map(({ path }) => path)).toEqual([
"scenes/main.gd",
"project.binary",
"data/é.json",
]);
expect(reader.header()).toMatchObject({
embedded,
engineVersion: "4.6.1",
fileCount: 3,
});
const contents = await Promise.all(
entries.map(async (entry) => text(await reader.open(entry))),
);
expect(contents).toEqual(members.map(({ data }) => data));
expect(entries[1]?.limitations).toEqual([]);
});
},
);

it("fails closed when a member contradicts its declared MD5", async () => {
const bytes = buildPck(3, [
{ path: "a.gd", data: "real", md5: Buffer.alloc(16, 1) },
]);
await withReader(bytes, async (reader) => {
const [entry] = await list(reader);
if (entry === undefined) throw new Error("expected one entry");
await expect(text(await reader.open(entry))).rejects.toMatchObject({
reason: "integrity",
});
});
});

it("reports encrypted members without reading them", async () => {
const bytes = buildPck(3, [{ path: "secret.gd", data: "x", flags: 1 }]);
await withReader(bytes, async (reader) => {
const [entry] = await list(reader);
if (entry === undefined) throw new Error("expected one entry");
expect(entry.encrypted).toBe(true);
await expect(reader.open(entry)).rejects.toMatchObject({
reason: "unavailable",
});
});
});

it.each([
[
"an encrypted directory",
buildPck(3, members, { packFlags: 2 | 1 }),
"unavailable",
],
[
"a sparse bundle",
buildPck(3, members, { packFlags: 2 | 4 }),
"unavailable",
],
[
"a patch removal entry",
buildPck(3, [{ path: "gone.gd", data: "", flags: 2 }]),
"unavailable",
],
[
"a member count that cannot fit",
buildPck(3, members, { fileCount: 0xffff_ffff }),
"format",
],
[
"member data beyond the pack",
buildPck(3, [{ path: "big.bin", data: "x", size: 1n << 40n }]),
"format",
],
[
"a Godot 3 format 1 pack",
Buffer.concat([Buffer.from("GDPC"), u32(1), Buffer.alloc(120)]),
"unavailable",
],
["bytes without a pack", Buffer.from("not a godot pack at all"), "format"],
[
"a trailer pointing outside the file",
Buffer.concat([
Buffer.from("engine"),
u64(1n << 40n),
Buffer.from("GDPC"),
]),
"format",
],
])("refuses %s", async (_, bytes, reason) => {
await withReader(bytes, async (reader) => {
await expect(list(reader)).rejects.toMatchObject({ reason });
});
});
});
Loading