Skip to content

Commit

Permalink
fix pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
xorus committed Jul 3, 2024
1 parent 6cbddf4 commit 053c612
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 48 deletions.
2 changes: 1 addition & 1 deletion Plugin/EngageTimer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="DalamudPackager" Version="2.1.12"/>
<PackageReference Include="DalamudPackager" Version="2.1.13" />
<PackageReference Include="EmbedIO" Version="3.5.2"/>
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" />
<!-- required for Unosquare.Swan.Lite (implicit dependency of EmbedIO) -->
Expand Down
55 changes: 11 additions & 44 deletions Plugin/Game/CountdownHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,24 @@

/*
* Based on the work (for finding the pointer) of https://github.com/Haplo064/Europe
* 7.0 function changes taken from https://github.com/DelvUI/DelvUI/commit/492211c8f43b813d10b2220e6fe768ac508dcede and
* https://discord.com/channels/581875019861328007/653504487352303619/1257920418388639774. Thanks Tischel for that work!
*/
namespace EngageTimer.Game;

public sealed class CountdownHook : IDisposable
{
[Signature("48 89 5C 24 ?? 57 48 83 EC 40 8B 41", DetourName = nameof(CountdownTimerFunc))]
[Signature("40 53 48 83 EC 40 80 79 38 00", DetourName = nameof(CountdownTimerFunc))]
private readonly Hook<CountdownTimerDelegate>? _countdownTimerHook = null;

private readonly State _state;

private ulong _countDown;
private bool _countDownRunning;

/// <summary>
/// Ticks since the timer stalled
/// </summary>
private int _countDownStallTicks;

private float _lastCountDownValue;

private ulong _paramValue;

public CountdownHook()
{
_state = Plugin.State;
_countDown = 0;
_paramValue = 0;
Plugin.GameInterop.InitializeFromAttributes(this);
_countdownTimerHook?.Enable();
}
Expand All @@ -61,7 +54,7 @@ public void Dispose()

private IntPtr CountdownTimerFunc(ulong value)
{
_countDown = value;
_paramValue = value;
return _countdownTimerHook!.Original(value);
}

Expand All @@ -74,37 +67,11 @@ public void Update()

private void UpdateCountDown()
{
var newCountingDown = false;
if (_countDown == 0)
{
_state.CountingDown = newCountingDown;
return;
}

var countDownPointerValue = Marshal.PtrToStructure<float>((IntPtr)_countDown + 0x2c);

// is last value close enough (workaround for floating point approx)
if (Math.Abs(countDownPointerValue - _lastCountDownValue) < 0.001f)
{
_countDownStallTicks++;
}
else
{
_countDownStallTicks = 0;
_countDownRunning = true;
}

if (_countDownStallTicks > 50) _countDownRunning = false;

if (countDownPointerValue > 0 && _countDownRunning)
{
var newValue = Marshal.PtrToStructure<float>((IntPtr)_countDown + 0x2c);
_state.CountDownValue = newValue;
newCountingDown = true;
}

_state.CountingDown = newCountingDown;
_lastCountDownValue = countDownPointerValue;
if (_paramValue == 0) return;
var countDownActive = Marshal.PtrToStructure<byte>((IntPtr)_paramValue + 0x38) == 1;
var countDownPointerValue = Marshal.PtrToStructure<float>((IntPtr)_paramValue + 0x2c);
_state.CountingDown = countDownActive && countDownPointerValue > 0f;
_state.CountDownValue = countDownPointerValue;
}

[UnmanagedFunctionPointer(CallingConvention.ThisCall, CharSet = CharSet.Ansi)]
Expand Down
6 changes: 3 additions & 3 deletions Plugin/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"net8.0-windows7.0": {
"DalamudPackager": {
"type": "Direct",
"requested": "[2.1.12, )",
"resolved": "2.1.12",
"contentHash": "Sc0PVxvgg4NQjcI8n10/VfUQBAS4O+Fw2pZrAqBdRMbthYGeogzu5+xmIGCGmsEZ/ukMOBuAqiNiB5qA3MRalg=="
"requested": "[2.1.13, )",
"resolved": "2.1.13",
"contentHash": "rMN1omGe8536f4xLMvx9NwfvpAc9YFFfeXJ1t4P4PE6Gu8WCIoFliR1sh07hM+bfODmesk/dvMbji7vNI+B/pQ=="
},
"EmbedIO": {
"type": "Direct",
Expand Down

0 comments on commit 053c612

Please sign in to comment.