fix:多余文件解压

This commit is contained in:
Tianpao
2026-02-09 21:51:34 +08:00
parent 9309303036
commit ccf286ad54
2 changed files with 81 additions and 84 deletions

View File

@@ -43,7 +43,7 @@ export class Dex {
await Promise.all([ await Promise.all([
zps._unzip(mpname), zps._unzip(mpname),
platform(plat).downloadfile(info, unpath, this.message) platform(plat).downloadfile(info, unpath, this.message)
]).catch(e=>{ ]).catch(e => {
console.log(e); console.log(e);
}); });
this.message.statusChange(); //改变状态 this.message.statusChange(); //改变状态
@@ -182,30 +182,45 @@ export class Dex {
for await (const entry of zip) { for await (const entry of zip) {
const isDir = entry.fileName.endsWith("/"); const isDir = entry.fileName.endsWith("/");
logger.info(`index: ${index}, fileName: ${entry.fileName}`); logger.info(`index: ${index}, fileName: ${entry.fileName}`);
if (isDir) {
if (this._ublack(entry.fileName)) { // 只解压 overrides/ 目录下的内容,跳过其他所有文件和目录
if (!entry.fileName.startsWith("overrides/")) {
logger.info("Skip non-overrides file", entry.fileName);
this.message.unzip(entry.fileName, zip.length, index); this.message.unzip(entry.fileName, zip.length, index);
index++; index++;
continue; continue;
} }
await fs.promises.mkdir(`${instancePath}/${entry.fileName}`, {
recursive: true, // 跳过 overrides 目录本身
}); if (entry.fileName === "overrides/") {
} else if (entry.fileName.startsWith("overrides/")) { logger.info("Skip overrides directory", entry.fileName);
// 跳过黑名单文件 this.message.unzip(entry.fileName, zip.length, index);
index++;
continue;
}
// 跳过黑名单文件/目录
if (this._ublack(entry.fileName)) { if (this._ublack(entry.fileName)) {
logger.info("Skip blacklist file", entry.fileName); logger.info("Skip blacklist file", entry.fileName);
this.message.unzip(entry.fileName, zip.length, index); this.message.unzip(entry.fileName, zip.length, index);
index++; index++;
continue; continue;
} }
if (isDir) {
let targetPath = entry.fileName.replace("overrides/", "");
await fs.promises.mkdir(`${instancePath}/${targetPath}`, {
recursive: true,
});
} else {
let targetPath = entry.fileName.replace("overrides/", "");
// 创建目标目录 // 创建目标目录
const targetPath = entry.fileName.replace("overrides/", "");
const dirPath = `${instancePath}/${targetPath.substring(0, targetPath.lastIndexOf("/"))}`; const dirPath = `${instancePath}/${targetPath.substring(0, targetPath.lastIndexOf("/"))}`;
await fs.promises.mkdir(dirPath, { recursive: true }); await fs.promises.mkdir(dirPath, { recursive: true });
// 解压文件 // 解压文件
const stream = await entry.openReadStream; const stream = await entry.openReadStream;
console.log(entry.fileName);
const write = fs.createWriteStream(`${instancePath}/${targetPath}`); const write = fs.createWriteStream(`${instancePath}/${targetPath}`);
await pipeline(stream, write); await pipeline(stream, write);
} }
@@ -223,17 +238,25 @@ export class Dex {
* @returns 是否在黑名单中 * @returns 是否在黑名单中
*/ */
private _ublack(filename: string): boolean { private _ublack(filename: string): boolean {
if (filename === "overrides/") return true;
const blacklist = [ const blacklist = [
"overrides/options.txt", "overrides/options.txt",
"shaderpacks", "overrides/shaderpacks",
"essential", "overrides/essential",
"resourcepacks", "overrides/resourcepacks",
"PCL", "overrides/PCL",
"CustomSkinLoader", "overrides/CustomSkinLoader"
"overrides"
]; ];
return blacklist.some(item => filename.includes(item)); // 跳过 overrides/ 目录本身
if (filename === "overrides/" || filename === "overrides") {
return true;
}
// 统一处理:确保黑名单项和文件名都以 / 结尾进行比较
return blacklist.some(item => {
const normalizedItem = item.endsWith("/") ? item : item + "/";
const normalizedFilename = filename.endsWith("/") ? filename : filename + "/";
return normalizedFilename === normalizedItem || normalizedFilename.startsWith(normalizedItem);
});
} }
} }

View File

@@ -39,7 +39,7 @@ export class Utils {
this.modrinth_url = "https://api.modrinth.com"; this.modrinth_url = "https://api.modrinth.com";
this.curseforge_url = "https://api.curseforge.com"; this.curseforge_url = "https://api.curseforge.com";
this.modrinth_Durl = "https://cdn.modrinth.com"; this.modrinth_Durl = "https://cdn.modrinth.com";
this.curseforge_Durl = "https://media.forgecdn.net"; this.curseforge_Durl = "https://edge.forgecdn.net";
if (config.mirror.mcimirror) { if (config.mirror.mcimirror) {
this.modrinth_url = "https://mod.mcimirror.top/modrinth"; this.modrinth_url = "https://mod.mcimirror.top/modrinth";
this.curseforge_url = "https://mod.mcimirror.top/curseforge"; this.curseforge_url = "https://mod.mcimirror.top/curseforge";
@@ -141,29 +141,16 @@ export function execPromise(cmd:string,options?:ExecOptions){
const child = spawn(command, args, { const child = spawn(command, args, {
...options, ...options,
shell: true shell: true,
}); stdio: ['ignore', 'ignore', 'ignore']
let stdout = '';
let stderr = '';
child.stdout?.on('data', (data) => {
stdout += data.toString();
});
child.stderr?.on('data', (data) => {
stderr += data.toString();
}); });
child.on('close', (code) => { child.on('close', (code) => {
if (code !== 0) { if (code !== 0) {
logger.error(`Command execution failed: ${cmd}`); logger.error(`Command execution failed: ${cmd}`);
logger.debug(`Stderr: ${stderr}`);
reject(new Error(`Command failed with exit code ${code}`)); reject(new Error(`Command failed with exit code ${code}`));
return; return;
} }
if (stdout) logger.debug(`Command stdout: ${stdout}`);
if (stderr) logger.debug(`Command stderr: ${stderr}`);
logger.debug(`Command completed with exit code: ${code}`); logger.debug(`Command completed with exit code: ${code}`);
resolve(code || 0); resolve(code || 0);
}); });
@@ -175,18 +162,7 @@ export function execPromise(cmd:string,options?:ExecOptions){
}) })
} }
export async function fastdownload(data: [string, string]|string[][]) { async function downloadFile(url: string, filePath: string, onProgress?: (total: number, current: number, path: string) => void) {
// 确保downloadList始终是[string, string][]类型
const downloadList: [string, string][] = Array.isArray(data[0])
? (data as string[][]).map(item => item as [string, string])
: [data as [string, string]];
logger.info(`Starting fast download of ${downloadList.length} files`);
return await pMap(
downloadList,
async (item: [string, string]) => {
const [url, filePath] = item;
try {
await pRetry( await pRetry(
async () => { async () => {
if (!fs.existsSync(filePath)) { if (!fs.existsSync(filePath)) {
@@ -201,10 +177,27 @@ export async function fastdownload(data: [string, string]|string[][]) {
logger.debug(`File already exists, skipping: ${filePath}`); logger.debug(`File already exists, skipping: ${filePath}`);
} }
}, },
{ retries: 3, onFailedAttempt: (error) => { {
retries: 3,
onFailedAttempt: (error) => {
logger.warn(`Download attempt failed for ${url}, retrying (${error.attemptNumber}/3)`); logger.warn(`Download attempt failed for ${url}, retrying (${error.attemptNumber}/3)`);
}} }
}
); );
}
export async function fastdownload(data: [string, string]|string[][]) {
const downloadList: [string, string][] = Array.isArray(data[0])
? (data as string[][]).map(item => item as [string, string])
: [data as [string, string]];
logger.info(`Starting fast download of ${downloadList.length} files`);
return await pMap(
downloadList,
async (item: [string, string]) => {
const [url, filePath] = item;
try {
await downloadFile(url, filePath);
} catch (error) { } catch (error) {
logger.error(`Failed to download ${url} after 3 attempts`, error); logger.error(`Failed to download ${url} after 3 attempts`, error);
throw error; throw error;
@@ -219,30 +212,11 @@ export async function Wfastdownload(data: string[][], ws: MessageWS) {
let index = 0; let index = 0;
return await pMap( return await pMap(
data, data,
async (item: string[], idx: number) => { async (item: string[]) => {
const [url, filePath] = item; const [url, filePath] = item;
try { try {
await pRetry( await downloadFile(url, filePath);
async () => {
if (!fs.existsSync(filePath)) {
logger.debug(`Downloading ${url} to ${filePath}`);
const res = await got.get(url, {
responseType: "buffer",
headers: { "user-agent": "DeEarthX" },
});
fse.outputFileSync(filePath, res.rawBody);
logger.debug(`Downloaded ${url} successfully`);
} else {
logger.debug(`File already exists, skipping: ${filePath}`);
}
// 更新下载进度
ws.download(data.length, ++index, filePath); ws.download(data.length, ++index, filePath);
},
{ retries: 3, onFailedAttempt: (error) => {
logger.warn(`Download attempt failed for ${url}, retrying (${error.attemptNumber}/3)`);
}}
);
} catch (error) { } catch (error) {
logger.error(`Failed to download ${url} after 3 attempts`, error); logger.error(`Failed to download ${url} after 3 attempts`, error);
throw error; throw error;