-
Notifications
You must be signed in to change notification settings - Fork 0
/
threadDecompress.cpp
264 lines (224 loc) · 7.41 KB
/
threadDecompress.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
#include "pakDataTypes.h"
#include <sstream>
list<ThreadConvertHelper> g_lThreadedResources;
u32 g_iCurResource;
u32 g_iNumResources;
HANDLE ghMutex;
bool g_bProgressOverwrite;
unsigned int g_iNumThreads;
//Create the folder that this resource ID's file will be placed in
void makeFolder(u32 resId)
{
wstring sFilename = getName(resId);
size_t pos = sFilename.find_last_of(L'/');
if(pos != wstring::npos)
sFilename = sFilename.substr(0,pos);
sFilename = TEXT("./") + sFilename;
cout << "Creating folder " << ws2s(sFilename) << endl;
ttvfs::CreateDirRec(ws2s(sFilename).c_str());
}
DWORD WINAPI decompressResource(LPVOID lpParam)
{
for(bool bDone = false;!bDone;) //Loop until we're done
{
ThreadConvertHelper dh;
wstring sFilename;
DWORD dwWaitResult = WaitForSingleObject(ghMutex, // wait for mutex
INFINITE); // no time-out interval
switch (dwWaitResult)
{
// The thread got ownership of the mutex
case WAIT_OBJECT_0:
if(!g_lThreadedResources.size()) //Done
bDone = true;
else
{
//Grab the top item off the list
dh = g_lThreadedResources.front();
sFilename = getName(dh.id); //Mutex on this too, since getName() isn't really threadsafe
makeFolder(dh.id); //Also create folder (not threadsafe, either)
g_lThreadedResources.pop_front(); //Done with this element
}
//Let user know which resource we're converting now
if(!bDone)
{
g_iCurResource++;
if(!(sFilename == TEXT(RESIDMAP_NAME) && g_iCurResource == 1))
{
if(g_bProgressOverwrite)
{
cout << "\rDecompressing file " << g_iCurResource << " out of " << g_iNumResources;
cout.flush();
}
else
cout << "Decompressing file " << g_iCurResource << " out of " << g_iNumResources << ": " << ws2s(sFilename) << endl;
}
}
// Release ownership of the mutex object
if(sFilename == TEXT(RESIDMAP_NAME) && g_iCurResource == 1) //Don't release residmap.dat mutex until we've read in all the filenames
{
g_iNumResources--;
}
else
{
if (!ReleaseMutex(ghMutex))
{
cout << "Error: Unable to release mutex." << endl;
return 1;
}
}
break;
// The thread got ownership of an abandoned mutex
// This is an indeterminate state
case WAIT_ABANDONED:
cout << "Error: Abandoned mutex" << endl;
return 1;
}
if(bDone)
{
if(sFilename == TEXT(RESIDMAP_NAME) && g_iCurResource == 1)
ReleaseMutex(ghMutex);
continue; //Stop here if done
}
if(dh.bCompressed) //Compressed
{
uint8_t* tempData = decompress(&dh.data);
if(tempData == NULL)
{
cout << "Error decompressing file " << ws2s(sFilename) << endl;
if(sFilename == TEXT(RESIDMAP_NAME) && g_iCurResource == 1)
ReleaseMutex(ghMutex);
return 1;
}
free(dh.data.data); //Free this compressed memory
dh.data.data = tempData; //Now we have the decompressed data
}
//See if this was a PNG image. Convert PNG images from the data in RAM
if(sFilename.find(TEXT(".png")) != wstring::npos ||
sFilename.find(TEXT(".PNG")) != wstring::npos ||
sFilename.find(TEXT("coloritemicon")) != wstring::npos ||
sFilename.find(TEXT("colorbgicon")) != wstring::npos ||
sFilename.find(TEXT("greybgicon")) != wstring::npos) //Also would include .png.normal files as well
{
pngHeader ph;
memcpy((void*)&ph, dh.data.data, sizeof(pngHeader));
//Convert all images
for(int i = 0; i < ph.numImages; i++)
{
wstringstream wss;
wss << sFilename << TEXT("_") << i << TEXT(".png");
imgOffset off;
memcpy((void*)&off, &dh.data.data[sizeof(pngHeader)+i*sizeof(imgOffset)], sizeof(imgOffset));
convertToPNG(wss.str().c_str(), &dh.data.data[off.offset], 0); //Do the conversion to PNG. We don't even use the size hooray
}
//Just convert the largest image
//imgOffset off;
//memcpy((void*)&off, &dh.data.data[sizeof(pngHeader)], sizeof(imgOffset));
//convertToPNG(sFilename.c_str(), &dh.data.data[off.offset], 0); //Do the conversion to PNG
}
else //For other file types, go ahead and write to the file before converting
{
//Write this out to the file
FILE* fOut = _wfopen(sFilename.c_str(), TEXT("wb"));
if(fOut == NULL)
{
cout << "Unable to open output file " << ws2s(sFilename) << endl;
if(sFilename == TEXT(RESIDMAP_NAME) && g_iCurResource == 1)
ReleaseMutex(ghMutex);
return 1;
}
fwrite(dh.data.data, 1, dh.data.decompressedSize, fOut);
fclose(fOut);
}
free(dh.data.data); //Free memory from this file
//Convert residmap.dat to XML
if(sFilename.find(TEXT("residmap.dat")) != wstring::npos)
{
residMapToXML(sFilename.c_str());
unlink(ws2s(sFilename).c_str());
}
//These are all broken/different in HRM
//TODO: Fix/figure out new formats
/*
//Convert .flac binary files to OGG
else if(sFilename.find(TEXT(".flac")) != wstring::npos ||
sFilename.find(TEXT(".FLAC")) != wstring::npos)
{
wstring s = sFilename;
s += TEXT(".ogg");
binaryToOgg(sFilename.c_str(), s.c_str());
unlink(ws2s(sFilename).c_str()); //Delete temporary .flac file
}
//Convert vdata/fontmanifest.dat to XML
else if(sFilename.find(TEXT("fontmanifest.dat")) != wstring::npos)
{
fontManifestToXML(sFilename);
unlink(ws2s(sFilename).c_str());
}
//Convert font files to XML
else if(sFilename.find(TEXT(".font.xml")) != wstring::npos)
{
fontToXML(sFilename);
}
//Convert sndmanifest.dat to XML
else if(sFilename.find(TEXT("sndmanifest.dat")) != wstring::npos)
{
sndManifestToXML(sFilename.c_str());
unlink(ws2s(sFilename).c_str());
}
*/
if(sFilename == TEXT(RESIDMAP_NAME) && g_iCurResource == 1)
{
ReleaseMutex(ghMutex);
g_iCurResource--;
}
}
return 0;
}
void threadedDecompress()
{
ttvfs::CreateDirRec("./output/");
g_iCurResource = 0;
g_iNumResources = g_lThreadedResources.size();
//Create mutex
ghMutex = CreateMutex(NULL, // default security attributes
FALSE, // initially not owned
NULL); // unnamed mutex
if (ghMutex == NULL)
{
cout << "Error: Unable to create mutex for multithreaded decompression. Aborting..." << endl;
return;
}
//Get how many processor cores we have, so we know how many threads to create
SYSTEM_INFO siSysInfo;
GetSystemInfo(&siSysInfo);
u32 iNumThreads = siSysInfo.dwNumberOfProcessors;
if(g_iNumThreads != 0)
iNumThreads = g_iNumThreads;
//Create memory for the threads
HANDLE* aThread = (HANDLE*)malloc(sizeof(HANDLE) * iNumThreads);
//Start threads
for(u32 i = 0; i < iNumThreads; i++ )
{
aThread[i] = CreateThread(
NULL, // default security attributes
0, // default stack size
(LPTHREAD_START_ROUTINE) decompressResource,
NULL, // no thread function arguments
0, // default creation flags
NULL); // no thread identifier
if( aThread[i] == NULL )
{
cout << "CreateThread error: " << GetLastError() << endl;
free(aThread);
return;
}
}
// Wait for all threads to terminate
WaitForMultipleObjects(iNumThreads, aThread, TRUE, INFINITE);
// Close thread and mutex handles
for(u32 i = 0; i < iNumThreads; i++ )
CloseHandle(aThread[i]);
CloseHandle(ghMutex);
free(aThread);
}