自动下载并更新

This commit is contained in:
ChsBuffer
2020-08-12 02:22:26 +08:00
parent 6f1025c73e
commit be2ee1a5df
16 changed files with 470 additions and 22 deletions

2
NetchUpdater/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
/bin
/obj

View File

@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{828318A8-9B90-4A5F-BD6B-E632CC9D8933}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>NetchUpdater</RootNamespace>
<AssemblyName>NetchUpdater</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<OutputPath>bin\x64\Release\</OutputPath>
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<OutputPath>bin\x64\Debug\</OutputPath>
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.IO.Compression.FileSystem" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

172
NetchUpdater/Program.cs Normal file
View File

@@ -0,0 +1,172 @@
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace NetchUpdater
{
internal class Program
{
public static void Main(string[] args)
{
var updaterDirectory = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
string zipFile, netchDir;
int port;
try
{
if (args.Length != 3)
{
Console.WriteLine("The program is not user-oriented\n此程序不是面向用户的");
return;
}
try
{
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");
return;
}
try
{
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");
return;
}
try
{
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");
return;
}
if (File.Exists(Path.Combine(updaterDirectory, "Netch.exe")))
{
var tempPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Directory.CreateDirectory(tempPath);
File.Copy(Process.GetCurrentProcess().MainModule.FileName,
Path.Combine(tempPath, AppDomain.CurrentDomain.FriendlyName));
Process.Start(new ProcessStartInfo
{
FileName = Path.Combine(tempPath, AppDomain.CurrentDomain.FriendlyName),
Arguments = $"{args[0]} {args[1]} {args[2]}",
UseShellExecute = false
});
return;
}
/*if (!Debugger.IsAttached)
{
Console.WriteLine("Waiting Attach");
Thread.Sleep(1000);
}*/
Process[] _;
if ((_ = Process.GetProcessesByName("Netch")).Any())
{
Console.WriteLine("Found Netch process, Send exit command");
try
{
var udpClient = new UdpClient("127.0.0.1", port);
var sendBytes = Encoding.ASCII.GetBytes("Exit");
udpClient.Send(sendBytes, sendBytes.Length);
}
catch
{
Console.WriteLine("Send command failed");
return;
}
foreach (var proc in _)
{
try
{
proc.WaitForExit();
}
catch (Exception)
{
// ignored
}
}
}
Console.WriteLine("Extract Zip");
ExtractToDirectory(zipFile, netchDir, true);
Console.WriteLine("Start Netch");
Process.Start(new ProcessStartInfo
{
FileName = Path.Combine(netchDir, "Netch.exe"),
UseShellExecute = true,
});
}
catch (Exception e)
{
if(e is InvalidDataException)
Console.WriteLine("Zip file Broken");
Console.WriteLine(e.ToString());
}
finally
{
Thread.Sleep(1000);
}
}
public static void ExtractToDirectory(string archiveFileName, string destinationDirectoryName, bool overwrite)
{
if (!overwrite)
{
ZipFile.ExtractToDirectory(archiveFileName, destinationDirectoryName);
}
else
{
using (var archive = ZipFile.OpenRead(archiveFileName))
{
foreach (var file in archive.Entries)
{
Console.WriteLine(file.FullName);
var completeFileName = Path.Combine(destinationDirectoryName, file.FullName);
var directory = Path.GetDirectoryName(completeFileName);
if (!Directory.Exists(directory) && !string.IsNullOrEmpty(directory))
Directory.CreateDirectory(directory);
if (file.Name != "")
file.ExtractToFile(completeFileName, true);
}
}
}
}
}
}

View File

@@ -0,0 +1,35 @@
using System.Reflection;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("NetchUpdater")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("NetchUpdater")]
[assembly: AssemblyCopyright("Copyright © 2020")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("828318A8-9B90-4A5F-BD6B-E632CC9D8933")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]