From 9943207e1f441b50b9926f2a27fe4b0dc356a461 Mon Sep 17 00:00:00 2001 From: ChsBuffer <33744752+chsbuffer@users.noreply.github.com> Date: Fri, 14 Aug 2020 04:06:30 +0800 Subject: [PATCH] Reduce Reference and cleanup --- .github/workflows/build.yml | 4 +- Netch/Controllers/NTTController.cs | 42 ++++++++----- Netch/Forms/Mode/Process.cs | 15 +---- Netch/Forms/SettingForm.cs | 2 +- Netch/Netch.csproj | 5 +- NetchUpdater/Program.cs | 94 +++++++++++++++--------------- 6 files changed, 81 insertions(+), 81 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a918b01d..b7270426 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -28,7 +28,7 @@ jobs: - name: Restore NuGet Package run: nuget restore Netch.sln - - name: Build .NET 4.8 + - name: Build Solution shell: pwsh run: | .\BUILD.ps1 @@ -37,7 +37,7 @@ jobs: echo "::set-env name=Netch_SHA256::$(.\GetSHA256.ps1 C:\builtfiles\Netch.zip)" echo "::set-env name=Netch_EXE_SHA256::$(.\GetSHA256.ps1 Netch\bin\x64\Release\win-x64\Netch.exe)" - - name: Upload + - name: Upload Artifact uses: actions/upload-artifact@v1 with: name: Netch diff --git a/Netch/Controllers/NTTController.cs b/Netch/Controllers/NTTController.cs index 24870d49..0269ae7b 100644 --- a/Netch/Controllers/NTTController.cs +++ b/Netch/Controllers/NTTController.cs @@ -66,22 +66,34 @@ namespace Netch.Controllers private new void OnOutputDataReceived(object sender, DataReceivedEventArgs e) { - if (!string.IsNullOrEmpty(e.Data)) + if (string.IsNullOrEmpty(e.Data)) return; + var str = e.Data.Split(':'); + if (str.Length < 2) + return; + str[1] = str[1].Trim(); + switch (str[0]) { - if (e.Data.StartsWith("Other address is: ")) - _Other_address = e.Data.Split(':')[1].Trim(); - if (e.Data.StartsWith("Binding test: ")) - _Binding_test = e.Data.Split(':')[1].Trim(); - if (e.Data.StartsWith("Local address: ")) - _Local_address = e.Data.Split(':')[1].Trim(); - if (e.Data.StartsWith("Mapped address: ")) - _Mapped_address = e.Data.Split(':')[1].Trim(); - if (e.Data.StartsWith("Nat mapping behavior: ")) - _Nat_mapping_behavior = e.Data.Split(':')[1].Trim(); - if (e.Data.StartsWith("Nat filtering behavior: ")) - _Nat_filtering_behavior = e.Data.Split(':')[1].Trim(); - if (e.Data.StartsWith("result: ")) - _lastResult = e.Data.Split(':')[1].Trim(); + case "Other address is": + _Other_address = str[1]; + break; + case "Binding test": + _Binding_test = str[1]; + break; + case "Local address": + _Local_address = str[1]; + break; + case "Mapped address": + _Mapped_address = str[1]; + break; + case "Nat mapping behavior": + _Nat_mapping_behavior = str[1]; + break; + case "Nat filtering behavior": + _Nat_filtering_behavior = str[1]; + break; + case "result": + _lastResult = str[1]; + break; } } diff --git a/Netch/Forms/Mode/Process.cs b/Netch/Forms/Mode/Process.cs index 3c7425b6..fd223f96 100644 --- a/Netch/Forms/Mode/Process.cs +++ b/Netch/Forms/Mode/Process.cs @@ -4,7 +4,6 @@ using System.IO; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; -using Microsoft.WindowsAPICodePack.Dialogs; using Netch.Utils; namespace Netch.Forms.Mode @@ -169,18 +168,10 @@ namespace Netch.Forms.Mode private void ScanButton_Click(object sender, EventArgs e) { - var dialog = new CommonOpenFileDialog + var dialog = new FolderBrowserDialog(); + if (dialog.ShowDialog(this) == DialogResult.OK) { - IsFolderPicker = true, - Multiselect = false, - Title = i18N.Translate("Select a folder"), - AddToMostRecentlyUsedList = false, - EnsurePathExists = true, - NavigateToShortcut = true - }; - if (dialog.ShowDialog(Win32Native.GetForegroundWindow()) == CommonFileDialogResult.Ok) - { - ScanDirectory(dialog.FileName); + ScanDirectory(dialog.SelectedPath); MessageBoxX.Show(i18N.Translate("Scan completed")); } } diff --git a/Netch/Forms/SettingForm.cs b/Netch/Forms/SettingForm.cs index b0fc00d4..b6c9e5b0 100644 --- a/Netch/Forms/SettingForm.cs +++ b/Netch/Forms/SettingForm.cs @@ -158,7 +158,7 @@ namespace Netch.Forms var stuns = File.ReadLines("bin\\stun.txt"); STUN_ServerComboBox.Items.AddRange(stuns.ToArray()); } - catch (Exception e) + catch (Exception) { // ignored } diff --git a/Netch/Netch.csproj b/Netch/Netch.csproj index 1aaa67d0..e2466d5a 100644 --- a/Netch/Netch.csproj +++ b/Netch/Netch.csproj @@ -57,13 +57,10 @@ - - + - - diff --git a/NetchUpdater/Program.cs b/NetchUpdater/Program.cs index 09c88315..61b7a180 100644 --- a/NetchUpdater/Program.cs +++ b/NetchUpdater/Program.cs @@ -13,76 +13,65 @@ namespace NetchUpdater { public static void Main(string[] args) { - var updaterDirectory = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName); - string zipFile, netchDir; - int port; - + var result = false; try { + var currentProcess = Process.GetCurrentProcess(); + if (currentProcess.MainModule == null) + { + Console.WriteLine("Current Process MainModule is null"); + return; + } + if (args.Length != 3) { Console.WriteLine("The program is not user-oriented\n此程序不是面向用户的"); return; } - try + // arg0 port + if (!int.TryParse(args[0], out var port)) { - var portParseResult = int.TryParse(args[0], out port); - if (!portParseResult) - { - Console.WriteLine("arg0 Port Parse failed"); - return; - } - } - catch (IndexOutOfRangeException) - { - Console.WriteLine("arg0 Port not specified"); + Console.WriteLine("arg0 Port Parse failed"); return; } - try + // arg1 zipFile + string zipFile; + if (!File.Exists(zipFile = Path.GetFullPath(args[1]))) { - var zipFileParseResult = File.Exists(zipFile = Path.GetFullPath(args[1])); - if (!zipFileParseResult) - { - Console.WriteLine("arg1 Zip file Not found"); - return; - } - } - catch (IndexOutOfRangeException) - { - Console.Write("arg1 Zip file not specified"); + Console.WriteLine("arg1 Zip file Not found"); return; } - try + // arg2 target Directory + string targetDir; + if (!File.Exists(Path.Combine(targetDir = Path.GetFullPath(args[2]), "Netch.exe"))) { - var netchDirParseResult = !File.Exists(Path.Combine(netchDir = Path.GetFullPath(args[2]), "Netch.exe")); - if (netchDirParseResult) - { - Console.Write("arg2 Netch Directory doesn't seems right"); - return; - } - } - catch (IndexOutOfRangeException) - { - Console.Write("arg2 Netch Directory not specified"); + Console.Write("arg2 Netch Directory doesn't seems right"); return; } + // check updater directory + var updaterFullName = currentProcess.MainModule.FileName; + var updaterDirectory = Path.GetDirectoryName(updaterFullName); + var updaterFriendlyName = Path.GetFileName(updaterFullName); + if (File.Exists(Path.Combine(updaterDirectory, "Netch.exe"))) { + // Updater 在目标目录下 + // 将程序复制到临时目录,传递参数 var tempPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); + var newUpdaterPath = Path.Combine(tempPath, updaterFriendlyName); Directory.CreateDirectory(tempPath); - File.Copy(Process.GetCurrentProcess().MainModule.FileName, - Path.Combine(tempPath, AppDomain.CurrentDomain.FriendlyName)); + File.Copy(updaterFullName, newUpdaterPath); Process.Start(new ProcessStartInfo { - FileName = Path.Combine(tempPath, AppDomain.CurrentDomain.FriendlyName), + FileName = newUpdaterPath, Arguments = $"{args[0]} {args[1]} {args[2]}", UseShellExecute = false }); - + result = true; return; } @@ -92,6 +81,7 @@ namespace NetchUpdater Thread.Sleep(1000); }*/ + // Let Netch Exit Process[] _; if ((_ = Process.GetProcessesByName("Netch")).Any()) { @@ -108,7 +98,6 @@ namespace NetchUpdater return; } - foreach (var proc in _) { try @@ -121,28 +110,39 @@ namespace NetchUpdater } } } + Thread.Sleep(500); + // Extract ZIP Console.WriteLine("Extract Zip"); - ExtractToDirectory(zipFile, netchDir, true); + ExtractToDirectory(zipFile, targetDir, true); + + // Start Netch Console.WriteLine("Start Netch"); Process.Start(new ProcessStartInfo { - FileName = Path.Combine(netchDir, "Netch.exe"), + FileName = Path.Combine(targetDir, "Netch.exe"), UseShellExecute = true, }); + result = true; } catch (Exception e) { if (e is InvalidDataException) Console.WriteLine("Zip file Broken"); Console.WriteLine(e.ToString()); - Console.WriteLine("Press any key to exit..."); - Console.Read(); + } + finally + { + if (!result) + { + Console.WriteLine("Press any key to exit..."); + Console.Read(); + } } } - public static void ExtractToDirectory(string archiveFileName, string destinationDirectoryName, bool overwrite) + private static void ExtractToDirectory(string archiveFileName, string destinationDirectoryName, bool overwrite) { if (!overwrite) {