collection.c 34.5 KB
Newer Older
1 2
/* IDirectMusicCollection Implementation
 *
3
 * Copyright (C) 2003-2004 Rok Mandeljc
4
 *
5 6 7 8
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
9 10 11
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
14
 *
15 16 17
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 19 20 21 22
 */

#include "dmusic_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(dmusic);
23
WINE_DECLARE_DEBUG_CHANNEL(dmfile);
24

25 26 27 28 29
static ULONG WINAPI IDirectMusicCollectionImpl_IUnknown_AddRef (LPUNKNOWN iface);
static ULONG WINAPI IDirectMusicCollectionImpl_IDirectMusicCollection_AddRef (LPDIRECTMUSICCOLLECTION iface);
static ULONG WINAPI IDirectMusicCollectionImpl_IDirectMusicObject_AddRef (LPDIRECTMUSICOBJECT iface);
static ULONG WINAPI IDirectMusicCollectionImpl_IPersistStream_AddRef (LPPERSISTSTREAM iface);

30 31 32 33
/*****************************************************************************
 * IDirectMusicCollectionImpl implementation
 */
/* IDirectMusicCollectionImpl IUnknown part: */
34
static HRESULT WINAPI IDirectMusicCollectionImpl_IUnknown_QueryInterface (LPUNKNOWN iface, REFIID riid, LPVOID *ppobj) {
35
	ICOM_THIS_MULTI(IDirectMusicCollectionImpl, UnknownVtbl, iface);
36 37
	TRACE("(%p, %s, %p)\n", This, debugstr_dmguid(riid), ppobj);

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
	if (IsEqualIID (riid, &IID_IUnknown)) {
		*ppobj = (LPVOID)&This->UnknownVtbl;
		IDirectMusicCollectionImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
		return S_OK;	
	} else if (IsEqualIID (riid, &IID_IDirectMusicCollection)) {
		*ppobj = (LPVOID)&This->CollectionVtbl;
		IDirectMusicCollectionImpl_IDirectMusicCollection_AddRef ((LPDIRECTMUSICCOLLECTION)&This->CollectionVtbl);
		return S_OK;
	} else if (IsEqualIID (riid, &IID_IDirectMusicObject)) {
		*ppobj = (LPVOID)&This->ObjectVtbl;
		IDirectMusicCollectionImpl_IDirectMusicObject_AddRef ((LPDIRECTMUSICOBJECT)&This->ObjectVtbl);		
		return S_OK;
	} else if (IsEqualIID (riid, &IID_IPersistStream)) {
		*ppobj = (LPVOID)&This->PersistStreamVtbl;
		IDirectMusicCollectionImpl_IPersistStream_AddRef ((LPPERSISTSTREAM)&This->PersistStreamVtbl);		
53 54
		return S_OK;
	}
55
	
56
	WARN("(%p, %s, %p): not found\n", This, debugstr_dmguid(riid), ppobj);
57 58 59
	return E_NOINTERFACE;
}

60
static ULONG WINAPI IDirectMusicCollectionImpl_IUnknown_AddRef (LPUNKNOWN iface) {
61
	ICOM_THIS_MULTI(IDirectMusicCollectionImpl, UnknownVtbl, iface);
62 63
	ULONG refCount = InterlockedIncrement(&This->ref);

64
	TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
65

66 67
	DMUSIC_LockModule();

68
	return refCount;
69 70
}

71
static ULONG WINAPI IDirectMusicCollectionImpl_IUnknown_Release (LPUNKNOWN iface) {
72
	ICOM_THIS_MULTI(IDirectMusicCollectionImpl, UnknownVtbl, iface);
73 74
	ULONG refCount = InterlockedDecrement(&This->ref);

75
	TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
76 77

	if (!refCount) {
78 79
		HeapFree(GetProcessHeap(), 0, This);
	}
80 81 82

	DMUSIC_UnlockModule();

83
	return refCount;
84 85
}

86
static const IUnknownVtbl DirectMusicCollection_Unknown_Vtbl = {
87 88 89 90 91 92
	IDirectMusicCollectionImpl_IUnknown_QueryInterface,
	IDirectMusicCollectionImpl_IUnknown_AddRef,
	IDirectMusicCollectionImpl_IUnknown_Release
};

/* IDirectMusicCollectionImpl IDirectMusicCollection part: */
93
static HRESULT WINAPI IDirectMusicCollectionImpl_IDirectMusicCollection_QueryInterface (LPDIRECTMUSICCOLLECTION iface, REFIID riid, LPVOID *ppobj) {
94 95 96 97
	ICOM_THIS_MULTI(IDirectMusicCollectionImpl, CollectionVtbl, iface);
	return IDirectMusicCollectionImpl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
}

