-
Notifications
You must be signed in to change notification settings - Fork 0
/
LegoRacers2WidescreenFix.cpp
243 lines (199 loc) · 8.25 KB
/
LegoRacers2WidescreenFix.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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <cstdint>
#include <limits>
#include <windows.h>
#include <conio.h>
#include <string>
#include <algorithm>
using namespace std;
// Constants
const double kPi = 3.14159265358979323846;
const streampos kFOVOffset1 = 0x0004A105;
const streampos kResolutionWidthOffset = 0x0002A912;
const streampos kResolutionHeightOffset = 0x0002A91A;
// Variables
int choice1, choice2, fileOpened, tempChoice;
int16_t currentWidth, currentHeight, newCustomResolutionValue, newWidth, newHeight;
bool fileNotFound, validKeyPressed;
double oldWidth = 4.0, oldHeight = 3.0, oldHFOV = 90.0, oldAspectRatio = oldWidth / oldHeight, newAspectRatio, currentHFOVInDegrees, currentVFOVInDegrees, newHFOVInDegrees, newVFOVInDegrees, newCustomFOVInDegrees, newFOV;
float currentFOV, fovInFloat;
string input;
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
}
}
}
float HandleFOVInput()
{
do
{
// Reads the input as a string
cin >> input;
// Replaces all commas with dots
replace(input.begin(), input.end(), ',', '.');
// Parses the string to a float
newCustomFOVInDegrees = stof(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);
return newCustomFOVInDegrees;
}
int16_t HandleResolutionInput()
{
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);
return newCustomResolutionValue;
}
// Function to open the file
void OpenFile(fstream &file)
{
fileNotFound = false;
fileOpened = 0; // Initializes fileOpened to 0
file.open("LEGO Racers 2.exe", 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("LEGO Racers 2.exe", ios::in | ios::out | ios::binary);
if (!file.is_open())
{
cout << "\nFailed to open LEGO Racers 2.exe, 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 it's currently being used. Press Enter when all the mentioned problems are solved." << endl;
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)
}
else
{
cout << "\nLEGO Racers 2.exe opened successfully!" << endl;
fileNotFound = false; // Sets fileNotFound to false as the file is found and opened
}
}
}
int main()
{
SetConsoleOutputCP(CP_UTF8);
cout << "LEGO Racers 2 (2001) Widescreen Fixer v1.0 by AlphaYellow, 2024\n\n----------------\n";
do
{
OpenFile(file);
file.seekg(kFOVOffset1);
file.read(reinterpret_cast<char *>(¤tFOV), sizeof(currentFOV));
file.seekg(kResolutionWidthOffset);
file.read(reinterpret_cast<char *>(¤tWidth), sizeof(currentWidth));
file.seekg(kResolutionHeightOffset);
file.read(reinterpret_cast<char *>(¤tHeight), sizeof(currentHeight));
cout << "\nCurrent FOV is " << currentFOV << "\u00B0" << endl;
cout << "Current resolution is " << currentWidth << "x" << currentHeight << "" << endl;
cout << "\n- Enter the desired width: ";
newWidth = HandleResolutionInput();
cout << "\n- Enter the desired height: ";
newHeight = HandleResolutionInput();
cout << "\n- Do you want to set FOV automatically based on the desired resolution above (1) or set a custom FOV value (2)?: ";
HandleChoiceInput(choice1);
switch (choice1)
{
case 1:
newAspectRatio = static_cast<double>(newWidth) / static_cast<double>(newHeight);
// Calculates the new FOV
newFOV = 2.0 * radToDeg(atan((newAspectRatio / oldAspectRatio) * tan(degToRad(oldHFOV / 2.0))));
break;
case 2:
cout << "\n- Enter the desired FOV value (from 1\u00B0 to 180\u00B0, default FOV for 4:3 aspect ratio is 90.0\u00B0): ";
newFOV = HandleFOVInput();
break;
}
fovInFloat = static_cast<float>(newFOV);
file.seekp(kFOVOffset1);
file.write(reinterpret_cast<const char *>(&fovInFloat), sizeof(fovInFloat));
file.seekp(kResolutionWidthOffset);
file.write(reinterpret_cast<char *>(&newWidth), sizeof(newWidth));
file.seekp(kResolutionHeightOffset);
file.write(reinterpret_cast<char *>(&newHeight), sizeof(newHeight));
// Confirmation message
cout << "\nSuccessfully changed the resolution to " << newWidth << "x" << newHeight << " and field of view to " << fovInFloat << "\u00B0." << 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;
}
} while (choice2 != 1); // Checks the flag in the loop condition
}