refactor: new code style in EntryPoint.cs

This commit is contained in:
goat 2021-03-31 15:19:25 +02:00
parent 115fa54957
commit a8d2a577b9

View file

@ -3,26 +3,45 @@ using System.IO;
using System.Net; using System.Net;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Dalamud.Interface; using Dalamud.Interface;
using EasyHook; using EasyHook;
using Serilog; using Serilog;
using Serilog.Core; using Serilog.Core;
using Serilog.Events; using Serilog.Events;
namespace Dalamud { namespace Dalamud
public sealed class EntryPoint : IEntryPoint { {
public EntryPoint(RemoteHooking.IContext ctx, DalamudStartInfo info) { /// <summary>
/// The main entrypoint for the Dalamud system.
/// </summary>
public sealed class EntryPoint : IEntryPoint
{
/// <summary>
/// Initializes a new instance of the <see cref="EntryPoint"/> class.
/// </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)
{
// Required by EasyHook // Required by EasyHook
} }
public void Run(RemoteHooking.IContext ctx, DalamudStartInfo info) { /// <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)
{
// Setup logger // Setup logger
var (logger, levelSwitch) = NewLogger(info.WorkingDirectory); var (logger, levelSwitch) = this.NewLogger(info.WorkingDirectory);
Log.Logger = logger; Log.Logger = logger;
var finishSignal = new ManualResetEvent(false); var finishSignal = new ManualResetEvent(false);
try { try
{
Log.Information(new string('-', 80)); Log.Information(new string('-', 80));
Log.Information("Initializing a session.."); Log.Information("Initializing a session..");
@ -31,22 +50,26 @@ namespace Dalamud {
SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3; SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;
// Log any unhandled exception. // Log any unhandled exception.
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException; AppDomain.CurrentDomain.UnhandledException += this.OnUnhandledException;
TaskScheduler.UnobservedTaskException += OnUnobservedTaskException; TaskScheduler.UnobservedTaskException += this.OnUnobservedTaskException;
var dalamud = new Dalamud(info, levelSwitch, finishSignal); var dalamud = new Dalamud(info, levelSwitch, finishSignal);
Log.Information("Starting a session.."); Log.Information("Starting a session..");
// Run session // Run session
dalamud.Start(); dalamud.Start();
dalamud.WaitForUnload(); dalamud.WaitForUnload();
dalamud.Dispose(); dalamud.Dispose();
} catch (Exception ex) { }
catch (Exception ex)
{
Log.Fatal(ex, "Unhandled exception on main thread."); Log.Fatal(ex, "Unhandled exception on main thread.");
} finally { }
AppDomain.CurrentDomain.UnhandledException -= OnUnhandledException; finally
{
AppDomain.CurrentDomain.UnhandledException -= this.OnUnhandledException;
Log.Information("Session has ended."); Log.Information("Session has ended.");
Log.CloseAndFlush(); Log.CloseAndFlush();
@ -54,7 +77,8 @@ namespace Dalamud {
} }
} }
private (Logger logger, LoggingLevelSwitch levelSwitch) NewLogger(string baseDirectory) { private (Logger logger, LoggingLevelSwitch levelSwitch) NewLogger(string baseDirectory)
{
#if DEBUG #if DEBUG
var logPath = Path.Combine(baseDirectory, "dalamud.log"); var logPath = Path.Combine(baseDirectory, "dalamud.log");
#else #else
@ -69,7 +93,7 @@ namespace Dalamud {
levelSwitch.MinimumLevel = LogEventLevel.Information; levelSwitch.MinimumLevel = LogEventLevel.Information;
#endif #endif
var newLogger = new LoggerConfiguration() var newLogger = new LoggerConfiguration()
.WriteTo.Async(a => a.File(logPath)) .WriteTo.Async(a => a.File(logPath))
.WriteTo.EventSink() .WriteTo.EventSink()
.MinimumLevel.ControlledBy(levelSwitch) .MinimumLevel.ControlledBy(levelSwitch)
@ -78,8 +102,10 @@ namespace Dalamud {
return (newLogger, levelSwitch); return (newLogger, levelSwitch);
} }
private void OnUnhandledException(object sender, UnhandledExceptionEventArgs arg) { private void OnUnhandledException(object sender, UnhandledExceptionEventArgs arg)
switch (arg.ExceptionObject) { {
switch (arg.ExceptionObject)
{
case Exception ex: case Exception ex:
Log.Fatal(ex, "Unhandled exception on AppDomain"); Log.Fatal(ex, "Unhandled exception on AppDomain");
break; break;