2 * Copyright (C) 2012 Christian Costa
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "d3dx9_36_private.h"
24 static HRESULT
error_dxfile_to_d3dxfile(HRESULT error
)
28 case DXFILEERR_BADFILETYPE
:
29 return D3DXFERR_BADFILETYPE
;
30 case DXFILEERR_BADFILEVERSION
:
31 return D3DXFERR_BADFILEVERSION
;
32 case DXFILEERR_BADFILEFLOATSIZE
:
33 return D3DXFERR_BADFILEFLOATSIZE
;
34 case DXFILEERR_PARSEERROR
:
35 return D3DXFERR_PARSEERROR
;
36 case DXFILEERR_BADVALUE
:
37 return D3DXFERR_BADVALUE
;
39 FIXME("Cannot map error %#x\n", error
);
46 ID3DXFile ID3DXFile_iface
;
51 struct d3dx9_file_enum_object
53 ID3DXFileEnumObject ID3DXFileEnumObject_iface
;
56 ID3DXFileData
**children
;
59 struct d3dx9_file_data
61 ID3DXFileData ID3DXFileData_iface
;
64 IDirectXFileData
*dxfile_data
;
66 ID3DXFileData
**children
;
69 static inline struct d3dx9_file
*impl_from_ID3DXFile(ID3DXFile
*iface
)
71 return CONTAINING_RECORD(iface
, struct d3dx9_file
, ID3DXFile_iface
);
74 static inline struct d3dx9_file_enum_object
*impl_from_ID3DXFileEnumObject(ID3DXFileEnumObject
*iface
)
76 return CONTAINING_RECORD(iface
, struct d3dx9_file_enum_object
, ID3DXFileEnumObject_iface
);
79 static inline struct d3dx9_file_data
*impl_from_ID3DXFileData(ID3DXFileData
*iface
)
81 return CONTAINING_RECORD(iface
, struct d3dx9_file_data
, ID3DXFileData_iface
);
84 static HRESULT WINAPI
d3dx9_file_data_QueryInterface(ID3DXFileData
*iface
, REFIID riid
, void **out
)
86 TRACE("iface %p, riid %s, out %p.\n", iface
, debugstr_guid(riid
), out
);
88 if (IsEqualGUID(riid
, &IID_ID3DXFileData
)
89 || IsEqualGUID(riid
, &IID_IUnknown
))
91 iface
->lpVtbl
->AddRef(iface
);
96 WARN("Interface %s not found.\n", debugstr_guid(riid
));
102 static ULONG WINAPI
d3dx9_file_data_AddRef(ID3DXFileData
*iface
)
104 struct d3dx9_file_data
*file_data
= impl_from_ID3DXFileData(iface
);
105 ULONG refcount
= InterlockedIncrement(&file_data
->ref
);
107 TRACE("%p increasing refcount to %u.\n", file_data
, refcount
);
112 static ULONG WINAPI
d3dx9_file_data_Release(ID3DXFileData
*iface
)
114 struct d3dx9_file_data
*file_data
= impl_from_ID3DXFileData(iface
);
115 ULONG refcount
= InterlockedDecrement(&file_data
->ref
);
117 TRACE("%p decreasing refcount to %u.\n", file_data
, refcount
);
123 for (i
= 0; i
< file_data
->nb_children
; ++i
)
125 ID3DXFileData
*child
= file_data
->children
[i
];
126 child
->lpVtbl
->Release(child
);
128 HeapFree(GetProcessHeap(), 0, file_data
->children
);
129 IDirectXFileData_Release(file_data
->dxfile_data
);
130 HeapFree(GetProcessHeap(), 0, file_data
);
136 static HRESULT WINAPI
d3dx9_file_data_GetEnum(ID3DXFileData
*iface
, ID3DXFileEnumObject
**enum_object
)
138 FIXME("iface %p, enum_object %p stub!\n", iface
, enum_object
);
143 static HRESULT WINAPI
d3dx9_file_data_GetName(ID3DXFileData
*iface
, char *name
, SIZE_T
*size
)
145 struct d3dx9_file_data
*file_data
= impl_from_ID3DXFileData(iface
);
149 TRACE("iface %p, name %p, size %p.\n", iface
, name
, size
);
152 return D3DXFERR_BADVALUE
;
156 ret
= IDirectXFileData_GetName(file_data
->dxfile_data
, name
, &dxfile_size
);
157 if (ret
!= DXFILE_OK
)
158 return error_dxfile_to_d3dxfile(ret
);
162 /* Contrary to d3dxof, d3dx9_36 returns an empty string with a null byte when no name is available.
163 * If the input size is 0, it returns a length of 1 without touching the buffer */
174 static HRESULT WINAPI
d3dx9_file_data_GetId(ID3DXFileData
*iface
, GUID
*guid
)
176 struct d3dx9_file_data
*file_data
= impl_from_ID3DXFileData(iface
);
179 TRACE("iface %p, guid %p.\n", iface
, guid
);
184 ret
= IDirectXFileData_GetId(file_data
->dxfile_data
, guid
);
185 if (ret
!= DXFILE_OK
)
186 return error_dxfile_to_d3dxfile(ret
);
191 static HRESULT WINAPI
d3dx9_file_data_Lock(ID3DXFileData
*iface
, SIZE_T
*size
, const void **data
)
193 struct d3dx9_file_data
*file_data
= impl_from_ID3DXFileData(iface
);
197 TRACE("iface %p, size %p, data %p.\n", iface
, size
, data
);
202 ret
= IDirectXFileData_GetData(file_data
->dxfile_data
, NULL
, &dxfile_size
, (void **)data
);
203 if (ret
!= DXFILE_OK
)
204 return error_dxfile_to_d3dxfile(ret
);
211 static HRESULT WINAPI
d3dx9_file_data_Unlock(ID3DXFileData
*iface
)
213 TRACE("iface %p.\n", iface
);
220 static HRESULT WINAPI
d3dx9_file_data_GetType(ID3DXFileData
*iface
, GUID
*guid
)
222 struct d3dx9_file_data
*file_data
= impl_from_ID3DXFileData(iface
);
223 const GUID
*dxfile_guid
;
226 TRACE("iface %p, guid %p.\n", iface
, guid
);
228 ret
= IDirectXFileData_GetType(file_data
->dxfile_data
, &dxfile_guid
);
229 if (ret
!= DXFILE_OK
)
230 return error_dxfile_to_d3dxfile(ret
);
232 *guid
= *dxfile_guid
;
237 static BOOL WINAPI
d3dx9_file_data_IsReference(ID3DXFileData
*iface
)
239 struct d3dx9_file_data
*file_data
= impl_from_ID3DXFileData(iface
);
241 TRACE("iface %p.\n", iface
);
243 return file_data
->reference
;
246 static HRESULT WINAPI
d3dx9_file_data_GetChildren(ID3DXFileData
*iface
, SIZE_T
*children
)
248 struct d3dx9_file_data
*file_data
= impl_from_ID3DXFileData(iface
);
250 TRACE("iface %p, children %p.\n", iface
, children
);
255 *children
= file_data
->nb_children
;
260 static HRESULT WINAPI
d3dx9_file_data_GetChild(ID3DXFileData
*iface
, SIZE_T id
, ID3DXFileData
**object
)
262 struct d3dx9_file_data
*file_data
= impl_from_ID3DXFileData(iface
);
264 TRACE("iface %p, id %#lx, object %p.\n", iface
, id
, object
);
269 *object
= file_data
->children
[id
];
270 (*object
)->lpVtbl
->AddRef(*object
);
275 static const ID3DXFileDataVtbl d3dx9_file_data_vtbl
=
277 d3dx9_file_data_QueryInterface
,
278 d3dx9_file_data_AddRef
,
279 d3dx9_file_data_Release
,
280 d3dx9_file_data_GetEnum
,
281 d3dx9_file_data_GetName
,
282 d3dx9_file_data_GetId
,
283 d3dx9_file_data_Lock
,
284 d3dx9_file_data_Unlock
,
285 d3dx9_file_data_GetType
,
286 d3dx9_file_data_IsReference
,
287 d3dx9_file_data_GetChildren
,
288 d3dx9_file_data_GetChild
,
291 static HRESULT
d3dx9_file_data_create(IDirectXFileObject
*dxfile_object
, ID3DXFileData
**ret_iface
)
293 struct d3dx9_file_data
*object
;
294 IDirectXFileObject
*data_object
;
295 unsigned int children_array_size
= 0;
298 TRACE("dxfile_object %p, ret_iface %p.\n", dxfile_object
, ret_iface
);
302 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
304 return E_OUTOFMEMORY
;
306 object
->ID3DXFileData_iface
.lpVtbl
= &d3dx9_file_data_vtbl
;
309 ret
= IDirectXFileObject_QueryInterface(dxfile_object
, &IID_IDirectXFileData
, (void **)&object
->dxfile_data
);
312 IDirectXFileDataReference
*reference
;
314 ret
= IDirectXFileObject_QueryInterface(dxfile_object
, &IID_IDirectXFileDataReference
, (void **)&reference
);
317 ret
= IDirectXFileDataReference_Resolve(reference
, &object
->dxfile_data
);
318 IUnknown_Release(reference
);
321 HeapFree(GetProcessHeap(), 0, object
);
324 object
->reference
= TRUE
;
328 FIXME("Don't know what to do with binary object\n");
329 HeapFree(GetProcessHeap(), 0, object
);
334 while (SUCCEEDED(ret
= IDirectXFileData_GetNextObject(object
->dxfile_data
, &data_object
)))
336 if (object
->nb_children
>= children_array_size
)
338 ID3DXFileData
**new_children
;
340 if (object
->children
)
342 children_array_size
*= 2;
343 new_children
= HeapReAlloc(GetProcessHeap(), 0, object
->children
,
344 sizeof(*object
->children
) * children_array_size
);
348 children_array_size
= 4;
349 new_children
= HeapAlloc(GetProcessHeap(), 0, sizeof(*object
->children
) * children_array_size
);
356 object
->children
= new_children
;
358 ret
= d3dx9_file_data_create(data_object
, &object
->children
[object
->nb_children
]);
359 IUnknown_Release(data_object
);
362 object
->nb_children
++;
364 if (ret
!= DXFILEERR_NOMOREOBJECTS
)
366 (&object
->ID3DXFileData_iface
)->lpVtbl
->Release(&object
->ID3DXFileData_iface
);
369 if (object
->children
)
371 ID3DXFileData
**new_children
;
373 new_children
= HeapReAlloc(GetProcessHeap(), 0, object
->children
,
374 sizeof(*object
->children
) * object
->nb_children
);
376 object
->children
= new_children
;
379 TRACE("Found %u children\n", object
->nb_children
);
381 *ret_iface
= &object
->ID3DXFileData_iface
;
386 static HRESULT WINAPI
d3dx9_file_enum_object_QueryInterface(ID3DXFileEnumObject
*iface
, REFIID riid
, void **out
)
388 TRACE("iface %p, riid %s, out %p.\n", iface
, debugstr_guid(riid
), out
);
390 if (IsEqualGUID(riid
, &IID_ID3DXFileEnumObject
)
391 || IsEqualGUID(riid
, &IID_IUnknown
))
393 iface
->lpVtbl
->AddRef(iface
);
398 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid
));
401 return E_NOINTERFACE
;
404 static ULONG WINAPI
d3dx9_file_enum_object_AddRef(ID3DXFileEnumObject
*iface
)
406 struct d3dx9_file_enum_object
*file_enum
= impl_from_ID3DXFileEnumObject(iface
);
407 ULONG refcount
= InterlockedIncrement(&file_enum
->ref
);
409 TRACE("%p increasing refcount to %u.\n", file_enum
, refcount
);
414 static ULONG WINAPI
d3dx9_file_enum_object_Release(ID3DXFileEnumObject
*iface
)
416 struct d3dx9_file_enum_object
*file_enum
= impl_from_ID3DXFileEnumObject(iface
);
417 ULONG refcount
= InterlockedDecrement(&file_enum
->ref
);
419 TRACE("%p decreasing refcount to %u.\n", file_enum
, refcount
);
425 for (i
= 0; i
< file_enum
->nb_children
; ++i
)
427 ID3DXFileData
*child
= file_enum
->children
[i
];
428 child
->lpVtbl
->Release(child
);
430 HeapFree(GetProcessHeap(), 0, file_enum
->children
);
431 HeapFree(GetProcessHeap(), 0, file_enum
);
437 static HRESULT WINAPI
d3dx9_file_enum_object_GetFile(ID3DXFileEnumObject
*iface
, ID3DXFile
**file
)
439 FIXME("iface %p, file %p stub!\n", iface
, file
);
444 static HRESULT WINAPI
d3dx9_file_enum_object_GetChildren(ID3DXFileEnumObject
*iface
, SIZE_T
*children
)
446 struct d3dx9_file_enum_object
*file_enum
= impl_from_ID3DXFileEnumObject(iface
);
448 TRACE("iface %p, children %p.\n", iface
, children
);
453 *children
= file_enum
->nb_children
;
458 static HRESULT WINAPI
d3dx9_file_enum_object_GetChild(ID3DXFileEnumObject
*iface
, SIZE_T id
, ID3DXFileData
**object
)
460 struct d3dx9_file_enum_object
*file_enum
= impl_from_ID3DXFileEnumObject(iface
);
462 TRACE("iface %p, id %#lx, object %p.\n", iface
, id
, object
);
467 *object
= file_enum
->children
[id
];
468 (*object
)->lpVtbl
->AddRef(*object
);
473 static HRESULT WINAPI
d3dx9_file_enum_object_GetDataObjectById(ID3DXFileEnumObject
*iface
,
474 REFGUID guid
, ID3DXFileData
**object
)
476 FIXME("iface %p, guid %s, object %p stub!\n", iface
, debugstr_guid(guid
), object
);
481 static HRESULT WINAPI
d3dx9_file_enum_object_GetDataObjectByName(ID3DXFileEnumObject
*iface
,
482 const char *name
, ID3DXFileData
**object
)
484 FIXME("iface %p, name %s, object %p stub!\n", iface
, debugstr_a(name
), object
);
489 static const ID3DXFileEnumObjectVtbl d3dx9_file_enum_object_vtbl
=
491 d3dx9_file_enum_object_QueryInterface
,
492 d3dx9_file_enum_object_AddRef
,
493 d3dx9_file_enum_object_Release
,
494 d3dx9_file_enum_object_GetFile
,
495 d3dx9_file_enum_object_GetChildren
,
496 d3dx9_file_enum_object_GetChild
,
497 d3dx9_file_enum_object_GetDataObjectById
,
498 d3dx9_file_enum_object_GetDataObjectByName
,
501 static HRESULT WINAPI
d3dx9_file_QueryInterface(ID3DXFile
*iface
, REFIID riid
, void **out
)
503 TRACE("iface %p, riid %s, out %p.\n", iface
, debugstr_guid(riid
), out
);
505 if (IsEqualGUID(riid
, &IID_ID3DXFile
)
506 || IsEqualGUID(riid
, &IID_IUnknown
))
508 iface
->lpVtbl
->AddRef(iface
);
513 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid
));
516 return E_NOINTERFACE
;
519 static ULONG WINAPI
d3dx9_file_AddRef(ID3DXFile
*iface
)
521 struct d3dx9_file
*file
= impl_from_ID3DXFile(iface
);
522 ULONG refcount
= InterlockedIncrement(&file
->ref
);
524 TRACE("%p increasing refcount to %u.\n", file
, refcount
);
529 static ULONG WINAPI
d3dx9_file_Release(ID3DXFile
*iface
)
531 struct d3dx9_file
*file
= impl_from_ID3DXFile(iface
);
532 ULONG refcount
= InterlockedDecrement(&file
->ref
);
534 TRACE("%p decreasing refcount to %u.\n", file
, refcount
);
538 IDirectXFile_Release(file
->dxfile
);
539 HeapFree(GetProcessHeap(), 0, file
);
545 static HRESULT WINAPI
d3dx9_file_CreateEnumObject(ID3DXFile
*iface
, const void *source
,
546 D3DXF_FILELOADOPTIONS options
, ID3DXFileEnumObject
**enum_object
)
548 struct d3dx9_file
*file
= impl_from_ID3DXFile(iface
);
549 struct d3dx9_file_enum_object
*object
;
550 IDirectXFileEnumObject
*dxfile_enum_object
;
552 DXFILELOADOPTIONS dxfile_options
;
553 DXFILELOADRESOURCE dxfile_resource
;
554 DXFILELOADMEMORY dxfile_memory
;
555 IDirectXFileData
*data_object
;
556 unsigned children_array_size
= 0;
559 TRACE("iface %p, source %p, options %#x, enum_object %p.\n", iface
, source
, options
, enum_object
);
566 if (options
== D3DXF_FILELOAD_FROMFILE
)
568 dxfile_source
= (void*)source
;
569 dxfile_options
= DXFILELOAD_FROMFILE
;
571 else if (options
== D3DXF_FILELOAD_FROMRESOURCE
)
573 D3DXF_FILELOADRESOURCE
*resource
= (D3DXF_FILELOADRESOURCE
*)source
;
575 dxfile_resource
.hModule
= resource
->hModule
;
576 dxfile_resource
.lpName
= resource
->lpName
;
577 dxfile_resource
.lpType
= resource
->lpType
;
578 dxfile_source
= &dxfile_resource
;
579 dxfile_options
= DXFILELOAD_FROMRESOURCE
;
581 else if (options
== D3DXF_FILELOAD_FROMMEMORY
)
583 D3DXF_FILELOADMEMORY
*memory
= (D3DXF_FILELOADMEMORY
*)source
;
585 dxfile_memory
.lpMemory
= memory
->lpMemory
;
586 dxfile_memory
.dSize
= memory
->dSize
;
587 dxfile_source
= &dxfile_memory
;
588 dxfile_options
= DXFILELOAD_FROMMEMORY
;
592 FIXME("Source type %u is not handled yet\n", options
);
596 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
598 return E_OUTOFMEMORY
;
600 object
->ID3DXFileEnumObject_iface
.lpVtbl
= &d3dx9_file_enum_object_vtbl
;
603 ret
= IDirectXFile_CreateEnumObject(file
->dxfile
, dxfile_source
, dxfile_options
, &dxfile_enum_object
);
607 HeapFree(GetProcessHeap(), 0, object
);
611 /* Fill enum object with top level data objects */
612 while (SUCCEEDED(ret
= IDirectXFileEnumObject_GetNextDataObject(dxfile_enum_object
, &data_object
)))
614 if (object
->nb_children
>= children_array_size
)
616 ID3DXFileData
**new_children
;
618 if (object
->children
)
620 children_array_size
*= 2;
621 new_children
= HeapReAlloc(GetProcessHeap(), 0, object
->children
,
622 sizeof(*object
->children
) * children_array_size
);
626 children_array_size
= 4;
627 new_children
= HeapAlloc(GetProcessHeap(), 0, sizeof(*object
->children
) * children_array_size
);
634 object
->children
= new_children
;
636 ret
= d3dx9_file_data_create((IDirectXFileObject
*)data_object
,
637 &object
->children
[object
->nb_children
]);
638 IUnknown_Release(data_object
);
641 object
->nb_children
++;
643 if (object
->children
)
645 ID3DXFileData
**new_children
;
647 new_children
= HeapReAlloc(GetProcessHeap(), 0, object
->children
,
648 sizeof(*object
->children
) * object
->nb_children
);
650 object
->children
= new_children
;
653 IDirectXFileEnumObject_Release(dxfile_enum_object
);
655 if (ret
!= DXFILEERR_NOMOREOBJECTS
)
656 WARN("Cannot get all top level data objects\n");
658 TRACE("Found %u children\n", object
->nb_children
);
660 *enum_object
= &object
->ID3DXFileEnumObject_iface
;
665 static HRESULT WINAPI
d3dx9_file_CreateSaveObject(ID3DXFile
*iface
, const void *data
,
666 D3DXF_FILESAVEOPTIONS options
, D3DXF_FILEFORMAT format
, ID3DXFileSaveObject
**save_object
)
668 FIXME("iface %p, data %p, options %#x, format %#x, save_object %p stub!\n",
669 iface
, data
, options
, format
, save_object
);
674 static HRESULT WINAPI
d3dx9_file_RegisterTemplates(ID3DXFile
*iface
, const void *data
, SIZE_T size
)
676 struct d3dx9_file
*file
= impl_from_ID3DXFile(iface
);
679 TRACE("iface %p, data %p, size %lu.\n", iface
, data
, size
);
681 ret
= IDirectXFile_RegisterTemplates(file
->dxfile
, (void *)data
, size
);
682 if (ret
!= DXFILE_OK
)
684 WARN("Error %#x\n", ret
);
685 return error_dxfile_to_d3dxfile(ret
);
691 static HRESULT WINAPI
d3dx9_file_RegisterEnumTemplates(ID3DXFile
*iface
, ID3DXFileEnumObject
*enum_object
)
693 FIXME("iface %p, enum_object %p stub!\n", iface
, enum_object
);
698 static const ID3DXFileVtbl d3dx9_file_vtbl
=
700 d3dx9_file_QueryInterface
,
703 d3dx9_file_CreateEnumObject
,
704 d3dx9_file_CreateSaveObject
,
705 d3dx9_file_RegisterTemplates
,
706 d3dx9_file_RegisterEnumTemplates
,
709 HRESULT WINAPI
D3DXFileCreate(ID3DXFile
**d3dxfile
)
711 struct d3dx9_file
*object
;
714 TRACE("d3dxfile %p.\n", d3dxfile
);
721 object
= HeapAlloc(GetProcessHeap(), 0, sizeof(*object
));
723 return E_OUTOFMEMORY
;
725 ret
= DirectXFileCreate(&object
->dxfile
);
728 HeapFree(GetProcessHeap(), 0, object
);
729 if (ret
== E_OUTOFMEMORY
)
734 object
->ID3DXFile_iface
.lpVtbl
= &d3dx9_file_vtbl
;
737 *d3dxfile
= &object
->ID3DXFile_iface
;