-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecalcSoundManifest.cpp
260 lines (221 loc) · 7.14 KB
/
recalcSoundManifest.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
#include "pakDataTypes.h"
const wchar_t* cFilename = TEXT("vdata/sndmanifest.dat.xml");
list<wstring> g_lsOggFiles;
map<wstring, takeRecord> g_mtrTakes;
int g_iCurResource;
int g_iNumResourcesTotal;
HANDLE g_hInMutex; //Mutex for g_lsOggFiles
HANDLE g_hOutMutex; //Mutex for g_mtrTakes
//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;
}
DWORD WINAPI oggParse(LPVOID lpParam)
{
for(bool bDone = false;!bDone;) //Loop until we're done
{
wstring sFile;
DWORD dwWaitResult = WaitForSingleObject(g_hInMutex, // wait for mutex
INFINITE); // no time-out interval
switch (dwWaitResult)
{
// The thread got ownership of the mutex
case WAIT_OBJECT_0:
if(!g_lsOggFiles.size()) //Done
bDone = true;
else
{
//Grab the top item off the list
sFile = g_lsOggFiles.front();
g_lsOggFiles.pop_front(); //Done with this element
}
//Let user know which resource we're converting now
if(!bDone)
cout << "Parsing sound " << ++g_iCurResource << " out of " << g_iNumResourcesTotal << ": " << ws2s(sFile) << endl;
// Release ownership of the mutex object
if (!ReleaseMutex(g_hInMutex))
{
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)
continue; //Stop here if done
//Parse this OGG file
takeRecord tr = getOggData(sFile.c_str());
//Save this result
dwWaitResult = WaitForSingleObject(g_hOutMutex, INFINITE);
switch(dwWaitResult)
{
// The thread got ownership of the mutex
case WAIT_OBJECT_0:
g_mtrTakes[sFile] = tr; //Store this
// Release ownership of the mutex object
if (!ReleaseMutex(g_hOutMutex))
{
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;
}
}
return 0;
}
bool threadedOgg()
{
g_iCurResource = 0;
g_iNumResourcesTotal = g_lsOggFiles.size();
//Create mutex
g_hInMutex = CreateMutex(NULL, FALSE, NULL);
g_hOutMutex = CreateMutex(NULL, FALSE, NULL);
if(g_hInMutex == NULL || g_hOutMutex == NULL)
{
cout << "Error: Unable to create mutexes for multithreaded OGG parsing. Aborting..." << endl;
return false;
}
//Get how many processor cores we have, so we know how many threads to create
SYSTEM_INFO siSysInfo;
GetSystemInfo(&siSysInfo);
u32 iNumThreads = siSysInfo.dwNumberOfProcessors;
//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) oggParse,
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 false;
}
}
// 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(g_hInMutex);
CloseHandle(g_hOutMutex);
free(aThread);
return true;
}
int main(int argc, char** argv)
{
DWORD iTicks = GetTickCount();
XMLDocument* doc = new XMLDocument;
int iErr = doc->LoadFile(ws2s(cFilename).c_str());
if(iErr != XML_NO_ERROR)
{
cout << "Error parsing XML file " << cFilename << ": Error " << iErr << endl;
delete doc;
return 1;
}
//Grab root element
XMLElement* root = doc->RootElement();
if(root == NULL)
{
cout << "Error: Root element NULL in XML file " << cFilename << endl;
delete doc;
return 1;
}
int iTotalSounds = 0;
root->QueryIntAttribute("numtakes", &iTotalSounds);
cout << endl << "Parsing XML..." << endl;
//Get the filenames that we'll be parsing
XMLElement* elem = root->FirstChildElement("sound");
while(elem != NULL)
{
//Get the takes for this sound
XMLElement* elem2 = elem->FirstChildElement("take");
while(elem2 != NULL)
{
const char* cName = elem2->Attribute("filename");
if(cName == NULL)
{
cout << "Error: Unable to get filename of take record in file " << cFilename << endl;
delete doc;
return 1;
}
g_lsOggFiles.push_back(s2ws(cName)); //Save this for later
elem2 = elem2->NextSiblingElement("take"); //Next item
}
elem = elem->NextSiblingElement("sound"); //Next item
}
//Start the threads running that'll get our OGG data
if(!threadedOgg())
{
cout << "Abort." << endl;
return 1;
}
//Roll through child elements
cout << endl << "Filling XML in with vorbis data..." << endl;
elem = root->FirstChildElement("sound");
//int iCurSound = 0;
while(elem != NULL)
{
//Get the takes for this sound
XMLElement* elem2 = elem->FirstChildElement("take");
while(elem2 != NULL)
{
const char* cName = elem2->Attribute("filename"); //Assume this works, since we've tested for this == NULL already
//cout << "Parsing sound " << ++iCurSound << " out of " << iTotalSounds << ": " << cName << endl;
takeRecord tr = g_mtrTakes[s2ws(cName)]; //Grab the header data that we've parsed
if(tr.resId != 0)
{
//Update this info
elem2->SetAttribute("channels", tr.channels);
elem2->SetAttribute("samplespersec", tr.samplesPerSec);
elem2->SetAttribute("samplecountperchannel", tr.sampleCountPerChannel);
elem2->SetAttribute("vorbisworkingsetsizebytes", tr.vorbisWorkingSetSizeBytes);
elem2->SetAttribute("vorbismarkerssizebytes", tr.vorbisMarkersSizeBytes);
elem2->SetAttribute("vorbispacketssizebytes", tr.vorbisPacketsSizeBytes);
}
//else
// cout << "Could not open file " << cName << "; Skipping" << endl;
elem2 = elem2->NextSiblingElement("take"); //Next item
}
elem = elem->NextSiblingElement("sound"); //Next item
}
doc->SaveFile(ws2s(cFilename).c_str()); //Save this back
delete doc; //We're done with this
cout << "Done" << endl;
iTicks = GetTickCount() - iTicks;
int iSeconds = iTicks / 1000; //Get seconds elapsed
int iMinutes = iSeconds / 60;
iSeconds -= iMinutes * 60;
cout << "Time elapsed: " << iMinutes << " min, " << iSeconds << " sec" << endl;
return 0;
}