98
static ULONG WINAPI IDirectMusicCollectionImpl_IDirectMusicCollection_AddRef (LPDIRECTMUSICCOLLECTION iface) {
99 100 101 102
	ICOM_THIS_MULTI(IDirectMusicCollectionImpl, CollectionVtbl, iface);
	return IDirectMusicCollectionImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
}

103
static ULONG WINAPI IDirectMusicCollectionImpl_IDirectMusicCollection_Release (LPDIRECTMUSICCOLLECTION iface) {
104 105 106 107
	ICOM_THIS_MULTI(IDirectMusicCollectionImpl, CollectionVtbl, iface);
	return IDirectMusicCollectionImpl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
}

108
/* IDirectMusicCollection Interface follow: */
109
static HRESULT WINAPI IDirectMusicCollectionImpl_IDirectMusicCollection_GetInstrument (LPDIRECTMUSICCOLLECTION iface, DWORD dwPatch, IDirectMusicInstrument** ppInstrument) {
110 111 112 113 114
	ICOM_THIS_MULTI(IDirectMusicCollectionImpl, CollectionVtbl, iface);
	DMUS_PRIVATE_INSTRUMENTENTRY *tmpEntry;
	struct list *listEntry;
	DWORD dwInstPatch;

115
	TRACE("(%p, %d, %p)\n", This, dwPatch, ppInstrument);
116 117 118 119 120 121 122 123 124
	
	LIST_FOR_EACH (listEntry, &This->Instruments) {
		tmpEntry = LIST_ENTRY(listEntry, DMUS_PRIVATE_INSTRUMENTENTRY, entry);
		IDirectMusicInstrument_GetPatch (tmpEntry->pInstrument, &dwInstPatch);
		if (dwPatch == dwInstPatch) {
			*ppInstrument = (LPDIRECTMUSICINSTRUMENT)tmpEntry->pInstrument;
			IDirectMusicInstrument_AddRef (tmpEntry->pInstrument);
			IDirectMusicInstrumentImpl_Custom_Load (tmpEntry->pInstrument, This->pStm); /* load instrument before returning it */
			TRACE(": returning instrument %p\n", *ppInstrument);
125 126
			return S_OK;
		}
127
			
128
	}
129
	TRACE(": instrument not found\n");
130 131
	
	return DMUS_E_INVALIDPATCH;
132 133
}

134
static HRESULT WINAPI IDirectMusicCollectionImpl_IDirectMusicCollection_EnumInstrument (LPDIRECTMUSICCOLLECTION iface, DWORD dwIndex, DWORD* pdwPatch, LPWSTR pwszName, DWORD dwNameLen) {
135
	ICOM_THIS_MULTI(IDirectMusicCollectionImpl, CollectionVtbl, iface);
136
	unsigned int r = 0;
137 138
	DMUS_PRIVATE_INSTRUMENTENTRY *tmpEntry;
	struct list *listEntry;
Peter Berg Larsen's avatar
Peter Berg Larsen committed
139
       DWORD dwLen;
140
		
141
	TRACE("(%p, %d, %p, %p, %d)\n", This, dwIndex, pdwPatch, pwszName, dwNameLen);
142 143 144 145 146
	LIST_FOR_EACH (listEntry, &This->Instruments) {
		tmpEntry = LIST_ENTRY(listEntry, DMUS_PRIVATE_INSTRUMENTENTRY, entry);
		if (r == dwIndex) {
			ICOM_NAME_MULTI (IDirectMusicInstrumentImpl, InstrumentVtbl, tmpEntry->pInstrument, pInstrument);
			IDirectMusicInstrument_GetPatch (tmpEntry->pInstrument, pdwPatch);
Peter Berg Larsen's avatar
Peter Berg Larsen committed
147 148 149 150 151
                       if (pwszName) {
                               dwLen = min(strlenW(pInstrument->wszName),dwNameLen-1);
                               memcpy (pwszName, pInstrument->wszName, dwLen * sizeof(WCHAR));
                               pwszName[dwLen] = '\0';
                       }
152 153 154
			return S_OK;
		}
		r++;		
155 156
	}
	
157
	return S_FALSE;
158 159
}

160
static const IDirectMusicCollectionVtbl DirectMusicCollection_Collection_Vtbl = {
161 162 163 164 165
	IDirectMusicCollectionImpl_IDirectMusicCollection_QueryInterface,
	IDirectMusicCollectionImpl_IDirectMusicCollection_AddRef,
	IDirectMusicCollectionImpl_IDirectMusicCollection_Release,
	IDirectMusicCollectionImpl_IDirectMusicCollection_GetInstrument,
	IDirectMusicCollectionImpl_IDirectMusicCollection_EnumInstrument
166 167
};

