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"
26 static HRESULT
error_dxfile_to_d3dxfile(HRESULT error
)
30 case DXFILEERR_BADFILETYPE
:
31 return D3DXFERR_BADFILETYPE
;
32 case DXFILEERR_BADFILEVERSION
:
33 return D3DXFERR_BADFILEVERSION
;
34 case DXFILEERR_BADFILEFLOATSIZE
:
35 return D3DXFERR_BADFILEFLOATSIZE
;
36 case DXFILEERR_PARSEERROR
:
37 return D3DXFERR_PARSEERROR
;
38 case DXFILEERR_BADVALUE
:
39 return D3DXFERR_BADVALUE
;
41 FIXME("Cannot map error %#x\n", error
);
48 ID3DXFile ID3DXFile_iface
;
53 struct d3dx9_file_enum_object
55 ID3DXFileEnumObject ID3DXFileEnumObject_iface
;
58 ID3DXFileData
**children
;
61 struct d3dx9_file_data
63 ID3DXFileData ID3DXFileData_iface
;
66 IDirectXFileData
*dxfile_data
;
68 ID3DXFileData
**children
;
71 static inline struct d3dx9_file
*impl_from_ID3DXFile(ID3DXFile
*iface
)
73 return CONTAINING_RECORD(iface
, struct d3dx9_file
, ID3DXFile_iface
);
76 static inline struct d3dx9_file_enum_object
*impl_from_ID3DXFileEnumObject(ID3DXFileEnumObject
*iface
)
78 return CONTAINING_RECORD(iface
, struct d3dx9_file_enum_object
, ID3DXFileEnumObject_iface
);
81 static inline struct d3dx9_file_data
*impl_from_ID3DXFileData(ID3DXFileData
*iface
)
83 return CONTAINING_RECORD(iface
, struct d3dx9_file_data
, ID3DXFileData_iface
);
86 static HRESULT WINAPI
d3dx9_file_data_QueryInterface(ID3DXFileData
*iface
, REFIID riid
, void **out
)
88 TRACE("iface %p, riid %s, out %p.\n", iface
, debugstr_guid(riid
), out
);
90 if (IsEqualGUID(riid
, &IID_ID3DXFileData
)
91 || IsEqualGUID(riid
, &IID_IUnknown
))
93 iface
->lpVtbl
->AddRef(iface
);
98 WARN("Interface %s not found.\n", debugstr_guid(riid
));
101 return E_NOINTERFACE
;
104 static ULONG WINAPI
d3dx9_file_data_AddRef(ID3DXFileData
*iface
)
106 struct d3dx9_file_data
*file_data
= impl_from_ID3DXFileData(iface
);
107 ULONG refcount
= InterlockedIncrement(&file_data
->ref
);
109 TRACE("%p increasing refcount to %u.\n", file_data
, refcount
);
114 static ULONG WINAPI
d3dx9_file_data_Release(ID3DXFileData
*iface
)
116 struct d3dx9_file_data
*file_data
= impl_from_ID3DXFileData(iface
);
117 ULONG refcount
= InterlockedDecrement(&file_data
->ref
);
119 TRACE("%p decreasing refcount to %u.\n", file_data
, refcount
);
125 for (i
= 0; i
< file_data
->nb_children
; ++i
)
127 ID3DXFileData
*child
= file_data
->children
[i
];
128 child
->lpVtbl
->Release(child
);
130 HeapFree(GetProcessHeap(), 0, file_data
->children
);
131 IDirectXFileData_Release(file_data
->dxfile_data
);
132 HeapFree(GetProcessHeap(), 0, file_data
);
138 static HRESULT WINAPI
d3dx9_file_data_GetEnum(ID3DXFileData
*iface
, ID3DXFileEnumObject
**enum_object
)
140 FIXME("iface %p, enum_object %p stub!\n", iface
, enum_object
);
145 static HRESULT WINAPI
d3dx9_file_data_GetName(ID3DXFileData
*iface
, char *name
, SIZE_T
*size
)
147 struct d3dx9_file_data
*file_data
= impl_from_ID3DXFileData(iface
);
151 TRACE("iface %p, name %p, size %p.\n", iface
, name
, size
);
154 return D3DXFERR_BADVALUE
;
158 ret
= IDirectXFileData_GetName(file_data
->dxfile_data
, name
, &dxfile_size
);
159 if (ret
!= DXFILE_OK
)
160 return error_dxfile_to_d3dxfile(ret
);
164 /* Contrary to d3dxof, d3dx9_36 returns an empty string with a null byte when no name is available.
165 * If the input size is 0, it returns a length of 1 without touching the buffer */
176 static HRESULT WINAPI
d3dx9_file_data_GetId(ID3DXFileData
*iface
, GUID
*guid
)
178 struct d3dx9_file_data
*file_data
= impl_from_ID3DXFileData(iface
);
181 TRACE("iface %p, guid %p.\n", iface
, guid
);
186 ret
= IDirectXFileData_GetId(file_data
->dxfile_data
, guid
);
187 if (ret
!= DXFILE_OK
)
188 return error_dxfile_to_d3dxfile(ret
);
193 static HRESULT WINAPI
d3dx9_file_data_Lock(ID3DXFileData
*iface
, SIZE_T
*size
, const void **data
)
195 struct d3dx9_file_data
*file_data
= impl_from_ID3DXFileData(iface
);
199 TRACE("iface %p, size %p, data %p.\n", iface
, size
, data
);
204 ret
= IDirectXFileData_GetData(file_data
->dxfile_data
, NULL
, &dxfile_size
, (void **)data
);
205 if (ret
!= DXFILE_OK
)
206 return error_dxfile_to_d3dxfile(ret
);
213 static HRESULT WINAPI
d3dx9_file_data_Unlock(ID3DXFileData
*iface
)
215 TRACE("iface %p.\n", iface
);
222 static HRESULT WINAPI
d3dx9_file_data_GetType(ID3DXFileData
*iface
, GUID
*guid
)
224 struct d3dx9_file_data
*file_data
= impl_from_ID3DXFileData(iface
);
225 const GUID
*dxfile_guid
;
228 TRACE("iface %p, guid %p.\n", iface
, guid
);
230 ret
= IDirectXFileData_GetType(file_data
->dxfile_data
, &dxfile_guid
);
231 if (ret
!= DXFILE_OK
)
232 return error_dxfile_to_d3dxfile(ret
);
234 *guid
= *dxfile_guid
;
239 static BOOL WINAPI
d3dx9_file_data_IsReference(ID3DXFileData
*iface
)
241 struct d3dx9_file_data
*file_data
= impl_from_ID3DXFileData(iface
);
243 TRACE("iface %p.\n", iface
);
245 return file_data
->reference
;
248 static HRESULT WINAPI
d3dx9_file_data_GetChildren(ID3DXFileData
*iface
, SIZE_T
*children
)
250 struct d3dx9_file_data
*file_data
= impl_from_ID3DXFileData(iface
);
252 TRACE("iface %p, children %p.\n", iface
, children
);
257 *children
= file_data
->nb_children
;
262 static HRESULT WINAPI
d3dx9_file_data_GetChild(ID3DXFileData
*iface
, SIZE_T id
, ID3DXFileData
**object
)
264 struct d3dx9_file_data
*file_data
= impl_from_ID3DXFileData(iface
);
266 TRACE("iface %p, id %#lx, object %p.\n", iface
, id
, object
);
271 *object
= file_data
->children
[id
];
272 (*object
)->lpVtbl
->AddRef(*object
);
277 static const ID3DXFileDataVtbl d3dx9_file_data_vtbl
=
279 d3dx9_file_data_QueryInterface
,
280 d3dx9_file_data_AddRef
,
281 d3dx9_file_data_Release
,
282 d3dx9_file_data_GetEnum
,
283 d3dx9_file_data_GetName
,
284 d3dx9_file_data_GetId
,
285 d3dx9_file_data_Lock
,
286 d3dx9_file_data_Unlock
,
287 d3dx9_file_data_GetType
,
288 d3dx9_file_data_IsReference
,
289 d3dx9_file_data_GetChildren
,
290 d3dx9_file_data_GetChild
,
293 static HRESULT
d3dx9_file_data_create(IDirectXFileObject
*dxfile_object
, ID3DXFileData
**ret_iface
)
295 struct d3dx9_file_data
*object
;
296 IDirectXFileObject
*data_object
;
297 unsigned int children_array_size
= 0;
300 TRACE("dxfile_object %p, ret_iface %p.\n", dxfile_object
, ret_iface
);
304 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
306 return E_OUTOFMEMORY
;
308 object
->ID3DXFileData_iface
.lpVtbl
= &d3dx9_file_data_vtbl
;
311 ret
= IDirectXFileObject_QueryInterface(dxfile_object
, &IID_IDirectXFileData
, (void **)&object
->dxfile_data
);
314 IDirectXFileDataReference
*reference
;
316 ret
= IDirectXFileObject_QueryInterface(dxfile_object
, &IID_IDirectXFileDataReference
, (void **)&reference
);
319 ret
= IDirectXFileDataReference_Resolve(reference
, &object
->dxfile_data
);
320 IUnknown_Release(reference
);
323 HeapFree(GetProcessHeap(), 0, object
);
326 object
->reference
= TRUE
;
330 FIXME("Don't known what to do with binary object\n");
331 HeapFree(GetProcessHeap(), 0, object
);
336 while (SUCCEEDED(ret
= IDirectXFileData_GetNextObject(object
->dxfile_data
, &data_object
)))
338 if (object
->nb_children
>= children_array_size
)
340 ID3DXFileData
**new_children
;
342 if (object
->children
)
344 children_array_size
*= 2;
345 new_children
= HeapReAlloc(GetProcessHeap(), 0, object
->children
,
346 sizeof(*object
->children
) * children_array_size
);
350 children_array_size
= 4;
351 new_children
= HeapAlloc(GetProcessHeap(), 0, sizeof(*object
->children
) * children_array_size
);
358 object
->children
= new_children
;
360 ret
= d3dx9_file_data_create(data_object
, &object
->children
[object
->nb_children
]);
361 IUnknown_Release(data_object
);
364 object
->nb_children
++;
366 if (ret
!= DXFILEERR_NOMOREOBJECTS
)
368 (&object
->ID3DXFileData_iface
)->lpVtbl
->Release(&object
->ID3DXFileData_iface
);
371 if (object
->children
)
373 ID3DXFileData
**new_children
;
375 new_children
= HeapReAlloc(GetProcessHeap(), 0, object
->children
,
376 sizeof(*object
->children
) * object
->nb_children
);
378 object
->children
= new_children
;
381 TRACE("Found %u children\n", object
->nb_children
);
383 *ret_iface
= &object
->ID3DXFileData_iface
;
388 static HRESULT WINAPI
d3dx9_file_enum_object_QueryInterface(ID3DXFileEnumObject
*iface
, REFIID riid
, void **out
)
390 TRACE("iface %p, riid %s, out %p.\n", iface
, debugstr_guid(riid
), out
);
392 if (IsEqualGUID(riid
, &IID_ID3DXFileEnumObject
)
393 || IsEqualGUID(riid
, &IID_IUnknown
))
395 iface
->lpVtbl
->AddRef(iface
);
400 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid
));
403 return E_NOINTERFACE
;
406 static ULONG WINAPI
d3dx9_file_enum_object_AddRef(ID3DXFileEnumObject
*iface
)
408 struct d3dx9_file_enum_object
*file_enum
= impl_from_ID3DXFileEnumObject(iface
);
409 ULONG refcount
= InterlockedIncrement(&file_enum
->ref
);
411 TRACE("%p increasing refcount to %u.\n", file_enum
, refcount
);
416 static ULONG WINAPI
d3dx9_file_enum_object_Release(ID3DXFileEnumObject
*iface
)
418 struct d3dx9_file_enum_object
*file_enum
= impl_from_ID3DXFileEnumObject(iface
);
419 ULONG refcount
= InterlockedDecrement(&file_enum
->ref
);
421 TRACE("%p decreasing refcount to %u.\n", file_enum
, refcount
);
427 for (i
= 0; i
< file_enum
->nb_children
; ++i
)
429 ID3DXFileData
*child
= file_enum
->children
[i
];
430 child
->lpVtbl
->Release(child
);
432 HeapFree(GetProcessHeap(), 0, file_enum
->children
);
433 HeapFree(GetProcessHeap(), 0, file_enum
);
439 static HRESULT WINAPI
d3dx9_file_enum_object_GetFile(ID3DXFileEnumObject
*iface
, ID3DXFile
**file
)
441 FIXME("iface %p, file %p stub!\n", iface
, file
);
446 static HRESULT WINAPI
d3dx9_file_enum_object_GetChildren(ID3DXFileEnumObject
*iface
, SIZE_T
*children
)
448 struct d3dx9_file_enum_object
*file_enum
= impl_from_ID3DXFileEnumObject(iface
);
450 TRACE("iface %p, children %p.\n", iface
, children
);
455 *children
= file_enum
->nb_children
;
460 static HRESULT WINAPI
d3dx9_file_enum_object_GetChild(ID3DXFileEnumObject
*iface
, SIZE_T id
, ID3DXFileData
**object
)
462 struct d3dx9_file_enum_object
*file_enum
= impl_from_ID3DXFileEnumObject(iface
);
464 TRACE("iface %p, id %#lx, object %p.\n", iface
, id
, object
);
469 *object
= file_enum
->children
[id
];
470 (*object
)->lpVtbl
->AddRef(*object
);
475 static HRESULT WINAPI
d3dx9_file_enum_object_GetDataObjectById(ID3DXFileEnumObject
*iface
,
476 REFGUID guid
, ID3DXFileData
**object
)
478 FIXME("iface %p, guid %s, object %p stub!\n", iface
, debugstr_guid(guid
), object
);
483 static HRESULT WINAPI
d3dx9_file_enum_object_GetDataObjectByName(ID3DXFileEnumObject
*iface
,
484 const char *name
, ID3DXFileData
**object
)
486 FIXME("iface %p, name %s, object %p stub!\n", iface
, debugstr_a(name
), object
);
491 static const ID3DXFileEnumObjectVtbl d3dx9_file_enum_object_vtbl
=
493 d3dx9_file_enum_object_QueryInterface
,
494 d3dx9_file_enum_object_AddRef
,
495 d3dx9_file_enum_object_Release
,
496 d3dx9_file_enum_object_GetFile
,
497 d3dx9_file_enum_object_GetChildren
,
498 d3dx9_file_enum_object_GetChild
,
499 d3dx9_file_enum_object_GetDataObjectById
,
500 d3dx9_file_enum_object_GetDataObjectByName
,
503 static HRESULT WINAPI
d3dx9_file_QueryInterface(ID3DXFile
*iface
, REFIID riid
, void **out
)
505 TRACE("iface %p, riid %s, out %p.\n", iface
, debugstr_guid(riid
), out
);
507 if (IsEqualGUID(riid
, &IID_ID3DXFile
)
508 || IsEqualGUID(riid
, &IID_IUnknown
))
510 iface
->lpVtbl
->AddRef(iface
);
515 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid
));
518 return E_NOINTERFACE
;
521 static ULONG WINAPI
d3dx9_file_AddRef(ID3DXFile
*iface
)
523 struct d3dx9_file
*file
= impl_from_ID3DXFile(iface
);
524 ULONG refcount
= InterlockedIncrement(&file
->ref
);
526 TRACE("%p increasing refcount to %u.\n", file
, refcount
);
531 static ULONG WINAPI
d3dx9_file_Release(ID3DXFile
*iface
)
533 struct d3dx9_file
*file
= impl_from_ID3DXFile(iface
);
534 ULONG refcount
= InterlockedDecrement(&file
->ref
);
536 TRACE("%p decreasing refcount to %u.\n", file
, refcount
);
540 IDirectXFile_Release(file
->dxfile
);
541 HeapFree(GetProcessHeap(), 0, file
);
547 static HRESULT WINAPI
d3dx9_file_CreateEnumObject(ID3DXFile
*iface
, const void *source
,
548 D3DXF_FILELOADOPTIONS options
, ID3DXFileEnumObject
**enum_object
)
550 struct d3dx9_file
*file
= impl_from_ID3DXFile(iface
);
551 struct d3dx9_file_enum_object
*object
;
552 IDirectXFileEnumObject
*dxfile_enum_object
;
554 DXFILELOADOPTIONS dxfile_options
;
555 DXFILELOADRESOURCE dxfile_resource
;
556 DXFILELOADMEMORY dxfile_memory
;
557 IDirectXFileData
*data_object
;
558 unsigned children_array_size
= 0;
561 TRACE("iface %p, source %p, options %#x, enum_object %p.\n", iface
, source
, options
, enum_object
);
568 if (options
== D3DXF_FILELOAD_FROMFILE
)
570 dxfile_source
= (void*)source
;
571 dxfile_options
= DXFILELOAD_FROMFILE
;
573 else if (options
== D3DXF_FILELOAD_FROMRESOURCE
)
575 D3DXF_FILELOADRESOURCE
*resource
= (D3DXF_FILELOADRESOURCE
*)source
;
577 dxfile_resource
.hModule
= resource
->hModule
;
578 dxfile_resource
.lpName
= resource
->lpName
;
579 dxfile_resource
.lpType
= resource
->lpType
;
580 dxfile_source
= &dxfile_resource
;
581 dxfile_options
= DXFILELOAD_FROMRESOURCE
;
583 else if (options
== D3DXF_FILELOAD_FROMMEMORY
)
585 D3DXF_FILELOADMEMORY
*memory
= (D3DXF_FILELOADMEMORY
*)source
;
587 dxfile_memory
.lpMemory
= memory
->lpMemory
;
588 dxfile_memory
.dSize
= memory
->dSize
;
589 dxfile_source
= &dxfile_memory
;
590 dxfile_options
= DXFILELOAD_FROMMEMORY
;
594 FIXME("Source type %u is not handled yet\n", options
);
598 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
600 return E_OUTOFMEMORY
;
602 object
->ID3DXFileEnumObject_iface
.lpVtbl
= &d3dx9_file_enum_object_vtbl
;
605 ret
= IDirectXFile_CreateEnumObject(file
->dxfile
, dxfile_source
, dxfile_options
, &dxfile_enum_object
);
609 HeapFree(GetProcessHeap(), 0, object
);
613 /* Fill enum object with top level data objects */
614 while (SUCCEEDED(ret
= IDirectXFileEnumObject_GetNextDataObject(dxfile_enum_object
, &data_object
)))
616 if (object
->nb_children
>= children_array_size
)
618 ID3DXFileData
**new_children
;
620 if (object
->children
)
622 children_array_size
*= 2;
623 new_children
= HeapReAlloc(GetProcessHeap(), 0, object
->children
,
624 sizeof(*object
->children
) * children_array_size
);
628 children_array_size
= 4;
629 new_children
= HeapAlloc(GetProcessHeap(), 0, sizeof(*object
->children
) * children_array_size
);
636 object
->children
= new_children
;
638 ret
= d3dx9_file_data_create((IDirectXFileObject
*)data_object
,
639 &object
->children
[object
->nb_children
]);
640 IUnknown_Release(data_object
);
643 object
->nb_children
++;
645 if (object
->children
)
647 ID3DXFileData
**new_children
;
649 new_children
= HeapReAlloc(GetProcessHeap(), 0, object
->children
,
650 sizeof(*object
->children
) * object
->nb_children
);
652 object
->children
= new_children
;
655 IDirectXFileEnumObject_Release(dxfile_enum_object
);
657 if (ret
!= DXFILEERR_NOMOREOBJECTS
)
658 WARN("Cannot get all top level data objects\n");
660 TRACE("Found %u children\n", object
->nb_children
);
662 *enum_object
= &object
->ID3DXFileEnumObject_iface
;
667 static HRESULT WINAPI
d3dx9_file_CreateSaveObject(ID3DXFile
*iface
, const void *data
,
668 D3DXF_FILESAVEOPTIONS options
, D3DXF_FILEFORMAT format
, ID3DXFileSaveObject
**save_object
)
670 FIXME("iface %p, data %p, options %#x, format %#x, save_object %p stub!\n",
671 iface
, data
, options
, format
, save_object
);
676 static HRESULT WINAPI
d3dx9_file_RegisterTemplates(ID3DXFile
*iface
, const void *data
, SIZE_T size
)
678 struct d3dx9_file
*file
= impl_from_ID3DXFile(iface
);
681 TRACE("iface %p, data %p, size %lu.\n", iface
, data
, size
);
683 ret
= IDirectXFile_RegisterTemplates(file
->dxfile
, (void *)data
, size
);
684 if (ret
!= DXFILE_OK
)
686 WARN("Error %#x\n", ret
);
687 return error_dxfile_to_d3dxfile(ret
);
693 static HRESULT WINAPI
d3dx9_file_RegisterEnumTemplates(ID3DXFile
*iface
, ID3DXFileEnumObject
*enum_object
)
695 FIXME("iface %p, enum_object %p stub!\n", iface
, enum_object
);
700 static const ID3DXFileVtbl d3dx9_file_vtbl
=
702 d3dx9_file_QueryInterface
,
705 d3dx9_file_CreateEnumObject
,
706 d3dx9_file_CreateSaveObject
,
707 d3dx9_file_RegisterTemplates
,
708 d3dx9_file_RegisterEnumTemplates
,
711 HRESULT WINAPI
D3DXFileCreate(ID3DXFile
**d3dxfile
)
713 struct d3dx9_file
*object
;
716 TRACE("d3dxfile %p.\n", d3dxfile
);
723 object
= HeapAlloc(GetProcessHeap(), 0, sizeof(*object
));
725 return E_OUTOFMEMORY
;
727 ret
= DirectXFileCreate(&object
->dxfile
);
730 HeapFree(GetProcessHeap(), 0, object
);
731 if (ret
== E_OUTOFMEMORY
)
736 object
->ID3DXFile_iface
.lpVtbl
= &d3dx9_file_vtbl
;
739 *d3dxfile
= &object
->ID3DXFile_iface
;