Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 29 additions & 18 deletions Rebound/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@
using Dalamud.IoC;
using Dalamud.Plugin;
using Dalamud.Plugin.Services;
using Dalamud.Utility.Signatures;
using FFXIVClientStructs.FFXIV.Client.Graphics.Physics;
using static FFXIVClientStructs.FFXIV.Client.Graphics.Physics.BonePhysicsUpdater;
using FFXIVClientStructs.FFXIV.Client.System.Framework;

namespace Rebound;

public sealed class Plugin : IDalamudPlugin
{
private readonly Hook<BonePhysicsUpdater.Delegates.BoneSimulatorTask>? boneSimulatorUpdateHook = null!;

[Signature("F3 0F 10 89 ?? ?? ?? ?? 4C 8B C2", DetourName = nameof(TaskHook))]
private readonly Hook<TaskHookDelegate>? taskHook = null!;

#if DEBUG
/// If the fix should be enabled, it's only toggleable here for debug purposes
public bool EnableFix = true;
Expand All @@ -38,7 +43,7 @@ public Plugin()
startTick = DateTime.Now.Ticks;

boneSimulatorUpdateHook?.Enable();
Framework.Update += Update;
taskHook?.Enable();

#if DEBUG
DebugWindow = new DebugWindow(this);
Expand All @@ -51,9 +56,6 @@ public Plugin()
#endif
}

[PluginService]
internal static IFramework Framework { get; private set; } = null!;

[PluginService]
internal static IGameInteropProvider Hooking { get; private set; } = null!;

Expand All @@ -74,21 +76,33 @@ public Plugin()
public void Dispose()
{
boneSimulatorUpdateHook?.Dispose();
Framework.Update -= Update;
taskHook?.Dispose();

#if DEBUG
WindowSystem.RemoveAllWindows();
DebugWindow.Dispose();
#endif
}

// Called every frame.
public void Update(IFramework _)
/// Our new bone simulator update function.
/// Called for each BoneSimulator, so possibly multiple times every frame. Should be kept very simple for performance reasons.
private unsafe void BoneUpdate(BonePhysicsUpdater* self, UpdateBoneSimulatorJobData* data)
{
if (!ExecutePhysics)
{
return;
}

boneSimulatorUpdateHook!.Original(self, data);
}

private unsafe void TaskHook(Framework* self, void* a2)
{
#if DEBUG
if (!EnableFix)
{
ExecutePhysics = true;
taskHook!.Original(self, a2);
return;
}
#endif
Expand All @@ -97,6 +111,7 @@ public void Update(IFramework _)
if (PluginInterface.UiBuilder.CutsceneActive)
{
ExecutePhysics = true;
taskHook!.Original(self, a2);
return;
}

Expand All @@ -118,20 +133,16 @@ public void Update(IFramework _)
RanPhysics = true;
ExecutePhysics = true;
}
}

/// Our new bone simulator update function.
/// Called for each BoneSimulator, so possibly multiple times every frame. Should be kept very simple for performance reasons.
private unsafe void BoneUpdate(BonePhysicsUpdater* self, UpdateBoneSimulatorJobData* data)
{
if (!ExecutePhysics)
{
return;
}

boneSimulatorUpdateHook!.Original(self, data);
var oldDeltaTime = self->FrameDeltaTime;
self->FrameDeltaTime = 1.0f / (float)TargetFps; // Unsure if this helps at all, but just in case.
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be check if you're under 60 FPS

taskHook!.Original(self, a2);
self->FrameDeltaTime = oldDeltaTime;
}

/// The detour function signature
private unsafe delegate void TaskHookDelegate(Framework* self, void* a2);

#if DEBUG
private void DrawUi() => WindowSystem.Draw();
#endif
Expand Down
Loading