Release 1.1.37.
[wine/gsoc-2012-control.git] / dlls / d3dxof / d3dxof.c
blobb1df5585fc5609fed5f1d1341950f09015db3feb
1 /*
2 * Implementation of DirectX File Interfaces
4 * Copyright 2004, 2008 Christian Costa
6 * This file contains the (internal) driver registration functions,
7 * driver enumeration APIs and DirectDraw creation functions.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "config.h"
25 #include "wine/debug.h"
27 #define COBJMACROS
29 #include "winbase.h"
30 #include "wingdi.h"
32 #include "d3dxof_private.h"
33 #include "dxfile.h"
35 #include <stdio.h>
37 WINE_DEFAULT_DEBUG_CHANNEL(d3dxof);
39 #define MAKEFOUR(a,b,c,d) ((DWORD)a + ((DWORD)b << 8) + ((DWORD)c << 16) + ((DWORD)d << 24))
40 #define XOFFILE_FORMAT_MAGIC MAKEFOUR('x','o','f',' ')
41 #define XOFFILE_FORMAT_VERSION_302 MAKEFOUR('0','3','0','2')
42 #define XOFFILE_FORMAT_VERSION_303 MAKEFOUR('0','3','0','3')
43 #define XOFFILE_FORMAT_BINARY MAKEFOUR('b','i','n',' ')
44 #define XOFFILE_FORMAT_TEXT MAKEFOUR('t','x','t',' ')
45 #define XOFFILE_FORMAT_COMPRESSED MAKEFOUR('c','m','p',' ')
46 #define XOFFILE_FORMAT_FLOAT_BITS_32 MAKEFOUR('0','0','3','2')
47 #define XOFFILE_FORMAT_FLOAT_BITS_64 MAKEFOUR('0','0','6','4')
49 static const struct IDirectXFileVtbl IDirectXFile_Vtbl;
50 static const struct IDirectXFileBinaryVtbl IDirectXFileBinary_Vtbl;
51 static const struct IDirectXFileDataVtbl IDirectXFileData_Vtbl;
52 static const struct IDirectXFileDataReferenceVtbl IDirectXFileDataReference_Vtbl;
53 static const struct IDirectXFileEnumObjectVtbl IDirectXFileEnumObject_Vtbl;
54 static const struct IDirectXFileObjectVtbl IDirectXFileObject_Vtbl;
55 static const struct IDirectXFileSaveObjectVtbl IDirectXFileSaveObject_Vtbl;
57 static HRESULT IDirectXFileDataReferenceImpl_Create(IDirectXFileDataReferenceImpl** ppObj);
58 static HRESULT IDirectXFileEnumObjectImpl_Create(IDirectXFileEnumObjectImpl** ppObj);
59 static HRESULT IDirectXFileSaveObjectImpl_Create(IDirectXFileSaveObjectImpl** ppObj);
61 HRESULT IDirectXFileImpl_Create(IUnknown* pUnkOuter, LPVOID* ppObj)
63 IDirectXFileImpl* object;
65 TRACE("(%p,%p)\n", pUnkOuter, ppObj);
67 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectXFileImpl));
68 if (!object)
70 ERR("Out of memory\n");
71 return DXFILEERR_BADALLOC;
74 object->lpVtbl = &IDirectXFile_Vtbl;
75 object->ref = 1;
77 *ppObj = object;
79 return S_OK;
82 /*** IUnknown methods ***/
83 static HRESULT WINAPI IDirectXFileImpl_QueryInterface(IDirectXFile* iface, REFIID riid, void** ppvObject)
85 IDirectXFileImpl *This = (IDirectXFileImpl *)iface;
87 TRACE("(%p/%p)->(%s,%p)\n", iface, This, debugstr_guid(riid), ppvObject);
89 if (IsEqualGUID(riid, &IID_IUnknown)
90 || IsEqualGUID(riid, &IID_IDirectXFile))
92 IClassFactory_AddRef(iface);
93 *ppvObject = This;
94 return S_OK;
97 ERR("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppvObject);
98 return E_NOINTERFACE;
101 static ULONG WINAPI IDirectXFileImpl_AddRef(IDirectXFile* iface)
103 IDirectXFileImpl *This = (IDirectXFileImpl *)iface;
104 ULONG ref = InterlockedIncrement(&This->ref);
106 TRACE("(%p/%p): AddRef from %d\n", iface, This, ref - 1);
108 return ref;
111 static ULONG WINAPI IDirectXFileImpl_Release(IDirectXFile* iface)
113 IDirectXFileImpl *This = (IDirectXFileImpl *)iface;
114 ULONG ref = InterlockedDecrement(&This->ref);
116 TRACE("(%p/%p): ReleaseRef to %d\n", iface, This, ref);
118 if (!ref)
119 HeapFree(GetProcessHeap(), 0, This);
121 return ref;
124 /*** IDirectXFile methods ***/
125 static HRESULT WINAPI IDirectXFileImpl_CreateEnumObject(IDirectXFile* iface, LPVOID pvSource, DXFILELOADOPTIONS dwLoadOptions, LPDIRECTXFILEENUMOBJECT* ppEnumObj)
127 IDirectXFileImpl *This = (IDirectXFileImpl *)iface;
128 IDirectXFileEnumObjectImpl* object;
129 HRESULT hr;
130 DWORD* header;
131 HANDLE hFile = INVALID_HANDLE_VALUE;
132 HANDLE file_mapping = 0;
133 LPBYTE buffer = NULL;
134 HGLOBAL resource_data = 0;
135 LPBYTE file_buffer;
136 DWORD file_size;
138 LPDXFILELOADMEMORY lpdxflm = NULL;
140 TRACE("(%p/%p)->(%p,%x,%p)\n", This, iface, pvSource, dwLoadOptions, ppEnumObj);
142 if (!ppEnumObj)
143 return DXFILEERR_BADVALUE;
145 /* Only lowest 4 bits are relevant in DXFILELOADOPTIONS */
146 dwLoadOptions &= 0xF;
148 if (dwLoadOptions == DXFILELOAD_FROMFILE)
150 TRACE("Open source file '%s'\n", (char*)pvSource);
152 hFile = CreateFileA(pvSource, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
153 if (hFile == INVALID_HANDLE_VALUE)
155 TRACE("File '%s' not found\n", (char*)pvSource);
156 return DXFILEERR_FILENOTFOUND;
159 file_size = GetFileSize(hFile, NULL);
161 file_mapping = CreateFileMappingA(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
162 if (!file_mapping)
164 hr = DXFILEERR_BADFILETYPE;
165 goto error;
168 buffer = MapViewOfFile(file_mapping, FILE_MAP_READ, 0, 0, 0);
169 if (!buffer)
171 hr = DXFILEERR_BADFILETYPE;
172 goto error;
174 file_buffer = buffer;
176 else if (dwLoadOptions == DXFILELOAD_FROMRESOURCE)
178 HRSRC resource_info;
179 LPDXFILELOADRESOURCE lpdxflr = pvSource;
181 TRACE("Source in resource (module = %p, name = %s, type = %s\n", lpdxflr->hModule, debugstr_a(lpdxflr->lpName), debugstr_a(lpdxflr->lpType));
183 resource_info = FindResourceA(lpdxflr->hModule, lpdxflr->lpName, lpdxflr->lpType);
184 if (!resource_info)
186 hr = DXFILEERR_RESOURCENOTFOUND;
187 goto error;
190 file_size = SizeofResource(lpdxflr->hModule, resource_info);
192 resource_data = LoadResource(lpdxflr->hModule, resource_info);
193 if (!resource_data)
195 hr = DXFILEERR_BADRESOURCE;
196 goto error;
199 file_buffer = LockResource(resource_data);
200 if (!file_buffer)
202 hr = DXFILEERR_BADRESOURCE;
203 goto error;
206 else if (dwLoadOptions == DXFILELOAD_FROMMEMORY)
208 lpdxflm = pvSource;
210 TRACE("Source in memory at %p with size %d\n", lpdxflm->lpMemory, lpdxflm->dSize);
212 file_buffer = lpdxflm->lpMemory;
213 file_size = lpdxflm->dSize;
215 else
217 FIXME("Source type %d is not handled yet\n", dwLoadOptions);
218 hr = DXFILEERR_NOTDONEYET;
219 goto error;
222 header = (DWORD*)file_buffer;
224 if (TRACE_ON(d3dxof))
226 char string[17];
227 memcpy(string, header, 16);
228 string[16] = 0;
229 TRACE("header = '%s'\n", string);
232 if (file_size < 16)
234 hr = DXFILEERR_BADFILETYPE;
235 goto error;
238 if (header[0] != XOFFILE_FORMAT_MAGIC)
240 hr = DXFILEERR_BADFILETYPE;
241 goto error;
244 if ((header[1] != XOFFILE_FORMAT_VERSION_302) && (header[1] != XOFFILE_FORMAT_VERSION_303))
246 hr = DXFILEERR_BADFILEVERSION;
247 goto error;
250 if ((header[2] != XOFFILE_FORMAT_BINARY) && (header[2] != XOFFILE_FORMAT_TEXT) && (header[2] != XOFFILE_FORMAT_COMPRESSED))
252 hr = DXFILEERR_BADFILETYPE;
253 goto error;
256 if (header[2] == XOFFILE_FORMAT_COMPRESSED)
258 FIXME("Compressed formats not supported yet\n");
259 hr = DXFILEERR_BADVALUE;
260 goto error;
263 if ((header[3] != XOFFILE_FORMAT_FLOAT_BITS_32) && (header[3] != XOFFILE_FORMAT_FLOAT_BITS_64))
265 hr = DXFILEERR_BADFILEFLOATSIZE;
266 goto error;
269 TRACE("Header is correct\n");
271 hr = IDirectXFileEnumObjectImpl_Create(&object);
272 if (FAILED(hr))
273 goto error;
275 object->source = dwLoadOptions;
276 object->hFile = hFile;
277 object->file_mapping = file_mapping;
278 object->buffer = buffer;
279 object->pDirectXFile = This;
280 object->buf.pdxf = This;
281 object->buf.txt = (header[2] == XOFFILE_FORMAT_TEXT);
282 object->buf.token_present = FALSE;
284 TRACE("File size is %d bytes\n", file_size);
286 /* Go to data after header */
287 object->buf.buffer = file_buffer + 16;
288 object->buf.rem_bytes = file_size - 16;
290 *ppEnumObj = (LPDIRECTXFILEENUMOBJECT)object;
292 while (object->buf.rem_bytes && is_template_available(&object->buf))
294 if (!parse_template(&object->buf))
296 TRACE("Template is not correct\n");
297 hr = DXFILEERR_BADVALUE;
298 goto error;
300 else
302 TRACE("Template successfully parsed:\n");
303 if (TRACE_ON(d3dxof))
304 dump_template(This->xtemplates, &This->xtemplates[This->nb_xtemplates - 1]);
308 if (TRACE_ON(d3dxof))
310 int i;
311 TRACE("Registered templates (%d):\n", This->nb_xtemplates);
312 for (i = 0; i < This->nb_xtemplates; i++)
313 DPRINTF("%s - %s\n", This->xtemplates[i].name, debugstr_guid(&This->xtemplates[i].class_id));
316 return DXFILE_OK;
318 error:
319 if (buffer)
320 UnmapViewOfFile(buffer);
321 if (file_mapping)
322 CloseHandle(file_mapping);
323 if (hFile != INVALID_HANDLE_VALUE)
324 CloseHandle(hFile);
325 if (resource_data)
326 FreeResource(resource_data);
327 *ppEnumObj = NULL;
329 return hr;
332 static HRESULT WINAPI IDirectXFileImpl_CreateSaveObject(IDirectXFile* iface, LPCSTR szFileName, DXFILEFORMAT dwFileFormat, LPDIRECTXFILESAVEOBJECT* ppSaveObj)
334 IDirectXFileImpl *This = (IDirectXFileImpl *)iface;
336 FIXME("(%p/%p)->(%s,%x,%p) partial stub!\n", This, iface, szFileName, dwFileFormat, ppSaveObj);
338 if (!szFileName || !ppSaveObj)
339 return E_POINTER;
341 return IDirectXFileSaveObjectImpl_Create((IDirectXFileSaveObjectImpl**)ppSaveObj);
344 static HRESULT WINAPI IDirectXFileImpl_RegisterTemplates(IDirectXFile* iface, LPVOID pvData, DWORD cbSize)
346 IDirectXFileImpl *This = (IDirectXFileImpl *)iface;
347 DWORD token_header;
348 parse_buffer buf;
350 buf.buffer = pvData;
351 buf.rem_bytes = cbSize;
352 buf.txt = FALSE;
353 buf.token_present = FALSE;
354 buf.pdxf = This;
356 TRACE("(%p/%p)->(%p,%d)\n", This, iface, pvData, cbSize);
358 if (!pvData)
359 return DXFILEERR_BADVALUE;
361 if (cbSize < 16)
362 return DXFILEERR_BADFILETYPE;
364 if (TRACE_ON(d3dxof))
366 char string[17];
367 memcpy(string, pvData, 16);
368 string[16] = 0;
369 TRACE("header = '%s'\n", string);
372 read_bytes(&buf, &token_header, 4);
374 if (token_header != XOFFILE_FORMAT_MAGIC)
375 return DXFILEERR_BADFILETYPE;
377 read_bytes(&buf, &token_header, 4);
379 if ((token_header != XOFFILE_FORMAT_VERSION_302) && (token_header != XOFFILE_FORMAT_VERSION_303))
380 return DXFILEERR_BADFILEVERSION;
382 read_bytes(&buf, &token_header, 4);
384 if ((token_header != XOFFILE_FORMAT_BINARY) && (token_header != XOFFILE_FORMAT_TEXT) && (token_header != XOFFILE_FORMAT_COMPRESSED))
385 return DXFILEERR_BADFILETYPE;
387 if (token_header == XOFFILE_FORMAT_TEXT)
389 buf.txt = TRUE;
392 if (token_header == XOFFILE_FORMAT_COMPRESSED)
394 FIXME("Compressed formats not supported yet\n");
395 return DXFILEERR_BADVALUE;
398 read_bytes(&buf, &token_header, 4);
400 if ((token_header != XOFFILE_FORMAT_FLOAT_BITS_32) && (token_header != XOFFILE_FORMAT_FLOAT_BITS_64))
401 return DXFILEERR_BADFILEFLOATSIZE;
403 TRACE("Header is correct\n");
405 while (buf.rem_bytes)
407 if (!parse_template(&buf))
409 TRACE("Template is not correct\n");
410 return DXFILEERR_BADVALUE;
412 else
414 TRACE("Template successfully parsed:\n");
415 if (TRACE_ON(d3dxof))
416 dump_template(This->xtemplates, &This->xtemplates[This->nb_xtemplates - 1]);
420 if (TRACE_ON(d3dxof))
422 int i;
423 TRACE("Registered templates (%d):\n", This->nb_xtemplates);
424 for (i = 0; i < This->nb_xtemplates; i++)
425 DPRINTF("%s - %s\n", This->xtemplates[i].name, debugstr_guid(&This->xtemplates[i].class_id));
428 return DXFILE_OK;
431 static const IDirectXFileVtbl IDirectXFile_Vtbl =
433 IDirectXFileImpl_QueryInterface,
434 IDirectXFileImpl_AddRef,
435 IDirectXFileImpl_Release,
436 IDirectXFileImpl_CreateEnumObject,
437 IDirectXFileImpl_CreateSaveObject,
438 IDirectXFileImpl_RegisterTemplates
441 static HRESULT IDirectXFileBinaryImpl_Create(IDirectXFileBinaryImpl** ppObj)
443 IDirectXFileBinaryImpl* object;
445 TRACE("(%p)\n", ppObj);
447 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectXFileBinaryImpl));
448 if (!object)
450 ERR("Out of memory\n");
451 return DXFILEERR_BADALLOC;
454 object->lpVtbl = &IDirectXFileBinary_Vtbl;
455 object->ref = 1;
457 *ppObj = object;
459 return DXFILE_OK;
462 /*** IUnknown methods ***/
463 static HRESULT WINAPI IDirectXFileBinaryImpl_QueryInterface(IDirectXFileBinary* iface, REFIID riid, void** ppvObject)
465 IDirectXFileBinaryImpl *This = (IDirectXFileBinaryImpl *)iface;
467 TRACE("(%p/%p)->(%s,%p)\n", iface, This, debugstr_guid(riid), ppvObject);
469 if (IsEqualGUID(riid, &IID_IUnknown)
470 || IsEqualGUID(riid, &IID_IDirectXFileObject)
471 || IsEqualGUID(riid, &IID_IDirectXFileBinary))
473 IClassFactory_AddRef(iface);
474 *ppvObject = This;
475 return S_OK;
478 /* Do not print an error for interfaces that can be queried to retrieve the type of the object */
479 if (!IsEqualGUID(riid, &IID_IDirectXFileData)
480 && !IsEqualGUID(riid, &IID_IDirectXFileDataReference))
481 ERR("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppvObject);
483 return E_NOINTERFACE;
486 static ULONG WINAPI IDirectXFileBinaryImpl_AddRef(IDirectXFileBinary* iface)
488 IDirectXFileBinaryImpl *This = (IDirectXFileBinaryImpl *)iface;
489 ULONG ref = InterlockedIncrement(&This->ref);
491 TRACE("(%p/%p): AddRef from %d\n", iface, This, ref - 1);
493 return ref;
496 static ULONG WINAPI IDirectXFileBinaryImpl_Release(IDirectXFileBinary* iface)
498 IDirectXFileBinaryImpl *This = (IDirectXFileBinaryImpl *)iface;
499 ULONG ref = InterlockedDecrement(&This->ref);
501 TRACE("(%p/%p): ReleaseRef to %d\n", iface, This, ref);
503 if (!ref)
504 HeapFree(GetProcessHeap(), 0, This);
506 return ref;
509 /*** IDirectXFileObject methods ***/
510 static HRESULT WINAPI IDirectXFileBinaryImpl_GetName(IDirectXFileBinary* iface, LPSTR pstrNameBuf, LPDWORD pdwBufLen)
513 IDirectXFileBinaryImpl *This = (IDirectXFileBinaryImpl *)iface;
515 FIXME("(%p/%p)->(%p,%p) stub!\n", This, iface, pstrNameBuf, pdwBufLen);
517 return DXFILEERR_BADVALUE;
520 static HRESULT WINAPI IDirectXFileBinaryImpl_GetId(IDirectXFileBinary* iface, LPGUID pGuid)
522 IDirectXFileBinaryImpl *This = (IDirectXFileBinaryImpl *)iface;
524 FIXME("(%p/%p)->(%p) stub!\n", This, iface, pGuid);
526 return DXFILEERR_BADVALUE;
529 /*** IDirectXFileBinary methods ***/
530 static HRESULT WINAPI IDirectXFileBinaryImpl_GetSize(IDirectXFileBinary* iface, DWORD* pcbSize)
532 IDirectXFileBinaryImpl *This = (IDirectXFileBinaryImpl *)iface;
534 FIXME("(%p/%p)->(%p) stub!\n", This, iface, pcbSize);
536 return DXFILEERR_BADVALUE;
539 static HRESULT WINAPI IDirectXFileBinaryImpl_GetMimeType(IDirectXFileBinary* iface, LPCSTR* pszMimeType)
541 IDirectXFileBinaryImpl *This = (IDirectXFileBinaryImpl *)iface;
543 FIXME("(%p/%p)->(%p) stub!\n", This, iface, pszMimeType);
545 return DXFILEERR_BADVALUE;
548 static HRESULT WINAPI IDirectXFileBinaryImpl_Read(IDirectXFileBinary* iface, LPVOID pvData, DWORD cbSize, LPDWORD pcbRead)
550 IDirectXFileBinaryImpl *This = (IDirectXFileBinaryImpl *)iface;
552 FIXME("(%p/%p)->(%p, %d, %p) stub!\n", This, iface, pvData, cbSize, pcbRead);
554 return DXFILEERR_BADVALUE;
557 static const IDirectXFileBinaryVtbl IDirectXFileBinary_Vtbl =
559 IDirectXFileBinaryImpl_QueryInterface,
560 IDirectXFileBinaryImpl_AddRef,
561 IDirectXFileBinaryImpl_Release,
562 IDirectXFileBinaryImpl_GetName,
563 IDirectXFileBinaryImpl_GetId,
564 IDirectXFileBinaryImpl_GetSize,
565 IDirectXFileBinaryImpl_GetMimeType,
566 IDirectXFileBinaryImpl_Read
569 static HRESULT IDirectXFileDataImpl_Create(IDirectXFileDataImpl** ppObj)
571 IDirectXFileDataImpl* object;
573 TRACE("(%p)\n", ppObj);
575 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectXFileDataImpl));
576 if (!object)
578 ERR("Out of memory\n");
579 return DXFILEERR_BADALLOC;
582 object->lpVtbl = &IDirectXFileData_Vtbl;
583 object->ref = 1;
585 *ppObj = object;
587 return S_OK;
590 /*** IUnknown methods ***/
591 static HRESULT WINAPI IDirectXFileDataImpl_QueryInterface(IDirectXFileData* iface, REFIID riid, void** ppvObject)
593 IDirectXFileDataImpl *This = (IDirectXFileDataImpl *)iface;
595 TRACE("(%p/%p)->(%s,%p)\n", iface, This, debugstr_guid(riid), ppvObject);
597 if (IsEqualGUID(riid, &IID_IUnknown)
598 || IsEqualGUID(riid, &IID_IDirectXFileObject)
599 || IsEqualGUID(riid, &IID_IDirectXFileData))
601 IClassFactory_AddRef(iface);
602 *ppvObject = This;
603 return S_OK;
606 /* Do not print an error for interfaces that can be queried to retrieve the type of the object */
607 if (!IsEqualGUID(riid, &IID_IDirectXFileBinary)
608 && !IsEqualGUID(riid, &IID_IDirectXFileDataReference))
609 ERR("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppvObject);
611 return E_NOINTERFACE;
614 static ULONG WINAPI IDirectXFileDataImpl_AddRef(IDirectXFileData* iface)
616 IDirectXFileDataImpl *This = (IDirectXFileDataImpl *)iface;
617 ULONG ref = InterlockedIncrement(&This->ref);
619 TRACE("(%p/%p): AddRef from %d\n", iface, This, ref - 1);
621 return ref;
624 static ULONG WINAPI IDirectXFileDataImpl_Release(IDirectXFileData* iface)
626 IDirectXFileDataImpl *This = (IDirectXFileDataImpl *)iface;
627 ULONG ref = InterlockedDecrement(&This->ref);
629 TRACE("(%p/%p): ReleaseRef to %d\n", iface, This, ref);
631 if (!ref)
633 if (!This->level && !This->from_ref)
635 HeapFree(GetProcessHeap(), 0, This->pstrings);
636 HeapFree(GetProcessHeap(), 0, This->pobj->pdata);
637 HeapFree(GetProcessHeap(), 0, This->pobj);
639 HeapFree(GetProcessHeap(), 0, This);
642 return ref;
645 /*** IDirectXFileObject methods ***/
646 static HRESULT WINAPI IDirectXFileDataImpl_GetName(IDirectXFileData* iface, LPSTR pstrNameBuf, LPDWORD pdwBufLen)
649 IDirectXFileDataImpl *This = (IDirectXFileDataImpl *)iface;
651 TRACE("(%p/%p)->(%p,%p)\n", This, iface, pstrNameBuf, pdwBufLen);
653 if (!pstrNameBuf)
654 return DXFILEERR_BADVALUE;
656 strcpy(pstrNameBuf, This->pobj->name);
658 return DXFILE_OK;
661 static HRESULT WINAPI IDirectXFileDataImpl_GetId(IDirectXFileData* iface, LPGUID pGuid)
663 IDirectXFileDataImpl *This = (IDirectXFileDataImpl *)iface;
665 TRACE("(%p/%p)->(%p)\n", This, iface, pGuid);
667 if (!pGuid)
668 return DXFILEERR_BADVALUE;
670 memcpy(pGuid, &This->pobj->class_id, 16);
672 return DXFILE_OK;
675 /*** IDirectXFileData methods ***/
676 static HRESULT WINAPI IDirectXFileDataImpl_GetData(IDirectXFileData* iface, LPCSTR szMember, DWORD* pcbSize, void** ppvData)
678 IDirectXFileDataImpl *This = (IDirectXFileDataImpl *)iface;
680 TRACE("(%p/%p)->(%s,%p,%p)\n", This, iface, szMember, pcbSize, ppvData);
682 if (!pcbSize || !ppvData)
683 return DXFILEERR_BADVALUE;
685 if (szMember)
687 FIXME("Specifying a member is not supported yet!\n");
688 return DXFILEERR_BADVALUE;
691 *pcbSize = This->pobj->size;
692 *ppvData = This->pobj->root->pdata + This->pobj->pos_data;
694 return DXFILE_OK;
697 static HRESULT WINAPI IDirectXFileDataImpl_GetType(IDirectXFileData* iface, const GUID** pguid)
699 IDirectXFileDataImpl *This = (IDirectXFileDataImpl *)iface;
700 static GUID guid;
702 TRACE("(%p/%p)->(%p)\n", This, iface, pguid);
704 if (!pguid)
705 return DXFILEERR_BADVALUE;
707 memcpy(&guid, &This->pobj->type, 16);
708 *pguid = &guid;
710 return DXFILE_OK;
713 static HRESULT WINAPI IDirectXFileDataImpl_GetNextObject(IDirectXFileData* iface, LPDIRECTXFILEOBJECT* ppChildObj)
715 HRESULT hr;
716 IDirectXFileDataImpl *This = (IDirectXFileDataImpl *)iface;
718 TRACE("(%p/%p)->(%p)\n", This, iface, ppChildObj);
720 if (This->cur_enum_object >= This->pobj->nb_childs)
721 return DXFILEERR_NOMOREOBJECTS;
723 if (This->from_ref && (This->level >= 1))
725 /* Only 2 levels can enumerated if the object is obtained from a reference */
726 return DXFILEERR_NOMOREOBJECTS;
729 if (This->pobj->childs[This->cur_enum_object]->binary)
731 IDirectXFileBinaryImpl *object;
733 hr = IDirectXFileBinaryImpl_Create(&object);
734 if (FAILED(hr))
735 return hr;
737 *ppChildObj = (LPDIRECTXFILEOBJECT)object;
739 else if (This->pobj->childs[This->cur_enum_object]->ptarget)
741 IDirectXFileDataReferenceImpl *object;
743 hr = IDirectXFileDataReferenceImpl_Create(&object);
744 if (FAILED(hr))
745 return hr;
747 object->ptarget = This->pobj->childs[This->cur_enum_object++]->ptarget;
749 *ppChildObj = (LPDIRECTXFILEOBJECT)object;
751 else
753 IDirectXFileDataImpl *object;
755 hr = IDirectXFileDataImpl_Create(&object);
756 if (FAILED(hr))
757 return hr;
759 object->pobj = This->pobj->childs[This->cur_enum_object++];
760 object->cur_enum_object = 0;
761 object->from_ref = This->from_ref;
762 object->level = This->level + 1;
764 *ppChildObj = (LPDIRECTXFILEOBJECT)object;
767 return DXFILE_OK;
770 static HRESULT WINAPI IDirectXFileDataImpl_AddDataObject(IDirectXFileData* iface, LPDIRECTXFILEDATA pDataObj)
772 IDirectXFileDataImpl *This = (IDirectXFileDataImpl *)iface;
774 FIXME("(%p/%p)->(%p) stub!\n", This, iface, pDataObj);
776 return DXFILEERR_BADVALUE;
779 static HRESULT WINAPI IDirectXFileDataImpl_AddDataReference(IDirectXFileData* iface, LPCSTR szRef, const GUID* pguidRef)
781 IDirectXFileDataImpl *This = (IDirectXFileDataImpl *)iface;
783 FIXME("(%p/%p)->(%s,%p) stub!\n", This, iface, szRef, pguidRef);
785 return DXFILEERR_BADVALUE;
788 static HRESULT WINAPI IDirectXFileDataImpl_AddBinaryObject(IDirectXFileData* iface, LPCSTR szName, const GUID* pguid, LPCSTR szMimeType, LPVOID pvData, DWORD cbSize)
790 IDirectXFileDataImpl *This = (IDirectXFileDataImpl *)iface;
792 FIXME("(%p/%p)->(%s,%p,%s,%p,%d) stub!\n", This, iface, szName, pguid, szMimeType, pvData, cbSize);
794 return DXFILEERR_BADVALUE;
797 static const IDirectXFileDataVtbl IDirectXFileData_Vtbl =
799 IDirectXFileDataImpl_QueryInterface,
800 IDirectXFileDataImpl_AddRef,
801 IDirectXFileDataImpl_Release,
802 IDirectXFileDataImpl_GetName,
803 IDirectXFileDataImpl_GetId,
804 IDirectXFileDataImpl_GetData,
805 IDirectXFileDataImpl_GetType,
806 IDirectXFileDataImpl_GetNextObject,
807 IDirectXFileDataImpl_AddDataObject,
808 IDirectXFileDataImpl_AddDataReference,
809 IDirectXFileDataImpl_AddBinaryObject
812 static HRESULT IDirectXFileDataReferenceImpl_Create(IDirectXFileDataReferenceImpl** ppObj)
814 IDirectXFileDataReferenceImpl* object;
816 TRACE("(%p)\n", ppObj);
818 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectXFileDataReferenceImpl));
819 if (!object)
821 ERR("Out of memory\n");
822 return DXFILEERR_BADALLOC;
825 object->lpVtbl = &IDirectXFileDataReference_Vtbl;
826 object->ref = 1;
828 *ppObj = object;
830 return S_OK;
833 /*** IUnknown methods ***/
834 static HRESULT WINAPI IDirectXFileDataReferenceImpl_QueryInterface(IDirectXFileDataReference* iface, REFIID riid, void** ppvObject)
836 IDirectXFileDataReferenceImpl *This = (IDirectXFileDataReferenceImpl *)iface;
838 TRACE("(%p/%p)->(%s,%p)\n", iface, This, debugstr_guid(riid), ppvObject);
840 if (IsEqualGUID(riid, &IID_IUnknown)
841 || IsEqualGUID(riid, &IID_IDirectXFileObject)
842 || IsEqualGUID(riid, &IID_IDirectXFileDataReference))
844 IClassFactory_AddRef(iface);
845 *ppvObject = This;
846 return S_OK;
849 /* Do not print an error for interfaces that can be queried to retrieve the type of the object */
850 if (!IsEqualGUID(riid, &IID_IDirectXFileData)
851 && !IsEqualGUID(riid, &IID_IDirectXFileBinary))
852 ERR("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppvObject);
854 return E_NOINTERFACE;
857 static ULONG WINAPI IDirectXFileDataReferenceImpl_AddRef(IDirectXFileDataReference* iface)
859 IDirectXFileDataReferenceImpl *This = (IDirectXFileDataReferenceImpl *)iface;
860 ULONG ref = InterlockedIncrement(&This->ref);
862 TRACE("(%p/%p): AddRef from %d\n", iface, This, ref - 1);
864 return ref;
867 static ULONG WINAPI IDirectXFileDataReferenceImpl_Release(IDirectXFileDataReference* iface)
869 IDirectXFileDataReferenceImpl *This = (IDirectXFileDataReferenceImpl *)iface;
870 ULONG ref = InterlockedDecrement(&This->ref);
872 TRACE("(%p/%p): ReleaseRef to %d\n", iface, This, ref);
874 if (!ref)
875 HeapFree(GetProcessHeap(), 0, This);
877 return ref;
880 /*** IDirectXFileObject methods ***/
881 static HRESULT WINAPI IDirectXFileDataReferenceImpl_GetName(IDirectXFileDataReference* iface, LPSTR pstrNameBuf, LPDWORD pdwBufLen)
883 IDirectXFileDataReferenceImpl *This = (IDirectXFileDataReferenceImpl *)iface;
885 TRACE("(%p/%p)->(%p,%p)\n", This, iface, pstrNameBuf, pdwBufLen);
887 if (!pstrNameBuf)
888 return DXFILEERR_BADVALUE;
890 strcpy(pstrNameBuf, This->ptarget->name);
892 return DXFILEERR_BADVALUE;
895 static HRESULT WINAPI IDirectXFileDataReferenceImpl_GetId(IDirectXFileDataReference* iface, LPGUID pGuid)
897 IDirectXFileDataReferenceImpl *This = (IDirectXFileDataReferenceImpl *)iface;
899 TRACE("(%p/%p)->(%p)\n", This, iface, pGuid);
901 if (!pGuid)
902 return DXFILEERR_BADVALUE;
904 memcpy(pGuid, &This->ptarget->class_id, 16);
906 return DXFILE_OK;
909 /*** IDirectXFileDataReference ***/
910 static HRESULT WINAPI IDirectXFileDataReferenceImpl_Resolve(IDirectXFileDataReference* iface, LPDIRECTXFILEDATA* ppDataObj)
912 IDirectXFileDataReferenceImpl *This = (IDirectXFileDataReferenceImpl *)iface;
913 IDirectXFileDataImpl *object;
914 HRESULT hr;
916 TRACE("(%p/%p)->(%p)\n", This, iface, ppDataObj);
918 if (!ppDataObj)
919 return DXFILEERR_BADVALUE;
921 hr = IDirectXFileDataImpl_Create(&object);
922 if (FAILED(hr))
923 return hr;
925 object->pobj = This->ptarget;
926 object->cur_enum_object = 0;
927 object->level = 0;
928 object->from_ref = TRUE;
930 *ppDataObj = (LPDIRECTXFILEDATA)object;
932 return DXFILE_OK;
935 static const IDirectXFileDataReferenceVtbl IDirectXFileDataReference_Vtbl =
937 IDirectXFileDataReferenceImpl_QueryInterface,
938 IDirectXFileDataReferenceImpl_AddRef,
939 IDirectXFileDataReferenceImpl_Release,
940 IDirectXFileDataReferenceImpl_GetName,
941 IDirectXFileDataReferenceImpl_GetId,
942 IDirectXFileDataReferenceImpl_Resolve
945 static HRESULT IDirectXFileEnumObjectImpl_Create(IDirectXFileEnumObjectImpl** ppObj)
947 IDirectXFileEnumObjectImpl* object;
949 TRACE("(%p)\n", ppObj);
951 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectXFileEnumObjectImpl));
952 if (!object)
954 ERR("Out of memory\n");
955 return DXFILEERR_BADALLOC;
958 object->lpVtbl = &IDirectXFileEnumObject_Vtbl;
959 object->ref = 1;
961 *ppObj = object;
963 return S_OK;
966 /*** IUnknown methods ***/
967 static HRESULT WINAPI IDirectXFileEnumObjectImpl_QueryInterface(IDirectXFileEnumObject* iface, REFIID riid, void** ppvObject)
969 IDirectXFileEnumObjectImpl *This = (IDirectXFileEnumObjectImpl *)iface;
971 TRACE("(%p/%p)->(%s,%p)\n", iface, This, debugstr_guid(riid), ppvObject);
973 if (IsEqualGUID(riid, &IID_IUnknown)
974 || IsEqualGUID(riid, &IID_IDirectXFileEnumObject))
976 IClassFactory_AddRef(iface);
977 *ppvObject = This;
978 return S_OK;
981 ERR("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppvObject);
982 return E_NOINTERFACE;
985 static ULONG WINAPI IDirectXFileEnumObjectImpl_AddRef(IDirectXFileEnumObject* iface)
987 IDirectXFileEnumObjectImpl *This = (IDirectXFileEnumObjectImpl *)iface;
988 ULONG ref = InterlockedIncrement(&This->ref);
990 TRACE("(%p/%p): AddRef from %d\n", iface, This, ref - 1);
992 return ref;
995 static ULONG WINAPI IDirectXFileEnumObjectImpl_Release(IDirectXFileEnumObject* iface)
997 IDirectXFileEnumObjectImpl *This = (IDirectXFileEnumObjectImpl *)iface;
998 ULONG ref = InterlockedDecrement(&This->ref);
1000 TRACE("(%p/%p): ReleaseRef to %d\n", iface, This, ref);
1002 if (!ref)
1004 int i;
1005 for (i = 0; i < This->nb_xobjects; i++)
1006 IDirectXFileData_Release(This->pRefObjects[i]);
1007 if (This->source == DXFILELOAD_FROMFILE)
1009 UnmapViewOfFile(This->buffer);
1010 CloseHandle(This->file_mapping);
1011 CloseHandle(This->hFile);
1013 else if (This->source == DXFILELOAD_FROMRESOURCE)
1014 FreeResource(This->resource_data);
1015 HeapFree(GetProcessHeap(), 0, This);
1018 return ref;
1021 /*** IDirectXFileEnumObject methods ***/
1022 static HRESULT WINAPI IDirectXFileEnumObjectImpl_GetNextDataObject(IDirectXFileEnumObject* iface, LPDIRECTXFILEDATA* ppDataObj)
1024 IDirectXFileEnumObjectImpl *This = (IDirectXFileEnumObjectImpl *)iface;
1025 IDirectXFileDataImpl* object;
1026 HRESULT hr;
1027 LPBYTE pstrings = NULL;
1029 TRACE("(%p/%p)->(%p)\n", This, iface, ppDataObj);
1031 if (This->nb_xobjects >= MAX_OBJECTS)
1033 ERR("Too many objects\n");
1034 return DXFILEERR_NOMOREOBJECTS;
1037 if (!This->buf.rem_bytes)
1038 return DXFILEERR_NOMOREOBJECTS;
1040 hr = IDirectXFileDataImpl_Create(&object);
1041 if (FAILED(hr))
1042 return hr;
1044 This->buf.pxo_globals = This->xobjects;
1045 This->buf.nb_pxo_globals = This->nb_xobjects;
1046 This->buf.level = 0;
1048 This->buf.pxo_tab = HeapAlloc(GetProcessHeap(), 0, sizeof(xobject)*MAX_SUBOBJECTS);
1049 if (!This->buf.pxo_tab)
1051 ERR("Out of memory\n");
1052 hr = DXFILEERR_BADALLOC;
1053 goto error;
1055 This->buf.pxo = This->xobjects[This->nb_xobjects] = This->buf.pxo_tab;
1057 This->buf.pxo->pdata = This->buf.pdata = NULL;
1058 This->buf.capacity = 0;
1059 This->buf.cur_pos_data = 0;
1060 This->buf.pxo->nb_subobjects = 1;
1062 pstrings = HeapAlloc(GetProcessHeap(), 0, MAX_STRINGS_BUFFER);
1063 if (!pstrings)
1065 ERR("Out of memory\n");
1066 hr = DXFILEERR_BADALLOC;
1067 goto error;
1069 This->buf.cur_pstrings = This->buf.pstrings = object->pstrings = pstrings;
1071 if (!parse_object(&This->buf))
1073 TRACE("Object is not correct\n");
1074 hr = DXFILEERR_PARSEERROR;
1075 goto error;
1078 if (This->buf.pxo->nb_subobjects > MAX_SUBOBJECTS)
1080 FIXME("Too many subobjects %d\n", This->buf.pxo->nb_subobjects);
1081 hr = DXFILEERR_BADALLOC;
1082 goto error;
1085 object->pstrings = pstrings;
1086 object->pobj = This->buf.pxo;
1087 object->cur_enum_object = 0;
1088 object->level = 0;
1089 object->from_ref = FALSE;
1091 *ppDataObj = (LPDIRECTXFILEDATA)object;
1093 /* Get a reference to created object */
1094 This->pRefObjects[This->nb_xobjects] = (LPDIRECTXFILEDATA)object;
1095 IDirectXFileData_AddRef(This->pRefObjects[This->nb_xobjects]);
1097 This->nb_xobjects++;
1099 return DXFILE_OK;
1101 error:
1103 HeapFree(GetProcessHeap(), 0, This->buf.pxo_tab);
1104 HeapFree(GetProcessHeap(), 0, This->buf.pxo->pdata);
1105 HeapFree(GetProcessHeap(), 0, pstrings);
1107 return hr;
1110 static HRESULT WINAPI IDirectXFileEnumObjectImpl_GetDataObjectById(IDirectXFileEnumObject* iface, REFGUID rguid, LPDIRECTXFILEDATA* ppDataObj)
1112 IDirectXFileEnumObjectImpl *This = (IDirectXFileEnumObjectImpl *)iface;
1114 FIXME("(%p/%p)->(%p,%p) stub!\n", This, iface, rguid, ppDataObj);
1116 return DXFILEERR_BADVALUE;
1119 static HRESULT WINAPI IDirectXFileEnumObjectImpl_GetDataObjectByName(IDirectXFileEnumObject* iface, LPCSTR szName, LPDIRECTXFILEDATA* ppDataObj)
1121 IDirectXFileEnumObjectImpl *This = (IDirectXFileEnumObjectImpl *)iface;
1123 FIXME("(%p/%p)->(%s,%p) stub!\n", This, iface, szName, ppDataObj);
1125 return DXFILEERR_BADVALUE;
1128 static const IDirectXFileEnumObjectVtbl IDirectXFileEnumObject_Vtbl =
1130 IDirectXFileEnumObjectImpl_QueryInterface,
1131 IDirectXFileEnumObjectImpl_AddRef,
1132 IDirectXFileEnumObjectImpl_Release,
1133 IDirectXFileEnumObjectImpl_GetNextDataObject,
1134 IDirectXFileEnumObjectImpl_GetDataObjectById,
1135 IDirectXFileEnumObjectImpl_GetDataObjectByName
1138 static HRESULT IDirectXFileSaveObjectImpl_Create(IDirectXFileSaveObjectImpl** ppObj)
1140 IDirectXFileSaveObjectImpl* object;
1142 TRACE("(%p)\n", ppObj);
1144 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectXFileSaveObjectImpl));
1145 if (!object)
1147 ERR("Out of memory\n");
1148 return DXFILEERR_BADALLOC;
1151 object->lpVtbl = &IDirectXFileSaveObject_Vtbl;
1152 object->ref = 1;
1154 *ppObj = object;
1156 return S_OK;
1159 /*** IUnknown methods ***/
1160 static HRESULT WINAPI IDirectXFileSaveObjectImpl_QueryInterface(IDirectXFileSaveObject* iface, REFIID riid, void** ppvObject)
1162 IDirectXFileSaveObjectImpl *This = (IDirectXFileSaveObjectImpl *)iface;
1164 TRACE("(%p/%p)->(%s,%p)\n", iface, This, debugstr_guid(riid), ppvObject);
1166 if (IsEqualGUID(riid, &IID_IUnknown)
1167 || IsEqualGUID(riid, &IID_IDirectXFileSaveObject))
1169 IClassFactory_AddRef(iface);
1170 *ppvObject = This;
1171 return S_OK;
1174 ERR("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppvObject);
1175 return E_NOINTERFACE;
1178 static ULONG WINAPI IDirectXFileSaveObjectImpl_AddRef(IDirectXFileSaveObject* iface)
1180 IDirectXFileSaveObjectImpl *This = (IDirectXFileSaveObjectImpl *)iface;
1181 ULONG ref = InterlockedIncrement(&This->ref);
1183 TRACE("(%p/%p): AddRef from %d\n", iface, This, ref - 1);
1185 return ref;
1188 static ULONG WINAPI IDirectXFileSaveObjectImpl_Release(IDirectXFileSaveObject* iface)
1190 IDirectXFileSaveObjectImpl *This = (IDirectXFileSaveObjectImpl *)iface;
1191 ULONG ref = InterlockedDecrement(&This->ref);
1193 TRACE("(%p/%p): ReleaseRef to %d\n", iface, This, ref);
1195 if (!ref)
1196 HeapFree(GetProcessHeap(), 0, This);
1198 return ref;
1201 static HRESULT WINAPI IDirectXFileSaveObjectImpl_SaveTemplates(IDirectXFileSaveObject* iface, DWORD cTemplates, const GUID** ppguidTemplates)
1203 IDirectXFileSaveObjectImpl *This = (IDirectXFileSaveObjectImpl *)iface;
1205 FIXME("(%p/%p)->(%d,%p) stub!\n", This, iface, cTemplates, ppguidTemplates);
1207 return DXFILEERR_BADVALUE;
1210 static HRESULT WINAPI IDirectXFileSaveObjectImpl_CreateDataObject(IDirectXFileSaveObject* iface, REFGUID rguidTemplate, LPCSTR szName, const GUID* pguid, DWORD cbSize, LPVOID pvData, LPDIRECTXFILEDATA* ppDataObj)
1212 IDirectXFileSaveObjectImpl *This = (IDirectXFileSaveObjectImpl *)iface;
1214 FIXME("(%p/%p)->(%p,%s,%p,%d,%p,%p) stub!\n", This, iface, rguidTemplate, szName, pguid, cbSize, pvData, ppDataObj);
1216 return DXFILEERR_BADVALUE;
1219 static HRESULT WINAPI IDirectXFileSaveObjectImpl_SaveData(IDirectXFileSaveObject* iface, LPDIRECTXFILEDATA ppDataObj)
1221 IDirectXFileSaveObjectImpl *This = (IDirectXFileSaveObjectImpl *)iface;
1223 FIXME("(%p/%p)->(%p) stub!\n", This, iface, ppDataObj);
1225 return DXFILEERR_BADVALUE;
1228 static const IDirectXFileSaveObjectVtbl IDirectXFileSaveObject_Vtbl =
1230 IDirectXFileSaveObjectImpl_QueryInterface,
1231 IDirectXFileSaveObjectImpl_AddRef,
1232 IDirectXFileSaveObjectImpl_Release,
1233 IDirectXFileSaveObjectImpl_SaveTemplates,
1234 IDirectXFileSaveObjectImpl_CreateDataObject,
1235 IDirectXFileSaveObjectImpl_SaveData