msi/tests: Use a different product code in the package tests to avoid cascading test...
[wine] / dlls / dmscript / script.c
1 /* IDirectMusicScript
2  *
3  * Copyright (C) 2003-2004 Rok Mandeljc
4  * Copyright (C) 2003-2004 Raphael Junqueira
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #ifdef HAVE_UNISTD_H
25 #include <unistd.h>
26 #endif
27
28 #include "dmscript_private.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(dmscript);
31 WINE_DECLARE_DEBUG_CHANNEL(dmfile);
32
33
34 static ULONG WINAPI IDirectMusicScriptImpl_IUnknown_AddRef (LPUNKNOWN iface);
35 static ULONG WINAPI IDirectMusicScriptImpl_IDirectMusicScript_AddRef (LPDIRECTMUSICSCRIPT iface);
36 static ULONG WINAPI IDirectMusicScriptImpl_IDirectMusicObject_AddRef (LPDIRECTMUSICOBJECT iface);
37 static ULONG WINAPI IDirectMusicScriptImpl_IPersistStream_AddRef (LPPERSISTSTREAM iface);
38
39 /*****************************************************************************
40  * IDirectMusicScriptImpl implementation
41  */
42 /* IDirectMusicScriptImpl IUnknown part: */
43 static HRESULT WINAPI IDirectMusicScriptImpl_IUnknown_QueryInterface (LPUNKNOWN iface, REFIID riid, LPVOID *ppobj) {
44   ICOM_THIS_MULTI(IDirectMusicScriptImpl, UnknownVtbl, iface);
45   TRACE("(%p, %s, %p)\n", This, debugstr_dmguid(riid), ppobj);
46   
47   if (IsEqualIID (riid, &IID_IUnknown)) {
48     *ppobj = &This->UnknownVtbl;
49     IDirectMusicScriptImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
50     return S_OK;        
51   } else if (IsEqualIID (riid, &IID_IDirectMusicScript)) {
52     *ppobj = &This->ScriptVtbl;
53     IDirectMusicScriptImpl_IDirectMusicScript_AddRef ((LPDIRECTMUSICSCRIPT)&This->ScriptVtbl);
54     return S_OK;
55   } else if (IsEqualIID (riid, &IID_IDirectMusicObject)) {
56     *ppobj = &This->ObjectVtbl;
57     IDirectMusicScriptImpl_IDirectMusicObject_AddRef ((LPDIRECTMUSICOBJECT)&This->ObjectVtbl);          
58     return S_OK;
59   } else if (IsEqualIID (riid, &IID_IPersistStream)) {
60     *ppobj = &This->PersistStreamVtbl;
61     IDirectMusicScriptImpl_IPersistStream_AddRef ((LPPERSISTSTREAM)&This->PersistStreamVtbl);           
62     return S_OK;
63   }
64   
65   WARN("(%p, %s, %p): not found\n", This, debugstr_dmguid(riid), ppobj);
66   return E_NOINTERFACE;
67 }
68
69 static ULONG WINAPI IDirectMusicScriptImpl_IUnknown_AddRef (LPUNKNOWN iface) {
70   ICOM_THIS_MULTI(IDirectMusicScriptImpl, UnknownVtbl, iface);
71   ULONG ref = InterlockedIncrement(&This->ref);
72
73   TRACE("(%p): AddRef from %d\n", This, ref - 1);
74
75   DMSCRIPT_LockModule();
76
77   return ref;
78 }
79
80 static ULONG WINAPI IDirectMusicScriptImpl_IUnknown_Release (LPUNKNOWN iface) {
81   ICOM_THIS_MULTI(IDirectMusicScriptImpl, UnknownVtbl, iface);
82   ULONG ref = InterlockedDecrement(&This->ref);
83
84   TRACE("(%p): ReleaseRef to %d\n", This, ref);
85
86   if (ref == 0) {
87     HeapFree(GetProcessHeap(), 0, This->pHeader);
88     HeapFree(GetProcessHeap(), 0, This->pVersion);
89     HeapFree(GetProcessHeap(), 0, This->pwzLanguage);
90     HeapFree(GetProcessHeap(), 0, This->pwzSource);
91     HeapFree(GetProcessHeap(), 0, This);
92   }
93
94   DMSCRIPT_UnlockModule();
95   
96   return ref;
97 }
98
99 static const IUnknownVtbl DirectMusicScript_Unknown_Vtbl = {
100   IDirectMusicScriptImpl_IUnknown_QueryInterface,
101   IDirectMusicScriptImpl_IUnknown_AddRef,
102   IDirectMusicScriptImpl_IUnknown_Release
103 };
104
105 /* IDirectMusicScriptImpl IDirectMusicScript part: */
106 static HRESULT WINAPI IDirectMusicScriptImpl_IDirectMusicScript_QueryInterface (LPDIRECTMUSICSCRIPT iface, REFIID riid, LPVOID *ppobj) {
107   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ScriptVtbl, iface);
108   return IDirectMusicScriptImpl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
109 }
110
111 static ULONG WINAPI IDirectMusicScriptImpl_IDirectMusicScript_AddRef (LPDIRECTMUSICSCRIPT iface) {
112   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ScriptVtbl, iface);
113   return IDirectMusicScriptImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
114 }
115
116 static ULONG WINAPI IDirectMusicScriptImpl_IDirectMusicScript_Release (LPDIRECTMUSICSCRIPT iface) {
117   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ScriptVtbl, iface);
118   return IDirectMusicScriptImpl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
119 }
120
121 static HRESULT WINAPI IDirectMusicScriptImpl_IDirectMusicScript_Init (LPDIRECTMUSICSCRIPT iface, IDirectMusicPerformance* pPerformance, DMUS_SCRIPT_ERRORINFO* pErrorInfo) {
122   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ScriptVtbl, iface);
123   FIXME("(%p, %p, %p): stub\n", This, pPerformance, pErrorInfo);
124   This->pPerformance = pPerformance;
125   return S_OK;
126 }
127
128 static HRESULT WINAPI IDirectMusicScriptImpl_IDirectMusicScript_CallRoutine (LPDIRECTMUSICSCRIPT iface, WCHAR* pwszRoutineName, DMUS_SCRIPT_ERRORINFO* pErrorInfo) {
129   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ScriptVtbl, iface);
130   FIXME("(%p, %s, %p): stub\n", This, debugstr_w(pwszRoutineName), pErrorInfo);
131   /*return E_NOTIMPL;*/
132   return S_OK;
133   /*return E_FAIL;*/
134 }
135
136 static HRESULT WINAPI IDirectMusicScriptImpl_IDirectMusicScript_SetVariableVariant (LPDIRECTMUSICSCRIPT iface, WCHAR* pwszVariableName, VARIANT varValue, BOOL fSetRef, DMUS_SCRIPT_ERRORINFO* pErrorInfo) {
137   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ScriptVtbl, iface);
138   FIXME("(%p, %s, FIXME, %d, %p): stub\n", This, debugstr_w(pwszVariableName),/* varValue,*/ fSetRef, pErrorInfo);
139   return S_OK;
140 }
141
142 static HRESULT WINAPI IDirectMusicScriptImpl_IDirectMusicScript_GetVariableVariant (LPDIRECTMUSICSCRIPT iface, WCHAR* pwszVariableName, VARIANT* pvarValue, DMUS_SCRIPT_ERRORINFO* pErrorInfo) {
143   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ScriptVtbl, iface);
144   FIXME("(%p, %s, %p, %p): stub\n", This, debugstr_w(pwszVariableName), pvarValue, pErrorInfo);
145   return S_OK;
146 }
147
148 static HRESULT WINAPI IDirectMusicScriptImpl_IDirectMusicScript_SetVariableNumber (LPDIRECTMUSICSCRIPT iface, WCHAR* pwszVariableName, LONG lValue, DMUS_SCRIPT_ERRORINFO* pErrorInfo) {
149   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ScriptVtbl, iface);
150   FIXME("(%p, %s, %i, %p): stub\n", This, debugstr_w(pwszVariableName), lValue, pErrorInfo);
151   return S_OK;
152 }
153
154 static HRESULT WINAPI IDirectMusicScriptImpl_IDirectMusicScript_GetVariableNumber (LPDIRECTMUSICSCRIPT iface, WCHAR* pwszVariableName, LONG* plValue, DMUS_SCRIPT_ERRORINFO* pErrorInfo) {
155   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ScriptVtbl, iface);
156   FIXME("(%p, %s, %p, %p): stub\n", This, debugstr_w(pwszVariableName), plValue, pErrorInfo);
157   return S_OK;
158 }
159
160 static HRESULT WINAPI IDirectMusicScriptImpl_IDirectMusicScript_SetVariableObject (LPDIRECTMUSICSCRIPT iface, WCHAR* pwszVariableName, IUnknown* punkValue, DMUS_SCRIPT_ERRORINFO* pErrorInfo) {
161   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ScriptVtbl, iface);
162   FIXME("(%p, %s, %p, %p): stub\n", This, debugstr_w(pwszVariableName), punkValue, pErrorInfo);
163   return S_OK;
164 }
165
166 static HRESULT WINAPI IDirectMusicScriptImpl_IDirectMusicScript_GetVariableObject (LPDIRECTMUSICSCRIPT iface, WCHAR* pwszVariableName, REFIID riid, LPVOID* ppv, DMUS_SCRIPT_ERRORINFO* pErrorInfo) {
167   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ScriptVtbl, iface);
168   FIXME("(%p, %s, %s, %p, %p): stub\n", This, debugstr_w(pwszVariableName), debugstr_dmguid(riid), ppv, pErrorInfo);
169   return S_OK;
170 }
171
172 static HRESULT WINAPI IDirectMusicScriptImpl_IDirectMusicScript_EnumRoutine (LPDIRECTMUSICSCRIPT iface, DWORD dwIndex, WCHAR* pwszName) {
173   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ScriptVtbl, iface);
174   FIXME("(%p, %d, %p): stub\n", This, dwIndex, pwszName);
175   return S_OK;
176 }
177
178 static HRESULT WINAPI IDirectMusicScriptImpl_IDirectMusicScript_EnumVariable (LPDIRECTMUSICSCRIPT iface, DWORD dwIndex, WCHAR* pwszName) {
179   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ScriptVtbl, iface);
180   FIXME("(%p, %d, %p): stub\n", This, dwIndex, pwszName);
181   return S_OK;
182 }
183
184 static const IDirectMusicScriptVtbl DirectMusicScript_Script_Vtbl = {
185   IDirectMusicScriptImpl_IDirectMusicScript_QueryInterface,
186   IDirectMusicScriptImpl_IDirectMusicScript_AddRef,
187   IDirectMusicScriptImpl_IDirectMusicScript_Release,
188   IDirectMusicScriptImpl_IDirectMusicScript_Init,
189   IDirectMusicScriptImpl_IDirectMusicScript_CallRoutine,
190   IDirectMusicScriptImpl_IDirectMusicScript_SetVariableVariant,
191   IDirectMusicScriptImpl_IDirectMusicScript_GetVariableVariant,
192   IDirectMusicScriptImpl_IDirectMusicScript_SetVariableNumber,
193   IDirectMusicScriptImpl_IDirectMusicScript_GetVariableNumber,
194   IDirectMusicScriptImpl_IDirectMusicScript_SetVariableObject,
195   IDirectMusicScriptImpl_IDirectMusicScript_GetVariableObject,
196   IDirectMusicScriptImpl_IDirectMusicScript_EnumRoutine,
197   IDirectMusicScriptImpl_IDirectMusicScript_EnumVariable
198 };
199
200 /* IDirectMusicScriptImpl IDirectMusicObject part: */
201 static HRESULT WINAPI IDirectMusicScriptImpl_IDirectMusicObject_QueryInterface (LPDIRECTMUSICOBJECT iface, REFIID riid, LPVOID *ppobj) {
202   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ObjectVtbl, iface);
203   return IDirectMusicScriptImpl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
204 }
205
206 static ULONG WINAPI IDirectMusicScriptImpl_IDirectMusicObject_AddRef (LPDIRECTMUSICOBJECT iface) {
207   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ObjectVtbl, iface);
208   return IDirectMusicScriptImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
209 }
210
211 static ULONG WINAPI IDirectMusicScriptImpl_IDirectMusicObject_Release (LPDIRECTMUSICOBJECT iface) {
212   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ObjectVtbl, iface);
213   return IDirectMusicScriptImpl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
214 }
215
216 static HRESULT WINAPI IDirectMusicScriptImpl_IDirectMusicObject_GetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
217   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ObjectVtbl, iface);
218   TRACE("(%p, %p)\n", This, pDesc);
219   /* I think we shouldn't return pointer here since then values can be changed; it'd be a mess */
220   memcpy (pDesc, This->pDesc, This->pDesc->dwSize);
221   return S_OK;
222 }
223
224 static HRESULT WINAPI IDirectMusicScriptImpl_IDirectMusicObject_SetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
225   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ObjectVtbl, iface);
226   TRACE("(%p, %p): setting descriptor:\n%s\n", This, pDesc, debugstr_DMUS_OBJECTDESC (pDesc));
227
228   /* According to MSDN, we should copy only given values, not whole struct */   
229   if (pDesc->dwValidData & DMUS_OBJ_OBJECT)
230     This->pDesc->guidObject = pDesc->guidObject;
231   if (pDesc->dwValidData & DMUS_OBJ_CLASS)
232     This->pDesc->guidClass = pDesc->guidClass;
233   if (pDesc->dwValidData & DMUS_OBJ_NAME)
234     lstrcpynW (This->pDesc->wszName, pDesc->wszName, DMUS_MAX_NAME);
235   if (pDesc->dwValidData & DMUS_OBJ_CATEGORY)
236     lstrcpynW (This->pDesc->wszCategory, pDesc->wszCategory, DMUS_MAX_CATEGORY);
237   if (pDesc->dwValidData & DMUS_OBJ_FILENAME)
238     lstrcpynW (This->pDesc->wszFileName, pDesc->wszFileName, DMUS_MAX_FILENAME);
239   if (pDesc->dwValidData & DMUS_OBJ_VERSION)
240     This->pDesc->vVersion = pDesc->vVersion;
241   if (pDesc->dwValidData & DMUS_OBJ_DATE)
242     This->pDesc->ftDate = pDesc->ftDate;
243   if (pDesc->dwValidData & DMUS_OBJ_MEMORY) {
244     memcpy (&This->pDesc->llMemLength, &pDesc->llMemLength, sizeof (pDesc->llMemLength));                               
245     memcpy (This->pDesc->pbMemData, pDesc->pbMemData, sizeof (pDesc->pbMemData));
246   }
247   if (pDesc->dwValidData & DMUS_OBJ_STREAM) {
248     /* according to MSDN, we copy the stream */
249     IStream_Clone (pDesc->pStream, &This->pDesc->pStream);      
250   }
251   
252   /* add new flags */
253   This->pDesc->dwValidData |= pDesc->dwValidData;
254   
255   return S_OK;
256 }
257
258 static HRESULT WINAPI IDirectMusicScriptImpl_IDirectMusicObject_ParseDescriptor (LPDIRECTMUSICOBJECT iface, LPSTREAM pStream, LPDMUS_OBJECTDESC pDesc) {
259   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ObjectVtbl, iface);
260   DMUS_PRIVATE_CHUNK Chunk;
261   DWORD StreamSize, StreamCount, ListSize[1], ListCount[1];
262   LARGE_INTEGER liMove; /* used when skipping chunks */
263
264         TRACE("(%p, %p, %p)\n", This, pStream, pDesc);
265
266         /* FIXME: should this be determined from stream? */
267         pDesc->dwValidData |= DMUS_OBJ_CLASS;
268         pDesc->guidClass = CLSID_DirectMusicScript;
269
270         IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
271         TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
272         switch (Chunk.fccID) {  
273                 case FOURCC_RIFF: {
274                         IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);                             
275                         TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
276                         StreamSize = Chunk.dwSize - sizeof(FOURCC);
277                         StreamCount = 0;
278                         if (Chunk.fccID == DMUS_FOURCC_SCRIPT_FORM) {
279                                 TRACE_(dmfile)(": script form\n");
280                                 do {
281                                         IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
282                                         StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
283                                         TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
284                                         switch (Chunk.fccID) {
285                                                 case DMUS_FOURCC_GUID_CHUNK: {
286                                                         TRACE_(dmfile)(": GUID chunk\n");
287                                                         pDesc->dwValidData |= DMUS_OBJ_OBJECT;
288                                                         IStream_Read (pStream, &pDesc->guidObject, Chunk.dwSize, NULL);
289                                                         break;
290                                                 }
291                                                 case DMUS_FOURCC_VERSION_CHUNK: {
292                                                         TRACE_(dmfile)(": version chunk\n");
293                                                         pDesc->dwValidData |= DMUS_OBJ_VERSION;
294                                                         IStream_Read (pStream, &pDesc->vVersion, Chunk.dwSize, NULL);
295                                                         break;
296                                                 }
297                                                 case DMUS_FOURCC_CATEGORY_CHUNK: {
298                                                         TRACE_(dmfile)(": category chunk\n");
299                                                         pDesc->dwValidData |= DMUS_OBJ_CATEGORY;
300                                                         IStream_Read (pStream, pDesc->wszCategory, Chunk.dwSize, NULL);
301                                                         break;
302                                                 }
303                                                 case FOURCC_LIST: {
304                                                         IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);                             
305                                                         TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
306                                                         ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
307                                                         ListCount[0] = 0;
308                                                         switch (Chunk.fccID) {
309                                                                 /* evil M$ UNFO list, which can (!?) contain INFO elements */
310                                                                 case DMUS_FOURCC_UNFO_LIST: {
311                                                                         TRACE_(dmfile)(": UNFO list\n");
312                                                                         do {
313                                                                                 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
314                                                                                 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
315                                                                                 TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
316                                                                                 switch (Chunk.fccID) {
317                                                                                         /* don't ask me why, but M$ puts INFO elements in UNFO list sometimes
318                                              (though strings seem to be valid unicode) */
319                                                                                         case mmioFOURCC('I','N','A','M'):
320                                                                                         case DMUS_FOURCC_UNAM_CHUNK: {
321                                                                                                 TRACE_(dmfile)(": name chunk\n");
322                                                                                                 pDesc->dwValidData |= DMUS_OBJ_NAME;
323                                                                                                 IStream_Read (pStream, pDesc->wszName, Chunk.dwSize, NULL);
324                                                                                                 break;
325                                                                                         }
326                                                                                         case mmioFOURCC('I','A','R','T'):
327                                                                                         case DMUS_FOURCC_UART_CHUNK: {
328                                                                                                 TRACE_(dmfile)(": artist chunk (ignored)\n");
329                                                                                                 liMove.QuadPart = Chunk.dwSize;
330                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
331                                                                                                 break;
332                                                                                         }
333                                                                                         case mmioFOURCC('I','C','O','P'):
334                                                                                         case DMUS_FOURCC_UCOP_CHUNK: {
335                                                                                                 TRACE_(dmfile)(": copyright chunk (ignored)\n");
336                                                                                                 liMove.QuadPart = Chunk.dwSize;
337                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
338                                                                                                 break;
339                                                                                         }
340                                                                                         case mmioFOURCC('I','S','B','J'):
341                                                                                         case DMUS_FOURCC_USBJ_CHUNK: {
342                                                                                                 TRACE_(dmfile)(": subject chunk (ignored)\n");
343                                                                                                 liMove.QuadPart = Chunk.dwSize;
344                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
345                                                                                                 break;
346                                                                                         }
347                                                                                         case mmioFOURCC('I','C','M','T'):
348                                                                                         case DMUS_FOURCC_UCMT_CHUNK: {
349                                                                                                 TRACE_(dmfile)(": comment chunk (ignored)\n");
350                                                                                                 liMove.QuadPart = Chunk.dwSize;
351                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
352                                                                                                 break;
353                                                                                         }
354                                                                                         default: {
355                                                                                                 TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
356                                                                                                 liMove.QuadPart = Chunk.dwSize;
357                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
358                                                                                                 break;                                          
359                                                                                         }
360                                                                                 }
361                                                                                 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
362                                                                         } while (ListCount[0] < ListSize[0]);
363                                                                         break;
364                                                                 }
365                                                                 default: {
366                                                                         TRACE_(dmfile)(": unknown (skipping)\n");
367                                                                         liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
368                                                                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
369                                                                         break;                                          
370                                                                 }
371                                                         }
372                                                         break;
373                                                 }       
374                                                 default: {
375                                                         TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
376                                                         liMove.QuadPart = Chunk.dwSize;
377                                                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
378                                                         break;                                          
379                                                 }
380                                         }
381                                         TRACE_(dmfile)(": StreamCount[0] = %d < StreamSize[0] = %d\n", StreamCount, StreamSize);
382                                 } while (StreamCount < StreamSize);
383                         } else {
384                                 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
385                                 liMove.QuadPart = StreamSize;
386                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
387                                 return E_FAIL;
388                         }
389                 
390                         TRACE_(dmfile)(": reading finished\n");
391                         break;
392                 }
393                 default: {
394                         TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
395                         liMove.QuadPart = Chunk.dwSize;
396                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
397                         return DMUS_E_INVALIDFILE;
398                 }
399         }       
400         
401         TRACE(": returning descriptor:\n%s\n", debugstr_DMUS_OBJECTDESC (pDesc));
402         
403         return S_OK;
404 }
405
406 static const IDirectMusicObjectVtbl DirectMusicScript_Object_Vtbl = {
407   IDirectMusicScriptImpl_IDirectMusicObject_QueryInterface,
408   IDirectMusicScriptImpl_IDirectMusicObject_AddRef,
409   IDirectMusicScriptImpl_IDirectMusicObject_Release,
410   IDirectMusicScriptImpl_IDirectMusicObject_GetDescriptor,
411   IDirectMusicScriptImpl_IDirectMusicObject_SetDescriptor,
412   IDirectMusicScriptImpl_IDirectMusicObject_ParseDescriptor
413 };
414
415 /* IDirectMusicScriptImpl IPersistStream part: */
416 static HRESULT WINAPI IDirectMusicScriptImpl_IPersistStream_QueryInterface (LPPERSISTSTREAM iface, REFIID riid, LPVOID *ppobj) {
417   ICOM_THIS_MULTI(IDirectMusicScriptImpl, PersistStreamVtbl, iface);
418   return IDirectMusicScriptImpl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
419 }
420
421 static ULONG WINAPI IDirectMusicScriptImpl_IPersistStream_AddRef (LPPERSISTSTREAM iface) {
422   ICOM_THIS_MULTI(IDirectMusicScriptImpl, PersistStreamVtbl, iface);
423   return IDirectMusicScriptImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
424 }
425
426 static ULONG WINAPI IDirectMusicScriptImpl_IPersistStream_Release (LPPERSISTSTREAM iface) {
427   ICOM_THIS_MULTI(IDirectMusicScriptImpl, PersistStreamVtbl, iface);
428   return IDirectMusicScriptImpl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
429 }
430
431 static HRESULT WINAPI IDirectMusicScriptImpl_IPersistStream_GetClassID (LPPERSISTSTREAM iface, CLSID* pClassID) {
432   ICOM_THIS_MULTI(IDirectMusicScriptImpl, PersistStreamVtbl, iface);
433   TRACE("(%p, %p)\n", This, pClassID);
434   *pClassID = CLSID_DirectMusicScript;
435   return S_OK;
436 }
437
438 static HRESULT WINAPI IDirectMusicScriptImpl_IPersistStream_IsDirty (LPPERSISTSTREAM iface) {
439   ICOM_THIS_MULTI(IDirectMusicScriptImpl, PersistStreamVtbl, iface);  
440   FIXME("(%p): stub, always S_FALSE\n", This);
441   return S_FALSE;
442 }
443
444 static HRESULT WINAPI IDirectMusicScriptImpl_IPersistStream_Load (LPPERSISTSTREAM iface, IStream* pStm) {
445         ICOM_THIS_MULTI(IDirectMusicScriptImpl, PersistStreamVtbl, iface);
446
447         DMUS_PRIVATE_CHUNK Chunk;
448         DWORD StreamSize, StreamCount, ListSize[3], ListCount[3];
449         LARGE_INTEGER liMove; /* used when skipping chunks */
450         LPDIRECTMUSICGETLOADER pGetLoader = NULL;
451         LPDIRECTMUSICLOADER pLoader = NULL;
452
453         FIXME("(%p, %p): Loading not implemented yet\n", This, pStm);
454         IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
455         TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
456         switch (Chunk.fccID) {  
457                 case FOURCC_RIFF: {
458                         IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);                                
459                         TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
460                         StreamSize = Chunk.dwSize - sizeof(FOURCC);
461                         StreamCount = 0;
462                         switch (Chunk.fccID) {
463                                 case DMUS_FOURCC_SCRIPT_FORM: {
464                                         TRACE_(dmfile)(": script form\n");
465                                         do {
466                                                 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
467                                                 StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
468                                                 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
469                                                 switch (Chunk.fccID) { 
470                                                         case DMUS_FOURCC_SCRIPT_CHUNK: {
471                                                                 TRACE_(dmfile)(": script header chunk\n");
472                                                                 This->pHeader = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, Chunk.dwSize);
473                                                                 IStream_Read (pStm, This->pHeader, Chunk.dwSize, NULL);
474                                                                 break;
475                                                         }
476                                                         case DMUS_FOURCC_SCRIPTVERSION_CHUNK: {
477                                                                 TRACE_(dmfile)(": script version chunk\n");
478                                                                 This->pVersion = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, Chunk.dwSize);
479                                                                 IStream_Read (pStm, This->pVersion, Chunk.dwSize, NULL); 
480                                                                 TRACE_(dmfile)("version: 0x%08x.0x%08x\n", This->pVersion->dwVersionMS, This->pVersion->dwVersionLS);
481                                                                 break;
482                                                         }
483                                                         case DMUS_FOURCC_SCRIPTLANGUAGE_CHUNK: {
484                                                                 TRACE_(dmfile)(": script language chunk\n");
485                                                                 This->pwzLanguage = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, Chunk.dwSize);
486                                                                 IStream_Read (pStm, This->pwzLanguage, Chunk.dwSize, NULL); 
487                                                                 TRACE_(dmfile)("using language: %s\n", debugstr_w(This->pwzLanguage));
488                                                                 break;
489                                                         }
490                                                         case DMUS_FOURCC_SCRIPTSOURCE_CHUNK: {
491                                                                 TRACE_(dmfile)(": script source chunk\n");
492                                                                 This->pwzSource = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, Chunk.dwSize);
493                                                                 IStream_Read (pStm, This->pwzSource, Chunk.dwSize, NULL); 
494                                                                 if (TRACE_ON(dmscript)) {
495                                                                     int count = WideCharToMultiByte(CP_ACP, 0, This->pwzSource, -1, NULL, 0, NULL, NULL);
496                                                                     LPSTR str = HeapAlloc(GetProcessHeap (), 0, count);
497                                                                     WideCharToMultiByte(CP_ACP, 0, This->pwzSource, -1, str, count, NULL, NULL);
498                                                                     str[count-1] = '\n';
499                                                                     TRACE("source:\n");
500                                                                     write( 2, str, count );
501                                                                     HeapFree(GetProcessHeap(), 0, str);
502                                                                 }
503                                                                 break;
504                                                         }
505                                                         case DMUS_FOURCC_GUID_CHUNK: {
506                                                                 TRACE_(dmfile)(": GUID chunk\n");
507                                                                 This->pDesc->dwValidData |= DMUS_OBJ_OBJECT;
508                                                                 IStream_Read (pStm, &This->pDesc->guidObject, Chunk.dwSize, NULL);
509                                                                 break;
510                                                         }
511                                                         case DMUS_FOURCC_VERSION_CHUNK: {
512                                                                 TRACE_(dmfile)(": version chunk\n");
513                                                                 This->pDesc->dwValidData |= DMUS_OBJ_VERSION;
514                                                                 IStream_Read (pStm, &This->pDesc->vVersion, Chunk.dwSize, NULL);
515                                                                 break;
516                                                         }
517                                                         case DMUS_FOURCC_CATEGORY_CHUNK: {
518                                                                 TRACE_(dmfile)(": category chunk\n");
519                                                                 This->pDesc->dwValidData |= DMUS_OBJ_CATEGORY;
520                                                                 IStream_Read (pStm, This->pDesc->wszCategory, Chunk.dwSize, NULL);
521                                                                 break;
522                                                         }
523                                                         case FOURCC_RIFF: {
524                                                                 IDirectMusicObject* pObject = NULL;
525                                                                 DMUS_OBJECTDESC desc;
526
527                                                                 ZeroMemory (&desc, sizeof(DMUS_OBJECTDESC));
528                                                                 desc.dwSize = sizeof(DMUS_OBJECTDESC);
529                                                                 desc.dwValidData = DMUS_OBJ_STREAM | DMUS_OBJ_CLASS;
530                                                                 desc.guidClass = CLSID_DirectMusicContainer;
531                                                                 desc.pStream = NULL;
532                                                                 IStream_Clone (pStm, &desc.pStream);
533
534                                                                 liMove.QuadPart = 0;
535                                                                 liMove.QuadPart -= (sizeof(FOURCC) + sizeof(DWORD));
536                                                                 IStream_Seek (desc.pStream, liMove, STREAM_SEEK_CUR, NULL);
537
538                                                                 IStream_QueryInterface (pStm, &IID_IDirectMusicGetLoader, (LPVOID*)&pGetLoader);
539                                                                 IDirectMusicGetLoader_GetLoader (pGetLoader, &pLoader);
540                                                                 IDirectMusicGetLoader_Release (pGetLoader);
541
542                                                                 if (SUCCEEDED(IDirectMusicLoader_GetObject (pLoader, &desc, &IID_IDirectMusicObject, (LPVOID*) &pObject))) {
543                                                                   IDirectMusicObject_Release (pObject);
544                                                                 } else {
545                                                                   ERR_(dmfile)("Error on GetObject while trying to load Scrip SubContainer\n");
546                                                                 }
547
548                                                                 IDirectMusicLoader_Release (pLoader); pLoader = NULL; /* release loader */
549                                                                 IStream_Release(desc.pStream); desc.pStream = NULL; /* release cloned stream */
550
551                                                                 liMove.QuadPart = Chunk.dwSize;
552                                                                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
553                                                                 /*
554                                                                 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);                                
555                                                                 TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
556                                                                 ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
557                                                                 ListCount[0] = 0;
558
559                                                                 switch (Chunk.fccID) {
560                                                                         default: {
561                                                                                 TRACE_(dmfile)(": unknown (skipping)\n");
562                                                                                 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
563                                                                                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
564                                                                                 break;                                          
565                                                                         }
566                                                                 }
567                                                                 */
568                                                                 break;
569                                                         }
570                                                         case FOURCC_LIST: {
571                                                                 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);                                
572                                                                 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
573                                                                 ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
574                                                                 ListCount[0] = 0;
575                                                                 switch (Chunk.fccID) {
576                                                                         case DMUS_FOURCC_UNFO_LIST: {
577                                                                                 TRACE_(dmfile)(": UNFO list\n");
578                                                                                 do {
579                                                                                         IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
580                                                                                         ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
581                                                                                         TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
582                                                                                         switch (Chunk.fccID) {
583                                                                                                 /* don't ask me why, but M$ puts INFO elements in UNFO list sometimes
584                                               (though strings seem to be valid unicode) */
585                                                                                                 case mmioFOURCC('I','N','A','M'):
586                                                                                                 case DMUS_FOURCC_UNAM_CHUNK: {
587                                                                                                         TRACE_(dmfile)(": name chunk\n");
588                                                                                                         This->pDesc->dwValidData |= DMUS_OBJ_NAME;
589                                                                                                         IStream_Read (pStm, This->pDesc->wszName, Chunk.dwSize, NULL);
590                                                                                                         break;
591                                                                                                 }
592                                                                                                 case mmioFOURCC('I','A','R','T'):
593                                                                                                 case DMUS_FOURCC_UART_CHUNK: {
594                                                                                                         TRACE_(dmfile)(": artist chunk (ignored)\n");
595                                                                                                         liMove.QuadPart = Chunk.dwSize;
596                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
597                                                                                                         break;
598                                                                                                 }
599                                                                                                 case mmioFOURCC('I','C','O','P'):
600                                                                                                 case DMUS_FOURCC_UCOP_CHUNK: {
601                                                                                                         TRACE_(dmfile)(": copyright chunk (ignored)\n");
602                                                                                                         liMove.QuadPart = Chunk.dwSize;
603                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
604                                                                                                         break;
605                                                                                                 }
606                                                                                                 case mmioFOURCC('I','S','B','J'):
607                                                                                                 case DMUS_FOURCC_USBJ_CHUNK: {
608                                                                                                         TRACE_(dmfile)(": subject chunk (ignored)\n");
609                                                                                                         liMove.QuadPart = Chunk.dwSize;
610                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
611                                                                                                         break;
612                                                                                                 }
613                                                                                                 case mmioFOURCC('I','C','M','T'):
614                                                                                                 case DMUS_FOURCC_UCMT_CHUNK: {
615                                                                                                         TRACE_(dmfile)(": comment chunk (ignored)\n");
616                                                                                                         liMove.QuadPart = Chunk.dwSize;
617                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
618                                                                                                         break;
619                                                                                                 }
620                                                                                                 default: {
621                                                                                                         TRACE_(dmfile)(": unknown sub-chunk (irrevelant & skipping)\n");
622                                                                                                         liMove.QuadPart = Chunk.dwSize;
623                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
624                                                                                                         break;                                          
625                                                                                                 }
626                                                                                         }
627                                                                                         TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
628                                                                                 } while (ListCount[0] < ListSize[0]);
629                                                                                 break;
630                                                                         }
631                                                                         default: {
632                                                                                 TRACE_(dmfile)(": unknown (skipping)\n");
633                                                                                 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
634                                                                                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
635                                                                                 break;                                          
636                                                                         }
637                                                                 }
638                                                                 break;
639                                                         }       
640                                                         default: {
641                                                                 TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
642                                                                 liMove.QuadPart = Chunk.dwSize;
643                                                                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
644                                                                 break;                                          
645                                                         }
646                                                 }
647                                                 TRACE_(dmfile)(": StreamCount[0] = %d < StreamSize[0] = %d\n", StreamCount, StreamSize);
648                                         } while (StreamCount < StreamSize);
649                                         break;
650                                 }
651                                 default: {
652                                         TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
653                                         liMove.QuadPart = StreamSize;
654                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
655                                         return E_FAIL;
656                                 }
657                         }
658                         TRACE_(dmfile)(": reading finished\n");
659                         break;
660                 }
661                 default: {
662                         TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
663                         liMove.QuadPart = Chunk.dwSize;
664                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
665                         return E_FAIL;
666                 }
667         }
668
669         return S_OK;
670 }
671
672 static HRESULT WINAPI IDirectMusicScriptImpl_IPersistStream_Save (LPPERSISTSTREAM iface, IStream* pStm, BOOL fClearDirty) {
673   return E_NOTIMPL;
674 }
675
676 static HRESULT WINAPI IDirectMusicScriptImpl_IPersistStream_GetSizeMax (LPPERSISTSTREAM iface, ULARGE_INTEGER* pcbSize) {
677   return E_NOTIMPL;
678 }
679
680 static const IPersistStreamVtbl DirectMusicScript_PersistStream_Vtbl = {
681   IDirectMusicScriptImpl_IPersistStream_QueryInterface,
682   IDirectMusicScriptImpl_IPersistStream_AddRef,
683   IDirectMusicScriptImpl_IPersistStream_Release,
684   IDirectMusicScriptImpl_IPersistStream_GetClassID,
685   IDirectMusicScriptImpl_IPersistStream_IsDirty,
686   IDirectMusicScriptImpl_IPersistStream_Load,
687   IDirectMusicScriptImpl_IPersistStream_Save,
688   IDirectMusicScriptImpl_IPersistStream_GetSizeMax
689 };
690
691 /* for ClassFactory */
692 HRESULT WINAPI DMUSIC_CreateDirectMusicScriptImpl (LPCGUID lpcGUID, LPVOID* ppobj, LPUNKNOWN pUnkOuter) {
693   IDirectMusicScriptImpl* obj;
694   
695   obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicScriptImpl));
696   if (NULL == obj) {
697     *ppobj = NULL;
698     return E_OUTOFMEMORY;
699   }
700   obj->UnknownVtbl = &DirectMusicScript_Unknown_Vtbl;
701   obj->ScriptVtbl = &DirectMusicScript_Script_Vtbl;
702   obj->ObjectVtbl = &DirectMusicScript_Object_Vtbl;
703   obj->PersistStreamVtbl = &DirectMusicScript_PersistStream_Vtbl;
704   obj->pDesc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DMUS_OBJECTDESC));
705   DM_STRUCT_INIT(obj->pDesc);
706   obj->pDesc->dwValidData |= DMUS_OBJ_CLASS;
707   obj->pDesc->guidClass = CLSID_DirectMusicScript;
708   obj->ref = 0; /* will be inited by QueryInterface */
709
710   return IDirectMusicScriptImpl_IUnknown_QueryInterface ((LPUNKNOWN)&obj->UnknownVtbl, lpcGUID, ppobj);
711 }