Release 2.2.
[wine.git] / dlls / d3dxof / d3dxof.c
blob549955193a5358a756a5e95a2abb1c529c8c652b
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 "config.h"
22 #include "wine/debug.h"
24 #define COBJMACROS
26 #include "winbase.h"
27 #include "wingdi.h"
29 #include "d3dxof_private.h"
30 #include "dxfile.h"
32 #include <stdio.h>
34 WINE_DEFAULT_DEBUG_CHANNEL(d3dxof);
35 WINE_DECLARE_DEBUG_CHANNEL(d3dxof_dump);
37 static const struct IDirectXFileVtbl IDirectXFile_Vtbl;
38 static const struct IDirectXFileBinaryVtbl IDirectXFileBinary_Vtbl;
39 static const struct IDirectXFileDataVtbl IDirectXFileData_Vtbl;
40 static const struct IDirectXFileDataReferenceVtbl IDirectXFileDataReference_Vtbl;
41 static const struct IDirectXFileEnumObjectVtbl IDirectXFileEnumObject_Vtbl;
42 static const struct IDirectXFileSaveObjectVtbl IDirectXFileSaveObject_Vtbl;
44 static HRESULT IDirectXFileDataReferenceImpl_Create(IDirectXFileDataReferenceImpl** ppObj);
45 static HRESULT IDirectXFileEnumObjectImpl_Create(IDirectXFileEnumObjectImpl** ppObj);
46 static HRESULT IDirectXFileSaveObjectImpl_Create(IDirectXFileSaveObjectImpl** ppObj);
48 #define TOKEN_DWORD 41
49 #define TOKEN_FLOAT 42
51 HRESULT IDirectXFileImpl_Create(IUnknown* pUnkOuter, LPVOID* ppObj)
53 IDirectXFileImpl* object;
55 TRACE("(%p,%p)\n", pUnkOuter, ppObj);
57 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectXFileImpl));
58 if (!object)
59 return DXFILEERR_BADALLOC;
61 object->IDirectXFile_iface.lpVtbl = &IDirectXFile_Vtbl;
62 object->ref = 1;
64 /* Reserve first template to handle the case sensitive legacy type indexColor */
65 object->nb_xtemplates = 1;
66 strcpy(object->xtemplates[0].name, "indexColor");
67 object->xtemplates[0].nb_members = 2;
68 object->xtemplates[0].members[0].type = TOKEN_DWORD;
69 object->xtemplates[0].members[0].nb_dims = 0;
70 object->xtemplates[0].members[1].type = TOKEN_FLOAT;
71 object->xtemplates[0].members[1].nb_dims = 1;
72 object->xtemplates[0].members[1].dim_fixed[0] = TRUE;
73 object->xtemplates[0].members[1].dim_value[0] = 4;
75 *ppObj = &object->IDirectXFile_iface;
77 return S_OK;
80 static inline IDirectXFileImpl *impl_from_IDirectXFile(IDirectXFile *iface)
82 return CONTAINING_RECORD(iface, IDirectXFileImpl, IDirectXFile_iface);
85 /*** IUnknown methods ***/
86 static HRESULT WINAPI IDirectXFileImpl_QueryInterface(IDirectXFile* iface, REFIID riid, void** ppvObject)
88 IDirectXFileImpl *This = impl_from_IDirectXFile(iface);
90 TRACE("(%p/%p)->(%s,%p)\n", iface, This, debugstr_guid(riid), ppvObject);
92 if (IsEqualGUID(riid, &IID_IUnknown)
93 || IsEqualGUID(riid, &IID_IDirectXFile))
95 IUnknown_AddRef(iface);
96 *ppvObject = &This->IDirectXFile_iface;
97 return S_OK;
100 ERR("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppvObject);
101 return E_NOINTERFACE;
104 static ULONG WINAPI IDirectXFileImpl_AddRef(IDirectXFile* iface)
106 IDirectXFileImpl *This = impl_from_IDirectXFile(iface);
107 ULONG ref = InterlockedIncrement(&This->ref);
109 TRACE("(%p/%p)->(): new ref %d\n", iface, This, ref);
111 return ref;
114 static ULONG WINAPI IDirectXFileImpl_Release(IDirectXFile* iface)
116 IDirectXFileImpl *This = impl_from_IDirectXFile(iface);
117 ULONG ref = InterlockedDecrement(&This->ref);
119 TRACE("(%p/%p)->(): new ref %d\n", iface, This, ref);
121 if (!ref)
122 HeapFree(GetProcessHeap(), 0, This);
124 return ref;
127 /*** IDirectXFile methods ***/
128 static HRESULT WINAPI IDirectXFileImpl_CreateEnumObject(IDirectXFile* iface, LPVOID pvSource, DXFILELOADOPTIONS dwLoadOptions, LPDIRECTXFILEENUMOBJECT* ppEnumObj)
130 IDirectXFileImpl *This = impl_from_IDirectXFile(iface);
131 IDirectXFileEnumObjectImpl* object;
132 HRESULT hr;
133 LPBYTE file_buffer;
134 DWORD file_size;
135 DWORD bytes_written;
137 TRACE("(%p/%p)->(%p,%x,%p)\n", This, iface, pvSource, dwLoadOptions, ppEnumObj);
139 if (!ppEnumObj)
140 return DXFILEERR_BADVALUE;
142 /* Only lowest 4 bits are relevant in DXFILELOADOPTIONS */
143 dwLoadOptions &= 0xF;
145 hr = IDirectXFileEnumObjectImpl_Create(&object);
146 if (FAILED(hr))
147 return hr;
149 if (dwLoadOptions == DXFILELOAD_FROMFILE)
151 HANDLE hFile, file_mapping;
153 TRACE("Open source file '%s'\n", (char*)pvSource);
155 hFile = CreateFileA(pvSource, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
156 if (hFile == INVALID_HANDLE_VALUE)
158 TRACE("File '%s' not found\n", (char*)pvSource);
159 return DXFILEERR_FILENOTFOUND;
162 file_size = GetFileSize(hFile, NULL);
164 file_mapping = CreateFileMappingA(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
165 CloseHandle(hFile);
166 if (!file_mapping)
168 hr = DXFILEERR_BADFILETYPE;
169 goto error;
172 object->mapped_memory = MapViewOfFile(file_mapping, FILE_MAP_READ, 0, 0, 0);
173 CloseHandle(file_mapping);
174 if (!object->mapped_memory)
176 hr = DXFILEERR_BADFILETYPE;
177 goto error;
179 file_buffer = object->mapped_memory;
181 else if (dwLoadOptions == DXFILELOAD_FROMRESOURCE)
183 HRSRC resource_info;
184 HGLOBAL resource_data;
185 LPDXFILELOADRESOURCE lpdxflr = pvSource;
187 TRACE("Source in resource (module = %p, name = %s, type = %s)\n", lpdxflr->hModule, debugstr_a(lpdxflr->lpName), debugstr_a(lpdxflr->lpType));
189 resource_info = FindResourceA(lpdxflr->hModule, lpdxflr->lpName, lpdxflr->lpType);
190 if (!resource_info)
192 hr = DXFILEERR_RESOURCENOTFOUND;
193 goto error;
196 file_size = SizeofResource(lpdxflr->hModule, resource_info);
198 resource_data = LoadResource(lpdxflr->hModule, resource_info);
199 if (!resource_data)
201 hr = DXFILEERR_BADRESOURCE;
202 goto error;
205 file_buffer = LockResource(resource_data);
206 if (!file_buffer)
208 hr = DXFILEERR_BADRESOURCE;
209 goto error;
212 else if (dwLoadOptions == DXFILELOAD_FROMMEMORY)
214 LPDXFILELOADMEMORY lpdxflm = pvSource;
216 TRACE("Source in memory at %p with size %d\n", lpdxflm->lpMemory, lpdxflm->dSize);
218 file_buffer = lpdxflm->lpMemory;
219 file_size = lpdxflm->dSize;
221 else
223 FIXME("Source type %d is not handled yet\n", dwLoadOptions);
224 hr = DXFILEERR_NOTDONEYET;
225 goto error;
228 TRACE("File size is %d bytes\n", file_size);
230 if (TRACE_ON(d3dxof_dump))
232 static USHORT num;
233 char tmp[12];
234 HANDLE file;
235 sprintf(tmp, "file%05u.x", num++);
237 file = CreateFileA(tmp, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, NULL);
238 if (file != INVALID_HANDLE_VALUE)
240 WriteFile(file, file_buffer, file_size, &bytes_written, NULL);
241 CloseHandle(file);
245 object->pDirectXFile = This;
247 object->buf.pdxf = This;
248 object->buf.token_present = FALSE;
249 object->buf.buffer = file_buffer;
250 object->buf.rem_bytes = file_size;
251 hr = parse_header(&object->buf, &object->decomp_buffer);
252 if (FAILED(hr))
253 goto error;
255 /* Check if there are templates defined before the object */
256 if (!parse_templates(&object->buf, TRUE))
258 hr = DXFILEERR_PARSEERROR;
259 goto error;
262 if (TRACE_ON(d3dxof))
264 ULONG i;
265 TRACE("Registered templates (%d):\n", This->nb_xtemplates);
266 for (i = 1; i < This->nb_xtemplates; i++)
267 DPRINTF("%s - %s\n", This->xtemplates[i].name, debugstr_guid(&This->xtemplates[i].class_id));
270 *ppEnumObj = &object->IDirectXFileEnumObject_iface;
272 return DXFILE_OK;
274 error:
275 IDirectXFileEnumObject_Release(&object->IDirectXFileEnumObject_iface);
276 *ppEnumObj = NULL;
278 return hr;
281 static HRESULT WINAPI IDirectXFileImpl_CreateSaveObject(IDirectXFile* iface, LPCSTR szFileName, DXFILEFORMAT dwFileFormat, LPDIRECTXFILESAVEOBJECT* ppSaveObj)
283 IDirectXFileImpl *This = impl_from_IDirectXFile(iface);
284 IDirectXFileSaveObjectImpl *object;
285 HRESULT hr;
287 FIXME("(%p/%p)->(%s,%x,%p) partial stub!\n", This, iface, szFileName, dwFileFormat, ppSaveObj);
289 if (!szFileName || !ppSaveObj)
290 return E_POINTER;
292 hr = IDirectXFileSaveObjectImpl_Create(&object);
293 if (SUCCEEDED(hr))
294 *ppSaveObj = &object->IDirectXFileSaveObject_iface;
295 return hr;
298 static HRESULT WINAPI IDirectXFileImpl_RegisterTemplates(IDirectXFile* iface, LPVOID pvData, DWORD cbSize)
300 IDirectXFileImpl *This = impl_from_IDirectXFile(iface);
301 parse_buffer buf;
302 HRESULT hr;
303 LPBYTE decomp_buffer = NULL;
304 DWORD bytes_written;
306 ZeroMemory(&buf, sizeof(buf));
307 buf.buffer = pvData;
308 buf.rem_bytes = cbSize;
309 buf.pdxf = This;
311 TRACE("(%p/%p)->(%p,%d)\n", This, iface, pvData, cbSize);
313 if (!pvData)
314 return DXFILEERR_BADVALUE;
316 if (TRACE_ON(d3dxof_dump))
318 static USHORT num;
319 char tmp[16];
320 HANDLE file;
321 sprintf(tmp, "template%05u.x", num++);
323 file = CreateFileA(tmp, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, NULL);
324 if (file != INVALID_HANDLE_VALUE)
326 WriteFile(file, pvData, cbSize, &bytes_written, NULL);
327 CloseHandle(file);
331 hr = parse_header(&buf, &decomp_buffer);
332 if (FAILED(hr))
333 goto cleanup;
335 if (!parse_templates(&buf, FALSE))
337 hr = DXFILEERR_PARSEERROR;
338 goto cleanup;
341 if (TRACE_ON(d3dxof))
343 ULONG i;
344 TRACE("Registered templates (%d):\n", This->nb_xtemplates);
345 for (i = 1; i < This->nb_xtemplates; i++)
346 DPRINTF("%s - %s\n", This->xtemplates[i].name, debugstr_guid(&This->xtemplates[i].class_id));
349 hr = DXFILE_OK;
350 cleanup:
351 HeapFree(GetProcessHeap(), 0, decomp_buffer);
352 return hr;
355 static const IDirectXFileVtbl IDirectXFile_Vtbl =
357 IDirectXFileImpl_QueryInterface,
358 IDirectXFileImpl_AddRef,
359 IDirectXFileImpl_Release,
360 IDirectXFileImpl_CreateEnumObject,
361 IDirectXFileImpl_CreateSaveObject,
362 IDirectXFileImpl_RegisterTemplates
365 static HRESULT IDirectXFileBinaryImpl_Create(IDirectXFileBinaryImpl** ppObj)
367 IDirectXFileBinaryImpl* object;
369 TRACE("(%p)\n", ppObj);
371 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectXFileBinaryImpl));
372 if (!object)
373 return DXFILEERR_BADALLOC;
375 object->IDirectXFileBinary_iface.lpVtbl = &IDirectXFileBinary_Vtbl;
376 object->ref = 1;
378 *ppObj = object;
380 return DXFILE_OK;
383 static inline IDirectXFileBinaryImpl *impl_from_IDirectXFileBinary(IDirectXFileBinary *iface)
385 return CONTAINING_RECORD(iface, IDirectXFileBinaryImpl, IDirectXFileBinary_iface);
388 /*** IUnknown methods ***/
389 static HRESULT WINAPI IDirectXFileBinaryImpl_QueryInterface(IDirectXFileBinary* iface, REFIID riid, void** ppvObject)
391 IDirectXFileBinaryImpl *This = impl_from_IDirectXFileBinary(iface);
393 TRACE("(%p/%p)->(%s,%p)\n", iface, This, debugstr_guid(riid), ppvObject);
395 if (IsEqualGUID(riid, &IID_IUnknown)
396 || IsEqualGUID(riid, &IID_IDirectXFileObject)
397 || IsEqualGUID(riid, &IID_IDirectXFileBinary))
399 IUnknown_AddRef(iface);
400 *ppvObject = &This->IDirectXFileBinary_iface;
401 return S_OK;
404 /* Do not print an error for interfaces that can be queried to retrieve the type of the object */
405 if (!IsEqualGUID(riid, &IID_IDirectXFileData)
406 && !IsEqualGUID(riid, &IID_IDirectXFileDataReference))
407 ERR("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppvObject);
409 return E_NOINTERFACE;
412 static ULONG WINAPI IDirectXFileBinaryImpl_AddRef(IDirectXFileBinary* iface)
414 IDirectXFileBinaryImpl *This = impl_from_IDirectXFileBinary(iface);
415 ULONG ref = InterlockedIncrement(&This->ref);
417 TRACE("(%p/%p)->(): new ref %d\n", iface, This, ref);
419 return ref;
422 static ULONG WINAPI IDirectXFileBinaryImpl_Release(IDirectXFileBinary* iface)
424 IDirectXFileBinaryImpl *This = impl_from_IDirectXFileBinary(iface);
425 ULONG ref = InterlockedDecrement(&This->ref);
427 TRACE("(%p/%p)->(): new ref %d\n", iface, This, ref);
429 if (!ref)
430 HeapFree(GetProcessHeap(), 0, This);
432 return ref;
435 /*** IDirectXFileObject methods ***/
436 static HRESULT WINAPI IDirectXFileBinaryImpl_GetName(IDirectXFileBinary* iface, LPSTR pstrNameBuf, LPDWORD pdwBufLen)
439 IDirectXFileBinaryImpl *This = impl_from_IDirectXFileBinary(iface);
441 FIXME("(%p/%p)->(%p,%p) stub!\n", This, iface, pstrNameBuf, pdwBufLen);
443 return DXFILEERR_BADVALUE;
446 static HRESULT WINAPI IDirectXFileBinaryImpl_GetId(IDirectXFileBinary* iface, LPGUID pGuid)
448 IDirectXFileBinaryImpl *This = impl_from_IDirectXFileBinary(iface);
450 FIXME("(%p/%p)->(%p) stub!\n", This, iface, pGuid);
452 return DXFILEERR_BADVALUE;
455 /*** IDirectXFileBinary methods ***/
456 static HRESULT WINAPI IDirectXFileBinaryImpl_GetSize(IDirectXFileBinary* iface, DWORD* pcbSize)
458 IDirectXFileBinaryImpl *This = impl_from_IDirectXFileBinary(iface);
460 FIXME("(%p/%p)->(%p) stub!\n", This, iface, pcbSize);
462 return DXFILEERR_BADVALUE;
465 static HRESULT WINAPI IDirectXFileBinaryImpl_GetMimeType(IDirectXFileBinary* iface, LPCSTR* pszMimeType)
467 IDirectXFileBinaryImpl *This = impl_from_IDirectXFileBinary(iface);
469 FIXME("(%p/%p)->(%p) stub!\n", This, iface, pszMimeType);
471 return DXFILEERR_BADVALUE;
474 static HRESULT WINAPI IDirectXFileBinaryImpl_Read(IDirectXFileBinary* iface, LPVOID pvData, DWORD cbSize, LPDWORD pcbRead)
476 IDirectXFileBinaryImpl *This = impl_from_IDirectXFileBinary(iface);
478 FIXME("(%p/%p)->(%p, %d, %p) stub!\n", This, iface, pvData, cbSize, pcbRead);
480 return DXFILEERR_BADVALUE;
483 static const IDirectXFileBinaryVtbl IDirectXFileBinary_Vtbl =
485 IDirectXFileBinaryImpl_QueryInterface,
486 IDirectXFileBinaryImpl_AddRef,
487 IDirectXFileBinaryImpl_Release,
488 IDirectXFileBinaryImpl_GetName,
489 IDirectXFileBinaryImpl_GetId,
490 IDirectXFileBinaryImpl_GetSize,
491 IDirectXFileBinaryImpl_GetMimeType,
492 IDirectXFileBinaryImpl_Read
495 static HRESULT IDirectXFileDataImpl_Create(IDirectXFileDataImpl** ppObj)
497 IDirectXFileDataImpl* object;
499 TRACE("(%p)\n", ppObj);
501 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectXFileDataImpl));
502 if (!object)
503 return DXFILEERR_BADALLOC;
505 object->IDirectXFileData_iface.lpVtbl = &IDirectXFileData_Vtbl;
506 object->ref = 1;
508 *ppObj = object;
510 return S_OK;
513 static inline IDirectXFileDataImpl *impl_from_IDirectXFileData(IDirectXFileData *iface)
515 return CONTAINING_RECORD(iface, IDirectXFileDataImpl, IDirectXFileData_iface);
518 /*** IUnknown methods ***/
519 static HRESULT WINAPI IDirectXFileDataImpl_QueryInterface(IDirectXFileData* iface, REFIID riid, void** ppvObject)
521 IDirectXFileDataImpl *This = impl_from_IDirectXFileData(iface);
523 TRACE("(%p/%p)->(%s,%p)\n", iface, This, debugstr_guid(riid), ppvObject);
525 if (IsEqualGUID(riid, &IID_IUnknown)
526 || IsEqualGUID(riid, &IID_IDirectXFileObject)
527 || IsEqualGUID(riid, &IID_IDirectXFileData))
529 IUnknown_AddRef(iface);
530 *ppvObject = &This->IDirectXFileData_iface;
531 return S_OK;
534 /* Do not print an error for interfaces that can be queried to retrieve the type of the object */
535 if (!IsEqualGUID(riid, &IID_IDirectXFileBinary)
536 && !IsEqualGUID(riid, &IID_IDirectXFileDataReference))
537 ERR("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppvObject);
539 return E_NOINTERFACE;
542 static ULONG WINAPI IDirectXFileDataImpl_AddRef(IDirectXFileData* iface)
544 IDirectXFileDataImpl *This = impl_from_IDirectXFileData(iface);
545 ULONG ref = InterlockedIncrement(&This->ref);
547 TRACE("(%p/%p)->(): new ref %d\n", iface, This, ref);
549 return ref;
552 static ULONG WINAPI IDirectXFileDataImpl_Release(IDirectXFileData* iface)
554 IDirectXFileDataImpl *This = impl_from_IDirectXFileData(iface);
555 ULONG ref = InterlockedDecrement(&This->ref);
557 TRACE("(%p/%p)->(): new ref %d\n", iface, This, ref);
559 if (!ref)
561 if (!This->level && !This->from_ref)
563 HeapFree(GetProcessHeap(), 0, This->pstrings);
564 if (This->pobj)
566 HeapFree(GetProcessHeap(), 0, This->pobj->pdata);
567 HeapFree(GetProcessHeap(), 0, This->pobj);
570 HeapFree(GetProcessHeap(), 0, This);
573 return ref;
576 /*** IDirectXFileObject methods ***/
577 static HRESULT WINAPI IDirectXFileDataImpl_GetName(IDirectXFileData* iface, LPSTR pstrNameBuf, LPDWORD pdwBufLen)
579 IDirectXFileDataImpl *This = impl_from_IDirectXFileData(iface);
580 DWORD len;
582 TRACE("(%p/%p)->(%p,%p)\n", This, iface, pstrNameBuf, pdwBufLen);
584 if (!pdwBufLen)
585 return DXFILEERR_BADVALUE;
587 len = strlen(This->pobj->name);
588 if (len)
589 len++;
591 if (pstrNameBuf) {
592 if (*pdwBufLen < len)
593 return DXFILEERR_BADVALUE;
594 CopyMemory(pstrNameBuf, This->pobj->name, len);
595 /* Even if we return a size of 0, an empty string with a null byte must be returned */
596 if (*pdwBufLen && !len)
597 pstrNameBuf[0] = 0;
599 *pdwBufLen = len;
601 return DXFILE_OK;
604 static HRESULT WINAPI IDirectXFileDataImpl_GetId(IDirectXFileData* iface, LPGUID pGuid)
606 IDirectXFileDataImpl *This = impl_from_IDirectXFileData(iface);
608 TRACE("(%p/%p)->(%p)\n", This, iface, pGuid);
610 if (!pGuid)
611 return DXFILEERR_BADVALUE;
613 *pGuid = This->pobj->class_id;
615 return DXFILE_OK;
618 /*** IDirectXFileData methods ***/
619 static HRESULT WINAPI IDirectXFileDataImpl_GetData(IDirectXFileData* iface, LPCSTR szMember, DWORD* pcbSize, void** ppvData)
621 IDirectXFileDataImpl *This = impl_from_IDirectXFileData(iface);
623 TRACE("(%p/%p)->(%s,%p,%p)\n", This, iface, debugstr_a(szMember), pcbSize, ppvData);
625 if (!pcbSize || !ppvData)
626 return DXFILEERR_BADVALUE;
628 if (szMember)
630 ULONG i;
631 for (i = 0; i < This->pobj->nb_members; i++)
632 if (!strcmp(This->pobj->members[i].name, szMember))
633 break;
634 if (i == This->pobj->nb_members)
636 WARN("Unknown member '%s'\n", szMember);
637 return DXFILEERR_BADDATAREFERENCE;
639 *pcbSize = This->pobj->members[i].size;
640 *ppvData = This->pobj->root->pdata + This->pobj->members[i].start;
642 else
644 *pcbSize = This->pobj->size;
645 *ppvData = This->pobj->root->pdata + This->pobj->pos_data;
648 return DXFILE_OK;
651 static HRESULT WINAPI IDirectXFileDataImpl_GetType(IDirectXFileData* iface, const GUID** pguid)
653 IDirectXFileDataImpl *This = impl_from_IDirectXFileData(iface);
654 static GUID guid;
656 TRACE("(%p/%p)->(%p)\n", This, iface, pguid);
658 if (!pguid)
659 return DXFILEERR_BADVALUE;
661 guid = This->pobj->type;
662 *pguid = &guid;
664 return DXFILE_OK;
667 static HRESULT WINAPI IDirectXFileDataImpl_GetNextObject(IDirectXFileData* iface, LPDIRECTXFILEOBJECT* ppChildObj)
669 HRESULT hr;
670 IDirectXFileDataImpl *This = impl_from_IDirectXFileData(iface);
672 TRACE("(%p/%p)->(%p)\n", This, iface, ppChildObj);
674 if (This->cur_enum_object >= This->pobj->nb_children)
676 *ppChildObj = NULL;
677 return DXFILEERR_NOMOREOBJECTS;
680 if (This->from_ref && (This->level >= 1))
682 /* Only 2 levels can be enumerated if the object is obtained from a reference */
683 *ppChildObj = NULL;
684 return DXFILEERR_NOMOREOBJECTS;
687 if (This->pobj->children[This->cur_enum_object]->binary)
689 IDirectXFileBinaryImpl *object;
691 hr = IDirectXFileBinaryImpl_Create(&object);
692 if (FAILED(hr))
693 return hr;
695 *ppChildObj = (LPDIRECTXFILEOBJECT)&object->IDirectXFileBinary_iface;
697 else if (This->pobj->children[This->cur_enum_object]->ptarget)
699 IDirectXFileDataReferenceImpl *object;
701 hr = IDirectXFileDataReferenceImpl_Create(&object);
702 if (FAILED(hr))
703 return hr;
705 object->ptarget = This->pobj->children[This->cur_enum_object++]->ptarget;
707 *ppChildObj = (LPDIRECTXFILEOBJECT)&object->IDirectXFileDataReference_iface;
709 else
711 IDirectXFileDataImpl *object;
713 hr = IDirectXFileDataImpl_Create(&object);
714 if (FAILED(hr))
715 return hr;
717 object->pobj = This->pobj->children[This->cur_enum_object++];
718 object->cur_enum_object = 0;
719 object->from_ref = This->from_ref;
720 object->level = This->level + 1;
722 *ppChildObj = (LPDIRECTXFILEOBJECT)&object->IDirectXFileData_iface;
725 return DXFILE_OK;
728 static HRESULT WINAPI IDirectXFileDataImpl_AddDataObject(IDirectXFileData* iface, LPDIRECTXFILEDATA pDataObj)
730 IDirectXFileDataImpl *This = impl_from_IDirectXFileData(iface);
732 FIXME("(%p/%p)->(%p) stub!\n", This, iface, pDataObj);
734 return DXFILEERR_BADVALUE;
737 static HRESULT WINAPI IDirectXFileDataImpl_AddDataReference(IDirectXFileData* iface, LPCSTR szRef, const GUID* pguidRef)
739 IDirectXFileDataImpl *This = impl_from_IDirectXFileData(iface);
741 FIXME("(%p/%p)->(%s,%p) stub!\n", This, iface, szRef, pguidRef);
743 return DXFILEERR_BADVALUE;
746 static HRESULT WINAPI IDirectXFileDataImpl_AddBinaryObject(IDirectXFileData* iface, LPCSTR szName, const GUID* pguid, LPCSTR szMimeType, LPVOID pvData, DWORD cbSize)
748 IDirectXFileDataImpl *This = impl_from_IDirectXFileData(iface);
750 FIXME("(%p/%p)->(%s,%p,%s,%p,%d) stub!\n", This, iface, szName, pguid, szMimeType, pvData, cbSize);
752 return DXFILEERR_BADVALUE;
755 static const IDirectXFileDataVtbl IDirectXFileData_Vtbl =
757 IDirectXFileDataImpl_QueryInterface,
758 IDirectXFileDataImpl_AddRef,
759 IDirectXFileDataImpl_Release,
760 IDirectXFileDataImpl_GetName,
761 IDirectXFileDataImpl_GetId,
762 IDirectXFileDataImpl_GetData,
763 IDirectXFileDataImpl_GetType,
764 IDirectXFileDataImpl_GetNextObject,
765 IDirectXFileDataImpl_AddDataObject,
766 IDirectXFileDataImpl_AddDataReference,
767 IDirectXFileDataImpl_AddBinaryObject
770 static HRESULT IDirectXFileDataReferenceImpl_Create(IDirectXFileDataReferenceImpl** ppObj)
772 IDirectXFileDataReferenceImpl* object;
774 TRACE("(%p)\n", ppObj);
776 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectXFileDataReferenceImpl));
777 if (!object)
778 return DXFILEERR_BADALLOC;
780 object->IDirectXFileDataReference_iface.lpVtbl = &IDirectXFileDataReference_Vtbl;
781 object->ref = 1;
783 *ppObj = object;
785 return S_OK;
788 static inline IDirectXFileDataReferenceImpl *impl_from_IDirectXFileDataReference(IDirectXFileDataReference *iface)
790 return CONTAINING_RECORD(iface, IDirectXFileDataReferenceImpl, IDirectXFileDataReference_iface);
793 /*** IUnknown methods ***/
794 static HRESULT WINAPI IDirectXFileDataReferenceImpl_QueryInterface(IDirectXFileDataReference* iface, REFIID riid, void** ppvObject)
796 IDirectXFileDataReferenceImpl *This = impl_from_IDirectXFileDataReference(iface);
798 TRACE("(%p/%p)->(%s,%p)\n", iface, This, debugstr_guid(riid), ppvObject);
800 if (IsEqualGUID(riid, &IID_IUnknown)
801 || IsEqualGUID(riid, &IID_IDirectXFileObject)
802 || IsEqualGUID(riid, &IID_IDirectXFileDataReference))
804 IUnknown_AddRef(iface);
805 *ppvObject = &This->IDirectXFileDataReference_iface;
806 return S_OK;
809 /* Do not print an error for interfaces that can be queried to retrieve the type of the object */
810 if (!IsEqualGUID(riid, &IID_IDirectXFileData)
811 && !IsEqualGUID(riid, &IID_IDirectXFileBinary))
812 ERR("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppvObject);
814 return E_NOINTERFACE;
817 static ULONG WINAPI IDirectXFileDataReferenceImpl_AddRef(IDirectXFileDataReference* iface)
819 IDirectXFileDataReferenceImpl *This = impl_from_IDirectXFileDataReference(iface);
820 ULONG ref = InterlockedIncrement(&This->ref);
822 TRACE("(%p/%p)->(): new ref %d\n", iface, This, ref);
824 return ref;
827 static ULONG WINAPI IDirectXFileDataReferenceImpl_Release(IDirectXFileDataReference* iface)
829 IDirectXFileDataReferenceImpl *This = impl_from_IDirectXFileDataReference(iface);
830 ULONG ref = InterlockedDecrement(&This->ref);
832 TRACE("(%p/%p)->(): new ref %d\n", iface, This, ref);
834 if (!ref)
835 HeapFree(GetProcessHeap(), 0, This);
837 return ref;
840 /*** IDirectXFileObject methods ***/
841 static HRESULT WINAPI IDirectXFileDataReferenceImpl_GetName(IDirectXFileDataReference* iface, LPSTR pstrNameBuf, LPDWORD pdwBufLen)
843 IDirectXFileDataReferenceImpl *This = impl_from_IDirectXFileDataReference(iface);
844 DWORD len;
846 TRACE("(%p/%p)->(%p,%p)\n", This, iface, pstrNameBuf, pdwBufLen);
848 if (!pdwBufLen)
849 return DXFILEERR_BADVALUE;
851 len = strlen(This->ptarget->name);
852 if (len)
853 len++;
855 if (pstrNameBuf) {
856 if (*pdwBufLen < len)
857 return DXFILEERR_BADVALUE;
858 CopyMemory(pstrNameBuf, This->ptarget->name, len);
859 /* Even if we return a size of 0, an empty string with a null byte must be returned */
860 if (*pdwBufLen && !len)
861 pstrNameBuf[0] = 0;
863 *pdwBufLen = len;
865 return DXFILE_OK;
868 static HRESULT WINAPI IDirectXFileDataReferenceImpl_GetId(IDirectXFileDataReference* iface, LPGUID pGuid)
870 IDirectXFileDataReferenceImpl *This = impl_from_IDirectXFileDataReference(iface);
872 TRACE("(%p/%p)->(%p)\n", This, iface, pGuid);
874 if (!pGuid)
875 return DXFILEERR_BADVALUE;
877 *pGuid = This->ptarget->class_id;
879 return DXFILE_OK;
882 /*** IDirectXFileDataReference ***/
883 static HRESULT WINAPI IDirectXFileDataReferenceImpl_Resolve(IDirectXFileDataReference* iface, LPDIRECTXFILEDATA* ppDataObj)
885 IDirectXFileDataReferenceImpl *This = impl_from_IDirectXFileDataReference(iface);
886 IDirectXFileDataImpl *object;
887 HRESULT hr;
889 TRACE("(%p/%p)->(%p)\n", This, iface, ppDataObj);
891 if (!ppDataObj)
892 return DXFILEERR_BADVALUE;
894 hr = IDirectXFileDataImpl_Create(&object);
895 if (FAILED(hr))
896 return hr;
898 object->pobj = This->ptarget;
899 object->cur_enum_object = 0;
900 object->level = 0;
901 object->from_ref = TRUE;
903 *ppDataObj = &object->IDirectXFileData_iface;
905 return DXFILE_OK;
908 static const IDirectXFileDataReferenceVtbl IDirectXFileDataReference_Vtbl =
910 IDirectXFileDataReferenceImpl_QueryInterface,
911 IDirectXFileDataReferenceImpl_AddRef,
912 IDirectXFileDataReferenceImpl_Release,
913 IDirectXFileDataReferenceImpl_GetName,
914 IDirectXFileDataReferenceImpl_GetId,
915 IDirectXFileDataReferenceImpl_Resolve
918 static HRESULT IDirectXFileEnumObjectImpl_Create(IDirectXFileEnumObjectImpl** ppObj)
920 IDirectXFileEnumObjectImpl* object;
922 TRACE("(%p)\n", ppObj);
924 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectXFileEnumObjectImpl));
925 if (!object)
926 return DXFILEERR_BADALLOC;
928 object->IDirectXFileEnumObject_iface.lpVtbl = &IDirectXFileEnumObject_Vtbl;
929 object->ref = 1;
931 *ppObj = object;
933 return S_OK;
936 static inline IDirectXFileEnumObjectImpl *impl_from_IDirectXFileEnumObject(IDirectXFileEnumObject *iface)
938 return CONTAINING_RECORD(iface, IDirectXFileEnumObjectImpl, IDirectXFileEnumObject_iface);
941 /*** IUnknown methods ***/
942 static HRESULT WINAPI IDirectXFileEnumObjectImpl_QueryInterface(IDirectXFileEnumObject* iface, REFIID riid, void** ppvObject)
944 IDirectXFileEnumObjectImpl *This = impl_from_IDirectXFileEnumObject(iface);
946 TRACE("(%p/%p)->(%s,%p)\n", iface, This, debugstr_guid(riid), ppvObject);
948 if (IsEqualGUID(riid, &IID_IUnknown)
949 || IsEqualGUID(riid, &IID_IDirectXFileEnumObject))
951 IUnknown_AddRef(iface);
952 *ppvObject = &This->IDirectXFileEnumObject_iface;
953 return S_OK;
956 ERR("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppvObject);
957 return E_NOINTERFACE;
960 static ULONG WINAPI IDirectXFileEnumObjectImpl_AddRef(IDirectXFileEnumObject* iface)
962 IDirectXFileEnumObjectImpl *This = impl_from_IDirectXFileEnumObject(iface);
963 ULONG ref = InterlockedIncrement(&This->ref);
965 TRACE("(%p/%p)->(): new ref %d\n", iface, This, ref);
967 return ref;
970 static ULONG WINAPI IDirectXFileEnumObjectImpl_Release(IDirectXFileEnumObject* iface)
972 IDirectXFileEnumObjectImpl *This = impl_from_IDirectXFileEnumObject(iface);
973 ULONG ref = InterlockedDecrement(&This->ref);
975 TRACE("(%p/%p)->(): new ref %d\n", iface, This, ref);
977 if (!ref)
979 ULONG i;
980 for (i = 0; i < This->nb_xobjects; i++)
981 IDirectXFileData_Release(This->pRefObjects[i]);
982 if (This->mapped_memory)
983 UnmapViewOfFile(This->mapped_memory);
984 HeapFree(GetProcessHeap(), 0, This->decomp_buffer);
985 HeapFree(GetProcessHeap(), 0, This);
988 return ref;
991 /*** IDirectXFileEnumObject methods ***/
992 static HRESULT WINAPI IDirectXFileEnumObjectImpl_GetNextDataObject(IDirectXFileEnumObject* iface, LPDIRECTXFILEDATA* ppDataObj)
994 IDirectXFileEnumObjectImpl *This = impl_from_IDirectXFileEnumObject(iface);
995 IDirectXFileDataImpl* object;
996 HRESULT hr;
998 if (!ppDataObj)
999 return E_POINTER;
1001 *ppDataObj = NULL;
1003 TRACE("(%p/%p)->(%p)\n", This, iface, ppDataObj);
1005 if (This->nb_xobjects >= MAX_OBJECTS)
1007 ERR("Too many objects\n");
1008 return DXFILEERR_NOMOREOBJECTS;
1011 /* Check if there are templates defined before the object */
1012 if (!parse_templates(&This->buf, TRUE))
1013 return DXFILEERR_PARSEERROR;
1015 if (!This->buf.rem_bytes)
1016 return DXFILEERR_NOMOREOBJECTS;
1018 hr = IDirectXFileDataImpl_Create(&object);
1019 if (FAILED(hr))
1020 return hr;
1022 object->pobj = HeapAlloc(GetProcessHeap(), 0, sizeof(xobject)*MAX_SUBOBJECTS);
1023 if (!object->pobj)
1025 hr = DXFILEERR_BADALLOC;
1026 goto error;
1029 object->pstrings = HeapAlloc(GetProcessHeap(), 0, MAX_STRINGS_BUFFER);
1030 if (!object->pstrings)
1032 hr = DXFILEERR_BADALLOC;
1033 goto error;
1036 object->cur_enum_object = 0;
1037 object->level = 0;
1038 object->from_ref = FALSE;
1040 This->buf.pxo_globals = This->xobjects;
1041 This->buf.nb_pxo_globals = This->nb_xobjects;
1042 This->buf.level = 0;
1043 This->buf.pdata = NULL;
1044 This->buf.capacity = 0;
1045 This->buf.cur_pos_data = 0;
1046 This->buf.cur_pstrings = This->buf.pstrings = object->pstrings;
1047 This->buf.pxo = This->xobjects[This->nb_xobjects] = This->buf.pxo_tab = object->pobj;
1048 This->buf.pxo->pdata = NULL;
1049 This->buf.pxo->nb_subobjects = 1;
1051 if (!parse_object(&This->buf))
1053 WARN("Object is not correct\n");
1054 hr = DXFILEERR_PARSEERROR;
1055 goto error;
1058 *ppDataObj = &object->IDirectXFileData_iface;
1060 /* Get a reference to created object */
1061 This->pRefObjects[This->nb_xobjects] = &object->IDirectXFileData_iface;
1062 IDirectXFileData_AddRef(This->pRefObjects[This->nb_xobjects]);
1064 This->nb_xobjects++;
1066 return DXFILE_OK;
1068 error:
1070 IDirectXFileData_Release(&object->IDirectXFileData_iface);
1072 return hr;
1075 static HRESULT WINAPI IDirectXFileEnumObjectImpl_GetDataObjectById(IDirectXFileEnumObject* iface, REFGUID rguid, LPDIRECTXFILEDATA* ppDataObj)
1077 IDirectXFileEnumObjectImpl *This = impl_from_IDirectXFileEnumObject(iface);
1079 FIXME("(%p/%p)->(%p,%p) stub!\n", This, iface, rguid, ppDataObj);
1081 return DXFILEERR_BADVALUE;
1084 static HRESULT WINAPI IDirectXFileEnumObjectImpl_GetDataObjectByName(IDirectXFileEnumObject* iface, LPCSTR szName, LPDIRECTXFILEDATA* ppDataObj)
1086 IDirectXFileEnumObjectImpl *This = impl_from_IDirectXFileEnumObject(iface);
1088 FIXME("(%p/%p)->(%s,%p) stub!\n", This, iface, szName, ppDataObj);
1090 return DXFILEERR_BADVALUE;
1093 static const IDirectXFileEnumObjectVtbl IDirectXFileEnumObject_Vtbl =
1095 IDirectXFileEnumObjectImpl_QueryInterface,
1096 IDirectXFileEnumObjectImpl_AddRef,
1097 IDirectXFileEnumObjectImpl_Release,
1098 IDirectXFileEnumObjectImpl_GetNextDataObject,
1099 IDirectXFileEnumObjectImpl_GetDataObjectById,
1100 IDirectXFileEnumObjectImpl_GetDataObjectByName
1103 static HRESULT IDirectXFileSaveObjectImpl_Create(IDirectXFileSaveObjectImpl** ppObj)
1105 IDirectXFileSaveObjectImpl* object;
1107 TRACE("(%p)\n", ppObj);
1109 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectXFileSaveObjectImpl));
1110 if (!object)
1111 return DXFILEERR_BADALLOC;
1113 object->IDirectXFileSaveObject_iface.lpVtbl = &IDirectXFileSaveObject_Vtbl;
1114 object->ref = 1;
1116 *ppObj = object;
1118 return S_OK;
1121 static inline IDirectXFileSaveObjectImpl *impl_from_IDirectXFileSaveObject(IDirectXFileSaveObject *iface)
1123 return CONTAINING_RECORD(iface, IDirectXFileSaveObjectImpl, IDirectXFileSaveObject_iface);
1126 /*** IUnknown methods ***/
1127 static HRESULT WINAPI IDirectXFileSaveObjectImpl_QueryInterface(IDirectXFileSaveObject* iface, REFIID riid, void** ppvObject)
1129 IDirectXFileSaveObjectImpl *This = impl_from_IDirectXFileSaveObject(iface);
1131 TRACE("(%p/%p)->(%s,%p)\n", iface, This, debugstr_guid(riid), ppvObject);
1133 if (IsEqualGUID(riid, &IID_IUnknown)
1134 || IsEqualGUID(riid, &IID_IDirectXFileSaveObject))
1136 IUnknown_AddRef(iface);
1137 *ppvObject = &This->IDirectXFileSaveObject_iface;
1138 return S_OK;
1141 ERR("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppvObject);
1142 return E_NOINTERFACE;
1145 static ULONG WINAPI IDirectXFileSaveObjectImpl_AddRef(IDirectXFileSaveObject* iface)
1147 IDirectXFileSaveObjectImpl *This = impl_from_IDirectXFileSaveObject(iface);
1148 ULONG ref = InterlockedIncrement(&This->ref);
1150 TRACE("(%p/%p)->(): new ref %d\n", iface, This, ref);
1152 return ref;
1155 static ULONG WINAPI IDirectXFileSaveObjectImpl_Release(IDirectXFileSaveObject* iface)
1157 IDirectXFileSaveObjectImpl *This = impl_from_IDirectXFileSaveObject(iface);
1158 ULONG ref = InterlockedDecrement(&This->ref);
1160 TRACE("(%p/%p)->(): new ref %d\n", iface, This, ref);
1162 if (!ref)
1163 HeapFree(GetProcessHeap(), 0, This);
1165 return ref;
1168 static HRESULT WINAPI IDirectXFileSaveObjectImpl_SaveTemplates(IDirectXFileSaveObject* iface, DWORD cTemplates, const GUID** ppguidTemplates)
1170 IDirectXFileSaveObjectImpl *This = impl_from_IDirectXFileSaveObject(iface);
1172 FIXME("(%p/%p)->(%d,%p) stub!\n", This, iface, cTemplates, ppguidTemplates);
1174 return DXFILE_OK;
1177 static HRESULT WINAPI IDirectXFileSaveObjectImpl_CreateDataObject(IDirectXFileSaveObject* iface, REFGUID rguidTemplate, LPCSTR szName, const GUID* pguid, DWORD cbSize, LPVOID pvData, LPDIRECTXFILEDATA* ppDataObj)
1179 IDirectXFileSaveObjectImpl *This = impl_from_IDirectXFileSaveObject(iface);
1181 FIXME("(%p/%p)->(%p,%s,%p,%d,%p,%p) stub!\n", This, iface, rguidTemplate, szName, pguid, cbSize, pvData, ppDataObj);
1183 return DXFILEERR_BADVALUE;
1186 static HRESULT WINAPI IDirectXFileSaveObjectImpl_SaveData(IDirectXFileSaveObject* iface, LPDIRECTXFILEDATA ppDataObj)
1188 IDirectXFileSaveObjectImpl *This = impl_from_IDirectXFileSaveObject(iface);
1190 FIXME("(%p/%p)->(%p) stub!\n", This, iface, ppDataObj);
1192 return DXFILEERR_BADVALUE;
1195 static const IDirectXFileSaveObjectVtbl IDirectXFileSaveObject_Vtbl =
1197 IDirectXFileSaveObjectImpl_QueryInterface,
1198 IDirectXFileSaveObjectImpl_AddRef,
1199 IDirectXFileSaveObjectImpl_Release,
1200 IDirectXFileSaveObjectImpl_SaveTemplates,
1201 IDirectXFileSaveObjectImpl_CreateDataObject,
1202 IDirectXFileSaveObjectImpl_SaveData