Skip to content

Commit

Permalink
Update CopyCharacterEvent.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ottermandias committed Oct 4, 2023
1 parent 53f1efa commit 0aeb407
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
15 changes: 10 additions & 5 deletions Penumbra/Interop/Services/GameEventManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public unsafe class GameEventManager : IDisposable
public GameEventManager(IGameInteropProvider interop)
{
interop.InitializeFromAttributes(this);

_copyCharacterHook =
interop.HookFromAddress<CopyCharacterDelegate>((nint)CharacterSetup.MemberFunctionPointers.CopyFromCharacter, CopyCharacterDetour);

_characterDtorHook.Enable();
_copyCharacterHook.Enable();
_resourceHandleDestructorHook.Enable();
Expand Down Expand Up @@ -78,19 +82,20 @@ private void CharacterDestructorDetour(Character* character)

#region Copy Character

private delegate ulong CopyCharacterDelegate(GameObject* target, GameObject* source, uint unk);
private delegate ulong CopyCharacterDelegate(CharacterSetup* target, GameObject* source, uint unk);

[Signature(Sigs.CopyCharacter, DetourName = nameof(CopyCharacterDetour))]
private readonly Hook<CopyCharacterDelegate> _copyCharacterHook = null!;
private readonly Hook<CopyCharacterDelegate> _copyCharacterHook;

private ulong CopyCharacterDetour(GameObject* target, GameObject* source, uint unk)
private ulong CopyCharacterDetour(CharacterSetup* target, GameObject* source, uint unk)
{
// TODO: update when CS updated.
var character = ((Character**)target)[1];
if (CopyCharacter != null)
foreach (var subscriber in CopyCharacter.GetInvocationList())
{
try
{
((CopyCharacterEvent)subscriber).Invoke((Character*)target, (Character*)source);
((CopyCharacterEvent)subscriber).Invoke(character, (Character*)source);
}
catch (Exception ex)
{
Expand Down
24 changes: 12 additions & 12 deletions Penumbra/Interop/Services/RedrawService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Dalamud.Game;
using Dalamud.Game.ClientState.Conditions;
using Dalamud.Game.ClientState.Objects;
using Dalamud.Game.ClientState.Objects.Enums;
Expand All @@ -20,8 +19,10 @@ public unsafe partial class RedrawService
public const int GPoseEndIdx = GPosePlayerIdx + GPoseSlots;

private readonly string?[] _gPoseNames = new string?[GPoseSlots];
private int _gPoseNameCounter = 0;
private bool _inGPose = false;
private int _gPoseNameCounter;

private bool InGPose
=> _clientState.IsGPosing;

// VFuncs that disable and enable draw, used only for GPose actors.
private static void DisableDraw(GameObject actor)
Expand All @@ -33,10 +34,7 @@ private static void EnableDraw(GameObject actor)
// Check whether we currently are in GPose.
// Also clear the name list.
private void SetGPose()
{
_inGPose = _objects[GPosePlayerIdx] != null;
_gPoseNameCounter = 0;
}
=> _gPoseNameCounter = 0;

private static bool IsGPoseActor(int idx)
=> idx is >= GPosePlayerIdx and < GPoseEndIdx;
Expand All @@ -50,7 +48,7 @@ private static bool IsGPoseActor(int idx)
private bool FindCorrectActor(int idx, out GameObject? obj)
{
obj = _objects[idx];
if (!_inGPose || obj == null || IsGPoseActor(idx))
if (!InGPose || obj == null || IsGPoseActor(idx))
return false;

var name = obj.Name.ToString();
Expand Down Expand Up @@ -100,23 +98,25 @@ private static bool BadRedrawIndices(GameObject? actor, out int tableIndex)

public sealed unsafe partial class RedrawService : IDisposable
{
private readonly IFramework _framework;
private readonly IFramework _framework;
private readonly IObjectTable _objects;
private readonly ITargetManager _targets;
private readonly ICondition _conditions;
private readonly ICondition _conditions;
private readonly IClientState _clientState;

private readonly List<int> _queue = new(100);
private readonly List<int> _afterGPoseQueue = new(GPoseSlots);
private int _target = -1;

public event GameObjectRedrawnDelegate? GameObjectRedrawn;

public RedrawService(IFramework framework, IObjectTable objects, ITargetManager targets, ICondition conditions)
public RedrawService(IFramework framework, IObjectTable objects, ITargetManager targets, ICondition conditions, IClientState clientState)
{
_framework = framework;
_objects = objects;
_targets = targets;
_conditions = conditions;
_clientState = clientState;
_framework.Update += OnUpdateEvent;
}

Expand Down Expand Up @@ -241,7 +241,7 @@ private void HandleRedraw()

private void HandleAfterGPose()
{
if (_afterGPoseQueue.Count == 0 || _inGPose)
if (_afterGPoseQueue.Count == 0 || InGPose)
return;

var numKept = 0;
Expand Down

0 comments on commit 0aeb407

Please sign in to comment.