Commit 1d0585c4 authored by Rok Mandeljc's avatar Rok Mandeljc Committed by Alexandre Julliard

Rewritten cache code and fixed a bug that was in it.

parent 87256791
...@@ -33,23 +33,19 @@ ...@@ -33,23 +33,19 @@
#include "dmplugin.h" #include "dmplugin.h"
#include "dmusicf.h" #include "dmusicf.h"
#include "dsound.h" #include "dsound.h"
#include "wine/list.h"
typedef struct _DMUS_PRIVATE_CACHE_ENTRY /*****************************************************************************
{ * Auxiliary definitions
*/
typedef struct _DMUS_PRIVATE_CACHE_ENTRY {
struct list entry; /* for listing elements */
GUID guidObject; GUID guidObject;
WCHAR pwzFileName[MAX_PATH]; WCHAR wzFileName[MAX_PATH];
IDirectMusicObject* pObject; IDirectMusicObject* pObject;
} DMUS_PRIVATE_CACHE_ENTRY, *LPDMUS_PRIVATE_CACHE_ENTRY; } DMUS_PRIVATE_CACHE_ENTRY, *LPDMUS_PRIVATE_CACHE_ENTRY;
typedef struct _DMUS_PRIVATE_OBJECT_REFERENCE DMUS_PRIVATE_OBJECT_REFERENCE;
struct _DMUS_PRIVATE_OBJECT_REFERENCE {
DMUS_PRIVATE_OBJECT_REFERENCE* pNext;
WCHAR pwsFileName[MAX_PATH];
GUID guidObject;
IDirectMusicObject* pObject;
};
/***************************************************************************** /*****************************************************************************
* Interfaces * Interfaces
...@@ -103,9 +99,8 @@ struct IDirectMusicLoader8Impl ...@@ -103,9 +99,8 @@ struct IDirectMusicLoader8Impl
/* IDirectMusicLoaderImpl fields */ /* IDirectMusicLoaderImpl fields */
WCHAR wzSearchPath[MAX_PATH]; WCHAR wzSearchPath[MAX_PATH];
/* simple cache */ /* simple cache (linked list) */
LPDMUS_PRIVATE_CACHE_ENTRY pCache; /* cache entries */ struct list CacheList;
DWORD dwCacheSize; /* nr. of entries */
}; };
/* IUnknown: */ /* IUnknown: */
......
...@@ -72,32 +72,37 @@ ULONG WINAPI IDirectMusicLoader8Impl_Release (LPDIRECTMUSICLOADER8 iface) ...@@ -72,32 +72,37 @@ ULONG WINAPI IDirectMusicLoader8Impl_Release (LPDIRECTMUSICLOADER8 iface)
HRESULT WINAPI IDirectMusicLoader8Impl_GetObject (LPDIRECTMUSICLOADER8 iface, LPDMUS_OBJECTDESC pDesc, REFIID riid, LPVOID* ppv) HRESULT WINAPI IDirectMusicLoader8Impl_GetObject (LPDIRECTMUSICLOADER8 iface, LPDMUS_OBJECTDESC pDesc, REFIID riid, LPVOID* ppv)
{ {
IDirectMusicObject* pObject; IDirectMusicObject* pObject;
DMUS_OBJECTDESC desc;
ICOM_THIS(IDirectMusicLoader8Impl,iface); ICOM_THIS(IDirectMusicLoader8Impl,iface);
int i;
HRESULT result; HRESULT result;
struct list *listEntry;
DMUS_PRIVATE_CACHE_ENTRY *cacheEntry;
LPDMUS_PRIVATE_CACHE_ENTRY newEntry;
TRACE("(%p, %p, %s, %p)\n", This, pDesc, debugstr_guid(riid), ppv); TRACE("(%p, %p, %s, %p)\n", This, pDesc, debugstr_guid(riid), ppv);
TRACE(": looking up cache");
/* first, check if requested object is already in cache (check by name and GUID) */ TRACE("looking up cache...\n");
for (i = 0; i < This->dwCacheSize; i++) {
if (pDesc->dwValidData & DMUS_OBJ_OBJECT) { LIST_FOR_EACH (listEntry, &This->CacheList) {
if (IsEqualGUID (&pDesc->guidObject, &This->pCache[i].guidObject)) { cacheEntry = LIST_ENTRY( listEntry, DMUS_PRIVATE_CACHE_ENTRY, entry );
TRACE(": object already exist in cache (found by GUID)\n");
if (This->pCache[i].pObject) { if ((pDesc->dwValidData & DMUS_OBJ_OBJECT) || (pDesc->dwValidData & DMUS_OBJ_FILENAME)) {
return IDirectMusicObject_QueryInterface (This->pCache[i].pObject, riid, ppv); if (pDesc->dwValidData & DMUS_OBJ_OBJECT) {
if (IsEqualGUID (&cacheEntry->guidObject, &pDesc->guidObject)) {
TRACE(": found it by GUID\n");
if (cacheEntry->pObject)
return IDirectMusicObject_QueryInterface ((LPDIRECTMUSICOBJECT)cacheEntry->pObject, riid, ppv);
} }
} }
} else if (pDesc->dwValidData & DMUS_OBJ_FILENAME) { if (pDesc->dwValidData & DMUS_OBJ_FILENAME) {
if (This->pCache[i].pwzFileName && !strncmpW(pDesc->wszFileName, This->pCache[i].pwzFileName, MAX_PATH)) { if (cacheEntry->wzFileName && !strncmpW (pDesc->wszFileName, cacheEntry->wzFileName, MAX_PATH)) {
TRACE(": object already exist in cache (found by file name)\n"); TRACE(": found it by FileName\n");
if (This->pCache[i].pObject) { if (cacheEntry->pObject)
return IDirectMusicObject_QueryInterface (This->pCache[i].pObject, riid, ppv); return IDirectMusicObject_QueryInterface ((LPDIRECTMUSICOBJECT)cacheEntry->pObject, riid, ppv);
} }
} }
} }
} }
/* object doesn't exist in cache... guess we'll have to load it */ /* object doesn't exist in cache... guess we'll have to load it */
TRACE(": object does not exist in cache\n"); TRACE(": object does not exist in cache\n");
result = CoCreateInstance (&pDesc->guidClass, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusicObject, (LPVOID*)&pObject); result = CoCreateInstance (&pDesc->guidClass, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusicObject, (LPVOID*)&pObject);
...@@ -106,16 +111,16 @@ HRESULT WINAPI IDirectMusicLoader8Impl_GetObject (LPDIRECTMUSICLOADER8 iface, LP ...@@ -106,16 +111,16 @@ HRESULT WINAPI IDirectMusicLoader8Impl_GetObject (LPDIRECTMUSICLOADER8 iface, LP
/* load object from file */ /* load object from file */
WCHAR wzFileName[MAX_PATH]; WCHAR wzFileName[MAX_PATH];
ILoaderStream* pStream; ILoaderStream* pStream;
IPersistStream *pPersistStream = NULL; IPersistStream *pPersistStream = NULL;
/* if it's full path, don't add search directory path, otherwise do */ /* if it's full path, don't add search directory path, otherwise do */
if (pDesc->dwValidData & DMUS_OBJ_FULLPATH) { if (pDesc->dwValidData & DMUS_OBJ_FULLPATH) {
lstrcpyW( wzFileName, pDesc->wszFileName ); lstrcpyW( wzFileName, pDesc->wszFileName );
} else { } else {
WCHAR *p; WCHAR *p;
lstrcpyW( wzFileName, This->wzSearchPath ); lstrcpyW( wzFileName, This->wzSearchPath );
p = wzFileName + lstrlenW(wzFileName); p = wzFileName + lstrlenW(wzFileName);
if (p > wzFileName && p[-1] != '\\') *p++ = '\\'; if (p > wzFileName && p[-1] != '\\') *p++ = '\\';
lstrcpyW( p, pDesc->wszFileName ); strcpyW( p, pDesc->wszFileName );
} }
TRACE(": loading from file (%s)\n", debugstr_w(wzFileName)); TRACE(": loading from file (%s)\n", debugstr_w(wzFileName));
...@@ -126,13 +131,13 @@ HRESULT WINAPI IDirectMusicLoader8Impl_GetObject (LPDIRECTMUSICLOADER8 iface, LP ...@@ -126,13 +131,13 @@ HRESULT WINAPI IDirectMusicLoader8Impl_GetObject (LPDIRECTMUSICLOADER8 iface, LP
if (FAILED(result)) return result; if (FAILED(result)) return result;
result = IDirectMusicObject_QueryInterface (pObject, &IID_IPersistStream, (LPVOID*)&pPersistStream); result = IDirectMusicObject_QueryInterface (pObject, &IID_IPersistStream, (LPVOID*)&pPersistStream);
if (FAILED(result)) return result; if (FAILED(result)) return result;
result = IPersistStream_Load (pPersistStream, (LPSTREAM)pStream); result = IPersistStream_Load (pPersistStream, (LPSTREAM)pStream);
if (FAILED(result)) return result; if (FAILED(result)) return result;
ILoaderStream_IStream_Release ((LPSTREAM)pStream); ILoaderStream_IStream_Release ((LPSTREAM)pStream);
IPersistStream_Release (pPersistStream); IPersistStream_Release (pPersistStream);
} else if (pDesc->dwValidData & DMUS_OBJ_STREAM) { } else if (pDesc->dwValidData & DMUS_OBJ_STREAM) {
/* load object from stream */ /* load object from stream */
IStream* pClonedStream = NULL; IStream* pClonedStream = NULL;
...@@ -140,22 +145,13 @@ HRESULT WINAPI IDirectMusicLoader8Impl_GetObject (LPDIRECTMUSICLOADER8 iface, LP ...@@ -140,22 +145,13 @@ HRESULT WINAPI IDirectMusicLoader8Impl_GetObject (LPDIRECTMUSICLOADER8 iface, LP
TRACE(": loading from stream\n"); TRACE(": loading from stream\n");
result = IDirectMusicObject_QueryInterface (pObject, &IID_IPersistStream, (LPVOID*)&pPersistStream); result = IDirectMusicObject_QueryInterface (pObject, &IID_IPersistStream, (LPVOID*)&pPersistStream);
if (FAILED(result)) { if (FAILED(result)) return result;
TRACE("couln\'t get IPersistStream\n");
return result;
}
result = IStream_Clone (pDesc->pStream, &pClonedStream); result = IStream_Clone (pDesc->pStream, &pClonedStream);
if (FAILED(result)) { if (FAILED(result)) return result;
TRACE("failed to clone\n");
return result;
}
result = IPersistStream_Load (pPersistStream, pClonedStream); result = IPersistStream_Load (pPersistStream, pClonedStream);
if (FAILED(result)) { if (FAILED(result)) return result;
TRACE("failed to load\n");
return result;
}
IPersistStream_Release (pPersistStream); IPersistStream_Release (pPersistStream);
IStream_Release (pClonedStream); IStream_Release (pClonedStream);
...@@ -163,11 +159,26 @@ HRESULT WINAPI IDirectMusicLoader8Impl_GetObject (LPDIRECTMUSICLOADER8 iface, LP ...@@ -163,11 +159,26 @@ HRESULT WINAPI IDirectMusicLoader8Impl_GetObject (LPDIRECTMUSICLOADER8 iface, LP
/* load object by GUID */ /* load object by GUID */
TRACE(": loading by GUID (only default DLS supported)\n"); TRACE(": loading by GUID (only default DLS supported)\n");
if (IsEqualGUID (&pDesc->guidObject, &GUID_DefaultGMCollection)) { if (IsEqualGUID (&pDesc->guidObject, &GUID_DefaultGMCollection)) {
/* great idea: let's just change dwValid and wszFileName fields and then call ourselves again :D */ WCHAR wzFileName[MAX_PATH];
pDesc->dwValidData = DMUS_OBJ_FILENAME | DMUS_OBJ_FULLPATH; IPersistStream *pPersistStream = NULL;
if (FAILED(DMUSIC_GetDefaultGMPath (pDesc->wszFileName))) ILoaderStream* pStream;
if (FAILED(DMUSIC_GetDefaultGMPath (wzFileName)))
return E_FAIL; return E_FAIL;
return IDirectMusicLoader8Impl_GetObject (iface, pDesc, riid, ppv); /* load object from file */
result = DMUSIC_CreateLoaderStream ((LPSTREAM*)&pStream);
if (FAILED(result)) return result;
result = ILoaderStream_Attach (pStream, wzFileName, (LPDIRECTMUSICLOADER)iface);
if (FAILED(result)) return result;
result = IDirectMusicObject_QueryInterface (pObject, &IID_IPersistStream, (LPVOID*)&pPersistStream);
if (FAILED(result)) return result;
result = IPersistStream_Load (pPersistStream, (LPSTREAM)pStream);
if (FAILED(result)) return result;
ILoaderStream_IStream_Release ((LPSTREAM)pStream);
IPersistStream_Release (pPersistStream);
} else { } else {
return E_FAIL; return E_FAIL;
} }
...@@ -176,32 +187,27 @@ HRESULT WINAPI IDirectMusicLoader8Impl_GetObject (LPDIRECTMUSICLOADER8 iface, LP ...@@ -176,32 +187,27 @@ HRESULT WINAPI IDirectMusicLoader8Impl_GetObject (LPDIRECTMUSICLOADER8 iface, LP
FIXME(": unknown/unsupported way of loading\n"); FIXME(": unknown/unsupported way of loading\n");
return E_FAIL; return E_FAIL;
} }
/* add object to cache */
memset((LPVOID)&desc, 0, sizeof(desc)); newEntry = (LPDMUS_PRIVATE_CACHE_ENTRY) HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(DMUS_PRIVATE_CACHE_ENTRY));
desc.dwSize = sizeof (DMUS_OBJECTDESC); if (pDesc->dwValidData & DMUS_OBJ_OBJECT)
IDirectMusicObject_GetDescriptor (pObject, &desc); memcpy (&newEntry->guidObject, &pDesc->guidObject, sizeof (pDesc->guidObject));
if (pDesc->dwValidData & DMUS_OBJ_FILENAME)
strncpyW (newEntry->wzFileName, pDesc->wszFileName, MAX_PATH);
if (pObject)
newEntry->pObject = pObject;
list_add_tail (&This->CacheList, &newEntry->entry);
TRACE(": filled in cache entry\n");
/* tests with native dlls show that descriptor, which is received by GetDescriptor doesn't contain filepath /* for debug purposes (e.g. to check if all files are cached) */
therefore we must copy it from input description */ #if 0
if (pDesc->dwValidData & DMUS_OBJ_FILENAME || desc.dwValidData & DMUS_OBJ_OBJECT) { int i = 0;
DMUS_PRIVATE_CACHE_ENTRY CacheEntry; LIST_FOR_EACH (listEntry, &This->CacheList) {
This->dwCacheSize++; /* increase size of cache for one entry */ i++;
This->pCache = HeapReAlloc (GetProcessHeap (), 0, This->pCache, sizeof(DMUS_PRIVATE_CACHE_ENTRY) * This->dwCacheSize); cacheEntry = LIST_ENTRY( listEntry, DMUS_PRIVATE_CACHE_ENTRY, entry );
if (desc.dwValidData & DMUS_OBJ_OBJECT) TRACE("Entry nr. %i: GUID = %s, FileName = %s\n", i, debugstr_guid (&cacheEntry->guidObject), debugstr_w (cacheEntry->wzFileName));
CacheEntry.guidObject = desc.guidObject;
if (pDesc->dwValidData & DMUS_OBJ_FILENAME)
strncpyW (CacheEntry.pwzFileName, pDesc->wszFileName, MAX_PATH);
CacheEntry.pObject = pObject;
IDirectMusicObject_AddRef (pObject); /* MSDN says that we should */
This->pCache[This->dwCacheSize - 1] = CacheEntry; /* fill in one backward, as list is zero based */
TRACE(": filled in cache entry\n");
} }
#endif
TRACE(": nr. of entries = %ld\n", This->dwCacheSize);
for (i = 0; i < This->dwCacheSize; i++) {
TRACE(": cache entry [%i]: GUID = %s, file name = %s, object = %p\n", i, debugstr_guid(&This->pCache[i].guidObject), debugstr_w(This->pCache[i].pwzFileName), This->pCache[i].pObject);
}
return IDirectMusicObject_QueryInterface (pObject, riid, ppv); return IDirectMusicObject_QueryInterface (pObject, riid, ppv);
} }
...@@ -352,6 +358,7 @@ HRESULT WINAPI DMUSIC_CreateDirectMusicLoader (LPCGUID lpcGUID, LPDIRECTMUSICLOA ...@@ -352,6 +358,7 @@ HRESULT WINAPI DMUSIC_CreateDirectMusicLoader (LPCGUID lpcGUID, LPDIRECTMUSICLOA
} }
dmloader->lpVtbl = &DirectMusicLoader8_Vtbl; dmloader->lpVtbl = &DirectMusicLoader8_Vtbl;
dmloader->ref = 1; dmloader->ref = 1;
list_init (&dmloader->CacheList);
*ppDMLoad = (LPDIRECTMUSICLOADER8)dmloader; *ppDMLoad = (LPDIRECTMUSICLOADER8)dmloader;
return S_OK; return S_OK;
} }
......
...@@ -46,7 +46,6 @@ HRESULT WINAPI ILoaderStream_Attach (ILoaderStream* This, LPCWSTR wzFile, IDirec ...@@ -46,7 +46,6 @@ HRESULT WINAPI ILoaderStream_Attach (ILoaderStream* This, LPCWSTR wzFile, IDirec
} }
/* create IDirectMusicGetLoader */ /* create IDirectMusicGetLoader */
(LPDIRECTMUSICLOADER)This->pLoader = pLoader; (LPDIRECTMUSICLOADER)This->pLoader = pLoader;
IDirectMusicLoader8_AddRef ((LPDIRECTMUSICLOADER8)This->pLoader);
strncpyW (This->wzFileName, wzFile, MAX_PATH); strncpyW (This->wzFileName, wzFile, MAX_PATH);
TRACE(": succeeded\n"); TRACE(": succeeded\n");
...@@ -165,41 +164,49 @@ HRESULT WINAPI ILoaderStream_IStream_Clone (LPSTREAM iface, IStream** ppstm) ...@@ -165,41 +164,49 @@ HRESULT WINAPI ILoaderStream_IStream_Clone (LPSTREAM iface, IStream** ppstm)
/* not needed*/ /* not needed*/
HRESULT WINAPI ILoaderStream_IStream_Write (LPSTREAM iface, const void* pv, ULONG cb, ULONG* pcbWritten) HRESULT WINAPI ILoaderStream_IStream_Write (LPSTREAM iface, const void* pv, ULONG cb, ULONG* pcbWritten)
{ {
ERR(": should not be needed\n");
return E_NOTIMPL; return E_NOTIMPL;
} }
HRESULT WINAPI ILoaderStream_IStream_SetSize (LPSTREAM iface, ULARGE_INTEGER libNewSize) HRESULT WINAPI ILoaderStream_IStream_SetSize (LPSTREAM iface, ULARGE_INTEGER libNewSize)
{ {
ERR(": should not be needed\n");
return E_NOTIMPL; return E_NOTIMPL;
} }
HRESULT WINAPI ILoaderStream_IStream_CopyTo (LPSTREAM iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten) HRESULT WINAPI ILoaderStream_IStream_CopyTo (LPSTREAM iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
{ {
ERR(": should not be needed\n");
return E_NOTIMPL; return E_NOTIMPL;
} }
HRESULT WINAPI ILoaderStream_IStream_Commit (LPSTREAM iface, DWORD grfCommitFlags) HRESULT WINAPI ILoaderStream_IStream_Commit (LPSTREAM iface, DWORD grfCommitFlags)
{ {
ERR(": should not be needed\n");
return E_NOTIMPL; return E_NOTIMPL;
} }
HRESULT WINAPI ILoaderStream_IStream_Revert (LPSTREAM iface) HRESULT WINAPI ILoaderStream_IStream_Revert (LPSTREAM iface)
{ {
ERR(": should not be needed\n");
return E_NOTIMPL; return E_NOTIMPL;
} }
HRESULT WINAPI ILoaderStream_IStream_LockRegion (LPSTREAM iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType) HRESULT WINAPI ILoaderStream_IStream_LockRegion (LPSTREAM iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
{ {
ERR(": should not be needed\n");
return E_NOTIMPL; return E_NOTIMPL;
} }
HRESULT WINAPI ILoaderStream_IStream_UnlockRegion (LPSTREAM iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType) HRESULT WINAPI ILoaderStream_IStream_UnlockRegion (LPSTREAM iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
{ {
ERR(": should not be needed\n");
return E_NOTIMPL; return E_NOTIMPL;
} }
HRESULT WINAPI ILoaderStream_IStream_Stat (LPSTREAM iface, STATSTG* pstatstg, DWORD grfStatFlag) HRESULT WINAPI ILoaderStream_IStream_Stat (LPSTREAM iface, STATSTG* pstatstg, DWORD grfStatFlag)
{ {
ERR(": should not be needed\n");
return E_NOTIMPL; return E_NOTIMPL;
} }
...@@ -249,7 +256,8 @@ HRESULT WINAPI ILoaderStream_IDirectMusicGetLoader_GetLoader (LPDIRECTMUSICGETLO ...@@ -249,7 +256,8 @@ HRESULT WINAPI ILoaderStream_IDirectMusicGetLoader_GetLoader (LPDIRECTMUSICGETLO
TRACE("(%p, %p)\n", This, ppLoader); TRACE("(%p, %p)\n", This, ppLoader);
*ppLoader = (LPDIRECTMUSICLOADER)This->pLoader; *ppLoader = (LPDIRECTMUSICLOADER)This->pLoader;
IDirectMusicLoader8_AddRef ((LPDIRECTMUSICLOADER8)*ppLoader);
return S_OK; return S_OK;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment