/**
* 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 .
*
**/
using System;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using GrasscutterTools.Properties;
using GrasscutterTools.Utils;
namespace GrasscutterTools
{
internal static class Program
{
private const string TAG = "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;
}
///
/// 应用程序的主入口点。
///
[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;
Logger.I(TAG, "Program startup");
try
{
// 初始化语言环境
if (!string.IsNullOrEmpty(Settings.Default.DefaultLanguage))
MultiLanguage.SetDefaultLanguage(Settings.Default.DefaultLanguage);
}
catch (Exception ex)
{
Logger.W(TAG, "Loading language error", ex);
MessageBox.Show(Resources.SettingLoadError + ex.ToString(), Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Application.Run(new Forms.FormMain());
Logger.I(TAG, "Program end.");
}
#region - 全局异常处理 -
private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
string str = GetExceptionMsg(e.Exception, e.ToString());
Logger.E(TAG, "Application_ThreadException");
Logger.E(TAG, 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());
Logger.E(TAG, "CurrentDomain_UnhandledException");
Logger.E(TAG, str);
MessageBox.Show(str, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
///
/// 生成自定义异常消息
///
/// 异常对象
/// 备用异常消息:当ex为null时有效
/// 异常字符串文本
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 - 全局异常处理 -
}
}