Files
svn/tech/client/交接文档/谭健交接文档/Script/JenkinsScript/JsonPackage/JsonPackage.js
2025-08-04 10:46:00 +08:00

251 lines
11 KiB
JavaScript

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.JsonPackage = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const ROOM_COUNT = 7;
const BASE64_KEYS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
const HEXCHAR = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
const BASE64_VALUES = new Array(123);
for (let i = 0; i < 123; ++i)
BASE64_VALUES[i] = 64;
for (let i = 0; i < 64; ++i)
BASE64_VALUES[BASE64_KEYS.charCodeAt(i)] = i;
const _t = ['', '', '', ''];
const uuidTemplate = _t.concat(_t, '-', _t, '-', _t, '-', _t, '-', _t, _t, _t);
const indices = uuidTemplate.map(function (value, index) { return value === '-' ? NaN : index; }).filter(isFinite);
class SettingJSDecode {
constructor(buildDir) {
this.packedAssets = null;
const settingDir = path_1.default.join(buildDir, 'src');
const files = fs_1.default.readdirSync(settingDir);
for (const file of files) {
if (!file.startsWith('settings'))
continue;
const settingsContent = fs_1.default.readFileSync(path_1.default.join(settingDir, file)).toString();
const ccSetting = this.evalSettings(settingsContent);
this.packedAssets = ccSetting === null || ccSetting === void 0 ? void 0 : ccSetting.packedAssets;
break;
}
}
evalSettings(content) {
content = content.replace('window.', 'settingsContainer.');
const settingsContainer = {};
eval(content);
return settingsContainer._CCSettings;
}
toUUID(base64) {
let baselen = base64.length;
if (baselen < 22) {
return base64;
}
uuidTemplate[0] = base64[0];
uuidTemplate[1] = base64[1];
for (let i = 2, j = 2; i < 22; i += 2) {
let lhs = BASE64_VALUES[base64.charCodeAt(i)];
let rhs = BASE64_VALUES[base64.charCodeAt(i + 1)];
uuidTemplate[indices[j++]] = HEXCHAR[lhs >> 2];
uuidTemplate[indices[j++]] = HEXCHAR[((lhs & 3) << 2) | rhs >> 4];
uuidTemplate[indices[j++]] = HEXCHAR[rhs & 0xF];
}
return uuidTemplate.join('');
}
pickPackageFiles(uuids) {
if (!this.packedAssets)
return uuids;
for (const fileName in this.packedAssets) {
const packageUUIDs = this.packedAssets[fileName];
let match = true;
const removedUUIDs = [];
for (const packageKey of packageUUIDs) {
const packageUUID = this.toUUID(packageKey);
const index = uuids.indexOf(packageUUID);
match = index >= 0;
if (match) {
removedUUIDs.push(...uuids.splice(index, 1));
}
else {
uuids.push(...removedUUIDs);
match = false;
break;
}
}
match && uuids.push(fileName);
}
return uuids;
}
}
class JsonPackage {
constructor(projectDir, buildDir) {
this.assetsDir = path_1.default.join(projectDir, 'assets');
this.buildDir = buildDir;
this.resourcesDir = path_1.default.join(buildDir, 'res');
this.outputDir = path_1.default.join(buildDir, 'res_package');
this.settingJSDecode = new SettingJSDecode(buildDir);
const buildJson = fs_1.default.readFileSync(path_1.default.join(projectDir, 'build', 'build.json'));
const buildData = JSON.parse(buildJson.toString());
this.uuidMap = buildData.uuid;
this.fileRoomData = this.decodeRoomData();
}
//通过鱼属性表读取相关资源所属的房间
decodeRoomData() {
const filesRoomData = {};
const gameConfigPath = path_1.default.join(this.assetsDir, 'resources', 'conf', 'allConf.json');
const gameConfigContent = fs_1.default.readFileSync(gameConfigPath);
const gameConfig = JSON.parse(gameConfigContent.toString());
const fishAttribute = this.dataArrayToObjectArray(gameConfig['FishAttribute']);
for (const fish of fishAttribute) {
//房间数据
const roomData = [];
const fishName = fish.name;
if (!fishName)
continue;
for (let roomType = 1; roomType <= ROOM_COUNT; roomType++) {
roomData.push(fish[`room${roomType}`]);
}
//处理鱼资源、鱼技能资源
this.updateRoomData(filesRoomData, `resources/anim/clip/fish/${fishName}`, roomData); //鱼动画
this.updateRoomData(filesRoomData, `res/anim/texture/fish/${fishName}`, roomData); //鱼动画纹理
this.updateRoomData(filesRoomData, `resources/texture/hall/scenePage/${fishName}_rk`, roomData); //Loading图标
this.updateRoomData(filesRoomData, `resources/texture/hall/scenePage/${fishName}_rkdes`, roomData); //Loading文字
//处理背景资源
const backgroundString = fish.Background;
if (backgroundString) {
const background = backgroundString.split('|');
switch (background[0]) {
case '1':
this.updateRoomData(filesRoomData, `resources/texture/mainScene/bg/${background[1]}.png`, roomData); //鱼背景静态图
break;
case '2':
}
}
//处理音效资源
const audioString = fish.bossBgm;
}
return filesRoomData;
}
getUUIDsInPath(filePath, uuids) {
uuids = uuids || [];
this.uuidMap.forEach(({ db, uuid }) => {
db = db.substring(12); //"db://assets/".length
if (!db.startsWith(filePath))
return;
if (uuids.indexOf(uuid) >= 0)
return;
uuids.push(uuid);
});
return uuids;
}
dataArrayToObjectArray(data) {
const title = data.shift();
const objArray = [];
for (const dataItem of data) {
const dataObj = {};
for (let i = 0, length = title.length; i < length; i++) {
const key = title[i];
if (!key)
break;
dataObj[key] = dataItem[i];
}
objArray.push(dataObj);
}
return objArray;
}
updateRoomData(container, key, data) {
const exitsData = container[key];
if (!exitsData) {
container[key] = data;
}
else {
const result = [];
for (let i = 0, length = Math.max(exitsData.length, data.length); i < length; i++) {
result.push((exitsData[i] || data[i]) ? 1 : 0);
}
container[key] = result;
}
}
mkdir(dir) {
if (!fs_1.default.existsSync(dir)) {
let parentDir = path_1.default.dirname(dir);
this.mkdir(parentDir);
fs_1.default.mkdirSync(dir);
}
}
pickPackageFile(dir, names, cache) {
cache = cache || {};
const files = fs_1.default.readdirSync(dir);
for (let i = 0; i < files.length; i++) {
const subFileName = files[i];
const subFilePath = path_1.default.join(dir, subFileName);
const stat = fs_1.default.statSync(subFilePath);
if (stat.isDirectory()) {
this.pickPackageFile(subFilePath, names, cache);
}
else {
if (path_1.default.extname(subFileName) !== '.json')
continue;
const fileName = subFileName.substring(0, subFileName.indexOf('.'));
const nameIndex = names.indexOf(fileName);
if (nameIndex < 0) {
continue;
}
names.splice(nameIndex, 1);
const content = fs_1.default.readFileSync(subFilePath).toString();
cache[fileName] = content;
}
}
return cache;
}
package() {
this.mkdir(this.outputDir);
const result = [];
for (let roomType = 1; roomType <= ROOM_COUNT; roomType++) {
const configName = `config_${roomType}.json`;
const uuids = [];
for (const filePath in this.fileRoomData) {
const fishRoomData = this.fileRoomData[filePath];
if (!fishRoomData[roomType - 1])
continue;
this.getUUIDsInPath(filePath, uuids);
}
this.getUUIDsInPath(`resources/spine/mainScene/nGun`, uuids);
this.getUUIDsInPath(`resources/spine/mainScene/yuwang`, uuids);
this.getUUIDsInPath(`resources/spine/mainScene/superWeapon`, uuids);
this.getUUIDsInPath(`resources/prefab/mainScene/preload`, uuids);
this.getUUIDsInPath(`resources/spine/mainScene/preload`, uuids);
this.getUUIDsInPath(`resources/prefab/mainScene/init`, uuids);
this.getUUIDsInPath(`resources/prefab/mainScene/skill`, uuids);
this.settingJSDecode.pickPackageFiles(uuids);
const contents = this.pickPackageFile(this.resourcesDir, uuids);
// console.log(`RoomUUIDs:\n${uuids.join(', ')}`);
// console.log(`RoomErrorUUIDs:\n${uuids.join(', ')}`);
for (const uuid in contents)
result.push({ uuid, name: configName });
fs_1.default.writeFileSync(path_1.default.join(this.outputDir, configName), JSON.stringify(contents));
}
fs_1.default.writeFileSync(path_1.default.join(this.outputDir, 'result.json'), JSON.stringify(result));
fs_1.default.copyFileSync(path_1.default.join(__dirname, 'prj.js'), path_1.default.join(this.buildDir, 'src', 'prj.js'));
const indexPath = path_1.default.join(this.buildDir, 'index.html');
let htmlContent = fs_1.default.readFileSync(indexPath).toString();
const bodyEndIndex = htmlContent.indexOf('</body>');
htmlContent = `${htmlContent.substring(0, bodyEndIndex)}<script type="text/javascript">
(function () {
if(window._CCSettings.jsList){
window._CCSettings.jsList.push("prj.js");
}else{
window._CCSettings.jsList = ["prj.js"];
}
})();
</script>${htmlContent.substring(bodyEndIndex)}`;
fs_1.default.writeFileSync(indexPath, htmlContent);
}
}
exports.JsonPackage = JsonPackage;
const args = process.argv;
const projectDir = args[2];
const buildDir = args[3];
new JsonPackage(projectDir, buildDir).package();