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 const GUID
*classes
[] = {
26 &GUID_DirectMusicAllTypes
, /* Keep as first */
27 &CLSID_DirectMusicAudioPathConfig
,
28 &CLSID_DirectMusicBand
,
29 &CLSID_DirectMusicContainer
,
30 &CLSID_DirectMusicCollection
,
31 &CLSID_DirectMusicChordMap
,
32 &CLSID_DirectMusicSegment
,
33 &CLSID_DirectMusicScript
,
34 &CLSID_DirectMusicSong
,
35 &CLSID_DirectMusicStyle
,
36 &CLSID_DirectMusicGraph
,
37 &CLSID_DirectSoundWave
40 /* cache/alias entry */
44 IDirectMusicObject
*pObject
;
45 BOOL bInvalidDefaultDLS
; /* workaround for enabling caching of "faulty" default dls collection */
48 typedef struct IDirectMusicLoaderImpl
{
49 IDirectMusicLoader8 IDirectMusicLoader8_iface
;
51 WCHAR
*search_paths
[ARRAY_SIZE(classes
)];
52 unsigned int cache_class
;
54 } IDirectMusicLoaderImpl
;
57 static inline IDirectMusicLoaderImpl
* impl_from_IDirectMusicLoader8(IDirectMusicLoader8
*iface
)
59 return CONTAINING_RECORD(iface
, IDirectMusicLoaderImpl
, IDirectMusicLoader8_iface
);
62 static int index_from_class(REFCLSID
class)
66 for (i
= 0; i
< ARRAY_SIZE(classes
); i
++)
67 if (IsEqualGUID(class, classes
[i
]))
73 static inline BOOL
is_cache_enabled(IDirectMusicLoaderImpl
*This
, REFCLSID
class)
75 return !!(This
->cache_class
& 1 << index_from_class(class));
78 static void get_search_path(IDirectMusicLoaderImpl
*This
, REFGUID
class, WCHAR
*path
)
80 int index
= index_from_class(class);
82 if (index
< 0 || !This
->search_paths
[index
])
83 index
= 0; /* Default to GUID_DirectMusicAllTypes */
85 if (This
->search_paths
[index
])
86 lstrcpynW(path
, This
->search_paths
[index
], MAX_PATH
);
91 static HRESULT
DMUSIC_CopyDescriptor(DMUS_OBJECTDESC
*pDst
, DMUS_OBJECTDESC
*pSrc
)
93 if (TRACE_ON(dmloader
))
94 dump_DMUS_OBJECTDESC(pSrc
);
96 /* copy field by field */
97 if (pSrc
->dwValidData
& DMUS_OBJ_CLASS
) pDst
->guidClass
= pSrc
->guidClass
;
98 if (pSrc
->dwValidData
& DMUS_OBJ_OBJECT
) pDst
->guidObject
= pSrc
->guidObject
;
99 if (pSrc
->dwValidData
& DMUS_OBJ_DATE
) pDst
->ftDate
= pSrc
->ftDate
;
100 if (pSrc
->dwValidData
& DMUS_OBJ_VERSION
) pDst
->vVersion
= pSrc
->vVersion
;
101 if (pSrc
->dwValidData
& DMUS_OBJ_NAME
) lstrcpyW (pDst
->wszName
, pSrc
->wszName
);
102 if (pSrc
->dwValidData
& DMUS_OBJ_CATEGORY
) lstrcpyW (pDst
->wszCategory
, pSrc
->wszCategory
);
103 if (pSrc
->dwValidData
& DMUS_OBJ_FILENAME
) lstrcpyW (pDst
->wszFileName
, pSrc
->wszFileName
);
104 if (pSrc
->dwValidData
& DMUS_OBJ_STREAM
) IStream_Clone (pSrc
->pStream
, &pDst
->pStream
);
105 if (pSrc
->dwValidData
& DMUS_OBJ_MEMORY
) {
106 pDst
->pbMemData
= pSrc
->pbMemData
;
107 pDst
->llMemLength
= pSrc
->llMemLength
;
110 pDst
->dwValidData
|= pSrc
->dwValidData
;
114 /*****************************************************************************
115 * IDirectMusicLoaderImpl implementation
117 /* IUnknown/IDirectMusicLoader(8) part: */
119 static HRESULT WINAPI
IDirectMusicLoaderImpl_QueryInterface(IDirectMusicLoader8
*iface
, REFIID riid
, void **ppobj
)
121 IDirectMusicLoaderImpl
*This
= impl_from_IDirectMusicLoader8(iface
);
123 TRACE("(%p, %s, %p)\n",This
, debugstr_dmguid(riid
), ppobj
);
124 if (IsEqualIID (riid
, &IID_IUnknown
) ||
125 IsEqualIID (riid
, &IID_IDirectMusicLoader
) ||
126 IsEqualIID (riid
, &IID_IDirectMusicLoader8
)) {
127 IDirectMusicLoader_AddRef (iface
);
132 WARN(": not found\n");
133 return E_NOINTERFACE
;
136 static ULONG WINAPI
IDirectMusicLoaderImpl_AddRef(IDirectMusicLoader8
*iface
)
138 IDirectMusicLoaderImpl
*This
= impl_from_IDirectMusicLoader8(iface
);
139 ULONG ref
= InterlockedIncrement(&This
->ref
);
141 TRACE("(%p)->(): new ref = %lu\n", iface
, ref
);
146 static ULONG WINAPI
IDirectMusicLoaderImpl_Release(IDirectMusicLoader8
*iface
)
148 IDirectMusicLoaderImpl
*This
= impl_from_IDirectMusicLoader8(iface
);
149 ULONG ref
= InterlockedDecrement(&This
->ref
);
151 TRACE("(%p)->(): new ref = %lu\n", iface
, ref
);
156 IDirectMusicLoader8_ClearCache(iface
, &GUID_DirectMusicAllTypes
);
157 for (i
= 0; i
< ARRAY_SIZE(classes
); i
++)
158 HeapFree(GetProcessHeap(), 0, This
->search_paths
[i
]);
159 HeapFree(GetProcessHeap(), 0, This
);
166 static struct cache_entry
*find_cache_object(IDirectMusicLoaderImpl
*This
, DMUS_OBJECTDESC
*desc
)
168 struct cache_entry
*existing
;
171 * The Object is looked up in the following order.
175 * 4. DMUS_OBJ_FILENAME and DMUS_OBJ_FULLPATH
176 * 5. DMUS_OBJ_NAME and DMUS_OBJ_CATEGORY
178 * 7. DMUS_OBJ_FILENAME
181 if (desc
->dwValidData
& DMUS_OBJ_OBJECT
) {
182 LIST_FOR_EACH_ENTRY(existing
, &This
->cache
, struct cache_entry
, entry
) {
183 if (existing
->Desc
.dwValidData
& DMUS_OBJ_OBJECT
&&
184 IsEqualGUID(&desc
->guidObject
, &existing
->Desc
.guidObject
) ) {
185 TRACE("Found by DMUS_OBJ_OBJECT\n");
191 if (desc
->dwValidData
& DMUS_OBJ_STREAM
)
192 FIXME("Finding DMUS_OBJ_STREAM cached objects currently not supported.\n");
194 if (desc
->dwValidData
& DMUS_OBJ_MEMORY
) {
195 LIST_FOR_EACH_ENTRY(existing
, &This
->cache
, struct cache_entry
, entry
) {
196 if (existing
->Desc
.dwValidData
& DMUS_OBJ_MEMORY
&&
197 desc
->llMemLength
== existing
->Desc
.llMemLength
&&
198 (desc
->pbMemData
== existing
->Desc
.pbMemData
||
199 !memcmp(desc
->pbMemData
, existing
->Desc
.pbMemData
, desc
->llMemLength
)) ) {
200 TRACE("Found by DMUS_OBJ_MEMORY (%d)\n",
201 desc
->pbMemData
== existing
->Desc
.pbMemData
);
207 if ((desc
->dwValidData
& (DMUS_OBJ_FILENAME
| DMUS_OBJ_FULLPATH
)) ==
208 (DMUS_OBJ_FILENAME
| DMUS_OBJ_FULLPATH
)) {
209 LIST_FOR_EACH_ENTRY(existing
, &This
->cache
, struct cache_entry
, entry
) {
210 if ((existing
->Desc
.dwValidData
& (DMUS_OBJ_FILENAME
| DMUS_OBJ_FULLPATH
)) ==
211 (DMUS_OBJ_FILENAME
| DMUS_OBJ_FULLPATH
) &&
212 !wcsncmp(desc
->wszFileName
, existing
->Desc
.wszFileName
, DMUS_MAX_FILENAME
)) {
213 TRACE("Found by DMUS_OBJ_FILENAME | DMUS_OBJ_FULLPATH\n");
219 if ((desc
->dwValidData
& (DMUS_OBJ_NAME
| DMUS_OBJ_CATEGORY
)) ==
220 (DMUS_OBJ_NAME
| DMUS_OBJ_CATEGORY
)) {
221 LIST_FOR_EACH_ENTRY(existing
, &This
->cache
, struct cache_entry
, entry
) {
222 if ((existing
->Desc
.dwValidData
& (DMUS_OBJ_NAME
| DMUS_OBJ_CATEGORY
)) ==
223 (DMUS_OBJ_NAME
| DMUS_OBJ_CATEGORY
) &&
224 !wcsncmp(desc
->wszName
, existing
->Desc
.wszName
, DMUS_MAX_NAME
) &&
225 !wcsncmp(desc
->wszCategory
, existing
->Desc
.wszCategory
, DMUS_MAX_CATEGORY
)) {
226 TRACE("Found by DMUS_OBJ_NAME | DMUS_OBJ_CATEGORY\n");
232 if ((desc
->dwValidData
& DMUS_OBJ_NAME
) == DMUS_OBJ_NAME
) {
233 LIST_FOR_EACH_ENTRY(existing
, &This
->cache
, struct cache_entry
, entry
) {
234 if ((existing
->Desc
.dwValidData
& DMUS_OBJ_NAME
) == DMUS_OBJ_NAME
&&
235 !wcsncmp(desc
->wszName
, existing
->Desc
.wszName
, DMUS_MAX_NAME
)) {
236 TRACE("Found by DMUS_OBJ_NAME\n");
242 if ((desc
->dwValidData
& DMUS_OBJ_FILENAME
) == DMUS_OBJ_FILENAME
) {
243 LIST_FOR_EACH_ENTRY(existing
, &This
->cache
, struct cache_entry
, entry
) {
244 if (((existing
->Desc
.dwValidData
& DMUS_OBJ_FILENAME
) == DMUS_OBJ_FILENAME
) &&
245 !wcsncmp(desc
->wszFileName
, existing
->Desc
.wszFileName
, DMUS_MAX_FILENAME
)) {
246 TRACE("Found by DMUS_OBJ_FILENAME\n");
255 static HRESULT WINAPI
IDirectMusicLoaderImpl_GetObject(IDirectMusicLoader8
*iface
, DMUS_OBJECTDESC
*pDesc
, REFIID riid
, void **ppv
)
257 IDirectMusicLoaderImpl
*This
= impl_from_IDirectMusicLoader8(iface
);
258 HRESULT result
= S_OK
;
259 HRESULT ret
= S_OK
; /* used at the end of function, to determine whether everything went OK */
260 struct cache_entry
*pExistingEntry
, *pObjectEntry
= NULL
;
262 IPersistStream
* pPersistStream
= NULL
;
264 LPDIRECTMUSICOBJECT pObject
;
265 DMUS_OBJECTDESC GotDesc
;
268 TRACE("(%p)->(%p, %s, %p)\n", This
, pDesc
, debugstr_dmguid(riid
), ppv
);
270 if (TRACE_ON(dmloader
))
271 dump_DMUS_OBJECTDESC(pDesc
);
273 /* sometimes it happens that guidClass is missing... which is a BadThingTM */
274 if (!(pDesc
->dwValidData
& DMUS_OBJ_CLASS
)) {
275 ERR(": guidClass not valid but needed\n");
277 return DMUS_E_LOADER_NOCLASSID
;
280 /* Iterate through the list of objects we know about; these are either loaded
281 * (GetObject, LoadObjectFromFile) or set via SetObject; */
282 TRACE(": looking if we have object in the cache or if it can be found via alias\n");
283 pExistingEntry
= find_cache_object(This
, pDesc
);
284 if (pExistingEntry
) {
286 if (pExistingEntry
->bInvalidDefaultDLS
) {
287 TRACE(": found faulty default DLS collection... enabling M$ compliant behaviour\n");
288 return DMUS_E_LOADER_NOFILENAME
;
291 if (pExistingEntry
->Desc
.dwValidData
& DMUS_OBJ_LOADED
) {
292 TRACE(": already loaded\n");
293 return IDirectMusicObject_QueryInterface(pExistingEntry
->pObject
, riid
, ppv
);
296 TRACE(": not loaded yet\n");
297 pObjectEntry
= pExistingEntry
;
300 /* basically, if we found alias, we use its descriptor to load...
301 else we use info we were given */
303 TRACE(": found alias entry for requested object... using stored info\n");
304 /* I think in certain cases it can happen that entry's descriptor lacks info about
305 where to load from (e.g.: if we loaded from stream and then released object
306 from cache; then only its CLSID, GUID and perhaps name are left); so just
307 overwrite whatever info the entry has (since it ought to be 100% correct) */
308 DMUSIC_CopyDescriptor (pDesc
, &pObjectEntry
->Desc
);
309 /*pDesc = &pObjectEntry->Desc; */ /* FIXME: is this OK? */
311 TRACE(": no cache/alias entry found for requested object\n");
314 if (pDesc
->dwValidData
& DMUS_OBJ_URL
) {
315 TRACE(": loading from URLs not supported yet\n");
316 return DMUS_E_LOADER_FORMATNOTSUPPORTED
;
318 else if (pDesc
->dwValidData
& DMUS_OBJ_FILENAME
) {
319 /* load object from file */
320 /* generate filename; if it's full path, don't add search
321 directory path, otherwise do */
322 WCHAR wszFileName
[MAX_PATH
];
324 if (pDesc
->dwValidData
& DMUS_OBJ_FULLPATH
) {
325 lstrcpyW(wszFileName
, pDesc
->wszFileName
);
328 get_search_path(This
, &pDesc
->guidClass
, wszFileName
);
329 p
= wszFileName
+ lstrlenW(wszFileName
);
330 if (p
> wszFileName
&& p
[-1] != '\\') *p
++ = '\\';
331 lstrcpyW(p
, pDesc
->wszFileName
);
333 TRACE(": loading from file (%s)\n", debugstr_w(wszFileName
));
334 /* create stream and associate it with file */
335 result
= DMUSIC_CreateDirectMusicLoaderFileStream ((LPVOID
*)&pStream
);
336 if (FAILED(result
)) {
337 ERR(": could not create file stream\n");
340 result
= IDirectMusicLoaderFileStream_Attach (pStream
, wszFileName
, iface
);
341 if (FAILED(result
)) {
342 ERR(": could not attach stream to file\n");
343 IStream_Release (pStream
);
347 else if (pDesc
->dwValidData
& DMUS_OBJ_MEMORY
) {
348 /* load object from resource */
349 TRACE(": loading from resource\n");
350 /* create stream and associate it with given resource */
351 result
= DMUSIC_CreateDirectMusicLoaderResourceStream ((LPVOID
*)&pStream
);
352 if (FAILED(result
)) {
353 ERR(": could not create resource stream\n");
356 result
= IDirectMusicLoaderResourceStream_Attach (pStream
, pDesc
->pbMemData
, pDesc
->llMemLength
, 0, iface
);
357 if (FAILED(result
)) {
358 ERR(": could not attach stream to resource\n");
359 IStream_Release (pStream
);
363 else if (pDesc
->dwValidData
& DMUS_OBJ_STREAM
) {
364 /* load object from stream */
365 TRACE(": loading from stream\n");
366 /* create universal stream and associate it with given one */
367 result
= DMUSIC_CreateDirectMusicLoaderGenericStream ((LPVOID
*)&pStream
);
368 if (FAILED(result
)) {
369 ERR(": could not create generic stream\n");
372 result
= IDirectMusicLoaderGenericStream_Attach (pStream
, pDesc
->pStream
, iface
);
373 if (FAILED(result
)) {
374 ERR(": failed to attach stream\n");
375 IStream_Release (pStream
);
379 /* nowhere to load from */
380 FIXME(": unknown/unsupported way of loading\n");
381 return DMUS_E_LOADER_NOFILENAME
; /* test shows this is returned */
385 result
= CoCreateInstance (&pDesc
->guidClass
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IDirectMusicObject
, (LPVOID
*)&pObject
);
386 if (FAILED(result
)) {
387 IStream_Release(pStream
);
388 ERR(": could not create object\n");
391 /* acquire PersistStream interface */
392 result
= IDirectMusicObject_QueryInterface (pObject
, &IID_IPersistStream
, (LPVOID
*)&pPersistStream
);
393 if (FAILED(result
)) {
394 IStream_Release(pStream
);
395 IDirectMusicObject_Release(pObject
);
396 ERR("failed to Query\n");
400 result
= IPersistStream_Load (pPersistStream
, pStream
);
401 if (result
!= S_OK
) {
402 IStream_Release(pStream
);
403 IPersistStream_Release(pPersistStream
);
404 IDirectMusicObject_Release(pObject
);
405 WARN(": failed to (completely) load object (%#lx)\n", result
);
409 DM_STRUCT_INIT(&GotDesc
);
410 result
= IDirectMusicObject_GetDescriptor (pObject
, &GotDesc
);
411 /* set filename (if we loaded via filename) */
412 if (pDesc
->dwValidData
& DMUS_OBJ_FILENAME
) {
413 GotDesc
.dwValidData
|= (pDesc
->dwValidData
& (DMUS_OBJ_FILENAME
| DMUS_OBJ_FULLPATH
));
414 lstrcpyW (GotDesc
.wszFileName
, pDesc
->wszFileName
);
416 if (FAILED(result
)) {
417 IStream_Release(pStream
);
418 IPersistStream_Release(pPersistStream
);
419 IDirectMusicObject_Release(pObject
);
420 ERR(": failed to get descriptor\n");
423 /* release all loading related stuff */
424 IStream_Release (pStream
);
425 IPersistStream_Release (pPersistStream
);
427 /* add object to cache/overwrite existing info (if cache is enabled) */
428 bCache
= is_cache_enabled(This
, &pDesc
->guidClass
);
431 pObjectEntry
= HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY
, sizeof(*pObjectEntry
));
432 DM_STRUCT_INIT(&pObjectEntry
->Desc
);
433 DMUSIC_CopyDescriptor (&pObjectEntry
->Desc
, &GotDesc
);
434 pObjectEntry
->pObject
= pObject
;
435 pObjectEntry
->bInvalidDefaultDLS
= FALSE
;
436 list_add_head(&This
->cache
, &pObjectEntry
->entry
);
438 DMUSIC_CopyDescriptor (&pObjectEntry
->Desc
, &GotDesc
);
439 pObjectEntry
->pObject
= pObject
;
440 pObjectEntry
->bInvalidDefaultDLS
= FALSE
;
442 TRACE(": filled in cache entry\n");
443 } else TRACE(": caching disabled\n");
445 result
= IDirectMusicObject_QueryInterface (pObject
, riid
, ppv
);
446 if (!bCache
) IDirectMusicObject_Release (pObject
); /* since loader's reference is not needed */
447 /* if there was trouble with loading, and if no other error occurred,
448 we should return DMUS_S_PARTIALLOAD; else, error is returned */
455 static HRESULT WINAPI
IDirectMusicLoaderImpl_SetObject(IDirectMusicLoader8
*iface
, DMUS_OBJECTDESC
*pDesc
)
457 IDirectMusicLoaderImpl
*This
= impl_from_IDirectMusicLoader8(iface
);
459 LPDIRECTMUSICOBJECT pObject
;
460 DMUS_OBJECTDESC Desc
;
461 struct cache_entry
*pObjectEntry
, *pNewEntry
;
464 TRACE("(%p)->(%p)\n", This
, pDesc
);
466 if (TRACE_ON(dmloader
))
467 dump_DMUS_OBJECTDESC(pDesc
);
469 /* create stream and load additional info from it */
470 if (pDesc
->dwValidData
& DMUS_OBJ_FILENAME
) {
471 /* generate filename; if it's full path, don't add search
472 directory path, otherwise do */
473 WCHAR wszFileName
[MAX_PATH
];
475 if (pDesc
->dwValidData
& DMUS_OBJ_FULLPATH
) {
476 lstrcpyW(wszFileName
, pDesc
->wszFileName
);
479 get_search_path(This
, &pDesc
->guidClass
, wszFileName
);
480 p
= wszFileName
+ lstrlenW(wszFileName
);
481 if (p
> wszFileName
&& p
[-1] != '\\') *p
++ = '\\';
482 lstrcpyW(p
, pDesc
->wszFileName
);
485 hr
= DMUSIC_CreateDirectMusicLoaderFileStream ((LPVOID
*)&pStream
);
487 ERR(": could not create file stream\n");
488 return DMUS_E_LOADER_FAILEDOPEN
;
491 hr
= IDirectMusicLoaderFileStream_Attach (pStream
, wszFileName
, iface
);
493 ERR(": could not attach stream to file %s, make sure it exists\n", debugstr_w(wszFileName
));
494 IStream_Release (pStream
);
495 return DMUS_E_LOADER_FAILEDOPEN
;
498 else if (pDesc
->dwValidData
& DMUS_OBJ_STREAM
) {
500 hr
= DMUSIC_CreateDirectMusicLoaderGenericStream ((LPVOID
*)&pStream
);
502 ERR(": could not create generic stream\n");
503 return DMUS_E_LOADER_FAILEDOPEN
;
506 hr
= IDirectMusicLoaderGenericStream_Attach (pStream
, pDesc
->pStream
, iface
);
508 ERR(": could not attach stream\n");
509 IStream_Release (pStream
);
510 return DMUS_E_LOADER_FAILEDOPEN
;
513 else if (pDesc
->dwValidData
& DMUS_OBJ_MEMORY
) {
515 hr
= DMUSIC_CreateDirectMusicLoaderResourceStream ((LPVOID
*)&pStream
);
517 ERR(": could not create resource stream\n");
518 return DMUS_E_LOADER_FAILEDOPEN
;
521 hr
= IDirectMusicLoaderResourceStream_Attach (pStream
, pDesc
->pbMemData
, pDesc
->llMemLength
, 0, iface
);
523 ERR(": could not attach stream to resource\n");
524 IStream_Release (pStream
);
525 return DMUS_E_LOADER_FAILEDOPEN
;
529 ERR(": no way to get additional info\n");
530 return DMUS_E_LOADER_FAILEDOPEN
;
534 hr
= CoCreateInstance (&pDesc
->guidClass
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IDirectMusicObject
, (LPVOID
*)&pObject
);
536 IStream_Release(pStream
);
537 ERR("Object creation of %s failed %#lx\n", debugstr_guid(&pDesc
->guidClass
),hr
);
538 return DMUS_E_LOADER_FAILEDOPEN
;
541 DM_STRUCT_INIT(&Desc
);
542 if (FAILED(IDirectMusicObject_ParseDescriptor (pObject
, pStream
, &Desc
))) {
543 IStream_Release(pStream
);
544 IDirectMusicObject_Release(pObject
);
545 ERR(": couldn't parse descriptor\n");
546 return DMUS_E_LOADER_FORMATNOTSUPPORTED
;
549 /* copy elements from parsed descriptor into input descriptor; this sets new info, overwriting if necessary,
550 but leaves info that's provided by input and not available from stream */
551 DMUSIC_CopyDescriptor (pDesc
, &Desc
);
553 /* release everything */
554 IDirectMusicObject_Release (pObject
);
555 IStream_Release (pStream
);
557 /* sometimes it happens that twisted programs call SetObject for same object twice...
558 in such cases, native loader returns S_OK and does nothing... a sound plan */
559 LIST_FOR_EACH_ENTRY(pObjectEntry
, &This
->cache
, struct cache_entry
, entry
) {
560 if (!memcmp (&pObjectEntry
->Desc
, pDesc
, sizeof(DMUS_OBJECTDESC
))) {
561 TRACE(": exactly same entry already exists\n");
567 TRACE(": adding alias entry with following info:\n");
568 if (TRACE_ON(dmloader
))
569 dump_DMUS_OBJECTDESC(pDesc
);
570 pNewEntry
= HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY
, sizeof(*pNewEntry
));
571 /* use this function instead of pure memcpy due to streams (memcpy just copies pointer),
572 which is basically used further by app that called SetDescriptor... better safety than exception */
573 DMUSIC_CopyDescriptor (&pNewEntry
->Desc
, pDesc
);
574 list_add_head(&This
->cache
, &pNewEntry
->entry
);
579 static HRESULT WINAPI
IDirectMusicLoaderImpl_SetSearchDirectory(IDirectMusicLoader8
*iface
,
580 REFGUID
class, WCHAR
*path
, BOOL clear
)
582 IDirectMusicLoaderImpl
*This
= impl_from_IDirectMusicLoader8(iface
);
583 int index
= index_from_class(class);
586 TRACE("(%p, %s, %s, %d)\n", This
, debugstr_dmguid(class), debugstr_w(path
), clear
);
592 attr
= GetFileAttributesW(path
);
593 if (attr
== INVALID_FILE_ATTRIBUTES
|| !(attr
& FILE_ATTRIBUTE_DIRECTORY
))
594 return DMUS_E_LOADER_BADPATH
;
598 FIXME("clear flag ignored\n");
600 /* Ignore invalid GUIDs */
604 if (!This
->search_paths
[index
])
605 This
->search_paths
[index
] = HeapAlloc(GetProcessHeap(), 0, MAX_PATH
);
606 else if (!wcsncmp(This
->search_paths
[index
], path
, MAX_PATH
))
609 lstrcpynW(This
->search_paths
[index
], path
, MAX_PATH
);
614 static HRESULT WINAPI
IDirectMusicLoaderImpl_ScanDirectory(IDirectMusicLoader8
*iface
, REFGUID rguidClass
, WCHAR
*pwzFileExtension
, WCHAR
*pwzScanFileName
)
616 IDirectMusicLoaderImpl
*This
= impl_from_IDirectMusicLoader8(iface
);
617 WIN32_FIND_DATAW FileData
;
619 WCHAR wszSearchString
[MAX_PATH
];
622 TRACE("(%p, %s, %s, %s)\n", This
, debugstr_dmguid(rguidClass
), debugstr_w(pwzFileExtension
),
623 debugstr_w(pwzScanFileName
));
624 if (index_from_class(rguidClass
) <= 0) {
625 ERR(": rguidClass invalid CLSID\n");
626 return REGDB_E_CLASSNOTREG
;
629 if (!pwzFileExtension
)
632 /* get search path for given class */
633 get_search_path(This
, rguidClass
, wszSearchString
);
635 p
= wszSearchString
+ lstrlenW(wszSearchString
);
636 if (p
> wszSearchString
&& p
[-1] != '\\') *p
++ = '\\';
637 *p
++ = '*'; /* any file */
638 if (lstrcmpW (pwzFileExtension
, L
"*"))
639 *p
++ = '.'; /* if we have actual extension, put a dot */
640 lstrcpyW (p
, pwzFileExtension
);
642 TRACE(": search string: %s\n", debugstr_w(wszSearchString
));
644 hSearch
= FindFirstFileW (wszSearchString
, &FileData
);
645 if (hSearch
== INVALID_HANDLE_VALUE
) {
646 TRACE(": no files found\n");
651 DMUS_OBJECTDESC Desc
;
652 DM_STRUCT_INIT(&Desc
);
653 Desc
.dwValidData
= DMUS_OBJ_CLASS
| DMUS_OBJ_FILENAME
| DMUS_OBJ_DATE
;
654 Desc
.guidClass
= *rguidClass
;
655 lstrcpyW (Desc
.wszFileName
, FileData
.cFileName
);
656 FileTimeToLocalFileTime (&FileData
.ftCreationTime
, &Desc
.ftDate
);
657 IDirectMusicLoader8_SetObject (iface
, &Desc
);
659 if (!FindNextFileW (hSearch
, &FileData
)) {
660 if (GetLastError () == ERROR_NO_MORE_FILES
) {
661 TRACE(": search completed\n");
664 ERR(": could not get next file\n");
673 static HRESULT WINAPI
IDirectMusicLoaderImpl_CacheObject(IDirectMusicLoader8
*iface
,
674 IDirectMusicObject
*object
)
676 IDirectMusicLoaderImpl
*This
= impl_from_IDirectMusicLoader8(iface
);
677 DMUS_OBJECTDESC desc
;
678 struct cache_entry
*entry
;
680 TRACE("(%p, %p)\n", This
, object
);
682 DM_STRUCT_INIT(&desc
);
683 IDirectMusicObject_GetDescriptor(object
, &desc
);
685 /* Iterate through the list and check if we have an alias (without object), corresponding
686 to the descriptor of the input object */
687 entry
= find_cache_object(This
, &desc
);
689 if ((entry
->Desc
.dwValidData
& DMUS_OBJ_LOADED
) && entry
->pObject
) {
690 TRACE("Object already loaded.\n");
694 entry
->Desc
.dwValidData
|= DMUS_OBJ_LOADED
;
695 entry
->pObject
= object
;
696 IDirectMusicObject_AddRef(entry
->pObject
);
700 return DMUS_E_LOADER_OBJECTNOTFOUND
;
703 static HRESULT WINAPI
IDirectMusicLoaderImpl_ReleaseObject(IDirectMusicLoader8
*iface
,
704 IDirectMusicObject
*object
)
706 IDirectMusicLoaderImpl
*This
= impl_from_IDirectMusicLoader8(iface
);
707 DMUS_OBJECTDESC desc
;
708 struct cache_entry
*entry
;
710 TRACE("(%p, %p)\n", This
, object
);
715 DM_STRUCT_INIT(&desc
);
716 IDirectMusicObject_GetDescriptor(object
, &desc
);
718 TRACE("Looking for the object in cache\n");
719 entry
= find_cache_object(This
, &desc
);
721 dump_DMUS_OBJECTDESC(&entry
->Desc
);
723 if (entry
->pObject
&& entry
->Desc
.dwValidData
& DMUS_OBJ_LOADED
) {
724 IDirectMusicObject_Release(entry
->pObject
);
725 entry
->pObject
= NULL
;
726 entry
->Desc
.dwValidData
&= ~DMUS_OBJ_LOADED
;
734 static HRESULT WINAPI
IDirectMusicLoaderImpl_ClearCache(IDirectMusicLoader8
*iface
, REFGUID
class)
736 IDirectMusicLoaderImpl
*This
= impl_from_IDirectMusicLoader8(iface
);
737 struct cache_entry
*obj
, *obj2
;
739 TRACE("(%p, %s)\n", This
, debugstr_dmguid(class));
741 LIST_FOR_EACH_ENTRY_SAFE(obj
, obj2
, &This
->cache
, struct cache_entry
, entry
) {
742 if ((IsEqualGUID(class, &GUID_DirectMusicAllTypes
) || IsEqualGUID(class, &obj
->Desc
.guidClass
)) &&
743 (obj
->Desc
.dwValidData
& DMUS_OBJ_LOADED
)) {
744 /* basically, wrap to ReleaseObject for each object found */
745 IDirectMusicLoader8_ReleaseObject(iface
, obj
->pObject
);
746 list_remove(&obj
->entry
);
747 HeapFree(GetProcessHeap(), 0, obj
);
754 static HRESULT WINAPI
IDirectMusicLoaderImpl_EnableCache(IDirectMusicLoader8
*iface
, REFGUID
class,
757 IDirectMusicLoaderImpl
*This
= impl_from_IDirectMusicLoader8(iface
);
760 TRACE("(%p, %s, %d)\n", This
, debugstr_dmguid(class), enable
);
762 current
= is_cache_enabled(This
, class);
764 if (IsEqualGUID(class, &GUID_DirectMusicAllTypes
))
765 This
->cache_class
= enable
? ~0 : 0;
767 int idx
= index_from_class(class);
768 if (idx
== -1) return S_FALSE
;
770 This
->cache_class
|= 1 << idx
;
772 This
->cache_class
&= ~(1 << idx
);
776 IDirectMusicLoader8_ClearCache(iface
, class);
778 if (current
== enable
)
784 static HRESULT WINAPI
IDirectMusicLoaderImpl_EnumObject(IDirectMusicLoader8
*iface
, REFGUID rguidClass
, DWORD dwIndex
, DMUS_OBJECTDESC
*pDesc
)
786 IDirectMusicLoaderImpl
*This
= impl_from_IDirectMusicLoader8(iface
);
788 struct cache_entry
*pObjectEntry
;
789 TRACE("(%p, %s, %ld, %p)\n", This
, debugstr_dmguid(rguidClass
), dwIndex
, pDesc
);
791 DM_STRUCT_INIT(pDesc
);
793 LIST_FOR_EACH_ENTRY(pObjectEntry
, &This
->cache
, struct cache_entry
, entry
) {
794 if (IsEqualGUID (rguidClass
, &GUID_DirectMusicAllTypes
) || IsEqualGUID (rguidClass
, &pObjectEntry
->Desc
.guidClass
)) {
795 if (dwCount
== dwIndex
) {
796 *pDesc
= pObjectEntry
->Desc
;
797 /* we aren't supposed to reveal this info */
798 pDesc
->dwValidData
&= ~(DMUS_OBJ_MEMORY
| DMUS_OBJ_STREAM
);
799 pDesc
->pbMemData
= NULL
;
800 pDesc
->llMemLength
= 0;
801 pDesc
->pStream
= NULL
;
808 TRACE(": not found\n");
812 static void WINAPI
IDirectMusicLoaderImpl_CollectGarbage(IDirectMusicLoader8
*iface
)
814 FIXME("(%p)->(): stub\n", iface
);
817 static HRESULT WINAPI
IDirectMusicLoaderImpl_ReleaseObjectByUnknown(IDirectMusicLoader8
*iface
, IUnknown
*pObject
)
819 IDirectMusicLoaderImpl
*This
= impl_from_IDirectMusicLoader8(iface
);
821 LPDIRECTMUSICOBJECT pObjectInterface
;
823 TRACE("(%p, %p)\n", This
, pObject
);
825 if (IsBadReadPtr (pObject
, sizeof(*pObject
))) {
826 ERR(": pObject bad write pointer\n");
829 /* we simply get IDirectMusicObject interface */
830 result
= IUnknown_QueryInterface (pObject
, &IID_IDirectMusicObject
, (LPVOID
*)&pObjectInterface
);
831 if (FAILED(result
)) return result
;
832 /* and release it in old-fashioned way */
833 result
= IDirectMusicLoader8_ReleaseObject (iface
, pObjectInterface
);
834 IDirectMusicObject_Release (pObjectInterface
);
839 static HRESULT WINAPI
IDirectMusicLoaderImpl_LoadObjectFromFile(IDirectMusicLoader8
*iface
, REFGUID rguidClassID
, REFIID iidInterfaceID
, WCHAR
*pwzFilePath
, void **ppObject
)
841 IDirectMusicLoaderImpl
*This
= impl_from_IDirectMusicLoader8(iface
);
842 DMUS_OBJECTDESC ObjDesc
;
843 WCHAR wszLoaderSearchPath
[MAX_PATH
];
845 TRACE("(%p, %s, %s, %s, %p): wrapping to IDirectMusicLoaderImpl_GetObject\n", This
, debugstr_dmguid(rguidClassID
), debugstr_dmguid(iidInterfaceID
), debugstr_w(pwzFilePath
), ppObject
);
847 DM_STRUCT_INIT(&ObjDesc
);
848 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 */
849 ObjDesc
.guidClass
= *rguidClassID
;
850 /* OK, MSDN says that search order is the following:
851 - current directory (DONE)
852 - windows search path (FIXME: how do I get that?)
853 - loader's search path (DONE)
855 get_search_path(This
, rguidClassID
, wszLoaderSearchPath
);
856 /* search in current directory */
857 if (!SearchPathW(NULL
, pwzFilePath
, NULL
, ARRAY_SIZE(ObjDesc
.wszFileName
), ObjDesc
.wszFileName
, NULL
) &&
858 /* search in loader's search path */
859 !SearchPathW(wszLoaderSearchPath
, pwzFilePath
, NULL
, ARRAY_SIZE(ObjDesc
.wszFileName
), ObjDesc
.wszFileName
, NULL
)) {
860 /* cannot find file */
861 TRACE(": cannot find file\n");
862 return DMUS_E_LOADER_FAILEDOPEN
;
865 TRACE(": full file path = %s\n", debugstr_w (ObjDesc
.wszFileName
));
867 return IDirectMusicLoader_GetObject(iface
, &ObjDesc
, iidInterfaceID
, ppObject
);
870 static const IDirectMusicLoader8Vtbl DirectMusicLoader_Loader_Vtbl
= {
871 IDirectMusicLoaderImpl_QueryInterface
,
872 IDirectMusicLoaderImpl_AddRef
,
873 IDirectMusicLoaderImpl_Release
,
874 IDirectMusicLoaderImpl_GetObject
,
875 IDirectMusicLoaderImpl_SetObject
,
876 IDirectMusicLoaderImpl_SetSearchDirectory
,
877 IDirectMusicLoaderImpl_ScanDirectory
,
878 IDirectMusicLoaderImpl_CacheObject
,
879 IDirectMusicLoaderImpl_ReleaseObject
,
880 IDirectMusicLoaderImpl_ClearCache
,
881 IDirectMusicLoaderImpl_EnableCache
,
882 IDirectMusicLoaderImpl_EnumObject
,
883 IDirectMusicLoaderImpl_CollectGarbage
,
884 IDirectMusicLoaderImpl_ReleaseObjectByUnknown
,
885 IDirectMusicLoaderImpl_LoadObjectFromFile
888 /* help function for DMUSIC_SetDefaultDLS */
889 static HRESULT
DMUSIC_GetDefaultGMPath (WCHAR wszPath
[MAX_PATH
]) {
891 DWORD returnType
, sizeOfReturnBuffer
= MAX_PATH
;
892 char szPath
[MAX_PATH
];
894 if ((RegOpenKeyExA (HKEY_LOCAL_MACHINE
, "Software\\Microsoft\\DirectMusic" , 0, KEY_READ
, &hkDM
) != ERROR_SUCCESS
) ||
895 (RegQueryValueExA (hkDM
, "GMFilePath", NULL
, &returnType
, (LPBYTE
) szPath
, &sizeOfReturnBuffer
) != ERROR_SUCCESS
)) {
896 WARN(": registry entry missing\n" );
899 /* FIXME: Check return types to ensure we're interpreting data right */
900 MultiByteToWideChar (CP_ACP
, 0, szPath
, -1, wszPath
, MAX_PATH
);
905 /* for ClassFactory */
906 HRESULT
create_dmloader(REFIID lpcGUID
, void **ppobj
)
908 IDirectMusicLoaderImpl
*obj
;
909 DMUS_OBJECTDESC Desc
;
910 struct cache_entry
*dls
;
913 TRACE("(%s, %p)\n", debugstr_dmguid(lpcGUID
), ppobj
);
914 obj
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IDirectMusicLoaderImpl
));
917 return E_OUTOFMEMORY
;
919 obj
->IDirectMusicLoader8_iface
.lpVtbl
= &DirectMusicLoader_Loader_Vtbl
;
920 obj
->ref
= 0; /* Will be inited with QueryInterface */
921 list_init(&obj
->cache
);
922 /* Caching is enabled by default for all classes */
923 obj
->cache_class
= ~0;
925 /* set default DLS collection (via SetObject... so that loading via DMUS_OBJ_OBJECT is possible) */
926 DM_STRUCT_INIT(&Desc
);
927 Desc
.dwValidData
= DMUS_OBJ_CLASS
| DMUS_OBJ_FILENAME
| DMUS_OBJ_FULLPATH
| DMUS_OBJ_OBJECT
;
928 Desc
.guidClass
= CLSID_DirectMusicCollection
;
929 Desc
.guidObject
= GUID_DefaultGMCollection
;
930 DMUSIC_GetDefaultGMPath (Desc
.wszFileName
);
931 IDirectMusicLoader_SetObject(&obj
->IDirectMusicLoader8_iface
, &Desc
);
932 /* and now the workaroundTM for "invalid" default DLS; basically,
933 my tests showed that if GUID chunk is present in default DLS
934 collection, loader treats it as "invalid" and returns
935 DMUS_E_LOADER_NOFILENAME for all requests for it; basically, we check
936 if out input guidObject was overwritten */
937 pEntry
= list_head(&obj
->cache
);
938 dls
= LIST_ENTRY(pEntry
, struct cache_entry
, entry
);
939 if (!IsEqualGUID(&Desc
.guidObject
, &GUID_DefaultGMCollection
)) {
940 dls
->bInvalidDefaultDLS
= TRUE
;
945 return IDirectMusicLoader_QueryInterface(&obj
->IDirectMusicLoader8_iface
, lpcGUID
, ppobj
);