forked from meh2481/liTools
/
threadDecompress.cpp
251 lines (215 loc) · 6.2 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
#include <mutex>
#include <thread>
#include "pakDataTypes.h"
#include "residmap.h"
#include "stringTools.h"
#include "zpipe.h"
#include "image.h"
list<ThreadConvertHelper> g_lThreadedResources;
u32 g_iCurResource;
u32 g_iNumResources;
std::mutex g_Mutex;
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 = L"./" + sFilename;
//cout << "Creating folder " << ws2s(sFilename) << endl;
ttvfs::CreateDirRec(ws2s(sFilename).c_str());
}
void decompressResource()
{
for(bool bDone = false;!bDone;) //Loop until we're done
{
ThreadConvertHelper dh;
wstring sFilename;
std::lock_guard<std::mutex> lock(g_Mutex);
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 == 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 == RESIDMAP_NAME && g_iCurResource == 1) //Don't release residmap.dat mutex until we've read in all the filenames
{
g_iNumResources--;
}
if(bDone)
{
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;
return;
}
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(L".png") != wstring::npos ||
sFilename.find(L".PNG") != wstring::npos ||
sFilename.find(L"coloritemicon") != wstring::npos ||
sFilename.find(L"colorbgicon") != wstring::npos ||
sFilename.find(L"greybgicon") != wstring::npos) //Also would include .png.normal files as well
{
convertToPNG(sFilename.c_str(), dh.data.data, dh.data.decompressedSize); //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 = fopen(ws2s(sFilename).c_str(), "wb");
if(fOut == NULL)
{
cout << "Unable to open output file " << ws2s(sFilename) << endl;
return;
}
fwrite(dh.data.data, 1, dh.data.decompressedSize, fOut);
fclose(fOut);
}
free(dh.data.data); //Free memory from this file
/*
//Convert wordPackDict.dat to XML
if(sFilename.find(L"wordPackDict.dat") != wstring::npos)
{
wordPackToXML(sFilename.c_str());
unlink(ws2s(sFilename).c_str());
}
//Convert sndmanifest.dat to XML
else if(sFilename.find(L"sndmanifest.dat") != wstring::npos)
{
sndManifestToXML(sFilename.c_str());
unlink(ws2s(sFilename).c_str());
}
//Convert itemmanifest.dat to XML
else if(sFilename.find(L"itemmanifest.dat") != wstring::npos)
{
itemManifestToXML(sFilename.c_str());
unlink(ws2s(sFilename).c_str());
}
//Convert letterdb.dat to XML
else if(sFilename.find(L"letterdb.dat") != wstring::npos)
{
letterToXML(sFilename.c_str());
unlink(ws2s(sFilename).c_str());
}
//Convert catalogdb.dat to XML
else if(sFilename.find(L"catalogdb.dat") != wstring::npos)
{
catalogToXML(sFilename.c_str());
unlink(ws2s(sFilename).c_str());
}
//Convert combodb.dat to XML
else if(sFilename.find(L"combodb.dat") != wstring::npos)
{
comboDBToXML(sFilename.c_str());
unlink(ws2s(sFilename).c_str());
}
//Convert residmap.dat to XML
else if(sFilename.find(L"residmap.dat") != wstring::npos)
{
residMapToXML(sFilename.c_str());
unlink(ws2s(sFilename).c_str());
}
//Convert .flac binary files to OGG
else if(sFilename.find(L".flac") != wstring::npos ||
sFilename.find(L".FLAC") != wstring::npos)
{
wstring s = sFilename;
s += L".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(L"fontmanifest.dat") != wstring::npos)
{
fontManifestToXML(sFilename);
unlink(ws2s(sFilename).c_str());
}
//Convert font files to XML
else if(sFilename.find(L".font.xml") != wstring::npos)
{
fontToXML(sFilename);
}
//Convert vdata/loctexmanifest.bin to XML
else if(sFilename.find(L"loctexmanifest.bin") != wstring::npos)
{
LoctexManifestToXML(sFilename);
unlink(ws2s(sFilename).c_str());
}
//Convert vdata/myPicturesImage.dat to XML
else if(sFilename.find(L"myPicturesImage.dat") != wstring::npos)
{
myPicturesToXML(sFilename);
unlink(ws2s(sFilename).c_str());
}
//Convert vdata/smokeImage.dat to XML
else if(sFilename.find(L"smokeImage.dat") != wstring::npos)
{
smokeImageToXML(sFilename);
unlink(ws2s(sFilename).c_str());
}
//Convert vdata/fluidPalettes.dat to XML
else if(sFilename.find(L"fluidPalettes.dat") != wstring::npos)
{
fluidPalettesToXML(sFilename);
unlink(ws2s(sFilename).c_str());
}
*/
if(sFilename == RESIDMAP_NAME && g_iCurResource == 1)
{
g_iCurResource--;
}
}
return;
}
void threadedDecompress()
{
g_iCurResource = 0;
g_iNumResources = g_lThreadedResources.size();
auto iNumThreads = std::thread::hardware_concurrency();
if(g_iNumThreads != 0)
{
iNumThreads = g_iNumThreads;
}
std::vector<std::thread> aThread;
//Start threads
for(u32 i = 0; i < iNumThreads; i++ )
{
aThread.emplace_back(decompressResource);
}
for(auto& it: aThread)
{
it.join();
}
}