Skip to content

Commit

Permalink
feat: Timer class cleanup and make CurrentTime client function call a…
Browse files Browse the repository at this point in the history
… frame cached value.
  • Loading branch information
nt153133 committed Dec 26, 2023
1 parent e43b1b1 commit 1a93266
Showing 1 changed file with 40 additions and 59 deletions.
99 changes: 40 additions & 59 deletions Helpers/Timers.cs
Original file line number Diff line number Diff line change
@@ -1,86 +1,67 @@
using System;
using System.Windows.Media;
using ff14bot;
using ff14bot.Helpers;
using LlamaLibrary.Logging;
using LlamaLibrary.Memory.Attributes;
using LlamaLibrary.Structs;

namespace LlamaLibrary.Helpers
namespace LlamaLibrary.Helpers;

public static class Timers
{
public static class Timers
{
private static readonly LLogger Log = new("TimersHelper", Colors.Peru);
private static readonly LLogger Log = new("TimersHelper", Colors.Peru);

internal static class Offsets
{
[Offset("Search 48 83 EC ? 48 8B 0D ? ? ? ? E8 ? ? ? ? 48 85 C0 74 ? 48 8B C8 48 83 C4 ? E9 ? ? ? ? E8 ? ? ? ?")]
internal static IntPtr GetCurrentTime;
private static readonly FrameCachedValue<ulong> CurrentTimeCachedValue = new(() => Core.Memory.CallInjected64<ulong>(Offsets.GetCurrentTime, 0));

[Offset("Search E8 ? ? ? ? 48 85 C0 0F 84 ? ? ? ? 41 0F B7 14 5C TraceCall")]
internal static IntPtr GetCycleExd;
}
// ReSharper disable once MemberCanBePrivate.Global
internal static class Offsets
{
[Offset("Search 48 83 EC ? 48 8B 0D ? ? ? ? E8 ? ? ? ? 48 85 C0 74 ? 48 8B C8 48 83 C4 ? E9 ? ? ? ? E8 ? ? ? ?")]
internal static IntPtr GetCurrentTime;

private const int MaxRows = 6;
private static readonly string[] Description = { "", "Duty/Beast Tribe Dailies", "Weekly Reset", "Unknown", "GC/Rowena", "Unknown" };
[Offset("Search E8 ? ? ? ? 48 85 C0 0F 84 ? ? ? ? 41 0F B7 14 5C TraceCall")]
internal static IntPtr GetCycleExd;
}

private static readonly CycleTime[] _cycles = new CycleTime[MaxRows];
private const int MaxRows = 6;
private static readonly string[] Description = { "", "Duty/Beast Tribe Dailies", "Weekly Reset", "Unknown", "GC/Rowena", "Unknown" };

public static DateTimeOffset CurrentTime => DateTimeOffset.FromUnixTimeSeconds((long)CurrentTimeStamp).LocalDateTime;
private static readonly CycleTime[] Cycles = new CycleTime[MaxRows];

static Timers()
{
for (var i = 0; i < MaxRows; i++)
{
_cycles[i] = GetCycleRow(i);
}
}
public static DateTimeOffset CurrentTime => DateTimeOffset.FromUnixTimeSeconds((long)CurrentTimeStamp).LocalDateTime;

public static void PrintTimers()
static Timers()
{
for (var i = 0; i < MaxRows; i++)
{
Log.Information($"Current Time: ({CurrentTime.LocalDateTime})");
for (var i = 1; i < MaxRows; i++)
{
var time = DateTimeOffset.FromUnixTimeSeconds(GetNextCycle(i));

Log.Information($"{time.LocalDateTime} ({Description[i]})");
}
Cycles[i] = GetCycleRow(i);
}
}

public static ulong CurrentTimeStamp
public static void PrintTimers()
{
Log.Information($"Current Time: ({CurrentTime.LocalDateTime})");
for (var i = 1; i < MaxRows; i++)
{
get
{
ulong currentTime;
lock (Core.Memory.Executor.AssemblyLock)
{
currentTime = Core.Memory.CallInjected64<ulong>(Offsets.GetCurrentTime, 0);
}
var time = DateTimeOffset.FromUnixTimeSeconds(GetNextCycle(i));

return currentTime;
}
Log.Information($"{time.LocalDateTime} ({Description[i]})");
}
}

public static long GetNextCycle(int index)
{
var row = _cycles[index];
Log.Information($"Getting Cycle: ({index})");
return row.FirstCycle + (row.Cycle * ((uint)(ushort)(((uint)CurrentTimeStamp - row.FirstCycle) / row.Cycle) + 1));
}
public static ulong CurrentTimeStamp => CurrentTimeCachedValue.Value;

public static CycleTime GetCycleRow(int index)
{
IntPtr CyclePtr;
lock (Core.Memory.Executor.AssemblyLock)
{
CyclePtr = Core.Memory.CallInjected64<IntPtr>(Offsets.GetCycleExd, index);
}
public static long GetNextCycle(int index)
{
var row = Cycles[index];
return row.FirstCycle + (row.Cycle * ((uint)(ushort)(((uint)CurrentTimeStamp - row.FirstCycle) / row.Cycle) + 1));
}

if (CyclePtr != IntPtr.Zero)
{
return Core.Memory.Read<CycleTime>(CyclePtr);
}
public static CycleTime GetCycleRow(int index)
{
var cyclePtr = Core.Memory.CallInjected64<IntPtr>(Offsets.GetCycleExd, index);

return default;
}
return cyclePtr != IntPtr.Zero ? Core.Memory.Read<CycleTime>(cyclePtr) : default;
}
}

0 comments on commit 1a93266

Please sign in to comment.