-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModComponent.cs
136 lines (113 loc) · 3.86 KB
/
ModComponent.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using Il2CppSystem.Input;
using Il2CppSystem.Input.KeyConfig;
using System;
using UnhollowerRuntimeLib;
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 _lastGameTimeScale = 1f;
private float _lastTimeScale = 1f;
private bool _keySelectUp = true;
public int DefaultFrameRate = 60;
public int LastFrameRate = 60;
public bool FixGetFrameRate = false;
public Vector2 LastPadAxis = new();
public Vector2 LastPadAxisSanitized = new();
public bool ProcessPadNoInput = false;
public BattleState CurrentBattleState = new();
public ModComponent(IntPtr ptr) : base(ptr) { }
public static bool Inject()
{
ClassInjector.RegisterTypeInIl2Cpp<ModComponent>();
var name = typeof(ModComponent).FullName;
Plugin.Log.LogInfo($"Initializing game object {name}");
var modObject = new GameObject(name)
{
hideFlags = HideFlags.HideAndDontSave
};
GameObject.DontDestroyOnLoad(modObject);
Plugin.Log.LogInfo($"Adding {name} to game object...");
ModComponent component = modObject.AddComponent<ModComponent>();
if (component == null)
{
GameObject.Destroy(modObject);
Plugin.Log.LogError($"The game object is missing the required component: {name}");
return false;
}
return true;
}
public void Awake()
{
try
{
Instance = this;
_lastGameTimeScale = Time.timeScale;
_lastTimeScale = _lastGameTimeScale;
Plugin.Log.LogInfo($"[{nameof(ModComponent)}].{nameof(Awake)}: Processed successfully.");
}
catch (Exception e)
{
_isDisabled = true;
Plugin.Log.LogError($"[{nameof(ModComponent)}].{nameof(Awake)}(): {e}");
}
}
public void LateUpdate()
{
try
{
if (_isDisabled)
{
return;
}
UpdateTimeScale();
UpdateBattle();
}
catch (Exception e)
{
_isDisabled = true;
Plugin.Log.LogError($"[{nameof(ModComponent)}].{nameof(LateUpdate)}(): {e}");
}
}
private void UpdateTimeScale()
{
var timeScale = Time.timeScale;
if (timeScale == 0f)
{
// Game paused
return;
}
var gameTimeScale = timeScale != _lastTimeScale ? timeScale : _lastGameTimeScale;
var newTimeScale = gameTimeScale;
var keyPageUp = InputListener.Instance?.GetKey(Il2CppSystem.Input.Key.PageUp, KeyValue.InputDeviceType.GamePad) ?? false;
if (keyPageUp || Input.GetKey(KeyCode.T))
{
var isBattle = Last.Battle.BattlePlugManager.Instance()?.IsBattle() ?? false;
var speedHackFactor = isBattle ? Plugin.Config.BattleSpeedHackFactor.Value : Plugin.Config.OutBattleSpeedHackFactor.Value;
if (speedHackFactor > 0f)
{
newTimeScale *= speedHackFactor;
}
}
_lastGameTimeScale = gameTimeScale;
_lastTimeScale = newTimeScale;
Time.timeScale = newTimeScale;
}
private void UpdateBattle()
{
var keySelect = Gamepad.current?.selectButton.isPressed ?? false; // select button is not mapped in the game
keySelect = keySelect || Input.GetKey(KeyCode.P);
if (!keySelect)
{
_keySelectUp = true;
}
else if (_keySelectUp)
{
CurrentBattleState.PassTurn();
_keySelectUp = false;
}
}
}