Skip to content

Commit

Permalink
Update RetResult
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Oct 21, 2024
1 parent 3dd5431 commit 5ea6b1e
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 77 deletions.
12 changes: 6 additions & 6 deletions v2rayN/ServiceLib/Handler/ConfigHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,7 @@ public static async Task<RetResult> AddCustomServer4Multiple(Config config, List
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;
}
Expand Down Expand Up @@ -1642,7 +1642,7 @@ public static async Task<int> InitRouting(Config config, bool blImportAdvancedRu
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

Expand All @@ -1665,7 +1665,7 @@ public static async Task<int> InitExternalRouting(Config config, bool blImportAd

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;
Expand Down Expand Up @@ -1795,7 +1795,7 @@ public static async Task<DNSItem> GetExternalDNSItem(ECoreType type, string url)
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;

Expand All @@ -1804,10 +1804,10 @@ public static async Task<DNSItem> GetExternalDNSItem(ECoreType type, string url)
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;
Expand Down
16 changes: 8 additions & 8 deletions v2rayN/ServiceLib/Handler/CoreConfigHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class CoreConfigHandler
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)
{
Expand All @@ -33,7 +33,7 @@ public static async Task<RetResult> GenerateClientConfig(ProfileItem node, strin
{
result = await new CoreConfigV2rayService(config).GenerateClientConfigContent(node);
}
if (result.Code != 0)
if (result.Success != true)
{
return result;
}
Expand All @@ -47,7 +47,7 @@ public static async Task<RetResult> GenerateClientConfig(ProfileItem node, strin

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)
Expand Down Expand Up @@ -83,7 +83,7 @@ private static async Task<RetResult> GenerateClientCustomConfig(ProfileItem node
}

ret.Msg = string.Format(ResUI.SuccessfulConfiguration, "");
ret.Code = 0;
ret.Success = true;
return ret;
}
catch (Exception ex)
Expand All @@ -96,7 +96,7 @@ private static async Task<RetResult> GenerateClientCustomConfig(ProfileItem node

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);
Expand All @@ -105,7 +105,7 @@ public static async Task<RetResult> GenerateClientSpeedtestConfig(Config config,
{
result = await new CoreConfigV2rayService(config).GenerateClientSpeedtestConfig(selecteds);
}
if (result.Code != 0)
if (result.Success != true)
{
return result;
}
Expand All @@ -115,7 +115,7 @@ public static async Task<RetResult> GenerateClientSpeedtestConfig(Config config,

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);
Expand All @@ -125,7 +125,7 @@ public static async Task<RetResult> GenerateClientMultipleLoadConfig(Config conf
result = await new CoreConfigV2rayService(config).GenerateClientMultipleLoadConfig(selecteds);
}

if (result.Code != 0)
if (result.Success != true)
{
return result;
}
Expand Down
6 changes: 3 additions & 3 deletions v2rayN/ServiceLib/Handler/CoreHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public async Task LoadCore(ProfileItem? node)
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;
}
Expand Down Expand Up @@ -72,7 +72,7 @@ public async Task<int> LoadCoreConfigSpeedtest(List<ServerTestItem> selecteds)
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);
}
Expand Down Expand Up @@ -225,7 +225,7 @@ private async Task CoreStart(ProfileItem node)
{
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);
Expand Down
19 changes: 16 additions & 3 deletions v2rayN/ServiceLib/Models/RetResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public CoreConfigClashService(Config config)
/// <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;
Expand Down Expand Up @@ -148,7 +148,7 @@ public async Task<RetResult> GenerateClientCustomConfig(ProfileItem node, string
ClashApiHandler.Instance.ProfileContent = fileContent;

ret.Msg = string.Format(ResUI.SuccessfulConfiguration, $"{node.GetSummary()}");
ret.Code = 0;
ret.Success = true;
return ret;
}
catch (Exception ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public CoreConfigSingboxService(Config config)

public async Task<RetResult> GenerateClientConfigContent(ProfileItem node)
{
var ret = new RetResult(-1);
var ret = new RetResult();
try
{
if (node == null
Expand Down Expand Up @@ -65,7 +65,7 @@ public async Task<RetResult> GenerateClientConfigContent(ProfileItem node)
await ConvertGeo2Ruleset(singboxConfig);

ret.Msg = string.Format(ResUI.SuccessfulConfiguration, "");
ret.Code = 0;
ret.Success = true;
ret.Data = JsonUtils.Serialize(singboxConfig);
return ret;
}
Expand All @@ -79,7 +79,7 @@ public async Task<RetResult> GenerateClientConfigContent(ProfileItem node)

public async Task<RetResult> GenerateClientSpeedtestConfig(List<ServerTestItem> selecteds)
{
var ret = new RetResult(-1);
var ret = new RetResult();
try
{
if (_config == null)
Expand Down Expand Up @@ -229,7 +229,7 @@ public async Task<RetResult> GenerateClientSpeedtestConfig(List<ServerTestItem>
//}

//ret.Msg =string.Format(ResUI.SuccessfulConfiguration"), node.getSummary());
ret.Code = 0;
ret.Success = true;
ret.Data = JsonUtils.Serialize(singboxConfig);
return ret;
}
Expand All @@ -243,7 +243,7 @@ public async Task<RetResult> GenerateClientSpeedtestConfig(List<ServerTestItem>

public async Task<RetResult> GenerateClientMultipleLoadConfig(List<ProfileItem> selecteds)
{
var ret = new RetResult(-1);
var ret = new RetResult();
try
{
if (_config == null)
Expand Down Expand Up @@ -345,7 +345,7 @@ public async Task<RetResult> GenerateClientMultipleLoadConfig(List<ProfileItem>
outSelector.outbounds.Insert(0, outUrltest.tag);
singboxConfig.outbounds.Add(outSelector);

ret.Code = 0;
ret.Success = true;
ret.Data = JsonUtils.Serialize(singboxConfig);
return ret;
}
Expand All @@ -359,7 +359,7 @@ public async Task<RetResult> GenerateClientMultipleLoadConfig(List<ProfileItem>

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;
Expand Down Expand Up @@ -425,7 +425,7 @@ public async Task<RetResult> GenerateClientCustomConfig(ProfileItem node, string
}

ret.Msg = string.Format(ResUI.SuccessfulConfiguration, "");
ret.Code = 0;
ret.Success = true;
return ret;
}
catch (Exception ex)
Expand Down
12 changes: 6 additions & 6 deletions v2rayN/ServiceLib/Services/CoreConfig/CoreConfigV2rayService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public CoreConfigV2rayService(Config config)

public async Task<RetResult> GenerateClientConfigContent(ProfileItem node)
{
var ret = new RetResult(-1);
var ret = new RetResult();
try
{
if (node == null
Expand Down Expand Up @@ -58,7 +58,7 @@ public async Task<RetResult> GenerateClientConfigContent(ProfileItem node)
await GenStatistic(v2rayConfig);

ret.Msg = string.Format(ResUI.SuccessfulConfiguration, "");
ret.Code = 0;
ret.Success = true;
ret.Data = JsonUtils.Serialize(v2rayConfig);
return ret;
}
Expand All @@ -72,7 +72,7 @@ public async Task<RetResult> GenerateClientConfigContent(ProfileItem node)

public async Task<RetResult> GenerateClientMultipleLoadConfig(List<ProfileItem> selecteds)
{
var ret = new RetResult(-1);
var ret = new RetResult();

try
{
Expand Down Expand Up @@ -185,7 +185,7 @@ public async Task<RetResult> GenerateClientMultipleLoadConfig(List<ProfileItem>
});
}

ret.Code = 0;
ret.Success = true;
ret.Data = JsonUtils.Serialize(v2rayConfig);
return ret;
}
Expand All @@ -199,7 +199,7 @@ public async Task<RetResult> GenerateClientMultipleLoadConfig(List<ProfileItem>

public async Task<RetResult> GenerateClientSpeedtestConfig(List<ServerTestItem> selecteds)
{
var ret = new RetResult(-1);
var ret = new RetResult();
try
{
if (_config == null)
Expand Down Expand Up @@ -336,7 +336,7 @@ public async Task<RetResult> GenerateClientSpeedtestConfig(List<ServerTestItem>
}

//ret.Msg =string.Format(ResUI.SuccessfulConfiguration"), node.getSummary());
ret.Code = 0;
ret.Success = true;
ret.Data = JsonUtils.Serialize(v2rayConfig);
return ret;
}
Expand Down
18 changes: 3 additions & 15 deletions v2rayN/ServiceLib/Services/DownloadService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -63,12 +51,12 @@ public async Task DownloadFileAsync(string url, string fileName, bool blProxy, i
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);
Expand Down
Loading

0 comments on commit 5ea6b1e

Please sign in to comment.