Skip to content

Commit

Permalink
hook AVIStreamGetFrameOpen
Browse files Browse the repository at this point in the history
  • Loading branch information
FunkyFr3sh committed Dec 20, 2024
1 parent 7025517 commit d83e9e4
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ TARGET ?= ddraw.dll

LDFLAGS ?= -Wl,--enable-stdcall-fixup -s -static -shared
CFLAGS ?= -Iinc -O2 -Wall -std=c99
LIBS = -lgdi32 -lwinmm -lole32 -lmsimg32
LIBS = -lgdi32 -lwinmm -lole32 -lmsimg32 -lavifil32

COMMIT := $(shell git describe --match=NeVeRmAtCh --always --dirty || echo UNKNOWN)
BRANCH := $(shell git rev-parse --abbrev-ref HEAD || echo UNKNOWN)
Expand Down
10 changes: 5 additions & 5 deletions cnc-ddraw.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<AdditionalDependencies>winmm.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>avifil32.lib;winmm.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<ModuleDefinitionFile>exports.def</ModuleDefinitionFile>
</Link>
<PostBuildEvent>
Expand Down Expand Up @@ -265,7 +265,7 @@ echo #define GIT_BRANCH "%GIT_BRANCH%" &gt;&gt; %DST_FILE%
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>winmm.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>avifil32.lib;winmm.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<ModuleDefinitionFile>exports.def</ModuleDefinitionFile>
</Link>
<PostBuildEvent>
Expand Down Expand Up @@ -307,7 +307,7 @@ echo #define GIT_BRANCH "%GIT_BRANCH%" &gt;&gt; %DST_FILE%
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>winmm.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>avifil32.lib;winmm.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<ModuleDefinitionFile>exports.def</ModuleDefinitionFile>
</Link>
<PostBuildEvent>
Expand Down Expand Up @@ -349,7 +349,7 @@ echo #define GIT_BRANCH "%GIT_BRANCH%" &gt;&gt; %DST_FILE%
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>winmm.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>avifil32.lib;winmm.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<ModuleDefinitionFile>exports.def</ModuleDefinitionFile>
</Link>
<PostBuildEvent>
Expand Down Expand Up @@ -391,7 +391,7 @@ echo #define GIT_BRANCH "%GIT_BRANCH%" &gt;&gt; %DST_FILE%
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>winmm.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>avifil32.lib;winmm.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<ModuleDefinitionFile>exports.def</ModuleDefinitionFile>
</Link>
<PostBuildEvent>
Expand Down
5 changes: 5 additions & 0 deletions inc/IDirectDrawSurface.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
#include "IDirectDraw.h"


typedef struct DDBITMAPINFO{
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[256];
} DDBITMAPINFO;

struct IDirectDrawSurfaceImpl;
struct IDirectDrawSurfaceImplVtbl;

Expand Down
4 changes: 4 additions & 0 deletions inc/hook.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define HOOK_H

#include <windows.h>
#include <vfw.h>


