Magic the magic happen

This commit is contained in:
Raymond Lynch 2021-07-11 16:32:29 -04:00
parent 84769ae5b7
commit 658eedca37
188 changed files with 10329 additions and 3549 deletions

View file

@ -1,12 +1,13 @@
using System;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Dalamud.Configuration;
using Dalamud.Interface;
using EasyHook;
using Dalamud.Configuration.Internal;
using Dalamud.Interface.Internal;
using Newtonsoft.Json;
using Serilog;
using Serilog.Core;
using Serilog.Events;
@ -16,31 +17,41 @@ namespace Dalamud
/// <summary>
/// The main entrypoint for the Dalamud system.
/// </summary>
public sealed class EntryPoint : IEntryPoint
public sealed class EntryPoint
{
/// <summary>
/// Initializes a new instance of the <see cref="EntryPoint"/> class.
/// A delegate used during initialization of the CLR from Dalamud.Boot.
/// </summary>
/// <param name="ctx">The <see cref="RemoteHooking.IContext"/> used to load the DLL.</param>
/// <param name="info">The <see cref="DalamudStartInfo"/> containing information needed to initialize Dalamud.</param>
public EntryPoint(RemoteHooking.IContext ctx, DalamudStartInfo info)
/// <param name="infoPtr">Pointer to a serialized <see cref="DalamudStartInfo"/> data.</param>
public delegate void InitDelegate(IntPtr infoPtr);
/// <summary>
/// Initialize Dalamud.
/// </summary>
/// <param name="infoPtr">Pointer to a serialized <see cref="DalamudStartInfo"/> data.</param>
public static void Initialize(IntPtr infoPtr)
{
// Required by EasyHook
var infoStr = Marshal.PtrToStringAnsi(infoPtr);
var info = JsonConvert.DeserializeObject<DalamudStartInfo>(infoStr);
new Thread(() => RunThread(info)).Start();
}
/// <summary>
/// Initialize all Dalamud subsystems and start running on the main thread.
/// </summary>
/// <param name="ctx">The <see cref="RemoteHooking.IContext"/> used to load the DLL.</param>
/// <param name="info">The <see cref="DalamudStartInfo"/> containing information needed to initialize Dalamud.</param>
public void Run(RemoteHooking.IContext ctx, DalamudStartInfo info)
private static void RunThread(DalamudStartInfo info)
{
// Load configuration first to get some early persistent state, like log level
var configuration = DalamudConfiguration.Load(info.ConfigurationPath);
// Setup logger
var (logger, levelSwitch) = this.NewLogger(info.WorkingDirectory, configuration.LogLevel);
Log.Logger = logger;
var levelSwitch = InitLogging(info.WorkingDirectory);
// Log any unhandled exception.
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
TaskScheduler.UnobservedTaskException += OnUnobservedTaskException;
var finishSignal = new ManualResetEvent(false);
@ -50,12 +61,7 @@ namespace Dalamud
Log.Information("Initializing a session..");
// This is due to GitHub not supporting TLS 1.0, so we enable all TLS versions globally
System.Net.ServicePointManager.SecurityProtocol =
SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;
// Log any unhandled exception.
AppDomain.CurrentDomain.UnhandledException += this.OnUnhandledException;
TaskScheduler.UnobservedTaskException += this.OnUnobservedTaskException;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls;
var dalamud = new Dalamud(info, levelSwitch, finishSignal, configuration);
Log.Information("Starting a session..");
@ -72,7 +78,8 @@ namespace Dalamud
}
finally
{
AppDomain.CurrentDomain.UnhandledException -= this.OnUnhandledException;
TaskScheduler.UnobservedTaskException -= OnUnobservedTaskException;
AppDomain.CurrentDomain.UnhandledException -= OnUnhandledException;
Log.Information("Session has ended.");
Log.CloseAndFlush();
@ -81,7 +88,7 @@ namespace Dalamud
}
}
private (Logger Logger, LoggingLevelSwitch LevelSwitch) NewLogger(string baseDirectory, LogEventLevel logLevel)
private static LoggingLevelSwitch InitLogging(string baseDirectory)
{
#if DEBUG
var logPath = Path.Combine(baseDirectory, "dalamud.log");
@ -96,33 +103,32 @@ namespace Dalamud
#else
levelSwitch.MinimumLevel = logLevel;
#endif
var newLogger = new LoggerConfiguration()
Log.Logger = new LoggerConfiguration()
.WriteTo.Async(a => a.File(logPath))
.WriteTo.Sink(SerilogEventSink.Instance)
.MinimumLevel.ControlledBy(levelSwitch)
.CreateLogger();
return (newLogger, levelSwitch);
return levelSwitch;
}
private void OnUnhandledException(object sender, UnhandledExceptionEventArgs arg)
private static void OnUnhandledException(object sender, UnhandledExceptionEventArgs args)
{
switch (arg.ExceptionObject)
switch (args.ExceptionObject)
{
case Exception ex:
Log.Fatal(ex, "Unhandled exception on AppDomain");
break;
default:
Log.Fatal("Unhandled SEH object on AppDomain: {Object}", arg.ExceptionObject);
Log.Fatal("Unhandled SEH object on AppDomain: {Object}", args.ExceptionObject);
break;
}
}
private void OnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
private static void OnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs args)
{
if (!e.Observed)
Log.Error(e.Exception, "Unobserved exception in Task.");
if (!args.Observed)
Log.Error(args.Exception, "Unobserved exception in Task.");
}
}
}