253 lines
8.3 KiB
TypeScript
253 lines
8.3 KiB
TypeScript
/**
|
|
* Discover installed mods inside the global mods directory and inspect their capabilities
|
|
* (server tuning/script, client pack patches, GUI patches, declarative exe patches,
|
|
* 3D model addons, and optional companion files).
|
|
*/
|
|
import { existsSync, readdirSync, statSync } from 'node:fs';
|
|
import { join, relative } from 'node:path';
|
|
import { MOD_MANIFEST_FILE, readModManifest } from './manifest.ts';
|
|
import {
|
|
MODDABLE_CLIENT_PACKS,
|
|
type DiscoveredMod,
|
|
type ModContentSummary,
|
|
type ModDiscoveryError,
|
|
type ModDiscoveryResult,
|
|
type ModManifest,
|
|
type ModPackOperation,
|
|
type ModRiskBadge,
|
|
type ModdableClientPack,
|
|
} from './types.ts';
|
|
|
|
const DEFAULT_SERVER_ENTRIES = ['server/index.ts', 'server/index.mjs', 'server/index.js'] as const;
|
|
const DEFAULT_SERVER_TUNING_DIR = 'server/tuning';
|
|
const BLK_PATCH_SUFFIX = '.patch.jsonc';
|
|
const CSV_PATCH_SUFFIX = '.patch.csv';
|
|
const CONFIG_BLK_PATCH_PATH = 'client/config-blk.patch.jsonc';
|
|
|
|
export function discoverMods(modsRootDir: string): ModDiscoveryResult {
|
|
if (!existsSync(modsRootDir)) return { mods: [], errors: [] };
|
|
|
|
const mods: DiscoveredMod[] = [];
|
|
const errors: ModDiscoveryError[] = [];
|
|
const seenIds = new Map<string, string>();
|
|
|
|
let entries;
|
|
try {
|
|
entries = readdirSync(modsRootDir, { withFileTypes: true });
|
|
} catch (error) {
|
|
return {
|
|
mods: [],
|
|
errors: [{ folderName: '.', rootDir: modsRootDir, reason: error instanceof Error ? error.message : String(error) }],
|
|
};
|
|
}
|
|
|
|
for (const entry of entries) {
|
|
if (!entry.isDirectory()) continue;
|
|
const rootDir = join(modsRootDir, entry.name);
|
|
if (!existsSync(join(rootDir, MOD_MANIFEST_FILE))) continue;
|
|
try {
|
|
const manifest = readModManifest(rootDir);
|
|
const duplicateFolder = seenIds.get(manifest.id);
|
|
if (duplicateFolder) {
|
|
errors.push({
|
|
folderName: entry.name,
|
|
rootDir,
|
|
reason: `duplicate mod id "${manifest.id}" (already provided by folder "${duplicateFolder}")`,
|
|
});
|
|
continue;
|
|
}
|
|
seenIds.set(manifest.id, entry.name);
|
|
const content = inspectModContent(rootDir, manifest);
|
|
mods.push({ folderName: entry.name, rootDir, manifest, content });
|
|
} catch (error) {
|
|
errors.push({
|
|
folderName: entry.name,
|
|
rootDir,
|
|
reason: error instanceof Error ? error.message : String(error),
|
|
});
|
|
}
|
|
}
|
|
|
|
mods.sort((left, right) => left.manifest.id.localeCompare(right.manifest.id));
|
|
errors.sort((left, right) => left.folderName.localeCompare(right.folderName));
|
|
return { mods, errors };
|
|
}
|
|
|
|
export function inspectModContent(rootDir: string, manifest: ModManifest): ModContentSummary {
|
|
const serverEntry = resolveServerEntry(rootDir, manifest);
|
|
const tuningDirRel = manifest.server?.tuning ?? DEFAULT_SERVER_TUNING_DIR;
|
|
const serverTuningFiles = listTuningFiles(rootDir, tuningDirRel);
|
|
const packOperations = scanClientPackOperations(rootDir);
|
|
const guiPatchDirs = scanPatchDirectories(rootDir, 'client/gui-patches');
|
|
const exePatchDirs = scanPatchDirectories(rootDir, 'client/exe-patches');
|
|
|
|
const addonDir = join(rootDir, 'client', 'addon');
|
|
const addonRelativeFiles = existsSync(addonDir) && isDirectory(addonDir)
|
|
? listRelativeFiles(addonDir)
|
|
: [];
|
|
const hasClientAddon = addonRelativeFiles.length > 0;
|
|
const addonHasGrpHdr = addonRelativeFiles.some((file) => file.toLowerCase() === 'res/grp_hdr.vromfs.bin');
|
|
|
|
const filesDir = join(rootDir, 'client', 'files');
|
|
const clientFiles = existsSync(filesDir) && isDirectory(filesDir)
|
|
? listRelativeFiles(filesDir)
|
|
: [];
|
|
|
|
const configBlkPatch = isFile(join(rootDir, ...CONFIG_BLK_PATCH_PATH.split('/')))
|
|
? CONFIG_BLK_PATCH_PATH
|
|
: undefined;
|
|
|
|
const badges = computeRiskBadges({
|
|
hasData: serverTuningFiles.length > 0 || packOperations.length > 0 || hasClientAddon || clientFiles.length > 0 || configBlkPatch !== undefined,
|
|
hasGui: guiPatchDirs.length > 0,
|
|
hasScript: serverEntry !== undefined,
|
|
hasExe: exePatchDirs.length > 0,
|
|
});
|
|
|
|
return {
|
|
...(serverEntry !== undefined ? { serverEntry } : {}),
|
|
serverTuningFiles,
|
|
packOperations,
|
|
guiPatchDirs,
|
|
exePatchDirs,
|
|
hasClientAddon,
|
|
addonHasGrpHdr,
|
|
addonRelativeFiles,
|
|
clientFiles,
|
|
...(configBlkPatch !== undefined ? { configBlkPatch } : {}),
|
|
badges,
|
|
};
|
|
}
|
|
|
|
function resolveServerEntry(rootDir: string, manifest: ModManifest): string | undefined {
|
|
if (manifest.server?.entry) {
|
|
const full = join(rootDir, ...manifest.server.entry.split('/'));
|
|
if (!isFile(full)) {
|
|
throw new Error(`declared server.entry "${manifest.server.entry}" does not exist`);
|
|
}
|
|
return manifest.server.entry;
|
|
}
|
|
for (const candidate of DEFAULT_SERVER_ENTRIES) {
|
|
if (isFile(join(rootDir, ...candidate.split('/')))) return candidate;
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
function listTuningFiles(rootDir: string, tuningDirRel: string): string[] {
|
|
const dir = join(rootDir, ...tuningDirRel.split('/'));
|
|
if (!isDirectory(dir)) return [];
|
|
return readdirSync(dir, { withFileTypes: true })
|
|
.filter((entry) => entry.isFile() && entry.name.endsWith('.jsonc'))
|
|
.map((entry) => entry.name.slice(0, -'.jsonc'.length))
|
|
.sort();
|
|
}
|
|
|
|
function scanClientPackOperations(rootDir: string): ModPackOperation[] {
|
|
const packsRoot = join(rootDir, 'client', 'packs');
|
|
if (!isDirectory(packsRoot)) return [];
|
|
|
|
const ops: ModPackOperation[] = [];
|
|
for (const pack of MODDABLE_CLIENT_PACKS) {
|
|
const packDir = join(packsRoot, ...pack.split('/'));
|
|
if (!isDirectory(packDir)) continue;
|
|
for (const rel of listRelativeFiles(packDir)) {
|
|
const sourceRelativePath = `client/packs/${pack}/${rel}`;
|
|
if (rel.toLowerCase().endsWith(BLK_PATCH_SUFFIX)) {
|
|
ops.push({
|
|
pack,
|
|
targetPath: rel.slice(0, -BLK_PATCH_SUFFIX.length),
|
|
action: 'blk-patch',
|
|
sourceRelativePath,
|
|
});
|
|
} else if (rel.toLowerCase().endsWith(CSV_PATCH_SUFFIX)) {
|
|
ops.push({
|
|
pack,
|
|
targetPath: rel.slice(0, -CSV_PATCH_SUFFIX.length),
|
|
action: 'csv-patch',
|
|
sourceRelativePath,
|
|
});
|
|
} else {
|
|
ops.push({
|
|
pack,
|
|
targetPath: rel,
|
|
action: 'file',
|
|
sourceRelativePath,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
return ops;
|
|
}
|
|
|
|
function scanPatchDirectories(rootDir: string, subPath: string): string[] {
|
|
const base = join(rootDir, ...subPath.split('/'));
|
|
if (!isDirectory(base)) return [];
|
|
const results: string[] = [];
|
|
const walk = (current: string) => {
|
|
for (const entry of readdirSync(current, { withFileTypes: true })) {
|
|
if (!entry.isDirectory()) continue;
|
|
const full = join(current, entry.name);
|
|
if (isFile(join(full, 'patch.jsonc'))) {
|
|
results.push(relative(rootDir, full).replaceAll('\\', '/'));
|
|
} else {
|
|
walk(full);
|
|
}
|
|
}
|
|
};
|
|
walk(base);
|
|
return results.sort();
|
|
}
|
|
|
|
function listRelativeFiles(dir: string): string[] {
|
|
const out: string[] = [];
|
|
const walk = (current: string) => {
|
|
for (const entry of readdirSync(current, { withFileTypes: true })) {
|
|
const full = join(current, entry.name);
|
|
if (entry.isDirectory()) walk(full);
|
|
else if (entry.isFile()) out.push(relative(dir, full).replaceAll('\\', '/'));
|
|
}
|
|
};
|
|
walk(dir);
|
|
return out.sort();
|
|
}
|
|
|
|
function computeRiskBadges(flags: {
|
|
readonly hasData: boolean;
|
|
readonly hasGui: boolean;
|
|
readonly hasScript: boolean;
|
|
readonly hasExe: boolean;
|
|
}): ModRiskBadge[] {
|
|
const badges: ModRiskBadge[] = [];
|
|
if (flags.hasData || (!flags.hasGui && !flags.hasScript && !flags.hasExe)) badges.push('data');
|
|
if (flags.hasGui) badges.push('gui');
|
|
if (flags.hasScript) badges.push('script');
|
|
if (flags.hasExe) badges.push('exe');
|
|
return badges;
|
|
}
|
|
|
|
function isFile(path: string): boolean {
|
|
try {
|
|
return statSync(path).isFile();
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function isDirectory(path: string): boolean {
|
|
try {
|
|
return statSync(path).isDirectory();
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export function touchedClientPacks(mods: readonly DiscoveredMod[]): ModdableClientPack[] {
|
|
const set = new Set<ModdableClientPack>();
|
|
for (const mod of mods) {
|
|
for (const op of mod.content.packOperations) set.add(op.pack);
|
|
if (mod.content.guiPatchDirs.length > 0) set.add('gui.vromfs.bin');
|
|
if (mod.content.hasClientAddon) set.add('aces.vromfs.bin');
|
|
}
|
|
return MODDABLE_CLIENT_PACKS.filter((pack) => set.has(pack));
|
|
}
|