const fs = require('fs');
const { v4: uuidv4 } = require('uuid');
// 读取文件并处理可能的错误
function readFilePromise(filePath) {
return new Promise((resolve, reject) => {
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
reject(err);
} else {
resolve(JSON.parse(data));
}
});
});
}
// 生成场景文件和元数据文件
async function generateScene() {
try {
// 读取 config.b8a92.json 文件内容
const configJson = await readFilePromise('config.b8a92.json');
const newUuid = uuidv4(); // uuid
const scenename = 'launch'; // 场景名称
// 读取 0bdd1b425.8c395.json 文件内容
const otherJson = await readFilePromise('0bdd1b425.8c395.json');
// 构建新的 JSON 结构
const mergedJson = [];
// 添加场景资产信息
mergedJson.push({
"__type__": "cc.SceneAsset",
"_name": configJson.name,
"_objFlags": 0,
"_native": "",
"scene": {
"__id__": 1
}
});
// 添加场景信息
mergedJson.push({
"__type__": "cc.Scene",
"_name": "scene-2d",
"_objFlags": 0,
"_parent": null,
"_children": [
{
"__id__": 2
}
],
"_active": true,
"_components": [],
"_prefab": null,
"autoReleaseAssets": false,
"_globals": {
"__id__": 8
},
"_id": newUuid
});
// 生成 .scene 文件内容
configJson.paths && Object.entries(configJson.paths).forEach(([key, value]) => {
const nodeName = value[0].split('/').pop();
mergedJson.push({
"__type__": "cc.Node",
"_name": nodeName,
"_objFlags": 0,
"_parent": {
"__id__": 1
},
"_children": [],
"_active": true,
"_components": [],
"_prefab": null,
"_lpos": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_lrot": {
"__type__": "cc.Quat",
"x": 0,
"y": 0,
"z": 0,
"w": 1
},
"_lscale": {
"__type__": "cc.Vec3",
"x": 1,
"y": 1,
"z": 1
},
"_layer": 33554432,
"_euler": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_id": `${uuidv4()}`
});
});
// 生成 .scene.meta 文件内容
const metaContent = {
"ver": "1.1.45",
"importer": "scene",
"imported": true,
"uuid": newUuid,
"files": [
".json"
],
"subMetas": {},
"userData": {}
};
const sceneCode = JSON.stringify(mergedJson, null, 2);
const metaCode = JSON.stringify(metaContent, null, 2);
// 写入 .scene 文件
fs.writeFile(scenename + '.scene', sceneCode, 'utf8', (err) => {
if (err) {
console.error('写入 .scene 文件时出错:', err);
return;
}
console.log('数据已成功写入到 launch.scene 文件中');
// 写入 .scene.meta 文件
fs.writeFile(scenename + '.scene.meta', metaCode, 'utf8', (err) => {
if (err) {
console.error('写入 .scene.meta 文件时出错:', err);
return;
}
console.log('数据已成功写入到 launch.scene.meta 文件中');
});
});
} catch (error) {
console.error('读取文件时出错:', error);
}
}
// 调用生成场景的函数
generateScene();
const fs = require('fs');
// 读取 config.b8a92.json 文件内容
const configJson = {
"importBase": "import",
"nativeBase": "native",
"name": "launch",
"deps": [],
"uuids": [
"1e1nbU80VCJZrUZzt6RGCO",
"26q0+7EzhEIq22giZBBddE",
"afRaYQKJlDQIFjUmi3BL/m",
"c8itx+T3VImYO55yzW+7L9",
"1e1nbU80VCJZrUZzt6RGCO@6c48a",
"1e1nbU80VCJZrUZzt6RGCO@f9941",
"26q0+7EzhEIq22giZBBddE@6c48a",
"26q0+7EzhEIq22giZBBddE@f9941",
"875RuX9SBFf47BIab0H06i",
"afRaYQKJlDQIFjUmi3BL/m@6c48a",
"afRaYQKJlDQIFjUmi3BL/m@f9941",
"c8itx+T3VImYO55yzW+7L9@6c48a",
"c8itx+T3VImYO55yzW+7L9@f9941",
"0bdd1b425"
],
"paths": {
"0": [
"res/progress",
1
],
"1": [
"res/progress_bg",
1
],
"2": [
"res/bg",
1
],
"3": [
"res/chip_icon",
1
],
"4": [
"res/progress/texture",
2,
1
],
"5": [
"res/progress/spriteFrame",
3,
1
],
"6": [
"res/progress_bg/texture",
2,
1
],
"7": [
"res/progress_bg/spriteFrame",
3,
1
],
"8": [
"prefab/launchUI",
0
],
"9": [
"res/bg/texture",
2,
1
],
"10": [
"res/bg/spriteFrame",
3,
1
],
"11": [
"res/chip_icon/texture",
2,
1
],
"12": [
"res/chip_icon/spriteFrame",
3,
1
]
},
"scenes": {},
"packs": {
"0bdd1b425": [
"0",
"4",
"5",
"1",
"6",
"7",
"8",
"2",
"9",
"10",
"3",
"11",
"12"
]
},
"versions": {
"import": [
13,
"8c395"
],
"native": [
0,
"c4781",
1,
"fc508",
2,
"c000e",
3,
"0b20b"
]
},
"redirect": [],
"debug": false,
"extensionMap": {},
"hasPreloadScript": true,
"dependencyRelationships": {
"4": [
"0"
],
"5": [
"4"
],
"6": [
"1"
],
"7": [
"6"
],
"8": [
"5",
"12",
"7"
],
"9": [
"2"
],
"10": [
"9"
],
"11": [
"3"
],
"12": [
"11"
]
},
"types": [
"cc.Prefab",
"cc.ImageAsset",
"cc.Texture2D",
"cc.SpriteFrame"
]
};
// 读取 0bdd1b425.8c395.json 文件内容
const otherJson = [1,["1e1nbU80VCJZrUZzt6RGCO@6c48a","26q0+7EzhEIq22giZBBddE@6c48a","1e1nbU80VCJZrUZzt6RGCO@f9941","c8itx+T3VImYO55yzW+7L9@f9941","26q0+7EzhEIq22giZBBddE@f9941","afRaYQKJlDQIFjUmi3BL/m@6c48a","c8itx+T3V\
"]];
// 构建新的 JSON 结构
const mergedJson = [];
// 添加场景资产信息
mergedJson.push({
"__type__": "cc.SceneAsset",
"_name": configJson.name,
"_objFlags": 0,
"_native": "",
"scene": {
"__id__": 1
}
});
// 添加场景信息
mergedJson.push({
"__type__": "cc.Scene",
"_name": "scene-2d",
"_objFlags": 0,
"_parent": null,
"_children": [],
"_active": true,
"_components": [],
"_prefab": null,
"autoReleaseAssets": false,
"_globals": {
"__id__": 8
},
"_id": "5132a964-3dd8-4587-bf79-ca4519eecf50"
});
// 添加节点信息(示例,可根据实际需求调整)
configJson.paths && Object.entries(configJson.paths).forEach(([key, value]) => {
const nodeName = value[0].split('/').pop();
mergedJson.push({
"__type__": "cc.Node",
"_name": nodeName,
"_objFlags": 0,
"_parent": {
"__id__": 1
},
"_children": [],
"_active": true,
"_components": [],
"_prefab": null,
"_lpos": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_lrot": {
"__type__": "cc.Quat",
"x": 0,
"y": 0,
"z": 0,
"w": 1
},
"_lscale": {
"__type__": "cc.Vec3",
"x": 1,
"y": 1,
"z": 1
},
"_layer": 33554432,
"_euler": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_id": `node_${key}`
});
});
const code = JSON.stringify(mergedJson, null, 2)
// 写入文件
fs.writeFile('index.json', code, 'utf8', (err) => {
if (err) {
console.error('写入文件时出错:', err);
return;
}
console.log('数据已成功写入到index.json文件中');
});
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END







暂无评论内容