-
Notifications
You must be signed in to change notification settings - Fork 0
/
Starsky&HutchFOVFix.cpp
297 lines (244 loc) · 10 KB
/
Starsky&HutchFOVFix.cpp
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <cstdint> // For uint32_t variable type
#include <limits>
#include <windows.h>
#include <vector>
#include <cstring>
#include <conio.h> // For getch() function [get character]
#include <string>
#include <algorithm>
using namespace std;
// Constants
const double kPi = 3.14159265358979323846;
const streampos kCameraFOVOffset = 0x0020FBA1;
// Variables
int choice1, choice2, tempChoice;
uint32_t newWidth, newHeight;
bool fileNotFound, validKeyPressed;
double currentCameraFOV, newCameraFOV, newCameraFOVValue, oldWidth = 4.0, oldHeight = 3.0, oldCameraHorizontalFOV = 85.0, oldAspectRatio = oldWidth / oldHeight, newAspectRatio;
string input, descriptor;
fstream file;
char ch;
// Function to convert degrees to radians
double degToRad(double degrees)
{
return degrees * (kPi / 180.0);
}
// Function to convert radians to degrees
double radToDeg(double radians)
{
return radians * (180.0 / kPi);
}
// Function to handle user input in choices
void HandleChoiceInput(int &choice)
{
tempChoice = -1; // Temporary variable to store the input
validKeyPressed = false; // Flag to track if a valid key was pressed
while (true)
{
ch = _getch(); // Waits for user to press a key
// Checks if the key is '1' or '2'
if ((ch == '1' || ch == '2') && !validKeyPressed)
{
tempChoice = ch - '0'; // Converts char to int and stores the input temporarily
cout << ch; // Echoes the valid input
validKeyPressed = true; // Sets the flag as a valid key has been pressed
}
else if (ch == '\b' || ch == 127) // Handles backspace or delete keys
{
if (tempChoice != -1) // Checks if there is something to delete
{
tempChoice = -1; // Resets the temporary choice
cout << "\b \b"; // Erases the last character from the console
validKeyPressed = false; // Resets the flag as the input has been deleted
}
}
// If 'Enter' is pressed and a valid key has been pressed prior
else if (ch == '\r' && validKeyPressed)
{
choice = tempChoice; // Assigns the temporary input to the choice variable
cout << endl; // Moves to a new line
break; // Exits the loop since we have a confirmed input
}
}
}
void HandleFOVInput(double &newCustomFOVInDegrees)
{
do
{
// Reads the input as a string
cin >> input;
// Replaces all commas with dots
replace(input.begin(), input.end(), ',', '.');
// Parses the string to a double
newCustomFOVInDegrees = stod(input);
if (cin.fail())
{
cin.clear(); // Clears error flags
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Ignores invalid input
cout << "Invalid input. Please enter a numeric value." << endl;
}
else if (newCustomFOVInDegrees <= 0 || newCustomFOVInDegrees >= 180)
{
cout << "Please enter a valid number for the FOV (greater than 0 and less than 180)." << endl;
}
} while (newCustomFOVInDegrees <= 0 || newCustomFOVInDegrees >= 180);
}
void HandleResolutionInput(uint32_t &newCustomResolutionValue)
{
do
{
cin >> newCustomResolutionValue;
cin.clear(); // Clears error flags
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Ignores invalid input
if (cin.fail())
{
cin.clear(); // Clears error flags
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Ignores invalid input
cout << "Invalid input. Please enter a numeric value." << endl;
}
else if (newCustomResolutionValue <= 0 || newCustomResolutionValue >= 65535)
{
cout << "Please enter a valid number." << endl;
}
} while (newCustomResolutionValue <= 0 || newCustomResolutionValue > 65535);
}
// Function to open the file
void OpenFile(fstream &file, const string &filename)
{
fileNotFound = false;
file.open(filename, ios::in | ios::out | ios::binary);
// If the file is not open, sets fileNotFound to true
if (!file.is_open())
{
fileNotFound = true;
}
// Loops until the file is found and opened
while (fileNotFound)
{
// Tries to open the file again
file.open(filename, ios::in | ios::out | ios::binary);
if (!file.is_open())
{
cout << "\nFailed to open " << filename << ", check if the executable has special permissions allowed that prevent the fixer from opening it (e.g: read-only mode), it's not present in the same directory as the fixer, or if the executable is currently running. Press Enter when all the mentioned problems are solved." << endl;
do
{
ch = _getch(); // Waits for user to press a key
} while (ch != '\r'); // Keeps waiting if the key is not Enter ('\r' is the Enter key in ASCII)
}
else
{
cout << "\n" << filename << " opened successfully!" << endl;
fileNotFound = false; // Sets fileNotFound to false as the file is found and opened
}
}
}
void SearchAndReplacePatterns(fstream &file)
{
// Defines the original and new patterns with their sizes
vector<pair<const char *, size_t>> patterns = {
{"\xD8\x0D\xCC\x88\x65\x00", 6},
// DISASSEMBLED CODE - PATTERN 1 (UNMODIFIED)
// 004BA929 | D8 0D CC 88 65 00 | fmul dword ptr [006588CC]
{"\x07\x8A\x10\x40\x84\xD2\x75\xF9\x2B\xC1\x48\xC3\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 36}
};
vector<pair<const char *, size_t>> replacements = {
{"\xD8\x0D\xA1\xFB\x60\x00", 6},
// DISASSEMBLED CODE - PATTERN 1 (MODIFIED)
// 004BA929 | D8 0D A1 FB 60 00 | fmul dword ptr [0060FBA1]
{"\x07\x8A\x10\x40\x84\xD2\x75\xF9\x2B\xC1\x48\xC3\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xAA\x42", 36}
};
// Reads the entire file content into memory
file.seekg(0, ios::end);
size_t fileSize = file.tellg();
file.seekg(0, ios::beg);
char *buffer = new char[fileSize];
file.read(buffer, fileSize);
// Iterates through each pattern
for (size_t i = 0; i < patterns.size(); ++i)
{
const char *originalPattern = patterns[i].first;
size_t patternSize = patterns[i].second;
const char *newPattern = replacements[i].first;
size_t newPatternSize = replacements[i].second;
// Searches for the pattern
char *patternLocation = search(buffer, buffer + fileSize, originalPattern, originalPattern + patternSize);
// If the pattern is found, replaces it
if (patternLocation != buffer + fileSize)
{
memcpy(patternLocation, newPattern, newPatternSize);
// Writes the modified content back to the file
file.seekp(patternLocation - buffer);
file.write(newPattern, newPatternSize);
}
}
// Cleans up
delete[] buffer;
file.flush();
}
double NewCameraFOVCalculation(uint32_t &newWidthValue, uint32_t &newHeightValue, double &oldCameraHorizontalFOVValue)
{
newCameraFOVValue = 2.0 * RadToDeg(atan((static_cast<double>(newWidthValue) / static_cast<double>(newHeightValue)) / oldAspectRatio) * tan(DegToRad(oldCameraHorizontalFOVValue / 2.0)));
return newCameraFOVValue;
}
int main()
{
SetConsoleOutputCP(CP_UTF8);
cout << "Starsky & Hutch (2003) FOV Fixer v1.0 by AlphaYellow, 2024\n\n----------------\n";
do
{
OpenFile(file, "StarskyPC.exe");
SearchAndReplacePatterns(file);
file.seekg(kCameraFOVOffset);
file.read(reinterpret_cast<char *>(¤tCameraFOV), sizeof(currentCameraFOV));
cout << "\nCurrent FOV is " << currentCameraFOV << "\u00B0" << endl;
cout << "\n- Do you want to set FOV automatically based on a desired resolution (1) or set a custom FOV value (2)?: ";
HandleChoiceInput(choice1);
switch (choice1)
{
case 1:
cout << "\n- Enter the desired width: ";
HandleResolutionInput(newWidth);
cout << "\n- Enter the desired height: ";
HandleResolutionInput(newHeight);
// Calculates the new camera FOV
newCameraFOV = NewCameraFOVCalculation(newWidth, newHeight, oldCameraHorizontalFOV);
descriptor = "fixed";
break;
case 2:
cout << "\n- Enter the desired FOV value (from 1\u00B0 to 180\u00B0, default FOV for 4:3 aspect ratio is 85\u00B0): ";
HandleFOVInput(newCameraFOV);
descriptor = "changed";
break;
}
file.seekp(kCameraFOVOffset);
file.write(reinterpret_cast<const char *>(&newCameraFOV), sizeof(newCameraFOV));
// Checks if any errors occurred during the file operations
if (file.good())
{
// Confirmation message
cout << "\nSuccessfully " << descriptor << " the field of view to " << newCameraFOV << "\u00B0." << endl;
}
else
{
cout << "\nError(s) occurred during the file operations." << endl;
}
// Closes the file
file.close();
cout << "\n- Do you want to exit the program (1) or try another value (2)?: ";
HandleChoiceInput(choice2);
if (choice2 == 1)
{
cout << "\nPress enter to exit the program...";
do
{
ch = _getch(); // Wait for user to press a key
} while (ch != '\r'); // Keep waiting if the key is not Enter ('\r' is the Enter key in ASCII)
return 0;
}
cout << "\n-----------------------------------------\n";
} while (choice2 != 1); // Checks the flag in the loop condition
}