From 5df28e4e22e52d133a029626f4bbda0983086769 Mon Sep 17 00:00:00 2001 From: goat <16760685+goaaats@users.noreply.github.com> Date: Tue, 19 Jan 2021 22:23:01 +0100 Subject: [PATCH] refactor: move dalamud init logic out of ctor, AntiDebug cleanup(WIP!!1!!!!) --- Dalamud/Dalamud.cs | 198 +++++++++++++------------- Dalamud/EntryPoint.cs | 6 +- Dalamud/Game/Internal/AntiDebug.cs | 6 +- Dalamud/Interface/DalamudInterface.cs | 2 +- 4 files changed, 106 insertions(+), 106 deletions(-) diff --git a/Dalamud/Dalamud.cs b/Dalamud/Dalamud.cs index 69ee6fc8b..20b7d790e 100644 --- a/Dalamud/Dalamud.cs +++ b/Dalamud/Dalamud.cs @@ -33,27 +33,27 @@ namespace Dalamud { /// /// Game framework subsystem /// - internal readonly Framework Framework; + internal Framework Framework { get; private set; } /// /// Anti-Debug detection prevention system /// - internal readonly AntiDebug AntiDebug; + internal AntiDebug AntiDebug { get; private set; } /// /// WinSock optimization subsystem /// - internal readonly WinSockHandlers WinSock2; + internal WinSockHandlers WinSock2 { get; private set; } /// /// ImGui Interface subsystem /// - internal readonly InterfaceManager InterfaceManager; + internal InterfaceManager InterfaceManager { get; private set; } /// /// ClientState subsystem /// - public readonly ClientState ClientState; + public ClientState ClientState { get; private set; } #endregion @@ -62,27 +62,27 @@ namespace Dalamud { /// /// Plugin Manager subsystem /// - internal readonly PluginManager PluginManager; + internal PluginManager PluginManager { get; private set; } /// /// Plugin Repository subsystem /// - internal readonly PluginRepository PluginRepository; + internal PluginRepository PluginRepository { get; private set; } /// /// Data provider subsystem /// - internal readonly DataManager Data; + internal DataManager Data { get; private set; } /// /// Command Manager subsystem /// - internal readonly CommandManager CommandManager; + internal CommandManager CommandManager { get; private set; } /// /// Localization subsystem facilitating localization for Dalamud and plugins /// - internal readonly Localization LocalizationManager; + internal Localization LocalizationManager { get; private set; } #endregion @@ -91,27 +91,27 @@ namespace Dalamud { /// /// SeStringManager subsystem facilitating string parsing /// - internal readonly SeStringManager SeStringManager; + internal SeStringManager SeStringManager { get; private set; } /// /// Copy-enabled SigScanner for target module /// - internal readonly SigScanner SigScanner; + internal SigScanner SigScanner { get; private set; } /// /// LoggingLevelSwitch for Dalamud and Plugin logs /// - internal readonly LoggingLevelSwitch LogLevelSwitch; + internal LoggingLevelSwitch LogLevelSwitch { get; private set; } /// /// StartInfo object passed from injector /// - internal readonly DalamudStartInfo StartInfo; + internal DalamudStartInfo StartInfo { get; private set; } /// /// Configuration object facilitating save and load of Dalamud configuration /// - internal readonly DalamudConfiguration Configuration; + internal DalamudConfiguration Configuration { get; private set; } #endregion @@ -120,22 +120,22 @@ namespace Dalamud { /// /// Dalamud base UI /// - internal readonly DalamudInterface DalamudUi; + internal DalamudInterface DalamudUi { get; private set; } /// /// Dalamud chat commands /// - internal readonly DalamudCommands DalamudCommands; + internal DalamudCommands DalamudCommands { get; private set; } /// /// Dalamud chat-based features /// - internal readonly ChatHandlers ChatHandlers; + internal ChatHandlers ChatHandlers { get; private set; } /// /// Dalamud network-based features /// - internal readonly NetworkHandlers NetworkHandlers; + internal NetworkHandlers NetworkHandlers { get; private set; } #endregion @@ -152,7 +152,7 @@ namespace Dalamud { /// /// Injected process module /// - internal readonly ProcessModule TargetModule; + internal ProcessModule TargetModule { get; private set; } /// /// Value indicating if Dalamud was successfully loaded @@ -165,92 +165,89 @@ namespace Dalamud { internal DirectoryInfo AssetDirectory => new DirectoryInfo(this.StartInfo.AssetDirectory); public Dalamud(DalamudStartInfo info, LoggingLevelSwitch loggingLevelSwitch, ManualResetEvent finishSignal) { - this.StartInfo = info; - this.LogLevelSwitch = loggingLevelSwitch; + StartInfo = info; + LogLevelSwitch = loggingLevelSwitch; this.baseDirectory = info.WorkingDirectory; this.unloadSignal = new ManualResetEvent(false); - this.finishUnloadSignal = finishSignal; + this.unloadSignal.Reset(); - this.Configuration = DalamudConfiguration.Load(info.ConfigurationPath); + this.finishUnloadSignal = finishSignal; + this.unloadSignal.Reset(); + } + + public void Start() { + Configuration = DalamudConfiguration.Load(StartInfo.ConfigurationPath); // Initialize the process information. - this.TargetModule = Process.GetCurrentProcess().MainModule; - this.SigScanner = new SigScanner(this.TargetModule, true); + TargetModule = Process.GetCurrentProcess().MainModule; + SigScanner = new SigScanner(TargetModule, true); - this.AntiDebug = new AntiDebug(this.SigScanner); + AntiDebug = new AntiDebug(SigScanner); +#if DEBUG + AntiDebug.Enable(); +#endif // Initialize game subsystem - this.Framework = new Framework(this.SigScanner, this); + Framework = new Framework(SigScanner, this); - this.WinSock2 = new WinSockHandlers(); + WinSock2 = new WinSockHandlers(); - this.NetworkHandlers = new NetworkHandlers(this, info.OptOutMbCollection); + NetworkHandlers = new NetworkHandlers(this, StartInfo.OptOutMbCollection); - this.ClientState = new ClientState(this, info, this.SigScanner); + ClientState = new ClientState(this, StartInfo, SigScanner); - this.LocalizationManager = new Localization(AssetDirectory.FullName); - if (!string.IsNullOrEmpty(this.Configuration.LanguageOverride)) - this.LocalizationManager.SetupWithLangCode(this.Configuration.LanguageOverride); + LocalizationManager = new Localization(AssetDirectory.FullName); + if (!string.IsNullOrEmpty(Configuration.LanguageOverride)) + LocalizationManager.SetupWithLangCode(Configuration.LanguageOverride); else - this.LocalizationManager.SetupWithUiCulture(); + LocalizationManager.SetupWithUiCulture(); - this.PluginRepository = new PluginRepository(this, this.StartInfo.PluginDirectory, this.StartInfo.GameVersion); + PluginRepository = new PluginRepository(this, StartInfo.PluginDirectory, StartInfo.GameVersion); - this.DalamudUi = new DalamudInterface(this); + DalamudUi = new DalamudInterface(this); var isInterfaceLoaded = false; - if (!bool.Parse(Environment.GetEnvironmentVariable("DALAMUD_NOT_HAVE_INTERFACE") ?? "false")) - { - try - { - this.InterfaceManager = new InterfaceManager(this, this.SigScanner); - this.InterfaceManager.OnDraw += this.DalamudUi.Draw; + if (!bool.Parse(Environment.GetEnvironmentVariable("DALAMUD_NOT_HAVE_INTERFACE") ?? "false")) { + try { + InterfaceManager = new InterfaceManager(this, SigScanner); + InterfaceManager.OnDraw += DalamudUi.Draw; - this.InterfaceManager.Enable(); + InterfaceManager.Enable(); isInterfaceLoaded = true; - } - catch (Exception e) - { + } catch (Exception e) { Log.Information(e, "Could not init interface."); } } - this.Data = new DataManager(this.StartInfo.Language); - try - { - this.Data.Initialize(AssetDirectory.FullName); - } - catch (Exception e) - { + Data = new DataManager(StartInfo.Language); + try { + Data.Initialize(AssetDirectory.FullName); + } catch (Exception e) { Log.Error(e, "Could not initialize DataManager."); Unload(); return; } - this.SeStringManager = new SeStringManager(this.Data); - -#if DEBUG - this.AntiDebug = new AntiDebug(this.SigScanner); -#endif + SeStringManager = new SeStringManager(Data); // Initialize managers. Basically handlers for the logic - this.CommandManager = new CommandManager(this, info.Language); - this.DalamudCommands = new DalamudCommands(this); - this.DalamudCommands.SetupCommands(); + CommandManager = new CommandManager(this, StartInfo.Language); + DalamudCommands = new DalamudCommands(this); + DalamudCommands.SetupCommands(); - this.ChatHandlers = new ChatHandlers(this); + ChatHandlers = new ChatHandlers(this); if (!bool.Parse(Environment.GetEnvironmentVariable("DALAMUD_NOT_HAVE_PLUGINS") ?? "false")) { try { - this.PluginRepository.CleanupPlugins(); + PluginRepository.CleanupPlugins(); - this.PluginManager = - new PluginManager(this, this.StartInfo.PluginDirectory, this.StartInfo.DefaultPluginDirectory); - this.PluginManager.LoadPlugins(); + PluginManager = + new PluginManager(this, StartInfo.PluginDirectory, StartInfo.DefaultPluginDirectory); + PluginManager.LoadPlugins(); } catch (Exception ex) { @@ -258,22 +255,18 @@ namespace Dalamud { } } - this.Framework.Enable(); - this.ClientState.Enable(); + Framework.Enable(); + ClientState.Enable(); IsReady = true; Troubleshooting.LogTroubleshooting(this, isInterfaceLoaded); - } - public void Start() { -#if DEBUG - AntiDebug.Enable(); - //ReplaceExceptionHandler(); -#endif + Log.Information("Dalamud is ready."); } public void Unload() { + Log.Information("Trigger unload"); this.unloadSignal.Set(); } @@ -286,35 +279,36 @@ namespace Dalamud { } public void Dispose() { - // this must be done before unloading plugins, or it can cause a race condition - // due to rendering happening on another thread, where a plugin might receive - // a render call after it has been disposed, which can crash if it attempts to - // use any resources that it freed in its own Dispose method - this.InterfaceManager?.Dispose(); + try { + // this must be done before unloading plugins, or it can cause a race condition + // due to rendering happening on another thread, where a plugin might receive + // a render call after it has been disposed, which can crash if it attempts to + // use any resources that it freed in its own Dispose method + InterfaceManager?.Dispose(); - try - { - this.PluginManager.UnloadPlugins(); + try { + PluginManager.UnloadPlugins(); + } catch (Exception ex) { + Log.Error(ex, "Plugin unload failed."); + } + + Framework.Dispose(); + ClientState.Dispose(); + + this.unloadSignal.Dispose(); + + WinSock2.Dispose(); + + SigScanner.Dispose(); + + Data.Dispose(); + + AntiDebug.Dispose(); + + Log.Debug("Dalamud::Dispose OK!"); + } catch (Exception ex) { + Log.Error(ex, "skjdgjjkodsfg"); } - catch (Exception ex) - { - Log.Error(ex, "Plugin unload failed."); - } - - this.Framework.Dispose(); - this.ClientState.Dispose(); - - this.unloadSignal.Dispose(); - - this.WinSock2.Dispose(); - - this.SigScanner.Dispose(); - - this.Data.Dispose(); - - this.AntiDebug?.Dispose(); - - Log.Debug("Dalamud::Dispose OK!"); } internal void ReplaceExceptionHandler() { diff --git a/Dalamud/EntryPoint.cs b/Dalamud/EntryPoint.cs index a64430644..028325c3d 100644 --- a/Dalamud/EntryPoint.cs +++ b/Dalamud/EntryPoint.cs @@ -23,7 +23,7 @@ namespace Dalamud { var finishSignal = new ManualResetEvent(false); try { - Log.Information(new string('-', 200)); + Log.Information(new string('-', 80)); Log.Information("Initializing a session.."); // This is due to GitHub not supporting TLS 1.0, so we enable all TLS versions globally @@ -34,12 +34,14 @@ namespace Dalamud { AppDomain.CurrentDomain.UnhandledException += OnUnhandledException; TaskScheduler.UnobservedTaskException += OnUnobservedTaskException; - using var dalamud = new Dalamud(info, levelSwitch, finishSignal); + var dalamud = new Dalamud(info, levelSwitch, finishSignal); Log.Information("Starting a session.."); // Run session dalamud.Start(); dalamud.WaitForUnload(); + + dalamud.Dispose(); } catch (Exception ex) { Log.Fatal(ex, "Unhandled exception on main thread."); } finally { diff --git a/Dalamud/Game/Internal/AntiDebug.cs b/Dalamud/Game/Internal/AntiDebug.cs index 8cd243c59..c6a423ba2 100644 --- a/Dalamud/Game/Internal/AntiDebug.cs +++ b/Dalamud/Game/Internal/AntiDebug.cs @@ -11,6 +11,8 @@ namespace Dalamud.Game.Internal { private IntPtr DebugCheckAddress { get; set; } + public bool IsEnabled { get; private set; } + public AntiDebug(SigScanner scanner) { DebugCheckAddress = scanner.ScanText("FF 15 ?? ?? ?? ?? 85 C0 74 11"); @@ -22,11 +24,13 @@ namespace Dalamud.Game.Internal public void Enable() { this.original = new byte[this.nop.Length]; - if (DebugCheckAddress != IntPtr.Zero) { + if (DebugCheckAddress != IntPtr.Zero && !IsEnabled) { Log.Information($"Overwriting Debug Check @ 0x{DebugCheckAddress.ToInt64():X}"); Marshal.Copy(DebugCheckAddress, this.original, 0, this.nop.Length); Marshal.Copy(this.nop, 0, DebugCheckAddress, this.nop.Length); } + + IsEnabled = true; } public void Dispose() { diff --git a/Dalamud/Interface/DalamudInterface.cs b/Dalamud/Interface/DalamudInterface.cs index 420b26820..96f6b2c14 100644 --- a/Dalamud/Interface/DalamudInterface.cs +++ b/Dalamud/Interface/DalamudInterface.cs @@ -108,7 +108,7 @@ namespace Dalamud.Interface ImGui.EndMenu(); } - if (this.dalamud.AntiDebug == null && ImGui.MenuItem("Enable AntiDebug")) + if (ImGui.MenuItem("Enable AntiDebug", null, this.dalamud.AntiDebug.IsEnabled)) { this.dalamud.AntiDebug.Enable(); }