新增toml支持

This commit is contained in:
yxsj245
2025-10-29 21:07:45 +08:00
parent 10a0a33d26
commit ca59c8fb6f
4 changed files with 66 additions and 3 deletions

View File

@@ -4,7 +4,8 @@ meta:
parser: "toml"
sections:
- fields:
- key: "fml"
fields:
- name: "earlyWindowHeight"
display: "早期窗口高度"
default: 480

View File

@@ -28,6 +28,7 @@
"multer": "2.0.2",
"node-cron": "^3.0.3",
"properties-reader": "^2.3.0",
"smol-toml": "^1.3.1",
"socket.io": "^4.7.4",
"tar": "^6.2.0",
"tar-stream": "^3.1.7",
@@ -6088,6 +6089,18 @@
"node": ">=8"
}
},
"node_modules/smol-toml": {
"version": "1.4.2",
"resolved": "https://registry.npmjs.org/smol-toml/-/smol-toml-1.4.2.tgz",
"integrity": "sha512-rInDH6lCNiEyn3+hH8KVGFdbjc099j47+OSgbMrfDYX1CmXLfdKd7qi6IfcWj2wFxvSVkuI46M+wPGYfEOEj6g==",
"license": "BSD-3-Clause",
"engines": {
"node": ">= 18"
},
"funding": {
"url": "https://github.com/sponsors/cyyynthia"
}
},
"node_modules/socket.io": {
"version": "4.8.1",
"resolved": "https://registry.npmjs.org/socket.io/-/socket.io-4.8.1.tgz",

View File

@@ -37,7 +37,8 @@
"uuid": "^9.0.1",
"winston": "^3.11.0",
"yaml": "^2.3.4",
"yauzl": "^2.10.0"
"yauzl": "^2.10.0",
"smol-toml": "^1.3.1"
},
"devDependencies": {
"@types/adm-zip": "^0.5.7",

View File

@@ -4,6 +4,7 @@ import path from 'path'
import { fileURLToPath } from 'url'
import YAML from 'yaml'
import PropertiesReader from 'properties-reader'
import TOML from 'smol-toml'
import logger from '../../utils/logger.js'
const __filename = fileURLToPath(import.meta.url)
@@ -77,7 +78,8 @@ export class GameConfigManager {
['configobj', this.parseWithConfigObj.bind(this)],
['yaml', this.parseWithYaml.bind(this)],
['ruamel.yaml', this.parseWithYaml.bind(this)],
['json', this.parseWithJson.bind(this)]
['json', this.parseWithJson.bind(this)],
['toml', this.parseWithToml.bind(this)]
])
}
@@ -207,6 +209,9 @@ export class GameConfigManager {
case 'json':
await this.saveWithJson(fullConfigPath, configData, configSchema)
break
case 'toml':
await this.saveWithToml(fullConfigPath, configData, configSchema)
break
default:
throw new Error(`不支持的解析器类型: ${parserType}`)
}
@@ -568,4 +573,47 @@ export class GameConfigManager {
return String(value)
}
/**
* 使用TOML格式解析配置文件
*/
private async parseWithToml(configPath: string, configSchema: GameConfigSchema): Promise<ParsedConfigData> {
try {
const content = await fs.readFile(configPath, 'utf-8')
const tomlData = TOML.parse(content) || {}
const result: ParsedConfigData = {}
for (const section of configSchema.sections) {
result[section.key] = {}
const sectionData = tomlData[section.key] || {}
for (const field of section.fields) {
const value = sectionData[field.name]
if (value !== undefined) {
result[section.key][field.name] = value
} else {
result[section.key][field.name] = field.default
}
}
}
return result
} catch (error) {
logger.error('TOML解析失败:', error)
throw error
}
}
/**
* 保存为TOML格式
*/
private async saveWithToml(configPath: string, configData: ParsedConfigData, configSchema: GameConfigSchema): Promise<void> {
try {
const tomlContent = TOML.stringify(configData)
await fs.writeFile(configPath, tomlContent, 'utf-8')
} catch (error) {
logger.error('TOML保存失败:', error)
throw error
}
}
}