168
/* IDirectMusicCollectionImpl IDirectMusicObject part: */
169
static HRESULT WINAPI IDirectMusicCollectionImpl_IDirectMusicObject_QueryInterface (LPDIRECTMUSICOBJECT iface, REFIID riid, LPVOID *ppobj) {
170 171
	ICOM_THIS_MULTI(IDirectMusicCollectionImpl, ObjectVtbl, iface);
	return IDirectMusicCollectionImpl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
172 173
}

174
static ULONG WINAPI IDirectMusicCollectionImpl_IDirectMusicObject_AddRef (LPDIRECTMUSICOBJECT iface) {
175 176
	ICOM_THIS_MULTI(IDirectMusicCollectionImpl, ObjectVtbl, iface);
	return IDirectMusicCollectionImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
177 178
}

179
static ULONG WINAPI IDirectMusicCollectionImpl_IDirectMusicObject_Release (LPDIRECTMUSICOBJECT iface) {
180 181
	ICOM_THIS_MULTI(IDirectMusicCollectionImpl, ObjectVtbl, iface);
	return IDirectMusicCollectionImpl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
182 183
}

184
static HRESULT WINAPI IDirectMusicCollectionImpl_IDirectMusicObject_GetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
185
	ICOM_THIS_MULTI(IDirectMusicCollectionImpl, ObjectVtbl, iface);
186
	TRACE("(%p, %p)\n", This, pDesc);
187 188
	/* I think we shouldn't return pointer here since then values can be changed; it'd be a mess */
	memcpy (pDesc, This->pDesc, This->pDesc->dwSize);
189 190 191
	return S_OK;
}

192
static HRESULT WINAPI IDirectMusicCollectionImpl_IDirectMusicObject_SetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
193
	ICOM_THIS_MULTI(IDirectMusicCollectionImpl, ObjectVtbl, iface);
194
	TRACE("(%p, %p): setting descriptor:\n%s\n", This, pDesc, debugstr_DMUS_OBJECTDESC (pDesc));
195 196 197 198 199 200 201
	
	/* According to MSDN, we should copy only given values, not whole struct */	
	if (pDesc->dwValidData & DMUS_OBJ_OBJECT)
		memcpy (&This->pDesc->guidObject, &pDesc->guidObject, sizeof (pDesc->guidObject));
	if (pDesc->dwValidData & DMUS_OBJ_CLASS)
		memcpy (&This->pDesc->guidClass, &pDesc->guidClass, sizeof (pDesc->guidClass));		
	if (pDesc->dwValidData & DMUS_OBJ_NAME)
Peter Berg Larsen's avatar
Peter Berg Larsen committed
202
               lstrcpynW(This->pDesc->wszName, pDesc->wszName, DMUS_MAX_NAME);
203
	if (pDesc->dwValidData & DMUS_OBJ_CATEGORY)
Peter Berg Larsen's avatar
Peter Berg Larsen committed
204
               lstrcpynW(This->pDesc->wszCategory, pDesc->wszCategory, DMUS_MAX_CATEGORY);
205
	if (pDesc->dwValidData & DMUS_OBJ_FILENAME)
Peter Berg Larsen's avatar
Peter Berg Larsen committed
206
               lstrcpynW(This->pDesc->wszFileName, pDesc->wszFileName, DMUS_MAX_FILENAME);
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
	if (pDesc->dwValidData & DMUS_OBJ_VERSION)
		memcpy (&This->pDesc->vVersion, &pDesc->vVersion, sizeof (pDesc->vVersion));				
	if (pDesc->dwValidData & DMUS_OBJ_DATE)
		memcpy (&This->pDesc->ftDate, &pDesc->ftDate, sizeof (pDesc->ftDate));				
	if (pDesc->dwValidData & DMUS_OBJ_MEMORY) {
		memcpy (&This->pDesc->llMemLength, &pDesc->llMemLength, sizeof (pDesc->llMemLength));				
		memcpy (This->pDesc->pbMemData, pDesc->pbMemData, sizeof (pDesc->pbMemData));
	}
	if (pDesc->dwValidData & DMUS_OBJ_STREAM) {
		/* according to MSDN, we copy the stream */
		IStream_Clone (pDesc->pStream, &This->pDesc->pStream);	
	}
	
	/* add new flags */
	This->pDesc->dwValidData |= pDesc->dwValidData;
222 223 224 225

	return S_OK;
}

