feat(studio): add nitro cleanup tool for fake and broken bundles
CI / check (push) Failing after 1m22s
CI / preflight (push) Skipped
CI / deploy (push) Skipped

This commit is contained in:
openhands committed 2026-09-15 20:54:10 +02:00
1 parent ce93b92a81
commit 694f87d98c
6 files changed
+869

No files matched your search

+228
View File
@@ -0,0 +1,228 @@
import { existsSync, promises as fs } from "node:fs";
import { sql } from "drizzle-orm";
import { db } from "@/lib/db";
import { getBaseClassname, getSafeClassnameStem } from "@/lib/furni/classname";
import { getFurniAssetWriteTargets } from "@/lib/services/furni-asset-dirs";
import { parseNitroBundle } from "@/lib/services/swf/nitro-builder";
import { getRuntimePath } from "@/lib/utils/runtime-path";
/**
* Catalog Studio nitro cleanup.
*
* Scans every .nitro directory (public + mirrored gamedata) and splits the
* found bundles into two removable groups:
*
* - "fake" .nitro files whose stem does not match any item in `items_base`
* (leftover files that belong to no furniture — including items that were
* deleted or imported under a different name);
* - "broken" .nitro files that do belong to an item but cannot be parsed as a
* valid Nitro bundle (truncated, corrupted or renamed files).
*
* Deletion only touches the .nitro files on disk — never database rows.
*/
export interface NitroCleanupEntry {
fileName: string;
base: string;
size: number;
dirs: string[];
error?: string;
}
export interface NitroCleanupScan {
fake: NitroCleanupEntry[];
broken: NitroCleanupEntry[];
total: number;
}
export interface NitroCleanupDeleteResult {
/** Files that were fully removed from every nitro directory. */
deleted: number;
/** Individual .nitro files removed across all directories (mirrors). */
copiesRemoved: number;
errors: string[];
files: Array<{ fileName: string; deleted: boolean; remaining: string[] }>;
}
const NITRO_FILE_RE = /^[a-z0-9_*\-.]+\.nitro$/i;
function uniqueDirs(dirs: string[]): string[] {
const seen = new Set<string>();
const out: string[] = [];
for (const dir of dirs) {
const key = process.platform === "win32" ? dir.toLowerCase() : dir;
if (seen.has(key)) continue;
seen.add(key);
out.push(dir);
}
return out;
}
/** Sliding-window worker pool: keeps up to `concurrency` tasks in flight. */
async function runPool<T>(
items: T[],
concurrency: number,
worker: (item: T) => Promise<void>,
): Promise<void> {
const limit = Math.min(Math.max(concurrency, 1), items.length);
if (limit === 0) return;
let next = 0;
await Promise.allSettled(
Array.from({ length: limit }, async () => {
while (true) {
const i = next++;
if (i >= items.length) break;
await worker(items[i]);
}
}),
);
}
/** Register every .nitro file stem a classname could legitimately use. */
function addStems(set: Set<string>, classname: string): void {
const normalized = classname.trim().toLowerCase();
if (!normalized) return;
const base = getBaseClassname(normalized).toLowerCase();
if (base) set.add(base);
if (normalized.includes("*")) {
const safe = getSafeClassnameStem(normalized).toLowerCase();
if (safe) set.add(safe);
}
}
export async function scanFakeBrokenNitros(): Promise<NitroCleanupScan> {
const targets = await getFurniAssetWriteTargets();
const nitroDirs = uniqueDirs([
targets.nitroDir,
...targets.mirrorDirs.map((d) => d.nitroDir),
]);
// name → dirs + largest size seen anywhere.
const byName = new Map<string, { dirs: string[]; size: number }>();
for (const dir of nitroDirs) {
const names = await fs.readdir(dir).catch(() => [] as string[]);
for (const name of names) {
if (!NITRO_FILE_RE.test(name)) continue;
const info = byName.get(name) ?? { dirs: [], size: 0 };
info.dirs.push(dir);
try {
const stat = await fs.stat(getRuntimePath(dir, name));
info.size = Math.max(info.size, stat.size);
} catch {
/* ignore unreadable entry */
}
byName.set(name, info);
}
}
// Authoritative stem set from items_base (item_name + public_name
// fallback), mirroring repair-nitros.ts classname resolution.
let rows: Array<{ item_name: string; public_name: string }> = [];
try {
const [result] = (await db.execute(sql`
SELECT item_name, public_name FROM items_base
`)) as unknown as [
Array<{ item_name: string; public_name: string }>,
unknown,
];
rows = result;
} catch {
/* empty set → every .nitro is treated as fake */
}
const validStems = new Set<string>();
for (const row of rows) {
addStems(validStems, row.item_name);
if (
row.public_name &&
row.public_name !== row.item_name &&
!row.public_name.includes(" ") &&
row.public_name !== "0"
) {
addStems(validStems, row.public_name);
}
}
const entries: NitroCleanupEntry[] = [...byName].map(([fileName, info]) => ({
fileName,
base: fileName.slice(0, -".nitro".length),
size: info.size,
dirs: info.dirs,
}));
const fake: NitroCleanupEntry[] = [];
const broken: NitroCleanupEntry[] = [];
await runPool(entries, 8, async (entry) => {
// A .nitro that no DB item maps to is a leftover / fake bundle.
if (!validStems.has(entry.base.toLowerCase())) {
fake.push(entry);
return;
}
// Otherwise validate the bundle content.
let parsed = false;
let firstError = "";
for (const dir of entry.dirs) {
try {
parseNitroBundle(
await fs.readFile(getRuntimePath(dir, entry.fileName)),
);
parsed = true;
break;
} catch (err) {
firstError = (err as Error).message;
}
}
if (!parsed) {
broken.push({ ...entry, error: firstError });
}
});
fake.sort((a, b) => a.fileName.localeCompare(b.fileName));
broken.sort((a, b) => a.fileName.localeCompare(b.fileName));
return { fake, broken, total: byName.size };
}
/** Delete .nitro files from every nitro directory (primary + mirrors). */
export async function deleteNitroCleanupFiles(
fileNames: string[],
): Promise<NitroCleanupDeleteResult> {
const targets = await getFurniAssetWriteTargets();
const nitroDirs = uniqueDirs([
targets.nitroDir,
...targets.mirrorDirs.map((d) => d.nitroDir),
]);
let deleted = 0;
let copiesRemoved = 0;
const errors: string[] = [];
const files: NitroCleanupDeleteResult["files"] = [];
for (const rawName of [...new Set(fileNames)]) {
const fileName = String(rawName ?? "");
if (!NITRO_FILE_RE.test(fileName)) {
errors.push(`Invalid nitro file name: ${fileName}`);
files.push({ fileName, deleted: false, remaining: [] });
continue;
}
const remaining: string[] = [];
let existed = false;
for (const dir of nitroDirs) {
const filePath = getRuntimePath(dir, fileName);
if (!existsSync(filePath)) continue;
existed = true;
try {
await fs.unlink(filePath);
copiesRemoved++;
} catch (err) {
remaining.push(dir);
errors.push(`${fileName} in ${dir}: ${(err as Error).message}`);
}
}
const fullyDeleted = existed && remaining.length === 0;
if (fullyDeleted) deleted++;
files.push({ fileName, deleted: fullyDeleted, remaining });
}
return { deleted, copiesRemoved, errors, files };
}