Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 34 additions & 18 deletions src/main/services/emulators/extract-disc-sku.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,7 @@ const extractCsoSku = async (filePath: string): Promise<string | null> => {
return sku;
};

const extractPs12Sku = async (filePath: string): Promise<string | null> => {
const lower = filePath.toLowerCase();
if (lower.endsWith(".chd")) {
return extractChdSku(filePath);
}
if (lower.endsWith(".cso")) {
return extractCsoSku(filePath);
}

const target = await resolveSniffTarget(filePath);
logger.log("[extract-sku] start", { filePath, target });
if (!target) {
logger.log("[extract-sku] no sniff target (unsupported format)", {
filePath,
});
return null;
}

const scanTargetForSku = async (target: string): Promise<string | null> => {
let fh: import("node:fs/promises").FileHandle | null = null;
try {
fh = await fs.open(target, "r");
Expand Down Expand Up @@ -143,6 +126,39 @@ const extractPs12Sku = async (filePath: string): Promise<string | null> => {
}
};

const extractPs12Sku = async (filePath: string): Promise<string | null> => {
const lower = filePath.toLowerCase();
if (lower.endsWith(".chd")) {
return extractChdSku(filePath);
}
if (lower.endsWith(".cso")) {
return extractCsoSku(filePath);
}

const target = await resolveSniffTarget(filePath);
logger.log("[extract-sku] start", { filePath, target });
if (!target) {
logger.log("[extract-sku] no sniff target (unsupported format)", {
filePath,
});
return null;
}

const targetExists = await fs
.access(target)
.then(() => true)
.catch(() => false);
if (!targetExists) {
logger.warn("[extract-sku] sniff target missing on disk", {
filePath,
target,
});
return null;
}

return scanTargetForSku(target);
};

const findKeyOffset = (
data: Buffer,
keyTableStart: number,
Expand Down
29 changes: 28 additions & 1 deletion src/main/services/emulators/sniff-disc-platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,41 @@ export const sniffDiscImage = async (
}
};

const resolveCueRef = async (
dir: string,
ref: string
): Promise<string | null> => {
const base = path.basename(ref.replaceAll("\\", "/"));
const resolved = path.resolve(dir, base);

try {
await fs.access(resolved);
return resolved;
} catch {
try {
const entries = await fs.readdir(dir);
const match = entries.find(
(entry) => entry.toLowerCase() === base.toLowerCase()
);
if (match) return path.resolve(dir, match);
} catch {
return null;
}
return null;
}
Comment thread
greptile-apps[bot] marked this conversation as resolved.
};

export const parseCueReferencedFiles = async (
cuePath: string
): Promise<string[]> => {
try {
const content = await fs.readFile(cuePath, "utf-8");
const dir = path.dirname(cuePath);
const matches = [...content.matchAll(/FILE\s+"(.+?)"\s+\w+/gi)];
return matches.map((m) => path.resolve(dir, m[1]));
const resolved = await Promise.all(
matches.map((m) => resolveCueRef(dir, m[1]))
);
return resolved.filter((p): p is string => p !== null);
} catch {
return [];
}
Expand Down
Loading