Rewrite v2rayUpgrade in WPF

This commit is contained in:
Computer Idiot 2022-08-16 13:41:13 +08:00
parent ec2fbca979
commit 9b35fb8991
9 changed files with 205 additions and 395 deletions

View file

@ -0,0 +1,8 @@
<Application x:Class="v2rayUpgrade.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>

View file

@ -0,0 +1,10 @@
using System.Windows;
namespace v2rayUpgrade;
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}

View file

@ -1,107 +0,0 @@
namespace v2rayUpgrade
{
partial class MainForm
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnClose = new System.Windows.Forms.Button();
this.btnOK = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// btnClose
//
this.btnClose.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnClose.Font = new System.Drawing.Font("微软雅黑", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.btnClose.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.btnClose.Location = new System.Drawing.Point(367, 118);
this.btnClose.Name = "btnClose";
this.btnClose.Size = new System.Drawing.Size(184, 89);
this.btnClose.TabIndex = 1;
this.btnClose.Text = "&Exit(退出)";
this.btnClose.UseVisualStyleBackColor = true;
this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
//
// btnOK
//
this.btnOK.Font = new System.Drawing.Font("微软雅黑", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.btnOK.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.btnOK.Location = new System.Drawing.Point(81, 118);
this.btnOK.Name = "btnOK";
this.btnOK.Size = new System.Drawing.Size(184, 89);
this.btnOK.TabIndex = 0;
this.btnOK.Text = "&Upgrade(升级)";
this.btnOK.UseVisualStyleBackColor = true;
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("微软雅黑", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label1.Location = new System.Drawing.Point(79, 64);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(205, 15);
this.label1.TabIndex = 8;
this.label1.Text = "升级成功后将自动重启v2rayN";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Font = new System.Drawing.Font("微软雅黑", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label2.Location = new System.Drawing.Point(79, 37);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(471, 15);
this.label2.TabIndex = 9;
this.label2.Text = "v2rayN will restart automatically after successful upgrade";
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(616, 284);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.btnClose);
this.Controls.Add(this.btnOK);
this.Name = "MainForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "v2rayUpgrade";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button btnClose;
private System.Windows.Forms.Button btnOK;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
}
}

View file

@ -1,148 +0,0 @@
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Text;
using System.Web;
using System.Windows.Forms;
namespace v2rayUpgrade
{
public partial class MainForm : Form
{
private readonly string defaultFilename = "v2ray-windows.zip";
private string fileName;
public MainForm(string[] args)
{
InitializeComponent();
if (args.Length > 0)
{
fileName = string.Join(" ", args);
fileName = HttpUtility.UrlDecode(fileName);
}
}
private void showWarn(string message)
{
MessageBox.Show(message, "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
private void btnOK_Click(object sender, EventArgs e)
{
try
{
Process[] existing = Process.GetProcessesByName("v2rayN");
foreach (Process p in existing)
{
string path = p.MainModule.FileName;
if (path == GetPath("v2rayN.exe"))
{
p.Kill();
p.WaitForExit(100);
}
}
}
catch (Exception ex)
{
// Access may be denied without admin right. The user may not be an administrator.
showWarn("Failed to close v2rayN(关闭v2rayN失败).\n" +
"Close it manually, or the upgrade may fail.(请手动关闭正在运行的v2rayN否则可能升级失败。\n\n" + ex.StackTrace);
}
StringBuilder sb = new StringBuilder();
try
{
if (!File.Exists(fileName))
{
if (File.Exists(defaultFilename))
{
fileName = defaultFilename;
}
else
{
showWarn("Upgrade Failed, File Not Exist(升级失败,文件不存在).");
return;
}
}
string thisAppOldFile = Application.ExecutablePath + ".tmp";
File.Delete(thisAppOldFile);
string startKey = "v2rayN/";
using (ZipArchive archive = ZipFile.OpenRead(fileName))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
try
{
if (entry.Length == 0)
{
continue;
}
string fullName = entry.FullName;
if (fullName.StartsWith(startKey))
{
fullName = fullName.Substring(startKey.Length, fullName.Length - startKey.Length);
}
if (Application.ExecutablePath.ToLower() == GetPath(fullName).ToLower())
{
File.Move(Application.ExecutablePath, thisAppOldFile);
}
string entryOuputPath = GetPath(fullName);
FileInfo fileInfo = new FileInfo(entryOuputPath);
fileInfo.Directory.Create();
entry.ExtractToFile(entryOuputPath, true);
}
catch (Exception ex)
{
sb.Append(ex.StackTrace);
}
}
}
}
catch (Exception ex)
{
showWarn("Upgrade Failed(升级失败)." + ex.StackTrace);
return;
}
if (sb.Length > 0)
{
showWarn("Upgrade Failed,Hold ctrl + c to copy to clipboard.\n" +
"(升级失败,按住ctrl+c可以复制到剪贴板)." + sb.ToString());
return;
}
Process.Start("v2rayN.exe");
MessageBox.Show("Upgrade successed(升级成功)", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
Close();
}
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
public static string GetExePath()
{
return Application.ExecutablePath;
}
public static string StartupPath()
{
return Application.StartupPath;
}
public static string GetPath(string fileName)
{
string startupPath = StartupPath();
if (string.IsNullOrEmpty(fileName))
{
return startupPath;
}
return Path.Combine(startupPath, fileName);
}
}
}

View file

@ -1,120 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View file

@ -0,0 +1,28 @@
<Window x:Class="v2rayUpgrade.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="v2rayUpgrade" Height="450" Width="800"
WindowStartupLocation="CenterScreen">
<Grid Margin="50px">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<StackPanel>
<Label Margin="20px" Content="v2rayN will restart automatically after successful upgrade"
FontSize="15" />
<Label Margin="20px" Content="升级成功后将自动重启v2rayN" FontSize="15" />
</StackPanel>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Button Margin="50px" Grid.Column="0" Content="_Upgrade(升级)" Click="ButtonOK_OnClick" />
<Button Margin="50px" Grid.Column="1" Content="_Exit(退出)" Click="ButtonClose_OnClick" />
</Grid>
</Grid>
</Window>

View file

@ -0,0 +1,155 @@
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Text;
using System.Web;
using System.Windows;
namespace v2rayUpgrade;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private const string DefaultFilename = "v2rayN.zip_temp";
private string _fileName;
public MainWindow()
{
InitializeComponent();
var args = Environment.GetCommandLineArgs();
if (args.Length == 2)
{
_fileName = args[1];
_fileName = HttpUtility.UrlDecode(_fileName);
}
else
{
_fileName = DefaultFilename;
}
}
private static void ShowWarn(string message)
{
MessageBox.Show(message, "", MessageBoxButton.OK, MessageBoxImage.Warning);
}
private void ButtonOK_OnClick(object sender, EventArgs e)
{
try
{
var existing = Process.GetProcessesByName("v2rayN");
foreach (var p in existing)
{
var path = p.MainModule!.FileName;
if (path != GetPath("v2rayN.exe")) continue;
p.Kill();
p.WaitForExit(100);
}
}
catch (Exception ex)
{
// Access may be denied without admin right. The user may not be an administrator.
ShowWarn(
$"Failed to close v2rayN(关闭v2rayN失败).\nClose it manually, or the upgrade may fail.(请手动关闭正在运行的v2rayN否则可能升级失败。\n\n{ex.StackTrace}");
}
var sb = new StringBuilder();
try
{
if (!File.Exists(_fileName))
{
if (File.Exists(DefaultFilename))
{
_fileName = DefaultFilename;
}
else
{
ShowWarn("Upgrade Failed, File Not Exist(升级失败,文件不存在).");
return;
}
}
var thisAppOldFile = ExePath() + ".tmp";
File.Delete(thisAppOldFile);
const string startKey = "v2rayN/";
using var archive = ZipFile.OpenRead(_fileName);
foreach (var entry in archive.Entries)
{
try
{
if (entry.Length == 0)
{
continue;
}
var fullName = entry.FullName;
if (fullName.StartsWith(startKey))
{
fullName = fullName.Substring(startKey.Length, fullName.Length - startKey.Length);
}
if (string.Equals(ExePath(), GetPath(fullName), StringComparison.CurrentCultureIgnoreCase))
{
File.Move(ExePath(), thisAppOldFile);
}
var entryOutputPath = GetPath(fullName);
var fileInfo = new FileInfo(entryOutputPath);
fileInfo.Directory!.Create();
entry.ExtractToFile(entryOutputPath, true);
}
catch (Exception ex)
{
sb.Append(ex.StackTrace);
}
}
}
catch (Exception ex)
{
ShowWarn("Upgrade Failed(升级失败)." + ex.StackTrace);
return;
}
if (sb.Length > 0)
{
ShowWarn($"Upgrade Failed,Hold ctrl + c to copy to clipboard.\n(升级失败,按住ctrl+c可以复制到剪贴板).{sb}");
return;
}
Process.Start("v2rayN.exe");
MessageBox.Show("Upgrade succeeded(升级成功)", "", MessageBoxButton.OK, MessageBoxImage.Information);
Close();
}
private void ButtonClose_OnClick(object sender, EventArgs e)
{
Close();
}
private static string ExePath()
{
return Process.GetCurrentProcess().MainModule.FileName;
}
private static string StartupPath()
{
return Path.GetDirectoryName(ExePath())!;
}
private static string GetPath(string fileName)
{
var startupPath = StartupPath();
if (string.IsNullOrEmpty(fileName))
{
return startupPath;
}
return Path.Combine(startupPath, fileName);
}
}

View file

@ -1,19 +0,0 @@
using System;
using System.Windows.Forms;
namespace v2rayUpgrade
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm(args));
}
}
}

View file

@ -1,11 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<LangVersion>preview</LangVersion>
<TargetFramework>net48</TargetFramework> <TargetFramework>net48</TargetFramework>
<OutputType>WinExe</OutputType> <OutputType>WinExe</OutputType>
<UseWindowsForms>true</UseWindowsForms> <Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<Copyright>Copyright © 2019-2020 (GPLv3)</Copyright> <Copyright>Copyright © 2019-2020 (GPLv3)</Copyright>
<FileVersion>1.1.0.0</FileVersion> <FileVersion>1.1.0.0</FileVersion>
<ApplicationManifest>app.manifest</ApplicationManifest> <ApplicationManifest>app.manifest</ApplicationManifest>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="System.IO.Compression" /> <Reference Include="System.IO.Compression" />