Add PublicInstanceId with event to ClientState

This commit is contained in:
Haselnussbomber 2025-09-12 20:36:47 +02:00
parent 8c7b150820
commit 19263a2ff9
No known key found for this signature in database
GPG key ID: BB905BB49E7295D1
3 changed files with 51 additions and 6 deletions

View file

@ -17,6 +17,7 @@ using FFXIVClientStructs.FFXIV.Application.Network;
using FFXIVClientStructs.FFXIV.Client.Game;
using FFXIVClientStructs.FFXIV.Client.Game.Event;
using FFXIVClientStructs.FFXIV.Client.Game.UI;
using FFXIVClientStructs.FFXIV.Client.Network;
using FFXIVClientStructs.FFXIV.Client.UI;
using FFXIVClientStructs.FFXIV.Client.UI.Agent;
@ -38,7 +39,7 @@ internal sealed class ClientState : IInternalDisposableService, IClientState
private readonly ClientStateAddressResolver address;
private readonly Hook<EventFramework.Delegates.SetTerritoryTypeId> setupTerritoryTypeHook;
private readonly Hook<UIModule.Delegates.HandlePacket> uiModuleHandlePacketHook;
private Hook<LogoutCallbackInterface.Delegates.OnLogout> onLogoutHook;
private readonly Hook<SetCurrentInstanceDelegate> setCurrentInstanceHook;
[ServiceManager.ServiceDependency]
private readonly Framework framework = Service<Framework>.Get();
@ -46,6 +47,7 @@ internal sealed class ClientState : IInternalDisposableService, IClientState
[ServiceManager.ServiceDependency]
private readonly NetworkHandlers networkHandlers = Service<NetworkHandlers>.Get();
private Hook<LogoutCallbackInterface.Delegates.OnLogout> onLogoutHook;
private bool lastConditionNone = true;
[ServiceManager.ServiceConstructor]
@ -64,24 +66,31 @@ internal sealed class ClientState : IInternalDisposableService, IClientState
this.setupTerritoryTypeHook = Hook<EventFramework.Delegates.SetTerritoryTypeId>.FromAddress(setTerritoryTypeAddr, this.SetupTerritoryTypeDetour);
this.uiModuleHandlePacketHook = Hook<UIModule.Delegates.HandlePacket>.FromAddress((nint)UIModule.StaticVirtualTablePointer->HandlePacket, this.UIModuleHandlePacketDetour);
this.setCurrentInstanceHook = Hook<SetCurrentInstanceDelegate>.FromAddress(this.AddressResolver.SetCurrentInstance, this.SetCurrentInstanceDetour);
this.framework.Update += this.OnFrameworkUpdate;
this.networkHandlers.CfPop += this.NetworkHandlersOnCfPop;
this.setupTerritoryTypeHook.Enable();
this.uiModuleHandlePacketHook.Enable();
this.setCurrentInstanceHook.Enable();
this.framework.RunOnTick(this.Setup);
}
private unsafe delegate void ProcessPacketPlayerSetupDelegate(nint a1, nint packet);
private unsafe delegate void SetCurrentInstanceDelegate(NetworkModuleProxy* thisPtr, short instanceId);
/// <inheritdoc/>
public event Action<ushort>? TerritoryChanged;
/// <inheritdoc/>
public event Action<uint>? MapChanged;
/// <inheritdoc/>
public event Action<uint>? PublicInstanceChanged;
/// <inheritdoc/>
public event IClientState.ClassJobChangeDelegate? ClassJobChanged;
@ -112,6 +121,9 @@ internal sealed class ClientState : IInternalDisposableService, IClientState
/// <inheritdoc/>
public uint MapId { get; private set; }
/// <inheritdoc/>
public uint PublicInstanceId { get; private set; }
/// <inheritdoc/>
public IPlayerCharacter? LocalPlayer => Service<ObjectTable>.GetNullable()?[0] as IPlayerCharacter;
@ -171,6 +183,7 @@ internal sealed class ClientState : IInternalDisposableService, IClientState
this.setupTerritoryTypeHook.Dispose();
this.uiModuleHandlePacketHook.Dispose();
this.onLogoutHook.Dispose();
this.setCurrentInstanceHook.Dispose();
this.framework.Update -= this.OnFrameworkUpdate;
this.networkHandlers.CfPop -= this.NetworkHandlersOnCfPop;
@ -270,6 +283,18 @@ internal sealed class ClientState : IInternalDisposableService, IClientState
}
}
private unsafe void SetCurrentInstanceDetour(NetworkModuleProxy* thisPtr, short instanceId)
{
this.setCurrentInstanceHook.Original(thisPtr, instanceId);
if (this.PublicInstanceId == instanceId || instanceId < 0)
return;
Log.Debug("Instance changed: {0}", instanceId);
this.PublicInstanceId = (uint)instanceId;
this.PublicInstanceChanged?.InvokeSafely((uint)instanceId);
}
private void OnFrameworkUpdate(IFramework framework)
{
this.UpdateLogin();
@ -381,6 +406,7 @@ internal class ClientStatePluginScoped : IInternalDisposableService, IClientStat
{
this.clientStateService.TerritoryChanged += this.TerritoryChangedForward;
this.clientStateService.MapChanged += this.MapChangedForward;
this.clientStateService.PublicInstanceChanged += this.PublicInstanceChangedForward;
this.clientStateService.ClassJobChanged += this.ClassJobChangedForward;
this.clientStateService.LevelChanged += this.LevelChangedForward;
this.clientStateService.Login += this.LoginForward;
@ -396,6 +422,9 @@ internal class ClientStatePluginScoped : IInternalDisposableService, IClientStat
/// <inheritdoc/>
public event Action<uint>? MapChanged;
/// <inheritdoc/>
public event Action<uint>? PublicInstanceChanged;
/// <inheritdoc/>
public event IClientState.ClassJobChangeDelegate? ClassJobChanged;
@ -426,6 +455,9 @@ internal class ClientStatePluginScoped : IInternalDisposableService, IClientStat
/// <inheritdoc/>
public uint MapId => this.clientStateService.MapId;
/// <inheritdoc/>
public uint PublicInstanceId => this.clientStateService.PublicInstanceId;
/// <inheritdoc/>
public IPlayerCharacter? LocalPlayer => this.clientStateService.LocalPlayer;
@ -455,6 +487,7 @@ internal class ClientStatePluginScoped : IInternalDisposableService, IClientStat
{
this.clientStateService.TerritoryChanged -= this.TerritoryChangedForward;
this.clientStateService.MapChanged -= this.MapChangedForward;
this.clientStateService.PublicInstanceChanged -= this.PublicInstanceChangedForward;
this.clientStateService.ClassJobChanged -= this.ClassJobChangedForward;
this.clientStateService.LevelChanged -= this.LevelChangedForward;
this.clientStateService.Login -= this.LoginForward;
@ -475,6 +508,8 @@ internal class ClientStatePluginScoped : IInternalDisposableService, IClientStat
private void MapChangedForward(uint mapId) => this.MapChanged?.Invoke(mapId);
private void PublicInstanceChangedForward(uint instanceId) => this.PublicInstanceChanged?.Invoke(instanceId);
private void ClassJobChangedForward(uint classJobId) => this.ClassJobChanged?.Invoke(classJobId);
private void LevelChangedForward(uint classJobId, uint level) => this.LevelChanged?.Invoke(classJobId, level);

View file

@ -10,19 +10,19 @@ internal sealed class ClientStateAddressResolver : BaseAddressResolver
/// <summary>
/// Gets the address of the keyboard state.
/// </summary>
public IntPtr KeyboardState { get; private set; }
public nint KeyboardState { get; private set; }
/// <summary>
/// Gets the address of the keyboard state index array which translates the VK enumeration to the key state.
/// </summary>
public IntPtr KeyboardStateIndexArray { get; private set; }
public nint KeyboardStateIndexArray { get; private set; }
// Functions
/// <summary>
/// Gets the address of the method which sets up the player.
/// Gets the address of the method that sets the current public instance.
/// </summary>
public IntPtr ProcessPacketPlayerSetup { get; private set; }
public nint SetCurrentInstance { get; private set; }
/// <summary>
/// Scan for and setup any configured address pointers.
@ -30,7 +30,7 @@ internal sealed class ClientStateAddressResolver : BaseAddressResolver
/// <param name="sig">The signature scanner to facilitate setup.</param>
protected override void Setup64Bit(ISigScanner sig)
{
this.ProcessPacketPlayerSetup = sig.ScanText("40 53 48 83 EC 20 48 8D 0D ?? ?? ?? ?? 48 8B DA E8 ?? ?? ?? ?? 48 8B D3"); // not in cs struct
this.SetCurrentInstance = sig.ScanText("E8 ?? ?? ?? ?? 0F B6 55 ?? 48 8D 0D ?? ?? ?? ?? C0 EA"); // NetworkModuleProxy.SetCurrentInstance
// These resolve to fixed offsets only, without the base address added in, so GetStaticAddressFromSig() can't be used.
// lea rcx, ds:1DB9F74h[rax*4] KeyboardState

View file

@ -39,6 +39,11 @@ public interface IClientState
/// </summary>
public event Action<uint> MapChanged;
/// <summary>
/// Event that gets fired when the current zone Instance changes.
/// </summary>
public event Action<uint> PublicInstanceChanged;
/// <summary>
/// Event that fires when a characters ClassJob changed.
/// </summary>
@ -90,6 +95,11 @@ public interface IClientState
/// </summary>
public uint MapId { get; }
/// <summary>
/// Gets the instance number of the current zone, used when multiple copies of an area are active.
/// </summary>
public uint PublicInstanceId { get; }
/// <summary>
/// Gets the local player character, if one is present.
/// </summary>