dmusic: Avoid swallowing collection Load failures.
[wine.git] / dlls / d3dxof / d3dxof.c
blob9ed02e1bdf9158c484275b7c2f16df05447c110f
1 /*
2 * Implementation of DirectX File Interfaces
4 * Copyright 2004, 2008, 2010 Christian Costa
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "d3dxof_private.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(d3dxof);
24 WINE_DECLARE_DEBUG_CHANNEL(d3dxof_dump);
26 static const struct IDirectXFileVtbl IDirectXFile_Vtbl;
27 static const struct IDirectXFileBinaryVtbl IDirectXFileBinary_Vtbl;
28 static const struct IDirectXFileDataVtbl IDirectXFileData_Vtbl;
29 static const struct IDirectXFileDataReferenceVtbl IDirectXFileDataReference_Vtbl;
30 static const struct IDirectXFileEnumObjectVtbl IDirectXFileEnumObject_Vtbl;
31 static const struct IDirectXFileSaveObjectVtbl IDirectXFileSaveObject_Vtbl;
33 static HRESULT IDirectXFileDataReferenceImpl_Create(IDirectXFileDataReferenceImpl** ppObj);
34 static HRESULT IDirectXFileEnumObjectImpl_Create(IDirectXFileEnumObjectImpl** ppObj);
35 static HRESULT IDirectXFileSaveObjectImpl_Create(IDirectXFileSaveObjectImpl** ppObj);
37 #define TOKEN_DWORD 41
38 #define TOKEN_FLOAT 42
40 HRESULT IDirectXFileImpl_Create(IUnknown* pUnkOuter, LPVOID* ppObj)
42 IDirectXFileImpl* object;
44 TRACE("(%p,%p)\n", pUnkOuter, ppObj);
46 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectXFileImpl));
47 if (!object)
48 return DXFILEERR_BADALLOC;
50 object->IDirectXFile_iface.lpVtbl = &IDirectXFile_Vtbl;
51 object->ref = 1;
53 /* Reserve first template to handle the case sensitive legacy type indexColor */
54 object->nb_xtemplates = 1;
55 strcpy(object->xtemplates[0].name, "indexColor");
56 object->xtemplates[0].nb_members = 2;
57 object->xtemplates[0].members[0].type = TOKEN_DWORD;
58 object->xtemplates[0].members[0].nb_dims = 0;
59 object->xtemplates[0].members[1].type = TOKEN_FLOAT;
60 object->xtemplates[0].members[1].nb_dims = 1;
61 object->xtemplates[0].members[1].dim_fixed[0] = TRUE;
62 object->xtemplates[0].members[1].dim_value[0] = 4;
64 *ppObj = &object->IDirectXFile_iface;
66 return S_OK;
69 static inline IDirectXFileImpl *impl_from_IDirectXFile(IDirectXFile *iface)
71 return CONTAINING_RECORD(iface, IDirectXFileImpl, IDirectXFile_iface);
74 static HRESULT WINAPI IDirectXFileImpl_QueryInterface(IDirectXFile *iface, REFIID riid, void **out)
76 IDirectXFileImpl *object = impl_from_IDirectXFile(iface);
78 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
80 if (IsEqualGUID(riid, &IID_IUnknown)
81 || IsEqualGUID(riid, &IID_IDirectXFile))
83 *out = &object->IDirectXFile_iface;
85 else
87 *out = NULL;
88 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
89 return E_NOINTERFACE;
92 IUnknown_AddRef((IUnknown *)*out);
93 return S_OK;
96 static ULONG WINAPI IDirectXFileImpl_AddRef(IDirectXFile *iface)
98 IDirectXFileImpl *object = impl_from_IDirectXFile(iface);
99 ULONG refcount = InterlockedIncrement(&object->ref);
101 TRACE("%p increasing refcount to %lu.\n", iface, refcount);
103 return refcount;
106 static ULONG WINAPI IDirectXFileImpl_Release(IDirectXFile *iface)
108 IDirectXFileImpl *object = impl_from_IDirectXFile(iface);
109 ULONG refcount = InterlockedDecrement(&object->ref);
111 TRACE("%p decreasing refcount to %lu.\n", iface, refcount);
113 if (!refcount)
114 HeapFree(GetProcessHeap(), 0, object);
116 return refcount;
119 /*** IDirectXFile methods ***/
120 static HRESULT WINAPI IDirectXFileImpl_CreateEnumObject(IDirectXFile* iface, LPVOID pvSource, DXFILELOADOPTIONS dwLoadOptions, LPDIRECTXFILEENUMOBJECT* ppEnumObj)
122 IDirectXFileImpl *This = impl_from_IDirectXFile(iface);
123 IDirectXFileEnumObjectImpl* object;
124 HRESULT hr;
125 LPBYTE file_buffer;
126 DWORD file_size;
127 DWORD bytes_written;
129 TRACE("iface %p, source %p, load_options %#lx, out %p.\n", iface, pvSource, dwLoadOptions, ppEnumObj);
131 if (!ppEnumObj)
132 return DXFILEERR_BADVALUE;
134 /* Only lowest 4 bits are relevant in DXFILELOADOPTIONS */
135 dwLoadOptions &= 0xF;
137 hr = IDirectXFileEnumObjectImpl_Create(&object);
138 if (FAILED(hr))
139 return hr;
141 if (dwLoadOptions == DXFILELOAD_FROMFILE)
143 HANDLE hFile, file_mapping;
145 TRACE("Open source file '%s'\n", (char*)pvSource);
147 hFile = CreateFileA(pvSource, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
148 if (hFile == INVALID_HANDLE_VALUE)
150 TRACE("File '%s' not found\n", (char*)pvSource);
151 return DXFILEERR_FILENOTFOUND;
154 file_size = GetFileSize(hFile, NULL);
156 file_mapping = CreateFileMappingA(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
157 CloseHandle(hFile);
158 if (!file_mapping)
160 hr = DXFILEERR_BADFILETYPE;
161 goto error;
164 object->mapped_memory = MapViewOfFile(file_mapping, FILE_MAP_READ, 0, 0, 0);
165 CloseHandle(file_mapping);
166 if (!object->mapped_memory)
168 hr = DXFILEERR_BADFILETYPE;
169 goto error;
171 file_buffer = object->mapped_memory;
173 else if (dwLoadOptions == DXFILELOAD_FROMRESOURCE)
175 HRSRC resource_info;
176 HGLOBAL resource_data;
177 LPDXFILELOADRESOURCE lpdxflr = pvSource;
179 TRACE("Source in resource (module = %p, name = %s, type = %s)\n", lpdxflr->hModule, debugstr_a(lpdxflr->lpName), debugstr_a(lpdxflr->lpType));
181 resource_info = FindResourceA(lpdxflr->hModule, lpdxflr->lpName, lpdxflr->lpType);
182 if (!resource_info)
184 hr = DXFILEERR_RESOURCENOTFOUND;
185 goto error;
188 file_size = SizeofResource(lpdxflr->hModule, resource_info);
190 resource_data = LoadResource(lpdxflr->hModule, resource_info);
191 if (!resource_data)
193 hr = DXFILEERR_BADRESOURCE;
194 goto error;
197 file_buffer = LockResource(resource_data);
198 if (!file_buffer)
200 hr = DXFILEERR_BADRESOURCE;
201 goto error;
204 else if (dwLoadOptions == DXFILELOAD_FROMMEMORY)
206 LPDXFILELOADMEMORY lpdxflm = pvSource;
208 TRACE("Source in memory at %p with size %ld.\n", lpdxflm->lpMemory, lpdxflm->dSize);
210 file_buffer = lpdxflm->lpMemory;
211 file_size = lpdxflm->dSize;
213 else
215 FIXME("Source type %ld not handled yet.\n", dwLoadOptions);
216 hr = DXFILEERR_NOTDONEYET;
217 goto error;
220 TRACE("File size is %lu bytes.\n", file_size);
222 if (TRACE_ON(d3dxof_dump))
224 static USHORT num;
225 char tmp[12];
226 HANDLE file;
227 sprintf(tmp, "file%05u.x", num++);
229 file = CreateFileA(tmp, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, NULL);
230 if (file != INVALID_HANDLE_VALUE)
232 WriteFile(file, file_buffer, file_size, &bytes_written, NULL);
233 CloseHandle(file);
237 object->pDirectXFile = This;
239 object->buf.pdxf = This;
240 object->buf.token_present = FALSE;
241 object->buf.buffer = file_buffer;
242 object->buf.rem_bytes = file_size;
243 hr = parse_header(&object->buf, &object->decomp_buffer);
244 if (FAILED(hr))
245 goto error;
247 /* Check if there are templates defined before the object */
248 if (!parse_templates(&object->buf, TRUE))
250 hr = DXFILEERR_PARSEERROR;
251 goto error;
254 if (TRACE_ON(d3dxof))
256 ULONG i;
257 TRACE("Registered templates (%lu):\n", This->nb_xtemplates);
258 for (i = 1; i < This->nb_xtemplates; i++)
259 TRACE("%s - %s\n", This->xtemplates[i].name, debugstr_guid(&This->xtemplates[i].class_id));
262 *ppEnumObj = &object->IDirectXFileEnumObject_iface;
264 return DXFILE_OK;
266 error:
267 IDirectXFileEnumObject_Release(&object->IDirectXFileEnumObject_iface);
268 *ppEnumObj = NULL;
270 return hr;
273 static HRESULT WINAPI IDirectXFileImpl_CreateSaveObject(IDirectXFile *iface, const char *filename,
274 DXFILEFORMAT format, LPDIRECTXFILESAVEOBJECT *out)
276 IDirectXFileSaveObjectImpl *object;
277 HRESULT hr;
279 FIXME("iface %p, filename %s, format %lu, out %p partial stub!\n", iface, debugstr_a(filename),
280 format, out);
282 if (!filename || !out)
283 return E_POINTER;
285 hr = IDirectXFileSaveObjectImpl_Create(&object);
286 if (SUCCEEDED(hr))
287 *out = &object->IDirectXFileSaveObject_iface;
288 return hr;
291 static HRESULT WINAPI IDirectXFileImpl_RegisterTemplates(IDirectXFile* iface, LPVOID pvData, DWORD cbSize)
293 IDirectXFileImpl *This = impl_from_IDirectXFile(iface);
294 parse_buffer buf;
295 HRESULT hr;
296 LPBYTE decomp_buffer = NULL;
297 DWORD bytes_written;
299 ZeroMemory(&buf, sizeof(buf));
300 buf.buffer = pvData;
301 buf.rem_bytes = cbSize;
302 buf.pdxf = This;
304 TRACE("iface %p, data %p, size %lu.\n", iface, pvData, cbSize);
306 if (!pvData)
307 return DXFILEERR_BADVALUE;
309 if (TRACE_ON(d3dxof_dump))
311 static USHORT num;
312 char tmp[16];
313 HANDLE file;
314 sprintf(tmp, "template%05u.x", num++);
316 file = CreateFileA(tmp, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, NULL);
317 if (file != INVALID_HANDLE_VALUE)
319 WriteFile(file, pvData, cbSize, &bytes_written, NULL);
320 CloseHandle(file);
324 hr = parse_header(&buf, &decomp_buffer);
325 if (FAILED(hr))
326 goto cleanup;
328 if (!parse_templates(&buf, FALSE))
330 hr = DXFILEERR_PARSEERROR;
331 goto cleanup;
334 if (TRACE_ON(d3dxof))
336 unsigned int i;
338 TRACE("Registered templates (%lu):\n", This->nb_xtemplates);
339 for (i = 1; i < This->nb_xtemplates; ++i)
340 TRACE("%s - %s\n", This->xtemplates[i].name, debugstr_guid(&This->xtemplates[i].class_id));
343 hr = DXFILE_OK;
344 cleanup:
345 HeapFree(GetProcessHeap(), 0, decomp_buffer);
346 return hr;
349 static const IDirectXFileVtbl IDirectXFile_Vtbl =
351 IDirectXFileImpl_QueryInterface,
352 IDirectXFileImpl_AddRef,
353 IDirectXFileImpl_Release,
354 IDirectXFileImpl_CreateEnumObject,
355 IDirectXFileImpl_CreateSaveObject,
356 IDirectXFileImpl_RegisterTemplates
359 static HRESULT IDirectXFileBinaryImpl_Create(IDirectXFileBinaryImpl** ppObj)
361 IDirectXFileBinaryImpl* object;
363 TRACE("(%p)\n", ppObj);
365 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectXFileBinaryImpl));
366 if (!object)
367 return DXFILEERR_BADALLOC;
369 object->IDirectXFileBinary_iface.lpVtbl = &IDirectXFileBinary_Vtbl;
370 object->ref = 1;
372 *ppObj = object;
374 return DXFILE_OK;
377 static inline IDirectXFileBinaryImpl *impl_from_IDirectXFileBinary(IDirectXFileBinary *iface)
379 return CONTAINING_RECORD(iface, IDirectXFileBinaryImpl, IDirectXFileBinary_iface);
382 static HRESULT WINAPI IDirectXFileBinaryImpl_QueryInterface(IDirectXFileBinary *iface,
383 REFIID riid, void **out)
385 IDirectXFileBinaryImpl *object = impl_from_IDirectXFileBinary(iface);
387 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
389 if (IsEqualGUID(riid, &IID_IUnknown)
390 || IsEqualGUID(riid, &IID_IDirectXFileObject)
391 || IsEqualGUID(riid, &IID_IDirectXFileBinary))
393 *out = &object->IDirectXFileBinary_iface;
395 else
397 *out = NULL;
398 /* Do not print an error for interfaces that can be queried to
399 * retrieve the type of the object */
400 if (!IsEqualGUID(riid, &IID_IDirectXFileData) && !IsEqualGUID(riid, &IID_IDirectXFileDataReference))
401 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
402 return E_NOINTERFACE;
405 IUnknown_AddRef((IUnknown *)*out);
406 return S_OK;
409 static ULONG WINAPI IDirectXFileBinaryImpl_AddRef(IDirectXFileBinary *iface)
411 IDirectXFileBinaryImpl *object = impl_from_IDirectXFileBinary(iface);
412 ULONG refcount = InterlockedIncrement(&object->ref);
414 TRACE("%p increasing refcount to %lu.\n", iface, refcount);
416 return refcount;
419 static ULONG WINAPI IDirectXFileBinaryImpl_Release(IDirectXFileBinary *iface)
421 IDirectXFileBinaryImpl *object = impl_from_IDirectXFileBinary(iface);
422 ULONG refcount = InterlockedDecrement(&object->ref);
424 TRACE("%p decreasing refcount to %lu.\n", iface, refcount);
426 if (!refcount)
427 HeapFree(GetProcessHeap(), 0, object);
429 return refcount;
432 static HRESULT WINAPI IDirectXFileBinaryImpl_GetName(IDirectXFileBinary *iface, char *name,
433 DWORD *length)
435 FIXME("iface %p, name %p, length %p stub!\n", iface, name, length);
437 return DXFILEERR_BADVALUE;
440 static HRESULT WINAPI IDirectXFileBinaryImpl_GetId(IDirectXFileBinary *iface, GUID *guid)
442 FIXME("iface %p, guid %p stub!\n", iface, guid);
444 return DXFILEERR_BADVALUE;
447 static HRESULT WINAPI IDirectXFileBinaryImpl_GetSize(IDirectXFileBinary *iface, DWORD *size)
449 FIXME("iface %p, size %p stub!\n", iface, size);
451 return DXFILEERR_BADVALUE;
454 static HRESULT WINAPI IDirectXFileBinaryImpl_GetMimeType(IDirectXFileBinary *iface, LPCSTR *mimetype)
456 FIXME("iface %p, mimetype %p stub!\n", iface, mimetype);
458 return DXFILEERR_BADVALUE;
461 static HRESULT WINAPI IDirectXFileBinaryImpl_Read(IDirectXFileBinary *iface, void *data,
462 DWORD size, DWORD *count)
464 FIXME("iface %p, data %p, size %lu, count %p stub!\n", iface, data, size, count);
466 return DXFILEERR_BADVALUE;
469 static const IDirectXFileBinaryVtbl IDirectXFileBinary_Vtbl =
471 IDirectXFileBinaryImpl_QueryInterface,
472 IDirectXFileBinaryImpl_AddRef,
473 IDirectXFileBinaryImpl_Release,
474 IDirectXFileBinaryImpl_GetName,
475 IDirectXFileBinaryImpl_GetId,
476 IDirectXFileBinaryImpl_GetSize,
477 IDirectXFileBinaryImpl_GetMimeType,
478 IDirectXFileBinaryImpl_Read
481 static HRESULT IDirectXFileDataImpl_Create(IDirectXFileDataImpl** ppObj)
483 IDirectXFileDataImpl* object;
485 TRACE("(%p)\n", ppObj);
487 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectXFileDataImpl));
488 if (!object)
489 return DXFILEERR_BADALLOC;
491 object->IDirectXFileData_iface.lpVtbl = &IDirectXFileData_Vtbl;
492 object->ref = 1;
494 *ppObj = object;
496 return S_OK;
499 static inline IDirectXFileDataImpl *impl_from_IDirectXFileData(IDirectXFileData *iface)
501 return CONTAINING_RECORD(iface, IDirectXFileDataImpl, IDirectXFileData_iface);
504 static HRESULT WINAPI IDirectXFileDataImpl_QueryInterface(IDirectXFileData *iface, REFIID riid,
505 void **out)
507 IDirectXFileDataImpl *data = impl_from_IDirectXFileData(iface);
509 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
511 if (IsEqualGUID(riid, &IID_IUnknown)
512 || IsEqualGUID(riid, &IID_IDirectXFileObject)
513 || IsEqualGUID(riid, &IID_IDirectXFileData))
515 *out = &data->IDirectXFileData_iface;
517 else
519 *out = NULL;
520 /* Do not print an error for interfaces that can be queried to
521 * retrieve the type of the object */
522 if (!IsEqualGUID(riid, &IID_IDirectXFileBinary) && !IsEqualGUID(riid, &IID_IDirectXFileDataReference))
523 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
524 return E_NOINTERFACE;
527 IUnknown_AddRef((IUnknown *)*out);
528 return S_OK;
531 static ULONG WINAPI IDirectXFileDataImpl_AddRef(IDirectXFileData *iface)
533 IDirectXFileDataImpl *data = impl_from_IDirectXFileData(iface);
534 ULONG refcount = InterlockedIncrement(&data->ref);
536 TRACE("%p increasing refcount to %lu.\n", iface, refcount);
538 return refcount;
541 static ULONG WINAPI IDirectXFileDataImpl_Release(IDirectXFileData *iface)
543 IDirectXFileDataImpl *data = impl_from_IDirectXFileData(iface);
544 ULONG refcount = InterlockedDecrement(&data->ref);
546 TRACE("%p decreasing refcount to %lu.\n", iface, refcount);
548 if (!refcount)
550 if (!data->level && !data->from_ref)
552 HeapFree(GetProcessHeap(), 0, data->pstrings);
553 if (data->pobj)
555 HeapFree(GetProcessHeap(), 0, data->pobj->pdata);
556 HeapFree(GetProcessHeap(), 0, data->pobj);
559 HeapFree(GetProcessHeap(), 0, data);
562 return refcount;
565 /*** IDirectXFileObject methods ***/
566 static HRESULT WINAPI IDirectXFileDataImpl_GetName(IDirectXFileData* iface, LPSTR pstrNameBuf, LPDWORD pdwBufLen)
568 IDirectXFileDataImpl *This = impl_from_IDirectXFileData(iface);
569 DWORD len;
571 TRACE("(%p/%p)->(%p,%p)\n", This, iface, pstrNameBuf, pdwBufLen);
573 if (!pdwBufLen)
574 return DXFILEERR_BADVALUE;
576 len = strlen(This->pobj->name);
577 if (len)
578 len++;
580 if (pstrNameBuf) {
581 if (*pdwBufLen < len)
582 return DXFILEERR_BADVALUE;
583 CopyMemory(pstrNameBuf, This->pobj->name, len);
584 /* Even if we return a size of 0, an empty string with a null byte must be returned */
585 if (*pdwBufLen && !len)
586 pstrNameBuf[0] = 0;
588 *pdwBufLen = len;
590 return DXFILE_OK;
593 static HRESULT WINAPI IDirectXFileDataImpl_GetId(IDirectXFileData* iface, LPGUID pGuid)
595 IDirectXFileDataImpl *This = impl_from_IDirectXFileData(iface);
597 TRACE("(%p/%p)->(%p)\n", This, iface, pGuid);
599 if (!pGuid)
600 return DXFILEERR_BADVALUE;
602 *pGuid = This->pobj->class_id;
604 return DXFILE_OK;
607 /*** IDirectXFileData methods ***/
608 static HRESULT WINAPI IDirectXFileDataImpl_GetData(IDirectXFileData* iface, LPCSTR szMember, DWORD* pcbSize, void** ppvData)
610 IDirectXFileDataImpl *This = impl_from_IDirectXFileData(iface);
612 TRACE("(%p/%p)->(%s,%p,%p)\n", This, iface, debugstr_a(szMember), pcbSize, ppvData);
614 if (!pcbSize || !ppvData)
615 return DXFILEERR_BADVALUE;
617 if (szMember)
619 ULONG i;
620 for (i = 0; i < This->pobj->nb_members; i++)
621 if (!strcmp(This->pobj->members[i].name, szMember))
622 break;
623 if (i == This->pobj->nb_members)
625 WARN("Unknown member '%s'\n", szMember);
626 return DXFILEERR_BADDATAREFERENCE;
628 *pcbSize = This->pobj->members[i].size;
629 *ppvData = This->pobj->root->pdata + This->pobj->members[i].start;
631 else
633 *pcbSize = This->pobj->size;
634 *ppvData = This->pobj->root->pdata + This->pobj->pos_data;
637 return DXFILE_OK;
640 static HRESULT WINAPI IDirectXFileDataImpl_GetType(IDirectXFileData* iface, const GUID** pguid)
642 IDirectXFileDataImpl *This = impl_from_IDirectXFileData(iface);
643 static GUID guid;
645 TRACE("(%p/%p)->(%p)\n", This, iface, pguid);
647 if (!pguid)
648 return DXFILEERR_BADVALUE;
650 guid = This->pobj->type;
651 *pguid = &guid;
653 return DXFILE_OK;
656 static HRESULT WINAPI IDirectXFileDataImpl_GetNextObject(IDirectXFileData* iface, LPDIRECTXFILEOBJECT* ppChildObj)
658 HRESULT hr;
659 IDirectXFileDataImpl *This = impl_from_IDirectXFileData(iface);
661 TRACE("(%p/%p)->(%p)\n", This, iface, ppChildObj);
663 if (This->cur_enum_object >= This->pobj->nb_children)
665 *ppChildObj = NULL;
666 return DXFILEERR_NOMOREOBJECTS;
669 if (This->from_ref && (This->level >= 1))
671 /* Only 2 levels can be enumerated if the object is obtained from a reference */
672 *ppChildObj = NULL;
673 return DXFILEERR_NOMOREOBJECTS;
676 if (This->pobj->children[This->cur_enum_object]->binary)
678 IDirectXFileBinaryImpl *object;
680 hr = IDirectXFileBinaryImpl_Create(&object);
681 if (FAILED(hr))
682 return hr;
684 *ppChildObj = (LPDIRECTXFILEOBJECT)&object->IDirectXFileBinary_iface;
686 else if (This->pobj->children[This->cur_enum_object]->ptarget)
688 IDirectXFileDataReferenceImpl *object;
690 hr = IDirectXFileDataReferenceImpl_Create(&object);
691 if (FAILED(hr))
692 return hr;
694 object->ptarget = This->pobj->children[This->cur_enum_object++]->ptarget;
696 *ppChildObj = (LPDIRECTXFILEOBJECT)&object->IDirectXFileDataReference_iface;
698 else
700 IDirectXFileDataImpl *object;
702 hr = IDirectXFileDataImpl_Create(&object);
703 if (FAILED(hr))
704 return hr;
706 object->pobj = This->pobj->children[This->cur_enum_object++];
707 object->cur_enum_object = 0;
708 object->from_ref = This->from_ref;
709 object->level = This->level + 1;
711 *ppChildObj = (LPDIRECTXFILEOBJECT)&object->IDirectXFileData_iface;
714 return DXFILE_OK;
717 static HRESULT WINAPI IDirectXFileDataImpl_AddDataObject(IDirectXFileData *iface, LPDIRECTXFILEDATA data)
719 FIXME("iface %p, data %p stub!\n", iface, data);
721 return DXFILEERR_BADVALUE;
724 static HRESULT WINAPI IDirectXFileDataImpl_AddDataReference(IDirectXFileData *iface,
725 const char *reference, const GUID *guidref)
727 FIXME("iface %p, reference %s, guid reference %s stub!\n", iface, debugstr_a(reference),
728 debugstr_guid(guidref));
730 return DXFILEERR_BADVALUE;
733 static HRESULT WINAPI IDirectXFileDataImpl_AddBinaryObject(IDirectXFileData *iface, const char *name,
734 const GUID *guid, const char *mimetype, void *data, DWORD size)
736 FIXME("iface %p, name %s, guid %s, mimetype %s, data %p, size %lu stub!\n", iface, debugstr_a(name),
737 debugstr_guid(guid), debugstr_a(mimetype), data, size);
739 return DXFILEERR_BADVALUE;
742 static const IDirectXFileDataVtbl IDirectXFileData_Vtbl =
744 IDirectXFileDataImpl_QueryInterface,
745 IDirectXFileDataImpl_AddRef,
746 IDirectXFileDataImpl_Release,
747 IDirectXFileDataImpl_GetName,
748 IDirectXFileDataImpl_GetId,
749 IDirectXFileDataImpl_GetData,
750 IDirectXFileDataImpl_GetType,
751 IDirectXFileDataImpl_GetNextObject,
752 IDirectXFileDataImpl_AddDataObject,
753 IDirectXFileDataImpl_AddDataReference,
754 IDirectXFileDataImpl_AddBinaryObject
757 static HRESULT IDirectXFileDataReferenceImpl_Create(IDirectXFileDataReferenceImpl** ppObj)
759 IDirectXFileDataReferenceImpl* object;
761 TRACE("(%p)\n", ppObj);
763 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectXFileDataReferenceImpl));
764 if (!object)
765 return DXFILEERR_BADALLOC;
767 object->IDirectXFileDataReference_iface.lpVtbl = &IDirectXFileDataReference_Vtbl;
768 object->ref = 1;
770 *ppObj = object;
772 return S_OK;
775 static inline IDirectXFileDataReferenceImpl *impl_from_IDirectXFileDataReference(IDirectXFileDataReference *iface)
777 return CONTAINING_RECORD(iface, IDirectXFileDataReferenceImpl, IDirectXFileDataReference_iface);
780 static HRESULT WINAPI IDirectXFileDataReferenceImpl_QueryInterface(IDirectXFileDataReference *iface,
781 REFIID riid, void **out)
783 IDirectXFileDataReferenceImpl *object = impl_from_IDirectXFileDataReference(iface);
785 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
787 if (IsEqualGUID(riid, &IID_IUnknown)
788 || IsEqualGUID(riid, &IID_IDirectXFileObject)
789 || IsEqualGUID(riid, &IID_IDirectXFileDataReference))
791 *out = &object->IDirectXFileDataReference_iface;
793 else
795 /* Do not print an error for interfaces that can be queried to
796 * retrieve the type of the object */
797 *out = NULL;
798 if (!IsEqualGUID(riid, &IID_IDirectXFileData) && !IsEqualGUID(riid, &IID_IDirectXFileBinary))
799 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
800 return E_NOINTERFACE;
803 IUnknown_AddRef((IUnknown *)*out);
804 return S_OK;
807 static ULONG WINAPI IDirectXFileDataReferenceImpl_AddRef(IDirectXFileDataReference *iface)
809 IDirectXFileDataReferenceImpl *object = impl_from_IDirectXFileDataReference(iface);
810 ULONG refcount = InterlockedIncrement(&object->ref);
812 TRACE("%p increasing refcount to %lu.\n", iface, refcount);
814 return refcount;
817 static ULONG WINAPI IDirectXFileDataReferenceImpl_Release(IDirectXFileDataReference *iface)
819 IDirectXFileDataReferenceImpl *object = impl_from_IDirectXFileDataReference(iface);
820 ULONG refcount = InterlockedDecrement(&object->ref);
822 TRACE("%p decreasing refcount to %lu.\n", iface, refcount);
824 if (!refcount)
825 HeapFree(GetProcessHeap(), 0, object);
827 return refcount;
830 /*** IDirectXFileObject methods ***/
831 static HRESULT WINAPI IDirectXFileDataReferenceImpl_GetName(IDirectXFileDataReference* iface, LPSTR pstrNameBuf, LPDWORD pdwBufLen)
833 IDirectXFileDataReferenceImpl *This = impl_from_IDirectXFileDataReference(iface);
834 DWORD len;
836 TRACE("(%p/%p)->(%p,%p)\n", This, iface, pstrNameBuf, pdwBufLen);
838 if (!pdwBufLen)
839 return DXFILEERR_BADVALUE;
841 len = strlen(This->ptarget->name);
842 if (len)
843 len++;
845 if (pstrNameBuf) {
846 if (*pdwBufLen < len)
847 return DXFILEERR_BADVALUE;
848 CopyMemory(pstrNameBuf, This->ptarget->name, len);
849 /* Even if we return a size of 0, an empty string with a null byte must be returned */
850 if (*pdwBufLen && !len)
851 pstrNameBuf[0] = 0;
853 *pdwBufLen = len;
855 return DXFILE_OK;
858 static HRESULT WINAPI IDirectXFileDataReferenceImpl_GetId(IDirectXFileDataReference *iface,
859 GUID *guid)
861 IDirectXFileDataReferenceImpl *object = impl_from_IDirectXFileDataReference(iface);
863 TRACE("iface %p, guid %p.\n", iface, guid);
865 if (!guid)
866 return DXFILEERR_BADVALUE;
868 *guid = object->ptarget->class_id;
870 return DXFILE_OK;
873 /*** IDirectXFileDataReference ***/
874 static HRESULT WINAPI IDirectXFileDataReferenceImpl_Resolve(IDirectXFileDataReference* iface, LPDIRECTXFILEDATA* ppDataObj)
876 IDirectXFileDataReferenceImpl *This = impl_from_IDirectXFileDataReference(iface);
877 IDirectXFileDataImpl *object;
878 HRESULT hr;
880 TRACE("(%p/%p)->(%p)\n", This, iface, ppDataObj);
882 if (!ppDataObj)
883 return DXFILEERR_BADVALUE;
885 hr = IDirectXFileDataImpl_Create(&object);
886 if (FAILED(hr))
887 return hr;
889 object->pobj = This->ptarget;
890 object->cur_enum_object = 0;
891 object->level = 0;
892 object->from_ref = TRUE;
894 *ppDataObj = &object->IDirectXFileData_iface;
896 return DXFILE_OK;
899 static const IDirectXFileDataReferenceVtbl IDirectXFileDataReference_Vtbl =
901 IDirectXFileDataReferenceImpl_QueryInterface,
902 IDirectXFileDataReferenceImpl_AddRef,
903 IDirectXFileDataReferenceImpl_Release,
904 IDirectXFileDataReferenceImpl_GetName,
905 IDirectXFileDataReferenceImpl_GetId,
906 IDirectXFileDataReferenceImpl_Resolve
909 static HRESULT IDirectXFileEnumObjectImpl_Create(IDirectXFileEnumObjectImpl** ppObj)
911 IDirectXFileEnumObjectImpl* object;
913 TRACE("(%p)\n", ppObj);
915 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectXFileEnumObjectImpl));
916 if (!object)
917 return DXFILEERR_BADALLOC;
919 object->IDirectXFileEnumObject_iface.lpVtbl = &IDirectXFileEnumObject_Vtbl;
920 object->ref = 1;
922 *ppObj = object;
924 return S_OK;
927 static inline IDirectXFileEnumObjectImpl *impl_from_IDirectXFileEnumObject(IDirectXFileEnumObject *iface)
929 return CONTAINING_RECORD(iface, IDirectXFileEnumObjectImpl, IDirectXFileEnumObject_iface);
932 static HRESULT WINAPI IDirectXFileEnumObjectImpl_QueryInterface(IDirectXFileEnumObject *iface,
933 REFIID riid, void **out)
935 IDirectXFileEnumObjectImpl *object = impl_from_IDirectXFileEnumObject(iface);
937 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
939 if (IsEqualGUID(riid, &IID_IUnknown)
940 || IsEqualGUID(riid, &IID_IDirectXFileEnumObject))
942 *out = &object->IDirectXFileEnumObject_iface;
944 else
946 *out = NULL;
947 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
948 return E_NOINTERFACE;
951 IUnknown_AddRef((IUnknown *)*out);
952 return S_OK;
955 static ULONG WINAPI IDirectXFileEnumObjectImpl_AddRef(IDirectXFileEnumObject *iface)
957 IDirectXFileEnumObjectImpl *object = impl_from_IDirectXFileEnumObject(iface);
958 ULONG refcount = InterlockedIncrement(&object->ref);
960 TRACE("%p increasing refcount to %lu.\n", iface, refcount);
962 return refcount;
965 static ULONG WINAPI IDirectXFileEnumObjectImpl_Release(IDirectXFileEnumObject *iface)
967 IDirectXFileEnumObjectImpl *object = impl_from_IDirectXFileEnumObject(iface);
968 ULONG refcount = InterlockedDecrement(&object->ref);
970 TRACE("%p decreasing refcount to %lu.\n", iface, refcount);
972 if (!refcount)
974 unsigned int i;
976 for (i = 0; i < object->nb_xobjects; ++i)
977 IDirectXFileData_Release(object->pRefObjects[i]);
978 if (object->mapped_memory)
979 UnmapViewOfFile(object->mapped_memory);
980 HeapFree(GetProcessHeap(), 0, object->decomp_buffer);
981 HeapFree(GetProcessHeap(), 0, object);
984 return refcount;
987 /*** IDirectXFileEnumObject methods ***/
988 static HRESULT WINAPI IDirectXFileEnumObjectImpl_GetNextDataObject(IDirectXFileEnumObject* iface, LPDIRECTXFILEDATA* ppDataObj)
990 IDirectXFileEnumObjectImpl *This = impl_from_IDirectXFileEnumObject(iface);
991 IDirectXFileDataImpl* object;
992 HRESULT hr;
994 if (!ppDataObj)
995 return E_POINTER;
997 *ppDataObj = NULL;
999 TRACE("(%p/%p)->(%p)\n", This, iface, ppDataObj);
1001 if (This->nb_xobjects >= MAX_OBJECTS)
1003 ERR("Too many objects\n");
1004 return DXFILEERR_NOMOREOBJECTS;
1007 /* Check if there are templates defined before the object */
1008 if (!parse_templates(&This->buf, TRUE))
1009 return DXFILEERR_PARSEERROR;
1011 if (!This->buf.rem_bytes)
1012 return DXFILEERR_NOMOREOBJECTS;
1014 hr = IDirectXFileDataImpl_Create(&object);
1015 if (FAILED(hr))
1016 return hr;
1018 object->pobj = HeapAlloc(GetProcessHeap(), 0, sizeof(xobject)*MAX_SUBOBJECTS);
1019 if (!object->pobj)
1021 hr = DXFILEERR_BADALLOC;
1022 goto error;
1025 object->pstrings = HeapAlloc(GetProcessHeap(), 0, MAX_STRINGS_BUFFER);
1026 if (!object->pstrings)
1028 hr = DXFILEERR_BADALLOC;
1029 goto error;
1032 object->cur_enum_object = 0;
1033 object->level = 0;
1034 object->from_ref = FALSE;
1036 This->buf.pxo_globals = This->xobjects;
1037 This->buf.nb_pxo_globals = This->nb_xobjects;
1038 This->buf.level = 0;
1039 This->buf.pdata = NULL;
1040 This->buf.capacity = 0;
1041 This->buf.cur_pos_data = 0;
1042 This->buf.cur_pstrings = This->buf.pstrings = object->pstrings;
1043 This->buf.pxo = This->xobjects[This->nb_xobjects] = This->buf.pxo_tab = object->pobj;
1044 This->buf.pxo->pdata = NULL;
1045 This->buf.pxo->nb_subobjects = 1;
1047 if (!parse_object(&This->buf))
1049 WARN("Object is not correct\n");
1050 hr = DXFILEERR_PARSEERROR;
1051 goto error;
1054 *ppDataObj = &object->IDirectXFileData_iface;
1056 /* Get a reference to created object */
1057 This->pRefObjects[This->nb_xobjects] = &object->IDirectXFileData_iface;
1058 IDirectXFileData_AddRef(This->pRefObjects[This->nb_xobjects]);
1060 This->nb_xobjects++;
1062 return DXFILE_OK;
1064 error:
1066 IDirectXFileData_Release(&object->IDirectXFileData_iface);
1068 return hr;
1071 static HRESULT WINAPI IDirectXFileEnumObjectImpl_GetDataObjectById(IDirectXFileEnumObject *iface,
1072 REFGUID guid, LPDIRECTXFILEDATA *data)
1074 FIXME("iface %p, guid %s, data %p stub!\n", iface, debugstr_guid(guid), data);
1076 return DXFILEERR_BADVALUE;
1079 static HRESULT WINAPI IDirectXFileEnumObjectImpl_GetDataObjectByName(IDirectXFileEnumObject *iface,
1080 const char *name, LPDIRECTXFILEDATA *data)
1082 FIXME("iface %p, name %s, data %p stub!\n", iface, debugstr_a(name), data);
1084 return DXFILEERR_BADVALUE;
1087 static const IDirectXFileEnumObjectVtbl IDirectXFileEnumObject_Vtbl =
1089 IDirectXFileEnumObjectImpl_QueryInterface,
1090 IDirectXFileEnumObjectImpl_AddRef,
1091 IDirectXFileEnumObjectImpl_Release,
1092 IDirectXFileEnumObjectImpl_GetNextDataObject,
1093 IDirectXFileEnumObjectImpl_GetDataObjectById,
1094 IDirectXFileEnumObjectImpl_GetDataObjectByName
1097 static HRESULT IDirectXFileSaveObjectImpl_Create(IDirectXFileSaveObjectImpl** ppObj)
1099 IDirectXFileSaveObjectImpl* object;
1101 TRACE("(%p)\n", ppObj);
1103 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectXFileSaveObjectImpl));
1104 if (!object)
1105 return DXFILEERR_BADALLOC;
1107 object->IDirectXFileSaveObject_iface.lpVtbl = &IDirectXFileSaveObject_Vtbl;
1108 object->ref = 1;
1110 *ppObj = object;
1112 return S_OK;
1115 static inline IDirectXFileSaveObjectImpl *impl_from_IDirectXFileSaveObject(IDirectXFileSaveObject *iface)
1117 return CONTAINING_RECORD(iface, IDirectXFileSaveObjectImpl, IDirectXFileSaveObject_iface);
1120 static HRESULT WINAPI IDirectXFileSaveObjectImpl_QueryInterface(IDirectXFileSaveObject *iface,
1121 REFIID riid, void **out)
1123 IDirectXFileSaveObjectImpl *object = impl_from_IDirectXFileSaveObject(iface);
1125 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
1127 if (IsEqualGUID(riid, &IID_IUnknown)
1128 || IsEqualGUID(riid, &IID_IDirectXFileSaveObject))
1130 *out = &object->IDirectXFileSaveObject_iface;
1132 else
1134 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
1135 return E_NOINTERFACE;
1138 IUnknown_AddRef((IUnknown *)*out);
1139 return S_OK;
1142 static ULONG WINAPI IDirectXFileSaveObjectImpl_AddRef(IDirectXFileSaveObject *iface)
1144 IDirectXFileSaveObjectImpl *object = impl_from_IDirectXFileSaveObject(iface);
1145 ULONG refcount = InterlockedIncrement(&object->ref);
1147 TRACE("%p increasing refcount to %lu.\n", iface, refcount);
1149 return refcount;
1152 static ULONG WINAPI IDirectXFileSaveObjectImpl_Release(IDirectXFileSaveObject *iface)
1154 IDirectXFileSaveObjectImpl *object = impl_from_IDirectXFileSaveObject(iface);
1155 ULONG refcount = InterlockedDecrement(&object->ref);
1157 TRACE("%p decreasing refcount to %lu.\n", iface, refcount);
1159 if (!refcount)
1160 HeapFree(GetProcessHeap(), 0, object);
1162 return refcount;
1165 static HRESULT WINAPI IDirectXFileSaveObjectImpl_SaveTemplates(IDirectXFileSaveObject *iface,
1166 DWORD count, const GUID **templates)
1168 FIXME("iface %p, count %lu, templates %p stub!\n", iface, count, templates);
1170 return DXFILE_OK;
1173 static HRESULT WINAPI IDirectXFileSaveObjectImpl_CreateDataObject(IDirectXFileSaveObject *iface, REFGUID template,
1174 const char *name, const GUID *guid, DWORD size, void *data, LPDIRECTXFILEDATA *dataobj)
1176 FIXME("iface %p, template %s, name %s, guid %s, size %lu, data %p, dataobj %p stub!\n",
1177 iface, debugstr_guid(template), debugstr_a(name), debugstr_guid(guid), size, data, dataobj);
1179 return DXFILEERR_BADVALUE;
1182 static HRESULT WINAPI IDirectXFileSaveObjectImpl_SaveData(IDirectXFileSaveObject *iface, LPDIRECTXFILEDATA data)
1184 FIXME("iface %p, data %p stub!\n", iface, data);
1186 return DXFILEERR_BADVALUE;
1189 static const IDirectXFileSaveObjectVtbl IDirectXFileSaveObject_Vtbl =
1191 IDirectXFileSaveObjectImpl_QueryInterface,
1192 IDirectXFileSaveObjectImpl_AddRef,
1193 IDirectXFileSaveObjectImpl_Release,
1194 IDirectXFileSaveObjectImpl_SaveTemplates,
1195 IDirectXFileSaveObjectImpl_CreateDataObject,
1196 IDirectXFileSaveObjectImpl_SaveData