From 5ea6b1e08a438a78c68a241777d3c02602dc51dd Mon Sep 17 00:00:00 2001
From: 2dust <31833384+2dust@users.noreply.github.com>
Date: Mon, 21 Oct 2024 18:18:41 +0800
Subject: [PATCH] Update RetResult

---
 v2rayN/ServiceLib/Handler/ConfigHandler.cs    | 12 +++----
 .../ServiceLib/Handler/CoreConfigHandler.cs   | 16 +++++-----
 v2rayN/ServiceLib/Handler/CoreHandler.cs      |  6 ++--
 v2rayN/ServiceLib/Models/RetResult.cs         | 19 +++++++++--
 .../CoreConfig/CoreConfigClashService.cs      |  4 +--
 .../CoreConfig/CoreConfigSingboxService.cs    | 16 +++++-----
 .../CoreConfig/CoreConfigV2rayService.cs      | 12 +++----
 v2rayN/ServiceLib/Services/DownloadService.cs | 18 ++---------
 v2rayN/ServiceLib/Services/UpdateService.cs   | 32 ++++++-------------
 .../ViewModels/ProfilesViewModel.cs           |  6 ++--
 10 files changed, 64 insertions(+), 77 deletions(-)

diff --git a/v2rayN/ServiceLib/Handler/ConfigHandler.cs b/v2rayN/ServiceLib/Handler/ConfigHandler.cs
index f68c67d0..24d06e4d 100644
--- a/v2rayN/ServiceLib/Handler/ConfigHandler.cs
+++ b/v2rayN/ServiceLib/Handler/ConfigHandler.cs
@@ -1009,7 +1009,7 @@ namespace ServiceLib.Handler
             var configPath = Utils.GetConfigPath(Global.CoreMultipleLoadConfigFileName);
 
             var result = await CoreConfigHandler.GenerateClientMultipleLoadConfig(config, configPath, selecteds, coreType);
-            if (result.Code != 0)
+            if (result.Success != true)
             {
                 return result;
             }