226
static HRESULT WINAPI IDirectMusicCollectionImpl_IDirectMusicObject_ParseDescriptor (LPDIRECTMUSICOBJECT iface, LPSTREAM pStream, LPDMUS_OBJECTDESC pDesc) {
227 228 229 230
	ICOM_THIS_MULTI(IDirectMusicCollectionImpl, ObjectVtbl, iface);
	DMUS_PRIVATE_CHUNK Chunk;
	DWORD StreamSize, StreamCount, ListSize[1], ListCount[1];
	LARGE_INTEGER liMove; /* used when skipping chunks */
231

232 233 234 235 236
	TRACE("(%p, %p, %p)\n", This, pStream, pDesc);
	
	/* FIXME: should this be determined from stream? */
	pDesc->dwValidData |= DMUS_OBJ_CLASS;
	memcpy (&pDesc->guidClass, &CLSID_DirectMusicCollection, sizeof(CLSID));
237
	
238
	IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
239
	TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
240 241 242 243 244 245 246 247 248 249 250
	switch (Chunk.fccID) {	
		case FOURCC_RIFF: {
			IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);				
			TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
			StreamSize = Chunk.dwSize - sizeof(FOURCC);
			StreamCount = 0;
			if (Chunk.fccID == mmioFOURCC('D','L','S',' ')) {
				TRACE_(dmfile)(": collection form\n");
				do {
					IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
					StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
251
					TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
					switch (Chunk.fccID) {
						case FOURCC_DLID: {
							TRACE_(dmfile)(": GUID chunk\n");
							pDesc->dwValidData |= DMUS_OBJ_OBJECT;
							IStream_Read (pStream, &pDesc->guidObject, Chunk.dwSize, NULL);
							break;
						}
						case DMUS_FOURCC_VERSION_CHUNK: {
							TRACE_(dmfile)(": version chunk\n");
							pDesc->dwValidData |= DMUS_OBJ_VERSION;
							IStream_Read (pStream, &pDesc->vVersion, Chunk.dwSize, NULL);
							break;
						}
						case DMUS_FOURCC_CATEGORY_CHUNK: {
							TRACE_(dmfile)(": category chunk\n");
							pDesc->dwValidData |= DMUS_OBJ_CATEGORY;
							IStream_Read (pStream, pDesc->wszCategory, Chunk.dwSize, NULL);
							break;
						}
						case FOURCC_LIST: {
							IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);				
							TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
							ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
							ListCount[0] = 0;
							switch (Chunk.fccID) {
								/* pure INFO list, such can be found in dls collections */
								case mmioFOURCC('I','N','F','O'): {
									TRACE_(dmfile)(": INFO list\n");
									do {
										IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
										ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
283
										TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
										switch (Chunk.fccID) {
											case mmioFOURCC('I','N','A','M'):{
												CHAR szName[DMUS_MAX_NAME];												
												TRACE_(dmfile)(": name chunk\n");
												pDesc->dwValidData |= DMUS_OBJ_NAME;
												IStream_Read (pStream, szName, Chunk.dwSize, NULL);
												MultiByteToWideChar (CP_ACP, 0, szName, -1, pDesc->wszName, DMUS_MAX_NAME);
												if (even_or_odd(Chunk.dwSize)) {
													ListCount[0] ++;
													liMove.QuadPart = 1;
													IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
												}
												break;
											}
											case mmioFOURCC('I','A','R','T'): {
												TRACE_(dmfile)(": artist chunk (ignored)\n");
												if (even_or_odd(Chunk.dwSize)) {
													ListCount[0] ++;
													Chunk.dwSize++;
												}
												liMove.QuadPart = Chunk.dwSize;
												IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
												break;
											}
											case mmioFOURCC('I','C','O','P'): {
												TRACE_(dmfile)(": copyright chunk (ignored)\n");
												if (even_or_odd(Chunk.dwSize)) {
													ListCount[0] ++;
													Chunk.dwSize++;
												}
												liMove.QuadPart = Chunk.dwSize;
												IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
												break;
											}
											case mmioFOURCC('I','S','B','J'): {
												TRACE_(dmfile)(": subject chunk (ignored)\n");
												if (even_or_odd(Chunk.dwSize)) {
													ListCount[0] ++;
													Chunk.dwSize++;
												}
												liMove.QuadPart = Chunk.dwSize;
												IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
												break;
											}
											case mmioFOURCC('I','C','M','T'): {
												TRACE_(dmfile)(": comment chunk (ignored)\n");
												if (even_or_odd(Chunk.dwSize)) {
													ListCount[0] ++;
													Chunk.dwSize++;
												}
												liMove.QuadPart = Chunk.dwSize;
												IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
												break;
											}
											default: {
												TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
												if (even_or_odd(Chunk.dwSize)) {
													ListCount[0] ++;
													Chunk.dwSize++;
												}
												liMove.QuadPart = Chunk.dwSize;
												IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
												break;						
											}
										}
349
										TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
									} while (ListCount[0] < ListSize[0]);
									break;
								}
								default: {
									TRACE_(dmfile)(": unknown (skipping)\n");
									liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
									IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
									break;						
								}
							}
							break;
						}	
						default: {
							TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
							liMove.QuadPart = Chunk.dwSize;
							IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
							break;						
						}
					}
369
					TRACE_(dmfile)(": StreamCount[0] = %d < StreamSize[0] = %d\n", StreamCount, StreamSize);
370 371 372 373 374 375 376 377 378 379
				} while (StreamCount < StreamSize);
			} else {
				TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
				liMove.QuadPart = StreamSize;
				IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
				return E_FAIL;
			}
		
			TRACE_(dmfile)(": reading finished\n");
			break;
380
		}
