mirror of
https://github.com/morluto/rea.git
synced 2026-11-19 13:12:26 +00:00
refactor(artifacts): read Godot PCK fields via BinaryReader (#11911)
This commit is contained in:
3 files changed
+29
-21
No files matched your search
@@ -3,7 +3,6 @@
|
||||
"src/application/MipsElfMetadata.ts": 10,
|
||||
"src/application/MipsGnuAttributes.ts": 2,
|
||||
"src/artifacts/DesktopFileInspection.ts": 4,
|
||||
"src/artifacts/GodotPckArtifactReader.ts": 6,
|
||||
"src/artifacts/UnityFsStructures.ts": 8,
|
||||
"src/artifacts/WxapkgStructures.ts": 3,
|
||||
"src/artifacts/apple/NibArchive.ts": 8,
|
||||
@@ -42,7 +41,6 @@
|
||||
"src/domain/firmware/devicetreeParser.ts": 4,
|
||||
"src/domain/firmware/fitImage.ts": 1,
|
||||
"src/domain/gamecubeDol.ts": 6,
|
||||
"src/domain/godotPckFormat.ts": 5,
|
||||
"src/domain/il2cpp/il2cppMetadataHeaderPrefix.ts": 3,
|
||||
"src/domain/inesContainer.ts": 5,
|
||||
"src/domain/managed/managedCliHeaderPrefix.ts": 5,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { readArtifactFileBytes } from "./ArtifactFileRange.js";
|
||||
import { BinaryReader } from "../domain/binaryContainer.js";
|
||||
import {
|
||||
DEFAULT_GODOT_READER_LIMITS,
|
||||
type GodotReaderLimits,
|
||||
@@ -211,6 +212,7 @@ const readGodotPckHeader = async (
|
||||
throw formatFailure("no standalone header or executable trailer");
|
||||
const { start, end, embedded } = location;
|
||||
const fixed = await readArtifactFileBytes(handle, start, 40, end, signal);
|
||||
const fixedReader = new BinaryReader(fixed, true, "godot-pck");
|
||||
const header = readGodotPckHeaderFacts(fixed);
|
||||
const version = header.formatVersion;
|
||||
if (![1, 2, 3, 4].includes(version))
|
||||
@@ -218,7 +220,7 @@ const readGodotPckHeader = async (
|
||||
"unavailable",
|
||||
`Godot PCK format ${String(version)} is unsupported; plain formats 1, 2, 3 and 4 are supported`,
|
||||
);
|
||||
const flags = version === 1 ? 0 : fixed.readUInt32LE(20);
|
||||
const flags = version === 1 ? 0 : fixedReader.u32(20);
|
||||
if (
|
||||
(flags & ~(PACK_DIR_ENCRYPTED | PACK_REL_FILEBASE | PACK_SPARSE_BUNDLE)) !==
|
||||
0
|
||||
@@ -231,18 +233,20 @@ const readGodotPckHeader = async (
|
||||
version === 1
|
||||
? 0
|
||||
: toSafeOffset(
|
||||
fixed.readBigUInt64LE(24),
|
||||
fixedReader.word(24, 64),
|
||||
version >= 3 || (flags & PACK_REL_FILEBASE) !== 0 ? start : 0,
|
||||
end,
|
||||
"file base",
|
||||
);
|
||||
const directory =
|
||||
version >= 3
|
||||
? toSafeOffset(fixed.readBigUInt64LE(32), start, end, "directory")
|
||||
? toSafeOffset(fixedReader.word(32, 64), start, end, "directory")
|
||||
: start + (version === 1 ? 84 : 96);
|
||||
const count = (
|
||||
await readArtifactFileBytes(handle, directory, 4, end, signal)
|
||||
).readUInt32LE(0);
|
||||
const count = new BinaryReader(
|
||||
await readArtifactFileBytes(handle, directory, 4, end, signal),
|
||||
true,
|
||||
"godot-pck",
|
||||
).u32(0);
|
||||
const status =
|
||||
(flags & PACK_DIR_ENCRYPTED) !== 0
|
||||
? "encrypted"
|
||||
@@ -339,10 +343,10 @@ class PckCursor {
|
||||
return this.#buffer.subarray(start, start + length);
|
||||
}
|
||||
async uint32(): Promise<number> {
|
||||
return (await this.bytes(4)).readUInt32LE(0);
|
||||
return new BinaryReader(await this.bytes(4), true, "godot-pck").u32(0);
|
||||
}
|
||||
async uint64(): Promise<bigint> {
|
||||
return (await this.bytes(8)).readBigUInt64LE(0);
|
||||
return new BinaryReader(await this.bytes(8), true, "godot-pck").word(0, 64);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { BinaryReader } from "./binaryContainer.js";
|
||||
|
||||
/** Whether bytes start with a standalone Godot PCK header. */
|
||||
export const hasGodotPckHeader = (bytes: Uint8Array): boolean =>
|
||||
bytes.length >= 4 &&
|
||||
@@ -14,16 +16,25 @@ export interface GodotPckHeaderFacts {
|
||||
readonly enginePatch: number;
|
||||
}
|
||||
|
||||
const pckReader = (bytes: Uint8Array): BinaryReader =>
|
||||
new BinaryReader(
|
||||
Buffer.isBuffer(bytes)
|
||||
? bytes
|
||||
: Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength),
|
||||
true,
|
||||
"godot-pck",
|
||||
);
|
||||
|
||||
/** Decode declared fields after the caller admits a complete 20-byte header. */
|
||||
export const readGodotPckHeaderFacts = (
|
||||
bytes: Uint8Array,
|
||||
): GodotPckHeaderFacts => {
|
||||
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
||||
const reader = pckReader(bytes);
|
||||
return {
|
||||
formatVersion: view.getUint32(4, true),
|
||||
engineMajor: view.getUint32(8, true),
|
||||
engineMinor: view.getUint32(12, true),
|
||||
enginePatch: view.getUint32(16, true),
|
||||
formatVersion: reader.u32(4),
|
||||
engineMajor: reader.u32(8),
|
||||
engineMinor: reader.u32(12),
|
||||
enginePatch: reader.u32(16),
|
||||
};
|
||||
};
|
||||
|
||||
@@ -43,12 +54,7 @@ export const readGodotPckTrailerFacts = (
|
||||
fileSize: number,
|
||||
): GodotPckTrailerFacts => {
|
||||
if (!hasGodotPckHeader(trailer.subarray(8, 12))) return { state: "absent" };
|
||||
const view = new DataView(
|
||||
trailer.buffer,
|
||||
trailer.byteOffset,
|
||||
trailer.byteLength,
|
||||
);
|
||||
const packBytes = view.getBigUint64(0, true);
|
||||
const packBytes = pckReader(trailer).word(0, 64);
|
||||
const end = fileSize - 12;
|
||||
return {
|
||||
state: "declared",
|
||||
|
||||
Reference in new issue
Block a user