-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.cpp
More file actions
268 lines (210 loc) · 10.2 KB
/
init.cpp
File metadata and controls
268 lines (210 loc) · 10.2 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include <filesystem>
#include <thread>
#include <chrono>
#include <libconfig.h++>
#include "REFix.h"
#include "AnimCurveFlattener.h"
#include "AnimCurveStraightener.h"
#include "Undamper.h"
#include "Hooks.h"
namespace fs = std::filesystem;
namespace REFix {
const REF::API::Method* get_keys_count;
const REF::API::Method* get_keys;
const REF::API::Method* set_keys;
const REF::API::TypeDefinition* key_frame_type;
const REF::API::Field* value_field;
const REF::API::Field* in_normal_field;
const REF::API::Field* out_normal_field;
const REF::API::Field* camera_param_field;
const REF::API::Field* field_of_view_field;
const REF::API::TypeDefinition* damping_struct_single;
REF::API::ManagedObject* input_system;
const REF::API::Field* input_system_input_mode;
libconfig::Config config;
static bool check_or_set(const char* name) {
bool value = true;
if (!config.lookupValue(name, value)) {
config.getRoot().add(name, libconfig::Setting::TypeBoolean) = true;
}
return value;
}
static void disable_input_pitch_scaling(const reframework::API::ManagedObject* twirler_camera_settings)
{
// Get the speed curves.
const REF::API::ManagedObject* const normal_speed_curve = *twirler_camera_settings->get_field<REF::API::ManagedObject*>("NormalSpeedCurve");
PRINT_PTR(normal_speed_curve);
const REF::API::ManagedObject* const hold_speed_curve = *twirler_camera_settings->get_field<REF::API::ManagedObject*>("HoldSpeedCurve");
PRINT_PTR(hold_speed_curve);
// The camera's yaw speed is scaled based on your pitch by default. This sets the scale to 1.0 for all angles.
const AnimationCurveFlattener flattener(1.0f);
const AnimationCurveStraightener straightener;
flattener.mutate(normal_speed_curve);
straightener.mutate(normal_speed_curve);
LOG_INFO("Normal speed curve flattened.");
flattener.mutate(hold_speed_curve);
straightener.mutate(hold_speed_curve);
LOG_INFO("Hold speed curve flattened.");
}
static void remove_input_damping(
const reframework::API::ManagedObject* twirler_camera_settings,
const reframework::API::ManagedObject* camera_controller
)
{
// Straighten the input curve.
const REF::API::ManagedObject* const input_curve = *twirler_camera_settings->get_field<REF::API::ManagedObject*>("InputCurve");
PRINT_PTR(input_curve);
const AnimationCurveStraightener straightener;
straightener.mutate(input_curve);
LOG_INFO("Input curve straightened.");
// Remove input damping.
REF::API::ManagedObject* const twirl_speed_yaw = *camera_controller->get_field<REF::API::ManagedObject*>(
"<TwirlSpeedYaw>k__BackingField"
);
PRINT_PTR(twirl_speed_yaw);
REF::API::ManagedObject* const twirl_speed_pitch = *camera_controller->get_field<REF::API::ManagedObject*>(
"<TwirlSpeedPitch>k__BackingField"
);
PRINT_PTR(twirl_speed_pitch);
const Undamper undamper(damping_struct_single);
undamper.undamp(twirl_speed_yaw);
LOG_INFO("Twirl speed yaw undamped.");
undamper.undamp(twirl_speed_pitch);
LOG_INFO("Twirl speed pitch undamped.");
}
static bool init() {
const fs::path data_dir = fs::current_path() / "reframework" / "data";
const fs::path config_file = data_dir / "refix_config.txt";
std::error_code err_create_data_dir;
if (!fs::create_directory(data_dir, err_create_data_dir)) {
if (err_create_data_dir) {
LOG_WARN("Error creating data folder 0x%x: %s", err_create_data_dir.value(), err_create_data_dir.message().c_str());
} else {
// The data directory exists. Read the config.
try {
config.readFile(config_file.string());
}
catch (const libconfig::FileIOException&) {
LOG_WARN("Failed to open the config.");
}
catch (const libconfig::ParseException& p) {
LOG_WARN("Config parse error: %s", p.getError());
}
}
}
// Get pointers to important types, fields, and methods.
const REF::API::TypeDefinition* const animation_curve_type = TDB()->find_type("via.AnimationCurve");
get_keys_count = animation_curve_type->find_method("getKeysCount");
PRINT_PTR(get_keys_count);
get_keys = animation_curve_type->find_method("getKeys");
PRINT_PTR(get_keys);
set_keys = animation_curve_type->find_method("setKeys");
PRINT_PTR(set_keys);
key_frame_type = TDB()->find_type("via.KeyFrame");
PRINT_PTR(key_frame_type);
const REF::API::Method* const get_camera_controller = TDB()->find_method(PREFIX ".camera.CameraSystem", "getCameraController");
// Get the player camera controller.
const REF::API::ManagedObject* const camera_system = REF::API::get()->get_managed_singleton(PREFIX ".camera.CameraSystem");
if (camera_system == nullptr)
{
LOG_ERROR("Could not get Camera System singleton.");
return false;
}
PRINT_PTR(camera_system);
const REF::API::ManagedObject* const player_camera_controller = get_camera_controller->call<REF::API::ManagedObject*>(VMC(), camera_system, 0);
if (player_camera_controller == nullptr) {
LOG_ERROR("Call to getCameraController(0) failed.");
return false;
}
PRINT_PTR(player_camera_controller);
#ifdef RE3
// Get the player sight camera controller.
const REF::API::ManagedObject* const player_sight_camera_controller = get_camera_controller->call<REF::API::ManagedObject*>(VMC(), camera_system, 1);
if (player_sight_camera_controller == nullptr) {
LOG_ERROR("Call to getCameraController(1) failed.");
return false;
}
PRINT_PTR(player_sight_camera_controller);
#endif
// Get the camera settings.
const REF::API::ManagedObject* const twirler_camera_settings = *player_camera_controller->get_field<REF::API::ManagedObject*>("TwirlerCameraSettings");
PRINT_PTR(twirler_camera_settings);
#ifdef RE3
const REF::API::ManagedObject* const sight_twirler_camera_settings = *player_sight_camera_controller->get_field<REF::API::ManagedObject*>("TwirlerCameraSettings");
PRINT_PTR(sight_twirler_camera_settings);
#endif
in_normal_field = key_frame_type->find_field("inNormal");
PRINT_PTR(in_normal_field);
out_normal_field = key_frame_type->find_field("outNormal");
PRINT_PTR(out_normal_field);
if (check_or_set("disable-input-pitch-scaling")) {
value_field = key_frame_type->find_field("value");
PRINT_PTR(value_field);
disable_input_pitch_scaling(twirler_camera_settings);
#ifdef RE3
disable_input_pitch_scaling(sight_twirler_camera_settings);
#endif
}
if (check_or_set("remove-input-damping")) {
damping_struct_single = TDB()->find_type(PREFIX ".DampingStruct`1<System.Single>");
PRINT_PTR(damping_struct_single);
remove_input_damping(twirler_camera_settings, player_camera_controller);
#ifdef RE3
remove_input_damping(sight_twirler_camera_settings, player_sight_camera_controller);
#endif
}
const REF::API::TypeDefinition* const twirler_camera_controller_root_type = TDB()->find_type(PREFIX ".camera.TwirlerCameraControllerRoot");
if (check_or_set("scale-input-with-fov")) {
// Scale the input by the current FOV.
camera_param_field = TDB()->find_field(PREFIX ".camera.CameraControllerRoot", "<CameraParam>k__BackingField");
PRINT_PTR(camera_param_field);
field_of_view_field = TDB()->find_field(PREFIX ".CameraParam", "FieldOfView");
PRINT_PTR(field_of_view_field);
twirler_camera_controller_root_type->find_method("updatePitch")->add_hook(pre_update_pitch_yaw, post_hook_null, false);
twirler_camera_controller_root_type->find_method("updateYaw")->add_hook(pre_update_pitch_yaw, post_hook_null, false);
}
if (check_or_set("remove-input-scaling"))
{
// Don't scale input by control magnitude on mouse and keyboard.
input_system = REF::API::get()->get_managed_singleton(PREFIX ".InputSystem");
if (input_system == nullptr)
{
LOG_ERROR("Could not get input system singleton.");
return false;
}
PRINT_PTR(input_system);
input_system_input_mode = TDB()->find_field(PREFIX ".InputSystem", "<InputMode>k__BackingField");
PRINT_PTR(input_system_input_mode);
twirler_camera_controller_root_type->find_method("getControlMagnitude")->add_hook(pre_hook_null, post_get_control_magnitude, false);
}
if (check_or_set("remove-dynamic-difficulty"))
{
// Remove dynamic difficulty modulation.
TDB()->find_method(PREFIX ".GameRankSystem", "addRankPointDirect")->add_hook(pre_add_rank_point_direct, post_hook_null, false);
}
if (check_or_set("fix-zombie-anims"))
{
// Make zombies animate at 60fps.
TDB()->find_method(PREFIX ".MotionIntervalController", "setIntervalLevel")->add_hook(REFix::pre_set_interval_level, REFix::post_hook_null, false);
}
// Write the config in case any settings were changed.
try {
config.writeFile(config_file.string());
}
catch (const libconfig::FileIOException&) {
LOG_WARN("Could not write to the config.");
}
return true;
}
}
bool reframework_plugin_initialize(const REFrameworkPluginInitializeParam* param) {
REF::API::initialize(param);
LOG_INFO("Running setup...");
while (!REFix::init())
{
LOG_INFO("Setup failed. Retrying...");
std::this_thread::sleep_for(std::chrono::seconds(1));
}
LOG_INFO("Setup completed.");
return true;
}