-
Notifications
You must be signed in to change notification settings - Fork 0
/
modManage.cpp
306 lines (264 loc) · 8.29 KB
/
modManage.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
298
299
300
301
302
303
304
305
306
#include "pakDataTypes.h"
#define RESOURCE_1_NAME TEXT("resource.pak")
#define RESOURCE_2_NAME TEXT("embed.pak")
#define RESOURCE_3_NAME TEXT("frontend.pak")
map<u32, virtualFile> g_mOrig;
map<u32, virtualFile> g_mMods;
list<resourceHeader> g_lResourceHeaders[3];
ofstream oWarnings("mergeresults.txt");
//Functions from Stack Overflow peoples
wstring s2ws(const string& s)
{
int len;
int slength = (int)s.length();
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wstring r(len, L'\0');
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, &r[0], len);
return r;
}
string ws2s(const wstring& s)
{
int len;
int slength = (int)s.length();
len = WideCharToMultiByte(CP_ACP, 0, s.c_str(), slength, 0, 0, 0, 0);
string r(len, '\0');
WideCharToMultiByte(CP_ACP, 0, s.c_str(), slength, &r[0], len, 0, 0);
return r;
}
void splitOutFiles(FILE* f, list<resourceHeader>* lRH, bool bMod)
{
//Iterate through these items, splitting them out of the file and saving the data
for(list<resourceHeader>::iterator i = lRH->begin(); i != lRH->end(); i++)
{
fseek(f, i->offset, SEEK_SET);
uint8_t* buf = (uint8_t*)malloc(i->size);
if(fread((void*)buf, 1, i->size, f) != i->size)
{
cout << "Error reading non-compressed data." << endl;
fclose(f);
//fclose(fOut);
free(buf);
continue;
}
virtualFile vf;
vf.size = i->size;
vf.data = buf;
if(bMod)
{
if(g_mMods.count(i->id)) //This was here already
{
//TODO: Merge residmap.dat files together
oWarnings << "Warning: Conflict with mod resource " << i->id << ". Overwriting..." << endl;
free(g_mMods[i->id].data); //Clean up original memory
}
g_mMods[i->id] = vf;
}
else
{
if(g_mOrig.count(i->id)) //This was here already
{
oWarnings << "Warning: Conflict with original resource " << i->id << ". Overwriting..." << endl;
free(g_mOrig[i->id].data); //Clean up original memory
}
g_mOrig[i->id] = vf;
}
}
}
void copyTempFiles()
{
for(map<u32, virtualFile>::iterator i = g_mMods.begin(); i != g_mMods.end(); i++)
{
if(g_mOrig.count(i->first)) //Original file here already
{
oWarnings << "Note: Mod overwriting original resource: " << i->first << endl;
free(g_mOrig[i->first].data); //Clean up this memory
}
else //We're copying in a new file. Stick it in embed.pak for now. TODO: Some sort of selection
{
resourceHeader rH;
rH.id = i->first; //Only need the ID, the rest will be filled in later
if(rH.id != RESIDMAP_ID)
//g_lResourceHeaders[1].push_front(rH); //TODO: pakfile selection
//else
g_lResourceHeaders[1].push_back(rH);
}
g_mOrig[i->first] = i->second;
}
}
//Main program entry point
int main(int argc, char** argv)
{
DWORD iTicks = GetTickCount(); //Store the starting number of milliseconds
if(argc < 2)
{
cout << "Usage: modManage [pakfile1] [pakfile2] ... [pakfileN]" << endl;
return 0;
}
//First, check and see what resource ID's are in each mod to merge, and split them all out
list<resourceHeader> lModResHeaders[argc-1]; //For splitting mods out of the .pak files
map<u32, u32> mModHeader; //For repacking, need to know the flags for this file
map<u32, bool> mModResources;
for(int iArg = 1; iArg < argc; iArg++)
{
//Read in these files
cout << "Unpacking mod " << argv[iArg] << endl;
FILE* f = _wfopen(s2ws(argv[iArg]).c_str(), TEXT("rb"));
if(f == NULL)
{
cout << "Unable to open file " << argv[iArg] << endl;
continue;
}
blobHeader bH;
if(fread((void*)&bH, 1, sizeof(blobHeader), f) != sizeof(blobHeader))
{
cout << "Error reading number of resources in file " << argv[iArg] << endl;
fclose(f);
continue;
}
for(int i = 0; i < bH.numItems; i++)
{
resourceHeader rH;
size_t sizeRead = fread((void*)&rH, 1, sizeof(resourceHeader), f);
if(sizeRead != sizeof(resourceHeader))
{
cout << "Read " << sizeRead << " bytes, which differs from resource header size " << sizeof(resourceHeader) << endl;
fclose(f);
continue;
}
lModResHeaders[iArg-1].push_back(rH);
mModResources[rH.id] = true;
mModHeader[rH.id] = rH.flags;
}
splitOutFiles(f, &lModResHeaders[iArg-1], true);
fclose(f);
}
bool bPack[3];
//Now that we have all the resources from the mod .pak files unpacked, check and see what original .pak files need to be changed
for(int iPak = 0; iPak < 3; iPak++)
{
wstring sArg;
switch(iPak)
{
case 0:
sArg = RESOURCE_1_NAME;
break;
case 1:
sArg = RESOURCE_2_NAME;
break;
default:
sArg = RESOURCE_3_NAME;
break;
}
cout << "Pulling headers from resource blob file " << ws2s(sArg) << endl;
FILE* f = _wfopen(sArg.c_str(), TEXT("rb"));
if(f == NULL)
{
cout << "Unable to open file " << ws2s(sArg) << endl;
continue;
}
blobHeader bH;
if(fread((void*)&bH, 1, sizeof(blobHeader), f) != sizeof(blobHeader))
{
cout << "Error reading number of resources in file " << ws2s(sArg) << endl;
fclose(f);
continue;
}
bPack[iPak] = false;
if(iPak == 1) //TODO: Stick new files in other .pak files instead of embed.pak
bPack[iPak] = true; //TODO: Actually test to see if new files, and in what pakfiles
for(int i = 0; i < bH.numItems; i++)
{
resourceHeader rH;
size_t sizeRead = fread((void*)&rH, 1, sizeof(resourceHeader), f);
if(sizeRead != sizeof(resourceHeader))
{
cout << "Read " << sizeRead << " bytes, which differs from resource header size " << sizeof(resourceHeader) << endl;
fclose(f);
continue;
}
g_lResourceHeaders[iPak].push_back(rH);
if(mModResources.count(rH.id))
bPack[iPak] = true;
}
if(bPack[iPak]) //Unpack this resource only if we need to
{
cout << "Unpacking resource " << ws2s(sArg) << endl;
splitOutFiles(f, &g_lResourceHeaders[iPak], false);
}
fclose(f);
}
//Now we have all the .pak files we need extracted. Copy mod files over
copyTempFiles();
//And recompress
for(int iPak = 0; iPak < 3; iPak++)
{
if(!bPack[iPak])
continue;
wstring sArg;
switch(iPak)
{
case 0:
sArg = RESOURCE_1_NAME;
break;
case 1:
sArg = RESOURCE_2_NAME;
break;
default:
sArg = RESOURCE_3_NAME;
break;
}
cout << "Repacking resource blob file " << ws2s(sArg) << endl;
//Open our output pakfile for writing
FILE* f = _wfopen(sArg.c_str(), TEXT("wb"));
if(f == NULL)
{
cout << "Unable to open file " << ws2s(sArg) << " for writing. Skipping..." << endl;
continue;
}
//Add the header
blobHeader bh;
bh.pakVersion = 0x01;
bh.numItems = g_lResourceHeaders[iPak].size();
fwrite(&bh, 1, sizeof(blobHeader), f);
//Get the starting file pos for where we (should) be writing objects to
size_t offsetPos = sizeof(blobHeader) + (g_lResourceHeaders[iPak].size() * sizeof(resourceHeader));
//Add the table of contents
cout << "Adding table of contents..." << endl;
for(list<resourceHeader>::iterator i = g_lResourceHeaders[iPak].begin(); i != g_lResourceHeaders[iPak].end(); i++)
{
i->offset = offsetPos; //Offset
i->size = g_mOrig[i->id].size; //Size of file
if(mModHeader.count(i->id))
i->flags = mModHeader[i->id]; //Set the flags to the flags of this file copied over (otherwise keep the same flags)
fwrite(&(*i), 1, sizeof(resourceHeader), f); //Write this to the file
offsetPos += i->size; //Add this size to our running tally of where we are
}
//Add actual resource data
cout << "Adding files..." << endl;
for(list<resourceHeader>::iterator i = g_lResourceHeaders[iPak].begin(); i != g_lResourceHeaders[iPak].end(); i++)
{
virtualFile vfIn = g_mOrig[i->id];
if(vfIn.data == NULL)
{
cout << "Error: resource " << i->id << " is in more than one output .pak file. Abort." << endl;
return 1;
}
//cout << "Merging " << i->id << endl;
fwrite(vfIn.data, 1, vfIn.size, f); //Write this to the file
//Free this data while we're at it
free(vfIn.data);
g_mOrig[i->id].data = NULL;
}
fclose(f); //Done packing this .pak file
}
//Done
cout << endl << "Done." << endl;
iTicks = GetTickCount() - iTicks;
float iSeconds = (float)iTicks / 1000.0; //Get seconds elapsed
int iMinutes = (int)iSeconds / 60;
iSeconds -= iMinutes * 60;
cout << "Time elapsed: " << iMinutes << " min, " << iSeconds << " sec" << endl;
oWarnings << "Done." << endl;
oWarnings.close();
return 0;
}