60 lines
2.4 KiB
JavaScript
60 lines
2.4 KiB
JavaScript
import { readFileSync, writeFileSync, mkdirSync } from 'node:fs'
|
||
// 用法:node tools/convert.mjs <上游仓库>/Source/GrasscutterTools/Resources/zh-cn
|
||
import { fileURLToPath } from 'node:url'
|
||
import { dirname, resolve } from 'node:path'
|
||
const SRC = process.argv[2] || './GrasscutterCommandGenerator/Source/GrasscutterTools/Resources/zh-cn'
|
||
const OUT = resolve(dirname(fileURLToPath(import.meta.url)), '../android/app/src/main/assets')
|
||
|
||
function parse(text) {
|
||
const lines = text.replace(/^\uFEFF/, '').split(/\r?\n/)
|
||
const out = []
|
||
let section = ''
|
||
for (const raw of lines) {
|
||
const line = raw.trim()
|
||
if (!line) continue
|
||
if (line.startsWith('//')) { section = line.slice(2).trim(); continue }
|
||
const i = line.indexOf(':')
|
||
if (i <= 0) continue
|
||
const id = line.slice(0, i).trim()
|
||
const name = line.slice(i + 1).trim()
|
||
if (!/^\d+$/.test(id) || !name) continue
|
||
out.push([Number(id), name, section])
|
||
}
|
||
return out
|
||
}
|
||
const files = {
|
||
items: 'Item.txt', monsters: 'Monsters.txt', achievements: 'Achievement.txt',
|
||
scenes: 'Scene.txt', dungeons: 'Dungeon.txt', cutscenes: 'Cutscene.txt',
|
||
artifacts: 'Artifact.txt', weapons: 'Weapon.txt', avatars: 'Avatar.txt',
|
||
artifactCats: 'ArtifactCat.txt', artifactMain: 'ArtifactMainAttribution.txt',
|
||
artifactSub: 'ArtifactSubAttribution.txt', props: 'PlayerProperty.txt',
|
||
}
|
||
const db = {}
|
||
for (const [k, f] of Object.entries(files)) {
|
||
db[k] = parse(readFileSync(`${SRC}/${f}`, 'utf8'))
|
||
}
|
||
|
||
// 自定义命令:上游是「标签一行、命令一行」交替(见 PageCustomCommands.SaveCustomCommandControls)
|
||
const customRaw = readFileSync(`${SRC}/CustomCommands.txt`, 'utf8').replace(/^\uFEFF/, '')
|
||
const custom = []
|
||
let label = ''
|
||
for (const raw of customRaw.split(/\r?\n/)) {
|
||
const line = raw.trim()
|
||
if (!line || line.startsWith('//')) continue
|
||
if (line.startsWith('/')) {
|
||
if (label) custom.push({ label, cmd: line })
|
||
label = ''
|
||
} else {
|
||
label = line
|
||
}
|
||
}
|
||
|
||
mkdirSync(OUT, { recursive: true })
|
||
writeFileSync(`${OUT}/gamedata.json`, JSON.stringify(db))
|
||
writeFileSync(`${OUT}/custom.json`, JSON.stringify(custom))
|
||
const stat = (k) => `${k}: ${db[k].length}`
|
||
console.log(Object.keys(db).map(stat).join('\n'))
|
||
console.log('custom entries:', custom.length)
|
||
console.log('size:', (readFileSync(`${OUT}/gamedata.json`).length / 1024 / 1024).toFixed(2), 'MB')
|
||
console.log('sample:', JSON.stringify(db.items.slice(0, 2)), JSON.stringify(db.avatars.slice(0, 2)))
|