mirror of https://github.com/2dust/v2rayN
				
				
				
			Code optimization
							parent
							
								
									b013213745
								
							
						
					
					
						commit
						3fafc6de93
					
				| 
						 | 
				
			
			@ -5,8 +5,8 @@
 | 
			
		|||
    <TargetFramework>net8.0</TargetFramework>
 | 
			
		||||
    <ImplicitUsings>enable</ImplicitUsings>
 | 
			
		||||
    <Nullable>enable</Nullable>
 | 
			
		||||
	<Copyright>Copyright © 2019-2024 (GPLv3)</Copyright>
 | 
			
		||||
	<FileVersion>1.2.0.0</FileVersion>
 | 
			
		||||
	<Copyright>Copyright © 2017-2024 (GPLv3)</Copyright>
 | 
			
		||||
	<FileVersion>1.3.0</FileVersion>
 | 
			
		||||
  </PropertyGroup>
 | 
			
		||||
 | 
			
		||||
</Project>
 | 
			
		||||
| 
						 | 
				
			
			@ -228,12 +228,6 @@ namespace ServiceLib.Common
 | 
			
		|||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// byte 转成 有两位小数点的 方便阅读的数据   比如 2.50 MB
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="amount">bytes</param>
 | 
			
		||||
        /// <param name="result">转换之后的数据</param>
 | 
			
		||||
        /// <param name="unit">单位</param>
 | 
			
		||||
        private static void ToHumanReadable(long amount, out double result, out string unit)
 | 
			
		||||
        {
 | 
			
		||||
            var factor = 1024u;
 | 
			
		||||
| 
						 | 
				
			
			@ -411,7 +405,6 @@ namespace ServiceLib.Common
 | 
			
		|||
        /// <param name="domain"></param>
 | 
			
		||||
        public static bool IsDomain(string? domain)
 | 
			
		||||
        {
 | 
			
		||||
            //如果为空
 | 
			
		||||
            if (IsNullOrEmpty(domain))
 | 
			
		||||
            {
 | 
			
		||||
                return false;
 | 
			
		||||
| 
						 | 
				
			
			@ -564,10 +557,6 @@ namespace ServiceLib.Common
 | 
			
		|||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// 获取系统hosts
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <returns></returns>
 | 
			
		||||
        public static Dictionary<string, string> GetSystemHosts()
 | 
			
		||||
        {
 | 
			
		||||
            var systemHosts = new Dictionary<string, string>();
 | 
			
		||||
| 
						 | 
				
			
			@ -634,10 +623,6 @@ namespace ServiceLib.Common
 | 
			
		|||
 | 
			
		||||
        #region TempPath
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// 获取启动了应用程序的可执行文件的路径
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <returns></returns>
 | 
			
		||||
        public static string GetPath(string fileName)
 | 
			
		||||
        {
 | 
			
		||||
            var startupPath = StartupPath();
 | 
			
		||||
| 
						 | 
				
			
			@ -647,11 +632,7 @@ namespace ServiceLib.Common
 | 
			
		|||
            }
 | 
			
		||||
            return Path.Combine(startupPath, fileName);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// 获取启动了应用程序的可执行文件的路径及文件名
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <returns></returns>
 | 
			
		||||
        
 | 
			
		||||
        public static string GetExePath()
 | 
			
		||||
        {
 | 
			
		||||
            return Environment.ProcessPath ?? Process.GetCurrentProcess().MainModule?.FileName ?? string.Empty;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -102,17 +102,17 @@
 | 
			
		|||
 | 
			
		||||
        #region SqliteHelper
 | 
			
		||||
 | 
			
		||||
        public async Task<List<SubItem>> SubItems()
 | 
			
		||||
        public async Task<List<SubItem>?> SubItems()
 | 
			
		||||
        {
 | 
			
		||||
            return await SQLiteHelper.Instance.TableAsync<SubItem>().OrderBy(t => t.Sort).ToListAsync();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task<SubItem> GetSubItem(string subid)
 | 
			
		||||
        public async Task<SubItem?> GetSubItem(string subid)
 | 
			
		||||
        {
 | 
			
		||||
            return await SQLiteHelper.Instance.TableAsync<SubItem>().FirstOrDefaultAsync(t => t.Id == subid);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task<List<ProfileItem>> ProfileItems(string subid)
 | 
			
		||||
        public async Task<List<ProfileItem>?> ProfileItems(string subid)
 | 
			
		||||
        {
 | 
			
		||||
            if (Utils.IsNullOrEmpty(subid))
 | 
			
		||||
            {
 | 
			
		||||
| 
						 | 
				
			
			@ -124,12 +124,12 @@
 | 
			
		|||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task<List<string>> ProfileItemIndexes(string subid)
 | 
			
		||||
        public async Task<List<string>?> ProfileItemIndexes(string subid)
 | 
			
		||||
        {
 | 
			
		||||
            return (await ProfileItems(subid)).Select(t => t.IndexId).ToList();
 | 
			
		||||
            return (await ProfileItems(subid))?.Select(t => t.IndexId)?.ToList();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task<List<ProfileItemModel>> ProfileItems(string subid, string filter)
 | 
			
		||||
        public async Task<List<ProfileItemModel>?> ProfileItems(string subid, string filter)
 | 
			
		||||
        {
 | 
			
		||||
            var sql = @$"select a.*
 | 
			
		||||
                           ,b.remarks subRemarks
 | 
			
		||||
| 
						 | 
				
			
			@ -152,7 +152,7 @@
 | 
			
		|||
            return await SQLiteHelper.Instance.QueryAsync<ProfileItemModel>(sql);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task<List<ProfileItemModel>> ProfileItemsEx(string subid, string filter)
 | 
			
		||||
        public async Task<List<ProfileItemModel>?> ProfileItemsEx(string subid, string filter)
 | 
			
		||||
        {
 | 
			
		||||
            var lstModel = await ProfileItems(_config.SubIndexId, filter);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -209,22 +209,22 @@
 | 
			
		|||
            return await SQLiteHelper.Instance.TableAsync<ProfileItem>().FirstOrDefaultAsync(it => it.Remarks == remarks);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task<List<RoutingItem>> RoutingItems()
 | 
			
		||||
        public async Task<List<RoutingItem>?> RoutingItems()
 | 
			
		||||
        {
 | 
			
		||||
            return await SQLiteHelper.Instance.TableAsync<RoutingItem>().Where(it => it.Locked == false).OrderBy(t => t.Sort).ToListAsync();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task<RoutingItem> GetRoutingItem(string id)
 | 
			
		||||
        public async Task<RoutingItem?> GetRoutingItem(string id)
 | 
			
		||||
        {
 | 
			
		||||
            return await SQLiteHelper.Instance.TableAsync<RoutingItem>().FirstOrDefaultAsync(it => it.Locked == false && it.Id == id);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task<List<DNSItem>> DNSItems()
 | 
			
		||||
        public async Task<List<DNSItem>?> DNSItems()
 | 
			
		||||
        {
 | 
			
		||||
            return await SQLiteHelper.Instance.TableAsync<DNSItem>().ToListAsync();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task<DNSItem> GetDNSItem(ECoreType eCoreType)
 | 
			
		||||
        public async Task<DNSItem?> GetDNSItem(ECoreType eCoreType)
 | 
			
		||||
        {
 | 
			
		||||
            return await SQLiteHelper.Instance.TableAsync<DNSItem>().FirstOrDefaultAsync(it => it.CoreType == eCoreType);
 | 
			
		||||
        }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -171,18 +171,7 @@ namespace ServiceLib.Handler
 | 
			
		|||
        /// </summary>
 | 
			
		||||
        /// <param name="config"></param>
 | 
			
		||||
        /// <returns></returns>
 | 
			
		||||
        public static async Task<int> SaveConfig(Config config, bool reload = true)
 | 
			
		||||
        {
 | 
			
		||||
            await ToJsonFile(config);
 | 
			
		||||
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// 存储文件
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="config"></param>
 | 
			
		||||
        private static async Task ToJsonFile(Config config)
 | 
			
		||||
        public static async Task<int> SaveConfig(Config config)
 | 
			
		||||
        {
 | 
			
		||||
            lock (_objLock)
 | 
			
		||||
            {
 | 
			
		||||
| 
						 | 
				
			
			@ -193,7 +182,7 @@ namespace ServiceLib.Handler
 | 
			
		|||
                    var tempPath = $"{resPath}_temp";
 | 
			
		||||
                    if (JsonUtils.ToFile(config, tempPath) != 0)
 | 
			
		||||
                    {
 | 
			
		||||
                        return;
 | 
			
		||||
                        return -1;
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    if (File.Exists(resPath))
 | 
			
		||||
| 
						 | 
				
			
			@ -206,8 +195,11 @@ namespace ServiceLib.Handler
 | 
			
		|||
                catch (Exception ex)
 | 
			
		||||
                {
 | 
			
		||||
                    Logging.SaveLog("ToJsonFile", ex);
 | 
			
		||||
                    return -1;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        #endregion ConfigHandler
 | 
			
		||||
| 
						 | 
				
			
			@ -369,7 +361,7 @@ namespace ServiceLib.Handler
 | 
			
		|||
 | 
			
		||||
            config.IndexId = indexId;
 | 
			
		||||
 | 
			
		||||
            await ToJsonFile(config);
 | 
			
		||||
            await SaveConfig(config);
 | 
			
		||||
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
| 
						 | 
				
			
			@ -1142,7 +1134,7 @@ namespace ServiceLib.Handler
 | 
			
		|||
                await SQLiteHelper.Instance.InsertAllAsync(lstAdd);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            await ToJsonFile(config);
 | 
			
		||||
            await SaveConfig(config);
 | 
			
		||||
            return countServers;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -1272,7 +1264,7 @@ namespace ServiceLib.Handler
 | 
			
		|||
                        counter++;
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                await ToJsonFile(config);
 | 
			
		||||
                await SaveConfig(config);
 | 
			
		||||
                return counter;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -1605,7 +1597,7 @@ namespace ServiceLib.Handler
 | 
			
		|||
                config.RoutingBasicItem.RoutingIndexId = routingItem.Id;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            await ToJsonFile(config);
 | 
			
		||||
            await SaveConfig(config);
 | 
			
		||||
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -566,8 +566,8 @@ namespace ServiceLib.ViewModels
 | 
			
		|||
 | 
			
		||||
        public async Task CloseCore()
 | 
			
		||||
        {
 | 
			
		||||
            await ConfigHandler.SaveConfig(_config, false);
 | 
			
		||||
            CoreHandler.Instance.CoreStop();
 | 
			
		||||
            await ConfigHandler.SaveConfig(_config);
 | 
			
		||||
            await CoreHandler.Instance.CoreStop();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private async Task AutoHideStartup()
 | 
			
		||||
| 
						 | 
				
			
			@ -588,7 +588,7 @@ namespace ServiceLib.ViewModels
 | 
			
		|||
            await ConfigHandler.InitRouting(_config);
 | 
			
		||||
            Locator.Current.GetService<StatusBarViewModel>()?.RefreshRoutingsMenu();
 | 
			
		||||
 | 
			
		||||
            await ConfigHandler.SaveConfig(_config, false);
 | 
			
		||||
            await ConfigHandler.SaveConfig(_config);
 | 
			
		||||
            await new UpdateService().UpdateGeoFileAll(_config, UpdateHandler);
 | 
			
		||||
            await Reload();
 | 
			
		||||
        }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -321,7 +321,7 @@ namespace ServiceLib.ViewModels
 | 
			
		|||
            NoticeHandler.Instance.SendMessageEx($"{ResUI.TipChangeSystemProxy} - {_config.SystemProxyItem.SysProxyType.ToString()}");
 | 
			
		||||
 | 
			
		||||
            SystemProxySelected = (int)_config.SystemProxyItem.SysProxyType;
 | 
			
		||||
            await ConfigHandler.SaveConfig(_config, false);
 | 
			
		||||
            await ConfigHandler.SaveConfig(_config);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task ChangeSystemProxyAsync(ESysProxyType type, bool blChange)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -6,6 +6,7 @@
 | 
			
		|||
		<ImplicitUsings>enable</ImplicitUsings>
 | 
			
		||||
		<ApplicationIcon>Assets\v2rayN.ico</ApplicationIcon>
 | 
			
		||||
        <BuiltInComInteropSupport>true</BuiltInComInteropSupport>
 | 
			
		||||
	    <Copyright>Copyright © 2017-2024 (GPLv3)</Copyright>
 | 
			
		||||
        <AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
 | 
			
		||||
    </PropertyGroup>
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -103,7 +103,7 @@ namespace v2rayN.Views
 | 
			
		|||
        {
 | 
			
		||||
            _config.GlobalHotkeys = _TextBoxKeyEventItem.Values.ToList();
 | 
			
		||||
 | 
			
		||||
            if (  ConfigHandler.SaveConfig(_config, false).Result == 0)
 | 
			
		||||
            if (  ConfigHandler.SaveConfig(_config).Result == 0)
 | 
			
		||||
            {
 | 
			
		||||
                HotkeyHandler.Instance.ReLoad();
 | 
			
		||||
                this.DialogResult = true;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue