refactor(desktop): own classification collections once (#16652)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-12-04 16:22:54 +08:00
1 parent 237b0d9329
commit 65a98e09b5
1 file changed
+55 -85
+55 -85
View File
@@ -110,21 +110,23 @@ const mainExecutables = (files: readonly DesktopFile[]): DesktopFile[] => {
};
const classifyDesktopFiles = (files: readonly DesktopFile[]) => {
const managed: DesktopFile[] = [];
const mono: DesktopFile[] = [];
const monoDirectories = new Set<string>();
const managedIl2cppBackup: DesktopFile[] = [];
const metadata: DesktopFile[] = [];
const supportedMetadata: DesktopFile[] = [];
const il2cpp: DesktopFile[] = [];
const godot: DesktopFile[] = [];
const unityfs: DesktopFile[] = [];
const gamemaker: DesktopFile[] = [];
const unreal: DesktopFile[] = [];
const electron: DesktopFile[] = [];
const electronManifests: DesktopFile[] = [];
const directories = new Map<string, DesktopFile>();
const markerBasis: DesktopFile[][] = COMPONENT_MARKERS.map(() => []);
const classified = {
managed: [] as DesktopFile[],
mono: [] as DesktopFile[],
monoDirectories: new Set<string>(),
managedIl2cppBackup: [] as DesktopFile[],
metadata: [] as DesktopFile[],
supportedMetadata: [] as DesktopFile[],
il2cpp: [] as DesktopFile[],
godot: [] as DesktopFile[],
unityfs: [] as DesktopFile[],
gamemaker: [] as DesktopFile[],
unreal: [] as DesktopFile[],
electron: [] as DesktopFile[],
electronManifests: [] as DesktopFile[],
directories: new Map<string, DesktopFile>(),
markerBasis: COMPONENT_MARKERS.map((): DesktopFile[] => []),
};
for (const file of files) {
let hasCli = false;
@@ -140,7 +142,7 @@ const classifyDesktopFiles = (files: readonly DesktopFile[]) => {
let hasUnreal = false;
let hasElectron = false;
let hasElectronManifest = false;
if (file.kind === "directory") directories.set(file.path, file);
if (file.kind === "directory") classified.directories.set(file.path, file);
for (const fact of file.facts) {
switch (fact.kind) {
@@ -189,48 +191,32 @@ const classifyDesktopFiles = (files: readonly DesktopFile[]) => {
}
if (hasCli) {
managed.push(file);
classified.managed.push(file);
if (hasUnityRef) {
mono.push(file);
classified.mono.push(file);
for (const fact of file.facts)
if (fact.kind === "unity-assembly-reference")
monoDirectories.add(fact.value.managed_directory);
classified.monoDirectories.add(fact.value.managed_directory);
}
if (hasIl2cppBackup) managedIl2cppBackup.push(file);
if (hasIl2cppBackup) classified.managedIl2cppBackup.push(file);
}
if (hasMetadata) {
metadata.push(file);
if (hasSupportedMetadata) supportedMetadata.push(file);
classified.metadata.push(file);
if (hasSupportedMetadata) classified.supportedMetadata.push(file);
}
if (hasIl2cppLib && hasNativeImage) il2cpp.push(file);
if (hasGodot) godot.push(file);
if (hasUnityfs) unityfs.push(file);
if (hasGamemaker) gamemaker.push(file);
if (hasUnreal) unreal.push(file);
if (hasElectron) electron.push(file);
if (hasElectronManifest) electronManifests.push(file);
if (hasIl2cppLib && hasNativeImage) classified.il2cpp.push(file);
if (hasGodot) classified.godot.push(file);
if (hasUnityfs) classified.unityfs.push(file);
if (hasGamemaker) classified.gamemaker.push(file);
if (hasUnreal) classified.unreal.push(file);
if (hasElectron) classified.electron.push(file);
if (hasElectronManifest) classified.electronManifests.push(file);
for (let i = 0; i < COMPONENT_MARKERS.length; i++)
if (COMPONENT_MARKERS[i]!.expression.test(file.path))
markerBasis[i]!.push(file);
classified.markerBasis[i]!.push(file);
}
return {
managed,
mono,
monoDirectories,
managedIl2cppBackup,
metadata,
supportedMetadata,
il2cpp,
godot,
unityfs,
gamemaker,
unreal,
electron,
electronManifests,
directories,
markerBasis,
};
return classified;
};
/** Interpret only the facts admitted by the directory reader, never target bytes. */
@@ -283,34 +269,18 @@ export const projectDesktopApplication = (
probes.set(probe_id, { probe_id, ...semantic });
};
const {
managed,
mono,
monoDirectories,
managedIl2cppBackup,
metadata,
supportedMetadata,
il2cpp,
godot,
unityfs,
gamemaker,
unreal,
electron,
electronManifests,
directories,
markerBasis,
} = classifyDesktopFiles(files);
const classified = classifyDesktopFiles(files);
candidate(
"electron",
[...electron, ...electronManifests],
[...classified.electron, ...classified.electronManifests],
"An app.asar, or an app folder with a package.json, in a resources directory suggests an Electron application; archive validity and execution are unverified.",
"inferred",
);
// The Electron application is the release's own code. Its probes come
// first, so the stable priority sort keeps them ahead of bundled helpers,
// including a managed helper that ships its own .runtimeconfig.json.
for (const file of electron) {
for (const file of classified.electron) {
probe(
"open_binary",
file,
@@ -324,8 +294,8 @@ export const projectDesktopApplication = (
"application",
);
}
for (const manifest of electronManifests) {
const folder = directories.get(directoryOf(manifest.path));
for (const manifest of classified.electronManifests) {
const folder = classified.directories.get(directoryOf(manifest.path));
if (folder !== undefined)
probe(
"analyze_javascript_application",
@@ -337,16 +307,16 @@ export const projectDesktopApplication = (
candidate(
"dotnet",
managed,
classified.managed,
"PE optional headers declare nonempty CLI directories; CLR execution and metadata validity remain unverified.",
);
if (monoDirectories.size > 0) {
for (const file of managed) {
if (classified.monoDirectories.size > 0) {
for (const file of classified.managed) {
const conventions = desktopPathConventions(file.path);
if (
conventions.unityPredefinedAssembly &&
conventions.unityManagedDirectory !== null &&
monoDirectories.has(conventions.unityManagedDirectory)
classified.monoDirectories.has(conventions.unityManagedDirectory)
)
probe(
"inspect_managed_members",
@@ -356,7 +326,7 @@ export const projectDesktopApplication = (
);
}
}
for (const file of managedIl2cppBackup)
for (const file of classified.managedIl2cppBackup)
probe(
"inspect_managed_members",
file,
@@ -368,7 +338,7 @@ export const projectDesktopApplication = (
ownStems.has(
`${directoryOf(file.path).toLowerCase()}/${stemOf(baseName(file.path)).toLowerCase()}`,
);
for (const file of [...managed].sort(
for (const file of [...classified.managed].sort(
(left, right) => Number(isOwn(right)) - Number(isOwn(left)),
)) {
if (isOwn(file)) {
@@ -406,23 +376,23 @@ export const projectDesktopApplication = (
}
candidate(
"unity-mono",
mono,
classified.mono,
"Managed assemblies in a Unity Data/Managed layout reference UnityEngine assemblies.",
);
for (const file of supportedMetadata)
for (const file of classified.supportedMetadata)
probe(
"inspect_il2cpp_metadata",
file,
"Inventory this supported-version metadata candidate; complete validity and native binary correspondence remain unverified.",
);
if (metadata.length > 0 && il2cpp.length > 0) {
if (classified.metadata.length > 0 && classified.il2cpp.length > 0) {
candidate(
"unity-il2cpp",
[...metadata, ...il2cpp],
[...classified.metadata, ...classified.il2cpp],
"IL2CPP metadata magic and conventionally named native libraries coexist; metadata validity and build correspondence are unverified.",
"inferred",
);
for (const file of il2cpp)
for (const file of classified.il2cpp)
probe(
"open_binary",
file,
@@ -431,17 +401,17 @@ export const projectDesktopApplication = (
}
candidate(
"godot",
godot,
classified.godot,
"A standalone or trailer-located Godot pack header was observed; pack contents and encryption are unexamined.",
);
for (const file of godot)
for (const file of classified.godot)
probe(
"open_binary",
file,
"Select this Godot pack carrier with open_binary, then call inspect_artifact to inventory its members, including a pack embedded in an executable's trailer; encrypted packs report their members as unavailable.",
"application",
);
for (const file of unityfs)
for (const file of classified.unityfs)
probe(
"open_binary",
file,
@@ -449,12 +419,12 @@ export const projectDesktopApplication = (
);
candidate(
"gamemaker",
gamemaker,
classified.gamemaker,
"A FORM container begins with a GEN8 chunk; its game resources remain unexamined.",
);
candidate(
"unreal",
unreal,
classified.unreal,
"Unreal Content/Paks paths or an IoStore TOC header suggest an Unreal package layout.",
"inferred",
);
@@ -501,7 +471,7 @@ export const projectDesktopApplication = (
}
}
for (let i = 0; i < COMPONENT_MARKERS.length; i++) {
const basis = markerBasis[i]!;
const basis = classified.markerBasis[i]!;
if (basis.length === 0) continue;
const marker = COMPONENT_MARKERS[i]!;
const semantic = {