Skip to content

Commit

Permalink
added a speedhack for both battle and out of battle
Browse files Browse the repository at this point in the history
  • Loading branch information
d3xMachina committed May 31, 2024
1 parent 194dc96 commit 55814b3
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 5 deletions.
1 change: 1 addition & 0 deletions FFPR_Fix.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="ModComponent.cs" />
<Compile Include="Patch.cs" />
<Compile Include="Plugin.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
88 changes: 88 additions & 0 deletions ModComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using Il2CppSystem.Diagnostics;
using System;
using UnityEngine;
using UnityEngine.InputSystem;

namespace FFPR_Fix
{
public sealed class ModComponent : MonoBehaviour
{
public static ModComponent Instance { get; private set; }
private bool _isDisabled;
private float defaultTimeScale;

public ModComponent(IntPtr ptr) : base(ptr) { }

public void Awake()
{
try
{
Instance = this;
defaultTimeScale = Time.timeScale;

string msg = $"[{nameof(ModComponent)}].{nameof(Awake)}: Processed successfully.";
Plugin.Log.LogInfo(msg);
}
catch (Exception e)
{
_isDisabled = true;
string msg = $"[{nameof(ModComponent)}].{nameof(Awake)}(): {e}";
Plugin.Log.LogError(msg);
}
}

private void UpdateTimeScale()
{
float newTimeScale = defaultTimeScale;
float newBattleSpeed = defaultTimeScale;

var triggerL = Gamepad.current.leftTrigger.ReadValue();
//Plugin.Log.LogInfo("Trigger value: " + triggerL);

if (triggerL >= 0.90f || Input.GetKeyDown(KeyCode.T))
{
//Plugin.Log.LogInfo("Key down!");
newTimeScale *= Plugin.outBattleSpeedHackFactor.Value;
newBattleSpeed *= Plugin.battleSpeedHackFactor.Value;
}

if (Plugin.outBattleSpeedHackFactor.Value != 1f && Time.timeScale != 0f)
{
Time.timeScale = newTimeScale;
}

if (Plugin.battleSpeedHackFactor.Value != 1f)
{
var battlePlugManager = Last.Battle.BattlePlugManager.instance;
if (battlePlugManager != null)
{
var battleOption = battlePlugManager.BattleOption;
if (battleOption != null && battleOption.GetGameSpeed() != 0f)
{
battleOption.SetGameSpeed(newBattleSpeed);
}
}
}
}

public void Update()
{
try
{
if (_isDisabled)
{
return;
}

UpdateTimeScale();

}
catch (Exception e)
{
_isDisabled = true;
string msg = $"[{nameof(ModComponent)}].{nameof(Update)}(): {e}";
Plugin.Log.LogError(msg);
}
}
}
}
9 changes: 7 additions & 2 deletions Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@ static void UncapFramerate()
// As this is always called on frame updates, we adjust the accumulation speed to match the new framerate
[HarmonyPatch(typeof(Il2CppSystem.Common.TimeFunction), nameof(Il2CppSystem.Common.TimeFunction.Function))]
[HarmonyPrefix]
static void TimeFunctionFix(Il2CppSystem.Action action, float waitTime, ref float acceleration, float waitTimeLowLimit, bool isAffectedTimeScale)
static void TimeFunctionFix(Il2CppSystem.Action action, ref float waitTime, ref float acceleration, ref float waitTimeLowLimit, bool isAffectedTimeScale)
{
var rate = DefaultFrameRate / (1.0f / Time.unscaledDeltaTime);
var rate = DefaultFrameRate / (1f / Time.unscaledDeltaTime);
acceleration *= rate;

// Fix fast input when the speedhack is enabled
float timeScale = Time.timeScale;
waitTime *= timeScale;
waitTimeLowLimit *= timeScale;
}
}
}
Expand Down
40 changes: 39 additions & 1 deletion Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
using BepInEx.IL2CPP;
using BepInEx.Logging;
using HarmonyLib;
using UnhollowerRuntimeLib;
using UnityEngine;

namespace FFPR_Fix
{
[BepInPlugin("d3xMachina.ffpr_fix", "FFPR Fix", "1.0.0.0")]
[BepInPlugin("d3xMachina.ffpr_fix", "FFPR Fix", "1.1.0")]
public class Plugin : BasePlugin
{
internal static new ManualLogSource Log;
Expand All @@ -16,6 +18,8 @@ public class Plugin : BasePlugin
public static ConfigEntry<bool> hideWorldMinimap;
public static ConfigEntry<bool> skipSplashscreens;
public static ConfigEntry<float> playerMovespeed;
public static ConfigEntry<float> outBattleSpeedHackFactor;
public static ConfigEntry<float> battleSpeedHackFactor;

public override void Load()
{
Expand All @@ -24,6 +28,7 @@ public override void Load()
Log.LogInfo("Loading...");

InitConfig();
InjectModComponent();
ApplyPatches();

Log.LogInfo("Patches applied!");
Expand Down Expand Up @@ -84,6 +89,20 @@ private void InitConfig()
"Skip the intro splashscreens."
);

outBattleSpeedHackFactor = Config.Bind(
"Hack",
"OutBattleSpeedHackFactor",
1f,
"Increase the game speed by X out of battle when T or L2 is pressed."
);

battleSpeedHackFactor = Config.Bind(
"Hack",
"BattleSpeedHackFactor",
1f,
"Increase the game speed by X in battle when T or L2 is pressed."
);

/*
playerMovespeed = Config.Bind(
"Player",
Expand All @@ -93,5 +112,24 @@ private void InitConfig()
);
*/
}

private void InjectModComponent()
{
ClassInjector.RegisterTypeInIl2Cpp<ModComponent>();
var name = typeof(ModComponent).FullName;

Log.LogInfo("Initializing game object " + name);
var modObject = new GameObject(name);
modObject.hideFlags = HideFlags.HideAndDontSave;
GameObject.DontDestroyOnLoad(modObject);

Log.LogInfo("Adding " + name + " to game object...");
ModComponent component = modObject.AddComponent<ModComponent>();
if (component == null)
{
GameObject.Destroy(modObject);
Log.LogError("The game object is missing the required component: " + name);
}
}
}
}
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]

0 comments on commit 55814b3

Please sign in to comment.