refactor: new code style in DalamudInterface.cs

This commit is contained in:
goat 2021-03-31 15:14:16 +02:00
parent 83eebe3aac
commit 115fa54957

View file

@ -1,16 +1,11 @@
using System; using System;
using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Numerics; using System.Numerics;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using CheapLoc; using CheapLoc;
using Dalamud.Game.ClientState;
using Dalamud.Game.Command;
using Dalamud.Game.Internal;
using Dalamud.Plugin; using Dalamud.Plugin;
using ImGuiNET; using ImGuiNET;
using Serilog; using Serilog;
@ -18,19 +13,15 @@ using Serilog.Events;
namespace Dalamud.Interface namespace Dalamud.Interface
{ {
/// <summary>
/// Class handling Dalamud core interface.
/// </summary>
internal class DalamudInterface internal class DalamudInterface
{ {
private readonly Dalamud dalamud; private readonly Dalamud dalamud;
private ulong frameCount = 0; private ulong frameCount = 0;
public DalamudInterface(Dalamud dalamud) {
this.dalamud = dalamud;
if (dalamud.Configuration.LogOpenAtStartup) {
OpenLog();
}
}
private bool isImguiDrawDemoWindow = false; private bool isImguiDrawDemoWindow = false;
#if DEBUG #if DEBUG
@ -39,12 +30,6 @@ namespace Dalamud.Interface
private bool isImguiDrawDevMenu = false; private bool isImguiDrawDevMenu = false;
#endif #endif
public bool IsDevMenu
{
get => this.isImguiDrawDevMenu;
set => this.isImguiDrawDevMenu = value;
}
private bool isImguiDrawLogWindow = false; private bool isImguiDrawLogWindow = false;
private bool isImguiDrawDataWindow = false; private bool isImguiDrawDataWindow = false;
private bool isImguiDrawPluginWindow = false; private bool isImguiDrawPluginWindow = false;
@ -61,6 +46,29 @@ namespace Dalamud.Interface
private DalamudPluginStatWindow pluginStatWindow; private DalamudPluginStatWindow pluginStatWindow;
private DalamudChangelogWindow changelogWindow; private DalamudChangelogWindow changelogWindow;
/// <summary>
/// Initializes a new instance of the <see cref="DalamudInterface"/> class.
/// </summary>
/// <param name="dalamud">The Dalamud instance to register to.</param>
public DalamudInterface(Dalamud dalamud)
{
this.dalamud = dalamud;
if (dalamud.Configuration.LogOpenAtStartup)
this.OpenLog();
}
/// <summary>
/// Gets or sets a value indicating whether the Dalamud dev menu is drawing.
/// </summary>
public bool IsDevMenu
{
get => this.isImguiDrawDevMenu;
set => this.isImguiDrawDevMenu = value;
}
/// <summary>
/// Draw the Dalamud core interface via ImGui.
/// </summary>
public void Draw() public void Draw()
{ {
this.frameCount++; this.frameCount++;
@ -96,18 +104,21 @@ namespace Dalamud.Interface
{ {
if (ImGui.BeginMenu("Dalamud")) if (ImGui.BeginMenu("Dalamud"))
{ {
ImGui.MenuItem("Draw Dalamud dev menu", "", ref this.isImguiDrawDevMenu); ImGui.MenuItem("Draw Dalamud dev menu", string.Empty, ref this.isImguiDrawDevMenu);
ImGui.Separator(); ImGui.Separator();
if (ImGui.MenuItem("Open Log window")) if (ImGui.MenuItem("Open Log window"))
{ {
this.logWindow = new DalamudLogWindow(this.dalamud.CommandManager, this.dalamud.Configuration); this.logWindow = new DalamudLogWindow(this.dalamud.CommandManager, this.dalamud.Configuration);
this.isImguiDrawLogWindow = true; this.isImguiDrawLogWindow = true;
} }
if (ImGui.BeginMenu("Set log level...")) if (ImGui.BeginMenu("Set log level..."))
{ {
foreach (var logLevel in Enum.GetValues(typeof(LogEventLevel)).Cast<LogEventLevel>()) foreach (var logLevel in Enum.GetValues(typeof(LogEventLevel)).Cast<LogEventLevel>())
{ {
if (ImGui.MenuItem(logLevel + "##logLevelSwitch", "", this.dalamud.LogLevelSwitch.MinimumLevel == logLevel)) if (ImGui.MenuItem(logLevel + "##logLevelSwitch", string.Empty, this.dalamud.LogLevelSwitch.MinimumLevel == logLevel))
{ {
this.dalamud.LogLevelSwitch.MinimumLevel = logLevel; this.dalamud.LogLevelSwitch.MinimumLevel = logLevel;
} }
@ -115,52 +126,66 @@ namespace Dalamud.Interface
ImGui.EndMenu(); ImGui.EndMenu();
} }
if (ImGui.MenuItem("Enable AntiDebug", null, this.dalamud.AntiDebug.IsEnabled)) if (ImGui.MenuItem("Enable AntiDebug", null, this.dalamud.AntiDebug.IsEnabled))
{ {
this.dalamud.AntiDebug.Enable(); this.dalamud.AntiDebug.Enable();
} }
ImGui.Separator(); ImGui.Separator();
if (ImGui.MenuItem("Open Data window")) if (ImGui.MenuItem("Open Data window"))
{ {
this.dataWindow = new DalamudDataWindow(this.dalamud); this.dataWindow = new DalamudDataWindow(this.dalamud);
this.isImguiDrawDataWindow = true; this.isImguiDrawDataWindow = true;
} }
if (ImGui.MenuItem("Open Credits window")) if (ImGui.MenuItem("Open Credits window"))
{ {
OpenCredits(); this.OpenCredits();
}
if (ImGui.MenuItem("Open Settings window"))
{
OpenSettings();
}
if (ImGui.MenuItem("Open Changelog window"))
{
OpenChangelog();
} }
if (this.dalamud.Fools != null) { if (ImGui.MenuItem("Open Settings window"))
ImGui.MenuItem("Draw Tippy", "", ref this.dalamud.Fools.IsEnabled); {
this.OpenSettings();
} }
ImGui.MenuItem("Draw ImGui demo", "", ref this.isImguiDrawDemoWindow); if (ImGui.MenuItem("Open Changelog window"))
{
this.OpenChangelog();
}
if (this.dalamud.Fools != null)
{
ImGui.MenuItem("Draw Tippy", string.Empty, ref this.dalamud.Fools.IsEnabled);
}
ImGui.MenuItem("Draw ImGui demo", string.Empty, ref this.isImguiDrawDemoWindow);
ImGui.Separator(); ImGui.Separator();
if (ImGui.MenuItem("Unload Dalamud")) if (ImGui.MenuItem("Unload Dalamud"))
{ {
this.dalamud.Unload(); this.dalamud.Unload();
} }
if (ImGui.MenuItem("Kill game")) if (ImGui.MenuItem("Kill game"))
{ {
Process.GetCurrentProcess().Kill(); Process.GetCurrentProcess().Kill();
} }
if (ImGui.MenuItem("Cause AccessViolation")) if (ImGui.MenuItem("Cause AccessViolation"))
{ {
var a = Marshal.ReadByte(IntPtr.Zero); var a = Marshal.ReadByte(IntPtr.Zero);
} }
ImGui.Separator(); ImGui.Separator();
if (ImGui.MenuItem("Enable Dalamud testing", "", this.dalamud.Configuration.DoDalamudTest)) { if (ImGui.MenuItem("Enable Dalamud testing", string.Empty, this.dalamud.Configuration.DoDalamudTest))
{
this.dalamud.Configuration.DoDalamudTest = !this.dalamud.Configuration.DoDalamudTest; this.dalamud.Configuration.DoDalamudTest = !this.dalamud.Configuration.DoDalamudTest;
this.dalamud.Configuration.Save(); this.dalamud.Configuration.Save();
} }
ImGui.MenuItem(Util.AssemblyVersion, false); ImGui.MenuItem(Util.AssemblyVersion, false);
ImGui.MenuItem(this.dalamud.StartInfo.GameVersion, false); ImGui.MenuItem(this.dalamud.StartInfo.GameVersion, false);
@ -184,7 +209,9 @@ namespace Dalamud.Interface
this.pluginWindow = new PluginInstallerWindow(this.dalamud, this.dalamud.StartInfo.GameVersion); this.pluginWindow = new PluginInstallerWindow(this.dalamud, this.dalamud.StartInfo.GameVersion);
this.isImguiDrawPluginWindow = true; this.isImguiDrawPluginWindow = true;
} }
ImGui.Separator(); ImGui.Separator();
if (ImGui.MenuItem("Open Plugin Stats")) if (ImGui.MenuItem("Open Plugin Stats"))
{ {
if (!this.isImguiDrawPluginStatWindow) if (!this.isImguiDrawPluginStatWindow)
@ -193,6 +220,7 @@ namespace Dalamud.Interface
this.isImguiDrawPluginStatWindow = true; this.isImguiDrawPluginStatWindow = true;
} }
} }
if (ImGui.MenuItem("Print plugin info")) if (ImGui.MenuItem("Print plugin info"))
{ {
foreach (var plugin in this.dalamud.PluginManager.Plugins) foreach (var plugin in this.dalamud.PluginManager.Plugins)
@ -201,6 +229,7 @@ namespace Dalamud.Interface
Log.Information($"{plugin.Plugin.Name}"); Log.Information($"{plugin.Plugin.Name}");
} }
} }
if (ImGui.MenuItem("Reload plugins")) if (ImGui.MenuItem("Reload plugins"))
{ {
try try
@ -249,6 +278,7 @@ namespace Dalamud.Interface
ImGui.EndMenu(); ImGui.EndMenu();
} }
ImGui.EndMenu(); ImGui.EndMenu();
} }
@ -323,33 +353,48 @@ namespace Dalamud.Interface
this.isImguiDrawChangelogWindow = this.changelogWindow != null && this.changelogWindow.Draw(); this.isImguiDrawChangelogWindow = this.changelogWindow != null && this.changelogWindow.Draw();
} }
} }
/// <summary>
/// Open the Plugin Installer window.
/// </summary>
internal void OpenPluginInstaller() internal void OpenPluginInstaller()
{ {
if (this.pluginWindow == null) this.pluginWindow ??= new PluginInstallerWindow(this.dalamud, this.dalamud.StartInfo.GameVersion);
{
this.pluginWindow = new PluginInstallerWindow(this.dalamud, this.dalamud.StartInfo.GameVersion);
}
this.isImguiDrawPluginWindow ^= true; this.isImguiDrawPluginWindow ^= true;
} }
/// <summary>
/// Open the changelog window.
/// </summary>
internal void OpenChangelog() internal void OpenChangelog()
{ {
this.changelogWindow = new DalamudChangelogWindow(this.dalamud); this.changelogWindow = new DalamudChangelogWindow(this.dalamud);
this.isImguiDrawChangelogWindow = true; this.isImguiDrawChangelogWindow = true;
} }
/// <summary>
/// Open the settings window.
/// </summary>
internal void OpenSettings() internal void OpenSettings()
{ {
this.settingsWindow = new DalamudSettingsWindow(this.dalamud); this.settingsWindow = new DalamudSettingsWindow(this.dalamud);
this.isImguiDrawSettingsWindow ^= true; this.isImguiDrawSettingsWindow ^= true;
} }
public void OpenLog() { /// <summary>
/// Open the log window.
/// </summary>
internal void OpenLog()
{
this.logWindow = new DalamudLogWindow(this.dalamud.CommandManager, this.dalamud.Configuration); this.logWindow = new DalamudLogWindow(this.dalamud.CommandManager, this.dalamud.Configuration);
this.isImguiDrawLogWindow = true; this.isImguiDrawLogWindow = true;
} }
public void OpenCredits() { /// <summary>
/// Open the credits window.
/// </summary>
internal void OpenCredits()
{
var logoGraphic = var logoGraphic =
this.dalamud.InterfaceManager.LoadImage( this.dalamud.InterfaceManager.LoadImage(
Path.Combine(this.dalamud.AssetDirectory.FullName, "UIRes", "logo.png")); Path.Combine(this.dalamud.AssetDirectory.FullName, "UIRes", "logo.png"));