cocos3dx web反编译第二篇

cocos3dx web反编译第二篇

将资源文件恢复到源工程的文件夹里面

const fs = require('fs');
const path = require('path');

// 支持的文件扩展名
const supportedExtensions = ['.jpg', '.png', '.gif', '.webp', '.mp3', '.mp4'];

// 资源文件夹路径
const resourcesDir = path.join(__dirname, 'resources');

// 确保 resources 文件夹存在
if (!fs.existsSync(resourcesDir)) {
    fs.mkdirSync(resourcesDir);
}

// 递归遍历文件夹处理资源文件
function traverseDirectoryForResources(dir) {
    const files = fs.readdirSync(dir);

    files.forEach(file => {
        const filePath = path.join(dir, file);
        const fileStat = fs.statSync(filePath);

        if (fileStat.isDirectory()) {
            // 如果是文件夹,继续递归遍历
            traverseDirectoryForResources(filePath);
        } else {
            // 如果是文件,检查扩展名
            const ext = path.extname(file).toLowerCase();
            if (supportedExtensions.includes(ext)) {
                // 处理文件名
                const newFileName = processFileName(file);
                const newFilePath = path.join(resourcesDir, newFileName);
                // 复制文件到 resources 文件夹
                fs.copyFileSync(filePath, newFilePath);
                console.log(`已复制文件 ${file} 到 ${newFilePath}`);
            }
        }
    });
}

// 处理文件名
function processFileName(fileName) {
    const parts = fileName.split('.');
    if (parts.length > 2) {
        // 如果文件名中有两个或更多的点,保留中间部分
        return parts[1] + path.extname(fileName);
    }
    return fileName;
}

// 递归遍历文件夹处理 JSON 文件
function traverseDirectoryForJson(dir) {
    const files = fs.readdirSync(dir);

    files.forEach(file => {
        const filePath = path.join(dir, file);
        const fileStat = fs.statSync(filePath);

        if (fileStat.isDirectory()) {
            // 如果是文件夹,继续递归遍历
            traverseDirectoryForJson(filePath);
        } else if (file.startsWith('config') && path.extname(file) === '.json') {
            // 处理 config.json 文件

            // 解析 JSON 文件
            const jsonData = fs.readFileSync(filePath, 'utf8');
            try {
                const jsonObject = JSON.parse(jsonData);
                if (jsonObject.paths && Array.isArray(jsonObject.versions.native)) {
                    const processedPaths = processPaths(jsonObject.paths);
                    const processedNative = processNative(jsonObject.versions.native);
                    const matchedData = matchData(processedPaths, processedNative);
                    processMatchedData(matchedData);
                }
            } catch (error) {
                console.error(`解析 JSON 文件 ${file} 时出错:`, error);
            }
        }
    });
}

// 处理 paths 字段
function processPaths(paths) {
    const processedPaths = [];
    for (const [id, value] of Object.entries(paths)) {
        const [addressWithName, tag] = value;
        const parts = addressWithName.split('/');
        const address = parts[0];
        const name = parts[1];
        processedPaths.push({ id: parseInt(id), address, name, tag });
    }
    return processedPaths;
}

// 处理 native 字段
function processNative(native) {
    const processedNative = [];
    if (Array.isArray(native)) {
        for (let i = 0; i < native.length; i += 2) {
            const id = native[i];
            const name = native[i + 1];
            processedNative.push({ id, name });
        }
    }
    return processedNative;
}

// 通过 id 匹配 paths 和 native 数据
function matchData(paths, native) {
    const matchedData = [];
    paths.forEach(pathItem => {
        const nativeItem = native.find(nativeItem => nativeItem.id === pathItem.id);
        if (nativeItem) {
            matchedData.push({
                id: pathItem.id,
                address: pathItem.address,
                name: pathItem.name,
                md5name: nativeItem.name,
                tag: pathItem.tag
            });
        }
    });
    return matchedData;
}

// 处理匹配后的数据
function processMatchedData(matchedData) {
    matchedData.forEach(item => {
        const files = fs.readdirSync(resourcesDir);
        const matchedFile = files.find(file => {
            const fileNameWithoutExtension = path.parse(file).name;
            return fileNameWithoutExtension === item.md5name && supportedExtensions.includes(path.extname(file));
        });
        if (matchedFile) {
            const sourceFilePath = path.join(resourcesDir, matchedFile);
            const destinationDir = path.join(resourcesDir, item.address);
            // 确保目标文件夹存在
            if (!fs.existsSync(destinationDir)) {
                fs.mkdirSync(destinationDir, { recursive: true });
            }
            const destinationFilePath = path.join(destinationDir, item.name + path.extname(matchedFile));
            // 移动文件
            fs.renameSync(sourceFilePath, destinationFilePath);
            console.log(`已将 ${matchedFile} 重命名为 ${item.name}${path.extname(matchedFile)} 并移动到 ${destinationDir}`);
        } else {
            console.log(`在 resources 文件夹中未找到 MD5 名称为 ${item.md5name} 的文件`);
        }
    });
}

// 开始遍历 assets 文件夹处理资源文件
const assetsDir = path.join(__dirname, 'assets');
if (fs.existsSync(assetsDir)) {
    traverseDirectoryForResources(assetsDir);
} else {
    console.log('assets 文件夹不存在');
}

// 开始遍历 assets 文件夹处理 JSON 文件
if (fs.existsSync(assetsDir)) {
    traverseDirectoryForJson(assetsDir);
} else {
    console.log('assets 文件夹不存在');
}

创建一个main.js文件放置到编译好的h5游戏目录的根目录运行即可

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容