Kaynağa Gözat

尝试修复设定文件损坏的问题。(魔盒异常重启可能导致)

felix 1 yıl önce
ebeveyn
işleme
f3c4cc962b

+ 80 - 55
Vinno.FIS.Sonopost/Settings/SonopostSystemSettingInfo.cs

@@ -41,40 +41,42 @@ namespace Vinno.FIS.Sonopost.Settings
 
         static SonopostSystemSettings()
         {
-            if (!LoadConfig())
+            try
             {
-                Instance = new SonopostSystemSettings
+                if (!LoadConfig())
                 {
-                    RainbowImageDetectSetting = new FISRainbowImageDetectConfig
+                    Instance = new SonopostSystemSettings
                     {
-                        BeforeDisableIntervalTime = 3000,
-                        BeforeEnableIntervalTime = 5000,
-                        AfterEnableIntervalTime = 2000,
-                        ScanIntervalTime = 7500,
-                        IsDetectRainbowImage = false,
-                        CaptureCardList = new List<string>
+                        RainbowImageDetectSetting = new FISRainbowImageDetectConfig
+                        {
+                            BeforeDisableIntervalTime = 3000,
+                            BeforeEnableIntervalTime = 5000,
+                            AfterEnableIntervalTime = 2000,
+                            ScanIntervalTime = 7500,
+                            IsDetectRainbowImage = false,
+                            CaptureCardList = new List<string>
                         {
                             "LT8501-DVI Card",
                             "HWSPro Capture Card"
                         },
-                    },
-                    UpdateSetting = new UpdateSetting
-                    {
-                        NeedAutoUpdate = true,
-                        AutoUpdateTime = 3,
-                    },
-                    WebSetting = new WebSetting
-                    {
-                        WebPort = 80,
-                        WebPortStandby = 8080,
-                        WebSocketPort = 54321,
-                    },
-                    InputDeviceNames = new List<string>
+                        },
+                        UpdateSetting = new UpdateSetting
+                        {
+                            NeedAutoUpdate = true,
+                            AutoUpdateTime = 3,
+                        },
+                        WebSetting = new WebSetting
+                        {
+                            WebPort = 80,
+                            WebPortStandby = 8080,
+                            WebSocketPort = 54321,
+                        },
+                        InputDeviceNames = new List<string>
                     {
                         "HD Video 1 (LT8501-DVI Card)",
                         "HDPro 1"
                     },
-                    SkippedMicDeviceNames = new List<string>
+                        SkippedMicDeviceNames = new List<string>
                     {
                         "HD Audio 1 (LT8501-DVI Card) (LT8501-DVI Card)",
                         "HD Audio 1 (LT8501-DVI Card) (2- LT8501-DVI Card)",
@@ -87,18 +89,23 @@ namespace Vinno.FIS.Sonopost.Settings
                         "麦克风 (High Definition Audio 设备)",
                         "麦克风 (2- High Definition Audio 设备)"
                     },
-                    SkippedSpeakerDeviceNames = new List<string>
+                        SkippedSpeakerDeviceNames = new List<string>
                     {
                         "耳机 (High Definition Audio 设备)",
                         "耳机 (2- High Definition Audio 设备)",
                         "Headphones (High Definition Audio Device)",
                         "Headphones (2- High Definition Audio Device)"
                     },
-                    ModelType = "SP-0002",
-                    CodecName = "h264_qsv",
-                    OledCommName = null,
-                };
-                Instance.Save();
+                        ModelType = "SP-0002",
+                        CodecName = "h264_qsv",
+                        OledCommName = null,
+                    };
+                    Instance.Save();
+                }
+            }
+            catch (Exception ex)
+            {
+                Logger.WriteLineError($"SonopostSystemSettings Static Cstr Error:{ex}");
             }
         }
 