@@ -1642,7 +1642,7 @@ namespace ServiceLib.Handler
         public static async Task<int> InitExternalRouting(Config config, bool blImportAdvancedRules = false)
         {
             var downloadHandle = new DownloadService();
-            var templateContent = Task.Run(() => downloadHandle.TryDownloadString(config.constItem.routeRulesTemplateSourceUrl, false, "")).Result;
+            var templateContent = await downloadHandle.TryDownloadString(config.constItem.routeRulesTemplateSourceUrl, true, "");
             if (string.IsNullOrEmpty(templateContent))
                 return await InitBuiltinRouting(config, blImportAdvancedRules); // fallback
 
@@ -1665,7 +1665,7 @@ namespace ServiceLib.Handler
 
                 var ruleSetsString = !string.IsNullOrEmpty(item.ruleSet)
                     ? item.ruleSet
-                    : Task.Run(() => downloadHandle.TryDownloadString(item.url, false, "")).Result;
+                    : await downloadHandle.TryDownloadString(item.url, true, "");
 
                 if (string.IsNullOrEmpty(ruleSetsString))
                     continue;
@@ -1795,7 +1795,7 @@ namespace ServiceLib.Handler
             var currentItem = await AppHandler.Instance.GetDNSItem(type);
 
             var downloadHandle = new DownloadService();
-            var templateContent = Task.Run(() => downloadHandle.TryDownloadString(url, true, "")).Result;
+            var templateContent = await downloadHandle.TryDownloadString(url, true, "");
             if (string.IsNullOrEmpty(templateContent))
                 return currentItem;
 
@@ -1804,10 +1804,10 @@ namespace ServiceLib.Handler
                 return currentItem;
 
             if (!string.IsNullOrEmpty(template.normalDNS))
-                template.normalDNS = Task.Run(() => downloadHandle.TryDownloadString(template.normalDNS, true, "")).Result;
+                template.normalDNS = await downloadHandle.TryDownloadString(template.normalDNS, true, "");
 
             if (!string.IsNullOrEmpty(template.tunDNS))
-                template.tunDNS = Task.Run(() => downloadHandle.TryDownloadString(template.tunDNS, true, "")).Result;
+                template.tunDNS = await downloadHandle.TryDownloadString(template.tunDNS, true, "");
 
             template.id = currentItem.id;
             template.enabled = currentItem.enabled;
diff --git a/v2rayN/ServiceLib/Handler/CoreConfigHandler.cs b/v2rayN/ServiceLib/Handler/CoreConfigHandler.cs
index e087b75a..40c00992 100644
--- a/v2rayN/ServiceLib/Handler/CoreConfigHandler.cs
+++ b/v2rayN/ServiceLib/Handler/CoreConfigHandler.cs
@@ -8,7 +8,7 @@
         public static async Task<RetResult> GenerateClientConfig(ProfileItem node, string? fileName)
         {
             var config = AppHandler.Instance.Config;
-            var result = new RetResult(-1);
+            var result = new RetResult();
 
             if (node.configType == EConfigType.Custom)
             {
@@ -33,7 +33,7 @@
             {
                 result = await new CoreConfigV2rayService(config).GenerateClientConfigContent(node);
             }
-            if (result.Code != 0)
+            if (result.Success != true)
             {
                 return result;
             }
@@ -47,7 +47,7 @@
 
         private static async Task<RetResult> GenerateClientCustomConfig(ProfileItem node, string? fileName)
         {
-            var ret = new RetResult(-1);
+            var ret = new RetResult();
             try
             {
                 if (node == null || fileName is null)
@@ -83,7 +83,7 @@
                 }
 
                 ret.Msg = string.Format(ResUI.SuccessfulConfiguration, "");
-                ret.Code = 0;
+                ret.Success = true;
                 return ret;
             }
             catch (Exception ex)
@@ -96,7 +96,7 @@
 
         public static async Task<RetResult> GenerateClientSpeedtestConfig(Config config, string fileName, List<ServerTestItem> selecteds, ECoreType coreType)
         {
-            var result = new RetResult(-1);
+            var result = new RetResult();
             if (coreType == ECoreType.sing_box)
             {
                 result = await new CoreConfigSingboxService(config).GenerateClientSpeedtestConfig(selecteds);
@@ -105,7 +105,7 @@
             {
                 result = await new CoreConfigV2rayService(config).GenerateClientSpeedtestConfig(selecteds);
             }
-            if (result.Code != 0)
+            if (result.Success != true)
             {
                 return result;
             }
@@ -115,7 +115,7 @@
 
         public static async Task<RetResult> GenerateClientMultipleLoadConfig(Config config, string fileName, List<ProfileItem> selecteds, ECoreType coreType)
         {
-            var result = new RetResult(-1);
+            var result = new RetResult();
             if (coreType == ECoreType.sing_box)
             {
                 result = await new CoreConfigSingboxService(config).GenerateClientMultipleLoadConfig(selecteds);
@@ -125,7 +125,7 @@
                 result = await new CoreConfigV2rayService(config).GenerateClientMultipleLoadConfig(selecteds);
             }
 
-            if (result.Code != 0)
+            if (result.Success != true)
             {
                 return result;
             }
diff --git a/v2rayN/ServiceLib/Handler/CoreHandler.cs b/v2rayN/ServiceLib/Handler/CoreHandler.cs
index 9f779558..f843aaf7 100644
--- a/v2rayN/ServiceLib/Handler/CoreHandler.cs
+++ b/v2rayN/ServiceLib/Handler/CoreHandler.cs
@@ -35,7 +35,7 @@ namespace ServiceLib.Handler
             var fileName = Utils.GetConfigPath(Global.CoreConfigFileName);
             var result = await CoreConfigHandler.GenerateClientConfig(node, fileName);
             ShowMsg(false, result.Msg);
-            if (result.Code != 0)
+            if (result.Success != true)
             {
                 return;
             }
@@ -72,7 +72,7 @@ namespace ServiceLib.Handler
             var configPath = Utils.GetConfigPath(Global.CoreSpeedtestConfigFileName);
             var result = await CoreConfigHandler.GenerateClientSpeedtestConfig(_config, configPath, selecteds, coreType);
             ShowMsg(false, result.Msg);
-            if (result.Code == 0)
+            if (result.Success)
             {
                 pid = CoreStartSpeedtest(configPath, coreType);
             }
@@ -225,7 +225,7 @@ namespace ServiceLib.Handler
                 {
                     string fileName2 = Utils.GetConfigPath(Global.CorePreConfigFileName);
                     var result = await CoreConfigHandler.GenerateClientConfig(itemSocks, fileName2);
-                    if (result.Code == 0)
+                    if (result.Success)
                     {
                         var coreInfo2 = CoreInfoHandler.Instance.GetCoreInfo(preCoreType);
                         var proc2 = RunProcess(node, coreInfo2, $" -c {Global.CorePreConfigFileName}", true);
diff --git a/v2rayN/ServiceLib/Models/RetResult.cs b/v2rayN/ServiceLib/Models/RetResult.cs
index d8914707..9c6122c0 100644
--- a/v2rayN/ServiceLib/Models/RetResult.cs
+++ b/v2rayN/ServiceLib/Models/RetResult.cs
@@ -2,13 +2,26 @@
 {
     public class RetResult
     {
-        public int Code { get; set; }
+        public bool Success { get; set; }
         public string? Msg { get; set; }
         public object? Data { get; set; }
 
-        public RetResult(int code)
+        public RetResult(bool success = false)
         {
-            Code = code;
+            Success = success;
+        }
+
+        public RetResult(bool success, string? msg)
+        {
+            Success = success;
+            Msg = msg;
+        }
+
+        public RetResult(bool success, string? msg, object? data)
+        {
+            Success = success;
+            Msg = msg;
+            Data = data;
         }
     }
 }
\ No newline at end of file
diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigClashService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigClashService.cs
index 36bf5aaf..58e03603 100644
--- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigClashService.cs
+++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigClashService.cs
@@ -21,7 +21,7 @@
         /// <returns></returns>
         public async Task<RetResult> GenerateClientCustomConfig(ProfileItem node, string? fileName)
         {
-            var ret = new RetResult(-1);
+            var ret = new RetResult();
             if (node == null || fileName is null)
             {
                 ret.Msg = ResUI.CheckServerSettings;
@@ -148,7 +148,7 @@
                 ClashApiHandler.Instance.ProfileContent = fileContent;
 
                 ret.Msg = string.Format(ResUI.SuccessfulConfiguration, $"{node.GetSummary()}");
-                ret.Code = 0;
+                ret.Success = true;
                 return ret;
             }
             catch (Exception ex)
diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs
index 925685b1..93575e33 100644
--- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs
+++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs
@@ -17,7 +17,7 @@ namespace ServiceLib.Services.CoreConfig
 
         public async Task<RetResult> GenerateClientConfigContent(ProfileItem node)
         {
-            var ret = new RetResult(-1);
+            var ret = new RetResult();
             try
             {
                 if (node == null
@@ -65,7 +65,7 @@ namespace ServiceLib.Services.CoreConfig
                 await ConvertGeo2Ruleset(singboxConfig);
 
                 ret.Msg = string.Format(ResUI.SuccessfulConfiguration, "");
-                ret.Code = 0;
+                ret.Success = true;
                 ret.Data = JsonUtils.Serialize(singboxConfig);
                 return ret;
             }
@@ -79,7 +79,7 @@ namespace ServiceLib.Services.CoreConfig
 
         public async Task<RetResult> GenerateClientSpeedtestConfig(List<ServerTestItem> selecteds)
         {
-            var ret = new RetResult(-1);
+            var ret = new RetResult();
             try
             {
                 if (_config == null)
@@ -229,7 +229,7 @@ namespace ServiceLib.Services.CoreConfig
                 //}
 
                 //ret.Msg =string.Format(ResUI.SuccessfulConfiguration"), node.getSummary());
-                ret.Code = 0;
+                ret.Success = true;
                 ret.Data = JsonUtils.Serialize(singboxConfig);
                 return ret;
             }
@@ -243,7 +243,7 @@ namespace ServiceLib.Services.CoreConfig
 
         public async Task<RetResult> GenerateClientMultipleLoadConfig(List<ProfileItem> selecteds)
         {
-            var ret = new RetResult(-1);
+            var ret = new RetResult();
             try
             {
                 if (_config == null)
@@ -345,7 +345,7 @@ namespace ServiceLib.Services.CoreConfig
                 outSelector.outbounds.Insert(0, outUrltest.tag);
                 singboxConfig.outbounds.Add(outSelector);
 
-                ret.Code = 0;
+                ret.Success = true;
                 ret.Data = JsonUtils.Serialize(singboxConfig);
                 return ret;
             }
@@ -359,7 +359,7 @@ namespace ServiceLib.Services.CoreConfig
 
         public async Task<RetResult> GenerateClientCustomConfig(ProfileItem node, string? fileName)
         {
-            var ret = new RetResult(-1);
+            var ret = new RetResult();
             if (node == null || fileName is null)
             {
                 ret.Msg = ResUI.CheckServerSettings;
@@ -425,7 +425,7 @@ namespace ServiceLib.Services.CoreConfig
                 }
 
                 ret.Msg = string.Format(ResUI.SuccessfulConfiguration, "");
-                ret.Code = 0;
+                ret.Success = true;
                 return ret;
             }
             catch (Exception ex)
diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigV2rayService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigV2rayService.cs
index 2a40bc53..9b7cf9c7 100644
--- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigV2rayService.cs
+++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigV2rayService.cs
@@ -17,7 +17,7 @@ namespace ServiceLib.Services.CoreConfig
 
         public async Task<RetResult> GenerateClientConfigContent(ProfileItem node)
         {
-            var ret = new RetResult(-1);
+            var ret = new RetResult();
             try
             {
                 if (node == null
@@ -58,7 +58,7 @@ namespace ServiceLib.Services.CoreConfig
                 await GenStatistic(v2rayConfig);
 
                 ret.Msg = string.Format(ResUI.SuccessfulConfiguration, "");
-                ret.Code = 0;
+                ret.Success = true;
                 ret.Data = JsonUtils.Serialize(v2rayConfig);
                 return ret;
             }
@@ -72,7 +72,7 @@ namespace ServiceLib.Services.CoreConfig
 
         public async Task<RetResult> GenerateClientMultipleLoadConfig(List<ProfileItem> selecteds)
         {
-            var ret = new RetResult(-1);
+            var ret = new RetResult();
 
             try
             {
@@ -185,7 +185,7 @@ namespace ServiceLib.Services.CoreConfig
                     });
                 }
 
-                ret.Code = 0;
+                ret.Success = true;
                 ret.Data = JsonUtils.Serialize(v2rayConfig);
                 return ret;
             }
@@ -199,7 +199,7 @@ namespace ServiceLib.Services.CoreConfig
 
         public async Task<RetResult> GenerateClientSpeedtestConfig(List<ServerTestItem> selecteds)
         {
-            var ret = new RetResult(-1);
+            var ret = new RetResult();
             try
             {
                 if (_config == null)
@@ -336,7 +336,7 @@ namespace ServiceLib.Services.CoreConfig
                 }
 
                 //ret.Msg =string.Format(ResUI.SuccessfulConfiguration"), node.getSummary());
-                ret.Code = 0;
+                ret.Success = true;
                 ret.Data = JsonUtils.Serialize(v2rayConfig);
                 return ret;
             }
diff --git a/v2rayN/ServiceLib/Services/DownloadService.cs b/v2rayN/ServiceLib/Services/DownloadService.cs
index 0a6c5ec7..40e8718a 100644
--- a/v2rayN/ServiceLib/Services/DownloadService.cs
+++ b/v2rayN/ServiceLib/Services/DownloadService.cs
@@ -10,22 +10,10 @@ namespace ServiceLib.Services
     /// </summary>
     public class DownloadService
     {
-        public event EventHandler<ResultEventArgs>? UpdateCompleted;
+        public event EventHandler<RetResult>? UpdateCompleted;
 
         public event ErrorEventHandler? Error;
 
-        public class ResultEventArgs : EventArgs
-        {
-            public bool Success;
-            public string Msg;
-
-            public ResultEventArgs(bool success, string msg)
-            {
-                Success = success;
-                Msg = msg;
-            }
-        }
-
         public async Task<int> DownloadDataAsync(string url, WebProxy webProxy, int downloadTimeout, Action<bool, string> updateFunc)
         {
             try
@@ -63,12 +51,12 @@ namespace ServiceLib.Services
             try
             {
                 SetSecurityProtocol(AppHandler.Instance.Config.guiItem.enableSecurityProtocolTls13);
-                UpdateCompleted?.Invoke(this, new ResultEventArgs(false, $"{ResUI.Downloading}   {url}"));
+                UpdateCompleted?.Invoke(this, new RetResult(false, $"{ResUI.Downloading}   {url}"));
 
                 var progress = new Progress<double>();
                 progress.ProgressChanged += (sender, value) =>
                 {
-                    UpdateCompleted?.Invoke(this, new ResultEventArgs(value > 100, $"...{value}%"));
+                    UpdateCompleted?.Invoke(this, new RetResult(value > 100, $"...{value}%"));
                 };
 
                 var webProxy = GetWebProxy(blProxy);
diff --git a/v2rayN/ServiceLib/Services/UpdateService.cs b/v2rayN/ServiceLib/Services/UpdateService.cs
index 4d70fddd..537487d3 100644
--- a/v2rayN/ServiceLib/Services/UpdateService.cs
+++ b/v2rayN/ServiceLib/Services/UpdateService.cs
@@ -9,20 +9,6 @@ namespace ServiceLib.Services
         private Config _config;
         private int _timeout = 30;
 
-        private class ResultEventArgs
-        {
-            public bool Success;
-            public string Msg;
-            public string Url;
-
-            public ResultEventArgs(bool success, string msg, string url = "")
-            {
-                Success = success;
-                Msg = msg;
-                Url = url;
-            }
-        }
-
         public async Task CheckUpdateGuiN(Config config, Action<bool, string> updateFunc, bool preRelease)
         {
             _config = config;
@@ -55,7 +41,7 @@ namespace ServiceLib.Services
                 _updateFunc?.Invoke(false, string.Format(ResUI.MsgParsingSuccessfully, ECoreType.v2rayN));
                 _updateFunc?.Invoke(false, args.Msg);
 
-                url = args.Url;
+                url = args.Data?.ToString();
                 fileName = Utils.GetTempPath(Utils.GetGuid());
                 await downloadHandle.DownloadFileAsync(url, fileName, true, _timeout);
             }
@@ -106,7 +92,7 @@ namespace ServiceLib.Services
                 _updateFunc?.Invoke(false, string.Format(ResUI.MsgParsingSuccessfully, type));
                 _updateFunc?.Invoke(false, args.Msg);
 
-                url = args.Url;
+                url = args.Data?.ToString();
                 var ext = url.Contains(".tar.gz") ? ".tar.gz" : Path.GetExtension(url);
                 fileName = Utils.GetTempPath(Utils.GetGuid() + ext);
                 await downloadHandle.DownloadFileAsync(url, fileName, true, _timeout);
@@ -266,7 +252,7 @@ namespace ServiceLib.Services
 
         #region private
 
-        private async Task<ResultEventArgs> CheckUpdateAsync(DownloadService downloadHandle, ECoreType type, bool preRelease)
+        private async Task<RetResult> CheckUpdateAsync(DownloadService downloadHandle, ECoreType type, bool preRelease)
         {
             try
             {
@@ -280,14 +266,14 @@ namespace ServiceLib.Services
                 }
                 else
                 {
-                    return new ResultEventArgs(false, "");
+                    return new RetResult(false, "");
                 }
             }
             catch (Exception ex)
             {
                 Logging.SaveLog(ex.Message, ex);
                 _updateFunc?.Invoke(false, ex.Message);
-                return new ResultEventArgs(false, ex.Message);
+                return new RetResult(false, ex.Message);
             }
         }
 
@@ -347,7 +333,7 @@ namespace ServiceLib.Services
             }
         }
 
-        private async Task<ResultEventArgs> ParseDownloadUrl(ECoreType type, string gitHubReleaseApi, bool preRelease)
+        private async Task<RetResult> ParseDownloadUrl(ECoreType type, string gitHubReleaseApi, bool preRelease)
         {
             try
             {
@@ -398,16 +384,16 @@ namespace ServiceLib.Services
 
                 if (curVersion >= version && version != new SemanticVersion(0, 0, 0))
                 {
-                    return new ResultEventArgs(false, message);
+                    return new RetResult(false, message);
                 }
 
-                return new ResultEventArgs(true, body, url);
+                return new RetResult(true, body, url);
             }
             catch (Exception ex)
             {
                 Logging.SaveLog(ex.Message, ex);
                 _updateFunc?.Invoke(false, ex.Message);
-                return new ResultEventArgs(false, ex.Message);
+                return new RetResult(false, ex.Message);
             }
         }
 
diff --git a/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs b/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs
index 065da8d0..39ed44e7 100644
--- a/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs
+++ b/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs
@@ -586,7 +586,7 @@ namespace ServiceLib.ViewModels
             }
 
             var ret = await ConfigHandler.AddCustomServer4Multiple(_config, lstSelecteds, coreType);
-            if (ret.Code != 0)
+            if (ret.Success != true)
             {
                 NoticeHandler.Instance.Enqueue(ResUI.OperationFailed);
                 return;
@@ -705,7 +705,7 @@ namespace ServiceLib.ViewModels
             if (blClipboard)
             {
                 var result = await CoreConfigHandler.GenerateClientConfig(item, null);
-                if (result.Code != 0)
+                if (result.Success != true)
                 {
                     NoticeHandler.Instance.Enqueue(result.Msg);
                 }
@@ -728,7 +728,7 @@ namespace ServiceLib.ViewModels
                 return;
             }
             var result = await CoreConfigHandler.GenerateClientConfig(item, null);
-            if (result.Code != 0)
+            if (result.Success != true)
             {
                 NoticeHandler.Instance.Enqueue(result.Msg);
             }