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 hr = CoCreateInstance (&pDesc->guidClass, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusicObject, (LPVOID*)&pObject);
468 ERR("Object creation of %s failed 0x%08x\n", debugstr_guid(&pDesc->guidClass),hr);
469 return DMUS_E_LOADER_FAILEDOPEN;
472 /* *sigh*... some ms objects have lousy implementation of ParseDescriptor that clears input descriptor :( */
473 #ifdef NOW_WE_ARE_FREE
474 /* parse descriptor: we actually use input descriptor; fields that aren't available from stream remain,
475 otherwise real info is set */
476 IDirectMusicObject_ParseDescriptor (pObject, pStream, pDesc);
478 /* hmph... due to some trouble I had with certain tests, we store current position and then set it back */
479 DM_STRUCT_INIT(&Desc);
480 if (FAILED(IDirectMusicObject_ParseDescriptor (pObject, pStream, &Desc))) {
481 ERR(": couldn't parse descriptor\n");
482 return DMUS_E_LOADER_FORMATNOTSUPPORTED;
485 /* copy elements from parsed descriptor into input descriptor; this sets new info, overwriting if necessary,
486 but leaves info that's provided by input and not available from stream */
487 DMUSIC_CopyDescriptor (pDesc, &Desc);
489 /* release everything */
490 IDirectMusicObject_Release (pObject);
491 IStream_Release (pStream);
493 /* sometimes it happens that twisted programs call SetObject for same object twice...
494 in such cases, native loader returns S_OK and does nothing... a sound plan */
495 LIST_FOR_EACH (pEntry, This->pObjects) {
496 pObjectEntry = LIST_ENTRY (pEntry, WINE_LOADER_ENTRY, entry);
497 if (!memcmp (&pObjectEntry->Desc, pDesc, sizeof(DMUS_OBJECTDESC))) {
498 TRACE(": exactly same entry already exists\n");
504 TRACE(": adding alias entry with following info:\n");
505 if (TRACE_ON(dmloader))
506 dump_DMUS_OBJECTDESC(pDesc);
507 pNewEntry = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(WINE_LOADER_ENTRY));
508 /* use this function instead of pure memcpy due to streams (memcpy just copies pointer),
509 which is basically used further by app that called SetDescriptor... better safety than exception */
510 DMUSIC_CopyDescriptor (&pNewEntry->Desc, pDesc);
511 list_add_head (This->pObjects, &pNewEntry->entry);
516 static HRESULT WINAPI IDirectMusicLoaderImpl_SetSearchDirectory(IDirectMusicLoader8 *iface, REFGUID rguidClass, WCHAR *pwzPath, BOOL fClear)
518 IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
519 WCHAR wszCurrentPath[MAX_PATH];
520 TRACE("(%p, %s, %s, %d)\n", This, debugstr_dmguid(rguidClass), debugstr_w(pwzPath), fClear);
521 FIXME(": fClear ignored\n");
522 DMUSIC_GetLoaderSettings (iface, rguidClass, wszCurrentPath, NULL);
523 if (!strncmpW(wszCurrentPath, pwzPath, MAX_PATH)) {
526 /* FIXME: check if path is valid; else return DMUS_E_LOADER_BADPATH */
527 return DMUSIC_SetLoaderSettings (iface, rguidClass, pwzPath, NULL);
530 static HRESULT WINAPI IDirectMusicLoaderImpl_ScanDirectory(IDirectMusicLoader8 *iface, REFGUID rguidClass, WCHAR *pwzFileExtension, WCHAR *pwzScanFileName)
532 IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
533 static const WCHAR wszAny[] = {'*',0};
534 WIN32_FIND_DATAW FileData;
536 WCHAR wszSearchString[MAX_PATH];
539 TRACE("(%p, %s, %p, %p)\n", This, debugstr_dmguid(rguidClass), pwzFileExtension, pwzScanFileName);
540 if (IsEqualGUID (rguidClass, &GUID_DirectMusicAllTypes) || !DMUSIC_IsValidLoadableClass(rguidClass)) {
541 ERR(": rguidClass invalid CLSID\n");
542 return REGDB_E_CLASSNOTREG;
545 /* get search path for given class */
546 DMUSIC_GetLoaderSettings (iface, rguidClass, wszSearchString, NULL);
548 p = wszSearchString + lstrlenW(wszSearchString);
549 if (p > wszSearchString && p[-1] != '\\') *p++ = '\\';
550 *p++ = '*'; /* any file */
551 if (strcmpW (pwzFileExtension, wszAny)) *p++ = '.'; /* if we have actual extension, put a dot */
552 strcpyW (p, pwzFileExtension);
554 TRACE(": search string: %s\n", debugstr_w(wszSearchString));
556 hSearch = FindFirstFileW (wszSearchString, &FileData);
557 if (hSearch == INVALID_HANDLE_VALUE) {
558 TRACE(": no files found\n");
563 DMUS_OBJECTDESC Desc;
564 DM_STRUCT_INIT(&Desc);
565 Desc.dwValidData = DMUS_OBJ_CLASS | DMUS_OBJ_FILENAME | DMUS_OBJ_FULLPATH | DMUS_OBJ_DATE;
566 Desc.guidClass = *rguidClass;
567 strcpyW (Desc.wszFileName, FileData.cFileName);
568 FileTimeToLocalFileTime (&FileData.ftCreationTime, &Desc.ftDate);
569 IDirectMusicLoader8_SetObject (iface, &Desc);
571 if (!FindNextFileW (hSearch, &FileData)) {
572 if (GetLastError () == ERROR_NO_MORE_FILES) {
573 TRACE(": search completed\n");
576 ERR(": could not get next file\n");
585 static HRESULT WINAPI IDirectMusicLoaderImpl_CacheObject(IDirectMusicLoader8 *iface, IDirectMusicObject *pObject)
587 IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
588 DMUS_OBJECTDESC Desc;
589 HRESULT result = DMUS_E_LOADER_OBJECTNOTFOUND;
591 LPWINE_LOADER_ENTRY pObjectEntry = NULL;
593 TRACE("(%p, %p)\n", This, pObject);
596 DM_STRUCT_INIT(&Desc);
597 IDirectMusicObject_GetDescriptor (pObject, &Desc);
599 /* now iterate through the list and check if we have an alias (without object), corresponding
600 to the descriptor of the input object */
601 LIST_FOR_EACH(pEntry, This->pObjects) {
602 pObjectEntry = LIST_ENTRY(pEntry, WINE_LOADER_ENTRY, entry);
603 if ((Desc.dwValidData & DMUS_OBJ_OBJECT) &&
604 (pObjectEntry->Desc.dwValidData & DMUS_OBJ_OBJECT) &&
605 IsEqualGUID (&Desc.guidObject, &pObjectEntry->Desc.guidObject)) {
606 TRACE(": found it by object GUID\n");
607 if ((pObjectEntry->Desc.dwValidData & DMUS_OBJ_LOADED) && pObjectEntry->pObject)
613 else if ((Desc.dwValidData & (DMUS_OBJ_FILENAME | DMUS_OBJ_FULLPATH)) &&
614 (pObjectEntry->Desc.dwValidData & (DMUS_OBJ_FILENAME | DMUS_OBJ_FULLPATH)) &&
615 !strncmpW (Desc.wszFileName, pObjectEntry->Desc.wszFileName, DMUS_MAX_FILENAME)) {
616 TRACE(": found it by fullpath filename\n");
617 if ((pObjectEntry->Desc.dwValidData & DMUS_OBJ_LOADED) && pObjectEntry->pObject)
623 else if ((Desc.dwValidData & (DMUS_OBJ_NAME | DMUS_OBJ_CATEGORY)) &&
624 (pObjectEntry->Desc.dwValidData & (DMUS_OBJ_NAME | DMUS_OBJ_CATEGORY)) &&
625 !strncmpW (Desc.wszName, pObjectEntry->Desc.wszName, DMUS_MAX_NAME) &&
626 !strncmpW (Desc.wszCategory, pObjectEntry->Desc.wszCategory, DMUS_MAX_CATEGORY)) {
627 TRACE(": found it by name and category\n");
628 if ((pObjectEntry->Desc.dwValidData & DMUS_OBJ_LOADED) && pObjectEntry->pObject)
634 else if ((Desc.dwValidData & DMUS_OBJ_NAME) &&
635 (pObjectEntry->Desc.dwValidData & DMUS_OBJ_NAME) &&
636 !strncmpW (Desc.wszName, pObjectEntry->Desc.wszName, DMUS_MAX_NAME)) {
637 TRACE(": found it by name\n");
638 if ((pObjectEntry->Desc.dwValidData & DMUS_OBJ_LOADED) && pObjectEntry->pObject)
644 else if ((Desc.dwValidData & DMUS_OBJ_FILENAME) &&
645 (pObjectEntry->Desc.dwValidData & DMUS_OBJ_FILENAME) &&
646 !strncmpW (Desc.wszFileName, pObjectEntry->Desc.wszFileName, DMUS_MAX_FILENAME)) {
647 TRACE(": found it by filename\n");
648 if ((pObjectEntry->Desc.dwValidData & DMUS_OBJ_LOADED) && pObjectEntry->pObject)
656 /* if we found such alias, then set everything */
657 if (result == S_OK) {
658 pObjectEntry->Desc.dwValidData &= DMUS_OBJ_LOADED;
659 pObjectEntry->pObject = pObject;
660 IDirectMusicObject_AddRef (pObjectEntry->pObject);
666 static HRESULT WINAPI IDirectMusicLoaderImpl_ReleaseObject(IDirectMusicLoader8 *iface, IDirectMusicObject *pObject)
668 IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
669 DMUS_OBJECTDESC Desc;
671 LPWINE_LOADER_ENTRY pObjectEntry = NULL;
672 HRESULT result = S_FALSE;
674 TRACE("(%p, %p)\n", This, pObject);
676 if(!pObject) return E_POINTER;
679 DM_STRUCT_INIT(&Desc);
680 IDirectMusicObject_GetDescriptor (pObject, &Desc);
682 /* iterate through the list of objects we know about; check only those with DMUS_OBJ_LOADED */
683 TRACE(": looking for the object in cache\n");
684 LIST_FOR_EACH(pEntry, This->pObjects) {
685 pObjectEntry = LIST_ENTRY(pEntry, WINE_LOADER_ENTRY, entry);
686 if ((Desc.dwValidData & DMUS_OBJ_OBJECT) &&
687 (pObjectEntry->Desc.dwValidData & (DMUS_OBJ_OBJECT | DMUS_OBJ_LOADED)) &&
688 IsEqualGUID (&Desc.guidObject, &pObjectEntry->Desc.guidObject)) {
689 TRACE(": found it by object GUID\n");
690 if (TRACE_ON(dmloader))
691 dump_DMUS_OBJECTDESC(&pObjectEntry->Desc);
695 else if ((Desc.dwValidData & (DMUS_OBJ_FILENAME | DMUS_OBJ_FULLPATH)) &&
696 (pObjectEntry->Desc.dwValidData & (DMUS_OBJ_FILENAME | DMUS_OBJ_FULLPATH | DMUS_OBJ_LOADED)) &&
697 !strncmpW (Desc.wszFileName, pObjectEntry->Desc.wszFileName, DMUS_MAX_FILENAME)) {
698 TRACE(": found it by fullpath filename\n");
702 else if ((Desc.dwValidData & (DMUS_OBJ_NAME | DMUS_OBJ_CATEGORY)) &&
703 (pObjectEntry->Desc.dwValidData & (DMUS_OBJ_NAME | DMUS_OBJ_CATEGORY | DMUS_OBJ_LOADED)) &&
704 !strncmpW (Desc.wszName, pObjectEntry->Desc.wszName, DMUS_MAX_NAME) &&
705 !strncmpW (Desc.wszCategory, pObjectEntry->Desc.wszCategory, DMUS_MAX_CATEGORY)) {
706 TRACE(": found it by name and category\n");
710 else if ((Desc.dwValidData & DMUS_OBJ_NAME) &&
711 (pObjectEntry->Desc.dwValidData & (DMUS_OBJ_NAME | DMUS_OBJ_LOADED)) &&
712 !strncmpW (Desc.wszName, pObjectEntry->Desc.wszName, DMUS_MAX_NAME)) {
713 TRACE(": found it by name\n");
717 else if ((Desc.dwValidData & DMUS_OBJ_FILENAME) &&
718 (pObjectEntry->Desc.dwValidData & (DMUS_OBJ_FILENAME | DMUS_OBJ_LOADED)) &&
719 !strncmpW (Desc.wszFileName, pObjectEntry->Desc.wszFileName, DMUS_MAX_FILENAME)) {
720 TRACE(": found it by filename\n");
725 if (result == S_OK) {
726 /*TRACE(": releasing:\n%s - bInvalidDefaultDLS = %i\n - pObject = %p\n", debugstr_DMUS_OBJECTDESC(&pObjectEntry->Desc), pObjectEntry->bInvalidDefaultDLS, pObjectEntry->pObject); */
727 IDirectMusicObject_Release (pObjectEntry->pObject);
728 pObjectEntry->pObject = NULL;
729 pObjectEntry->Desc.dwValidData &= ~DMUS_OBJ_LOADED;
734 static HRESULT WINAPI IDirectMusicLoaderImpl_ClearCache(IDirectMusicLoader8 *iface, REFGUID rguidClass)
736 IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
738 LPWINE_LOADER_ENTRY pObjectEntry;
739 TRACE("(%p, %s)\n", This, debugstr_dmguid(rguidClass));
741 LIST_FOR_EACH (pEntry, This->pObjects) {
742 pObjectEntry = LIST_ENTRY (pEntry, WINE_LOADER_ENTRY, entry);
744 if ((IsEqualGUID (rguidClass, &GUID_DirectMusicAllTypes) || IsEqualGUID (rguidClass, &pObjectEntry->Desc.guidClass)) &&
745 (pObjectEntry->Desc.dwValidData & DMUS_OBJ_LOADED)) {
746 /* basically, wrap to ReleaseObject for each object found */
747 IDirectMusicLoader8_ReleaseObject (iface, pObjectEntry->pObject);
754 static HRESULT WINAPI IDirectMusicLoaderImpl_EnableCache(IDirectMusicLoader8 *iface, REFGUID rguidClass, BOOL fEnable)
756 IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
758 TRACE("(%p, %s, %d)\n", This, debugstr_dmguid(rguidClass), fEnable);
759 DMUSIC_GetLoaderSettings (iface, rguidClass, NULL, &bCurrent);
760 if (bCurrent == fEnable)
763 return DMUSIC_SetLoaderSettings (iface, rguidClass, NULL, &fEnable);
766 static HRESULT WINAPI IDirectMusicLoaderImpl_EnumObject(IDirectMusicLoader8 *iface, REFGUID rguidClass, DWORD dwIndex, DMUS_OBJECTDESC *pDesc)
768 IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
771 LPWINE_LOADER_ENTRY pObjectEntry;
772 TRACE("(%p, %s, %d, %p)\n", This, debugstr_dmguid(rguidClass), dwIndex, pDesc);
774 DM_STRUCT_INIT(pDesc);
776 LIST_FOR_EACH (pEntry, This->pObjects) {
777 pObjectEntry = LIST_ENTRY (pEntry, WINE_LOADER_ENTRY, entry);
779 if (IsEqualGUID (rguidClass, &GUID_DirectMusicAllTypes) || IsEqualGUID (rguidClass, &pObjectEntry->Desc.guidClass)) {
780 if (dwCount == dwIndex) {
781 *pDesc = pObjectEntry->Desc;
782 /* we aren't supposed to reveal this info */
783 pDesc->dwValidData &= ~(DMUS_OBJ_MEMORY | DMUS_OBJ_STREAM);
784 pDesc->pbMemData = NULL;
785 pDesc->llMemLength = 0;
786 pDesc->pStream = NULL;
793 TRACE(": not found\n");
797 static void WINAPI IDirectMusicLoaderImpl_CollectGarbage(IDirectMusicLoader8 *iface)
799 FIXME("(%p)->(): stub\n", iface);
802 static HRESULT WINAPI IDirectMusicLoaderImpl_ReleaseObjectByUnknown(IDirectMusicLoader8 *iface, IUnknown *pObject)
804 IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
806 LPDIRECTMUSICOBJECT pObjectInterface;
808 TRACE("(%p, %p)\n", This, pObject);
810 if (IsBadReadPtr (pObject, sizeof(*pObject))) {
811 ERR(": pObject bad write pointer\n");
814 /* we simply get IDirectMusicObject interface */
815 result = IUnknown_QueryInterface (pObject, &IID_IDirectMusicObject, (LPVOID*)&pObjectInterface);
816 if (FAILED(result)) return result;
817 /* and release it in old-fashioned way */
818 result = IDirectMusicLoader8_ReleaseObject (iface, pObjectInterface);
819 IDirectMusicObject_Release (pObjectInterface);
824 static HRESULT WINAPI IDirectMusicLoaderImpl_LoadObjectFromFile(IDirectMusicLoader8 *iface, REFGUID rguidClassID, REFIID iidInterfaceID, WCHAR *pwzFilePath, void **ppObject)
826 IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
827 DMUS_OBJECTDESC ObjDesc;
828 WCHAR wszLoaderSearchPath[MAX_PATH];
830 TRACE("(%p, %s, %s, %s, %p): wrapping to IDirectMusicLoaderImpl_GetObject\n", This, debugstr_dmguid(rguidClassID), debugstr_dmguid(iidInterfaceID), debugstr_w(pwzFilePath), ppObject);
832 DM_STRUCT_INIT(&ObjDesc);
833 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 */
834 ObjDesc.guidClass = *rguidClassID;
835 /* OK, MSDN says that search order is the following:
836 - current directory (DONE)
837 - windows search path (FIXME: how do I get that?)
838 - loader's search path (DONE)
840 DMUSIC_GetLoaderSettings (iface, rguidClassID, wszLoaderSearchPath, NULL);
841 /* search in current directory */
842 if (!SearchPathW (NULL, pwzFilePath, NULL, sizeof(ObjDesc.wszFileName)/sizeof(WCHAR), ObjDesc.wszFileName, NULL) &&
843 /* search in loader's search path */
844 !SearchPathW (wszLoaderSearchPath, pwzFilePath, NULL, sizeof(ObjDesc.wszFileName)/sizeof(WCHAR), ObjDesc.wszFileName, NULL)) {
845 /* cannot find file */
846 TRACE(": cannot find file\n");
847 return DMUS_E_LOADER_FAILEDOPEN;
850 TRACE(": full file path = %s\n", debugstr_w (ObjDesc.wszFileName));
852 return IDirectMusicLoader_GetObject(iface, &ObjDesc, iidInterfaceID, ppObject);
855 static const IDirectMusicLoader8Vtbl DirectMusicLoader_Loader_Vtbl = {
856 IDirectMusicLoaderImpl_QueryInterface,
857 IDirectMusicLoaderImpl_AddRef,
858 IDirectMusicLoaderImpl_Release,
859 IDirectMusicLoaderImpl_GetObject,
860 IDirectMusicLoaderImpl_SetObject,
861 IDirectMusicLoaderImpl_SetSearchDirectory,
862 IDirectMusicLoaderImpl_ScanDirectory,
863 IDirectMusicLoaderImpl_CacheObject,
864 IDirectMusicLoaderImpl_ReleaseObject,
865 IDirectMusicLoaderImpl_ClearCache,
866 IDirectMusicLoaderImpl_EnableCache,
867 IDirectMusicLoaderImpl_EnumObject,
868 IDirectMusicLoaderImpl_CollectGarbage,
869 IDirectMusicLoaderImpl_ReleaseObjectByUnknown,
870 IDirectMusicLoaderImpl_LoadObjectFromFile
873 /* help function for DMUSIC_SetDefaultDLS */
874 static HRESULT DMUSIC_GetDefaultGMPath (WCHAR wszPath[MAX_PATH]) {
876 DWORD returnType, sizeOfReturnBuffer = MAX_PATH;
877 char szPath[MAX_PATH];
879 if ((RegOpenKeyExA (HKEY_LOCAL_MACHINE, "Software\\Microsoft\\DirectMusic" , 0, KEY_READ, &hkDM) != ERROR_SUCCESS) ||
880 (RegQueryValueExA (hkDM, "GMFilePath", NULL, &returnType, (LPBYTE) szPath, &sizeOfReturnBuffer) != ERROR_SUCCESS)) {
881 WARN(": registry entry missing\n" );
884 /* FIXME: Check return types to ensure we're interpreting data right */
885 MultiByteToWideChar (CP_ACP, 0, szPath, -1, wszPath, MAX_PATH);
890 /* for ClassFactory */
891 HRESULT WINAPI DMUSIC_CreateDirectMusicLoaderImpl(const GUID *lpcGUID, void **ppobj, IUnknown *pUnkOuter)
893 IDirectMusicLoaderImpl *obj;
894 DMUS_OBJECTDESC Desc;
895 LPWINE_LOADER_ENTRY pDefaultDLSEntry;
898 TRACE("(%s, %p, %p)\n", debugstr_dmguid(lpcGUID), ppobj, pUnkOuter);
899 obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicLoaderImpl));
902 return E_OUTOFMEMORY;
904 obj->IDirectMusicLoader8_iface.lpVtbl = &DirectMusicLoader_Loader_Vtbl;
905 obj->ref = 0; /* Will be inited with QueryInterface */
906 /* init cache/alias list */
907 obj->pObjects = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(struct list));
908 list_init (obj->pObjects);
910 obj->pClassSettings = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(struct list));
911 list_init (obj->pClassSettings);
912 DMUSIC_InitLoaderSettings(&obj->IDirectMusicLoader8_iface);
914 /* set default DLS collection (via SetObject... so that loading via DMUS_OBJ_OBJECT is possible) */
915 DM_STRUCT_INIT(&Desc);
916 Desc.dwValidData = DMUS_OBJ_CLASS | DMUS_OBJ_FILENAME | DMUS_OBJ_FULLPATH | DMUS_OBJ_OBJECT;
917 Desc.guidClass = CLSID_DirectMusicCollection;
918 Desc.guidObject = GUID_DefaultGMCollection;
919 DMUSIC_GetDefaultGMPath (Desc.wszFileName);
920 IDirectMusicLoader_SetObject(&obj->IDirectMusicLoader8_iface, &Desc);
921 /* and now the workaroundTM for "invalid" default DLS; basically,
922 my tests showed that if GUID chunk is present in default DLS
923 collection, loader treats it as "invalid" and returns
924 DMUS_E_LOADER_NOFILENAME for all requests for it; basically, we check
925 if out input guidObject was overwritten */
926 pEntry = list_head (obj->pObjects);
927 pDefaultDLSEntry = LIST_ENTRY (pEntry, WINE_LOADER_ENTRY, entry);
928 if (!IsEqualGUID(&Desc.guidObject, &GUID_DefaultGMCollection)) {
929 pDefaultDLSEntry->bInvalidDefaultDLS = TRUE;
934 return IDirectMusicLoader_QueryInterface(&obj->IDirectMusicLoader8_iface, lpcGUID, ppobj);
937 /* help function for retrieval of search path and caching option for certain class */
938 static HRESULT DMUSIC_GetLoaderSettings(IDirectMusicLoader8 *iface, REFGUID pClassID, WCHAR *wszSearchPath, BOOL *pbCache)
940 IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
942 TRACE(": (%p, %s, %p, %p)\n", This, debugstr_dmguid(pClassID), wszSearchPath, pbCache);
944 LIST_FOR_EACH(pEntry, This->pClassSettings) {
945 LPWINE_LOADER_OPTION pOptionEntry = LIST_ENTRY(pEntry, WINE_LOADER_OPTION, entry);
946 if (IsEqualCLSID (pClassID, &pOptionEntry->guidClass)) {
948 strcpyW(wszSearchPath, pOptionEntry->wszSearchPath);
950 *pbCache = pOptionEntry->bCache;
957 /* help function for setting search path and caching option for certain class */
958 static HRESULT DMUSIC_SetLoaderSettings(IDirectMusicLoader8 *iface, REFGUID pClassID, WCHAR *wszSearchPath, BOOL *pbCache)
960 IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
962 HRESULT result = S_FALSE; /* in case pClassID != GUID_DirectMusicAllTypes and not a valid CLSID */
963 TRACE(": (%p, %s, %p, %p)\n", This, debugstr_dmguid(pClassID), wszSearchPath, pbCache);
965 LIST_FOR_EACH(pEntry, This->pClassSettings) {
966 LPWINE_LOADER_OPTION pOptionEntry = LIST_ENTRY(pEntry, WINE_LOADER_OPTION, entry);
967 /* well, either we have GUID_DirectMusicAllTypes and need to set it to all,
968 or specific CLSID is given and we set it only to it */
969 if (IsEqualGUID (pClassID, &GUID_DirectMusicAllTypes) ||
970 IsEqualCLSID (pClassID, &pOptionEntry->guidClass)) {
972 strcpyW(pOptionEntry->wszSearchPath, wszSearchPath);
974 pOptionEntry->bCache = *pbCache;
982 static HRESULT DMUSIC_InitLoaderSettings(IDirectMusicLoader8 *iface)
984 IDirectMusicLoaderImpl *This = impl_from_IDirectMusicLoader8(iface);
986 /* hard-coded list of classes */
987 static REFCLSID classes[] = {
988 &CLSID_DirectMusicAudioPathConfig,
989 &CLSID_DirectMusicBand,
990 &CLSID_DirectMusicContainer,
991 &CLSID_DirectMusicCollection,
992 &CLSID_DirectMusicChordMap,
993 &CLSID_DirectMusicSegment,
994 &CLSID_DirectMusicScript,
995 &CLSID_DirectMusicSong,
996 &CLSID_DirectMusicStyle,
997 &CLSID_DirectMusicGraph,
998 &CLSID_DirectSoundWave
1002 WCHAR wszCurrent[MAX_PATH];
1004 TRACE(": (%p)\n", This);
1005 GetCurrentDirectoryW (MAX_PATH, wszCurrent);
1007 for (i = 0; i < sizeof(classes)/sizeof(REFCLSID); i++) {
1008 LPWINE_LOADER_OPTION pNewSetting = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(WINE_LOADER_OPTION));
1009 pNewSetting->guidClass = *classes[i];
1010 strcpyW (pNewSetting->wszSearchPath, wszCurrent);
1011 pNewSetting->bCache = TRUE;
1012 list_add_tail (This->pClassSettings, &pNewSetting->entry);