From 6c877723ce5ef56d78d583a2d4abd3081b0ec8b2 Mon Sep 17 00:00:00 2001 From: goaaats Date: Sat, 28 May 2022 17:31:52 +0200 Subject: [PATCH] feat: add TimingEvent, time IM scene init --- Dalamud/Dalamud.cs | 3 +- .../Interface/Internal/InterfaceManager.cs | 207 +++++++++--------- Dalamud/Utility/Timing/TimingEvent.cs | 35 +++ Dalamud/Utility/Timing/TimingHandle.cs | 34 +-- Dalamud/Utility/Timing/Timings.cs | 24 ++ 5 files changed, 169 insertions(+), 134 deletions(-) create mode 100644 Dalamud/Utility/Timing/TimingEvent.cs diff --git a/Dalamud/Dalamud.cs b/Dalamud/Dalamud.cs index 6f8498b17..a902fc2d0 100644 --- a/Dalamud/Dalamud.cs +++ b/Dalamud/Dalamud.cs @@ -120,7 +120,7 @@ namespace Dalamud Log.Information("[T1] Game thread continued!"); // Initialize FFXIVClientStructs function resolver - using (Timings.Start("FFXIVClientStructs Resolver Init")) + using (Timings.Start("CS Resolver Init")) { FFXIVClientStructs.Resolver.Initialize(); Log.Information("[T1] FFXIVClientStructs initialized!"); @@ -311,6 +311,7 @@ namespace Dalamud Troubleshooting.LogTroubleshooting(); Log.Information("Dalamud is ready."); + Timings.Event("Dalamud ready"); } catch (Exception ex) { diff --git a/Dalamud/Interface/Internal/InterfaceManager.cs b/Dalamud/Interface/Internal/InterfaceManager.cs index d105e5c27..a4a477f25 100644 --- a/Dalamud/Interface/Internal/InterfaceManager.cs +++ b/Dalamud/Interface/Internal/InterfaceManager.cs @@ -475,118 +475,121 @@ namespace Dalamud.Interface.Internal if (this.scene == null) { - try + using (Timings.Start("IM Scene Init")) { - this.scene = new RawDX11Scene(swapChain); - } - catch (DllNotFoundException ex) - { - Log.Error(ex, "Could not load ImGui dependencies."); - - var res = PInvoke.User32.MessageBox( - IntPtr.Zero, - "Dalamud plugins require the Microsoft Visual C++ Redistributable to be installed.\nPlease install the runtime from the official Microsoft website or disable Dalamud.\n\nDo you want to download the redistributable now?", - "Dalamud Error", - User32.MessageBoxOptions.MB_YESNO | User32.MessageBoxOptions.MB_TOPMOST | User32.MessageBoxOptions.MB_ICONERROR); - - if (res == User32.MessageBoxResult.IDYES) + try { - var psi = new ProcessStartInfo + this.scene = new RawDX11Scene(swapChain); + } + catch (DllNotFoundException ex) + { + Log.Error(ex, "Could not load ImGui dependencies."); + + var res = PInvoke.User32.MessageBox( + IntPtr.Zero, + "Dalamud plugins require the Microsoft Visual C++ Redistributable to be installed.\nPlease install the runtime from the official Microsoft website or disable Dalamud.\n\nDo you want to download the redistributable now?", + "Dalamud Error", + User32.MessageBoxOptions.MB_YESNO | User32.MessageBoxOptions.MB_TOPMOST | User32.MessageBoxOptions.MB_ICONERROR); + + if (res == User32.MessageBoxResult.IDYES) { - FileName = "https://aka.ms/vs/16/release/vc_redist.x64.exe", - UseShellExecute = true, - }; - Process.Start(psi); + var psi = new ProcessStartInfo + { + FileName = "https://aka.ms/vs/16/release/vc_redist.x64.exe", + UseShellExecute = true, + }; + Process.Start(psi); + } + + Environment.Exit(-1); } - Environment.Exit(-1); - } + var startInfo = Service.Get(); + var configuration = Service.Get(); - var startInfo = Service.Get(); - var configuration = Service.Get(); + var iniFileInfo = new FileInfo(Path.Combine(Path.GetDirectoryName(startInfo.ConfigurationPath), "dalamudUI.ini")); - var iniFileInfo = new FileInfo(Path.Combine(Path.GetDirectoryName(startInfo.ConfigurationPath), "dalamudUI.ini")); - - try - { - if (iniFileInfo.Length > 1200000) + try { - Log.Warning("dalamudUI.ini was over 1mb, deleting"); - iniFileInfo.CopyTo(Path.Combine(iniFileInfo.DirectoryName, $"dalamudUI-{DateTimeOffset.Now.ToUnixTimeSeconds()}.ini")); - iniFileInfo.Delete(); + if (iniFileInfo.Length > 1200000) + { + Log.Warning("dalamudUI.ini was over 1mb, deleting"); + iniFileInfo.CopyTo(Path.Combine(iniFileInfo.DirectoryName, $"dalamudUI-{DateTimeOffset.Now.ToUnixTimeSeconds()}.ini")); + iniFileInfo.Delete(); + } } + catch (Exception ex) + { + Log.Error(ex, "Could not delete dalamudUI.ini"); + } + + this.scene.ImGuiIniPath = iniFileInfo.FullName; + this.scene.OnBuildUI += this.Display; + this.scene.OnNewInputFrame += this.OnNewInputFrame; + + StyleModel.TransferOldModels(); + + if (configuration.SavedStyles == null || configuration.SavedStyles.All(x => x.Name != StyleModelV1.DalamudStandard.Name)) + { + configuration.SavedStyles = new List { StyleModelV1.DalamudStandard, StyleModelV1.DalamudClassic }; + configuration.ChosenStyle = StyleModelV1.DalamudStandard.Name; + } + else if (configuration.SavedStyles.Count == 1) + { + configuration.SavedStyles.Add(StyleModelV1.DalamudClassic); + } + else if (configuration.SavedStyles[1].Name != StyleModelV1.DalamudClassic.Name) + { + configuration.SavedStyles.Insert(1, StyleModelV1.DalamudClassic); + } + + configuration.SavedStyles[0] = StyleModelV1.DalamudStandard; + configuration.SavedStyles[1] = StyleModelV1.DalamudClassic; + + var style = configuration.SavedStyles.FirstOrDefault(x => x.Name == configuration.ChosenStyle); + if (style == null) + { + style = StyleModelV1.DalamudStandard; + configuration.ChosenStyle = style.Name; + configuration.Save(); + } + + style.Apply(); + + ImGui.GetIO().FontGlobalScale = configuration.GlobalUiScale; + + this.SetupFonts(); + + if (!configuration.IsDocking) + { + ImGui.GetIO().ConfigFlags &= ~ImGuiConfigFlags.DockingEnable; + } + else + { + ImGui.GetIO().ConfigFlags |= ImGuiConfigFlags.DockingEnable; + } + + // NOTE (Chiv) Toggle gamepad navigation via setting + if (!configuration.IsGamepadNavigationEnabled) + { + ImGui.GetIO().BackendFlags &= ~ImGuiBackendFlags.HasGamepad; + ImGui.GetIO().ConfigFlags &= ~ImGuiConfigFlags.NavEnableSetMousePos; + } + else + { + ImGui.GetIO().BackendFlags |= ImGuiBackendFlags.HasGamepad; + ImGui.GetIO().ConfigFlags |= ImGuiConfigFlags.NavEnableSetMousePos; + } + + // NOTE (Chiv) Explicitly deactivate on dalamud boot + ImGui.GetIO().ConfigFlags &= ~ImGuiConfigFlags.NavEnableGamepad; + + ImGuiHelpers.MainViewport = ImGui.GetMainViewport(); + + Log.Information("[IM] Scene & ImGui setup OK!"); + + Service.Get().Enable(); } - catch (Exception ex) - { - Log.Error(ex, "Could not delete dalamudUI.ini"); - } - - this.scene.ImGuiIniPath = iniFileInfo.FullName; - this.scene.OnBuildUI += this.Display; - this.scene.OnNewInputFrame += this.OnNewInputFrame; - - StyleModel.TransferOldModels(); - - if (configuration.SavedStyles == null || configuration.SavedStyles.All(x => x.Name != StyleModelV1.DalamudStandard.Name)) - { - configuration.SavedStyles = new List { StyleModelV1.DalamudStandard, StyleModelV1.DalamudClassic }; - configuration.ChosenStyle = StyleModelV1.DalamudStandard.Name; - } - else if (configuration.SavedStyles.Count == 1) - { - configuration.SavedStyles.Add(StyleModelV1.DalamudClassic); - } - else if (configuration.SavedStyles[1].Name != StyleModelV1.DalamudClassic.Name) - { - configuration.SavedStyles.Insert(1, StyleModelV1.DalamudClassic); - } - - configuration.SavedStyles[0] = StyleModelV1.DalamudStandard; - configuration.SavedStyles[1] = StyleModelV1.DalamudClassic; - - var style = configuration.SavedStyles.FirstOrDefault(x => x.Name == configuration.ChosenStyle); - if (style == null) - { - style = StyleModelV1.DalamudStandard; - configuration.ChosenStyle = style.Name; - configuration.Save(); - } - - style.Apply(); - - ImGui.GetIO().FontGlobalScale = configuration.GlobalUiScale; - - this.SetupFonts(); - - if (!configuration.IsDocking) - { - ImGui.GetIO().ConfigFlags &= ~ImGuiConfigFlags.DockingEnable; - } - else - { - ImGui.GetIO().ConfigFlags |= ImGuiConfigFlags.DockingEnable; - } - - // NOTE (Chiv) Toggle gamepad navigation via setting - if (!configuration.IsGamepadNavigationEnabled) - { - ImGui.GetIO().BackendFlags &= ~ImGuiBackendFlags.HasGamepad; - ImGui.GetIO().ConfigFlags &= ~ImGuiConfigFlags.NavEnableSetMousePos; - } - else - { - ImGui.GetIO().BackendFlags |= ImGuiBackendFlags.HasGamepad; - ImGui.GetIO().ConfigFlags |= ImGuiConfigFlags.NavEnableSetMousePos; - } - - // NOTE (Chiv) Explicitly deactivate on dalamud boot - ImGui.GetIO().ConfigFlags &= ~ImGuiConfigFlags.NavEnableGamepad; - - ImGuiHelpers.MainViewport = ImGui.GetMainViewport(); - - Log.Information("[IM] Scene & ImGui setup OK!"); - - Service.Get().Enable(); } if (this.address.IsReshade) diff --git a/Dalamud/Utility/Timing/TimingEvent.cs b/Dalamud/Utility/Timing/TimingEvent.cs new file mode 100644 index 000000000..8067c8a68 --- /dev/null +++ b/Dalamud/Utility/Timing/TimingEvent.cs @@ -0,0 +1,35 @@ +namespace Dalamud.Utility.Timing; + +public class TimingEvent +{ + internal TimingEvent(string name) + { + this.Name = name; + this.StartTime = Timings.Stopwatch.Elapsed.TotalMilliseconds; + } + + /// + /// Gets the time this timing started. + /// + public double StartTime { get; private set; } + + /// + /// Gets the name of the timing. + /// + public string Name { get; init; } + + /// + /// Gets the member that created this timing. + /// + public string? MemberName { get; init; } + + /// + /// Gets the file name that created this timing. + /// + public string? FileName { get; init; } + + /// + /// Gets the line number that created this timing. + /// + public int LineNumber { get; init; } +} diff --git a/Dalamud/Utility/Timing/TimingHandle.cs b/Dalamud/Utility/Timing/TimingHandle.cs index 7bdebc420..a0b55b30f 100644 --- a/Dalamud/Utility/Timing/TimingHandle.cs +++ b/Dalamud/Utility/Timing/TimingHandle.cs @@ -8,16 +8,14 @@ namespace Dalamud.Utility.Timing; /// Class used for tracking a time interval taken. /// [DebuggerDisplay("{Name} - {Duration}")] -public sealed class TimingHandle : IDisposable +public sealed class TimingHandle : TimingEvent, IDisposable { /// /// Initializes a new instance of the class. /// /// The name of this timing. - internal TimingHandle(string name) + internal TimingHandle(string name) : base(name) { - this.Name = name; - this.Parent = Timings.Current.Value; Timings.Current.Value = this; @@ -27,8 +25,7 @@ public sealed class TimingHandle : IDisposable { this.ChildCount++; } - - this.StartTime = Timings.Stopwatch.Elapsed.TotalMilliseconds; + this.EndTime = this.StartTime; this.IsMainThread = ThreadSafety.IsMainThread; @@ -41,11 +38,6 @@ public sealed class TimingHandle : IDisposable } } - /// - /// Gets the time this timing started. - /// - public double StartTime { get; private set; } - /// /// Gets the time this timing ended. /// @@ -81,26 +73,6 @@ public sealed class TimingHandle : IDisposable /// public uint Depth { get; private set; } - /// - /// Gets the name of the timing. - /// - public string Name { get; init; } - - /// - /// Gets the member that created this timing. - /// - public string? MemberName { get; init; } - - /// - /// Gets the file name that created this timing. - /// - public string? FileName { get; init; } - - /// - /// Gets the line number that created this timing. - /// - public int LineNumber { get; init; } - /// public void Dispose() { diff --git a/Dalamud/Utility/Timing/Timings.cs b/Dalamud/Utility/Timing/Timings.cs index 48d8d5c17..c644156f5 100644 --- a/Dalamud/Utility/Timing/Timings.cs +++ b/Dalamud/Utility/Timing/Timings.cs @@ -25,6 +25,8 @@ public static class Timings /// All active timings. /// internal static readonly List ActiveTimings = new(); + + internal static readonly List Events = new(); /// /// Current active timing entry. @@ -48,4 +50,26 @@ public static class Timings LineNumber = sourceLineNumber, }; } + + /// + /// Record a one-time event. + /// + /// The name of the timing. + /// Name of the calling member. + /// Name of the calling file. + /// Name of the calling line number. + /// Disposable that stops the timing once disposed. + public static void Event(string name, [CallerMemberName] string memberName = "", [CallerFilePath] string sourceFilePath = "", + [CallerLineNumber] int sourceLineNumber = 0) + { + lock (Events) + { + Events.Add(new TimingEvent(name) + { + MemberName = memberName, + FileName = sourceFilePath, + LineNumber = sourceLineNumber, + }); + } + } }