From 55814b3ba4623490f69beb93f209963174c364a6 Mon Sep 17 00:00:00 2001 From: d3xMachina <16732772+d3xMachina@users.noreply.github.com> Date: Fri, 31 May 2024 23:43:37 +0200 Subject: [PATCH] added a speedhack for both battle and out of battle --- FFPR_Fix.csproj | 1 + ModComponent.cs | 88 ++++++++++++++++++++++++++++++++++++++ Patch.cs | 9 +++- Plugin.cs | 40 ++++++++++++++++- Properties/AssemblyInfo.cs | 4 +- 5 files changed, 137 insertions(+), 5 deletions(-) create mode 100644 ModComponent.cs diff --git a/FFPR_Fix.csproj b/FFPR_Fix.csproj index 5ae2bb1..73f4b0d 100644 --- a/FFPR_Fix.csproj +++ b/FFPR_Fix.csproj @@ -280,6 +280,7 @@ + diff --git a/ModComponent.cs b/ModComponent.cs new file mode 100644 index 0000000..292d643 --- /dev/null +++ b/ModComponent.cs @@ -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); + } + } + } +} diff --git a/Patch.cs b/Patch.cs index 83763c9..3f49dcd 100644 --- a/Patch.cs +++ b/Patch.cs @@ -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; } } } diff --git a/Plugin.cs b/Plugin.cs index 2e982cd..e9c9ee3 100644 --- a/Plugin.cs +++ b/Plugin.cs @@ -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; @@ -16,6 +18,8 @@ public class Plugin : BasePlugin public static ConfigEntry hideWorldMinimap; public static ConfigEntry skipSplashscreens; public static ConfigEntry playerMovespeed; + public static ConfigEntry outBattleSpeedHackFactor; + public static ConfigEntry battleSpeedHackFactor; public override void Load() { @@ -24,6 +28,7 @@ public override void Load() Log.LogInfo("Loading..."); InitConfig(); + InjectModComponent(); ApplyPatches(); Log.LogInfo("Patches applied!"); @@ -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", @@ -93,5 +112,24 @@ private void InitConfig() ); */ } + + private void InjectModComponent() + { + ClassInjector.RegisterTypeInIl2Cpp(); + 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(); + if (component == null) + { + GameObject.Destroy(modObject); + Log.LogError("The game object is missing the required component: " + name); + } + } } } diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs index 22ece6e..4c7fa9c 100644 --- a/Properties/AssemblyInfo.cs +++ b/Properties/AssemblyInfo.cs @@ -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")]