Skip to content

Commit

Permalink
Add IsNotEmpty function
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Sep 17, 2024
1 parent 4c0a59a commit 61635db
Show file tree
Hide file tree
Showing 33 changed files with 140 additions and 135 deletions.
2 changes: 1 addition & 1 deletion v2rayN/ServiceLib/Common/DownloaderHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class DownloaderHelper
Uri uri = new(url);
//Authorization Header
var headers = new WebHeaderCollection();
if (!Utils.IsNullOrEmpty(uri.UserInfo))
if (Utils.IsNotEmpty(uri.UserInfo))
{
headers.Add(HttpRequestHeader.Authorization, "Basic " + Utils.Base64Encode(uri.UserInfo));
}
Expand Down
4 changes: 2 additions & 2 deletions v2rayN/ServiceLib/Common/FileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static bool ZipExtractToFile(string fileName, string toPath, string ignor
}
try
{
if (!Utils.IsNullOrEmpty(ignoredName) && entry.Name.Contains(ignoredName))
if (Utils.IsNotEmpty(ignoredName) && entry.Name.Contains(ignoredName))
{
continue;
}
Expand Down Expand Up @@ -157,7 +157,7 @@ public static void CopyDirectory(string sourceDir, string destinationDir, bool r
// Get the files in the source directory and copy to the destination directory
foreach (FileInfo file in dir.GetFiles())
{
if (!Utils.IsNullOrEmpty(ignoredName) && file.Name.Contains(ignoredName))
if (Utils.IsNotEmpty(ignoredName) && file.Name.Contains(ignoredName))
{
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/ServiceLib/Common/HttpClientHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class HttpClientHelper

public async Task<string?> TryGetAsync(string url)
{
if (string.IsNullOrEmpty(url))
if (Utils.IsNullOrEmpty(url))
return null;

try
Expand Down
5 changes: 5 additions & 0 deletions v2rayN/ServiceLib/Common/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,11 @@ public static bool IsNullOrEmpty(string? text)
return false;
}

public static bool IsNotEmpty(string? text)
{
return !string.IsNullOrEmpty(text);
}

/// <summary>
/// 验证IP地址是否合法
/// </summary>
Expand Down
22 changes: 11 additions & 11 deletions v2rayN/ServiceLib/Handler/ConfigHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static int LoadConfig(ref Config? config)
{
//载入配置文件
var result = Utils.LoadResource(Utils.GetConfigPath(configRes));
if (!Utils.IsNullOrEmpty(result))
if (Utils.IsNotEmpty(result))
{
//转成Json
config = JsonUtils.Deserialize<Config>(result);
Expand Down Expand Up @@ -1007,7 +1007,7 @@ public static int AddVlessServer(Config config, ProfileItem profileItem, bool to
{
return -1;
}
if (!Utils.IsNullOrEmpty(profileItem.security) && profileItem.security != Global.None)
if (Utils.IsNotEmpty(profileItem.security) && profileItem.security != Global.None)
{
profileItem.security = Global.None;
}
Expand Down Expand Up @@ -1045,7 +1045,7 @@ public static int AddServerCommon(Config config, ProfileItem profileItem, bool t
{
profileItem.configVersion = 2;

if (!Utils.IsNullOrEmpty(profileItem.streamSecurity))
if (Utils.IsNotEmpty(profileItem.streamSecurity))
{
if (profileItem.streamSecurity != Global.StreamSecurity
&& profileItem.streamSecurity != Global.StreamSecurityReality)
Expand All @@ -1065,7 +1065,7 @@ public static int AddServerCommon(Config config, ProfileItem profileItem, bool t
}
}

if (!Utils.IsNullOrEmpty(profileItem.network) && !Global.Networks.Contains(profileItem.network))
if (Utils.IsNotEmpty(profileItem.network) && !Global.Networks.Contains(profileItem.network))
{
profileItem.network = Global.DefaultNetwork;
}
Expand Down Expand Up @@ -1186,7 +1186,7 @@ private static int AddBatchServers(Config config, string strData, string subid,

string subFilter = string.Empty;
//remove sub items
if (isSub && !Utils.IsNullOrEmpty(subid))
if (isSub && Utils.IsNotEmpty(subid))
{
RemoveServerViaSubid(config, subid, isSub);
subFilter = LazyConfig.Instance.GetSubItem(subid)?.filter ?? "";
Expand Down Expand Up @@ -1219,7 +1219,7 @@ private static int AddBatchServers(Config config, string strData, string subid,
}

//exist sub items
if (isSub && !Utils.IsNullOrEmpty(subid))
if (isSub && Utils.IsNotEmpty(subid))
{
var existItem = lstOriSub?.FirstOrDefault(t => t.isSub == isSub
&& config.uiItem.enableUpdateSubOnlyRemarksExist ? t.remarks == profileItem.remarks : CompareProfileItem(t, profileItem, true));
Expand All @@ -1241,7 +1241,7 @@ private static int AddBatchServers(Config config, string strData, string subid,
}
}
//filter
if (!Utils.IsNullOrEmpty(subFilter))
if (Utils.IsNotEmpty(subFilter))
{
if (!Regex.IsMatch(profileItem.remarks, subFilter))
{
Expand Down Expand Up @@ -1305,7 +1305,7 @@ private static int AddBatchServers4Custom(Config config, string strData, string
}
if (lstProfiles != null && lstProfiles.Count > 0)
{
if (isSub && !Utils.IsNullOrEmpty(subid))
if (isSub && Utils.IsNotEmpty(subid))
{
RemoveServerViaSubid(config, subid, isSub);
}
Expand Down Expand Up @@ -1361,7 +1361,7 @@ private static int AddBatchServers4Custom(Config config, string strData, string
return -1;
}

if (isSub && !Utils.IsNullOrEmpty(subid))
if (isSub && Utils.IsNotEmpty(subid))
{
RemoveServerViaSubid(config, subid, isSub);
}
Expand Down Expand Up @@ -1389,7 +1389,7 @@ private static int AddBatchServers4SsSIP008(Config config, string strData, strin
return -1;
}

if (isSub && !Utils.IsNullOrEmpty(subid))
if (isSub && Utils.IsNotEmpty(subid))
{
RemoveServerViaSubid(config, subid, isSub);
}
Expand Down Expand Up @@ -1421,7 +1421,7 @@ public static int AddBatchServers(Config config, string strData, string subid, b
return -1;
}
List<ProfileItem>? lstOriSub = null;
if (isSub && !Utils.IsNullOrEmpty(subid))
if (isSub && Utils.IsNotEmpty(subid))
{
lstOriSub = LazyConfig.Instance.ProfileItems(subid);
}
Expand Down
4 changes: 2 additions & 2 deletions v2rayN/ServiceLib/Handler/CoreConfig/CoreConfigClash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public int GenerateClientCustomConfig(ProfileItem node, string? fileName, out st
}

string addressFileName = node.address;
if (string.IsNullOrEmpty(addressFileName))
if (Utils.IsNullOrEmpty(addressFileName))
{
msg = ResUI.FailedGetDefaultConfiguration;
return -1;
Expand Down Expand Up @@ -117,7 +117,7 @@ public int GenerateClientCustomConfig(ProfileItem node, string? fileName, out st
if (_config.tunModeItem.enableTun)
{
string tun = Utils.GetEmbedText(Global.ClashTunYaml);
if (!string.IsNullOrEmpty(tun))
if (Utils.IsNotEmpty(tun))
{
var tunContent = YamlUtils.FromYaml<Dictionary<string, object>>(tun);
if (tunContent != null)
Expand Down
36 changes: 18 additions & 18 deletions v2rayN/ServiceLib/Handler/CoreConfig/CoreConfigSingbox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ public int GenerateClientCustomConfig(ProfileItem node, string? fileName, out st
}

string addressFileName = node.address;
if (string.IsNullOrEmpty(addressFileName))
if (Utils.IsNullOrEmpty(addressFileName))
{
msg = ResUI.FailedGetDefaultConfiguration;
return -1;
Expand Down Expand Up @@ -489,7 +489,7 @@ private int GenInbounds(SingboxConfig singboxConfig)
if (_config.routingBasicItem.enableRoutingAdvanced)
{
var routing = ConfigHandler.GetDefaultRouting(_config);
if (!Utils.IsNullOrEmpty(routing.domainStrategy4Singbox))
if (Utils.IsNotEmpty(routing.domainStrategy4Singbox))
{
inbound.domain_strategy = routing.domainStrategy4Singbox;
}
Expand All @@ -512,7 +512,7 @@ private int GenInbounds(SingboxConfig singboxConfig)
singboxConfig.inbounds.Add(inbound4);

//auth
if (!Utils.IsNullOrEmpty(_config.inbound[0].user) && !Utils.IsNullOrEmpty(_config.inbound[0].pass))
if (Utils.IsNotEmpty(_config.inbound[0].user) && Utils.IsNotEmpty(_config.inbound[0].pass))
{
inbound3.users = new() { new() { username = _config.inbound[0].user, password = _config.inbound[0].pass } };
inbound4.users = new() { new() { username = _config.inbound[0].user, password = _config.inbound[0].pass } };
Expand Down Expand Up @@ -604,8 +604,8 @@ private int GenOutbound(ProfileItem node, Outbound4Sbox outbound)
case EConfigType.Socks:
{
outbound.version = "5";
if (!Utils.IsNullOrEmpty(node.security)
&& !Utils.IsNullOrEmpty(node.id))
if (Utils.IsNotEmpty(node.security)
&& Utils.IsNotEmpty(node.id))
{
outbound.username = node.security;
outbound.password = node.id;
Expand All @@ -614,8 +614,8 @@ private int GenOutbound(ProfileItem node, Outbound4Sbox outbound)
}
case EConfigType.Http:
{
if (!Utils.IsNullOrEmpty(node.security)
&& !Utils.IsNullOrEmpty(node.id))
if (Utils.IsNotEmpty(node.security)
&& Utils.IsNotEmpty(node.id))
{
outbound.username = node.security;
outbound.password = node.id;
Expand Down Expand Up @@ -649,7 +649,7 @@ private int GenOutbound(ProfileItem node, Outbound4Sbox outbound)
{
outbound.password = node.id;

if (!Utils.IsNullOrEmpty(node.path))
if (Utils.IsNotEmpty(node.path))
{
outbound.obfs = new()
{
Expand Down Expand Up @@ -695,7 +695,7 @@ private int GenOutboundMux(ProfileItem node, Outbound4Sbox outbound)
{
try
{
if (_config.coreBasicItem.muxEnabled && !Utils.IsNullOrEmpty(_config.mux4SboxItem.protocol))
if (_config.coreBasicItem.muxEnabled && Utils.IsNotEmpty(_config.mux4SboxItem.protocol))
{
var mux = new Multiplex4Sbox()
{
Expand All @@ -721,11 +721,11 @@ private int GenOutboundTls(ProfileItem node, Outbound4Sbox outbound)
if (node.streamSecurity == Global.StreamSecurityReality || node.streamSecurity == Global.StreamSecurity)
{
var server_name = string.Empty;
if (!Utils.IsNullOrEmpty(node.sni))
if (Utils.IsNotEmpty(node.sni))
{
server_name = node.sni;
}
else if (!Utils.IsNullOrEmpty(node.requestHost))
else if (Utils.IsNotEmpty(node.requestHost))
{
server_name = Utils.String2List(node.requestHost)[0];
}
Expand All @@ -736,7 +736,7 @@ private int GenOutboundTls(ProfileItem node, Outbound4Sbox outbound)
insecure = Utils.ToBool(node.allowInsecure.IsNullOrEmpty() ? _config.coreBasicItem.defAllowInsecure.ToString().ToLower() : node.allowInsecure),
alpn = node.GetAlpn(),
};
if (!Utils.IsNullOrEmpty(node.fingerprint))
if (Utils.IsNotEmpty(node.fingerprint))
{
tls.utls = new Utls4Sbox()
{
Expand Down Expand Up @@ -798,7 +798,7 @@ private int GenOutboundTransport(ProfileItem node, Outbound4Sbox outbound)
case nameof(ETransport.ws):
transport.type = nameof(ETransport.ws);
transport.path = Utils.IsNullOrEmpty(node.path) ? null : node.path;
if (!Utils.IsNullOrEmpty(node.requestHost))
if (Utils.IsNotEmpty(node.requestHost))
{
transport.headers = new()
{
Expand Down Expand Up @@ -1020,7 +1020,7 @@ private int GenRoutingUserRule(RulesItem item, List<Rule4Sbox> rules)
outbound = item.outboundTag,
};

if (!Utils.IsNullOrEmpty(item.port))
if (Utils.IsNotEmpty(item.port))
{
if (item.port.Contains("-"))
{
Expand All @@ -1031,7 +1031,7 @@ private int GenRoutingUserRule(RulesItem item, List<Rule4Sbox> rules)
rule.port = new List<int> { Utils.ToInt(item.port) };
}
}
if (!Utils.IsNullOrEmpty(item.network))
if (Utils.IsNotEmpty(item.network))
{
rule.network = Utils.String2List(item.network);
}
Expand Down Expand Up @@ -1221,7 +1221,7 @@ private int GenDnsDomains(ProfileItem? node, SingboxConfig singboxConfig, DNSIte
});

var lstDomain = singboxConfig.outbounds
.Where(t => !Utils.IsNullOrEmpty(t.server) && Utils.IsDomain(t.server))
.Where(t => Utils.IsNotEmpty(t.server) && Utils.IsDomain(t.server))
.Select(t => t.server)
.Distinct()
.ToList();
Expand Down Expand Up @@ -1324,10 +1324,10 @@ static void AddRuleSets(List<string> ruleSets, List<string>? rule_set)
if (_config.routingBasicItem.enableRoutingAdvanced)
{
var routing = ConfigHandler.GetDefaultRouting(_config);
if (!Utils.IsNullOrEmpty(routing.customRulesetPath4Singbox))
if (Utils.IsNotEmpty(routing.customRulesetPath4Singbox))
{
var result = Utils.LoadResource(routing.customRulesetPath4Singbox);
if (!Utils.IsNullOrEmpty(result))
if (Utils.IsNotEmpty(result))
{
customRulesets = (JsonUtils.Deserialize<List<Ruleset4Sbox>>(result) ?? [])
.Where(t => t.tag != null)
Expand Down
Loading

0 comments on commit 61635db

Please sign in to comment.