Files
netch/Netch/Netch.cs
Connection Refused 5803b94ae9 Update NativeMethods
2021-04-02 02:40:13 +08:00

116 lines
3.9 KiB
C#

using Netch.Controllers;
using Netch.Forms;
using Netch.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static Vanara.PInvoke.Kernel32;
namespace Netch
{
public static class Netch
{
public static readonly SingleInstance.SingleInstance SingleInstance = new($"Global\\{nameof(Netch)}");
/// <summary>
/// 应用程序的主入口点
/// </summary>
[STAThread]
public static void Main(string[] args)
{
#if DEBUG
AttachAllocConsole();
#else
if (args.Contains(Constants.Parameter.Console))
AttachAllocConsole();
#endif
if (args.Contains(Constants.Parameter.ForceUpdate))
Flags.AlwaysShowNewVersionFound = true;
// 设置当前目录
Directory.SetCurrentDirectory(Global.NetchDir);
var binPath = Path.Combine(Global.NetchDir, "bin");
Environment.SetEnvironmentVariable("PATH", $"{Environment.GetEnvironmentVariable("PATH")};{binPath}");
AddDllDirectory(binPath);
Updater.Updater.CleanOld(Global.NetchDir);
// 预创建目录
var directories = new[] { "mode\\Custom", "data", "i18n", "logging" };
foreach (var item in directories)
if (!Directory.Exists(item))
Directory.CreateDirectory(item);
// 加载配置
Configuration.Load();
if (!SingleInstance.IsFirstInstance)
{
SingleInstance.PassArgumentsToFirstInstance(args.Append(Constants.Parameter.Show));
Environment.Exit(0);
return;
}
SingleInstance.ArgumentsReceived.Subscribe(SingleInstance_ArgumentsReceived);
SingleInstance.ListenForArgumentsFromSuccessiveInstances();
// 清理上一次的日志文件,防止淤积占用磁盘空间
if (Directory.Exists("logging"))
{
var directory = new DirectoryInfo("logging");
foreach (var file in directory.GetFiles())
file.Delete();
foreach (var dir in directory.GetDirectories())
dir.Delete(true);
}
// 加载语言
i18N.Load(Global.Settings.Language);
if (!Directory.Exists("bin") || !Directory.EnumerateFileSystemEntries("bin").Any())
{
MessageBoxX.Show(i18N.Translate("Please extract all files then run the program!"));
Environment.Exit(2);
}
Global.Logger.Info($"版本: {UpdateChecker.Owner}/{UpdateChecker.Repo}@{UpdateChecker.Version}");
Task.Run(() => { Global.Logger.Info($"主程序 SHA256: {Utils.Utils.SHA256CheckSum(Global.NetchExecutable)}"); });
// 绑定错误捕获
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.ThreadException += Application_OnException;
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(Global.MainForm);
}
private static void AttachAllocConsole()
{
if (!AttachConsole(ATTACH_PARENT_PROCESS))
AllocConsole();
}
public static void Application_OnException(object sender, ThreadExceptionEventArgs e)
{
Global.Logger.Error(e.Exception.ToString());
Global.Logger.ShowLog();
}
private static void SingleInstance_ArgumentsReceived(IEnumerable<string> args)
{
if (args.Contains(Constants.Parameter.Show))
{
Global.MainForm.ShowMainFormToolStripButton_Click(null!, null!);
}
}
}
}