import 'config_imports.dart'; class AppConfig { final BasicConfig basicConfig; final DNSConfig? dnsConfig; final TunConfig? tunConfig; final List? proxies; final List? proxyGroups; final List rules; AppConfig({ required this.basicConfig, this.dnsConfig, this.tunConfig, this.proxies, this.proxyGroups, required this.rules, }); factory AppConfig.fromMap(Map map) { return AppConfig( basicConfig: BasicConfig.fromMap(map['basic']), dnsConfig: map['dns'] != null ? DNSConfig.fromMap(map['dns']) : null, tunConfig: map['tun'] != null ? TunConfig.fromMap(map['tun']) : null, proxies: map['proxies'] != null ? List.from(map['proxies'].map((x) => ProxyConfig.fromMap(x))) : null, proxyGroups: map['proxy-groups'] != null ? List.from(map['proxy-groups'].map((x) => ProxyGroup.fromMap(x))) : null, rules: List.from(map['rules'].map((x) => Rule.fromMap(x))), ); } Map toJson() { return { 'basic': basicConfig.toJson(), if (dnsConfig != null) 'dns': dnsConfig!.toJson(), if (tunConfig != null) 'tun': tunConfig!.toJson(), if (proxies != null) 'proxies': proxies!.map((proxy) => proxy.toJson()).toList(), if (proxyGroups != null) 'proxy-groups': proxyGroups!.map((group) => group.toJson()).toList(), 'rules': rules.map((rule) => rule.toJson()).toList(), }; } String toYaml() { var yaml = basicConfig.toYaml(); if (dnsConfig != null) { yaml += '\n${dnsConfig!.toYaml()}'; } if (tunConfig != null) { yaml += '\n${tunConfig!.toYaml()}'; } // 总是包含 'proxies:' 键,即使列表为空 var proxiesYaml = proxies != null && proxies!.isNotEmpty ? proxies!.map((proxy) => proxy.toYaml()).join('\n') : ''; yaml += '\nproxies:\n$proxiesYaml'; // 总是包含 'proxy-groups:' 键,即使列表为空 var proxyGroupsYaml = proxyGroups != null && proxyGroups!.isNotEmpty ? proxyGroups!.map((group) => group.toYaml()).join('\n') : ''; yaml += '\nproxy-groups:\n$proxyGroupsYaml'; var rulesYaml = rules.map((rule) => rule.toYaml()).join('\n'); yaml += '\nrules:\n$rulesYaml'; return yaml; } }