diff --git a/Dalamud/Dalamud.cs b/Dalamud/Dalamud.cs index d5757f768..6c5d1f1c3 100644 --- a/Dalamud/Dalamud.cs +++ b/Dalamud/Dalamud.cs @@ -440,7 +440,7 @@ namespace Dalamud { private void OnBgmSetCommand(string command, string arguments) { - Framework.Network.InjectBgmTest(int.Parse(arguments)); + Framework.Gui.SetBgm(ushort.Parse(arguments)); } private void OnItemLinkCommand(string command, string arguments) { @@ -474,26 +474,5 @@ namespace Dalamud { }); } - - public static byte[] StringToByteArray(String value) - { - byte[] bytes = new byte[value.Length * sizeof(char)]; - Buffer.BlockCopy(value.ToCharArray(), 0, bytes, 0, bytes.Length); - return bytes; - } - - public static String ByteArrayToString(byte[] value) - { - var chars = new char[value.Length / sizeof(char)]; - - var atValue = 0; - for (var i = 0; i < chars.Length; i++) { - chars[i] = BitConverter.ToChar(value, atValue); - - atValue += 2; - } - - return new string(chars); - } } } diff --git a/Dalamud/Game/Internal/Gui/GameGui.cs b/Dalamud/Game/Internal/Gui/GameGui.cs index a2b79aabe..d05559413 100644 --- a/Dalamud/Game/Internal/Gui/GameGui.cs +++ b/Dalamud/Game/Internal/Gui/GameGui.cs @@ -1,27 +1,55 @@ using System; +using System.Runtime.InteropServices; +using Dalamud.Game.Chat; +using Dalamud.Hooking; using Serilog; namespace Dalamud.Game.Internal.Gui { public sealed class GameGui : IDisposable { private GameGuiAddressResolver Address { get; } - public ChatGui Chat { get; private set; } - + public ChatGui Chat { get; private set; } + + [UnmanagedFunctionPointer(CallingConvention.ThisCall)] + private delegate IntPtr SetGlobalBgmDelegate(UInt16 bgmKey, byte a2, UInt32 a3, UInt32 a4, UInt32 a5, byte a6); + + private readonly Hook setGlobalBgmHook; + public GameGui(IntPtr baseAddress, SigScanner scanner, Dalamud dalamud) { Address = new GameGuiAddressResolver(baseAddress); Address.Setup(scanner); - + + Log.Verbose("===== G A M E G U I ====="); + Log.Verbose("GameGuiManager address {Address}", Address.BaseAddress); - + Log.Verbose("SetGlobalBgm address {Address}", Address.SetGlobalBgm); + Chat = new ChatGui(Address.ChatManager, scanner, dalamud); + + this.setGlobalBgmHook = + new Hook(Address.SetGlobalBgm, + new SetGlobalBgmDelegate(HandleSetGlobalBgmDetour), + this); } + private IntPtr HandleSetGlobalBgmDetour(UInt16 bgmKey, byte a2, UInt32 a3, UInt32 a4, UInt32 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; + } + + public void SetBgm(ushort bgmKey) => this.setGlobalBgmHook.Original(bgmKey, 0, 0, 0, 0, 0); + public void Enable() { Chat.Enable(); + this.setGlobalBgmHook.Enable(); } public void Dispose() { Chat.Dispose(); + this.setGlobalBgmHook.Dispose(); } } } diff --git a/Dalamud/Game/Internal/Gui/GameGuiAddressResolver.cs b/Dalamud/Game/Internal/Gui/GameGuiAddressResolver.cs index 7461f5d17..c0127e8d4 100644 --- a/Dalamud/Game/Internal/Gui/GameGuiAddressResolver.cs +++ b/Dalamud/Game/Internal/Gui/GameGuiAddressResolver.cs @@ -1,11 +1,14 @@ -using System; +using System; using System.Runtime.InteropServices; +using Serilog; namespace Dalamud.Game.Internal.Gui { public sealed class GameGuiAddressResolver : BaseAddressResolver { public IntPtr BaseAddress { get; private set; } public IntPtr ChatManager { get; private set; } + + public IntPtr SetGlobalBgm { get; private set; } public GameGuiAddressResolver(IntPtr baseAddress) { BaseAddress = baseAddress; @@ -21,7 +24,8 @@ namespace Dalamud.Game.Internal.Gui { } protected override void Setup64Bit(SigScanner sig) { - // Do nothing, still required. + //SetGlobalBgm = sig.ScanText("4C 8B 15 ?? ?? ?? ?? 4D 85 D2 74 58 41 83 7A ?? ?? 76 51 4D 8B 92 ?? ?? ?? ?? 0F B6 44 24 ?? 49 81 C2 ?? ?? ?? ?? 66 41 89 4A ?? 33 C9 41 88 52 30 41 89 4A 14 66 41 89 4A ?? 41 88 42 12 49 89 4A 38 41 89 4A 40 49 89 4A 48 41 38 4A 30 74 14 8B 44 24 28 41 89 42 40 45 89 42 38"); + SetGlobalBgm = sig.ScanText("4C 8B 15 ?? ?? ?? ?? 4D 85 D2 74 58"); } } } diff --git a/Dalamud/Game/Internal/Gui/IconReplacer.cs b/Dalamud/Game/Internal/Gui/IconReplacer.cs index 8beb46653..dcad7009c 100644 --- a/Dalamud/Game/Internal/Gui/IconReplacer.cs +++ b/Dalamud/Game/Internal/Gui/IconReplacer.cs @@ -63,6 +63,7 @@ namespace Dalamud.Game.Internal.Gui { public void Dispose() { this.iconHook.Dispose(); this.checkerHook.Dispose(); + Log.Verbose("IconReplacer unhooked"); } // I hate this function. This is the dumbest function to exist in the game. Just return 1. diff --git a/Dalamud/Game/Internal/Network/GameNetwork.cs b/Dalamud/Game/Internal/Network/GameNetwork.cs index a2093e1cd..90d565c6e 100644 --- a/Dalamud/Game/Internal/Network/GameNetwork.cs +++ b/Dalamud/Game/Internal/Network/GameNetwork.cs @@ -28,6 +28,7 @@ namespace Dalamud.Game.Internal.Network { Address = new GameNetworkAddressResolver(); Address.Setup(scanner); + Log.Verbose("===== G A M E N E T W O R K ====="); Log.Verbose("ProcessZonePacket address {ProcessZonePacket}", Address.ProcessZonePacket); this.processZonePacketHook = @@ -86,10 +87,6 @@ namespace Dalamud.Game.Internal.Network { InjectZoneProtoPacket(packetData); } - public void InjectBgmTest(int key) { - InjectActorControl(0xa1, key); - } - /// /// Process a chat queue. ///