Removed unsafe handling of game object and owned object
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Dalamud.Plugin.Services;
|
||||
using Dalamud.Game.ClientState.Objects.Types;
|
||||
using Dalamud.Plugin.Services;
|
||||
using LightlessSync.API.Data;
|
||||
using LightlessSync.API.Data.Enum;
|
||||
using LightlessSync.Interop.Ipc;
|
||||
@@ -7,11 +8,15 @@ using LightlessSync.PlayerData.Pairs;
|
||||
using LightlessSync.Services;
|
||||
using LightlessSync.Services.ActorTracking;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Dalamud.Game.ClientState.Objects.Enums;
|
||||
using DalamudObjectKind = Dalamud.Game.ClientState.Objects.Enums.ObjectKind;
|
||||
using ObjectKind = LightlessSync.API.Data.Enum.ObjectKind;
|
||||
|
||||
namespace LightlessSync.PlayerData.Handlers;
|
||||
|
||||
internal sealed class OwnedObjectHandler
|
||||
{
|
||||
// Debug information for owned object resolution
|
||||
internal readonly record struct OwnedResolveDebug(
|
||||
DateTime? ResolvedAtUtc,
|
||||
nint Address,
|
||||
@@ -26,12 +31,15 @@ internal sealed class OwnedObjectHandler
|
||||
private OwnedResolveDebug _minionResolveDebug = OwnedResolveDebug.Empty;
|
||||
public OwnedResolveDebug MinionResolveDebug => _minionResolveDebug;
|
||||
|
||||
// Dependencies
|
||||
private readonly ILogger _logger;
|
||||
private readonly DalamudUtilService _dalamudUtil;
|
||||
private readonly GameObjectHandlerFactory _handlerFactory;
|
||||
private readonly IpcManager _ipc;
|
||||
private readonly ActorObjectService _actorObjectService;
|
||||
private IObjectTable _objectTable;
|
||||
private readonly IObjectTable _objectTable;
|
||||
|
||||
// Timeouts for fully loaded checks
|
||||
private const int _fullyLoadedTimeoutMsPlayer = 30000;
|
||||
private const int _fullyLoadedTimeoutMsOther = 5000;
|
||||
|
||||
@@ -51,6 +59,18 @@ internal sealed class OwnedObjectHandler
|
||||
_objectTable = objectTable;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Applies the specified changes to the owned object of the given kind.
|
||||
/// </summary>
|
||||
/// <param name="applicationId">Application ID of the Character Object</param>
|
||||
/// <param name="kind">Object Kind of the given object</param>
|
||||
/// <param name="changes">Changes of the object</param>
|
||||
/// <param name="data">Data of the object</param>
|
||||
/// <param name="playerHandler">Owner of the object</param>
|
||||
/// <param name="penumbraCollection">Collection if needed</param>
|
||||
/// <param name="customizeIds">Customizing identications for the object</param>
|
||||
/// <param name="token">Cancellation Token</param>
|
||||
/// <returns>Successfully applied or not</returns>
|
||||
public async Task<bool> ApplyAsync(
|
||||
Guid applicationId,
|
||||
ObjectKind kind,
|
||||
@@ -61,9 +81,11 @@ internal sealed class OwnedObjectHandler
|
||||
Dictionary<ObjectKind, Guid?> customizeIds,
|
||||
CancellationToken token)
|
||||
{
|
||||
// Validate player handler
|
||||
if (playerHandler.Address == nint.Zero)
|
||||
return false;
|
||||
|
||||
// Create handler for owned object
|
||||
var handler = await CreateHandlerAsync(kind, playerHandler, token).ConfigureAwait(false);
|
||||
if (handler is null || handler.Address == nint.Zero)
|
||||
return false;
|
||||
@@ -72,17 +94,20 @@ internal sealed class OwnedObjectHandler
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
// Determine if we have file replacements for this kind
|
||||
bool hasFileReplacements =
|
||||
kind != ObjectKind.Player
|
||||
&& data.FileReplacements.TryGetValue(kind, out var repls)
|
||||
&& repls is { Count: > 0 };
|
||||
|
||||
// Determine if we should assign a Penumbra collection
|
||||
bool shouldAssignCollection =
|
||||
kind != ObjectKind.Player
|
||||
&& hasFileReplacements
|
||||
&& penumbraCollection != Guid.Empty
|
||||
&& _ipc.Penumbra.APIAvailable;
|
||||
|
||||
|
||||
// Determine if only IPC-only changes are being made for player
|
||||
bool isPlayerIpcOnly =
|
||||
kind == ObjectKind.Player
|
||||
&& changes.Count > 0
|
||||
@@ -91,17 +116,21 @@ internal sealed class OwnedObjectHandler
|
||||
or PlayerChanges.PetNames
|
||||
or PlayerChanges.Heels);
|
||||
|
||||
// Wait for drawing to complete
|
||||
await handler.IsBeingDrawnRunOnFrameworkAsync().ConfigureAwait(false);
|
||||
|
||||
// Determine timeouts
|
||||
var drawTimeoutMs = handler.ObjectKind == ObjectKind.Player ? 30000 : 5000;
|
||||
var fullyLoadedTimeoutMs = handler.ObjectKind == ObjectKind.Player ? _fullyLoadedTimeoutMsPlayer : _fullyLoadedTimeoutMsOther;
|
||||
|
||||
// Wait for drawing to complete
|
||||
await _dalamudUtil
|
||||
.WaitWhileCharacterIsDrawing(_logger, handler, applicationId, drawTimeoutMs, token)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
if (handler.Address != nint.Zero)
|
||||
{
|
||||
// Wait for fully loaded
|
||||
var loaded = await _actorObjectService
|
||||
.WaitForFullyLoadedAsync(handler.Address, token, fullyLoadedTimeoutMs)
|
||||
.ConfigureAwait(false);
|
||||
@@ -115,8 +144,10 @@ internal sealed class OwnedObjectHandler
|
||||
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
// Assign Penumbra collection if needed
|
||||
if (shouldAssignCollection)
|
||||
{
|
||||
// Get object index
|
||||
var objIndex = await _dalamudUtil
|
||||
.RunOnFrameworkThread(() => handler.GetGameObject()?.ObjectIndex)
|
||||
.ConfigureAwait(false);
|
||||
@@ -127,6 +158,7 @@ internal sealed class OwnedObjectHandler
|
||||
return false;
|
||||
}
|
||||
|
||||
// Assign collection
|
||||
await _ipc.Penumbra
|
||||
.AssignTemporaryCollectionAsync(_logger, penumbraCollection, objIndex.Value)
|
||||
.ConfigureAwait(false);
|
||||
@@ -134,10 +166,12 @@ internal sealed class OwnedObjectHandler
|
||||
|
||||
var tasks = new List<Task>();
|
||||
|
||||
// Apply each change
|
||||
foreach (var change in changes.OrderBy(c => (int)c))
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
// Handle each change type
|
||||
switch (change)
|
||||
{
|
||||
case PlayerChanges.Customize:
|
||||
@@ -146,7 +180,7 @@ internal sealed class OwnedObjectHandler
|
||||
else if (customizeIds.TryGetValue(kind, out var existingId))
|
||||
tasks.Add(RevertCustomizeAsync(existingId, kind, customizeIds));
|
||||
break;
|
||||
|
||||
|
||||
case PlayerChanges.Glamourer:
|
||||
if (data.GlamourerData.TryGetValue(kind, out var glamourerData) && !string.IsNullOrEmpty(glamourerData))
|
||||
tasks.Add(_ipc.Glamourer.ApplyAllAsync(_logger, handler, glamourerData, applicationId, token));
|
||||
@@ -180,11 +214,13 @@ internal sealed class OwnedObjectHandler
|
||||
}
|
||||
}
|
||||
|
||||
// Await all tasks for change applications
|
||||
if (tasks.Count > 0)
|
||||
await Task.WhenAll(tasks).ConfigureAwait(false);
|
||||
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
// Determine if redraw is needed
|
||||
bool needsRedraw =
|
||||
_ipc.Penumbra.APIAvailable
|
||||
&& (
|
||||
@@ -196,9 +232,11 @@ internal sealed class OwnedObjectHandler
|
||||
|| changes.Contains(PlayerChanges.Customize)
|
||||
);
|
||||
|
||||
// Skip redraw for player if only IPC-only changes were made
|
||||
if (isPlayerIpcOnly)
|
||||
needsRedraw = false;
|
||||
|
||||
// Perform redraw if needed
|
||||
if (needsRedraw && _ipc.Penumbra.APIAvailable)
|
||||
{
|
||||
_logger.LogWarning(
|
||||
@@ -219,8 +257,18 @@ internal sealed class OwnedObjectHandler
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a GameObjectHandler for the owned object of the specified kind.
|
||||
/// </summary>
|
||||
/// <param name="kind">Object kind of the handler</param>
|
||||
/// <param name="playerHandler">Owner of the given object</param>
|
||||
/// <param name="token">Cancellation Token</param>
|
||||
/// <returns>Handler for the GameObject with the handler</returns>
|
||||
private async Task<GameObjectHandler?> CreateHandlerAsync(ObjectKind kind, GameObjectHandler playerHandler, CancellationToken token)
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
// Debug info setter
|
||||
void SetMinionDebug(string stage, string? failure, nint addr = default, ushort? objIndex = null)
|
||||
{
|
||||
if (kind != ObjectKind.MinionOrMount)
|
||||
@@ -234,9 +282,11 @@ internal sealed class OwnedObjectHandler
|
||||
failure);
|
||||
}
|
||||
|
||||
// Direct return for player
|
||||
if (kind == ObjectKind.Player)
|
||||
return playerHandler;
|
||||
|
||||
// First, try direct retrieval via Dalamud API
|
||||
var playerPtr = playerHandler.Address;
|
||||
if (playerPtr == nint.Zero)
|
||||
{
|
||||
@@ -244,6 +294,7 @@ internal sealed class OwnedObjectHandler
|
||||
return null;
|
||||
}
|
||||
|
||||
// Try direct retrieval
|
||||
nint ownedPtr = kind switch
|
||||
{
|
||||
ObjectKind.Companion => await _dalamudUtil.GetCompanionAsync(playerPtr).ConfigureAwait(false),
|
||||
@@ -252,20 +303,29 @@ internal sealed class OwnedObjectHandler
|
||||
_ => nint.Zero
|
||||
};
|
||||
|
||||
// If that fails, scan the object table for owned objects
|
||||
var stage = ownedPtr != nint.Zero ? "direct" : "direct_miss";
|
||||
|
||||
// Owner ID based scan
|
||||
if (ownedPtr == nint.Zero)
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
// Get owner entity ID
|
||||
var ownerEntityId = playerHandler.EntityId;
|
||||
if (ownerEntityId == 0 || ownerEntityId == uint.MaxValue)
|
||||
{
|
||||
ownerEntityId = await _dalamudUtil.RunOnFrameworkThread(() => ReadEntityIdUnsafe(playerPtr))
|
||||
// Read unsafe
|
||||
ownerEntityId = await _dalamudUtil
|
||||
.RunOnFrameworkThread(() => ReadEntityIdSafe(playerHandler))
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
if (ownerEntityId != 0 && ownerEntityId != uint.MaxValue)
|
||||
{
|
||||
ownedPtr = await _dalamudUtil.RunOnFrameworkThread(() => FindOwnedByOwnerIdUnsafe(kind, ownerEntityId))
|
||||
// Scan for owned object
|
||||
ownedPtr = await _dalamudUtil
|
||||
.RunOnFrameworkThread(() => FindOwnedByOwnerIdSafe(kind, ownerEntityId))
|
||||
.ConfigureAwait(false);
|
||||
|
||||
stage = ownedPtr != nint.Zero ? "owner_scan" : "owner_scan_miss";
|
||||
@@ -282,6 +342,9 @@ internal sealed class OwnedObjectHandler
|
||||
return null;
|
||||
}
|
||||
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
// Create handler
|
||||
var handler = await _handlerFactory.Create(kind, () => ownedPtr, isWatched: false).ConfigureAwait(false);
|
||||
if (handler is null || handler.Address == nint.Zero)
|
||||
{
|
||||
@@ -289,83 +352,102 @@ internal sealed class OwnedObjectHandler
|
||||
return null;
|
||||
}
|
||||
|
||||
// Get object index for debug
|
||||
ushort? objIndex = await _dalamudUtil.RunOnFrameworkThread(() => handler.GetGameObject()?.ObjectIndex)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
SetMinionDebug(stage, null, handler.Address, objIndex);
|
||||
SetMinionDebug(stage, failure: null, handler.Address, objIndex);
|
||||
return handler;
|
||||
}
|
||||
|
||||
private static unsafe uint ReadEntityIdUnsafe(nint playerPtr)
|
||||
{
|
||||
if (playerPtr == nint.Zero) return 0;
|
||||
var ch = (FFXIVClientStructs.FFXIV.Client.Game.Character.Character*)playerPtr;
|
||||
return ch != null ? ch->EntityId : 0;
|
||||
}
|
||||
/// <summary>
|
||||
/// Entity ID reader with safety checks.
|
||||
/// </summary>
|
||||
/// <param name="playerHandler">Handler of the Object</param>
|
||||
/// <returns>Entity Id</returns>
|
||||
private static uint ReadEntityIdSafe(GameObjectHandler playerHandler) => playerHandler.GetGameObject()?.EntityId ?? 0;
|
||||
|
||||
private unsafe nint FindOwnedByOwnerIdUnsafe(ObjectKind kind, uint ownerEntityId)
|
||||
/// <summary>
|
||||
/// Finds an owned object by scanning the object table for the specified owner entity ID.
|
||||
/// </summary>
|
||||
/// <param name="kind">Object kind to find of owned object</param>
|
||||
/// <param name="ownerEntityId">Owner Id</param>
|
||||
/// <returns>Object Id</returns>
|
||||
private nint FindOwnedByOwnerIdSafe(ObjectKind kind, uint ownerEntityId)
|
||||
{
|
||||
// Validate owner ID
|
||||
if (ownerEntityId == 0 || ownerEntityId == uint.MaxValue)
|
||||
return nint.Zero;
|
||||
|
||||
// Scan object table
|
||||
foreach (var obj in _objectTable)
|
||||
{
|
||||
// Validate object
|
||||
if (obj is null || obj.Address == nint.Zero)
|
||||
continue;
|
||||
|
||||
var addr = obj.Address;
|
||||
var go = (FFXIVClientStructs.FFXIV.Client.Game.Object.GameObject*)addr;
|
||||
if (go == null)
|
||||
// Check owner ID match
|
||||
if (obj.OwnerId != ownerEntityId)
|
||||
continue;
|
||||
|
||||
var ok = kind switch
|
||||
{
|
||||
ObjectKind.MinionOrMount =>
|
||||
obj.ObjectKind is Dalamud.Game.ClientState.Objects.Enums.ObjectKind.MountType
|
||||
or Dalamud.Game.ClientState.Objects.Enums.ObjectKind.Companion,
|
||||
|
||||
ObjectKind.Pet =>
|
||||
obj.ObjectKind == Dalamud.Game.ClientState.Objects.Enums.ObjectKind.BattleNpc
|
||||
&& go->BattleNpcSubKind == FFXIVClientStructs.FFXIV.Client.Game.Object.BattleNpcSubKind.Pet,
|
||||
|
||||
ObjectKind.Companion =>
|
||||
obj.ObjectKind == Dalamud.Game.ClientState.Objects.Enums.ObjectKind.BattleNpc
|
||||
&& go->BattleNpcSubKind == FFXIVClientStructs.FFXIV.Client.Game.Object.BattleNpcSubKind.Buddy,
|
||||
|
||||
_ => false
|
||||
};
|
||||
|
||||
if (!ok)
|
||||
// Check kind match
|
||||
if (!IsOwnedKindMatch(obj, kind))
|
||||
continue;
|
||||
|
||||
var resolvedOwner = ResolveOwnerIdUnsafe(go);
|
||||
if (resolvedOwner == ownerEntityId)
|
||||
return addr;
|
||||
return obj.Address;
|
||||
}
|
||||
|
||||
return nint.Zero;
|
||||
}
|
||||
|
||||
private static unsafe uint ResolveOwnerIdUnsafe(FFXIVClientStructs.FFXIV.Client.Game.Object.GameObject* gameObject)
|
||||
/// <summary>
|
||||
/// Determines if the given object matches the specified owned kind.
|
||||
/// </summary>
|
||||
/// <param name="obj">Game Object</param>
|
||||
/// <param name="kind">Object Kind</param>
|
||||
/// <returns></returns>
|
||||
private static bool IsOwnedKindMatch(IGameObject obj, ObjectKind kind) => kind switch
|
||||
{
|
||||
if (gameObject == null) return 0;
|
||||
// Match minion or mount
|
||||
ObjectKind.MinionOrMount =>
|
||||
obj.ObjectKind is DalamudObjectKind.MountType
|
||||
or DalamudObjectKind.Companion,
|
||||
|
||||
if (gameObject->OwnerId != 0)
|
||||
return gameObject->OwnerId;
|
||||
// Match pet
|
||||
ObjectKind.Pet =>
|
||||
obj.ObjectKind == DalamudObjectKind.BattleNpc
|
||||
&& obj is IBattleNpc bnPet
|
||||
&& bnPet.BattleNpcKind == BattleNpcSubKind.Pet,
|
||||
|
||||
var character = (FFXIVClientStructs.FFXIV.Client.Game.Character.Character*)gameObject;
|
||||
if (character == null) return 0;
|
||||
|
||||
if (character->CompanionOwnerId != 0)
|
||||
return character->CompanionOwnerId;
|
||||
|
||||
var parent = character->GetParentCharacter();
|
||||
return parent != null ? parent->EntityId : 0;
|
||||
}
|
||||
// Match companion
|
||||
ObjectKind.Companion =>
|
||||
obj.ObjectKind == DalamudObjectKind.BattleNpc
|
||||
&& obj is IBattleNpc bnBuddy
|
||||
&& bnBuddy.BattleNpcKind == BattleNpcSubKind.Chocobo,
|
||||
|
||||
_ => false
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Applies Customize Plus data to the specified object.
|
||||
/// </summary>
|
||||
/// <param name="address">Object Address</param>
|
||||
/// <param name="customizeData">Data of the Customize+ that has to be applied</param>
|
||||
/// <param name="kind">Object Kind</param>
|
||||
/// <param name="customizeIds">Customize+ Ids</param>
|
||||
/// <returns>Task</returns>
|
||||
private async Task ApplyCustomizeAsync(nint address, string customizeData, ObjectKind kind, Dictionary<ObjectKind, Guid?> customizeIds)
|
||||
{
|
||||
customizeIds[kind] = await _ipc.CustomizePlus.SetBodyScaleAsync(address, customizeData).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reverts Customize Plus changes for the specified object.
|
||||
/// </summary>
|
||||
/// <param name="customizeId">Customize+ Id</param>
|
||||
/// <param name="kind">Object Id</param>
|
||||
/// <param name="customizeIds">List of Customize+ ids</param>
|
||||
/// <returns></returns>
|
||||
private async Task RevertCustomizeAsync(Guid? customizeId, ObjectKind kind, Dictionary<ObjectKind, Guid?> customizeIds)
|
||||
{
|
||||
if (!customizeId.HasValue)
|
||||
|
||||
Reference in New Issue
Block a user