Files
WT-Offline/apps/launcher/src/main/services/game-folder/inspect-target-folder.ts
T
N0VAN0M4LY d06989622f Inspect the folders the player chooses for installation
The install wizard needs to tell whether the chosen folder is a War
Thunder installation, which version it is, whether it is a Steam
library, and whether the folder for the copy can take it.

The patcher reads the game version from win64/aces.exe (the
VS_FIXEDFILEINFO block of its version resource) and works out the
versions every exe and GUI patch was verified on, from their
verified_on lists, so the version check follows the patch definitions
instead of a constant.

The launcher gets four IPC calls, each validated against the contract:
- dialog:pickFolder, a folder picker titled by the window;
- install:inspectGameFolder: missing game files, version, verified
  versions, Steam library;
- install:inspectTargetFolder: new or empty, not overlapping the stock
  installation, Steam library, free space on its drive, and whether it
  is on the stock installation's drive (the space saving copy needs
  that);
- install:measureFolder: bytes and files of the stock installation,
  regular files only, links not followed.
A folder that does not exist or cannot be read is the folder_unreadable
error code, translated in both languages. In development the patch
folders and the hook library come from this repository; the packaged
layout comes with the installer build.

Checked with both typechecks, the launcher build, and the calls made
from a running launcher window: the stock folder (2.61.0.37, verified),
a non-game folder, a missing path, a Steam library path, four target
folders (new, inside the stock folder, not empty, other drive) and the
size of the offline copy (96.9 GB in 10,972 files).
2026-11-29 03:36:00 +08:00

64 lines
2.4 KiB
TypeScript

import { readdir, realpath, stat, statfs } from 'node:fs/promises';
import { dirname, parse, relative, resolve, isAbsolute } from 'node:path';
import { isInSteamLibrary } from '@wto/patcher';
import type { TargetFolderInspection } from '@shared/schemas/install.ts';
/**
* Look at the folder the player chose for the game copy (install by copying): it must be new or
* empty, must not overlap the stock installation, and the drive must have room. Only reads.
*/
export async function inspectTargetFolder(path: string, stockPath: string): Promise<TargetFolderInspection> {
const target = await canonical(path);
const stock = await canonical(stockPath);
return {
path,
problem: await targetProblem(target, stock),
inSteamLibrary: isInSteamLibrary(target),
freeBytes: await freeBytes(target),
sameDriveAsStock: parse(target).root.toLowerCase() === parse(stock).root.toLowerCase(),
};
}
async function targetProblem(target: string, stock: string): Promise<TargetFolderInspection['problem']> {
if (contains(stock, target) || contains(target, stock)) return 'overlaps_stock';
let entries: string[];
try {
const info = await stat(target);
if (!info.isDirectory()) return 'not_a_folder';
entries = await readdir(target);
} catch {
return null; // does not exist yet: the copy creates it
}
return entries.length ? 'not_empty' : null;
}
/** Whether `inner` is `outer` or lies inside it. */
function contains(outer: string, inner: string): boolean {
const path = relative(outer.toLowerCase(), inner.toLowerCase());
return path === '' || (!path.startsWith('..') && !isAbsolute(path));
}
/** Free bytes on the drive of the nearest existing folder at or above `path`. */
async function freeBytes(path: string): Promise<number | null> {
for (let folder = path; ; folder = dirname(folder)) {
try {
const info = await statfs(folder);
return info.bavail * info.bsize;
} catch {
if (dirname(folder) === folder) return null;
}
}
}
/** The real path of the nearest existing part, with the rest appended as given. */
async function canonical(path: string): Promise<string> {
const absolute = resolve(path);
try {
return await realpath(absolute);
} catch {
const parent = dirname(absolute);
if (parent === absolute) return absolute;
return resolve(await canonical(parent), relative(parent, absolute));
}
}