psx map improvement

This commit is contained in:
MaddoScientisto 2026-04-16 23:52:41 +02:00
commit 9fe261610f
14 changed files with 859 additions and 483 deletions

View file

@ -9,12 +9,150 @@ function readU16LE(buffer, offset) {
}
const ALLOWED_LANE_WORDS = new Set([0x20, 0x22, 0x30]);
const PSX_SCREEN_SCALE = 2;
// 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}`);
@ -101,6 +239,13 @@ export function parseLsetWdl(buffer, filePath) {
sections,
boundaryCandidates,
regions,
loaderLayout: (() => {
try {
return parseLoaderLayout(buffer, { variant: 'lset' });
} catch {
return null;
}
})(),
};
}
@ -447,3 +592,157 @@ export function parseRegion01Records(region) {
records,
};
}
// Loader-faithful 12-byte constructor-placement records straight out of the
// `ctorPlacements` subrange of the section pack. Layout per
// `psx_dispatch_section0_constructor_placements @ 0x800258cc`:
// [u32 count][count * { u16 typeWord; u16 X; u16 Y; u16 Z; u16 selector;
// u16 flags }]
// Each record is called as `descriptor_table[typeWord].slot0(record, 0)` and
// `psx_object_create_compound_record` then reads exactly those 6 u16 fields.
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 words = [];
for (let cursor = 0; cursor < 12; cursor += 2) {
words.push(readU16LE(block.buffer, recordOffset + cursor));
}
const record = buildRecord(words, 'ctorPlacements', recordOffset, words);
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:
// +4 u16 typeId (argument to descriptor_table[typeId])
// +8 u16 screenX (for +/-0x140 cull)
// +10 u16 screenY (for +/-0x140 cull)
// +16 u16 flags (bit 3 = skip this record)
// Z, selector and lane are not universally used by the dispatcher. The empirical
// best mapping that matches constructor-placement convention is:
// +6 u16 zeta (often 0)
// +12 u16 selector/rotation
// +14 u16 lane/flags-lo
// We keep them in rawWords so downstream consumers can probe further, but
// buildRecord uses the cull-verified X/Y/typeId for positioning.
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));
}
// Project into buildRecord's 6-word ctor-placement shape using the fields
// the live dispatcher reads. rawWords indices:
// [0..1] (+0..+3) prefix
// [2] (+4) typeId (dispatch)
// [3] (+6) zeta / z-ish
// [4] (+8) X (cull)
// [5] (+10) Y (cull)
// [6] (+12) selector-ish
// [7] (+14) lane-ish
// [8] (+16) flags (bit 3 skip)
// [9..11] trailing
const flags = rawWords[8];
if ((flags & 0x8) !== 0) {
continue;
}
const typeWord = rawWords[2];
const xWord = rawWords[4];
const yWord = rawWords[5];
const zWord = rawWords[3] & 0xff;
const selectorWord = rawWords[6];
const laneWord = rawWords[7];
// Relaxed plausibility: dispatch-root records can have lane==0 or large
// selectors (e.g. behavior opcodes) because the dispatcher only reads
// typeId, X, Y, and the +0x10 skip flag. We only require typeId in the
// scene-relevant range and X/Y not both zero. This is looser than
// `isPlausibleRecord` on purpose.
if (typeWord < 0x20 || typeWord > 0x1ff) {
continue;
}
if ((xWord | yWord) === 0) {
continue;
}
const words = [typeWord, xWord, yWord, zWord, selectorWord, laneWord];
const screenX = (yWord - xWord) * PSX_SCREEN_SCALE;
const screenY = ((2 * zWord) - Math.floor((xWord + yWord) / 2)) * PSX_SCREEN_SCALE;
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,
};
}