diff --git a/Dalamud/Game/Gui/GameGui.cs b/Dalamud/Game/Gui/GameGui.cs index aecbb7201..09ff17737 100644 --- a/Dalamud/Game/Gui/GameGui.cs +++ b/Dalamud/Game/Gui/GameGui.cs @@ -9,6 +9,7 @@ using Dalamud.Logging.Internal; using Dalamud.Plugin.Services; using Dalamud.Utility; +using FFXIVClientStructs.FFXIV.Client.Game; using FFXIVClientStructs.FFXIV.Client.Game.Control; using FFXIVClientStructs.FFXIV.Client.Graphics.Kernel; using FFXIVClientStructs.FFXIV.Client.System.String; @@ -16,6 +17,7 @@ using FFXIVClientStructs.FFXIV.Client.UI; using FFXIVClientStructs.FFXIV.Client.UI.Agent; using FFXIVClientStructs.FFXIV.Common.Component.BGCollision; using FFXIVClientStructs.FFXIV.Component.GUI; + using ImGuiNET; using Vector2 = System.Numerics.Vector2; @@ -34,7 +36,6 @@ internal sealed unsafe class GameGui : IInternalDisposableService, IGameGui private readonly GameGuiAddressResolver address; - private readonly Hook setGlobalBgmHook; private readonly Hook handleItemHoverHook; private readonly Hook handleItemOutHook; private readonly Hook handleActionHoverHook; @@ -50,12 +51,8 @@ internal sealed unsafe class GameGui : IInternalDisposableService, IGameGui this.address.Setup(sigScanner); Log.Verbose("===== G A M E G U I ====="); - Log.Verbose($"GameGuiManager address {Util.DescribeAddress(this.address.BaseAddress)}"); - Log.Verbose($"SetGlobalBgm address {Util.DescribeAddress(this.address.SetGlobalBgm)}"); Log.Verbose($"HandleImm address {Util.DescribeAddress(this.address.HandleImm)}"); - this.setGlobalBgmHook = Hook.FromAddress(this.address.SetGlobalBgm, this.HandleSetGlobalBgmDetour); - this.handleItemHoverHook = Hook.FromAddress((nint)AgentItemDetail.StaticVirtualTablePointer->Update, this.HandleItemHoverDetour); this.handleItemOutHook = Hook.FromAddress((nint)AgentItemDetail.StaticVirtualTablePointer->ReceiveEvent, this.HandleItemOutDetour); @@ -68,7 +65,6 @@ internal sealed unsafe class GameGui : IInternalDisposableService, IGameGui this.utf8StringFromSequenceHook = Hook.FromAddress(Utf8String.Addresses.Ctor_FromSequence.Value, this.Utf8StringFromSequenceDetour); - this.setGlobalBgmHook.Enable(); this.handleItemHoverHook.Enable(); this.handleItemOutHook.Enable(); this.handleImmHook.Enable(); @@ -80,9 +76,6 @@ internal sealed unsafe class GameGui : IInternalDisposableService, IGameGui // Hooked delegates - [UnmanagedFunctionPointer(CallingConvention.ThisCall)] - private delegate IntPtr SetGlobalBgmDelegate(ushort bgmKey, byte a2, uint a3, uint a4, uint a5, byte a6); - [UnmanagedFunctionPointer(CallingConvention.ThisCall)] private delegate char HandleImmDelegate(IntPtr framework, char a2, byte a3); @@ -254,7 +247,6 @@ internal sealed unsafe class GameGui : IInternalDisposableService, IGameGui /// void IInternalDisposableService.DisposeService() { - this.setGlobalBgmHook.Dispose(); this.handleItemHoverHook.Dispose(); this.handleItemOutHook.Dispose(); this.handleImmHook.Dispose(); @@ -265,25 +257,26 @@ internal sealed unsafe class GameGui : IInternalDisposableService, IGameGui } /// - /// Indicates if the game is on the title screen. + /// Indicates if the game is in the lobby scene (title screen, chara select, chara make, aesthetician etc.). /// - /// A value indicating whether or not the game is on the title screen. - internal bool IsOnTitleScreen() + /// A value indicating whether or not the game is in the lobby scene. + internal bool IsInLobby() { - var charaSelect = this.GetAddonByName("CharaSelect"); - var charaMake = this.GetAddonByName("CharaMake"); - var titleDcWorldMap = this.GetAddonByName("TitleDCWorldMap"); - if (charaMake != nint.Zero || charaSelect != nint.Zero || titleDcWorldMap != nint.Zero) - return false; - - return !Service.Get().IsLoggedIn; + return RaptureAtkModule.Instance()->CurrentUISceneString == "LobbyMain"; } /// - /// Set the current background music. + /// Sets the current background music. /// - /// The background music key. - internal void SetBgm(ushort bgmKey) => this.setGlobalBgmHook.Original(bgmKey, 0, 0, 0, 0, 0); + /// The BGM row id. + /// The BGM scene index. Defaults to MiniGame scene to avoid conflicts. + internal void SetBgm(ushort bgmId, uint sceneId = 2) => BGMSystem.SetBGM(bgmId, sceneId); + + /// + /// Resets the current background music. + /// + /// The BGM scene index. + internal void ResetBgm(uint sceneId = 2) => BGMSystem.Instance()->ResetBGM(sceneId); /// /// Reset the stored "UI hide" state. @@ -293,15 +286,6 @@ internal sealed unsafe class GameGui : IInternalDisposableService, IGameGui this.GameUiHidden = false; } - private IntPtr HandleSetGlobalBgmDetour(ushort bgmKey, byte a2, uint a3, uint a4, uint a5, byte a6) - { - var retVal = this.setGlobalBgmHook.Original(bgmKey, a2, a3, a4, a5, a6); - - Log.Verbose("SetGlobalBgm: {0} {1} {2} {3} {4} {5} -> {6}", bgmKey, a2, a3, a4, a5, a6, retVal); - - return retVal; - } - private void HandleItemHoverDetour(AgentItemDetail* thisPtr, uint frameCount) { this.handleItemHoverHook.Original(thisPtr, frameCount); diff --git a/Dalamud/Game/Gui/GameGuiAddressResolver.cs b/Dalamud/Game/Gui/GameGuiAddressResolver.cs index bdd579c7e..92b89c5a9 100644 --- a/Dalamud/Game/Gui/GameGuiAddressResolver.cs +++ b/Dalamud/Game/Gui/GameGuiAddressResolver.cs @@ -5,16 +5,6 @@ namespace Dalamud.Game.Gui; /// internal sealed class GameGuiAddressResolver : BaseAddressResolver { - /// - /// Gets the base address of the native GuiManager class. - /// - public IntPtr BaseAddress { get; private set; } - - /// - /// Gets the address of the native SetGlobalBgm method. - /// - public IntPtr SetGlobalBgm { get; private set; } - /// /// Gets the address of the native HandleImm method. /// @@ -23,7 +13,6 @@ internal sealed class GameGuiAddressResolver : BaseAddressResolver /// protected override void Setup64Bit(ISigScanner sig) { - this.SetGlobalBgm = sig.ScanText("E8 ?? ?? ?? ?? 8B 2F"); // unnamed in CS this.HandleImm = sig.ScanText("E8 ?? ?? ?? ?? 84 C0 75 10 48 83 FF 09"); // unnamed in CS } } diff --git a/Dalamud/Interface/Internal/DalamudCommands.cs b/Dalamud/Interface/Internal/DalamudCommands.cs index fb64ad979..00997c1d5 100644 --- a/Dalamud/Interface/Internal/DalamudCommands.cs +++ b/Dalamud/Interface/Internal/DalamudCommands.cs @@ -266,7 +266,7 @@ internal class DalamudCommands : IServiceType else { // Revert to the original BGM by specifying an invalid one - gameGui.SetBgm(9999); + gameGui.ResetBgm(); } } diff --git a/Dalamud/Interface/Internal/Windows/Settings/Tabs/SettingsTabAbout.cs b/Dalamud/Interface/Internal/Windows/Settings/Tabs/SettingsTabAbout.cs index 6a231eeaf..428be05c2 100644 --- a/Dalamud/Interface/Internal/Windows/Settings/Tabs/SettingsTabAbout.cs +++ b/Dalamud/Interface/Internal/Windows/Settings/Tabs/SettingsTabAbout.cs @@ -1,4 +1,4 @@ -using System.Diagnostics; +using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Numerics; @@ -194,6 +194,7 @@ Contribute at: https://github.com/goatcorp/Dalamud private readonly IFontAtlas privateAtlas; private string creditsText; + private bool isBgmSet; private bool resetNow = false; private IDalamudTextureWrap? logoTexture; @@ -222,10 +223,13 @@ Contribute at: https://github.com/goatcorp/Dalamud this.creditsText = string.Format(CreditsTextTempl, typeof(Dalamud).Assembly.GetName().Version, pluginCredits, Util.GetGitHashClientStructs()); - var gg = Service.Get(); - if (!gg.IsOnTitleScreen() && UIState.Instance() != null) + var gameGui = Service.Get(); + var playerState = PlayerState.Instance(); + + if (!gameGui.IsInLobby() && playerState != null) { - gg.SetBgm((ushort)(UIState.Instance()->PlayerState.MaxExpansion > 3 ? 833 : 132)); + gameGui.SetBgm((ushort)(playerState->MaxExpansion > 3 ? 833 : 132)); + this.isBgmSet = true; } this.creditsThrottler.Restart(); @@ -242,9 +246,15 @@ Contribute at: https://github.com/goatcorp/Dalamud { this.creditsThrottler.Reset(); - var gg = Service.Get(); - if (!gg.IsOnTitleScreen()) - gg.SetBgm(9999); + if (this.isBgmSet) + { + var gameGui = Service.Get(); + + if (!gameGui.IsInLobby()) + gameGui.ResetBgm(); + + this.isBgmSet = false; + } Service.Get().SetCreditsDarkeningAnimation(false); }