@@ -119,42 +126,60 @@ namespace Vinno.FIS.Sonopost.Settings
             {
                 if (File.Exists(_systemSettingFilePath))
                 {
-                    try
+                    if (LoadConfigFile())
                     {
-                        var jsonString = File.ReadAllText(_systemSettingFilePath);
-                        Instance = JsonHelper.JsonToObj<SonopostSystemSettings>(jsonString);
-                        if (Instance == null)
-                        {
-                            Logger.WriteLineError($"SonopostSystemSettingInfo Load Config Fail,Instance is null");
-                            return false;
-                        }
                         return true;
                     }
-                    catch (Exception ex)
+                    else if (File.Exists(_systemSettingTempFilePath))
                     {
-                        Logger.WriteLineError($"SonopostSystemSettingInfo Load Config Fail,ex:{ex}");
+                        return LoadTempConfigFile();
                     }
                 }
                 else
                 {
-                    try
-                    {
-                        var jsonString = File.ReadAllText(_systemSettingTempFilePath);
-                        Instance = JsonHelper.JsonToObj<SonopostSystemSettings>(jsonString);
-                        if (Instance == null)
-                        {
-                            Logger.WriteLineError($"SonopostSystemSettingInfo Load temp Config Fail,Instance is null");
-                            return false;
-                        }
-                        return true;
-                    }
-                    catch (Exception ex)
-                    {
-                        Logger.WriteLineError($"SonopostSystemSettingInfo Load temp Config Fail,ex:{ex}");
-                    }
+                    return LoadTempConfigFile();
                 }
             }
+            return false;
+        }
 
+        private static bool LoadTempConfigFile()
+        {
+            try
+            {
+                var jsonString = File.ReadAllText(_systemSettingTempFilePath);
+                Instance = JsonHelper.JsonToObj<SonopostSystemSettings>(jsonString);
+                if (Instance == null)
+                {
+                    Logger.WriteLineError($"SonopostSystemSettingInfo Load temp Config Fail,Instance is null");
+                    return false;
+                }
+                return true;
+            }
+            catch (Exception ex)
+            {
+                Logger.WriteLineError($"SonopostSystemSettingInfo Load temp Config Fail,ex:{ex}");
+            }
+            return false;
+        }
+
+        private static bool LoadConfigFile()
+        {
+            try
+            {
+                var jsonString = File.ReadAllText(_systemSettingFilePath);
+                Instance = JsonHelper.JsonToObj<SonopostSystemSettings>(jsonString);
+                if (Instance == null)
+                {
+                    Logger.WriteLineError($"SonopostSystemSettingInfo Load Config Fail,Instance is null");
+                    return false;
+                }
+                return true;
+            }
+            catch (Exception ex)
+            {
+                Logger.WriteLineError($"SonopostSystemSettingInfo Load Config Fail,ex:{ex}");
+            }
             return false;
         }
 
@@ -171,7 +196,7 @@ namespace Vinno.FIS.Sonopost.Settings
                     WriteIndented = true
                 });
                 File.WriteAllText(_systemSettingTempFilePath, jsonString);
