Skip to content

Open-source Xbox-inspired desktop launcher for Xodus on Linux

TypeScript

[source-only] fix(dlc): resolve source and destination package layout coordinates

Nexus Maintainer84933211e1a9
diff --git a/src/main/game-content.test.ts b/src/main/game-content.test.ts
index 8995d47..dafded2 100644
--- a/src/main/game-content.test.ts
+++ b/src/main/game-content.test.ts
@@ -322,6 +322,92 @@ it('rejects ambiguous, excluded or unsafe destination layouts without mutating c
});
+// A package can be built from a staging tree whose source coordinate is a build
+// path while the package root is the declared destination. Such a tree is
+// already deployed when the metadata and payloads sit at the root.
+async function rootDestinationFixture() {
+ const f = await installFixture();
+ await fs.mkdir(path.join(f.directory, 'Assets'));
+ await fs.rename(path.join(f.directory, 'logo.PNG'), path.join(f.directory, 'Assets', 'logo.PNG'));
+ await fs.writeFile(path.join(f.directory, `layout_${GUID}.xml`), '<Package><Chunk><FileGroup SourcePath="build" DestinationPath="\\" Include="MicrosoftGame.config"/><FileGroup SourcePath="build\\Assets" DestinationPath="\\Assets" Include="*.*"/></Chunk></Package>');
+ return f;
+}
+// The metadata group is coordinate-neutral because its source path equals the
+// deployed root; only the payload groups carry coordinate evidence.
+async function neutralMetadataRootFixture() {
+ const f = await installFixture();
+ await fs.mkdir(path.join(f.directory, 'Assets'));
+ await fs.rename(path.join(f.directory, 'logo.PNG'), path.join(f.directory, 'Assets', 'logo.PNG'));
+ await fs.writeFile(path.join(f.directory, `layout_${GUID}.xml`), '<Package><Chunk><FileGroup SourcePath="." DestinationPath="\\" Include="MicrosoftGame.config"/><FileGroup SourcePath="build\\Assets" DestinationPath="\\Assets" Include="*.*"/></Chunk></Package>');
+ return f;
+}
+it('accepts an already-deployed root tree whose declared source coordinate is a build path', async () => {
+ const f = await rootDestinationFixture();
+ const configBytes = await fs.readFile(path.join(f.directory, 'MicrosoftGame.Config'));
+ await f.manager.complete(f.game, f.selected);
+ expect((await fs.readFile(path.join(f.directory, 'MicrosoftGame.Config'))).equals(configBytes)).toBe(true);
+ expect(await f.manager.roots(f.game)).toEqual([f.directory]);
+ await f.manager.complete(f.game, f.selected);
+ expect(await f.manager.roots(f.game)).toEqual([f.directory]);
+});
+it('accepts case-insensitive root destination metadata and payload without rewriting them', async () => {
+ const f = await rootDestinationFixture();
+ await fs.rename(path.join(f.directory, 'Assets'), path.join(f.directory, 'assets'));
+ await fs.rename(path.join(f.directory, 'MicrosoftGame.Config'), path.join(f.directory, 'MICROSOFTGAME.CONFIG'));
+ const configBytes = await fs.readFile(path.join(f.directory, 'MICROSOFTGAME.CONFIG'));
+ await f.manager.complete(f.game, f.selected);
+ expect((await fs.readFile(path.join(f.directory, 'MICROSOFTGAME.CONFIG'))).equals(configBytes)).toBe(true);
+ expect(await f.manager.roots(f.game)).toEqual([f.directory]);
+});
+it('resolves a root tree by whole-layout evidence when the metadata group is coordinate-neutral', async () => {
+ const f = await neutralMetadataRootFixture();
+ await f.manager.complete(f.game, f.selected);
+ expect(await f.manager.roots(f.game)).toEqual([f.directory]);
+});
+it('rejects a root tree that resolves under both layout coordinates and never mixes groups', async () => {
+ const f = await neutralMetadataRootFixture();
+ await fs.mkdir(path.join(f.directory, 'build', 'Assets'), { recursive: true });
+ await fs.writeFile(path.join(f.directory, 'build', 'Assets', 'logo.PNG'), 'conflicting payload');
+ const before = await snapshotTree(f.directory);
+ await expect(f.manager.complete(f.game, f.selected)).rejects.toThrow('matches both layout coordinates');
+ expect(await snapshotTree(f.directory)).toEqual(before);
+ expect(await f.manager.roots(f.game)).toEqual([]);
+});
+it('rejects a root tree whose groups only resolve under different coordinates', async () => {
+ const f = await installFixture();
+ await fs.mkdir(path.join(f.directory, 'payload'));
+ await fs.rename(path.join(f.directory, 'logo.PNG'), path.join(f.directory, 'payload', 'logo.PNG'));
+ await fs.mkdir(path.join(f.directory, 'extra'));
+ await fs.writeFile(path.join(f.directory, 'extra', 'value.dat'), 'source payload');
+ await fs.writeFile(path.join(f.directory, `layout_${GUID}.xml`), '<Package><Chunk><FileGroup SourcePath="." DestinationPath="\\" Include="MicrosoftGame.config"/><FileGroup SourcePath="build\\only" DestinationPath="\\payload" Include="*.png"/><FileGroup SourcePath="extra" DestinationPath="\\out" Include="*.dat"/></Chunk></Package>');
+ const before = await snapshotTree(f.directory);
+ await expect(f.manager.complete(f.game, f.selected)).rejects.toThrow('declared by the content layout is missing');
+ expect(await snapshotTree(f.directory)).toEqual(before);
+ expect(await f.manager.roots(f.game)).toEqual([]);
+});
+it('rejects a root destination tree with missing or ambiguous payload paths', async () => {
+ for (const [failure, reason] of [['missing', 'declared by the content layout is missing'], ['ambiguous', 'Missing or ambiguous content payload directory.']] as const) {
+ const f = await rootDestinationFixture();
+ if (failure === 'missing') await fs.unlink(path.join(f.directory, 'Assets', 'logo.PNG'));
+ if (failure === 'ambiguous') {
+ await fs.mkdir(path.join(f.directory, 'assets'));
+ await fs.writeFile(path.join(f.directory, 'assets', 'logo.PNG'), 'conflicting payload');
+ }
+ const before = await snapshotTree(f.directory);
+ await expect(f.manager.complete(f.game, f.selected)).rejects.toThrow(reason);
+ expect(await snapshotTree(f.directory)).toEqual(before);
+ expect(await f.manager.roots(f.game)).toEqual([]);
+ }
+});
+it('rejects a root tree whose declared metadata group is excluded from the layout', async () => {
+ const f = await rootDestinationFixture();
+ await fs.writeFile(path.join(f.directory, `layout_${GUID}.xml`), '<Package><Chunk><FileGroup SourcePath="build" DestinationPath="\\" Include="MicrosoftGame.config" Exclude="MicrosoftGame.Config"/><FileGroup SourcePath="build\\Assets" DestinationPath="\\Assets" Include="*.*"/></Chunk></Package>');
+ const before = await snapshotTree(f.directory);
+ await expect(f.manager.complete(f.game, f.selected)).rejects.toThrow('unambiguously mapped');
+ expect(await snapshotTree(f.directory)).toEqual(before);
+ expect(await f.manager.roots(f.game)).toEqual([]);
+});
+
async function installedWithoutDeclaredContent() {
const f = await installFixture();
await f.manager.complete(f.game, f.selected);
diff --git a/src/main/game-content.ts b/src/main/game-content.ts
index 4b362f0..964de4b 100644
--- a/src/main/game-content.ts
+++ b/src/main/game-content.ts
@@ -113,6 +113,60 @@ async function layoutPayload(directory: string, layout: FileGroupLayout, coordin
if ((await fs.lstat(path.join(folder, name))).isFile()) return 'ok';
return 'missing';
}
+/**
+ * Every declared group must resolve under the same coordinate; groups are never
+ * mixed. Returns the reported failure instead of throwing so that coordinate
+ * selection can compare whole-layout evidence first.
+ */
+async function layoutFailure(directory: string, layouts: FileGroupLayout[], coordinate: LayoutCoordinate): Promise<string | undefined> {
+ for (const layout of layouts) {
+ const result = await layoutPayload(directory, layout, coordinate);
+ if (result === 'ambiguous') return 'Missing or ambiguous content payload directory.';
+ if (result === 'unsafe') return 'Invalid content payload directory.';
+ if (result !== 'ok') return 'A file group declared by the content layout is missing.';
+ }
+ return undefined;
+}
+/**
+ * A downloaded tree can be extracted at either layout coordinate. The metadata
+ * copy that exists is authoritative (SRC when present, otherwise the root copy),
+ * and the whole layout must agree on one interpretation. Source-coordinate trees
+ * are deployable only when the metadata's declared destination is the package
+ * root that runtime discovery reads; destination-coordinate trees already sit at
+ * their deployment paths, including a package-relative \SRC.
+ *
+ * The metadata declaration alone can be coordinate-neutral (a source path equal
+ * to the deployed path), while the payload groups differ, so a neutral
+ * declaration is settled by whole-layout evidence. Two fully valid but distinct
+ * interpretations stay ambiguous and are rejected.
+ */
+async function layoutCoordinate(directory: string, layouts: FileGroupLayout[], configuration: ContentConfiguration): Promise<LayoutCoordinate> {
+ const configName = 'MicrosoftGame.config';
+ const physical = path.relative(directory, (configuration.source ?? configuration.root)!).split(path.sep).filter(Boolean);
+ const declarations = layouts.filter(group => {
+ if (!contentPattern(group.pattern).test(configName)) return false;
+ const relative = [...group.prefix, configName];
+ return (sameLayoutPath([...group.source, ...relative], physical) || sameLayoutPath([...group.destination, ...relative], physical))
+ && !layoutExcluded(group, relative);
+ });
+ if (declarations.length !== 1) throw Error('MicrosoftGame.config must be unambiguously mapped by the content layout.');
+ const declaration = declarations[0], relative = [...declaration.prefix, configName];
+ const sourcePath = [...declaration.source, ...relative], destinationPath = [...declaration.destination, ...relative];
+ const sourceCoordinate = sameLayoutPath(sourcePath, physical) && sameLayoutPath(destinationPath, [configName]);
+ const destinationCoordinate = sameLayoutPath(destinationPath, physical);
+ if (sourceCoordinate && destinationCoordinate) {
+ if (layouts.every(layout => sameLayoutPath(layout.source, layout.destination))) return 'source';
+ const sourceFailure = await layoutFailure(directory, layouts, 'source');
+ const destinationFailure = await layoutFailure(directory, layouts, 'destination');
+ if (!sourceFailure && !destinationFailure) throw Error('MicrosoftGame.config matches both layout coordinates.');
+ if (!sourceFailure) return 'source';
+ if (!destinationFailure) return 'destination';
+ throw Error(sourceFailure);
+ }
+ if (sourceCoordinate) return 'source';
+ if (destinationCoordinate) return 'destination';
+ throw Error('MicrosoftGame.config is not at a layout-declared location.');
+}
export function contentManifest(xml: string): Manifest {
const root = parsePackageXml(xml);
if (root.name.split(':').at(-1) !== 'Game') throw Error('Expected a Microsoft Game manifest.');
@@ -257,38 +311,9 @@ export class GameContent {
visit(layout, 0);
if (!groups.length || groups.length > 256) throw Error('Content layout contains no supported file groups.');
const fileGroups = layoutGroups(groups);
- let coordinate: LayoutCoordinate = 'source';
- if (configuration.source) {
- // A downloaded tree can be extracted at either layout coordinate. SRC
- // metadata is authoritative because the root copy is optional, then the
- // whole layout must agree on one interpretation. Source-coordinate trees
- // are deployable only when their declared destination is the package root
- // that runtime discovery reads; destination-coordinate trees already sit
- // at their deployment paths, including a package-relative \SRC.
- const configName = 'MicrosoftGame.config';
- const physical = path.relative(directory, configuration.source).split(path.sep).filter(Boolean);
- const declarations = fileGroups.filter(group => {
- if (!contentPattern(group.pattern).test(configName)) return false;
- const relative = [...group.prefix, configName];
- return (sameLayoutPath([...group.source, ...relative], physical) || sameLayoutPath([...group.destination, ...relative], physical))
- && !layoutExcluded(group, relative);
- });
- if (declarations.length !== 1) throw Error('MicrosoftGame.config must be unambiguously mapped by the content layout.');
- const declaration = declarations[0], relative = [...declaration.prefix, configName];
- const sourcePath = [...declaration.source, ...relative], destinationPath = [...declaration.destination, ...relative];
- // A source-coordinate tree can be registered only when its declared
- // deployment is the package root. Otherwise the tree must already sit at
- // its destination coordinates, including a package-relative \SRC.
- if (sameLayoutPath(sourcePath, physical) && sameLayoutPath(destinationPath, [configName])) coordinate = 'source';
- else if (sameLayoutPath(destinationPath, physical)) coordinate = 'destination';
- else throw Error('MicrosoftGame.config is not at a layout-declared location.');
- }
- for (const group of fileGroups) {
- const result = await layoutPayload(directory, group, coordinate);
- if (result === 'ambiguous') throw Error('Missing or ambiguous content payload directory.');
- if (result === 'unsafe') throw Error('Invalid content payload directory.');
- if (result !== 'ok') throw Error('A file group declared by the content layout is missing.');
- }
+ const coordinate = await layoutCoordinate(directory, fileGroups, configuration);
+ const failure = await layoutFailure(directory, fileGroups, coordinate);
+ if (failure) throw Error(failure);
return { ...content, configuration };
}
async plan(game: Game, readAccess: () => Promise<{ checkedAt: string; accessByProductId: Access }>, market = 'US'): Promise<{ plan: GameContentPlan; packages: ContentPackage[] }> {