alroyso 1 month ago
parent
commit
bb7b95fab1
2 changed files with 63 additions and 50 deletions
  1. 11 1
      lib/app/controller/GlobalController.dart
  2. 52 49
      lib/app/controller/config.dart

+ 11 - 1
lib/app/controller/GlobalController.dart

@@ -94,6 +94,11 @@ class GlobalController extends GetxController {
     // 如果核心未运行,尝试启动它
     if (!allStopped) {
       try {
+        // 确保配置已初始化
+        if (!controllers.config.isInitialized.value) {
+          await controllers.config.initConfig();
+        }
+        
         if (Platform.isWindows) {
           await controllers.service.startClashCore();
         } else if (Platform.isMacOS) {
@@ -103,6 +108,7 @@ class GlobalController extends GetxController {
             await controllers.service.startClashCore();
           }
         }
+        
         // 等待核心启动
         await Future.delayed(Duration(seconds: 2));
         // 再次检查核心状态
@@ -176,7 +182,11 @@ class GlobalController extends GetxController {
 
   Future<void> fetchNodes() async {
     try {
-      // 检查内核是否正在运行
+      // 检查内核是否正在运行,同时确保配置已初始化
+      if (!controllers.config.isInitialized.value) {
+        await controllers.config.initConfig();
+      }
+      
       if (!await checkAllCoresStopped()) {
         throw Exception("内核未启动");
       }

+ 52 - 49
lib/app/controller/config.dart

@@ -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;
 }