GrasscutterCommandGenerator/Source/GrasscutterTools/Program.cs
筱傑 c7898401ce Clean up all
Remvoe TextBoxXP
2022-11-27 16:51:47 +08:00

130 lines
5.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Grasscutter Tools
* Copyright (C) 2022 jie65535
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
**/
using System;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using GrasscutterTools.Properties;
namespace GrasscutterTools
{
internal static class Program
{
static Program()
{
AppDomain.CurrentDomain.AssemblyResolve += OnResolveAssembly;
}
private static Assembly OnResolveAssembly(object sender, ResolveEventArgs args)
{
//var executingAssembly = Assembly.GetExecutingAssembly();
//var assemblyName = new AssemblyName(args.Name);
//var path = assemblyName.Name + ".dll";
//if (assemblyName.CultureInfo.Equals(CultureInfo.InvariantCulture) == false)
// path = $@"{assemblyName.CultureInfo}\{path}";
//using (var stream = executingAssembly.GetManifestResourceStream(path))
//{
// if (stream == null) return null;
// var assemblyRawBytes = new byte[stream.Length];
// stream.Read(assemblyRawBytes, 0, assemblyRawBytes.Length);
// return Assembly.Load(assemblyRawBytes);
//}
// 手工加载嵌入的dll文件
if (new AssemblyName(args.Name).Name == "Newtonsoft.Json")
return Assembly.Load(Resources.Newtonsoft_Json);
return null;
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
private static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//设置应用程序处理异常方式ThreadException处理
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
//处理线程异常
Application.ThreadException += Application_ThreadException;
//处理非UI线程异常
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
// 初始化语言环境
if (!string.IsNullOrEmpty(Settings.Default.DefaultLanguage))
MultiLanguage.SetDefaultLanguage(Settings.Default.DefaultLanguage);
Application.Run(new Forms.FormMain());
Console.WriteLine("Program end.");
}
#region - -
private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
string str = GetExceptionMsg(e.Exception, e.ToString());
Console.WriteLine("Application_ThreadException");
Console.WriteLine(str);
MessageBox.Show(str, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
Console.WriteLine("CurrentDomain_UnhandledException");
Console.WriteLine(str);
MessageBox.Show(str, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
/// <summary>
/// 生成自定义异常消息
/// </summary>
/// <param name="ex">异常对象</param>
/// <param name="backStr">备用异常消息当ex为null时有效</param>
/// <returns>异常字符串文本</returns>
private static string GetExceptionMsg(Exception ex, string backStr)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("****************************异常文本****************************");
sb.AppendLine("【出现时间】:" + DateTime.Now.ToString());
if (ex != null)
{
sb.AppendLine("【异常类型】:" + ex.GetType().Name);
sb.AppendLine("【异常信息】:" + ex.Message);
#if DEBUG
sb.AppendLine("【堆栈调用】:" + ex.StackTrace);
#endif
}
else
{
sb.AppendLine("【未处理异常】:" + backStr);
}
sb.AppendLine("***************************************************************");
return sb.ToString();
}
#endregion - -
}
}