From 099fb5fad124186d56a263d987904e0109c9b4df Mon Sep 17 00:00:00 2001 From: Piotr Caban Date: Wed, 25 May 2022 19:00:20 +0200 Subject: [PATCH] d3dx10: Share code for file data loading. Signed-off-by: Piotr Caban --- dlls/d3dx10_43/async.c | 38 ++++++++++++++++++----------- dlls/d3dx10_43/dxhelpers.h | 19 +++++++++++++++ dlls/d3dx10_43/texture.c | 60 ++++------------------------------------------ 3 files changed, 48 insertions(+), 69 deletions(-) create mode 100644 dlls/d3dx10_43/dxhelpers.h diff --git a/dlls/d3dx10_43/async.c b/dlls/d3dx10_43/async.c index 5a8ba77db87..21fa437ac73 100644 --- a/dlls/d3dx10_43/async.c +++ b/dlls/d3dx10_43/async.c @@ -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; diff --git a/dlls/d3dx10_43/dxhelpers.h b/dlls/d3dx10_43/dxhelpers.h new file mode 100644 index 00000000000..9afc9bd901a --- /dev/null +++ b/dlls/d3dx10_43/dxhelpers.h @@ -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; diff --git a/dlls/d3dx10_43/texture.c b/dlls/d3dx10_43/texture.c index 41f9723de6b..56258fdd60d 100644 --- a/dlls/d3dx10_43/texture.c +++ b/dlls/d3dx10_43/texture.c @@ -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); -- 2.11.4.GIT