Skip to content

Commit

Permalink
Add function level promotion detours for the CreateFont() family.
Browse files Browse the repository at this point in the history
Yeah, I suck at inventing new terminology. Just read the comment, it should make more sense then.
  • Loading branch information
nmlgc committed Jun 24, 2014
1 parent 552dc1f commit ee2741c
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 0 deletions.
60 changes: 60 additions & 0 deletions thcrap/src/promote.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Touhou Community Reliant Automatic Patcher
* Main DLL
*
* ----
*
* Win32 detour function level promotion.
*/

#include "thcrap.h"
#include "promote.h"

/// CreateFont() family (promoting to CreateFontIndirectExA)
/// -------------------
HFONT WINAPI promote_CreateFontA(
__in int cHeight,
__in int cWidth,
__in int cEscapement,
__in int cOrientation,
__in int cWeight,
__in DWORD bItalic,
__in DWORD bUnderline,
__in DWORD bStrikeOut,
__in DWORD iCharSet,
__in DWORD iOutPrecision,
__in DWORD iClipPrecision,
__in DWORD iQuality,
__in DWORD iPitchAndFamily,
__in_opt LPCSTR pszFaceName
)
{
CreateFontIndirectA_type target = (CreateFontIndirectA_type)detour_top(
"gdi32.dll", "CreateFontIndirectA", (FARPROC)CreateFontIndirectU
);
return lower_CreateFontA(target,
cHeight, cWidth, cEscapement, cOrientation, cWeight, bItalic,
bUnderline, bStrikeOut, iCharSet, iOutPrecision,
iClipPrecision, iQuality, iPitchAndFamily, pszFaceName
);
}

HFONT WINAPI promote_CreateFontIndirectA(
__in CONST LOGFONTA *lplf
)
{
CreateFontIndirectExA_type target = (CreateFontIndirectExA_type)detour_top(
"gdi32.dll", "CreateFontIndirectExA", (FARPROC)CreateFontIndirectExU
);
return lower_CreateFontIndirectA(target, lplf);
}
/// -------------------

void promote_mod_init(void)
{
detour_chain("gdi32.dll", 0,
"CreateFontIndirectA", promote_CreateFontIndirectA,
"CreateFontA", promote_CreateFontA,
NULL
);
}
58 changes: 58 additions & 0 deletions thcrap/src/promote.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Touhou Community Reliant Automatic Patcher
* Main DLL
*
* ----
*
* Win32 detour function level promotion.
*/

#pragma once

/**
* This module eliminates the redundancy in detouring certain Win32 function
* families - sets of functions that act as wrappers of decreasing levels
* around one functionality.
*
* This is done by detouring each individual wrapper with a function that
* merely converts and passes each level's parameters to the next one
* ("promoting"), terminating in an invocation of the detour chain for the
* lowest-level function.
*
* Other modules would then only have to detour a single function - the
* lowest-level one - to catch all calls to the whole family of functions,
* instead of detouring every function and duplicating all the promotion
* code required.
*
* For example, the CreateFont() family consists of three functions:
* CreateFont(), CreateFontIndirect() and CreateFontIndirectEx(). With the
* promotion module, it's enough to detour CreateFontIndirectEx() to catch
* all three functions.
*/

/// CreateFont() family (promoting to CreateFontIndirectExA)
/// -------------------
HFONT WINAPI promote_CreateFontA(
__in int cHeight,
__in int cWidth,
__in int cEscapement,
__in int cOrientation,
__in int cWeight,
__in DWORD bItalic,
__in DWORD bUnderline,
__in DWORD bStrikeOut,
__in DWORD iCharSet,
__in DWORD iOutPrecision,
__in DWORD iClipPrecision,
__in DWORD iQuality,
__in DWORD iPitchAndFamily,
__in_opt LPCSTR pszFaceName
);

HFONT WINAPI promote_CreateFontIndirectA(
__in CONST LOGFONTA *lplf
);
/// -------------------

// Because we want these to be as low as possible... Nifty.
void promote_mod_init(void);
4 changes: 4 additions & 0 deletions thcrap/thcrap.def
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ EXPORTS
plugins_load
plugins_close

; Detour promotion
; ----------------
promote_mod_init

; Hot-repatching
; --------------
repatch_mod_init
Expand Down
2 changes: 2 additions & 0 deletions thcrap/thcrap.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<ClCompile Include="src\patchfile.c" />
<ClCompile Include="src\pe.c" />
<ClCompile Include="src\plugin.c" />
<ClCompile Include="src\promote.c" />
<ClCompile Include="src\repatch.c" />
<ClCompile Include="src\sha256.c">
<PrecompiledHeader>NotUsing</PrecompiledHeader>
Expand Down Expand Up @@ -88,6 +89,7 @@
<ClInclude Include="src\patchfile.h" />
<ClInclude Include="src\pe.h" />
<ClInclude Include="src\plugin.h" />
<ClInclude Include="src\promote.h" />
<ClInclude Include="src\repatch.h" />
<ClInclude Include="src\sha256.h" />
<ClInclude Include="src\inject.h" />
Expand Down
6 changes: 6 additions & 0 deletions thcrap/thcrap.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@
<ClCompile Include="src\zip.c">
<Filter>Source files</Filter>
</ClCompile>
<ClCompile Include="src\promote.c">
<Filter>Source files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\binhack.h">
Expand Down Expand Up @@ -163,6 +166,9 @@
<ClInclude Include="src\zip.h">
<Filter>Source files</Filter>
</ClInclude>
<ClInclude Include="src\promote.h">
<Filter>Source files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="thcrap.def">
Expand Down

0 comments on commit ee2741c

Please sign in to comment.