381 382 383 384 385 386 387 388
		default: {
			TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
			liMove.QuadPart = Chunk.dwSize;
			IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
			return DMUS_E_INVALIDFILE;
		}
	}	
	
389
	TRACE(": returning descriptor:\n%s\n", debugstr_DMUS_OBJECTDESC (pDesc));
390
	
391
	return S_OK;
392 393
}

394
static const IDirectMusicObjectVtbl DirectMusicCollection_Object_Vtbl = {
395 396 397 398 399 400 401
	IDirectMusicCollectionImpl_IDirectMusicObject_QueryInterface,
	IDirectMusicCollectionImpl_IDirectMusicObject_AddRef,
	IDirectMusicCollectionImpl_IDirectMusicObject_Release,
	IDirectMusicCollectionImpl_IDirectMusicObject_GetDescriptor,
	IDirectMusicCollectionImpl_IDirectMusicObject_SetDescriptor,
	IDirectMusicCollectionImpl_IDirectMusicObject_ParseDescriptor
};
402

403
/* IDirectMusicCollectionImpl IPersistStream part: */
404
static HRESULT WINAPI IDirectMusicCollectionImpl_IPersistStream_QueryInterface (LPPERSISTSTREAM iface, REFIID riid, LPVOID *ppobj) {
405 406
	ICOM_THIS_MULTI(IDirectMusicCollectionImpl, PersistStreamVtbl, iface);
	return IDirectMusicCollectionImpl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
407
}
408

409
static ULONG WINAPI IDirectMusicCollectionImpl_IPersistStream_AddRef (LPPERSISTSTREAM iface) {
410 411
	ICOM_THIS_MULTI(IDirectMusicCollectionImpl, PersistStreamVtbl, iface);
	return IDirectMusicCollectionImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
412 413
}

414
static ULONG WINAPI IDirectMusicCollectionImpl_IPersistStream_Release (LPPERSISTSTREAM iface) {
415 416
	ICOM_THIS_MULTI(IDirectMusicCollectionImpl, PersistStreamVtbl, iface);
	return IDirectMusicCollectionImpl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
417 418
}

419
static HRESULT WINAPI IDirectMusicCollectionImpl_IPersistStream_GetClassID (LPPERSISTSTREAM iface, CLSID* pClassID) {
420 421 422
	return E_NOTIMPL;
}

423
static HRESULT WINAPI IDirectMusicCollectionImpl_IPersistStream_IsDirty (LPPERSISTSTREAM iface) {
424 425 426
	return E_NOTIMPL;
}

427
static HRESULT WINAPI IDirectMusicCollectionImpl_IPersistStream_Load (LPPERSISTSTREAM iface, IStream* pStm) {
428
	ICOM_THIS_MULTI(IDirectMusicCollectionImpl, PersistStreamVtbl, iface);
429

430 431 432 433
	DMUS_PRIVATE_CHUNK Chunk;
	DWORD StreamSize, StreamCount, ListSize[3], ListCount[3];
	LARGE_INTEGER liMove; /* used when skipping chunks */
	ULARGE_INTEGER dlibCollectionPosition, dlibInstrumentPosition, dlibWavePoolPosition;
434
	
435 436 437 438 439 440 441
	IStream_AddRef (pStm); /* add count for later references */
	liMove.QuadPart = 0;
	IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, &dlibCollectionPosition); /* store offset, in case it'll be needed later */
	This->liCollectionPosition.QuadPart = dlibCollectionPosition.QuadPart;
	This->pStm = pStm;
	
	IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
442
	TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
