From 954949d7cb96ed63334665ac99f4903369e716e3 Mon Sep 17 00:00:00 2001 From: Elisha Riedlinger Date: Sun, 23 Jun 2024 21:03:07 -0700 Subject: [PATCH] Create extra buffer for device data --- BuildNo.rc | 2 +- IDirectInputDeviceX.cpp | 25 ++++++++++++++----------- IDirectInputDeviceX.h | 21 +++++++++++++++++++++ 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/BuildNo.rc b/BuildNo.rc index 653fcd8..3f10354 100644 --- a/BuildNo.rc +++ b/BuildNo.rc @@ -1 +1 @@ -#define BUILD_NUMBER 69 +#define BUILD_NUMBER 70 diff --git a/IDirectInputDeviceX.cpp b/IDirectInputDeviceX.cpp index db3b4f3..88a7915 100644 --- a/IDirectInputDeviceX.cpp +++ b/IDirectInputDeviceX.cpp @@ -493,9 +493,7 @@ HRESULT m_IDirectInputDeviceX::GetDeviceData(DWORD cbObjectData, LPDIDEVICEOBJEC // Check the size of the array if (*pdwInOut > pdod.size()) { - pdod.resize(*pdwInOut); - - Logging::LogDebug() << __FUNCTION__ << " Update dod memory! " << *pdwInOut; + pdod.resize((*pdwInOut / 100 + 1) * 100); // Increase buffer by factors of 100 } HRESULT hr = ProxyInterface->GetDeviceData(sizeof(DIDEVICEOBJECTDATA), &pdod[0], pdwInOut, dwFlags); @@ -503,9 +501,13 @@ HRESULT m_IDirectInputDeviceX::GetDeviceData(DWORD cbObjectData, LPDIDEVICEOBJEC // Copy array if (SUCCEEDED(hr)) { - for (UINT x = 0; x < *pdwInOut; x++) + if (cbObjectData == sizeof(DIDEVICEOBJECTDATA_DX3)) + { + CopyDeviceData((DIDEVICEOBJECTDATA_DX3*)rgdod, pdod.data(), *pdwInOut); + } + else { - CopyMemory((void*)((DWORD)rgdod + (cbObjectData * x)), &pdod[x], cbObjectData); + CopyDeviceData((DIDEVICEOBJECTDATA*)rgdod, pdod.data(), *pdwInOut); } } @@ -814,16 +816,17 @@ HRESULT m_IDirectInputDeviceX::SendDeviceData(DWORD cbObjectData, LPCDIDEVICEOBJ // Check the size of the array if (*pdwInOut > pdod.size()) { - pdod.resize(*pdwInOut); - - Logging::LogDebug() << __FUNCTION__ << " Update dod memory! " << *pdwInOut; + pdod.resize((*pdwInOut / 100 + 1) * 100); // Increase buffer by factors of 100 } // Copy array - ZeroMemory(&pdod[0], sizeof(DIDEVICEOBJECTDATA) * pdod.size()); - for (UINT x = 0; x < *pdwInOut; x++) + if (cbObjectData == sizeof(DIDEVICEOBJECTDATA_DX3)) + { + CopyDeviceData(pdod.data(), (DIDEVICEOBJECTDATA_DX3*)rgdod, *pdwInOut); + } + else { - CopyMemory(&pdod[x], (void*)((DWORD)rgdod + (cbObjectData * x)), cbObjectData); + CopyDeviceData(pdod.data(), (DIDEVICEOBJECTDATA*)rgdod, *pdwInOut); } HRESULT hr = ProxyInterface->SendDeviceData(sizeof(DIDEVICEOBJECTDATA), &pdod[0], pdwInOut, fl); diff --git a/IDirectInputDeviceX.h b/IDirectInputDeviceX.h index 35f94b3..4efed24 100644 --- a/IDirectInputDeviceX.h +++ b/IDirectInputDeviceX.h @@ -49,6 +49,27 @@ class m_IDirectInputDeviceX : public AddressLookupTableDinputObject void InitializeEnumObjectData(); void SetEnumObjectDataFromFormat(LPCDIDATAFORMAT lpdf); + template + inline void CopyDeviceData(T* DestDod, DIDEVICEOBJECTDATA* SrcDod, DWORD dwNumRecords) + { + for (UINT x = 0; x < dwNumRecords; x++) + { + *DestDod = *(T*)SrcDod; + SrcDod = (DIDEVICEOBJECTDATA*)((DWORD)SrcDod + sizeof(DIDEVICEOBJECTDATA)); + DestDod = (T*)((DWORD)DestDod + sizeof(T)); + } + } + inline void CopyDeviceData(DIDEVICEOBJECTDATA* DestDod, DIDEVICEOBJECTDATA_DX3* SrcDod, DWORD dwNumRecords) + { + for (UINT x = 0; x < dwNumRecords; x++) + { + *(DIDEVICEOBJECTDATA_DX3*)DestDod = *SrcDod; + DestDod->uAppData = NULL; + SrcDod = (DIDEVICEOBJECTDATA_DX3*)((DWORD)SrcDod + sizeof(DIDEVICEOBJECTDATA_DX3)); + DestDod = (DIDEVICEOBJECTDATA*)((DWORD)DestDod + sizeof(DIDEVICEOBJECTDATA)); + } + } + // Wrapper interface functions inline REFIID GetWrapperType(DWORD DirectXVersion) {