-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPresitter.h
244 lines (202 loc) · 7.6 KB
/
Presitter.h
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
#pragma once
#include "stdio.h"
#include "InGameFunctions.h"
#include "GlobalVariables.h"
static injector::hook_back<HRESULT(WINAPI*)(HWND, int, HANDLE, DWORD, LPSTR)> hb_SHGetFolderPathA;
static injector::hook_back<unsigned int(*)(char const*)> hb_rmdir;
int Presitter_Delete(char const* Path)
{
char PresetFolderPath[MAX_PATH];
char PresetPath[MAX_PATH];
// Get preset folder
sprintf(PresetFolderPath, "%s\\Presets", Path);
// Get rid of SkipSave and SkipLoad first
sprintf(PresetPath, "%s\\SkipSave", Path);
FILE* SkipFile = fopen(PresetPath, "rb");
if (SkipFile)
{
fclose(SkipFile); // Close the file
remove(PresetPath); // Delete the file we just opened
}
sprintf(PresetPath, "%s\\SkipLoad", Path);
SkipFile = fopen(PresetPath, "rb");
if (SkipFile)
{
fclose(SkipFile); // Close the file
remove(PresetPath); // Delete the file we just opened
}
// Empty the directory
for (int i = 0; i < NumFECustomizationRecords; i++)
{
// Create file handle
sprintf(PresetPath, "%s\\%02d.bin", PresetFolderPath, i);
FILE* PresetFile = fopen(PresetPath, "rb");
if (PresetFile)
{
fclose(PresetFile); // Close the file
remove(PresetPath); // Delete the file we just opened
}
}
// Delete the directory
hb_rmdir.fun(PresetFolderPath);
#ifdef _DEBUG
bReleasePrintf("Presitter: Deleted preset data.\n");
#endif
return hb_rmdir.fun(Path);
}
void Presitter_Save(char const* ProfileName)
{
// Presittter: Dump preset data from customization records next to the game profile
char SaveProfilePath[MAX_PATH];
char PresetPath[MAX_PATH];
char CarTypeName[CarNameLength];
char PresetName[PresetNameLength];
int zero = 0;
hb_SHGetFolderPathA.fun(0, 0x8005, 0, 0, SaveProfilePath); // Get save profile folder
strcat(SaveProfilePath, "\\NFS Most Wanted");
strcat(SaveProfilePath, "\\");
strcat(SaveProfilePath, ProfileName);
strcat(SaveProfilePath, "\\Presets");
CreateDirectoryA(SaveProfilePath, 0);
sprintf(PresetPath, "%s\\SkipSave", SaveProfilePath);
FILE* SkipFile = fopen(PresetPath, "rb");
if (!SkipFile)
{
DWORD* FEDatabase = *(DWORD**)_FEDatabase;
// Delete all files first
for (int i = 0; i < NumFECustomizationRecords; i++)
{
// Create file handle
sprintf(PresetPath, "%s\\%02d.bin", SaveProfilePath, i);
FILE* PresetFile = fopen(PresetPath, "rb");
if (PresetFile)
{
fclose(PresetFile); // Close the file
remove(PresetPath); // Delete the file we just opened
}
}
for (int i = 0; i < NumFECarRecords; i++)
{
// Get car record
DWORD* CarRecord = (DWORD*)(FEDatabase[4] + 1044 + 20 * i); // In game implementation returns 0 on bonus cars
if (CarRecord)
{
// Get Customization Record
BYTE CustRecHandle = ((BYTE*)CarRecord)[16];
if (CarRecord[0] != -1 && CustRecHandle != 0xFF) // Valid car and customization records
{
// Get customization record
DWORD* CustomizationRecord = FEPlayerCarDB_GetCustomizationRecordByHandle((DWORD*)(FEDatabase[4] + 1044), CustRecHandle);
if (CustomizationRecord)
{
// Create file handle
sprintf(PresetPath, "%s\\%02d.bin", SaveProfilePath, CustRecHandle);
FILE* PresetFile = fopen(PresetPath, "wb");
if (PresetFile)
{
// TODO: Maybe add a message to show the presitter is working on the preset data
// (Saving %s, please don't turn off the system.)
memset(CarTypeName, 0, sizeof(CarTypeName));
memset(PresetName, 0, sizeof(PresetName));
fwrite(&zero, sizeof(DWORD), 1, PresetFile); // padding 00 00 00 00
fwrite(&zero, sizeof(DWORD), 1, PresetFile); // padding 00 00 00 00
sprintf(CarTypeName, GetCarTypeName(FECarRecord_GetType(CarRecord))); // CarTypeName
fwrite(CarTypeName, sizeof(CarTypeName), 1, PresetFile);
sprintf(PresetName, "CUSTOMIZATION_RECORD_%02d", CustRecHandle); // Preset Name
fwrite(PresetName, sizeof(PresetName), 1, PresetFile);
fwrite(&CarRecord[1], sizeof(DWORD), 1, PresetFile); // frontend hash
fwrite(&zero, sizeof(DWORD), 1, PresetFile); // padding 00 00 00 00
fwrite(&CarRecord[2], sizeof(DWORD), 1, PresetFile); // pvehicle hash
fwrite(&zero, sizeof(DWORD), 1, PresetFile); // padding 00 00 00 00
fwrite(&zero, sizeof(DWORD), 1, PresetFile); // padding 00 00 00 00
fwrite(&zero, sizeof(DWORD), 1, PresetFile); // padding 00 00 00 00
// Write Parts
for (int p = 0; p < NumCarSlots; p++)
{
DWORD* TheCarPart = FECustomizationRecord_GetInstalledPart(CustomizationRecord, FECarRecord_GetType(CarRecord), p);
if (TheCarPart) fwrite(&TheCarPart[0], sizeof(DWORD), 1, PresetFile);
else fwrite(&zero, sizeof(DWORD), 1, PresetFile); // padding 00 00 00 00
}
fwrite(&zero, sizeof(DWORD), 1, PresetFile); // padding 00 00 00 00
// Close the file
fclose(PresetFile);
}
}
}
}
}
#ifdef _DEBUG
bReleasePrintf("Presitter: Saved customization records as preset data.\n");
#endif
}
else fclose(SkipFile);
}
void Presitter_Load(char const* ProfileName)
{
// Presitter: Load preset data over customization records
char SaveProfilePath[MAX_PATH];
char PresetPath[MAX_PATH];
char CarTypeName[CarNameLength];
char PresetName[PresetNameLength];
BYTE PresetBuf[PresetFileLength];
int WrongCarCount = 0;
hb_SHGetFolderPathA.fun(0, 0x8005, 0, 0, SaveProfilePath); // Get save profile folder
strcat(SaveProfilePath, "\\NFS Most Wanted");
strcat(SaveProfilePath, "\\");
strcat(SaveProfilePath, ProfileName);
strcat(SaveProfilePath, "\\Presets");
CreateDirectoryA(SaveProfilePath, 0);
sprintf(PresetPath, "%s\\SkipLoad", SaveProfilePath);
FILE* SkipFile = fopen(PresetPath, "rb");
if (!SkipFile)
{
DWORD* FEDatabase = *(DWORD**)_FEDatabase;
for (int i = 0; i < NumFECarRecords; i++)
{
// Get car record
DWORD* CarRecord = (DWORD*)(FEDatabase[4] + 1044 + 20 * i); // In game implementation returns 0 on bonus cars
if (CarRecord)
{
// Get Customization Record
BYTE CustRecHandle = ((BYTE*)CarRecord)[16];
if (CarRecord[0] != -1 && CustRecHandle != 0xFF) // Valid car and customization records
{
// Get customization record
DWORD* CustomizationRecord = FEPlayerCarDB_GetCustomizationRecordByHandle((DWORD*)(FEDatabase[4] + 1044), CustRecHandle);
if (CustomizationRecord)
{
// Create file handle
sprintf(PresetPath, "%s\\%02d.bin", SaveProfilePath, CustRecHandle);
FILE* PresetFile = fopen(PresetPath, "rb");
if (PresetFile)
{
memset(PresetBuf, 0, sizeof(PresetBuf)); // clear the buffer
// Check file size
fseek(PresetFile, 0, SEEK_END); // Go to the end
int FileSize = ftell(PresetFile); // Get current offset
if (FileSize == PresetFileLength)
{
fseek(PresetFile, 0, SEEK_SET); // Return to the beginning
fread(PresetBuf, sizeof(PresetBuf), 1, PresetFile); // read file to buffer
char* CarNameFromFEREC = GetCarTypeName(FECarRecord_GetType(CarRecord));
char* CarNameFromPRESET = (char*)(PresetBuf + 8);
if (!strcmp(CarNameFromFEREC, CarNameFromPRESET)) // Check if it's the same car
{
FECustomizationRecord_BecomePresitter((WORD*)CustomizationRecord, 0, PresetBuf); // Apply parts from preset over Customization record
}
else WrongCarCount++;
}
else WrongCarCount++;
fclose(PresetFile); // Close the file
}
}
}
}
}
#ifdef _DEBUG
if (WrongCarCount) bReleasePrintf("Presitter: Loaded preset data over the customization records with %d errors.\n", WrongCarCount);
else bReleasePrintf("Presitter: Loaded preset data over the customization records.\n");
#endif
}
else fclose(SkipFile);
}