443 444 445 446 447 448 449 450 451 452 453 454
	switch (Chunk.fccID) {	
		case FOURCC_RIFF: {
			IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);				
			TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
			StreamSize = Chunk.dwSize - sizeof(FOURCC);
			StreamCount = 0;
			switch (Chunk.fccID) {
				case FOURCC_DLS: {
					TRACE_(dmfile)(": collection form\n");
					do {
						IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
						StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
455
						TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
						switch (Chunk.fccID) {
							case FOURCC_COLH: {
								TRACE_(dmfile)(": collection header chunk\n");
								This->pHeader = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, Chunk.dwSize);
								IStream_Read (pStm, This->pHeader, Chunk.dwSize, NULL);
								break;								
							}
							case FOURCC_DLID: {
								TRACE_(dmfile)(": DLID (GUID) chunk\n");
								This->pDesc->dwValidData |= DMUS_OBJ_OBJECT;
								IStream_Read (pStm, &This->pDesc->guidObject, Chunk.dwSize, NULL);
								break;
							}
							case FOURCC_VERS: {
								TRACE_(dmfile)(": version chunk\n");
								This->pDesc->dwValidData |= DMUS_OBJ_VERSION;
								IStream_Read (pStm, &This->pDesc->vVersion, Chunk.dwSize, NULL);
								break;
							}
							case FOURCC_PTBL: {
								TRACE_(dmfile)(": pool table chunk\n");
								This->pPoolTable = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(POOLTABLE));
								IStream_Read (pStm, This->pPoolTable, sizeof(POOLTABLE), NULL);
								Chunk.dwSize -= sizeof(POOLTABLE);
								This->pPoolCues = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, This->pPoolTable->cCues*sizeof(POOLCUE));
								IStream_Read (pStm, This->pPoolCues, Chunk.dwSize, NULL);
								break;
							}
							case FOURCC_LIST: {
								IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);				
								TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
								ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
								ListCount[0] = 0;
								switch (Chunk.fccID) {
									case mmioFOURCC('I','N','F','O'): {
										TRACE_(dmfile)(": INFO list\n");
										do {
											IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
											ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
495
											TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
											switch (Chunk.fccID) {
												case mmioFOURCC('I','N','A','M'): {
													CHAR szName[DMUS_MAX_NAME];
													TRACE_(dmfile)(": name chunk\n");
													This->pDesc->dwValidData |= DMUS_OBJ_NAME;
													IStream_Read (pStm, szName, Chunk.dwSize, NULL);
													MultiByteToWideChar (CP_ACP, 0, szName, -1, This->pDesc->wszName, DMUS_MAX_NAME);
													if (even_or_odd(Chunk.dwSize)) {
														ListCount[0] ++;
														liMove.QuadPart = 1;
														IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
													}
													break;
												}
												case mmioFOURCC('I','A','R','T'): {
													TRACE_(dmfile)(": artist chunk (ignored)\n");
													if (even_or_odd(Chunk.dwSize)) {
														ListCount[0] ++;
														Chunk.dwSize++;
													}
													liMove.QuadPart = Chunk.dwSize;
													IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
													break;
												}
												case mmioFOURCC('I','C','O','P'): {
													TRACE_(dmfile)(": copyright chunk\n");
													This->szCopyright = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, Chunk.dwSize);
													IStream_Read (pStm, This->szCopyright, Chunk.dwSize, NULL);
													if (even_or_odd(Chunk.dwSize)) {
														ListCount[0] ++;
														liMove.QuadPart = 1;
														IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
													}
													break;
												}
												case mmioFOURCC('I','S','B','J'): {
													TRACE_(dmfile)(": subject chunk (ignored)\n");
													if (even_or_odd(Chunk.dwSize)) {
														ListCount[0] ++;
														Chunk.dwSize++;
													}
													liMove.QuadPart = Chunk.dwSize;
													IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
													break;
												}
												case mmioFOURCC('I','C','M','T'): {
													TRACE_(dmfile)(": comment chunk (ignored)\n");
													if (even_or_odd(Chunk.dwSize)) {
														ListCount[0] ++;
														Chunk.dwSize++;
													}
													liMove.QuadPart = Chunk.dwSize;
													IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
													break;
												}
												default: {
													TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
													if (even_or_odd(Chunk.dwSize)) {
														ListCount[0] ++;
														Chunk.dwSize++;
													}
													liMove.QuadPart = Chunk.dwSize;
													IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
													break;						
												}
											}
562
											TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
										} while (ListCount[0] < ListSize[0]);
										break;
									}
									case FOURCC_WVPL: {
										TRACE_(dmfile)(": wave pool list (mark & skip)\n");
										liMove.QuadPart = 0;
										IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, &dlibWavePoolPosition); /* store position */
										This->liWavePoolTablePosition.QuadPart = dlibWavePoolPosition.QuadPart;
										liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
										IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
										break;	
									}
									case FOURCC_LINS: {
										TRACE_(dmfile)(": instruments list\n");
										do {
											IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
											ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
580
											TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
											switch (Chunk.fccID) {
												case FOURCC_LIST: {
													IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);				
													TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
													ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
													ListCount[1] = 0;													
													switch (Chunk.fccID) {
														case FOURCC_INS: {
															LPDMUS_PRIVATE_INSTRUMENTENTRY pNewInstrument = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(DMUS_PRIVATE_INSTRUMENTENTRY));
															TRACE_(dmfile)(": instrument list\n");
															DMUSIC_CreateDirectMusicInstrumentImpl (&IID_IDirectMusicInstrument, (LPVOID*)&pNewInstrument->pInstrument, NULL); /* only way to create this one... even M$ does it discretly */
                                                                                                                        {
                                                                                                                            ICOM_NAME_MULTI (IDirectMusicInstrumentImpl, InstrumentVtbl, pNewInstrument->pInstrument, pInstrument);
                                                                                                                            liMove.QuadPart = 0;
                                                                                                                            IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, &dlibInstrumentPosition);
                                                                                                                            pInstrument->liInstrumentPosition.QuadPart = dlibInstrumentPosition.QuadPart - (2*sizeof(FOURCC) + sizeof(DWORD)); /* store offset, it'll be needed later */

                                                                                                                            do {
																IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
																ListCount[1] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
601
																TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
																switch (Chunk.fccID) {
																	case FOURCC_INSH: {
																		TRACE_(dmfile)(": instrument header chunk\n");
																		pInstrument->pHeader = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, Chunk.dwSize);
																		IStream_Read (pStm, pInstrument->pHeader, Chunk.dwSize, NULL);
																		break;	
																	}
																	case FOURCC_DLID: {
																		TRACE_(dmfile)(": DLID (GUID) chunk\n");
																		pInstrument->pInstrumentID = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, Chunk.dwSize);
																		IStream_Read (pStm, pInstrument->pInstrumentID, Chunk.dwSize, NULL);
																		break;
																	}
																	case FOURCC_LIST: {
																		IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);				
																		TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
																		ListSize[2] = Chunk.dwSize - sizeof(FOURCC);
																		ListCount[2] = 0;
																		switch (Chunk.fccID) {
																			default: {
																				TRACE_(dmfile)(": unknown (skipping)\n");
																				liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
																				IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
																				break;						
																			}
																		}
																		break;
																	}				
																	default: {
																		TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
																		liMove.QuadPart = Chunk.dwSize;
																		IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
																		break;						
																	}
																}
637
																TRACE_(dmfile)(": ListCount[1] = %d < ListSize[1] = %d\n", ListCount[1], ListSize[1]);
638 639 640 641 642
                                                                                                                            } while (ListCount[1] < ListSize[1]);
                                                                                                                            /* DEBUG: dumps whole instrument object tree: */
                                                                                                                            if (TRACE_ON(dmusic)) {
																TRACE("*** IDirectMusicInstrument (%p) ***\n", pInstrument);
																if (pInstrument->pInstrumentID)
643
																	TRACE(" - GUID = %s\n", debugstr_dmguid(pInstrument->pInstrumentID));
644 645
																
																TRACE(" - Instrument header:\n");
646
																TRACE("    - cRegions: %d\n", pInstrument->pHeader->cRegions);
647
																TRACE("    - Locale:\n");
648 649 650
																TRACE("       - ulBank: %d\n", pInstrument->pHeader->Locale.ulBank);
																TRACE("       - ulInstrument: %d\n", pInstrument->pHeader->Locale.ulInstrument);
																TRACE("       => dwPatch: %d\n", MIDILOCALE2Patch(&pInstrument->pHeader->Locale));
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
                                                                                                                            }
                                                                                                                            list_add_tail (&This->Instruments, &pNewInstrument->entry);
                                                                                                                        }
															break;
														}
													}
													break;
												}
												default: {
													TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
													liMove.QuadPart = Chunk.dwSize;
													IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
													break;						
												}
											}
