-
Notifications
You must be signed in to change notification settings - Fork 0
/
hrmDecompress.cpp
205 lines (178 loc) · 5.33 KB
/
hrmDecompress.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
#include "pakDataTypes.h"
extern list<ThreadConvertHelper> g_lThreadedResources;
extern bool g_bProgressOverwrite;
extern unsigned int g_iNumThreads;
extern bool biOS;
ttvfs::VFSHelper vfs;
void parseCmdLine(int argc, char** argv)
{
for(int i = 1; i < argc; i++)
{
string s = argv[i];
if(s == "--overwrite-progress")
g_bProgressOverwrite = true;
else if(s.find("--threads=") != string::npos)
{
size_t pos = s.find('=')+1;
if(s.length() <= pos)
{
cout << "missing thread count" << endl;
continue;
}
int iNumThreads = atoi(&s.c_str()[pos]);
if(iNumThreads < 0 ||
iNumThreads > MAX_NUM_THREADS)
{
cout << "Invalid number of threads: " << iNumThreads << endl;
continue;
}
g_iNumThreads = iNumThreads;
}
else if(s == "-ios")
{
biOS = true;
}
else if(argv[i][0] == '-')
cout << "Unknown commandline switch " << argv[i] << ". Ignoring..." << endl;
}
}
//Main program entry point
int main(int argc, char** argv)
{
//Firstly, parse residmap.dat...
//residMapToXML(s2ws("residmap.dat").c_str());
//return 0;
g_bProgressOverwrite = false;
biOS = false;
g_iNumThreads = 0;
DWORD iTicks = GetTickCount(); //Store the starting number of milliseconds
vfs.Prepare();
//read in the resource names to unpack
initResMap();
initSoundManifest();
parseCmdLine(argc,argv);
if(argc < 2)
{
cout << "Usage: liDecompress [filename1] [filename2] ... [filenameN]" << endl;
return 0;
}
for(int iArg = 1; iArg < argc; iArg++)
{
if(argv[iArg][0] == '-') //Skip over commandline switches
continue;
cout << endl << "Unpacking resource blob file " << argv[iArg] << endl;
FILE* f = fopen(argv[iArg], "rb");
if(f == NULL)
{
cout << "Unable to open file " << argv[iArg] << endl;
continue;
}
//Read .pak header
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;
}
list<resourceHeader> lResourceHeaders;
//Read resource headers
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;
}
lResourceHeaders.push_back(rH);
}
//Create list file with all the files that were in this .pak
string sPakListFilename = "";
for(int i = strlen(argv[iArg])-1; i >= 0; i--)
{
if(argv[iArg][i] == '\\' ||
argv[iArg][i] == '/')
break;
sPakListFilename.insert(sPakListFilename.begin(), argv[iArg][i]);
}
sPakListFilename += ".filelist.txt";
//Iterate through these items, splitting them out of the file and creating new files out of each
cout << "Extracting files..." << endl;
for(list<resourceHeader>::iterator i = lResourceHeaders.begin(); i != lResourceHeaders.end(); i++)
{
ThreadConvertHelper dh;
//makeFolder(i->id);
//const wchar_t* cName = getName(i->id);
//oPakList << ws2s(cName) << endl;
fseek(f, i->offset, SEEK_SET);
//dh.sFilename = cName;
dh.id = i->id;
if(i->flags & FLAG_ZLIBCOMPRESSED)
{
compressedHeader cH;
if(fread((void*)&cH, 1, sizeof(compressedHeader), f) != sizeof(compressedHeader))
{
cout << "Error reading compressed header." << endl;
fclose(f);
continue;
}
uint32_t size = cH.compressedSizeBytes;
uint8_t* buf = (uint8_t*)malloc(size);
size_t sizeRead = fread((void*)buf, 1, size, f);
if(sizeRead != size)
{
cout << "Error reading compressed data. Size: " << size << " read: " << sizeRead << endl;
fclose(f);
free(buf);
continue;
}
dh.data.data = buf;
dh.data.compressedSize = cH.compressedSizeBytes;
dh.data.decompressedSize = cH.uncompressedSizeBytes;
dh.bCompressed = true;
}
else// if(i->flags & FLAG_ZLIBCOMPRESSED == FLAG_NOCOMPRESSION) //Uncompressed
{
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);
free(buf);
continue;
}
dh.data.data = buf;
dh.data.compressedSize = dh.data.decompressedSize = i->size;
dh.bCompressed = false;
}
/*else
{
cout << "Invalid resource flag " << i->flags << ". Skipping resource " << i->id << endl;
continue;
}*/
g_lThreadedResources.push_back(dh);
}
threadedDecompress();
fclose(f);
ofstream oPakList(sPakListFilename.c_str());
wstring sIsResidFilename = getName(lResourceHeaders.front().id);
if(sIsResidFilename == TEXT(RESIDMAP_NAME))
lResourceHeaders.pop_front(); //HACK: So we don't end up with recursive residmap.dat files in our pakfiles...
for(list<resourceHeader>::iterator i = lResourceHeaders.begin(); i != lResourceHeaders.end(); i++)
{
oPakList << ws2s(getName(i->id)) << endl;
}
oPakList.close();
}
cout << "\rDone. " << endl;
iTicks = GetTickCount() - iTicks;
float iSeconds = (float)iTicks / 1000.0; //Get seconds elapsed
int iMinutes = iSeconds / 60;
iSeconds -= iMinutes * 60;
cout << "Time elapsed: " << iMinutes << " min, " << iSeconds << " sec" << endl;
//system("PAUSE");
return 0;
}