From 8d1d10b7839bedabe1b4c469b10ac8b2efa06f3f Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Tue, 20 Aug 2024 14:15:29 +0800 Subject: [PATCH] Refactoring Project --- .../Base/MyReactiveObject.cs | 3 +- v2rayN/ServiceLib/Common/Utils.cs | 1 + v2rayN/ServiceLib/Enums/EViewAction.cs | 4 + v2rayN/ServiceLib/FodyWeavers.xml | 3 + v2rayN/ServiceLib/Global.cs | 1 + v2rayN/ServiceLib/GlobalUsings.cs | 8 +- v2rayN/ServiceLib/Handler/ConfigHandler.cs | 4 + v2rayN/ServiceLib/Handler/LazyConfig.cs | 12 ++- .../Handler/NoticeHandler.cs | 14 +--- v2rayN/ServiceLib/ServiceLib.csproj | 2 + .../ViewModels/AddServer2ViewModel.cs | 5 +- .../ViewModels/AddServerViewModel.cs | 4 +- .../ViewModels/ClashConnectionsViewModel.cs | 3 +- .../ViewModels/ClashProxiesViewModel.cs | 4 +- .../ViewModels/DNSSettingViewModel.cs | 4 +- .../ViewModels/MainWindowViewModel.cs | 65 ++++++---------- .../ViewModels/OptionSettingViewModel.cs | 4 +- .../ViewModels/ProfilesViewModel.cs | 10 +-- .../ViewModels/RoutingRuleDetailsViewModel.cs | 4 +- .../ViewModels/RoutingRuleSettingViewModel.cs | 17 +++-- .../ViewModels/RoutingSettingViewModel.cs | 4 +- .../ViewModels/SubEditViewModel.cs | 4 +- .../ViewModels/SubSettingViewModel.cs | 4 +- v2rayN/v2rayN/App.xaml.cs | 4 +- v2rayN/v2rayN/GlobalUsings.cs | 9 +-- .../ViewModels/ThemeSettingViewModel.cs | 2 - v2rayN/v2rayN/Views/AddServer2Window.xaml | 2 +- v2rayN/v2rayN/Views/AddServer2Window.xaml.cs | 23 +++--- v2rayN/v2rayN/Views/AddServerWindow.xaml | 2 +- v2rayN/v2rayN/Views/AddServerWindow.xaml.cs | 7 +- v2rayN/v2rayN/Views/ClashConnectionsView.xaml | 2 +- .../v2rayN/Views/ClashConnectionsView.xaml.cs | 1 - v2rayN/v2rayN/Views/ClashProxiesView.xaml | 2 +- v2rayN/v2rayN/Views/ClashProxiesView.xaml.cs | 1 - v2rayN/v2rayN/Views/DNSSettingWindow.xaml | 2 +- v2rayN/v2rayN/Views/DNSSettingWindow.xaml.cs | 7 +- .../Views/GlobalHotkeySettingWindow.xaml | 2 +- v2rayN/v2rayN/Views/MainWindow.xaml | 12 +-- v2rayN/v2rayN/Views/MainWindow.xaml.cs | 47 ++++++++++-- v2rayN/v2rayN/Views/OptionSettingWindow.xaml | 2 +- .../v2rayN/Views/OptionSettingWindow.xaml.cs | 9 ++- v2rayN/v2rayN/Views/ProfilesView.xaml | 2 +- v2rayN/v2rayN/Views/ProfilesView.xaml.cs | 6 +- .../Views/RoutingRuleDetailsWindow.xaml | 12 +-- .../Views/RoutingRuleDetailsWindow.xaml.cs | 7 +- .../Views/RoutingRuleSettingWindow.xaml | 12 +-- .../Views/RoutingRuleSettingWindow.xaml.cs | 75 +++++++++++-------- v2rayN/v2rayN/Views/RoutingSettingWindow.xaml | 2 +- .../v2rayN/Views/RoutingSettingWindow.xaml.cs | 31 ++++---- v2rayN/v2rayN/Views/SubEditWindow.xaml | 10 +-- v2rayN/v2rayN/Views/SubEditWindow.xaml.cs | 8 +- v2rayN/v2rayN/Views/SubSettingWindow.xaml | 2 +- v2rayN/v2rayN/Views/SubSettingWindow.xaml.cs | 40 +++++----- 53 files changed, 278 insertions(+), 249 deletions(-) rename v2rayN/{v2rayN => ServiceLib}/Base/MyReactiveObject.cs (84%) create mode 100644 v2rayN/ServiceLib/FodyWeavers.xml rename v2rayN/{v2rayN => ServiceLib}/Handler/NoticeHandler.cs (67%) rename v2rayN/{v2rayN => ServiceLib}/ViewModels/AddServer2ViewModel.cs (97%) rename v2rayN/{v2rayN => ServiceLib}/ViewModels/AddServerViewModel.cs (97%) rename v2rayN/{v2rayN => ServiceLib}/ViewModels/ClashConnectionsViewModel.cs (99%) rename v2rayN/{v2rayN => ServiceLib}/ViewModels/ClashProxiesViewModel.cs (99%) rename v2rayN/{v2rayN => ServiceLib}/ViewModels/DNSSettingViewModel.cs (98%) rename v2rayN/{v2rayN => ServiceLib}/ViewModels/MainWindowViewModel.cs (95%) rename v2rayN/{v2rayN => ServiceLib}/ViewModels/OptionSettingViewModel.cs (99%) rename v2rayN/{v2rayN => ServiceLib}/ViewModels/ProfilesViewModel.cs (98%) rename v2rayN/{v2rayN => ServiceLib}/ViewModels/RoutingRuleDetailsViewModel.cs (97%) rename v2rayN/{v2rayN => ServiceLib}/ViewModels/RoutingRuleSettingViewModel.cs (96%) rename v2rayN/{v2rayN => ServiceLib}/ViewModels/RoutingSettingViewModel.cs (99%) rename v2rayN/{v2rayN => ServiceLib}/ViewModels/SubEditViewModel.cs (95%) rename v2rayN/{v2rayN => ServiceLib}/ViewModels/SubSettingViewModel.cs (97%) diff --git a/v2rayN/v2rayN/Base/MyReactiveObject.cs b/v2rayN/ServiceLib/Base/MyReactiveObject.cs similarity index 84% rename from v2rayN/v2rayN/Base/MyReactiveObject.cs rename to v2rayN/ServiceLib/Base/MyReactiveObject.cs index b326c7cb..8d7c3521 100644 --- a/v2rayN/v2rayN/Base/MyReactiveObject.cs +++ b/v2rayN/ServiceLib/Base/MyReactiveObject.cs @@ -1,7 +1,6 @@ using ReactiveUI; -using v2rayN.Handler; -namespace v2rayN.Base +namespace ServiceLib.Base { public class MyReactiveObject : ReactiveObject { diff --git a/v2rayN/ServiceLib/Common/Utils.cs b/v2rayN/ServiceLib/Common/Utils.cs index 77587642..2204cd21 100644 --- a/v2rayN/ServiceLib/Common/Utils.cs +++ b/v2rayN/ServiceLib/Common/Utils.cs @@ -366,6 +366,7 @@ namespace ServiceLib.Common public static bool IsBase64String(string plainText) { + if(plainText.IsNullOrEmpty()) return false; var buffer = new Span(new byte[plainText.Length]); return Convert.TryFromBase64String(plainText, buffer, out int _); } diff --git a/v2rayN/ServiceLib/Enums/EViewAction.cs b/v2rayN/ServiceLib/Enums/EViewAction.cs index 3331cead..88954a78 100644 --- a/v2rayN/ServiceLib/Enums/EViewAction.cs +++ b/v2rayN/ServiceLib/Enums/EViewAction.cs @@ -7,6 +7,10 @@ SaveFileDialog, AddBatchRoutingRulesYesNo, AdjustMainLvColWidth, + UpdateSysProxy, + SetClipboardData, + AddServerViaClipboard, + ImportRulesFromClipboard, ProfilesFocus, ShareSub, ShareServer, diff --git a/v2rayN/ServiceLib/FodyWeavers.xml b/v2rayN/ServiceLib/FodyWeavers.xml new file mode 100644 index 00000000..63fc1484 --- /dev/null +++ b/v2rayN/ServiceLib/FodyWeavers.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/v2rayN/ServiceLib/Global.cs b/v2rayN/ServiceLib/Global.cs index 44720c71..53b97a50 100644 --- a/v2rayN/ServiceLib/Global.cs +++ b/v2rayN/ServiceLib/Global.cs @@ -71,6 +71,7 @@ public const int MaxPort = 65536; public const string CommandClearMsg = "CommandClearMsg"; public const string CommandSendMsgView = "CommandSendMsgView"; + public const string CommandSendSnackMsg = "CommandSendSnackMsg"; public const string CommandStopSpeedTest = "CommandStopSpeedTest"; public const string CommandRefreshProfiles = "CommandRefreshProfiles"; public const string DelayUnit = ""; diff --git a/v2rayN/ServiceLib/GlobalUsings.cs b/v2rayN/ServiceLib/GlobalUsings.cs index 5470f5eb..ae515d50 100644 --- a/v2rayN/ServiceLib/GlobalUsings.cs +++ b/v2rayN/ServiceLib/GlobalUsings.cs @@ -1,7 +1,11 @@ -global using ServiceLib.Common; -global using ServiceLib.Enums; +global using ServiceLib; +global using ServiceLib.Base; +global using ServiceLib.Common; +global using ServiceLib.Handler; global using ServiceLib.Handler.CoreConfig; global using ServiceLib.Handler.Fmt; global using ServiceLib.Handler.Statistics; +global using ServiceLib.Enums; global using ServiceLib.Models; +global using ServiceLib.ViewModels; global using ServiceLib.Resx; \ No newline at end of file diff --git a/v2rayN/ServiceLib/Handler/ConfigHandler.cs b/v2rayN/ServiceLib/Handler/ConfigHandler.cs index 0d1d96ca..5b289ee4 100644 --- a/v2rayN/ServiceLib/Handler/ConfigHandler.cs +++ b/v2rayN/ServiceLib/Handler/ConfigHandler.cs @@ -1406,6 +1406,10 @@ namespace ServiceLib.Handler public static int AddBatchServers(Config config, string strData, string subid, bool isSub) { + if (Utils.IsNullOrEmpty(strData)) + { + return -1; + } List? lstOriSub = null; if (isSub && !Utils.IsNullOrEmpty(subid)) { diff --git a/v2rayN/ServiceLib/Handler/LazyConfig.cs b/v2rayN/ServiceLib/Handler/LazyConfig.cs index a8fd5b97..7914679b 100644 --- a/v2rayN/ServiceLib/Handler/LazyConfig.cs +++ b/v2rayN/ServiceLib/Handler/LazyConfig.cs @@ -1,4 +1,6 @@ -namespace ServiceLib.Handler +using System.Runtime.InteropServices; + +namespace ServiceLib.Handler { public sealed class LazyConfig { @@ -28,7 +30,7 @@ } } - private Job _processJob = new(); + private Job? _processJob; public LazyConfig() { @@ -52,7 +54,11 @@ public void AddProcess(IntPtr processHandle) { - _processJob.AddProcess(processHandle); + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + _processJob ??= new(); + _processJob?.AddProcess(processHandle); + } } #endregion Config diff --git a/v2rayN/v2rayN/Handler/NoticeHandler.cs b/v2rayN/ServiceLib/Handler/NoticeHandler.cs similarity index 67% rename from v2rayN/v2rayN/Handler/NoticeHandler.cs rename to v2rayN/ServiceLib/Handler/NoticeHandler.cs index 900756d5..ec7e3333 100644 --- a/v2rayN/v2rayN/Handler/NoticeHandler.cs +++ b/v2rayN/ServiceLib/Handler/NoticeHandler.cs @@ -1,24 +1,16 @@ -using MaterialDesignThemes.Wpf; -using ReactiveUI; +using ReactiveUI; -namespace v2rayN.Handler +namespace ServiceLib.Handler { public class NoticeHandler { - private readonly ISnackbarMessageQueue? _snackbarMessageQueue; - - public NoticeHandler(ISnackbarMessageQueue? snackbarMessageQueue) - { - _snackbarMessageQueue = snackbarMessageQueue ?? throw new ArgumentNullException(nameof(snackbarMessageQueue)); - } - public void Enqueue(string? content) { if (content.IsNullOrEmpty()) { return; } - _snackbarMessageQueue?.Enqueue(content); + MessageBus.Current.SendMessage(content, Global.CommandSendSnackMsg); } public void SendMessage(string? content) diff --git a/v2rayN/ServiceLib/ServiceLib.csproj b/v2rayN/ServiceLib/ServiceLib.csproj index 47b0750a..e176383f 100644 --- a/v2rayN/ServiceLib/ServiceLib.csproj +++ b/v2rayN/ServiceLib/ServiceLib.csproj @@ -8,6 +8,8 @@ + + diff --git a/v2rayN/v2rayN/ViewModels/AddServer2ViewModel.cs b/v2rayN/ServiceLib/ViewModels/AddServer2ViewModel.cs similarity index 97% rename from v2rayN/v2rayN/ViewModels/AddServer2ViewModel.cs rename to v2rayN/ServiceLib/ViewModels/AddServer2ViewModel.cs index 97ea82e7..9f11cebb 100644 --- a/v2rayN/v2rayN/ViewModels/AddServer2ViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/AddServer2ViewModel.cs @@ -1,12 +1,9 @@ using ReactiveUI; using ReactiveUI.Fody.Helpers; using Splat; -using System.IO; using System.Reactive; -using v2rayN.Base; -using v2rayN.Handler; -namespace v2rayN.ViewModels +namespace ServiceLib.ViewModels { public class AddServer2ViewModel : MyReactiveObject { diff --git a/v2rayN/v2rayN/ViewModels/AddServerViewModel.cs b/v2rayN/ServiceLib/ViewModels/AddServerViewModel.cs similarity index 97% rename from v2rayN/v2rayN/ViewModels/AddServerViewModel.cs rename to v2rayN/ServiceLib/ViewModels/AddServerViewModel.cs index c5502508..889de9b2 100644 --- a/v2rayN/v2rayN/ViewModels/AddServerViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/AddServerViewModel.cs @@ -2,10 +2,8 @@ using ReactiveUI.Fody.Helpers; using Splat; using System.Reactive; -using v2rayN.Base; -using v2rayN.Handler; -namespace v2rayN.ViewModels +namespace ServiceLib.ViewModels { public class AddServerViewModel : MyReactiveObject { diff --git a/v2rayN/v2rayN/ViewModels/ClashConnectionsViewModel.cs b/v2rayN/ServiceLib/ViewModels/ClashConnectionsViewModel.cs similarity index 99% rename from v2rayN/v2rayN/ViewModels/ClashConnectionsViewModel.cs rename to v2rayN/ServiceLib/ViewModels/ClashConnectionsViewModel.cs index 529088ec..e733ce6a 100644 --- a/v2rayN/v2rayN/ViewModels/ClashConnectionsViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/ClashConnectionsViewModel.cs @@ -4,9 +4,8 @@ using ReactiveUI; using ReactiveUI.Fody.Helpers; using System.Reactive; using System.Reactive.Linq; -using v2rayN.Base; -namespace v2rayN.ViewModels +namespace ServiceLib.ViewModels { public class ClashConnectionsViewModel : MyReactiveObject { diff --git a/v2rayN/v2rayN/ViewModels/ClashProxiesViewModel.cs b/v2rayN/ServiceLib/ViewModels/ClashProxiesViewModel.cs similarity index 99% rename from v2rayN/v2rayN/ViewModels/ClashProxiesViewModel.cs rename to v2rayN/ServiceLib/ViewModels/ClashProxiesViewModel.cs index 2a7fe3e5..ffdd8562 100644 --- a/v2rayN/v2rayN/ViewModels/ClashProxiesViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/ClashProxiesViewModel.cs @@ -5,12 +5,10 @@ using ReactiveUI.Fody.Helpers; using Splat; using System.Reactive; using System.Reactive.Linq; -using v2rayN.Base; -using v2rayN.Handler; using static ServiceLib.Models.ClashProviders; using static ServiceLib.Models.ClashProxies; -namespace v2rayN.ViewModels +namespace ServiceLib.ViewModels { public class ClashProxiesViewModel : MyReactiveObject { diff --git a/v2rayN/v2rayN/ViewModels/DNSSettingViewModel.cs b/v2rayN/ServiceLib/ViewModels/DNSSettingViewModel.cs similarity index 98% rename from v2rayN/v2rayN/ViewModels/DNSSettingViewModel.cs rename to v2rayN/ServiceLib/ViewModels/DNSSettingViewModel.cs index 571ff8d3..42a4f5a4 100644 --- a/v2rayN/v2rayN/ViewModels/DNSSettingViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/DNSSettingViewModel.cs @@ -2,10 +2,8 @@ using ReactiveUI.Fody.Helpers; using Splat; using System.Reactive; -using v2rayN.Base; -using v2rayN.Handler; -namespace v2rayN.ViewModels +namespace ServiceLib.ViewModels { public class DNSSettingViewModel : MyReactiveObject { diff --git a/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs similarity index 95% rename from v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs rename to v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs index d9ca6b90..83774035 100644 --- a/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs @@ -3,14 +3,11 @@ using ReactiveUI; using ReactiveUI.Fody.Helpers; using Splat; using System.Diagnostics; -using System.IO; using System.Reactive; using System.Reactive.Linq; using System.Text; -using v2rayN.Base; -using v2rayN.Handler; -namespace v2rayN.ViewModels +namespace ServiceLib.ViewModels { public class MainWindowViewModel : MyReactiveObject { @@ -149,6 +146,8 @@ namespace v2rayN.ViewModels [Reactive] public int TabMainSelectedIndex { get; set; } + public bool IsAdministrator { get; set; } + #endregion UI #region Init @@ -159,22 +158,10 @@ namespace v2rayN.ViewModels _noticeHandler = Locator.Current.GetService(); _updateView = updateView; - ThreadPool.RegisterWaitForSingleObject(App.ProgramStarted, OnProgramStarted, null, -1, false); MessageBus.Current.Listen(Global.CommandRefreshProfiles).Subscribe(x => _updateView?.Invoke(EViewAction.DispatcherRefreshServersBiz, null)); SelectedRouting = new(); SelectedServer = new(); - if (_config.tunModeItem.enableTun) - { - if (WindowsUtils.IsAdministrator()) - { - EnableTun = true; - } - else - { - _config.tunModeItem.enableTun = EnableTun = false; - } - } Init(); @@ -244,7 +231,7 @@ namespace v2rayN.ViewModels }); AddServerViaClipboardCmd = ReactiveCommand.Create(() => { - AddServerViaClipboard(); + AddServerViaClipboard(null); }); AddServerViaScanCmd = ReactiveCommand.Create(() => { @@ -385,11 +372,6 @@ namespace v2rayN.ViewModels ChangeSystemProxyStatus(_config.systemProxyItem.sysProxyType, true); } - private void OnProgramStarted(object state, bool timeout) - { - _updateView?.Invoke(EViewAction.ShowHideWindow, true); - } - #endregion Init #region Actions @@ -457,24 +439,15 @@ namespace v2rayN.ViewModels try { Logging.SaveLog("MyAppExit Begin"); + //if (blWindowsShutDown) + _updateView?.Invoke(EViewAction.UpdateSysProxy, true); ConfigHandler.SaveConfig(_config); - - if (blWindowsShutDown) - { - SysProxyHandler.ResetIEProxy4WindowsShutDown(); - } - else - { - SysProxyHandler.UpdateSysProxy(_config, true); - } - ProfileExHandler.Instance.SaveTo(); - StatisticsHandler.Instance.SaveTo(); StatisticsHandler.Instance.Close(); - _coreHandler.CoreStop(); + Logging.SaveLog("MyAppExit End"); } catch { } @@ -574,10 +547,14 @@ namespace v2rayN.ViewModels } } - public void AddServerViaClipboard() + public void AddServerViaClipboard(string? clipboardData) { - var clipboardData = WindowsUtils.GetClipboardData(); - int ret = ConfigHandler.AddBatchServers(_config, clipboardData!, _config.subIndexId, false); + if (clipboardData == null) + { + _updateView?.Invoke(EViewAction.AddServerViaClipboard, null); + return; + } + int ret = ConfigHandler.AddBatchServers(_config, clipboardData, _config.subIndexId, false); if (ret > 0) { RefreshSubscriptions(); @@ -843,11 +820,11 @@ namespace v2rayN.ViewModels { await Task.Run(() => { - if (_config.tunModeItem.enableTun) - { - Task.Delay(1000).Wait(); - WindowsUtils.RemoveTunDevice(); - } + //if (_config.tunModeItem.enableTun) + //{ + // Task.Delay(1000).Wait(); + // WindowsUtils.RemoveTunDevice(); + //} var node = ConfigHandler.GetDefaultServer(_config); _coreHandler.LoadCore(node); @@ -882,7 +859,7 @@ namespace v2rayN.ViewModels private void ChangeSystemProxyStatus(ESysProxyType type, bool blChange) { - SysProxyHandler.UpdateSysProxy(_config, _config.tunModeItem.enableTun ? true : false); + _updateView?.Invoke(EViewAction.UpdateSysProxy, _config.tunModeItem.enableTun ? true : false); _noticeHandler?.SendMessage($"{ResUI.TipChangeSystemProxy} - {_config.systemProxyItem.sysProxyType.ToString()}", true); BlSystemProxyClear = (type == ESysProxyType.ForcedClear); @@ -968,7 +945,7 @@ namespace v2rayN.ViewModels { _config.tunModeItem.enableTun = EnableTun; // When running as a non-administrator, reboot to administrator mode - if (EnableTun && !WindowsUtils.IsAdministrator()) + if (EnableTun && !IsAdministrator) { _config.tunModeItem.enableTun = false; RebootAsAdmin(); diff --git a/v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs b/v2rayN/ServiceLib/ViewModels/OptionSettingViewModel.cs similarity index 99% rename from v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs rename to v2rayN/ServiceLib/ViewModels/OptionSettingViewModel.cs index 46f6bced..8dc332f3 100644 --- a/v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/OptionSettingViewModel.cs @@ -2,10 +2,8 @@ using ReactiveUI.Fody.Helpers; using Splat; using System.Reactive; -using v2rayN.Base; -using v2rayN.Handler; -namespace v2rayN.ViewModels +namespace ServiceLib.ViewModels { public class OptionSettingViewModel : MyReactiveObject { diff --git a/v2rayN/v2rayN/ViewModels/ProfilesViewModel.cs b/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs similarity index 98% rename from v2rayN/v2rayN/ViewModels/ProfilesViewModel.cs rename to v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs index d2d83daf..46117477 100644 --- a/v2rayN/v2rayN/ViewModels/ProfilesViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs @@ -6,10 +6,8 @@ using Splat; using System.Reactive; using System.Reactive.Linq; using System.Text; -using v2rayN.Base; -using v2rayN.Handler; -namespace v2rayN.ViewModels +namespace ServiceLib.ViewModels { public class ProfilesViewModel : MyReactiveObject { @@ -697,7 +695,7 @@ namespace v2rayN.ViewModels } else { - WindowsUtils.SetClipboardData(content); + _updateView?.Invoke(EViewAction.SetClipboardData, content); _noticeHandler?.SendMessage(ResUI.OperationSuccess); } } @@ -746,11 +744,11 @@ namespace v2rayN.ViewModels { if (blEncode) { - WindowsUtils.SetClipboardData(Utils.Base64Encode(sb.ToString())); + _updateView?.Invoke(EViewAction.SetClipboardData, Utils.Base64Encode(sb.ToString())); } else { - WindowsUtils.SetClipboardData(sb.ToString()); + _updateView?.Invoke(EViewAction.SetClipboardData, sb.ToString()); } _noticeHandler?.SendMessage(ResUI.BatchExportURLSuccessfully); } diff --git a/v2rayN/v2rayN/ViewModels/RoutingRuleDetailsViewModel.cs b/v2rayN/ServiceLib/ViewModels/RoutingRuleDetailsViewModel.cs similarity index 97% rename from v2rayN/v2rayN/ViewModels/RoutingRuleDetailsViewModel.cs rename to v2rayN/ServiceLib/ViewModels/RoutingRuleDetailsViewModel.cs index 31ab1a0b..60294a42 100644 --- a/v2rayN/v2rayN/ViewModels/RoutingRuleDetailsViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/RoutingRuleDetailsViewModel.cs @@ -2,10 +2,8 @@ using ReactiveUI.Fody.Helpers; using Splat; using System.Reactive; -using v2rayN.Base; -using v2rayN.Handler; -namespace v2rayN.ViewModels +namespace ServiceLib.ViewModels { public class RoutingRuleDetailsViewModel : MyReactiveObject { diff --git a/v2rayN/v2rayN/ViewModels/RoutingRuleSettingViewModel.cs b/v2rayN/ServiceLib/ViewModels/RoutingRuleSettingViewModel.cs similarity index 96% rename from v2rayN/v2rayN/ViewModels/RoutingRuleSettingViewModel.cs rename to v2rayN/ServiceLib/ViewModels/RoutingRuleSettingViewModel.cs index c60ca6ea..e699a78e 100644 --- a/v2rayN/v2rayN/ViewModels/RoutingRuleSettingViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/RoutingRuleSettingViewModel.cs @@ -3,10 +3,8 @@ using ReactiveUI; using ReactiveUI.Fody.Helpers; using Splat; using System.Reactive; -using v2rayN.Base; -using v2rayN.Handler; -namespace v2rayN.ViewModels +namespace ServiceLib.ViewModels { public class RoutingRuleSettingViewModel : MyReactiveObject { @@ -70,7 +68,7 @@ namespace v2rayN.ViewModels }); ImportRulesFromClipboardCmd = ReactiveCommand.Create(() => { - ImportRulesFromClipboard(); + ImportRulesFromClipboard(null); }); ImportRulesFromUrlCmd = ReactiveCommand.Create(() => { @@ -199,8 +197,7 @@ namespace v2rayN.ViewModels } if (lst.Count > 0) { - WindowsUtils.SetClipboardData(JsonUtils.Serialize(lst)); - //_noticeHandler?.Enqueue(ResUI.OperationSuccess")); + _updateView?.Invoke(EViewAction.SetClipboardData, JsonUtils.Serialize(lst)); } } @@ -273,9 +270,13 @@ namespace v2rayN.ViewModels } } - private void ImportRulesFromClipboard() + public void ImportRulesFromClipboard(string? clipboardData) { - var clipboardData = WindowsUtils.GetClipboardData(); + if (clipboardData == null) + { + _updateView?.Invoke(EViewAction.ImportRulesFromClipboard, null); + return; + } if (AddBatchRoutingRules(SelectedRouting, clipboardData) == 0) { RefreshRulesItems(); diff --git a/v2rayN/v2rayN/ViewModels/RoutingSettingViewModel.cs b/v2rayN/ServiceLib/ViewModels/RoutingSettingViewModel.cs similarity index 99% rename from v2rayN/v2rayN/ViewModels/RoutingSettingViewModel.cs rename to v2rayN/ServiceLib/ViewModels/RoutingSettingViewModel.cs index d9426185..8ce7d3a5 100644 --- a/v2rayN/v2rayN/ViewModels/RoutingSettingViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/RoutingSettingViewModel.cs @@ -3,10 +3,8 @@ using ReactiveUI; using ReactiveUI.Fody.Helpers; using Splat; using System.Reactive; -using v2rayN.Base; -using v2rayN.Handler; -namespace v2rayN.ViewModels +namespace ServiceLib.ViewModels { public class RoutingSettingViewModel : MyReactiveObject { diff --git a/v2rayN/v2rayN/ViewModels/SubEditViewModel.cs b/v2rayN/ServiceLib/ViewModels/SubEditViewModel.cs similarity index 95% rename from v2rayN/v2rayN/ViewModels/SubEditViewModel.cs rename to v2rayN/ServiceLib/ViewModels/SubEditViewModel.cs index d74c5130..6c10ed51 100644 --- a/v2rayN/v2rayN/ViewModels/SubEditViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/SubEditViewModel.cs @@ -2,10 +2,8 @@ using ReactiveUI.Fody.Helpers; using Splat; using System.Reactive; -using v2rayN.Base; -using v2rayN.Handler; -namespace v2rayN.ViewModels +namespace ServiceLib.ViewModels { public class SubEditViewModel : MyReactiveObject { diff --git a/v2rayN/v2rayN/ViewModels/SubSettingViewModel.cs b/v2rayN/ServiceLib/ViewModels/SubSettingViewModel.cs similarity index 97% rename from v2rayN/v2rayN/ViewModels/SubSettingViewModel.cs rename to v2rayN/ServiceLib/ViewModels/SubSettingViewModel.cs index 3b716d58..4d7592cb 100644 --- a/v2rayN/v2rayN/ViewModels/SubSettingViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/SubSettingViewModel.cs @@ -4,10 +4,8 @@ using ReactiveUI; using ReactiveUI.Fody.Helpers; using Splat; using System.Reactive; -using v2rayN.Base; -using v2rayN.Handler; -namespace v2rayN.ViewModels +namespace ServiceLib.ViewModels { public class SubSettingViewModel : MyReactiveObject { diff --git a/v2rayN/v2rayN/App.xaml.cs b/v2rayN/v2rayN/App.xaml.cs index f5b543c2..d66ac929 100644 --- a/v2rayN/v2rayN/App.xaml.cs +++ b/v2rayN/v2rayN/App.xaml.cs @@ -1,4 +1,5 @@ -using System.Diagnostics; +using Splat; +using System.Diagnostics; using System.Windows; using System.Windows.Threading; @@ -58,6 +59,7 @@ namespace v2rayN return; } LazyConfig.Instance.SetConfig(_config); + Locator.CurrentMutable.RegisterLazySingleton(() => new NoticeHandler(), typeof(NoticeHandler)); //Under Win10 if (Environment.OSVersion.Version.Major < 10) diff --git a/v2rayN/v2rayN/GlobalUsings.cs b/v2rayN/v2rayN/GlobalUsings.cs index 42722976..a049b0ab 100644 --- a/v2rayN/v2rayN/GlobalUsings.cs +++ b/v2rayN/v2rayN/GlobalUsings.cs @@ -1,9 +1,8 @@ global using ServiceLib; +global using ServiceLib.Base; global using ServiceLib.Common; -global using ServiceLib.Handler; -global using ServiceLib.Handler.CoreConfig; -global using ServiceLib.Handler.Fmt; -global using ServiceLib.Handler.Statistics; global using ServiceLib.Enums; +global using ServiceLib.Handler; global using ServiceLib.Models; -global using ServiceLib.Resx; \ No newline at end of file +global using ServiceLib.Resx; +global using ServiceLib.ViewModels; diff --git a/v2rayN/v2rayN/ViewModels/ThemeSettingViewModel.cs b/v2rayN/v2rayN/ViewModels/ThemeSettingViewModel.cs index 1e717bb5..abdd4736 100644 --- a/v2rayN/v2rayN/ViewModels/ThemeSettingViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/ThemeSettingViewModel.cs @@ -10,8 +10,6 @@ using System.Reactive.Linq; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Interop; -using v2rayN.Base; -using v2rayN.Handler; namespace v2rayN.ViewModels { diff --git a/v2rayN/v2rayN/Views/AddServer2Window.xaml b/v2rayN/v2rayN/Views/AddServer2Window.xaml index 06c74878..4e8b003c 100644 --- a/v2rayN/v2rayN/Views/AddServer2Window.xaml +++ b/v2rayN/v2rayN/Views/AddServer2Window.xaml @@ -8,7 +8,7 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:reactiveui="http://reactiveui.net" xmlns:resx="clr-namespace:ServiceLib.Resx;assembly=ServiceLib" - xmlns:vms="clr-namespace:v2rayN.ViewModels" + xmlns:vms="clr-namespace:ServiceLib.ViewModels;assembly=ServiceLib" Title="v2rayN" Width="700" Height="500" diff --git a/v2rayN/v2rayN/Views/AddServer2Window.xaml.cs b/v2rayN/v2rayN/Views/AddServer2Window.xaml.cs index 1f78e790..b3038418 100644 --- a/v2rayN/v2rayN/Views/AddServer2Window.xaml.cs +++ b/v2rayN/v2rayN/Views/AddServer2Window.xaml.cs @@ -1,7 +1,6 @@ using ReactiveUI; using System.Reactive.Disposables; using System.Windows; -using v2rayN.ViewModels; namespace v2rayN.Views { @@ -40,17 +39,19 @@ namespace v2rayN.Views private bool UpdateViewHandler(EViewAction action, object? obj) { - if (action == EViewAction.CloseWindow) + switch (action) { - this.DialogResult = true; - } - else if (action == EViewAction.BrowseServer) - { - if (UI.OpenFileDialog(out string fileName, "Config|*.json|YAML|*.yaml;*.yml|All|*.*") != true) - { - return false; - } - ViewModel?.BrowseServer(fileName); + case EViewAction.CloseWindow: + this.DialogResult = true; + break; + + case EViewAction.BrowseServer: + if (UI.OpenFileDialog(out string fileName, "Config|*.json|YAML|*.yaml;*.yml|All|*.*") != true) + { + return false; + } + ViewModel?.BrowseServer(fileName); + break; } return true; diff --git a/v2rayN/v2rayN/Views/AddServerWindow.xaml b/v2rayN/v2rayN/Views/AddServerWindow.xaml index 6dfdb1a2..5cedc44a 100644 --- a/v2rayN/v2rayN/Views/AddServerWindow.xaml +++ b/v2rayN/v2rayN/Views/AddServerWindow.xaml @@ -8,7 +8,7 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:reactiveui="http://reactiveui.net" xmlns:resx="clr-namespace:ServiceLib.Resx;assembly=ServiceLib" - xmlns:vms="clr-namespace:v2rayN.ViewModels" + xmlns:vms="clr-namespace:ServiceLib.ViewModels;assembly=ServiceLib" Title="{x:Static resx:ResUI.menuServers}" Width="900" Height="700" diff --git a/v2rayN/v2rayN/Views/AddServerWindow.xaml.cs b/v2rayN/v2rayN/Views/AddServerWindow.xaml.cs index d55aab33..e2da0157 100644 --- a/v2rayN/v2rayN/Views/AddServerWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/AddServerWindow.xaml.cs @@ -2,7 +2,6 @@ using System.Reactive.Disposables; using System.Windows; using System.Windows.Controls; -using v2rayN.ViewModels; namespace v2rayN.Views { @@ -223,9 +222,11 @@ namespace v2rayN.Views private bool UpdateViewHandler(EViewAction action, object? obj) { - if (action == EViewAction.CloseWindow) + switch (action) { - this.DialogResult = true; + case EViewAction.CloseWindow: + this.DialogResult = true; + break; } return true; } diff --git a/v2rayN/v2rayN/Views/ClashConnectionsView.xaml b/v2rayN/v2rayN/Views/ClashConnectionsView.xaml index ccb5caec..8aa7d97c 100644 --- a/v2rayN/v2rayN/Views/ClashConnectionsView.xaml +++ b/v2rayN/v2rayN/Views/ClashConnectionsView.xaml @@ -7,7 +7,7 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:reactiveui="http://reactiveui.net" xmlns:resx="clr-namespace:ServiceLib.Resx;assembly=ServiceLib" - xmlns:vms="clr-namespace:v2rayN.ViewModels" + xmlns:vms="clr-namespace:ServiceLib.ViewModels;assembly=ServiceLib" d:DesignHeight="450" d:DesignWidth="800" x:TypeArguments="vms:ClashConnectionsViewModel" diff --git a/v2rayN/v2rayN/Views/ClashConnectionsView.xaml.cs b/v2rayN/v2rayN/Views/ClashConnectionsView.xaml.cs index 7846aa4f..d3c61617 100644 --- a/v2rayN/v2rayN/Views/ClashConnectionsView.xaml.cs +++ b/v2rayN/v2rayN/Views/ClashConnectionsView.xaml.cs @@ -2,7 +2,6 @@ using ReactiveUI; using System.Reactive.Disposables; using System.Windows; using System.Windows.Threading; -using v2rayN.ViewModels; namespace v2rayN.Views { diff --git a/v2rayN/v2rayN/Views/ClashProxiesView.xaml b/v2rayN/v2rayN/Views/ClashProxiesView.xaml index 7d87a05d..71654aa8 100644 --- a/v2rayN/v2rayN/Views/ClashProxiesView.xaml +++ b/v2rayN/v2rayN/Views/ClashProxiesView.xaml @@ -8,7 +8,7 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:reactiveui="http://reactiveui.net" xmlns:resx="clr-namespace:ServiceLib.Resx;assembly=ServiceLib" - xmlns:vms="clr-namespace:v2rayN.ViewModels" + xmlns:vms="clr-namespace:ServiceLib.ViewModels;assembly=ServiceLib" d:DesignHeight="450" d:DesignWidth="800" x:TypeArguments="vms:ClashProxiesViewModel" diff --git a/v2rayN/v2rayN/Views/ClashProxiesView.xaml.cs b/v2rayN/v2rayN/Views/ClashProxiesView.xaml.cs index dd40d85e..0715a30c 100644 --- a/v2rayN/v2rayN/Views/ClashProxiesView.xaml.cs +++ b/v2rayN/v2rayN/Views/ClashProxiesView.xaml.cs @@ -4,7 +4,6 @@ using System.Reactive.Disposables; using System.Windows; using System.Windows.Input; using System.Windows.Threading; -using v2rayN.ViewModels; namespace v2rayN.Views { diff --git a/v2rayN/v2rayN/Views/DNSSettingWindow.xaml b/v2rayN/v2rayN/Views/DNSSettingWindow.xaml index 06178169..7776e418 100644 --- a/v2rayN/v2rayN/Views/DNSSettingWindow.xaml +++ b/v2rayN/v2rayN/Views/DNSSettingWindow.xaml @@ -8,7 +8,7 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:reactiveui="http://reactiveui.net" xmlns:resx="clr-namespace:ServiceLib.Resx;assembly=ServiceLib" - xmlns:vms="clr-namespace:v2rayN.ViewModels" + xmlns:vms="clr-namespace:ServiceLib.ViewModels;assembly=ServiceLib" Title="{x:Static resx:ResUI.menuDNSSetting}" Width="1000" Height="700" diff --git a/v2rayN/v2rayN/Views/DNSSettingWindow.xaml.cs b/v2rayN/v2rayN/Views/DNSSettingWindow.xaml.cs index 2d98a425..08c55513 100644 --- a/v2rayN/v2rayN/Views/DNSSettingWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/DNSSettingWindow.xaml.cs @@ -1,7 +1,6 @@ using ReactiveUI; using System.Reactive.Disposables; using System.Windows; -using v2rayN.ViewModels; namespace v2rayN.Views { @@ -56,9 +55,11 @@ namespace v2rayN.Views private bool UpdateViewHandler(EViewAction action, object? obj) { - if (action == EViewAction.CloseWindow) + switch (action) { - this.DialogResult = true; + case EViewAction.CloseWindow: + this.DialogResult = true; + break; } return true; } diff --git a/v2rayN/v2rayN/Views/GlobalHotkeySettingWindow.xaml b/v2rayN/v2rayN/Views/GlobalHotkeySettingWindow.xaml index 874d95ce..1100b522 100644 --- a/v2rayN/v2rayN/Views/GlobalHotkeySettingWindow.xaml +++ b/v2rayN/v2rayN/Views/GlobalHotkeySettingWindow.xaml @@ -8,7 +8,7 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:reactiveui="http://reactiveui.net" xmlns:resx="clr-namespace:ServiceLib.Resx;assembly=ServiceLib" - xmlns:vms="clr-namespace:v2rayN.ViewModels" + xmlns:vms="clr-namespace:ServiceLib.ViewModels;assembly=ServiceLib" Title="{x:Static resx:ResUI.menuSetting}" Width="700" Height="500" diff --git a/v2rayN/v2rayN/Views/MainWindow.xaml b/v2rayN/v2rayN/Views/MainWindow.xaml index a8a87a61..efadb9cb 100644 --- a/v2rayN/v2rayN/Views/MainWindow.xaml +++ b/v2rayN/v2rayN/Views/MainWindow.xaml @@ -1,15 +1,15 @@ _noticeHandler, typeof(NoticeHandler)); - + MessageBus.Current.Listen(Global.CommandSendSnackMsg).Subscribe(x => DelegateSnackMsg(x)); ViewModel = new MainWindowViewModel(UpdateViewHandler); Locator.CurrentMutable.RegisterLazySingleton(() => ViewModel, typeof(MainWindowViewModel)); @@ -146,7 +143,19 @@ namespace v2rayN.Views }); var IsAdministrator = WindowsUtils.IsAdministrator(); + ViewModel.IsAdministrator = IsAdministrator; this.Title = $"{Utils.GetVersion()} - {(IsAdministrator ? ResUI.RunAsAdmin : ResUI.NotRunAsAdmin)}"; + if (_config.tunModeItem.enableTun) + { + if (IsAdministrator) + { + ViewModel.EnableTun = true; + } + else + { + _config.tunModeItem.enableTun = ViewModel.EnableTun = false; + } + } if (!_config.guiItem.enableHWA) { @@ -182,6 +191,19 @@ namespace v2rayN.Views #region Event + private void OnProgramStarted(object state, bool timeout) + { + ShowHideWindow(true); + } + + private void DelegateSnackMsg(string content) + { + Application.Current?.Dispatcher.Invoke((() => + { + MainSnackbar.MessageQueue?.Enqueue(content); + }), DispatcherPriority.Normal); + } + private bool UpdateViewHandler(EViewAction action, object? obj) { switch (action) @@ -258,9 +280,19 @@ namespace v2rayN.Views Application.Current.Shutdown(); break; - case EViewAction ScanScreenTask: + case EViewAction.ScanScreenTask: ScanScreenTaskAsync().ContinueWith(_ => { }); break; + + case EViewAction.UpdateSysProxy: + if (obj is null) return false; + SysProxyHandler.UpdateSysProxy(_config, (bool)obj); + break; + + case EViewAction.AddServerViaClipboard: + var clipboardData = WindowsUtils.GetClipboardData(); + ViewModel?.AddServerViaClipboard(clipboardData); + break; } return true; @@ -321,7 +353,8 @@ namespace v2rayN.Views switch (e.Key) { case Key.V: - ViewModel?.AddServerViaClipboard(); + var clipboardData = WindowsUtils.GetClipboardData(); + ViewModel?.AddServerViaClipboard(clipboardData); break; case Key.S: diff --git a/v2rayN/v2rayN/Views/OptionSettingWindow.xaml b/v2rayN/v2rayN/Views/OptionSettingWindow.xaml index d3bb85ae..22292711 100644 --- a/v2rayN/v2rayN/Views/OptionSettingWindow.xaml +++ b/v2rayN/v2rayN/Views/OptionSettingWindow.xaml @@ -8,7 +8,7 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:reactiveui="http://reactiveui.net" xmlns:resx="clr-namespace:ServiceLib.Resx;assembly=ServiceLib" - xmlns:vms="clr-namespace:v2rayN.ViewModels" + xmlns:vms="clr-namespace:ServiceLib.ViewModels;assembly=ServiceLib" Title="{x:Static resx:ResUI.menuSetting}" Width="1000" Height="700" diff --git a/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs b/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs index 09bdbe4f..c5549a30 100644 --- a/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs @@ -4,7 +4,6 @@ using System.IO; using System.Reactive.Disposables; using System.Windows; using System.Windows.Media; -using v2rayN.ViewModels; namespace v2rayN.Views { @@ -171,10 +170,12 @@ namespace v2rayN.Views private bool UpdateViewHandler(EViewAction action, object? obj) { - if (action == EViewAction.CloseWindow) + switch (action) { - WindowsUtils.SetAutoRun(Global.AutoRunRegPath, Global.AutoRunName, togAutoRun.IsChecked ?? false); - this.DialogResult = true; + case EViewAction.CloseWindow: + WindowsUtils.SetAutoRun(Global.AutoRunRegPath, Global.AutoRunName, togAutoRun.IsChecked ?? false); + this.DialogResult = true; + break; } return true; } diff --git a/v2rayN/v2rayN/Views/ProfilesView.xaml b/v2rayN/v2rayN/Views/ProfilesView.xaml index a8c03958..024e41a2 100644 --- a/v2rayN/v2rayN/Views/ProfilesView.xaml +++ b/v2rayN/v2rayN/Views/ProfilesView.xaml @@ -9,7 +9,7 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:reactiveui="http://reactiveui.net" xmlns:resx="clr-namespace:ServiceLib.Resx;assembly=ServiceLib" - xmlns:vms="clr-namespace:v2rayN.ViewModels" + xmlns:vms="clr-namespace:ServiceLib.ViewModels;assembly=ServiceLib" d:DesignHeight="450" d:DesignWidth="800" x:TypeArguments="vms:ProfilesViewModel" diff --git a/v2rayN/v2rayN/Views/ProfilesView.xaml.cs b/v2rayN/v2rayN/Views/ProfilesView.xaml.cs index 6bcdacc6..3f2530eb 100644 --- a/v2rayN/v2rayN/Views/ProfilesView.xaml.cs +++ b/v2rayN/v2rayN/Views/ProfilesView.xaml.cs @@ -10,7 +10,6 @@ using System.Windows.Input; using System.Windows.Media; using System.Windows.Threading; using v2rayN.Base; -using v2rayN.ViewModels; using Point = System.Windows.Point; namespace v2rayN.Views @@ -103,6 +102,11 @@ namespace v2rayN.Views { switch (action) { + case EViewAction.SetClipboardData: + if (obj is null) return false; + WindowsUtils.SetClipboardData((string)obj); + break; + case EViewAction.AdjustMainLvColWidth: Application.Current?.Dispatcher.Invoke((() => { diff --git a/v2rayN/v2rayN/Views/RoutingRuleDetailsWindow.xaml b/v2rayN/v2rayN/Views/RoutingRuleDetailsWindow.xaml index 75307c9f..920cdecf 100644 --- a/v2rayN/v2rayN/Views/RoutingRuleDetailsWindow.xaml +++ b/v2rayN/v2rayN/Views/RoutingRuleDetailsWindow.xaml @@ -1,14 +1,14 @@