666
											TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
										} while (ListCount[0] < ListSize[0]);
										break;
									}
									default: {
										TRACE_(dmfile)(": unknown (skipping)\n");
										liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
										IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
										break;						
									}
								}
								break;
							}	
							default: {
								TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
								liMove.QuadPart = Chunk.dwSize;
								IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
								break;						
							}
						}
686
						TRACE_(dmfile)(": StreamCount = %d < StreamSize = %d\n", StreamCount, StreamSize);
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
					} while (StreamCount < StreamSize);
					break;
				}
				default: {
					TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
					liMove.QuadPart = StreamSize;
					IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
					return E_FAIL;
				}
			}
			TRACE_(dmfile)(": reading finished\n");
			break;
		}
		default: {
			TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
			liMove.QuadPart = Chunk.dwSize;
			IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
			return E_FAIL;
		}
	}

	/* DEBUG: dumps whole collection object tree: */
	if (TRACE_ON(dmusic)) {
		int r = 0;
		DMUS_PRIVATE_INSTRUMENTENTRY *tmpEntry;
		struct list *listEntry;

		TRACE("*** IDirectMusicCollection (%p) ***\n", This->CollectionVtbl);
		if (This->pDesc->dwValidData & DMUS_OBJ_OBJECT)
716
			TRACE(" - GUID = %s\n", debugstr_dmguid(&This->pDesc->guidObject));
717
		if (This->pDesc->dwValidData & DMUS_OBJ_VERSION)
718
			TRACE(" - Version = %i,%i,%i,%i\n", (This->pDesc->vVersion.dwVersionMS >> 8) & 0x0000FFFF, This->pDesc->vVersion.dwVersionMS & 0x0000FFFF,
719
				(This->pDesc->vVersion.dwVersionLS >> 8) & 0x0000FFFF, This->pDesc->vVersion.dwVersionLS & 0x0000FFFF);
720 721 722 723
		if (This->pDesc->dwValidData & DMUS_OBJ_NAME)
			TRACE(" - Name = %s\n", debugstr_w(This->pDesc->wszName));
		
		TRACE(" - Collection header:\n");
724
		TRACE("    - cInstruments: %d\n", This->pHeader->cInstruments);
725 726 727 728 729 730 731
		TRACE(" - Instruments:\n");
		
		LIST_FOR_EACH (listEntry, &This->Instruments) {
			tmpEntry = LIST_ENTRY( listEntry, DMUS_PRIVATE_INSTRUMENTENTRY, entry );
			TRACE("    - Instrument[%i]: %p\n", r, tmpEntry->pInstrument);
			r++;
		}
732 733 734 735 736
	}
	
	return S_OK;
}

