2 * IDirectMusicLoaderImpl
4 * Copyright (C) 2003-2004 Rok Mandeljc
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "dmloader_private.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(dmloader);
25 static inline IDirectMusicLoaderImpl* impl_from_IDirectMusicLoader8(IDirectMusicLoader8 *iface)
27 return CONTAINING_RECORD(iface, IDirectMusicLoaderImpl, IDirectMusicLoader8_iface);
30 static HRESULT DMUSIC_InitLoaderSettings(IDirectMusicLoader8 *iface);
31 static HRESULT DMUSIC_GetLoaderSettings(IDirectMusicLoader8 *iface, REFGUID class_id, WCHAR *search_path, BOOL *cache);
32 static HRESULT DMUSIC_SetLoaderSettings(IDirectMusicLoader8 *iface, REFGUID class_id, WCHAR *search_path, BOOL *cache);
34 static HRESULT DMUSIC_CopyDescriptor(DMUS_OBJECTDESC *pDst, DMUS_OBJECTDESC *pSrc)
36 if (TRACE_ON(dmloader))
37 dump_DMUS_OBJECTDESC(pSrc);
39 /* copy field by field */
40 if (pSrc->dwValidData & DMUS_OBJ_CLASS) pDst->guidClass = pSrc->guidClass;
41 if (pSrc->dwValidData & DMUS_OBJ_OBJECT) pDst->guidObject = pSrc->guidObject;
42 if (pSrc->dwValidData & DMUS_OBJ_DATE) pDst->ftDate = pSrc->ftDate;
43 if (pSrc->dwValidData & DMUS_OBJ_VERSION) pDst->vVersion = pSrc->vVersion;
44 if (pSrc->dwValidData & DMUS_OBJ_NAME) strcpyW (pDst->wszName, pSrc->wszName);
45 if (pSrc->dwValidData & DMUS_OBJ_CATEGORY) strcpyW (pDst->wszCategory, pSrc->wszCategory);
46 if (pSrc->dwValidData & DMUS_OBJ_FILENAME) strcpyW (pDst->wszFileName, pSrc->wszFileName);
47 if (pSrc->dwValidData & DMUS_OBJ_STREAM) IStream_Clone (pSrc->pStream, &pDst->pStream);
48 if (pSrc->dwValidData & DMUS_OBJ_MEMORY) {
49 pDst->pbMemData = pSrc->pbMemData;
50 pDst->llMemLength = pSrc->llMemLength;
53 pDst->dwValidData |= pSrc->dwValidData;
58 static BOOL DMUSIC_IsValidLoadableClass (REFCLSID pClassID) {
59 if (IsEqualCLSID(pClassID, &CLSID_DirectMusicAudioPathConfig) ||
60 IsEqualCLSID(pClassID, &CLSID_DirectMusicBand) ||
61 IsEqualCLSID(pClassID, &CLSID_DirectMusicContainer) ||
62 IsEqualCLSID(pClassID, &CLSID_DirectMusicCollection) ||
63 IsEqualCLSID(pClassID, &CLSID_DirectMusicChordMap) ||
64 IsEqualCLSID(pClassID, &CLSID_DirectMusicSegment) ||
65 IsEqualCLSID(pClassID, &CLSID_DirectMusicScript) ||
66 IsEqualCLSID(pClassID, &CLSID_DirectMusicSong) ||
67 IsEqualCLSID(pClassID, &CLSID_DirectMusicStyle) ||
68 IsEqualCLSID(pClassID, &CLSID_DirectMusicGraph) ||
69 IsEqualCLSID(pClassID, &CLSID_DirectSoundWave) ||
70 IsEqualCLSID(pClassID, &GUID_DirectMusicAllTypes))
76 /*****************************************************************************
77 * IDirectMusicLoaderImpl implementation
79 /* IUnknown/IDirectMusicLoader(8) part: */
81 static HRESULT WINAPI IDirectMusicLoaderImpl_QueryInterface(IDirectMusicLoader8 *iface, REFIID riid, void **ppobj)
83 IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
85 TRACE("(%p, %s, %p)\n",This, debugstr_dmguid(riid), ppobj);
86 if (IsEqualIID (riid, &IID_IUnknown) ||
87 IsEqualIID (riid, &IID_IDirectMusicLoader) ||
88 IsEqualIID (riid, &IID_IDirectMusicLoader8)) {
89 IDirectMusicLoader_AddRef (iface);
94 WARN(": not found\n");
98 static ULONG WINAPI IDirectMusicLoaderImpl_AddRef(IDirectMusicLoader8 *iface)
100 IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
101 ULONG ref = InterlockedIncrement(&This->ref);
103 TRACE("(%p)->(): new ref = %u\n", iface, ref);
108 static ULONG WINAPI IDirectMusicLoaderImpl_Release(IDirectMusicLoader8 *iface)
110 IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
111 ULONG ref = InterlockedDecrement(&This->ref);
113 TRACE("(%p)->(): new ref = %u\n", iface, ref);
116 /* Firstly, release the cache */
117 IDirectMusicLoader8_ClearCache(iface, &GUID_DirectMusicAllTypes);
118 /* FIXME: Release all allocated entries */
119 HeapFree(GetProcessHeap(), 0, This);
126 static HRESULT WINAPI IDirectMusicLoaderImpl_GetObject(IDirectMusicLoader8 *iface, DMUS_OBJECTDESC *pDesc, REFIID riid, void **ppv)
128 IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
129 HRESULT result = S_OK;
130 HRESULT ret = S_OK; /* used at the end of function, to determine whether everything went OK */
133 LPWINE_LOADER_ENTRY pObjectEntry = NULL;
135 IPersistStream* pPersistStream = NULL;
137 LPDIRECTMUSICOBJECT pObject;
138 DMUS_OBJECTDESC GotDesc;
141 TRACE("(%p)->(%p, %s, %p)\n", This, pDesc, debugstr_dmguid(riid), ppv);
143 if (TRACE_ON(dmloader))
144 dump_DMUS_OBJECTDESC(pDesc);
146 /* sometimes it happens that guidClass is missing... which is a BadThingTM */
147 if (!(pDesc->dwValidData & DMUS_OBJ_CLASS)) {
148 ERR(": guidClass not valid but needed\n");
150 return DMUS_E_LOADER_NOCLASSID;
153 /* OK, first we iterate through the list of objects we know about; these are either loaded (GetObject, LoadObjectFromFile)
154 or set via SetObject; */
155 TRACE(": looking if we have object in the cache or if it can be found via alias\n");
156 LIST_FOR_EACH(pEntry, This->pObjects) {
157 LPWINE_LOADER_ENTRY pExistingEntry = LIST_ENTRY(pEntry, WINE_LOADER_ENTRY, entry);
158 if ((pDesc->dwValidData & DMUS_OBJ_OBJECT) &&
159 (pExistingEntry->Desc.dwValidData & DMUS_OBJ_OBJECT) &&
160 IsEqualGUID (&pDesc->guidObject, &pExistingEntry->Desc.guidObject)) {
161 TRACE(": found it by object GUID\n");
162 /* I suppose such stuff can happen only when GUID for object is given (GUID_DefaultGMCollection) */
163 if (pExistingEntry->bInvalidDefaultDLS) {
164 TRACE(": found faulty default DLS collection... enabling M$ compliant behaviour\n");
165 return DMUS_E_LOADER_NOFILENAME;
167 if (pExistingEntry->Desc.dwValidData & DMUS_OBJ_LOADED) {
168 TRACE(": already loaded\n");
169 return IDirectMusicObject_QueryInterface (pExistingEntry->pObject, riid, ppv);
171 TRACE(": not loaded yet\n");
172 pObjectEntry = pExistingEntry;
175 else if ((pDesc->dwValidData & (DMUS_OBJ_FILENAME | DMUS_OBJ_FULLPATH)) &&
176 (pExistingEntry->Desc.dwValidData & (DMUS_OBJ_FILENAME | DMUS_OBJ_FULLPATH)) &&
177 !strncmpW (pDesc->wszFileName, pExistingEntry->Desc.wszFileName, DMUS_MAX_FILENAME)) {
178 TRACE(": found it by fullpath filename\n");
179 if (pExistingEntry->Desc.dwValidData & DMUS_OBJ_LOADED) {
180 TRACE(": already loaded\n");
181 return IDirectMusicObject_QueryInterface (pExistingEntry->pObject, riid, ppv);
183 TRACE(": not loaded yet\n");
184 pObjectEntry = pExistingEntry;
187 else if ((pDesc->dwValidData & (DMUS_OBJ_NAME | DMUS_OBJ_CATEGORY)) &&
188 (pExistingEntry->Desc.dwValidData & (DMUS_OBJ_NAME | DMUS_OBJ_CATEGORY)) &&
189 !strncmpW (pDesc->wszName, pExistingEntry->Desc.wszName, DMUS_MAX_NAME) &&
190 !strncmpW (pDesc->wszCategory, pExistingEntry->Desc.wszCategory, DMUS_MAX_CATEGORY)) {
191 TRACE(": found it by name and category\n");
192 if (pExistingEntry->Desc.dwValidData & DMUS_OBJ_LOADED) {
193 TRACE(": already loaded\n");
194 return IDirectMusicObject_QueryInterface (pExistingEntry->pObject, riid, ppv);
196 TRACE(": not loaded yet\n");
197 pObjectEntry = pExistingEntry;
200 else if ((pDesc->dwValidData & DMUS_OBJ_NAME) &&
201 (pExistingEntry->Desc.dwValidData & DMUS_OBJ_NAME) &&
202 !strncmpW (pDesc->wszName, pExistingEntry->Desc.wszName, DMUS_MAX_NAME)) {
203 TRACE(": found it by name\n");
204 if (pExistingEntry->Desc.dwValidData & DMUS_OBJ_LOADED) {
205 TRACE(": already loaded\n");
206 return IDirectMusicObject_QueryInterface (pExistingEntry->pObject, riid, ppv);
208 TRACE(": not loaded yet\n");
209 pObjectEntry = pExistingEntry;
212 else if ((pDesc->dwValidData & DMUS_OBJ_FILENAME) &&
213 (pExistingEntry->Desc.dwValidData & DMUS_OBJ_FILENAME) &&
214 !strncmpW (pDesc->wszFileName, pExistingEntry->Desc.wszFileName, DMUS_MAX_FILENAME)) {
215 TRACE(": found it by filename\n");
216 if (pExistingEntry->Desc.dwValidData & DMUS_OBJ_LOADED) {
217 TRACE(": already loaded\n");
218 return IDirectMusicObject_QueryInterface (pExistingEntry->pObject, riid, ppv);
220 TRACE(": not loaded yet\n");
221 pObjectEntry = pExistingEntry;
226 /* basically, if we found alias, we use its descriptor to load...
227 else we use info we were given */
229 TRACE(": found alias entry for requested object... using stored info\n");
230 /* I think in certain cases it can happen that entry's descriptor lacks info about
231 where to load from (e.g.: if we loaded from stream and then released object
232 from cache; then only its CLSID, GUID and perhaps name are left); so just
233 overwrite whatever info the entry has (since it ought to be 100% correct) */
234 DMUSIC_CopyDescriptor (pDesc, &pObjectEntry->Desc);
235 /*pDesc = &pObjectEntry->Desc; */ /* FIXME: is this OK? */
237 TRACE(": no cache/alias entry found for requested object\n");
240 if (pDesc->dwValidData & DMUS_OBJ_URL) {
241 TRACE(": loading from URLs not supported yet\n");
242 return DMUS_E_LOADER_FORMATNOTSUPPORTED;
244 else if (pDesc->dwValidData & DMUS_OBJ_FILENAME) {
245 /* load object from file */
246 /* generate filename; if it's full path, don't add search
247 directory path, otherwise do */
248 WCHAR wszFileName[MAX_PATH];
250 if (pDesc->dwValidData & DMUS_OBJ_FULLPATH) {
251 lstrcpyW(wszFileName, pDesc->wszFileName);
253 WCHAR *p, wszSearchPath[MAX_PATH];
254 DMUSIC_GetLoaderSettings (iface, &pDesc->guidClass, wszSearchPath, NULL);
255 lstrcpyW(wszFileName, wszSearchPath);
256 p = wszFileName + lstrlenW(wszFileName);
257 if (p > wszFileName && p[-1] != '\\') *p++ = '\\';
258 strcpyW(p, pDesc->wszFileName);
260 TRACE(": loading from file (%s)\n", debugstr_w(wszFileName));
261 /* create stream and associate it with file */
262 result = DMUSIC_CreateDirectMusicLoaderFileStream ((LPVOID*)&pStream);
263 if (FAILED(result)) {
264 ERR(": could not create file stream\n");
267 result = IDirectMusicLoaderFileStream_Attach (pStream, wszFileName, iface);
268 if (FAILED(result)) {
269 ERR(": could not attach stream to file\n");
270 IStream_Release (pStream);
274 else if (pDesc->dwValidData & DMUS_OBJ_MEMORY) {
275 /* load object from resource */
276 TRACE(": loading from resource\n");
277 /* create stream and associate it with given resource */
278 result = DMUSIC_CreateDirectMusicLoaderResourceStream ((LPVOID*)&pStream);
279 if (FAILED(result)) {
280 ERR(": could not create resource stream\n");
283 result = IDirectMusicLoaderResourceStream_Attach (pStream, pDesc->pbMemData, pDesc->llMemLength, 0, iface);
284 if (FAILED(result)) {
285 ERR(": could not attach stream to resource\n");
286 IStream_Release (pStream);
290 else if (pDesc->dwValidData & DMUS_OBJ_STREAM) {
291 /* load object from stream */
292 TRACE(": loading from stream\n");
293 /* create universal stream and associate it with given one */
294 result = DMUSIC_CreateDirectMusicLoaderGenericStream ((LPVOID*)&pStream);
295 if (FAILED(result)) {
296 ERR(": could not create generic stream\n");
299 result = IDirectMusicLoaderGenericStream_Attach (pStream, pDesc->pStream, iface);
300 if (FAILED(result)) {
301 ERR(": failed to attach stream\n");
302 IStream_Release (pStream);
306 /* nowhere to load from */
307 FIXME(": unknown/unsupported way of loading\n");
308 return DMUS_E_LOADER_NOFILENAME; /* test shows this is returned */
312 result = CoCreateInstance (&pDesc->guidClass, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusicObject, (LPVOID*)&pObject);
313 if (FAILED(result)) {
314 ERR(": could not create object\n");
317 /* acquire PersistStream interface */
318 result = IDirectMusicObject_QueryInterface (pObject, &IID_IPersistStream, (LPVOID*)&pPersistStream);
319 if (FAILED(result)) {
320 ERR("failed to Query\n");
324 result = IPersistStream_Load (pPersistStream, pStream);
325 if (result != S_OK) {
326 WARN(": failed to (completely) load object (%s)\n", debugstr_dmreturn(result));
330 DM_STRUCT_INIT(&GotDesc);
331 result = IDirectMusicObject_GetDescriptor (pObject, &GotDesc);
332 /* set filename (if we loaded via filename) */
333 if (pDesc->dwValidData & DMUS_OBJ_FILENAME) {
334 GotDesc.dwValidData |= (pDesc->dwValidData & (DMUS_OBJ_FILENAME | DMUS_OBJ_FULLPATH));
335 strcpyW (GotDesc.wszFileName, pDesc->wszFileName);
337 if (FAILED(result)) {
338 ERR(": failed to get descriptor\n");
341 /* release all loading related stuff */
342 IStream_Release (pStream);
343 IPersistStream_Release (pPersistStream);
345 /* add object to cache/overwrite existing info (if cache is enabled) */
346 DMUSIC_GetLoaderSettings (iface, &pDesc->guidClass, NULL, &bCache);
349 pObjectEntry = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(WINE_LOADER_ENTRY));
350 DM_STRUCT_INIT(&pObjectEntry->Desc);
351 DMUSIC_CopyDescriptor (&pObjectEntry->Desc, &GotDesc);
352 pObjectEntry->pObject = pObject;
353 pObjectEntry->bInvalidDefaultDLS = FALSE;
354 list_add_head (This->pObjects, &pObjectEntry->entry);
356 DMUSIC_CopyDescriptor (&pObjectEntry->Desc, &GotDesc);
357 pObjectEntry->pObject = pObject;
358 pObjectEntry->bInvalidDefaultDLS = FALSE;
360 TRACE(": filled in cache entry\n");
361 } else TRACE(": caching disabled\n");
364 /* for debug purposes (e.g. to check if all files are cached) */
365 TRACE("*** Loader's cache ***\n");
367 LIST_FOR_EACH (pEntry, This->pObjects) {
369 pObjectEntry = LIST_ENTRY(pEntry, WINE_LOADER_ENTRY, entry);
370 TRACE(": entry nr. %i:\n%s\n - bInvalidDefaultDLS = %i\n - pObject = %p\n", i, debugstr_DMUS_OBJECTDESC(&pObjectEntry->Desc), pObjectEntry->bInvalidDefaultDLS, pObjectEntry->pObject);
374 result = IDirectMusicObject_QueryInterface (pObject, riid, ppv);
375 if (!bCache) IDirectMusicObject_Release (pObject); /* since loader's reference is not needed */
376 /* if there was trouble with loading, and if no other error occurred,
377 we should return DMUS_S_PARTIALLOAD; else, error is returned */
384 static HRESULT WINAPI IDirectMusicLoaderImpl_SetObject(IDirectMusicLoader8 *iface, DMUS_OBJECTDESC *pDesc)
386 IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
388 LPDIRECTMUSICOBJECT pObject;
389 DMUS_OBJECTDESC Desc;
391 LPWINE_LOADER_ENTRY pObjectEntry, pNewEntry;
394 TRACE("(%p)->(%p)\n", This, pDesc);
396 if (TRACE_ON(dmloader))
397 dump_DMUS_OBJECTDESC(pDesc);
399 /* create stream and load additional info from it */
400 if (pDesc->dwValidData & DMUS_OBJ_FILENAME) {
401 /* generate filename; if it's full path, don't add search
402 directory path, otherwise do */
403 WCHAR wszFileName[MAX_PATH];
405 if (pDesc->dwValidData & DMUS_OBJ_FULLPATH) {
406 lstrcpyW(wszFileName, pDesc->wszFileName);
409 WCHAR wszSearchPath[MAX_PATH];
410 DMUSIC_GetLoaderSettings (iface, &pDesc->guidClass, wszSearchPath, NULL);
411 lstrcpyW(wszFileName, wszSearchPath);
412 p = wszFileName + lstrlenW(wszFileName);
413 if (p > wszFileName && p[-1] != '\\') *p++ = '\\';
414 strcpyW(p, pDesc->wszFileName);
417 hr = DMUSIC_CreateDirectMusicLoaderFileStream ((LPVOID*)&pStream);
419 ERR(": could not create file stream\n");
420 return DMUS_E_LOADER_FAILEDOPEN;
423 hr = IDirectMusicLoaderFileStream_Attach (pStream, wszFileName, iface);
425 ERR(": could not attach stream to file\n");
426 IStream_Release (pStream);
427 return DMUS_E_LOADER_FAILEDOPEN;
430 else if (pDesc->dwValidData & DMUS_OBJ_STREAM) {
432 hr = DMUSIC_CreateDirectMusicLoaderGenericStream ((LPVOID*)&pStream);
434 ERR(": could not create generic stream\n");
435 return DMUS_E_LOADER_FAILEDOPEN;
438 hr = IDirectMusicLoaderGenericStream_Attach (pStream, pDesc->pStream, iface);
440 ERR(": could not attach stream\n");
441 IStream_Release (pStream);
442 return DMUS_E_LOADER_FAILEDOPEN;
445 else if (pDesc->dwValidData & DMUS_OBJ_MEMORY) {
447 hr = DMUSIC_CreateDirectMusicLoaderResourceStream ((LPVOID*)&pStream);
449 ERR(": could not create resource stream\n");
450 return DMUS_E_LOADER_FAILEDOPEN;
453 hr = IDirectMusicLoaderResourceStream_Attach (pStream, pDesc->pbMemData, pDesc->llMemLength, 0, iface);
455 ERR(": could not attach stream to resource\n");
456 IStream_Release (pStream);
457 return DMUS_E_LOADER_FAILEDOPEN;
461 ERR(": no way to get additional info\n");
462 return DMUS_E_LOADER_FAILEDOPEN;
466 CoCreateInstance (&pDesc->guidClass, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusicObject, (LPVOID*)&pObject);
468 /* *sigh*... some ms objects have lousy implementation of ParseDescriptor that clears input descriptor :( */
469 #ifdef NOW_WE_ARE_FREE
470 /* parse descriptor: we actually use input descriptor; fields that aren't available from stream remain,
471 otherwise real info is set */
472 IDirectMusicObject_ParseDescriptor (pObject, pStream, pDesc);
474 /* hmph... due to some trouble I had with certain tests, we store current position and then set it back */
475 DM_STRUCT_INIT(&Desc);
476 if (FAILED(IDirectMusicObject_ParseDescriptor (pObject, pStream, &Desc))) {
477 ERR(": couldn't parse descriptor\n");
478 return DMUS_E_LOADER_FORMATNOTSUPPORTED;
481 /* copy elements from parsed descriptor into input descriptor; this sets new info, overwriting if necessary,
482 but leaves info that's provided by input and not available from stream */
483 DMUSIC_CopyDescriptor (pDesc, &Desc);
485 /* release everything */
486 IDirectMusicObject_Release (pObject);
487 IStream_Release (pStream);
489 /* sometimes it happens that twisted programs call SetObject for same object twice...
490 in such cases, native loader returns S_OK and does nothing... a sound plan */
491 LIST_FOR_EACH (pEntry, This->pObjects) {
492 pObjectEntry = LIST_ENTRY (pEntry, WINE_LOADER_ENTRY, entry);
493 if (!memcmp (&pObjectEntry->Desc, pDesc, sizeof(DMUS_OBJECTDESC))) {
494 TRACE(": exactly same entry already exists\n");
500 TRACE(": adding alias entry with following info:\n");
501 if (TRACE_ON(dmloader))
502 dump_DMUS_OBJECTDESC(pDesc);
503 pNewEntry = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(WINE_LOADER_ENTRY));
504 /* use this function instead of pure memcpy due to streams (memcpy just copies pointer),
505 which is basically used further by app that called SetDescriptor... better safety than exception */
506 DMUSIC_CopyDescriptor (&pNewEntry->Desc, pDesc);
507 list_add_head (This->pObjects, &pNewEntry->entry);
512 static HRESULT WINAPI IDirectMusicLoaderImpl_SetSearchDirectory(IDirectMusicLoader8 *iface, REFGUID rguidClass, WCHAR *pwzPath, BOOL fClear)
514 IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
515 WCHAR wszCurrentPath[MAX_PATH];
516 TRACE("(%p, %s, %s, %d)\n", This, debugstr_dmguid(rguidClass), debugstr_w(pwzPath), fClear);
517 FIXME(": fClear ignored\n");
518 DMUSIC_GetLoaderSettings (iface, rguidClass, wszCurrentPath, NULL);
519 if (!strncmpW(wszCurrentPath, pwzPath, MAX_PATH)) {
522 /* FIXME: check if path is valid; else return DMUS_E_LOADER_BADPATH */
523 return DMUSIC_SetLoaderSettings (iface, rguidClass, pwzPath, NULL);
526 static HRESULT WINAPI IDirectMusicLoaderImpl_ScanDirectory(IDirectMusicLoader8 *iface, REFGUID rguidClass, WCHAR *pwzFileExtension, WCHAR *pwzScanFileName)
528 IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
529 static const WCHAR wszAny[] = {'*',0};
530 WIN32_FIND_DATAW FileData;
532 WCHAR wszSearchString[MAX_PATH];
535 TRACE("(%p, %s, %p, %p)\n", This, debugstr_dmguid(rguidClass), pwzFileExtension, pwzScanFileName);
536 if (IsEqualGUID (rguidClass, &GUID_DirectMusicAllTypes) || !DMUSIC_IsValidLoadableClass(rguidClass)) {
537 ERR(": rguidClass invalid CLSID\n");
538 return REGDB_E_CLASSNOTREG;
541 /* get search path for given class */
542 DMUSIC_GetLoaderSettings (iface, rguidClass, wszSearchString, NULL);
544 p = wszSearchString + lstrlenW(wszSearchString);
545 if (p > wszSearchString && p[-1] != '\\') *p++ = '\\';
546 *p++ = '*'; /* any file */
547 if (strcmpW (pwzFileExtension, wszAny)) *p++ = '.'; /* if we have actual extension, put a dot */
548 strcpyW (p, pwzFileExtension);
550 TRACE(": search string: %s\n", debugstr_w(wszSearchString));
552 hSearch = FindFirstFileW (wszSearchString, &FileData);
553 if (hSearch == INVALID_HANDLE_VALUE) {
554 TRACE(": no files found\n");
559 DMUS_OBJECTDESC Desc;
560 DM_STRUCT_INIT(&Desc);
561 Desc.dwValidData = DMUS_OBJ_CLASS | DMUS_OBJ_FILENAME | DMUS_OBJ_FULLPATH | DMUS_OBJ_DATE;
562 Desc.guidClass = *rguidClass;
563 strcpyW (Desc.wszFileName, FileData.cFileName);
564 FileTimeToLocalFileTime (&FileData.ftCreationTime, &Desc.ftDate);
565 IDirectMusicLoader8_SetObject (iface, &Desc);
567 if (!FindNextFileW (hSearch, &FileData)) {
568 if (GetLastError () == ERROR_NO_MORE_FILES) {
569 TRACE(": search completed\n");
572 ERR(": could not get next file\n");
581 static HRESULT WINAPI IDirectMusicLoaderImpl_CacheObject(IDirectMusicLoader8 *iface, IDirectMusicObject *pObject)
583 IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
584 DMUS_OBJECTDESC Desc;
585 HRESULT result = DMUS_E_LOADER_OBJECTNOTFOUND;
587 LPWINE_LOADER_ENTRY pObjectEntry = NULL;
589 TRACE("(%p, %p)\n", This, pObject);
592 DM_STRUCT_INIT(&Desc);
593 IDirectMusicObject_GetDescriptor (pObject, &Desc);
595 /* now iterate through the list and check if we have an alias (without object), corresponding
596 to the descriptor of the input object */
597 LIST_FOR_EACH(pEntry, This->pObjects) {
598 pObjectEntry = LIST_ENTRY(pEntry, WINE_LOADER_ENTRY, entry);
599 if ((Desc.dwValidData & DMUS_OBJ_OBJECT) &&
600 (pObjectEntry->Desc.dwValidData & DMUS_OBJ_OBJECT) &&
601 IsEqualGUID (&Desc.guidObject, &pObjectEntry->Desc.guidObject)) {
602 TRACE(": found it by object GUID\n");
603 if ((pObjectEntry->Desc.dwValidData & DMUS_OBJ_LOADED) && pObjectEntry->pObject)
609 else if ((Desc.dwValidData & (DMUS_OBJ_FILENAME | DMUS_OBJ_FULLPATH)) &&
610 (pObjectEntry->Desc.dwValidData & (DMUS_OBJ_FILENAME | DMUS_OBJ_FULLPATH)) &&
611 !strncmpW (Desc.wszFileName, pObjectEntry->Desc.wszFileName, DMUS_MAX_FILENAME)) {
612 TRACE(": found it by fullpath filename\n");
613 if ((pObjectEntry->Desc.dwValidData & DMUS_OBJ_LOADED) && pObjectEntry->pObject)
619 else if ((Desc.dwValidData & (DMUS_OBJ_NAME | DMUS_OBJ_CATEGORY)) &&
620 (pObjectEntry->Desc.dwValidData & (DMUS_OBJ_NAME | DMUS_OBJ_CATEGORY)) &&
621 !strncmpW (Desc.wszName, pObjectEntry->Desc.wszName, DMUS_MAX_NAME) &&
622 !strncmpW (Desc.wszCategory, pObjectEntry->Desc.wszCategory, DMUS_MAX_CATEGORY)) {
623 TRACE(": found it by name and category\n");
624 if ((pObjectEntry->Desc.dwValidData & DMUS_OBJ_LOADED) && pObjectEntry->pObject)
630 else if ((Desc.dwValidData & DMUS_OBJ_NAME) &&
631 (pObjectEntry->Desc.dwValidData & DMUS_OBJ_NAME) &&
632 !strncmpW (Desc.wszName, pObjectEntry->Desc.wszName, DMUS_MAX_NAME)) {
633 TRACE(": found it by name\n");
634 if ((pObjectEntry->Desc.dwValidData & DMUS_OBJ_LOADED) && pObjectEntry->pObject)
640 else if ((Desc.dwValidData & DMUS_OBJ_FILENAME) &&
641 (pObjectEntry->Desc.dwValidData & DMUS_OBJ_FILENAME) &&
642 !strncmpW (Desc.wszFileName, pObjectEntry->Desc.wszFileName, DMUS_MAX_FILENAME)) {
643 TRACE(": found it by filename\n");
644 if ((pObjectEntry->Desc.dwValidData & DMUS_OBJ_LOADED) && pObjectEntry->pObject)
652 /* if we found such alias, then set everything */
653 if (result == S_OK) {
654 pObjectEntry->Desc.dwValidData &= DMUS_OBJ_LOADED;
655 pObjectEntry->pObject = pObject;
656 IDirectMusicObject_AddRef (pObjectEntry->pObject);
662 static HRESULT WINAPI IDirectMusicLoaderImpl_ReleaseObject(IDirectMusicLoader8 *iface, IDirectMusicObject *pObject)
664 IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
665 DMUS_OBJECTDESC Desc;
667 LPWINE_LOADER_ENTRY pObjectEntry = NULL;
668 HRESULT result = S_FALSE;
670 TRACE("(%p, %p)\n", This, pObject);
672 if(!pObject) return E_POINTER;
675 DM_STRUCT_INIT(&Desc);
676 IDirectMusicObject_GetDescriptor (pObject, &Desc);
678 /* iterate through the list of objects we know about; check only those with DMUS_OBJ_LOADED */
679 TRACE(": looking for the object in cache\n");
680 LIST_FOR_EACH(pEntry, This->pObjects) {
681 pObjectEntry = LIST_ENTRY(pEntry, WINE_LOADER_ENTRY, entry);
682 if ((Desc.dwValidData & DMUS_OBJ_OBJECT) &&
683 (pObjectEntry->Desc.dwValidData & (DMUS_OBJ_OBJECT | DMUS_OBJ_LOADED)) &&
684 IsEqualGUID (&Desc.guidObject, &pObjectEntry->Desc.guidObject)) {
685 TRACE(": found it by object GUID\n");
686 if (TRACE_ON(dmloader))
687 dump_DMUS_OBJECTDESC(&pObjectEntry->Desc);
691 else if ((Desc.dwValidData & (DMUS_OBJ_FILENAME | DMUS_OBJ_FULLPATH)) &&
692 (pObjectEntry->Desc.dwValidData & (DMUS_OBJ_FILENAME | DMUS_OBJ_FULLPATH | DMUS_OBJ_LOADED)) &&
693 !strncmpW (Desc.wszFileName, pObjectEntry->Desc.wszFileName, DMUS_MAX_FILENAME)) {
694 TRACE(": found it by fullpath filename\n");
698 else if ((Desc.dwValidData & (DMUS_OBJ_NAME | DMUS_OBJ_CATEGORY)) &&
699 (pObjectEntry->Desc.dwValidData & (DMUS_OBJ_NAME | DMUS_OBJ_CATEGORY | DMUS_OBJ_LOADED)) &&
700 !strncmpW (Desc.wszName, pObjectEntry->Desc.wszName, DMUS_MAX_NAME) &&
701 !strncmpW (Desc.wszCategory, pObjectEntry->Desc.wszCategory, DMUS_MAX_CATEGORY)) {
702 TRACE(": found it by name and category\n");
706 else if ((Desc.dwValidData & DMUS_OBJ_NAME) &&
707 (pObjectEntry->Desc.dwValidData & (DMUS_OBJ_NAME | DMUS_OBJ_LOADED)) &&
708 !strncmpW (Desc.wszName, pObjectEntry->Desc.wszName, DMUS_MAX_NAME)) {
709 TRACE(": found it by name\n");
713 else if ((Desc.dwValidData & DMUS_OBJ_FILENAME) &&
714 (pObjectEntry->Desc.dwValidData & (DMUS_OBJ_FILENAME | DMUS_OBJ_LOADED)) &&
715 !strncmpW (Desc.wszFileName, pObjectEntry->Desc.wszFileName, DMUS_MAX_FILENAME)) {
716 TRACE(": found it by filename\n");
721 if (result == S_OK) {
722 /*TRACE(": releasing:\n%s - bInvalidDefaultDLS = %i\n - pObject = %p\n", debugstr_DMUS_OBJECTDESC(&pObjectEntry->Desc), pObjectEntry->bInvalidDefaultDLS, pObjectEntry->pObject); */
723 IDirectMusicObject_Release (pObjectEntry->pObject);
724 pObjectEntry->pObject = NULL;
725 pObjectEntry->Desc.dwValidData &= ~DMUS_OBJ_LOADED;
730 static HRESULT WINAPI IDirectMusicLoaderImpl_ClearCache(IDirectMusicLoader8 *iface, REFGUID rguidClass)
732 IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
734 LPWINE_LOADER_ENTRY pObjectEntry;
735 TRACE("(%p, %s)\n", This, debugstr_dmguid(rguidClass));
737 LIST_FOR_EACH (pEntry, This->pObjects) {
738 pObjectEntry = LIST_ENTRY (pEntry, WINE_LOADER_ENTRY, entry);
740 if ((IsEqualGUID (rguidClass, &GUID_DirectMusicAllTypes) || IsEqualGUID (rguidClass, &pObjectEntry->Desc.guidClass)) &&
741 (pObjectEntry->Desc.dwValidData & DMUS_OBJ_LOADED)) {
742 /* basically, wrap to ReleaseObject for each object found */
743 IDirectMusicLoader8_ReleaseObject (iface, pObjectEntry->pObject);
750 static HRESULT WINAPI IDirectMusicLoaderImpl_EnableCache(IDirectMusicLoader8 *iface, REFGUID rguidClass, BOOL fEnable)
752 IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
754 TRACE("(%p, %s, %d)\n", This, debugstr_dmguid(rguidClass), fEnable);
755 DMUSIC_GetLoaderSettings (iface, rguidClass, NULL, &bCurrent);
756 if (bCurrent == fEnable)
759 return DMUSIC_SetLoaderSettings (iface, rguidClass, NULL, &fEnable);
762 static HRESULT WINAPI IDirectMusicLoaderImpl_EnumObject(IDirectMusicLoader8 *iface, REFGUID rguidClass, DWORD dwIndex, DMUS_OBJECTDESC *pDesc)
764 IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
767 LPWINE_LOADER_ENTRY pObjectEntry;
768 TRACE("(%p, %s, %d, %p)\n", This, debugstr_dmguid(rguidClass), dwIndex, pDesc);
770 DM_STRUCT_INIT(pDesc);
772 LIST_FOR_EACH (pEntry, This->pObjects) {
773 pObjectEntry = LIST_ENTRY (pEntry, WINE_LOADER_ENTRY, entry);
775 if (IsEqualGUID (rguidClass, &GUID_DirectMusicAllTypes) || IsEqualGUID (rguidClass, &pObjectEntry->Desc.guidClass)) {
776 if (dwCount == dwIndex) {
777 *pDesc = pObjectEntry->Desc;
778 /* we aren't supposed to reveal this info */
779 pDesc->dwValidData &= ~(DMUS_OBJ_MEMORY | DMUS_OBJ_STREAM);
780 pDesc->pbMemData = NULL;
781 pDesc->llMemLength = 0;
782 pDesc->pStream = NULL;
789 TRACE(": not found\n");
793 static void WINAPI IDirectMusicLoaderImpl_CollectGarbage(IDirectMusicLoader8 *iface)
795 FIXME("(%p)->(): stub\n", iface);
798 static HRESULT WINAPI IDirectMusicLoaderImpl_ReleaseObjectByUnknown(IDirectMusicLoader8 *iface, IUnknown *pObject)
800 IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
802 LPDIRECTMUSICOBJECT pObjectInterface;
804 TRACE("(%p, %p)\n", This, pObject);
806 if (IsBadReadPtr (pObject, sizeof(*pObject))) {
807 ERR(": pObject bad write pointer\n");
810 /* we simply get IDirectMusicObject interface */
811 result = IUnknown_QueryInterface (pObject, &IID_IDirectMusicObject, (LPVOID*)&pObjectInterface);
812 if (FAILED(result)) return result;
813 /* and release it in old-fashioned way */
814 result = IDirectMusicLoader8_ReleaseObject (iface, pObjectInterface);
815 IDirectMusicObject_Release (pObjectInterface);
820 static HRESULT WINAPI IDirectMusicLoaderImpl_LoadObjectFromFile(IDirectMusicLoader8 *iface, REFGUID rguidClassID, REFIID iidInterfaceID, WCHAR *pwzFilePath, void **ppObject)
822 IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
823 DMUS_OBJECTDESC ObjDesc;
824 WCHAR wszLoaderSearchPath[MAX_PATH];
826 TRACE("(%p, %s, %s, %s, %p): wrapping to IDirectMusicLoaderImpl_GetObject\n", This, debugstr_dmguid(rguidClassID), debugstr_dmguid(iidInterfaceID), debugstr_w(pwzFilePath), ppObject);
828 DM_STRUCT_INIT(&ObjDesc);
829 ObjDesc.dwValidData = DMUS_OBJ_FILENAME | DMUS_OBJ_FULLPATH | DMUS_OBJ_CLASS; /* I believe I've read somewhere in MSDN that this function requires either full path or relative path */
830 ObjDesc.guidClass = *rguidClassID;
831 /* OK, MSDN says that search order is the following:
832 - current directory (DONE)
833 - windows search path (FIXME: how do I get that?)
834 - loader's search path (DONE)
836 DMUSIC_GetLoaderSettings (iface, rguidClassID, wszLoaderSearchPath, NULL);
837 /* search in current directory */
838 if (!SearchPathW (NULL, pwzFilePath, NULL, sizeof(ObjDesc.wszFileName)/sizeof(WCHAR), ObjDesc.wszFileName, NULL) &&
839 /* search in loader's search path */
840 !SearchPathW (wszLoaderSearchPath, pwzFilePath, NULL, sizeof(ObjDesc.wszFileName)/sizeof(WCHAR), ObjDesc.wszFileName, NULL)) {
841 /* cannot find file */
842 TRACE(": cannot find file\n");
843 return DMUS_E_LOADER_FAILEDOPEN;
846 TRACE(": full file path = %s\n", debugstr_w (ObjDesc.wszFileName));
848 return IDirectMusicLoader_GetObject(iface, &ObjDesc, iidInterfaceID, ppObject);
851 static const IDirectMusicLoader8Vtbl DirectMusicLoader_Loader_Vtbl = {
852 IDirectMusicLoaderImpl_QueryInterface,
853 IDirectMusicLoaderImpl_AddRef,
854 IDirectMusicLoaderImpl_Release,
855 IDirectMusicLoaderImpl_GetObject,
856 IDirectMusicLoaderImpl_SetObject,
857 IDirectMusicLoaderImpl_SetSearchDirectory,
858 IDirectMusicLoaderImpl_ScanDirectory,
859 IDirectMusicLoaderImpl_CacheObject,
860 IDirectMusicLoaderImpl_ReleaseObject,
861 IDirectMusicLoaderImpl_ClearCache,
862 IDirectMusicLoaderImpl_EnableCache,
863 IDirectMusicLoaderImpl_EnumObject,
864 IDirectMusicLoaderImpl_CollectGarbage,
865 IDirectMusicLoaderImpl_ReleaseObjectByUnknown,
866 IDirectMusicLoaderImpl_LoadObjectFromFile
869 /* help function for DMUSIC_SetDefaultDLS */
870 static HRESULT DMUSIC_GetDefaultGMPath (WCHAR wszPath[MAX_PATH]) {
872 DWORD returnType, sizeOfReturnBuffer = MAX_PATH;
873 char szPath[MAX_PATH];
875 if ((RegOpenKeyExA (HKEY_LOCAL_MACHINE, "Software\\Microsoft\\DirectMusic" , 0, KEY_READ, &hkDM) != ERROR_SUCCESS) ||
876 (RegQueryValueExA (hkDM, "GMFilePath", NULL, &returnType, (LPBYTE) szPath, &sizeOfReturnBuffer) != ERROR_SUCCESS)) {
877 WARN(": registry entry missing\n" );
880 /* FIXME: Check return types to ensure we're interpreting data right */
881 MultiByteToWideChar (CP_ACP, 0, szPath, -1, wszPath, MAX_PATH);
886 /* for ClassFactory */
887 HRESULT WINAPI DMUSIC_CreateDirectMusicLoaderImpl(const GUID *lpcGUID, void **ppobj, IUnknown *pUnkOuter)
889 IDirectMusicLoaderImpl *obj;
890 DMUS_OBJECTDESC Desc;
891 LPWINE_LOADER_ENTRY pDefaultDLSEntry;
894 TRACE("(%s, %p, %p)\n", debugstr_dmguid(lpcGUID), ppobj, pUnkOuter);
895 obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicLoaderImpl));
898 return E_OUTOFMEMORY;
900 obj->IDirectMusicLoader8_iface.lpVtbl = &DirectMusicLoader_Loader_Vtbl;
901 obj->ref = 0; /* Will be inited with QueryInterface */
902 /* init cache/alias list */
903 obj->pObjects = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(struct list));
904 list_init (obj->pObjects);
906 obj->pClassSettings = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(struct list));
907 list_init (obj->pClassSettings);
908 DMUSIC_InitLoaderSettings(&obj->IDirectMusicLoader8_iface);
910 /* set default DLS collection (via SetObject... so that loading via DMUS_OBJ_OBJECT is possible) */
911 DM_STRUCT_INIT(&Desc);
912 Desc.dwValidData = DMUS_OBJ_CLASS | DMUS_OBJ_FILENAME | DMUS_OBJ_FULLPATH | DMUS_OBJ_OBJECT;
913 Desc.guidClass = CLSID_DirectMusicCollection;
914 Desc.guidObject = GUID_DefaultGMCollection;
915 DMUSIC_GetDefaultGMPath (Desc.wszFileName);
916 IDirectMusicLoader_SetObject(&obj->IDirectMusicLoader8_iface, &Desc);
917 /* and now the workaroundTM for "invalid" default DLS; basically,
918 my tests showed that if GUID chunk is present in default DLS
919 collection, loader treats it as "invalid" and returns
920 DMUS_E_LOADER_NOFILENAME for all requests for it; basically, we check
921 if out input guidObject was overwritten */
922 pEntry = list_head (obj->pObjects);
923 pDefaultDLSEntry = LIST_ENTRY (pEntry, WINE_LOADER_ENTRY, entry);
924 if (!IsEqualGUID(&Desc.guidObject, &GUID_DefaultGMCollection)) {
925 pDefaultDLSEntry->bInvalidDefaultDLS = TRUE;
930 return IDirectMusicLoader_QueryInterface(&obj->IDirectMusicLoader8_iface, lpcGUID, ppobj);
933 /* help function for retrieval of search path and caching option for certain class */
934 static HRESULT DMUSIC_GetLoaderSettings(IDirectMusicLoader8 *iface, REFGUID pClassID, WCHAR *wszSearchPath, BOOL *pbCache)
936 IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
938 TRACE(": (%p, %s, %p, %p)\n", This, debugstr_dmguid(pClassID), wszSearchPath, pbCache);
940 LIST_FOR_EACH(pEntry, This->pClassSettings) {
941 LPWINE_LOADER_OPTION pOptionEntry = LIST_ENTRY(pEntry, WINE_LOADER_OPTION, entry);
942 if (IsEqualCLSID (pClassID, &pOptionEntry->guidClass)) {
944 strcpyW(wszSearchPath, pOptionEntry->wszSearchPath);
946 *pbCache = pOptionEntry->bCache;
953 /* help function for setting search path and caching option for certain class */
954 static HRESULT DMUSIC_SetLoaderSettings(IDirectMusicLoader8 *iface, REFGUID pClassID, WCHAR *wszSearchPath, BOOL *pbCache)
956 IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
958 HRESULT result = S_FALSE; /* in case pClassID != GUID_DirectMusicAllTypes and not a valid CLSID */
959 TRACE(": (%p, %s, %p, %p)\n", This, debugstr_dmguid(pClassID), wszSearchPath, pbCache);
961 LIST_FOR_EACH(pEntry, This->pClassSettings) {
962 LPWINE_LOADER_OPTION pOptionEntry = LIST_ENTRY(pEntry, WINE_LOADER_OPTION, entry);
963 /* well, either we have GUID_DirectMusicAllTypes and need to set it to all,
964 or specific CLSID is given and we set it only to it */
965 if (IsEqualGUID (pClassID, &GUID_DirectMusicAllTypes) ||
966 IsEqualCLSID (pClassID, &pOptionEntry->guidClass)) {
968 strcpyW(pOptionEntry->wszSearchPath, wszSearchPath);
970 pOptionEntry->bCache = *pbCache;
978 static HRESULT DMUSIC_InitLoaderSettings(IDirectMusicLoader8 *iface)
980 IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
982 /* hard-coded list of classes */
983 static REFCLSID classes[] = {
984 &CLSID_DirectMusicAudioPathConfig,
985 &CLSID_DirectMusicBand,
986 &CLSID_DirectMusicContainer,
987 &CLSID_DirectMusicCollection,
988 &CLSID_DirectMusicChordMap,
989 &CLSID_DirectMusicSegment,
990 &CLSID_DirectMusicScript,
991 &CLSID_DirectMusicSong,
992 &CLSID_DirectMusicStyle,
993 &CLSID_DirectMusicGraph,
994 &CLSID_DirectSoundWave
998 WCHAR wszCurrent[MAX_PATH];
1000 TRACE(": (%p)\n", This);
1001 GetCurrentDirectoryW (MAX_PATH, wszCurrent);
1003 for (i = 0; i < sizeof(classes)/sizeof(REFCLSID); i++) {
1004 LPWINE_LOADER_OPTION pNewSetting = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(WINE_LOADER_OPTION));
1005 pNewSetting->guidClass = *classes[i];
1006 strcpyW (pNewSetting->wszSearchPath, wszCurrent);
1007 pNewSetting->bCache = TRUE;
1008 list_add_tail (This->pClassSettings, &pNewSetting->entry);