Optimized config

This commit is contained in:
2dust 2022-02-03 20:57:02 +08:00
parent f220825b80
commit 7cdead8cb1
2 changed files with 49 additions and 46 deletions

View file

@ -33,7 +33,7 @@ namespace v2rayN.Handler
config = Utils.FromJson<Config>(result); config = Utils.FromJson<Config>(result);
} }
else else
{ {
if (File.Exists(Utils.GetPath(configRes))) if (File.Exists(Utils.GetPath(configRes)))
{ {
Utils.SaveLog("LoadConfig Exception"); Utils.SaveLog("LoadConfig Exception");
@ -241,7 +241,7 @@ namespace v2rayN.Handler
/// <returns></returns> /// <returns></returns>
public static int RemoveServer(ref Config config, List<int> indexs) public static int RemoveServer(ref Config config, List<int> indexs)
{ {
var itemId = config.getItemId(); var indexId = config.indexId();
for (int k = indexs.Count - 1; k >= 0; k--) for (int k = indexs.Count - 1; k >= 0; k--)
{ {
@ -254,23 +254,7 @@ namespace v2rayN.Handler
config.vmess.RemoveAt(index); config.vmess.RemoveAt(index);
} }
var index_ = config.vmess.FindIndex(it => it.getItemId() == itemId); SetIndex(ref config, indexId);
if (index_ >= 0)
{
config.index = index_;
}
else
{
if (config.vmess.Count > 0)
{
config.index = 0;
}
else
{
config.index = -1;
}
}
Global.reloadV2ray = true;
ToJsonFile(config); ToJsonFile(config);
@ -861,6 +845,7 @@ namespace v2rayN.Handler
/// <returns></returns> /// <returns></returns>
public static int RemoveServerViaSubid(ref Config config, string subid) public static int RemoveServerViaSubid(ref Config config, string subid)
{ {
var indexId = config.indexId();
if (Utils.IsNullOrEmpty(subid) || config.vmess.Count <= 0) if (Utils.IsNullOrEmpty(subid) || config.vmess.Count <= 0)
{ {
return -1; return -1;
@ -873,6 +858,8 @@ namespace v2rayN.Handler
} }
} }
SetIndex(ref config, indexId);
ToJsonFile(config); ToJsonFile(config);
return 0; return 0;
} }
@ -902,7 +889,8 @@ namespace v2rayN.Handler
default: default:
return -1; return -1;
} }
string itemId = config.getItemId();
var indexId = config.indexId();
var items = config.vmess.AsQueryable(); var items = config.vmess.AsQueryable();
if (asc) if (asc)
@ -914,11 +902,7 @@ namespace v2rayN.Handler
config.vmess = items.OrderByDescending(propertyName).ToList(); config.vmess = items.OrderByDescending(propertyName).ToList();
} }
var index_ = config.vmess.FindIndex(it => it.getItemId() == itemId); SetIndex(ref config, indexId);
if (index_ >= 0)
{
config.index = index_;
}
ToJsonFile(config); ToJsonFile(config);
return 0; return 0;
@ -954,7 +938,7 @@ namespace v2rayN.Handler
} }
} }
else else
{ {
AddServerCommon(ref config, vmessItem); AddServerCommon(ref config, vmessItem);
} }
@ -968,8 +952,8 @@ namespace v2rayN.Handler
public static int DedupServerList(ref Config config) public static int DedupServerList(ref Config config)
{ {
var itemId = config.getItemId(); var indexId = config.indexId();
List<Mode.VmessItem> source = config.vmess; List<Mode.VmessItem> source = config.vmess;
bool keepOlder = config.keepOlderDedupl; bool keepOlder = config.keepOlderDedupl;
@ -1002,23 +986,7 @@ namespace v2rayN.Handler
if (!keepOlder) list.Reverse(); if (!keepOlder) list.Reverse();
config.vmess = list; config.vmess = list;
var index_ = config.vmess.FindIndex(it => it.getItemId() == itemId); SetIndex(ref config, indexId);
if (index_ >= 0)
{
config.index = index_;
}
else
{
if (config.vmess.Count > 0)
{
config.index = 0;
}
else
{
config.index = -1;
}
}
Global.reloadV2ray = true;
return 0; return 0;
} }
@ -1040,6 +1008,34 @@ namespace v2rayN.Handler
} }
return 0; return 0;
} }
public static int SetIndex(ref Config config, string indexId)
{
var index_ = config.FindIndexId(indexId);
if (config.index == index_)
{
return 0;
}
else if (index_ >= 0)
{
config.index = index_;
}
else
{
if (config.vmess.Count > 0)
{
config.index = 0;
}
else
{
config.index = -1;
}
}
Global.reloadV2ray = true;
return 0;
}
#endregion #endregion
#region UI #region UI

View file

@ -375,7 +375,14 @@ namespace v2rayN.Mode
return null; return null;
} }
} }
public string indexId()
{
if (index < 0)
{
return string.Empty;
}
return vmess[index].indexId.TrimEx();
}
public int FindIndexId(string indexId) public int FindIndexId(string indexId)
{ {
if (string.IsNullOrEmpty(indexId)) if (string.IsNullOrEmpty(indexId))