737
static HRESULT WINAPI IDirectMusicCollectionImpl_IPersistStream_Save (LPPERSISTSTREAM iface, IStream* pStm, BOOL fClearDirty) {
738 739 740
	return E_NOTIMPL;
}

741
static HRESULT WINAPI IDirectMusicCollectionImpl_IPersistStream_GetSizeMax (LPPERSISTSTREAM iface, ULARGE_INTEGER* pcbSize) {
742 743 744
	return E_NOTIMPL;
}

745
static const IPersistStreamVtbl DirectMusicCollection_PersistStream_Vtbl = {
746 747 748 749 750 751 752 753
	IDirectMusicCollectionImpl_IPersistStream_QueryInterface,
	IDirectMusicCollectionImpl_IPersistStream_AddRef,
	IDirectMusicCollectionImpl_IPersistStream_Release,
	IDirectMusicCollectionImpl_IPersistStream_GetClassID,
	IDirectMusicCollectionImpl_IPersistStream_IsDirty,
	IDirectMusicCollectionImpl_IPersistStream_Load,
	IDirectMusicCollectionImpl_IPersistStream_Save,
	IDirectMusicCollectionImpl_IPersistStream_GetSizeMax
754
};
755 756 757 758 759 760 761 762


/* for ClassFactory */
HRESULT WINAPI DMUSIC_CreateDirectMusicCollectionImpl (LPCGUID lpcGUID, LPVOID* ppobj, LPUNKNOWN pUnkOuter) {
	IDirectMusicCollectionImpl* obj;
	
	obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicCollectionImpl));
	if (NULL == obj) {
763
		*ppobj = NULL;
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
		return E_OUTOFMEMORY;
	}
	obj->UnknownVtbl = &DirectMusicCollection_Unknown_Vtbl;
	obj->CollectionVtbl = &DirectMusicCollection_Collection_Vtbl;
	obj->ObjectVtbl = &DirectMusicCollection_Object_Vtbl;
	obj->PersistStreamVtbl = &DirectMusicCollection_PersistStream_Vtbl;
	obj->pDesc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DMUS_OBJECTDESC));
	DM_STRUCT_INIT(obj->pDesc);
	obj->pDesc->dwValidData |= DMUS_OBJ_CLASS;
	memcpy (&obj->pDesc->guidClass, &CLSID_DirectMusicCollection, sizeof (CLSID));
	obj->ref = 0; /* will be inited by QueryInterface */
	list_init (&obj->Instruments);
	
	return IDirectMusicCollectionImpl_IUnknown_QueryInterface ((LPUNKNOWN)&obj->UnknownVtbl, lpcGUID, ppobj);
}