-                FileHelper.MoveFile(_systemSettingTempFilePath, _systemSettingFilePath, true);
+                FileHelper.CopyFile(_systemSettingTempFilePath, _systemSettingFilePath, true);
             }
             catch (Exception ex)
             {

+ 65 - 40
Vinno.FIS.Sonopost/Settings/SonopostUserDefinedSettingInfo.cs

@@ -41,9 +41,16 @@ namespace Vinno.FIS.Sonopost.Settings
 
         static SonopostUserDefinedSettings()
         {
-            if (!LoadConfig())
+            try
+            {
+                if (!LoadConfig())
+                {
+                    InitInstance();
+                }
+            }
+            catch (Exception ex)
             {
-                InitInstance();
+                Logger.WriteLineError($"SonopostUserDefinedSettings Static Cstr Error:{ex}");
             }
         }
 
@@ -64,54 +71,72 @@ namespace Vinno.FIS.Sonopost.Settings
             {
                 if (File.Exists(_userDefinedFilePath))
                 {
-                    try
+                    if (LoadConfigFile())
                     {
-                        var jsonString = File.ReadAllText(_userDefinedFilePath);
-                        Instance = JsonHelper.JsonToObj<SonopostUserDefinedSettings>(jsonString);
-                        if (Instance == null)
-                        {
-                            Logger.WriteLineError($"SonopostUserDefinedSettings Load Config Fail,Instance is null");
-                            return false;
-                        }
-                        if (Instance.Version < SonopostConstants.CurrentSonopostUserDefinedSettingVersion)
-                        {
-                            UpgradeSettingFrom0To1();
-                            UpgradeSettingFrom1To2();
-                            Instance.Save();
-                        }
                         return true;
                     }
-                    catch (Exception ex)
+                    else if (File.Exists(_userDefinedTempFilePath))
                     {
-                        Logger.WriteLineError($"SonopostUserDefinedSettings Load Config Fail,ex:{ex}");
+                        return LoadTempConfigFile();
                     }
                 }
                 else
                 {
-                    try
-                    {
-                        var jsonString = File.ReadAllText(_userDefinedTempFilePath);
-                        Instance = JsonHelper.JsonToObj<SonopostUserDefinedSettings>(jsonString);
-                        if (Instance == null)
-                        {
-                            Logger.WriteLineError($"SonopostUserDefinedSettings Load temp Config Fail,Instance is null");
-                            return false;
-                        }
-                        if (Instance.Version < SonopostConstants.CurrentSonopostUserDefinedSettingVersion)
-                        {
-                            UpgradeSettingFrom0To1();
-                            UpgradeSettingFrom1To2();
-                            Instance.Save();
-                        }
-                        return true;
-                    }
-                    catch (Exception ex)
-                    {
-                        Logger.WriteLineError($"SonopostUserDefinedSettings Load temp Config Fail,ex:{ex}");
-                    }
+                    return LoadTempConfigFile();
+                }
+            }
+            return false;
+        }
+
+        private static bool LoadTempConfigFile()
+        {
+            try
+            {
+                var jsonString = File.ReadAllText(_userDefinedTempFilePath);
+                Instance = JsonHelper.JsonToObj<SonopostUserDefinedSettings>(jsonString);
+                if (Instance == null)
+                {
+                    Logger.WriteLineError($"SonopostUserDefinedSettings Load temp Config Fail,Instance is null");
+                    return false;
+                }
+                if (Instance.Version < SonopostConstants.CurrentSonopostUserDefinedSettingVersion)
+                {
+                    UpgradeSettingFrom0To1();
+                    UpgradeSettingFrom1To2();
+                    Instance.Save();
                 }
+                return true;
+            }
+            catch (Exception ex)
+            {
+                Logger.WriteLineError($"SonopostUserDefinedSettings Load temp Config Fail,ex:{ex}");
             }
+            return false;
+        }
 
+        private static bool LoadConfigFile()
+        {
+            try
+            {
+                var jsonString = File.ReadAllText(_userDefinedFilePath);
+                Instance = JsonHelper.JsonToObj<SonopostUserDefinedSettings>(jsonString);
+                if (Instance == null)
+                {
+                    Logger.WriteLineError($"SonopostUserDefinedSettings Load Config Fail,Instance is null");
+                    return false;
+                }
+                if (Instance.Version < SonopostConstants.CurrentSonopostUserDefinedSettingVersion)
+                {
+                    UpgradeSettingFrom0To1();
+                    UpgradeSettingFrom1To2();
+                    Instance.Save();
+                }
+                return true;
+            }
+            catch (Exception ex)
+            {
+                Logger.WriteLineError($"SonopostUserDefinedSettings Load Config Fail,ex:{ex}");
+            }
             return false;
         }
 
@@ -215,7 +240,7 @@ namespace Vinno.FIS.Sonopost.Settings
                     WriteIndented = true
                 });
                 File.WriteAllText(_userDefinedTempFilePath, jsonString);
-                FileHelper.MoveFile(_userDefinedTempFilePath, _userDefinedFilePath, true);
+                FileHelper.CopyFile(_userDefinedTempFilePath, _userDefinedFilePath, true);
             }
             catch (Exception ex)
             {