aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Client/Common/DLC/DLCAudioFile.cpp
blob: 49ba52cda9a89e4c1f490818f9a2c548f4216c8f (plain)
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
#include "stdafx.h"
#include "DLCManager.h"
#include "DLCAudioFile.h"
#if defined _XBOX || defined _WINDOWS64
#include "..\..\Xbox\XML\ATGXmlParser.h"
#include "..\..\Xbox\XML\xmlFilesCallback.h"
#endif

DLCAudioFile::DLCAudioFile(const wstring &path) : DLCFile(DLCManager::e_DLCType_Audio,path)
{	
	m_pbData = NULL;
	m_dwBytes = 0;
}

void DLCAudioFile::addData(PBYTE pbData, DWORD dwBytes)
{
	m_pbData = pbData;
	m_dwBytes = dwBytes;

	processDLCDataFile(pbData,dwBytes);
}

PBYTE DLCAudioFile::getData(DWORD &dwBytes)
{
	dwBytes = m_dwBytes;
	return m_pbData;
}

WCHAR *DLCAudioFile::wchTypeNamesA[]=
{
	L"CUENAME",
	L"CREDIT",
};

DLCAudioFile::EAudioParameterType DLCAudioFile::getParameterType(const wstring &paramName)
{
	EAudioParameterType type = e_AudioParamType_Invalid;

	for(DWORD i = 0; i < e_AudioParamType_Max; ++i)
	{
		if(paramName.compare(wchTypeNamesA[i]) == 0)
		{
			type = (EAudioParameterType)i;
			break;
		}
	}

	return type;
}

void DLCAudioFile::addParameter(EAudioType type, EAudioParameterType ptype, const wstring &value)
{
	switch(ptype)
	{

		case e_AudioParamType_Credit: // If this parameter exists, then mark this as free
			//add it to the DLC credits list

			// we'll need to justify this text since we don't have a lot of room for lines of credits
			{
				// don't look for duplicate in the music credits

				//if(app.AlreadySeenCreditText(value)) break;

				int maximumChars = 55;

				bool bIsSDMode=!RenderManager.IsHiDef() && !RenderManager.IsWidescreen();

				if(bIsSDMode)
				{
					maximumChars = 45;
				}

				switch(XGetLanguage())
				{
				case XC_LANGUAGE_JAPANESE:
				case XC_LANGUAGE_TCHINESE:
				case XC_LANGUAGE_KOREAN:
					maximumChars = 35;
					break;
				}
				wstring creditValue = value;
				while (creditValue.length() > maximumChars)
				{
					unsigned int i = 1;
					while (i < creditValue.length() && (i + 1) <= maximumChars)
					{
						i++;
					}
					int iLast=(int)creditValue.find_last_of(L" ",i);
					switch(XGetLanguage())
					{
					case XC_LANGUAGE_JAPANESE:
					case XC_LANGUAGE_TCHINESE:
					case XC_LANGUAGE_KOREAN:
						iLast = maximumChars;
						break;
					default:
						iLast=(int)creditValue.find_last_of(L" ",i);
						break;
					}

					// if a space was found, include the space on this line
					if(iLast!=i)
					{
						iLast++;
					}

					app.AddCreditText((creditValue.substr(0, iLast)).c_str());
					creditValue = creditValue.substr(iLast);
				}
				app.AddCreditText(creditValue.c_str());

			}
			break;
		case e_AudioParamType_Cuename:
			m_parameters[type].push_back(value);
			//m_parameters[(int)type] = value;
			break;
	}
}

