2025-01-02 17:48:16 +08:00
|
|
|
const { log } = require('console');
|
|
|
|
|
const fs = require('fs')
|
|
|
|
|
const path = require('path');
|
|
|
|
|
const folderPath = "./configs"
|
|
|
|
|
function jsonToLua(jsonObj, indent = '', linefeed = '\n') {
|
|
|
|
|
let luaStr = '';
|
|
|
|
|
const strKey = key => isNaN(key) ? `${key}` : `[${key}]`;
|
|
|
|
|
for (let key in jsonObj) {
|
|
|
|
|
let value = jsonObj[key];
|
|
|
|
|
key = strKey(key)
|
|
|
|
|
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
|
|
|
|
|
// 对象
|
|
|
|
|
luaStr += `${indent}${key} = {`;
|
|
|
|
|
luaStr += jsonToLua(value, indent + ' ');
|
|
|
|
|
luaStr += `${indent}},\n`;
|
|
|
|
|
} else if (Array.isArray(value)) {
|
|
|
|
|
// 数组
|
|
|
|
|
luaStr += `${indent}${key} = {`;
|
|
|
|
|
value.forEach(item => {
|
|
|
|
|
|
|
|
|
|
if (typeof item === 'string') {
|
|
|
|
|
luaStr += `${indent} '${item}',`;
|
2025-01-02 17:54:58 +08:00
|
|
|
} else if (typeof item == 'object') {
|
2025-01-02 17:48:16 +08:00
|
|
|
luaStr += `{${jsonToLua(item, '', '')}},`;
|
2025-01-02 17:54:58 +08:00
|
|
|
} else {
|
|
|
|
|
luaStr += `${indent} ${item},`;
|
2025-01-02 17:48:16 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
luaStr += `${indent}},\n`;
|
|
|
|
|
} else {
|
|
|
|
|
// 基础类型
|
|
|
|
|
if (typeof value === 'string') {
|
|
|
|
|
luaStr += `${indent}${key}="${value}",${linefeed}`;
|
|
|
|
|
} else {
|
|
|
|
|
luaStr += `${indent}${key} = ${value},${linefeed}`;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return luaStr;
|
|
|
|
|
}
|
|
|
|
|
const start = () => {
|
|
|
|
|
const files = fs.readdirSync(folderPath);
|
|
|
|
|
|
|
|
|
|
// 过滤出所有的 JSON 文件
|
2025-01-13 00:33:01 +08:00
|
|
|
const jsonFiles = files.filter(file => path.extname(file) === '.json').filter(file => ["StdItems","ItemMergeConfig","MergeConfig","MergeTotal"].map(e => `${e}.json`).includes(file));
|
2025-01-02 17:48:16 +08:00
|
|
|
jsonFiles.forEach(file => {
|
|
|
|
|
const json = JSON.parse(fs.readFileSync(path.join(folderPath, file), 'utf8'))
|
|
|
|
|
const filename = path.basename(file, path.extname(file));
|
|
|
|
|
const luaConfig = `${filename}={\n${jsonToLua(json)}\n}`
|
|
|
|
|
fs.writeFileSync(`./luaConfigs/${filename}.config`, luaConfig);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
start()
|