d3dx10: Share code for file data loading.

Signed-off-by: Piotr Caban <piotr@codeweavers.com>
This commit is contained in:
Piotr Caban 2022-05-25 19:00:20 +02:00 committed by Alexandre Julliard
parent c9e7db923a
commit 099fb5fad1
3 changed files with 48 additions and 69 deletions

View file

@ -84,38 +84,48 @@ static const ID3DX10DataLoaderVtbl memorydataloadervtbl =
memorydataloader_Destroy
};
static HRESULT WINAPI filedataloader_Load(ID3DX10DataLoader *iface)
HRESULT load_file(const WCHAR *path, void **data, DWORD *size)
{
struct asyncdataloader *loader = impl_from_ID3DX10DataLoader(iface);
DWORD size, read_len;
DWORD read_len;
HANDLE file;
void *data;
BOOL ret;
TRACE("iface %p.\n", iface);
/* Always buffer file contents, even if Load() was already called. */
file = CreateFileW(loader->u.file.path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
file = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (file == INVALID_HANDLE_VALUE)
return D3D10_ERROR_FILE_NOT_FOUND;
size = GetFileSize(file, NULL);
data = malloc(size);
if (!data)
*size = GetFileSize(file, NULL);
*data = malloc(*size);
if (!*data)
{
CloseHandle(file);
return E_OUTOFMEMORY;
}
ret = ReadFile(file, data, size, &read_len, NULL);
ret = ReadFile(file, *data, *size, &read_len, NULL);
CloseHandle(file);
if (!ret)
if (!ret || read_len != *size)
{
WARN("Failed to read file contents.\n");
free(data);
free(*data);
return E_FAIL;
}
return S_OK;
}
static HRESULT WINAPI filedataloader_Load(ID3DX10DataLoader *iface)
{
struct asyncdataloader *loader = impl_from_ID3DX10DataLoader(iface);
void *data;
DWORD size;
HRESULT hr;
TRACE("iface %p.\n", iface);
/* Always buffer file contents, even if Load() was already called. */
if (FAILED((hr = load_file(loader->u.file.path, &data, &size))))
return hr;
free(loader->data);
loader->data = data;

View file

@ -0,0 +1,19 @@
/*
* Copyright 2022 Piotr Caban
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
extern HRESULT load_file(const WCHAR *path, void **data, DWORD *size) DECLSPEC_HIDDEN;

View file

@ -23,6 +23,7 @@
#include "d3d10_1.h"
#include "d3dx10.h"
#include "wincodec.h"
#include "dxhelpers.h"
WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
@ -291,57 +292,6 @@ static DXGI_FORMAT get_d3dx10_dds_format(DXGI_FORMAT format)
return format;
}
static HRESULT load_file(const WCHAR *filename, void **buffer, DWORD *size)
{
HRESULT hr = S_OK;
DWORD bytes_read;
HANDLE file;
BOOL ret;
file = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
if (file == INVALID_HANDLE_VALUE)
{
hr = HRESULT_FROM_WIN32(GetLastError());
goto done;
}
*size = GetFileSize(file, NULL);
if (*size == INVALID_FILE_SIZE)
{
hr = HRESULT_FROM_WIN32(GetLastError());
goto done;
}
*buffer = malloc(*size);
if (!*buffer)
{
hr = E_OUTOFMEMORY;
goto done;
}
ret = ReadFile(file, *buffer, *size, &bytes_read, NULL);
if (!ret)
{
hr = HRESULT_FROM_WIN32(GetLastError());
goto done;
}
if (bytes_read != *size)
{
hr = E_FAIL;
goto done;
}
done:
if (FAILED(hr))
{
free(*buffer);
*buffer = NULL;
}
if (file != INVALID_HANDLE_VALUE)
CloseHandle(file);
return hr;
}
static HRESULT load_resource(HMODULE module, HRSRC res_info, void **buffer, DWORD *size)
{
HGLOBAL resource;
@ -398,8 +348,8 @@ HRESULT WINAPI D3DX10GetImageInfoFromFileW(const WCHAR *src_file, ID3DX10ThreadP
if (!src_file || !info)
return E_FAIL;
if (FAILED(load_file(src_file, &buffer, &size)))
return D3D10_ERROR_FILE_NOT_FOUND;
if (FAILED((hr = load_file(src_file, &buffer, &size))))
return hr;
hr = D3DX10GetImageInfoFromMemory(buffer, size, pump, info, result);
@ -612,8 +562,8 @@ HRESULT WINAPI D3DX10CreateTextureFromFileW(ID3D10Device *device, const WCHAR *s
if (!src_file || !texture)
return E_FAIL;
if (FAILED(load_file(src_file, &buffer, &size)))
return D3D10_ERROR_FILE_NOT_FOUND;
if (FAILED((hr = load_file(src_file, &buffer, &size))))
return hr;
hr = D3DX10CreateTextureFromMemory(device, buffer, size, load_info, pump, texture, hresult);