770 lines
26 KiB
JavaScript
770 lines
26 KiB
JavaScript
import path from 'node:path';
|
|
|
|
function readU32LE(buffer, offset) {
|
|
return buffer.readUInt32LE(offset);
|
|
}
|
|
|
|
function readU16LE(buffer, offset) {
|
|
return buffer.readUInt16LE(offset);
|
|
}
|
|
|
|
const ALLOWED_LANE_WORDS = new Set([0x20, 0x22, 0x30]);
|
|
// Loader-faithful projection: per `psx_project_object_main_visible` the
|
|
// executable computes `screenX = Y - X` and `screenY = 2*Z - (X+Y)/2` in pixel
|
|
// units with no extra scale (the view-cull box is +/-0x140 = +/-320 pixels,
|
|
// matching PSX screen width). Keeping an explicit constant at 1 documents the
|
|
// 1:1 world-to-screen mapping and leaves room for experimentation.
|
|
const PSX_SCREEN_SCALE = 1;
|
|
|
|
function uniqueSorted(values) {
|
|
return [...new Set(values)].sort((left, right) => left - right);
|
|
}
|
|
|
|
// Loader-faithful layout per wdl_resource_bundle_load_by_index @ 0x80039444.
|
|
// The 0x38-byte header at the start of an LSET*.WDL file contains 14 u32 size
|
|
// words that parameterize every downstream read. Names follow the decompiled
|
|
// local variable order (local_c8 is read first into file offset 0x00).
|
|
const LOADER_SIZE_FIELDS = [
|
|
'packPreamble', // +0x00 local_c8 - bytes consumed before dispatch roots inside the section pack
|
|
'dispatchRootsSize', // +0x04 local_c4 - section-0 root dispatch rows
|
|
'ctorPlacementsSize', // +0x08 local_c0 - section-0 constructor placement records (12-byte stride)
|
|
'packTailRewindSize', // +0x0c local_bc - trailing bytes in pack that are heap-rewound after load
|
|
'ctorPlacementSection',// +0x10 local_b8 - psx_ctor_placement_section_ptr block
|
|
'sectionPackBaseSize', // +0x14 local_b4 - psx_level_section_pack_base to psx_type_policy_table_ptr
|
|
'policyTableSize', // +0x18 local_b0 - psx_type_policy_table_ptr block (per-type policy bits)
|
|
'table8006754cSize', // +0x1c local_ac - auxiliary table between policy and opcode streams
|
|
'opcodeStreamsSize', // +0x20 local_a8 - psx_control_opcode_stream_table to psx_level_clut_table_ptr
|
|
'detachedBlobSize', // +0x24 local_a4 - psx_level_detached_blob (audio runtime stream)
|
|
'artInstallSize', // +0x28 local_a0 - bundle B: per-type active-header + built-resource install
|
|
'stateBankSize', // +0x2c local_9c - state-script/component/extents bank install
|
|
'overrideSize', // +0x30 local_98 - late header-only per-type active-header override stream
|
|
'stateBank2Size', // +0x34 local_94 - second state-script/component/extents bank install
|
|
];
|
|
|
|
// Layout variant for SPEC_A.WDL: the first 0x3520 bytes are a VRAM image pair
|
|
// uploaded on first boot; the 0x38-byte loader header starts at offset 0x3520.
|
|
// Only the last five size words (a4/a0/9c/98/94) matter — the c8..a8 slots are
|
|
// leftover/undefined bytes in the SPEC_A header because it skips the section
|
|
// pack entirely and goes straight to bundle A.
|
|
const SPEC_A_VRAM_PRELOAD_SIZE = 0x3520;
|
|
|
|
export function parseLoaderLayout(buffer, options = {}) {
|
|
const variant = options.variant ?? 'lset';
|
|
const headerOffset = variant === 'spec_a' ? SPEC_A_VRAM_PRELOAD_SIZE : 0;
|
|
if (buffer.length < headerOffset + 0x38) {
|
|
throw new Error(`Buffer too small for loader header at 0x${headerOffset.toString(16)}`);
|
|
}
|
|
|
|
const sizes = {};
|
|
LOADER_SIZE_FIELDS.forEach((name, index) => {
|
|
sizes[name] = readU32LE(buffer, headerOffset + index * 4);
|
|
});
|
|
|
|
const blocks = [];
|
|
let cursor = headerOffset + 0x38;
|
|
const addBlock = (name, size) => {
|
|
const entry = {
|
|
name,
|
|
offset: cursor,
|
|
size,
|
|
end: cursor + size,
|
|
buffer: size > 0 && cursor + size <= buffer.length
|
|
? buffer.subarray(cursor, cursor + size)
|
|
: null,
|
|
};
|
|
blocks.push(entry);
|
|
cursor += size;
|
|
return entry;
|
|
};
|
|
|
|
if (variant !== 'spec_a') {
|
|
// Section pack = sum of c8..a8 read as a single contiguous CD read, then the
|
|
// loader subdivides it using the same size words.
|
|
const packSize = sizes.packPreamble
|
|
+ sizes.dispatchRootsSize
|
|
+ sizes.ctorPlacementsSize
|
|
+ sizes.packTailRewindSize
|
|
+ sizes.ctorPlacementSection
|
|
+ sizes.sectionPackBaseSize
|
|
+ sizes.policyTableSize
|
|
+ sizes.table8006754cSize
|
|
+ sizes.opcodeStreamsSize;
|
|
addBlock('sectionPack', packSize);
|
|
addBlock('detachedBlob', sizes.detachedBlobSize);
|
|
}
|
|
|
|
// SPEC_A skips both the section pack and the detached blob: after the
|
|
// 0x38-byte header it jumps straight to bundle A. LSET reads the section
|
|
// pack first, then the detached blob, then bundle B. Past this point both
|
|
// variants share the same art-install / state / override / state2 sequence.
|
|
addBlock('artInstall', sizes.artInstallSize);
|
|
addBlock('stateBank', sizes.stateBankSize);
|
|
addBlock('override', sizes.overrideSize);
|
|
addBlock('stateBank2', sizes.stateBank2Size);
|
|
|
|
const blocksByName = new Map(blocks.map((block) => [block.name, block]));
|
|
|
|
// Subranges inside the section pack follow the loader pointer chain:
|
|
// dispatch_roots = pack + packPreamble
|
|
// ctor_placements = dispatch_roots + dispatchRootsSize
|
|
// ctor_placement_section = ctor_placements + ctorPlacementsSize
|
|
// section_pack_base = ctor_placement_section + ctorPlacementSection
|
|
// policy_table = section_pack_base + sectionPackBaseSize
|
|
// table_8006754c = policy_table + policyTableSize
|
|
// opcode_streams = table_8006754c + table8006754cSize
|
|
// (pack tail of packTailRewindSize bytes is heap-rewound)
|
|
let packSubranges = [];
|
|
const pack = blocksByName.get('sectionPack');
|
|
if (pack) {
|
|
let sub = pack.offset;
|
|
const push = (name, size) => {
|
|
packSubranges.push({
|
|
name,
|
|
offset: sub,
|
|
size,
|
|
end: sub + size,
|
|
buffer: size > 0 && sub + size <= buffer.length
|
|
? buffer.subarray(sub, sub + size)
|
|
: null,
|
|
});
|
|
sub += size;
|
|
};
|
|
push('packPreamble', sizes.packPreamble);
|
|
push('dispatchRoots', sizes.dispatchRootsSize);
|
|
push('ctorPlacements', sizes.ctorPlacementsSize);
|
|
push('ctorPlacementSection', sizes.ctorPlacementSection);
|
|
push('sectionPackBase', sizes.sectionPackBaseSize);
|
|
push('policyTable', sizes.policyTableSize);
|
|
push('table_8006754c', sizes.table8006754cSize);
|
|
push('opcodeStreams', sizes.opcodeStreamsSize);
|
|
push('packTailRewind', sizes.packTailRewindSize);
|
|
}
|
|
|
|
const remaining = buffer.length - cursor;
|
|
return {
|
|
variant,
|
|
headerOffset,
|
|
sizes,
|
|
blocks,
|
|
blocksByName,
|
|
packSubranges,
|
|
trailingBytes: remaining,
|
|
totalConsumed: cursor,
|
|
};
|
|
}
|
|
|
|
export function parseLsetWdl(buffer, filePath) {
|
|
if (buffer.length < 0x34) {
|
|
throw new Error(`File too small for LSET header: ${filePath}`);
|
|
}
|
|
|
|
const headerSize = readU32LE(buffer, 0);
|
|
if (headerSize < 0x34 || headerSize % 4 !== 0 || headerSize > buffer.length) {
|
|
throw new Error(`Unexpected header size 0x${headerSize.toString(16)} in ${filePath}`);
|
|
}
|
|
|
|
const headerWords = [];
|
|
for (let offset = 0; offset < headerSize; offset += 4) {
|
|
headerWords.push(readU32LE(buffer, offset));
|
|
}
|
|
|
|
const audioSize = readU32LE(buffer, 4);
|
|
const postAudioStart = headerSize + audioSize;
|
|
const sectionSizes = [];
|
|
for (let offset = 0x08; offset < 0x38 && offset + 4 <= buffer.length; offset += 4) {
|
|
sectionSizes.push(readU32LE(buffer, offset));
|
|
}
|
|
|
|
const sections = [];
|
|
let sectionCursor = postAudioStart;
|
|
for (let index = 0; index < sectionSizes.length; index += 1) {
|
|
const size = sectionSizes[index];
|
|
if (size <= 0 || sectionCursor + size > buffer.length) {
|
|
break;
|
|
}
|
|
|
|
sections.push({
|
|
name: `post_audio_section_${String(index).padStart(2, '0')}`,
|
|
offset: sectionCursor,
|
|
size,
|
|
buffer: buffer.subarray(sectionCursor, sectionCursor + size),
|
|
});
|
|
sectionCursor += size;
|
|
}
|
|
|
|
const boundaryCandidates = uniqueSorted(
|
|
headerWords
|
|
.slice(2)
|
|
.filter((value) => value > postAudioStart && value < buffer.length)
|
|
);
|
|
|
|
if (boundaryCandidates.length < 4) {
|
|
throw new Error(
|
|
`Expected at least 4 post-audio boundaries, found ${boundaryCandidates.length} in ${filePath}`
|
|
);
|
|
}
|
|
|
|
const selectedBoundaries = boundaryCandidates.slice(0, 4);
|
|
const regions = [];
|
|
const regionStarts = [postAudioStart, ...selectedBoundaries];
|
|
const regionEnds = [...selectedBoundaries, buffer.length];
|
|
|
|
regions.push({
|
|
name: 'audio_or_spu_blob',
|
|
offset: headerSize,
|
|
size: audioSize,
|
|
buffer: buffer.subarray(headerSize, postAudioStart),
|
|
});
|
|
|
|
for (let index = 0; index < regionStarts.length; index += 1) {
|
|
const offset = regionStarts[index];
|
|
const end = regionEnds[index];
|
|
regions.push({
|
|
name: `post_audio_region_${String(index).padStart(2, '0')}`,
|
|
offset,
|
|
size: end - offset,
|
|
buffer: buffer.subarray(offset, end),
|
|
});
|
|
}
|
|
|
|
return {
|
|
filePath,
|
|
fileName: path.basename(filePath),
|
|
buffer,
|
|
headerSize,
|
|
audioSize,
|
|
postAudioStart,
|
|
headerWords,
|
|
sectionSizes,
|
|
sections,
|
|
boundaryCandidates,
|
|
regions,
|
|
loaderLayout: (() => {
|
|
try {
|
|
return parseLoaderLayout(buffer, { variant: 'lset' });
|
|
} catch {
|
|
return null;
|
|
}
|
|
})(),
|
|
};
|
|
}
|
|
|
|
function isPlausibleRecord(words) {
|
|
const [typeWord, xWord, yWord, zWord, selectorWord, laneWord] = words;
|
|
if (typeWord < 0x20 || typeWord > 0x1ff) {
|
|
return false;
|
|
}
|
|
if ((xWord | yWord | zWord) === 0) {
|
|
return false;
|
|
}
|
|
if (laneWord === 0 || laneWord > 0x1fff) {
|
|
return false;
|
|
}
|
|
if (selectorWord > 0x03ff) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
function isStructuredCandidate(words) {
|
|
const [typeWord, xWord, yWord, zWord, selectorWord, laneWord] = words;
|
|
if (typeWord >= 0x200) {
|
|
return false;
|
|
}
|
|
if (xWord === 0 && yWord === 0) {
|
|
return false;
|
|
}
|
|
if (xWord >= 0x4000 || yWord >= 0x4000) {
|
|
return false;
|
|
}
|
|
if (zWord > 0x20 || selectorWord > 0x04) {
|
|
return false;
|
|
}
|
|
if (!ALLOWED_LANE_WORDS.has(laneWord)) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
function projectCtorPlacement(xWord, yWord, zWord) {
|
|
// Engine-accurate projection, matching `psx_project_object_main_visible`
|
|
// (0x80040d44) followed by the camera subtraction at 0x80040e3c/0x80040e5c.
|
|
//
|
|
// The projection step writes to obj+0x78/0x7c:
|
|
// proj_x = Y - X
|
|
// proj_y = 2*Z - (X+Y)/2
|
|
// The draw step then writes to obj+0x20/0x22:
|
|
// screen_x = (cam_X - proj_x) - origin_X
|
|
// screen_y = (cam_Y - proj_y) - origin_Y
|
|
// i.e. the engine renders the world with the sign flipped relative to the
|
|
// raw projection. For a camera-less full-map export we fold that flip into
|
|
// the projection so higher world Z appears higher on the canvas (Y-down).
|
|
const projX = yWord - xWord;
|
|
const projY = (2 * zWord) - Math.floor((xWord + yWord) / 2);
|
|
return {
|
|
screenX: -projX * PSX_SCREEN_SCALE,
|
|
screenY: -projY * PSX_SCREEN_SCALE,
|
|
};
|
|
}
|
|
|
|
function buildRecord(words, source, offset, rawWords = words) {
|
|
if (!isPlausibleRecord(words)) {
|
|
return null;
|
|
}
|
|
|
|
const [typeWord, xWord, yWord, zWord, selectorWord, laneWord] = words;
|
|
const { screenX, screenY } = projectCtorPlacement(xWord, yWord, zWord);
|
|
|
|
return {
|
|
index: -1,
|
|
source,
|
|
offset,
|
|
words,
|
|
rawWords,
|
|
typeWord,
|
|
xWord,
|
|
yWord,
|
|
zWord,
|
|
selectorWord,
|
|
laneWord,
|
|
screenX,
|
|
screenY,
|
|
sourceFamily: null,
|
|
sourceRole: null,
|
|
recordSide: null,
|
|
rowIndex: -1,
|
|
};
|
|
}
|
|
|
|
function decodeRecord(buffer, offset, source) {
|
|
const words = [];
|
|
for (let cursor = 0; cursor < 12; cursor += 2) {
|
|
words.push(readU16LE(buffer, offset + cursor));
|
|
}
|
|
|
|
return buildRecord(words, source, offset, words);
|
|
}
|
|
|
|
function makeAsciiPreview(buffer, length = 64) {
|
|
const slice = buffer.subarray(0, Math.min(length, buffer.length));
|
|
let text = '';
|
|
for (const value of slice) {
|
|
text += value >= 0x20 && value <= 0x7e ? String.fromCharCode(value) : '.';
|
|
}
|
|
return text;
|
|
}
|
|
|
|
function scanOffsetTableCandidates(buffer, maxBase = 0x200) {
|
|
const candidates = [];
|
|
const limit = Math.min(maxBase, Math.max(0, buffer.length - 8));
|
|
|
|
for (let base = 0; base <= limit; base += 2) {
|
|
const count = readU16LE(buffer, base);
|
|
if (count <= 0 || count >= 0x200) {
|
|
continue;
|
|
}
|
|
|
|
const tableEnd = base + 2 + count * 2;
|
|
if (tableEnd > buffer.length) {
|
|
continue;
|
|
}
|
|
|
|
let previous = -1;
|
|
let monotonic = true;
|
|
const firstOffsets = [];
|
|
for (let index = 0; index < count; index += 1) {
|
|
const offset = readU16LE(buffer, base + 2 + index * 2);
|
|
if (index < 8) {
|
|
firstOffsets.push(offset);
|
|
}
|
|
if (offset < previous || offset >= buffer.length) {
|
|
monotonic = false;
|
|
break;
|
|
}
|
|
previous = offset;
|
|
}
|
|
|
|
if (monotonic) {
|
|
candidates.push({ base, count, firstOffsets });
|
|
}
|
|
}
|
|
|
|
return candidates;
|
|
}
|
|
|
|
function scanPlausible12ByteRecordStarts(buffer, maxBase = 0x200) {
|
|
const starts = [];
|
|
const limit = Math.min(maxBase, Math.max(0, buffer.length - 12));
|
|
|
|
for (let base = 0; base <= limit; base += 2) {
|
|
const words = [];
|
|
for (let cursor = 0; cursor < 12; cursor += 2) {
|
|
words.push(readU16LE(buffer, base + cursor));
|
|
}
|
|
if (isPlausibleRecord(words)) {
|
|
starts.push({ base, words });
|
|
}
|
|
}
|
|
|
|
return starts;
|
|
}
|
|
|
|
function buildPreviewRows(buffer, rowWordWidth = 8, rowCount = 24) {
|
|
const rows = [];
|
|
const maxRows = Math.min(rowCount, Math.floor(buffer.length / (rowWordWidth * 2)));
|
|
|
|
for (let rowIndex = 0; rowIndex < maxRows; rowIndex += 1) {
|
|
const offset = rowIndex * rowWordWidth * 2;
|
|
const words = [];
|
|
for (let wordIndex = 0; wordIndex < rowWordWidth; wordIndex += 1) {
|
|
words.push(readU16LE(buffer, offset + wordIndex * 2));
|
|
}
|
|
|
|
const bytes = buffer.subarray(offset, offset + rowWordWidth * 2);
|
|
rows.push({
|
|
rowIndex,
|
|
offset,
|
|
words,
|
|
ascii: makeAsciiPreview(bytes, bytes.length),
|
|
});
|
|
}
|
|
|
|
return rows;
|
|
}
|
|
|
|
export function summarizeRegion02(region) {
|
|
const firstU32 = [];
|
|
const firstU16 = [];
|
|
|
|
for (let offset = 0; offset + 4 <= region.buffer.length && firstU32.length < 8; offset += 4) {
|
|
firstU32.push(readU32LE(region.buffer, offset));
|
|
}
|
|
for (let offset = 0; offset + 2 <= region.buffer.length && firstU16.length < 16; offset += 2) {
|
|
firstU16.push(readU16LE(region.buffer, offset));
|
|
}
|
|
|
|
const offsetTableCandidates = scanOffsetTableCandidates(region.buffer);
|
|
const plausible12ByteRecordStarts = scanPlausible12ByteRecordStarts(region.buffer);
|
|
|
|
return {
|
|
offset: region.offset,
|
|
size: region.size,
|
|
firstU32,
|
|
firstU16,
|
|
asciiPreview: makeAsciiPreview(region.buffer, 96),
|
|
previewRows: buildPreviewRows(region.buffer),
|
|
offsetTableCandidates: offsetTableCandidates.slice(0, 16),
|
|
plausible12ByteRecordStarts: plausible12ByteRecordStarts.slice(0, 16),
|
|
note: offsetTableCandidates.length === 0 && plausible12ByteRecordStarts.length === 0
|
|
? 'Leading region-02 bytes do not look like a count-prefixed offset table or direct 12-byte placement rows.'
|
|
: 'Region-02 exposes candidate structure and should be correlated against live loader-installed subordinate slices.',
|
|
};
|
|
}
|
|
|
|
export function parseRegion00Records(region) {
|
|
const rowCount = region.buffer.length >= 4 ? readU32LE(region.buffer, 0) : 0;
|
|
const records = [];
|
|
for (let rowIndex = 0; rowIndex < rowCount; rowIndex += 1) {
|
|
const rowBase = 4 + rowIndex * 24;
|
|
if (rowBase + 24 > region.buffer.length) {
|
|
break;
|
|
}
|
|
|
|
const rowWords = [];
|
|
for (let wordIndex = 0; wordIndex < 12; wordIndex += 1) {
|
|
rowWords.push(readU16LE(region.buffer, rowBase + wordIndex * 2));
|
|
}
|
|
|
|
const leftRawWords = rowWords.slice(0, 6);
|
|
const rightRawWords = rowWords.slice(6, 12);
|
|
const leftWords = [rowWords[4], rowWords[5], rowWords[0], rowWords[1], rowWords[2], rowWords[3]];
|
|
const rightWords = [rowWords[10], rowWords[11], rowWords[6], rowWords[7], rowWords[8], rowWords[9]];
|
|
|
|
for (const [recordSide, wordSet, rawWordSet, sourceByteOffset] of [
|
|
['left', leftWords, leftRawWords, 0],
|
|
['right', rightWords, rightRawWords, 12],
|
|
]) {
|
|
const record = buildRecord(wordSet, 'region00', rowBase + sourceByteOffset, rawWordSet);
|
|
if (!record) {
|
|
continue;
|
|
}
|
|
|
|
record.sourceFamily = 'section0_dispatch_roots';
|
|
record.sourceRole = 'root-dispatch';
|
|
record.rowIndex = rowIndex;
|
|
record.recordSide = recordSide;
|
|
records.push(record);
|
|
}
|
|
}
|
|
|
|
records.forEach((record, index) => {
|
|
record.index = index;
|
|
record.absoluteOffset = region.offset + record.offset;
|
|
});
|
|
|
|
return {
|
|
source: 'region00',
|
|
recordStartOffset: 4,
|
|
records,
|
|
};
|
|
}
|
|
|
|
function detectStructured12ByteStream(buffer) {
|
|
let bestCandidate = null;
|
|
|
|
for (let headerOffset = 0; headerOffset + 16 <= buffer.length; headerOffset += 4) {
|
|
const count = readU32LE(buffer, headerOffset);
|
|
const recordStartOffset = headerOffset + 4;
|
|
const maxPossibleCount = Math.floor((buffer.length - recordStartOffset) / 12);
|
|
|
|
if (count === 0 || count > maxPossibleCount) {
|
|
continue;
|
|
}
|
|
|
|
let prefixStructuredCount = 0;
|
|
for (let index = 0; index < count; index += 1) {
|
|
const recordOffset = recordStartOffset + index * 12;
|
|
const words = [];
|
|
for (let cursor = 0; cursor < 12; cursor += 2) {
|
|
words.push(readU16LE(buffer, recordOffset + cursor));
|
|
}
|
|
if (!isStructuredCandidate(words)) {
|
|
break;
|
|
}
|
|
prefixStructuredCount += 1;
|
|
}
|
|
|
|
if (prefixStructuredCount < 16) {
|
|
continue;
|
|
}
|
|
|
|
if (
|
|
!bestCandidate ||
|
|
prefixStructuredCount > bestCandidate.prefixStructuredCount ||
|
|
(prefixStructuredCount === bestCandidate.prefixStructuredCount && headerOffset < bestCandidate.headerOffset)
|
|
) {
|
|
bestCandidate = {
|
|
headerOffset,
|
|
recordStartOffset,
|
|
count,
|
|
prefixStructuredCount,
|
|
};
|
|
}
|
|
}
|
|
|
|
return bestCandidate;
|
|
}
|
|
|
|
export function parseRegion01Records(region) {
|
|
const records = [];
|
|
const stream = detectStructured12ByteStream(region.buffer);
|
|
|
|
if (stream) {
|
|
for (let index = 0; index < stream.count; index += 1) {
|
|
const recordOffset = stream.recordStartOffset + index * 12;
|
|
if (recordOffset + 12 > region.buffer.length) {
|
|
break;
|
|
}
|
|
|
|
const record = decodeRecord(region.buffer, recordOffset, 'region01');
|
|
if (!record || !isStructuredCandidate(record.words)) {
|
|
continue;
|
|
}
|
|
|
|
record.sourceFamily = 'section0_constructor_placements';
|
|
record.sourceRole = 'constructor-placement';
|
|
record.rowIndex = index;
|
|
record.recordSide = null;
|
|
records.push(record);
|
|
}
|
|
} else {
|
|
for (let rowOffset = 0; rowOffset + 24 <= region.buffer.length; rowOffset += 24) {
|
|
const left = decodeRecord(region.buffer, rowOffset, 'region01-left');
|
|
const right = decodeRecord(region.buffer, rowOffset + 12, 'region01-right');
|
|
if (left && isStructuredCandidate(left.words)) {
|
|
left.sourceFamily = 'section0_constructor_placements';
|
|
left.sourceRole = 'constructor-placement';
|
|
left.rowIndex = Math.floor(rowOffset / 24);
|
|
left.recordSide = 'left';
|
|
records.push(left);
|
|
}
|
|
if (right && isStructuredCandidate(right.words)) {
|
|
right.sourceFamily = 'section0_constructor_placements';
|
|
right.sourceRole = 'constructor-placement';
|
|
right.rowIndex = Math.floor(rowOffset / 24);
|
|
right.recordSide = 'right';
|
|
records.push(right);
|
|
}
|
|
}
|
|
}
|
|
|
|
records.forEach((record, index) => {
|
|
record.index = index;
|
|
record.absoluteOffset = region.offset + record.offset;
|
|
});
|
|
|
|
return {
|
|
source: 'region01',
|
|
recordStartOffset: stream?.recordStartOffset ?? 0,
|
|
streamHeaderOffset: stream?.headerOffset ?? null,
|
|
streamRecordCount: stream?.count ?? null,
|
|
streamStructuredPrefixCount: stream?.prefixStructuredCount ?? null,
|
|
records,
|
|
};
|
|
}
|
|
|
|
// Loader-faithful 12-byte constructor-placement records straight out of the
|
|
// `ctorPlacements` subrange of the section pack. Layout per
|
|
// `psx_object_create_compound_record @ 0x80025040`:
|
|
// +0x00 u16 typeWord -> descriptor_table index
|
|
// +0x02 u16 X -> obj world X (<<16 seed)
|
|
// +0x04 u16 Y -> obj world Y (<<16 seed)
|
|
// +0x06 u8 Z (low byte only!)-> obj world Z (<<16 seed). The high byte at
|
|
// +0x07 is not read as part of Z.
|
|
// +0x08 u8 selector -> state-script seed
|
|
// +0x09 u8 (padding/unknown)
|
|
// +0x0a u16 laneWord -> OR'd into obj+0x1c (flags/lane)
|
|
// The previous implementation decoded Z as a u16 from +0x06, which mixed the
|
|
// selector byte into Z and produced large bogus elevations.
|
|
export function parseCtorPlacementsBlock(block, variant = 'lset') {
|
|
if (!block || !block.buffer || block.size < 4) {
|
|
return { source: 'ctorPlacements', records: [], count: 0 };
|
|
}
|
|
const count = readU32LE(block.buffer, 0);
|
|
const records = [];
|
|
if (count === 0 || count > 0x2000) {
|
|
return { source: 'ctorPlacements', records, count };
|
|
}
|
|
for (let index = 0; index < count; index += 1) {
|
|
const recordOffset = 4 + index * 12;
|
|
if (recordOffset + 12 > block.buffer.length) {
|
|
break;
|
|
}
|
|
const typeWord = readU16LE(block.buffer, recordOffset + 0x00);
|
|
const xWord = readU16LE(block.buffer, recordOffset + 0x02);
|
|
const yWord = readU16LE(block.buffer, recordOffset + 0x04);
|
|
const zWord = block.buffer[recordOffset + 0x06];
|
|
const z2Byte = block.buffer[recordOffset + 0x07];
|
|
const selectorWord = block.buffer[recordOffset + 0x08];
|
|
const selectorHighByte = block.buffer[recordOffset + 0x09];
|
|
const laneWord = readU16LE(block.buffer, recordOffset + 0x0a);
|
|
const rawWords = [typeWord, xWord, yWord, (z2Byte << 8) | zWord, (selectorHighByte << 8) | selectorWord, laneWord];
|
|
const words = [typeWord, xWord, yWord, zWord, selectorWord, laneWord];
|
|
const record = buildRecord(words, 'ctorPlacements', recordOffset, rawWords);
|
|
if (!record) {
|
|
continue;
|
|
}
|
|
record.sourceFamily = 'section0_constructor_placements';
|
|
record.sourceRole = 'constructor-placement';
|
|
record.rowIndex = index;
|
|
record.authoredLayer = 'constructors';
|
|
record.authoredVariant = variant;
|
|
records.push(record);
|
|
}
|
|
records.forEach((record, index) => {
|
|
record.index = index;
|
|
record.absoluteOffset = block.offset + record.offset;
|
|
});
|
|
return {
|
|
source: 'ctorPlacements',
|
|
recordStartOffset: 4,
|
|
reportedCount: count,
|
|
records,
|
|
};
|
|
}
|
|
|
|
// Loader-faithful 24-byte dispatch-root records from the `dispatchRoots`
|
|
// subrange of the section pack. Layout per
|
|
// `psx_dispatch_section0_dispatch_roots @ 0x800256b0`:
|
|
// [u32 count][count * 24 bytes]
|
|
// Within each record the dispatcher reads:
|
|
// +0x04 u16 typeId (descriptor_table index)
|
|
// +0x08 u16 worldX (compared to cam+0x3e +/- 0x140 — cam+0x3e is the
|
|
// camera's world-X short, not the projected integer
|
|
// part; proof: root worldX values overlap constructor
|
|
// placement worldX values in the same map)
|
|
// +0x0a u16 worldY (compared to cam+0x42 +/- 0x140)
|
|
// +0x10 u16 flags (bit 3 = skip this record)
|
|
// We therefore project root worldX/worldY through the same projection as
|
|
// constructor placements (with Z=0 since dispatch roots do not carry a Z byte
|
|
// at a known offset) and render them on the same canvas. The trailing raw
|
|
// fields are preserved as `dispatchRootRawWords` for future probes.
|
|
export function parseDispatchRootsBlock(block, variant = 'lset') {
|
|
if (!block || !block.buffer || block.size < 4) {
|
|
return { source: 'dispatchRoots', records: [], count: 0 };
|
|
}
|
|
const count = readU32LE(block.buffer, 0);
|
|
const records = [];
|
|
if (count === 0 || count > 0x2000) {
|
|
return { source: 'dispatchRoots', records, count };
|
|
}
|
|
for (let index = 0; index < count; index += 1) {
|
|
const recordOffset = 4 + index * 24;
|
|
if (recordOffset + 24 > block.buffer.length) {
|
|
break;
|
|
}
|
|
const rawWords = [];
|
|
for (let cursor = 0; cursor < 24; cursor += 2) {
|
|
rawWords.push(readU16LE(block.buffer, recordOffset + cursor));
|
|
}
|
|
const flags = rawWords[8];
|
|
if ((flags & 0x8) !== 0) {
|
|
continue;
|
|
}
|
|
const typeWord = rawWords[2];
|
|
const xWord = rawWords[4];
|
|
const yWord = rawWords[5];
|
|
// Engine reads Z from the LOW byte of the u16 at +0x06 of the shared
|
|
// authored prefix (constructor view), even when the record is iterated
|
|
// through the dispatcher view. Confirmed via psx_object_create_compound_record
|
|
// @ 0x80024eec (memory note psx-coordinate-decode-2026-04-18.md). The HIGH
|
|
// byte of the same u16 is a palette-override token, NOT a Z extension.
|
|
// The constructor-view u16 at +0x06 lives at rawWords[3] in this 12-u16
|
|
// expansion of the 24-byte dispatch-root record.
|
|
const zWord = rawWords[3] & 0xff;
|
|
const selectorWord = rawWords[6];
|
|
const laneWord = rawWords[7];
|
|
if (typeWord < 0x20 || typeWord > 0x1ff) {
|
|
continue;
|
|
}
|
|
if ((xWord | yWord) === 0) {
|
|
continue;
|
|
}
|
|
const words = [typeWord, xWord, yWord, zWord, selectorWord, laneWord];
|
|
const { screenX, screenY } = projectCtorPlacement(xWord, yWord, zWord);
|
|
const record = {
|
|
index: -1,
|
|
source: 'dispatchRoots',
|
|
offset: recordOffset,
|
|
words,
|
|
rawWords: words,
|
|
typeWord,
|
|
xWord,
|
|
yWord,
|
|
zWord,
|
|
selectorWord,
|
|
laneWord,
|
|
screenX,
|
|
screenY,
|
|
sourceFamily: 'section0_dispatch_roots',
|
|
sourceRole: 'root-dispatch',
|
|
recordSide: null,
|
|
rowIndex: index,
|
|
authoredLayer: 'roots',
|
|
authoredVariant: variant,
|
|
dispatchRootRawWords: rawWords,
|
|
dispatchRootFlags: flags,
|
|
};
|
|
records.push(record);
|
|
}
|
|
records.forEach((record, index) => {
|
|
record.index = index;
|
|
record.absoluteOffset = block.offset + record.offset;
|
|
});
|
|
return {
|
|
source: 'dispatchRoots',
|
|
recordStartOffset: 4,
|
|
reportedCount: count,
|
|
records,
|
|
};
|
|
}
|