|
@@ -30,6 +30,7 @@ final Map<String, dynamic> _defaultVersionConfig = {
|
|
|
class ConfigController extends GetxController {
|
|
|
var config = GuiConfig.fromJson(_defaultConfig).obs;
|
|
|
var versionConfig = VersionConfig.fromJson(_defaultVersionConfig).obs;
|
|
|
+ var isInitialized = false.obs;
|
|
|
|
|
|
var clashCoreApiAddress = '127.0.0.1:9799'.obs;
|
|
|
var clashCoreApiSecret = ''.obs;
|
|
@@ -41,74 +42,77 @@ class ConfigController extends GetxController {
|
|
|
var dnsPort = kDnsListenPort.obs;
|
|
|
|
|
|
Future<void> initConfig() async {
|
|
|
-
|
|
|
- if (await Paths.config.exists()) {
|
|
|
- if (await Files.versionConfig.exists()) {
|
|
|
- final localVersionConfig = json.decode(await Files.versionConfig.readAsString());
|
|
|
- final localAppVersion = localVersionConfig['appVersion'];
|
|
|
- if (compareVersions(localAppVersion, kVersion) < 0) {
|
|
|
+ try {
|
|
|
+ if (await Paths.config.exists()) {
|
|
|
+ if (await Files.versionConfig.exists()) {
|
|
|
+ final localVersionConfig = json.decode(await Files.versionConfig.readAsString());
|
|
|
+ final localAppVersion = localVersionConfig['appVersion'];
|
|
|
+ if (compareVersions(localAppVersion, kVersion) < 0) {
|
|
|
+ await Paths.config.delete(recursive: true);
|
|
|
+ await Paths.config.create(recursive: true);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
await Paths.config.delete(recursive: true);
|
|
|
await Paths.config.create(recursive: true);
|
|
|
}
|
|
|
- } else {
|
|
|
- await Paths.config.delete(recursive: true);
|
|
|
+ }
|
|
|
+ // 创建用户配置目录
|
|
|
+ if (!await Paths.config.exists()) {
|
|
|
await Paths.config.create(recursive: true);
|
|
|
}
|
|
|
- }
|
|
|
- // 创建用户配置目录
|
|
|
- if (!await Paths.config.exists()) {
|
|
|
- await Paths.config.create(recursive: true);
|
|
|
- }
|
|
|
|
|
|
- // 复制资源文件到配置目录
|
|
|
- await _copyAssetToConfig(Files.assetsCountryMmdb, Files.configCountryMmdb);
|
|
|
- await _copyAssetToConfig(Files.assetsGeoIP, Files.configGeoIP);
|
|
|
- await _copyAssetToConfig(Files.assetsGeosite, Files.configGeosite);
|
|
|
- if (Platform.isWindows) {
|
|
|
- await _copyAssetToConfig(Files.assetsWintun, Files.configWintun);
|
|
|
- }
|
|
|
+ // 复制资源文件到配置目录
|
|
|
+ await _copyAssetToConfig(Files.assetsCountryMmdb, Files.configCountryMmdb);
|
|
|
+ await _copyAssetToConfig(Files.assetsGeoIP, Files.configGeoIP);
|
|
|
+ await _copyAssetToConfig(Files.assetsGeosite, Files.configGeosite);
|
|
|
+ if (Platform.isWindows) {
|
|
|
+ await _copyAssetToConfig(Files.assetsWintun, Files.configWintun);
|
|
|
+ }
|
|
|
|
|
|
- // 设置默认语言
|
|
|
- final locale = Get.deviceLocale!;
|
|
|
- _defaultConfig['language'] = '${locale.languageCode}_${locale.countryCode}';
|
|
|
+ // 设置默认语言
|
|
|
+ final locale = Get.deviceLocale!;
|
|
|
+ _defaultConfig['language'] = '${locale.languageCode}_${locale.countryCode}';
|
|
|
|
|
|
- // 读取或创建配置文件
|
|
|
- if (await Files.configConfig.exists()) {
|
|
|
- final local = json.decode(await Files.configConfig.readAsString());
|
|
|
- config.value = GuiConfig.fromJson({..._defaultConfig, ...local});
|
|
|
- } else {
|
|
|
- config.value = GuiConfig.fromJson(_defaultConfig);
|
|
|
- }
|
|
|
+ // 读取或创建配置文件
|
|
|
+ if (await Files.configConfig.exists()) {
|
|
|
+ final local = json.decode(await Files.configConfig.readAsString());
|
|
|
+ config.value = GuiConfig.fromJson({..._defaultConfig, ...local});
|
|
|
+ } else {
|
|
|
+ config.value = GuiConfig.fromJson(_defaultConfig);
|
|
|
+ }
|
|
|
|
|
|
- // 读取或创建版本配置文件
|
|
|
- if (await Files.versionConfig.exists()) {
|
|
|
- final local = json.decode(await Files.versionConfig.readAsString());
|
|
|
- versionConfig.value = VersionConfig.fromJson({..._defaultVersionConfig, ...local});
|
|
|
- } else {
|
|
|
- versionConfig.value = VersionConfig.fromJson(_defaultVersionConfig);
|
|
|
- }
|
|
|
+ // 读取或创建版本配置文件
|
|
|
+ if (await Files.versionConfig.exists()) {
|
|
|
+ final local = json.decode(await Files.versionConfig.readAsString());
|
|
|
+ versionConfig.value = VersionConfig.fromJson({..._defaultVersionConfig, ...local});
|
|
|
+ } else {
|
|
|
+ versionConfig.value = VersionConfig.fromJson(_defaultVersionConfig);
|
|
|
+ }
|
|
|
|
|
|
- print(Files.configConfig.path);
|
|
|
+ print(Files.configConfig.path);
|
|
|
|
|
|
- // 保存 GuiConfig 到 gui_config.json
|
|
|
- await saveGuiConfig();
|
|
|
+ // 保存 GuiConfig 到 gui_config.json
|
|
|
+ await saveGuiConfig();
|
|
|
|
|
|
- // 保存 VersionConfig 到 version_config.json
|
|
|
- await saveVersionConfig();
|
|
|
+ // 保存 VersionConfig 到 version_config.json
|
|
|
+ await saveVersionConfig();
|
|
|
|
|
|
- // await makeInitConfig();
|
|
|
+ isInitialized.value = true;
|
|
|
+ } catch (e) {
|
|
|
+ LogHelper().e("配置初始化失败: $e");
|
|
|
+ isInitialized.value = false;
|
|
|
+ rethrow;
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
Future<void> saveConfig() async {
|
|
|
// 保存 GuiConfig 到 gui_config.json
|
|
|
await saveGuiConfig();
|
|
|
|
|
|
// 保存 VersionConfig 到 version_config.json
|
|
|
await saveVersionConfig();
|
|
|
-
|
|
|
- // await makeInitConfig();
|
|
|
}
|
|
|
|
|
|
-
|
|
|
// 比较两个版本号
|
|
|
int compareVersions(String v1, String v2) {
|
|
|
var parts1 = v1.split('.');
|
|
@@ -194,7 +198,6 @@ class ConfigController extends GetxController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
- // ... 其他方法保持不变
|
|
|
+ // 在 GlobalController 中可以这样使用
|
|
|
+ bool get initialized => isInitialized.value;
|
|
|
}
|