Skip to content

Commit 8e2381a

Browse files
committed
refactor: 统一配置 section 顺序
- 按 tmpSettingsSort.txt 调整所有公开 section 的输出顺序 - 将 General 规范化为 CustomVersionText 并兼容读取旧名称 - 为配置 section 增加通用读取别名支持
1 parent f8baf39 commit 8e2381a

33 files changed

Lines changed: 144 additions & 56 deletions

packages/applechu-schema/src/generator/parser.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ impl Parse for SectionDecl {
6060
let metadata: Ident = content.parse()?;
6161
content.parse::<Token![:]>()?;
6262
match metadata.to_string().as_str() {
63+
"aliases" => {
64+
let aliases_content;
65+
syn::bracketed!(aliases_content in content);
66+
let _ = aliases_content
67+
.parse_terminated(|input| input.parse::<LitStr>(), Token![,])?;
68+
}
6369
"export" => export = Some(content.parse::<LitBool>()?.value),
6470
"group" => group = Some(content.parse()?),
6571
"community" => community = content.parse::<LitBool>()?.value,

packages/applechu-schema/src/lib.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,64 @@ mod tests {
10051005
assert!(config.contains("[SliderDevice]"));
10061006
}
10071007

1008+
#[test]
1009+
fn default_config_follows_settings_sort_order() {
1010+
// Given: Rust 声明生成完整的公开配置 schema。
1011+
let schema = generated_schema();
1012+
1013+
// When: 默认配置按 section 顺序输出。
1014+
let config = schema.default_config_toml();
1015+
let sections = config
1016+
.lines()
1017+
.filter_map(|line| line.strip_prefix('[')?.strip_suffix(']'))
1018+
.collect::<Vec<_>>();
1019+
1020+
// Then: 顺序与设置管理器约定完全一致。
1021+
assert_eq!(
1022+
sections,
1023+
[
1024+
"System",
1025+
"Amdaemon",
1026+
"Dns",
1027+
"AllowLocalhost",
1028+
"Keychip",
1029+
"Aime",
1030+
"Io4",
1031+
"SliderDevice",
1032+
"ChuniIo",
1033+
"Led",
1034+
"Led15093",
1035+
"Vfd",
1036+
"Window",
1037+
"FreePlay",
1038+
"CreditFreeze",
1039+
"SkipStartup",
1040+
"DisableTimer",
1041+
"CustomTimers",
1042+
"UnlockTracks",
1043+
"SkipMapAnimation",
1044+
"Unlocker",
1045+
"ForceSharedAudio",
1046+
"Force2chAudio",
1047+
"CustomVersionText",
1048+
"DisableEncryption",
1049+
"DisableTLS",
1050+
"Unlock120fps",
1051+
"Bypass1080p",
1052+
"Bypass120hz",
1053+
"BypassAppUser",
1054+
"NetLog",
1055+
"Autoplay",
1056+
"FpsDisplay",
1057+
"FrameLock",
1058+
"DpiAware",
1059+
"PCBID",
1060+
"VFS",
1061+
"NetEnv",
1062+
]
1063+
);
1064+
}
1065+
10081066
#[test]
10091067
fn rejects_malformed_entries_instead_of_silently_dropping_them() {
10101068
let source = r#"

packages/applechu/src/aime/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static EXTERNAL: Lazy<Mutex<Option<ExternalAimeIo>>> = Lazy::new(|| Mutex::new(N
4545
crate::config_section! {
4646
pub(crate) struct AimeSectionConfig => AIME_CONFIG_SECTION {
4747
section: "Aime",
48-
order: 300,
48+
order: 50,
4949
default_on: true,
5050
always_enabled: false,
5151
hidden: false,

packages/applechu/src/amdaemon.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ impl Drop for AutoStartedChild {
104104
crate::config_section! {
105105
pub(crate) struct AmdaemonConfig => AMDAEMON_CONFIG_SECTION {
106106
section: "Amdaemon",
107-
order: 5,
107+
order: 20,
108108
default_on: true,
109109
always_enabled: false,
110110
hidden: false,
@@ -132,7 +132,7 @@ crate::config_section! {
132132
crate::config_section! {
133133
pub struct AllowLocalhostConfig => ALLOW_LOCALHOST_CONFIG_SECTION {
134134
section: "AllowLocalhost",
135-
order: 6,
135+
order: 35,
136136
default_on: false,
137137
always_enabled: false,
138138
hidden: false,
@@ -144,7 +144,7 @@ crate::config_section! {
144144
crate::config_section! {
145145
pub struct CreditFreezeConfig => CREDIT_FREEZE_CONFIG_SECTION {
146146
section: "CreditFreeze",
147-
order: 7,
147+
order: 140,
148148
default_on: false,
149149
always_enabled: false,
150150
hidden: false,
@@ -156,7 +156,7 @@ crate::config_section! {
156156
crate::config_section! {
157157
pub struct DnsConfig => DNS_CONFIG_SECTION {
158158
section: "Dns",
159-
order: 980,
159+
order: 30,
160160
default_on: true,
161161
always_enabled: false,
162162
hidden: false,
@@ -189,7 +189,7 @@ crate::config_section! {
189189
crate::config_section! {
190190
pub struct KeychipConfig => KEYCHIP_CONFIG_SECTION {
191191
section: "Keychip",
192-
order: 970,
192+
order: 40,
193193
default_on: true,
194194
always_enabled: false,
195195
hidden: false,
@@ -221,7 +221,7 @@ crate::config_section! {
221221
crate::config_section! {
222222
pub struct NetEnvConfig => NETENV_CONFIG_SECTION {
223223
section: "NetEnv",
224-
order: 975,
224+
order: 370,
225225
default_on: true,
226226
always_enabled: false,
227227
hidden: false,

packages/applechu/src/autoplay/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::util::api::Api;
2020
crate::config_section! {
2121
pub(crate) struct AutoplayConfig => AUTOPLAY_CONFIG_SECTION {
2222
section: "Autoplay",
23-
order: 180,
23+
order: 310,
2424
default_on: false,
2525
always_enabled: false,
2626
hidden: false,

packages/applechu/src/chuniio/led_output.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const GENERIC_WRITE: u32 = 0x4000_0000;
2929
crate::config_section! {
3030
pub(crate) struct LedOutputConfig => LED_OUTPUT_CONFIG_SECTION {
3131
section: "Led",
32-
order: 313,
32+
order: 90,
3333
default_on: true,
3434
always_enabled: false,
3535
hidden: false,

packages/applechu/src/chuniio/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use self::led_output::LedOutputConfig;
2020
crate::config_section! {
2121
pub(crate) struct ExternalChuniIoConfig => CHUNI_IO_CONFIG_SECTION {
2222
section: "ChuniIo",
23-
order: 309,
23+
order: 80,
2424
default_on: true,
2525
always_enabled: false,
2626
hidden: false,

packages/applechu/src/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ macro_rules! config_section {
3838
default_on: $default_on:expr,
3939
always_enabled: $always_enabled:expr,
4040
hidden: $hidden:expr,
41+
$(aliases: [$($alias:literal),* $(,)?],)?
4142
$(export: $export:expr,)?
4243
$(group: $group:literal,)?
4344
$(community: $community:expr,)?
@@ -154,6 +155,7 @@ macro_rules! config_section {
154155
default_on: $default_on,
155156
always_enabled: $always_enabled,
156157
hidden: $hidden,
158+
aliases: &[$($($alias),*)?],
157159
comment: $comment,
158160
type_id: std::any::TypeId::of::<$name>,
159161
parse: $name::parse_config_section,

packages/applechu/src/config/document.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use std::path::{Path, PathBuf};
66
use std::sync::OnceLock;
77

88
use super::schema::{
9-
append_comment, find_section, ConfigDiagnostic, ConfigSection, DiagnosticLevel, LoadedSection,
10-
SectionDescriptor, SectionRef, CONFIG_SECTIONS,
9+
append_comment, find_section_for, ConfigDiagnostic, ConfigSection, DiagnosticLevel,
10+
LoadedSection, SectionDescriptor, SectionRef, CONFIG_SECTIONS,
1111
};
1212
use super::validation::{validate_document, validate_registry, warn_unknown_sections};
1313

@@ -146,7 +146,7 @@ impl Config {
146146
let mut sections = HashMap::new();
147147
for descriptor in &descriptors {
148148
let table = (!descriptor.builtin())
149-
.then(|| find_section(root, descriptor.name))
149+
.then(|| find_section_for(root, descriptor))
150150
.flatten();
151151
let loaded = (descriptor.parse)(table, &mut diagnostics);
152152
sections.insert((descriptor.type_id)(), loaded);
@@ -160,7 +160,7 @@ impl Config {
160160
&& value.is_table()
161161
&& !descriptors
162162
.iter()
163-
.any(|descriptor| applechu_schema::keys_equal(descriptor.name, name))
163+
.any(|descriptor| descriptor.matches_name(name))
164164
})
165165
.filter_map(|(name, value)| value.as_table().map(|table| (name.clone(), table.clone())))
166166
.collect();

packages/applechu/src/config/schema.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub struct SectionDescriptor {
4040
pub default_on: bool,
4141
pub always_enabled: bool,
4242
pub hidden: bool,
43+
pub aliases: &'static [&'static str],
4344
pub comment: &'static str,
4445
pub type_id: fn() -> TypeId,
4546
pub parse: fn(Option<&toml::Table>, &mut Vec<ConfigDiagnostic>) -> LoadedSection,
@@ -63,6 +64,14 @@ impl SectionDescriptor {
6364
crate::schema_embed::section(self.name).map_or(self.hidden, |section| section.hidden)
6465
}
6566

67+
pub fn matches_name(&self, name: &str) -> bool {
68+
applechu_schema::keys_equal(self.name, name)
69+
|| self
70+
.aliases
71+
.iter()
72+
.any(|alias| applechu_schema::keys_equal(alias, name))
73+
}
74+
6675
/// schema 中没有声明的运行时栏目属于内置实现,不写入玩家配置
6776
pub fn builtin(&self) -> bool {
6877
self.hidden() && crate::schema_embed::section(self.name).is_none()
@@ -156,6 +165,15 @@ pub fn find_section<'a>(root: &'a toml::Table, name: &str) -> Option<&'a toml::T
156165
.and_then(toml::Value::as_table)
157166
}
158167

168+
pub fn find_section_for<'a>(
169+
root: &'a toml::Table,
170+
descriptor: &SectionDescriptor,
171+
) -> Option<&'a toml::Table> {
172+
root.iter()
173+
.find(|(key, _)| descriptor.matches_name(key))
174+
.and_then(|(_, value)| value.as_table())
175+
}
176+
159177
fn section_enabled(
160178
table: Option<&toml::Table>,
161179
descriptor: &SectionDescriptor,

0 commit comments

Comments
 (0)