Skip to content

Commit

Permalink
hook GetSystemPaletteEntries
Browse files Browse the repository at this point in the history
  • Loading branch information
FunkyFr3sh committed Dec 14, 2024
1 parent 806924f commit 1c263ca
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
3 changes: 3 additions & 0 deletions inc/hook.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ typedef SHORT(WINAPI* GETASYNCKEYSTATEPROC)(int);
typedef int (WINAPI* GETDEVICECAPSPROC)(HDC, int);
typedef HFONT(WINAPI* CREATEFONTINDIRECTAPROC)(CONST LOGFONT*);
typedef HFONT(WINAPI* CREATEFONTAPROC)(int, int, int, int, int, DWORD, DWORD, DWORD, DWORD, DWORD, DWORD, DWORD, DWORD, LPCTSTR);
typedef UINT(WINAPI* GETSYSTEMPALETTEENTRIESPROC)(HDC, UINT, UINT, LPPALETTEENTRY);

typedef HMODULE(WINAPI* LOADLIBRARYAPROC)(LPCSTR);
typedef HMODULE(WINAPI* LOADLIBRARYWPROC)(LPCWSTR);
typedef HMODULE(WINAPI* LOADLIBRARYEXAPROC)(LPCSTR, HANDLE, DWORD);
Expand Down Expand Up @@ -116,6 +118,7 @@ extern GETASYNCKEYSTATEPROC real_GetAsyncKeyState;
extern GETDEVICECAPSPROC real_GetDeviceCaps;
extern CREATEFONTINDIRECTAPROC real_CreateFontIndirectA;
extern CREATEFONTAPROC real_CreateFontA;
extern GETSYSTEMPALETTEENTRIESPROC real_GetSystemPaletteEntries;
extern LOADLIBRARYAPROC real_LoadLibraryA;
extern LOADLIBRARYWPROC real_LoadLibraryW;
extern LOADLIBRARYEXAPROC real_LoadLibraryExA;
Expand Down
1 change: 1 addition & 0 deletions inc/winapi_hooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ int WINAPI fake_StretchDIBits(

HFONT WINAPI fake_CreateFontIndirectA(CONST LOGFONTA*);
HFONT WINAPI fake_CreateFontA(int, int, int, int, int, DWORD, DWORD, DWORD, DWORD, DWORD, DWORD, DWORD, DWORD, LPCTSTR);
UINT WINAPI fake_GetSystemPaletteEntries(HDC, UINT, UINT, LPPALETTEENTRY);

HMODULE WINAPI fake_LoadLibraryA(LPCSTR lpLibFileName);
HMODULE WINAPI fake_LoadLibraryW(LPCWSTR lpLibFileName);
Expand Down
11 changes: 9 additions & 2 deletions src/ddpalette.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ HRESULT ddp_GetEntries(
DWORD dwNumEntries,
LPPALETTEENTRY lpEntries)
{
for (int i = dwBase, x = 0; i < dwBase + dwNumEntries; i++, x++)
if (!lpEntries)
return DDERR_INVALIDPARAMS;

for (int i = dwBase, x = 0; i < dwBase + dwNumEntries && i < 256; i++, x++)
{
lpEntries[x].peRed = This->data_rgb[i].rgbRed;
lpEntries[x].peGreen = This->data_rgb[i].rgbGreen;
lpEntries[x].peBlue = This->data_rgb[i].rgbBlue;
lpEntries[x].peFlags = 0;
}

return DD_OK;
Expand All @@ -31,7 +35,10 @@ HRESULT ddp_SetEntries(
DWORD dwCount,
LPPALETTEENTRY lpEntries)
{
for (int i = dwStartingEntry, x = 0; i < dwStartingEntry + dwCount; i++, x++)
if (!lpEntries)
return DDERR_INVALIDPARAMS;

for (int i = dwStartingEntry, x = 0; i < dwStartingEntry + dwCount && i < 256; i++, x++)
{
This->data_bgr[i] = (lpEntries[x].peBlue << 16) | (lpEntries[x].peGreen << 8) | lpEntries[x].peRed;

Expand Down
2 changes: 2 additions & 0 deletions src/hook.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ GETASYNCKEYSTATEPROC real_GetAsyncKeyState = GetAsyncKeyState;
GETDEVICECAPSPROC real_GetDeviceCaps = GetDeviceCaps;
CREATEFONTINDIRECTAPROC real_CreateFontIndirectA = CreateFontIndirectA;
CREATEFONTAPROC real_CreateFontA = CreateFontA;
GETSYSTEMPALETTEENTRIESPROC real_GetSystemPaletteEntries = GetSystemPaletteEntries;
LOADLIBRARYAPROC real_LoadLibraryA = LoadLibraryA;
LOADLIBRARYWPROC real_LoadLibraryW = LoadLibraryW;
LOADLIBRARYEXAPROC real_LoadLibraryExA = LoadLibraryExA;
Expand Down Expand Up @@ -149,6 +150,7 @@ HOOKLIST g_hook_hooklist[] =
{ "StretchDIBits", (PROC)fake_StretchDIBits, (PROC*)&real_StretchDIBits, HOOK_SKIP_2 },
{ "GetDeviceCaps", (PROC)fake_GetDeviceCaps, (PROC*)&real_GetDeviceCaps, HOOK_LOCAL_ONLY },
{ "CreateFontA", (PROC)fake_CreateFontA, (PROC*)&real_CreateFontA, 0 },
{ "GetSystemPaletteEntries", (PROC)fake_GetSystemPaletteEntries, (PROC*)&real_GetSystemPaletteEntries, 0 },
{ "CreateFontIndirectA", (PROC)fake_CreateFontIndirectA, (PROC*)&real_CreateFontIndirectA, 0 },
{ "", NULL, NULL, 0 }
}
Expand Down
15 changes: 15 additions & 0 deletions src/winapi_hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "dllmain.h"
#include "hook.h"
#include "directinput.h"
#include "ddpalette.h"


BOOL WINAPI fake_GetCursorPos(LPPOINT lpPoint)
Expand Down Expand Up @@ -1348,6 +1349,20 @@ HFONT WINAPI fake_CreateFontA(
lpszFace);
}

UINT WINAPI fake_GetSystemPaletteEntries(HDC hdc, UINT iStart, UINT cEntries, LPPALETTEENTRY pPalEntries)
{
if (g_ddraw.ref && g_ddraw.bpp == 8 && WindowFromDC(hdc) == g_ddraw.hwnd)
{
if (g_ddraw.primary && g_ddraw.primary->palette)
{
ddp_GetEntries(g_ddraw.primary->palette, 0, iStart, cEntries, pPalEntries);
return cEntries - iStart;
}
}

return real_GetSystemPaletteEntries(hdc, iStart, cEntries, pPalEntries);
}

HMODULE WINAPI fake_LoadLibraryA(LPCSTR lpLibFileName)
{
HMODULE hmod_old = GetModuleHandleA(lpLibFileName);
Expand Down

0 comments on commit 1c263ca

Please sign in to comment.