bool DLCAudioFile::processDLCDataFile(PBYTE pbData, DWORD dwLength)
{
	unordered_map<int, EAudioParameterType> parameterMapping;
	unsigned int uiCurrentByte=0;

	// File format defined in the AudioPacker
	// File format: Version 1

	unsigned int uiVersion=*(unsigned int *)pbData;
	uiCurrentByte+=sizeof(int);

	if(uiVersion < CURRENT_AUDIO_VERSION_NUM)
	{
		if(pbData!=NULL) delete [] pbData;
		app.DebugPrintf("DLC version of %d is too old to be read\n", uiVersion);
		return false;
	}
	
	unsigned int uiParameterTypeCount=*(unsigned int *)&pbData[uiCurrentByte];
	uiCurrentByte+=sizeof(int);
	C4JStorage::DLC_FILE_PARAM *pParams = (C4JStorage::DLC_FILE_PARAM *)&pbData[uiCurrentByte];
	
	for(unsigned int i=0;i<uiParameterTypeCount;i++)
	{
		// Map DLC strings to application strings, then store the DLC index mapping to application index
		wstring parameterName((WCHAR *)pParams->wchData);
		EAudioParameterType type = getParameterType(parameterName);
		if( type != e_AudioParamType_Invalid )
		{
			parameterMapping[pParams->dwType] = type;
		}
		uiCurrentByte+= sizeof(C4JStorage::DLC_FILE_PARAM)+(pParams->dwWchCount*sizeof(WCHAR));
		pParams = (C4JStorage::DLC_FILE_PARAM *)&pbData[uiCurrentByte];
	}
	unsigned int uiFileCount=*(unsigned int *)&pbData[uiCurrentByte];
	uiCurrentByte+=sizeof(int);
	C4JStorage::DLC_FILE_DETAILS *pFile = (C4JStorage::DLC_FILE_DETAILS *)&pbData[uiCurrentByte];

	DWORD dwTemp=uiCurrentByte;
	for(unsigned int i=0;i<uiFileCount;i++)
	{
		dwTemp+=sizeof(C4JStorage::DLC_FILE_DETAILS)+pFile->dwWchCount*sizeof(WCHAR);
		pFile = (C4JStorage::DLC_FILE_DETAILS *)&pbData[dwTemp];
	}
	PBYTE pbTemp=((PBYTE )pFile);
	pFile = (C4JStorage::DLC_FILE_DETAILS *)&pbData[uiCurrentByte];

	for(unsigned int i=0;i<uiFileCount;i++)
	{
		EAudioType type = (EAudioType)pFile->dwType;
		// Params
		unsigned int uiParameterCount=*(unsigned int *)pbTemp;
		pbTemp+=sizeof(int);
		pParams = (C4JStorage::DLC_FILE_PARAM *)pbTemp;
		for(unsigned int j=0;j<uiParameterCount;j++)
		{
			//EAudioParameterType paramType = e_AudioParamType_Invalid;

			AUTO_VAR(it, parameterMapping.find( pParams->dwType ));

			if(it != parameterMapping.end() )
			{
 				addParameter(type,(EAudioParameterType)pParams->dwType,(WCHAR *)pParams->wchData);
			}
			pbTemp+=sizeof(C4JStorage::DLC_FILE_PARAM)+(sizeof(WCHAR)*pParams->dwWchCount);
			pParams = (C4JStorage::DLC_FILE_PARAM *)pbTemp;
		}
		// Move the pointer to the start of the next files data;
		pbTemp+=pFile->uiFileSize;
		uiCurrentByte+=sizeof(C4JStorage::DLC_FILE_DETAILS)+pFile->dwWchCount*sizeof(WCHAR);

		pFile=(C4JStorage::DLC_FILE_DETAILS *)&pbData[uiCurrentByte];

	}

	return true;
}

int DLCAudioFile::GetCountofType(DLCAudioFile::EAudioType eType)
{
	return m_parameters[eType].size();
}


wstring &DLCAudioFile::GetSoundName(int iIndex)
{
	int iWorldType=e_AudioType_Overworld;
	while(iIndex>=m_parameters[iWorldType].size())
	{
		iIndex-=m_parameters[iWorldType].size();
		iWorldType++;
	}
	return m_parameters[iWorldType].at(iIndex);
}