From ca59c8fb6f132ee0f1e7a5e0646ca3a0fd774d4a Mon Sep 17 00:00:00 2001 From: yxsj245 <17737475682@163.com> Date: Wed, 29 Oct 2025 21:07:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Etoml=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/data/gameconfig/我的世界_forge.yml | 3 +- server/package-lock.json | 13 +++++ server/package.json | 3 +- .../modules/gameConfig/GameConfigManager.ts | 50 ++++++++++++++++++- 4 files changed, 66 insertions(+), 3 deletions(-) diff --git a/server/data/gameconfig/我的世界_forge.yml b/server/data/gameconfig/我的世界_forge.yml index 572a437..6b291fa 100644 --- a/server/data/gameconfig/我的世界_forge.yml +++ b/server/data/gameconfig/我的世界_forge.yml @@ -4,7 +4,8 @@ meta: parser: "toml" sections: - - fields: + - key: "fml" + fields: - name: "earlyWindowHeight" display: "早期窗口高度" default: 480 diff --git a/server/package-lock.json b/server/package-lock.json index 6a07e6e..884e1d0 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -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", diff --git a/server/package.json b/server/package.json index acee384..72e86bc 100644 --- a/server/package.json +++ b/server/package.json @@ -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", diff --git a/server/src/modules/gameConfig/GameConfigManager.ts b/server/src/modules/gameConfig/GameConfigManager.ts index ef715e5..2e02bdf 100644 --- a/server/src/modules/gameConfig/GameConfigManager.ts +++ b/server/src/modules/gameConfig/GameConfigManager.ts @@ -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 { + 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 { + try { + const tomlContent = TOML.stringify(configData) + await fs.writeFile(configPath, tomlContent, 'utf-8') + } catch (error) { + logger.error('TOML保存失败:', error) + throw error + } + } } \ No newline at end of file