#define HOOK_SKIP_2 0x00000001l
Expand Down Expand Up @@ -83,6 +84,8 @@ typedef HRESULT(WINAPI* COCREATEINSTANCEPROC)(REFCLSID, LPUNKNOWN, DWORD, REFIID
typedef MCIERROR(WINAPI* MCISENDCOMMANDAPROC)(MCIDEVICEID, UINT, DWORD_PTR, DWORD_PTR);
typedef LPTOP_LEVEL_EXCEPTION_FILTER(WINAPI* SETUNHANDLEDEXCEPTIONFILTERPROC)(LPTOP_LEVEL_EXCEPTION_FILTER);

typedef PGETFRAME (WINAPI* AVISTREAMGETFRAMEOPENPROC)(PAVISTREAM, LPBITMAPINFOHEADER);

extern GETCURSORPOSPROC real_GetCursorPos;
extern CLIPCURSORPROC real_ClipCursor;
extern SHOWCURSORPROC real_ShowCursor;
Expand Down Expand Up @@ -140,6 +143,7 @@ extern GETVERSIONEXAPROC real_GetVersionExA;
extern COCREATEINSTANCEPROC real_CoCreateInstance;
extern MCISENDCOMMANDAPROC real_mciSendCommandA;
extern SETUNHANDLEDEXCEPTIONFILTERPROC real_SetUnhandledExceptionFilter;
extern AVISTREAMGETFRAMEOPENPROC real_AVIStreamGetFrameOpen;

extern BOOL g_hook_active;
extern HOOKLIST g_hook_hooklist[];
Expand Down
3 changes: 3 additions & 0 deletions inc/winapi_hooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define WINAPI_HOOKS_H

#include <windows.h>
#include <vfw.h>


BOOL WINAPI fake_GetCursorPos(LPPOINT lpPoint);
Expand Down Expand Up @@ -93,4 +94,6 @@ MCIERROR WINAPI fake_mciSendCommandA(MCIDEVICEID IDDevice, UINT uMsg, DWORD_PTR
LPTOP_LEVEL_EXCEPTION_FILTER WINAPI fake_SetUnhandledExceptionFilter(
LPTOP_LEVEL_EXCEPTION_FILTER lpTopLevelExceptionFilter);

PGETFRAME WINAPI fake_AVIStreamGetFrameOpen(PAVISTREAM pavi, LPBITMAPINFOHEADER lpbiWanted);

#endif
3 changes: 1 addition & 2 deletions src/ddsurface.c
Original file line number Diff line number Diff line change
Expand Up @@ -1467,10 +1467,9 @@ HRESULT dd_CreateSurface(

DWORD aligned_width = dst_surface->pitch / dst_surface->bytes_pp;

DWORD bmi_size = sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256;
DWORD bmp_size = dst_surface->pitch * (dst_surface->height + g_config.guard_lines);

dst_surface->bmi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bmi_size);
dst_surface->bmi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DDBITMAPINFO));
dst_surface->bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
dst_surface->bmi->bmiHeader.biWidth = aligned_width;
dst_surface->bmi->bmiHeader.biHeight = -((int)dst_surface->height + g_config.guard_lines);
Expand Down
8 changes: 8 additions & 0 deletions src/hook.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ GETVERSIONEXAPROC real_GetVersionExA = GetVersionExA;
COCREATEINSTANCEPROC real_CoCreateInstance = CoCreateInstance;
MCISENDCOMMANDAPROC real_mciSendCommandA = mciSendCommandA;
SETUNHANDLEDEXCEPTIONFILTERPROC real_SetUnhandledExceptionFilter = SetUnhandledExceptionFilter;
AVISTREAMGETFRAMEOPENPROC real_AVIStreamGetFrameOpen = AVIStreamGetFrameOpen;

#if (_WIN32_WINNT >= _WIN32_WINNT_WIN2K)
SETWINDOWLONGWPROC real_SetWindowLongW = SetWindowLongW;
Expand Down Expand Up @@ -142,6 +143,13 @@ HOOKLIST g_hook_hooklist[] =
{ "", NULL, NULL, 0 }
}
},
{
"Avifil32.dll",
{
{ "AVIStreamGetFrameOpen", (PROC)fake_AVIStreamGetFrameOpen, (PROC*)&real_AVIStreamGetFrameOpen, 0 },
{ "", NULL, NULL, 0 }
}
},
{
"dinput.dll",
{
Expand Down
25 changes: 25 additions & 0 deletions src/winapi_hooks.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <windows.h>
#include <windowsx.h>
#include <math.h>
#include <vfw.h>
#include "debug.h"
#include "config.h"
#include "dd.h"
Expand Down Expand Up @@ -2174,3 +2175,27 @@ LPTOP_LEVEL_EXCEPTION_FILTER WINAPI fake_SetUnhandledExceptionFilter(
return old;
//return real_SetUnhandledExceptionFilter(lpTopLevelExceptionFilter);
}

PGETFRAME WINAPI fake_AVIStreamGetFrameOpen(PAVISTREAM pavi, LPBITMAPINFOHEADER lpbiWanted)
{
if (g_ddraw.ref && g_ddraw.primary && (!lpbiWanted || (DWORD)lpbiWanted == 1))
{
DDBITMAPINFO bmi;
memcpy(&bmi, g_ddraw.primary->bmi, sizeof(DDBITMAPINFO));

bmi.bmiHeader.biHeight = 0;
bmi.bmiHeader.biWidth = 0;

if (g_ddraw.bpp == 8 && g_ddraw.primary->palette)
{
memcpy(&bmi.bmiColors[0], g_ddraw.primary->palette->data_rgb, sizeof(bmi.bmiColors));
}

PGETFRAME result = real_AVIStreamGetFrameOpen(pavi, (LPBITMAPINFOHEADER)&bmi);

if (result)
return result;
}

return real_AVIStreamGetFrameOpen(pavi, lpbiWanted);
}

0 comments on commit d83e9e4

Please sign in to comment.