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