Skip to content

Commit

Permalink
Detours are now structs, public: not needed
Browse files Browse the repository at this point in the history
  • Loading branch information
emd4600 committed Jun 30, 2019
1 parent 2c2f05c commit 6369bb2
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CppRevEng.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@
</ItemGroup>
<ItemGroup>
<None Include=".gitignore" />
<None Include="example\README.md" />
<None Include="LICENSE" />
<None Include="README.md" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
Expand Down
2 changes: 2 additions & 0 deletions CppRevEng.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,7 @@
<ItemGroup>
<None Include=".gitignore" />
<None Include="README.md" />
<None Include="example\README.md" />
<None Include="LICENSE" />
</ItemGroup>
</Project>
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ This header file defines 3 macros:
- `member_detour`: for non-static, non-virtual methods.
- `virtual_detour`: for non-static, virtual methods.
These macros are class declarations, so you must follow them with a {}; - don't forget the final semicolon!
Also, use the `public:` keyword when defining the detoured method.
These macros are struct declarations, so you must follow them with a {}; - don't forget the final semicolon!
All macros take at least two parameters:
- `name`: A unique name to identify the detour object. You will use it to attach the detour.
Expand All @@ -123,7 +122,6 @@ All macros take at least two parameters:
Imagine you want to detour a static function that returns a float, and takes a const char* as parameter. You would do it like this:
```cpp
static_detour(MyDetour1, float(const char*)) {
public:
float detoured(const char* pString) {
if (pString[0] == '_') return -1.0f;
else {
Expand All @@ -145,7 +143,6 @@ protected:
}

member_detour(MyDetour2, ClassManager, void()) {
public:
void detoured() {
MessageBox("Objects are being deleted");
// You can access the public and protected fields:
Expand All @@ -166,13 +163,12 @@ protected:
}
virtual_detour(MyDetour3, cCreature, ICombatant, void(float, float)) {
public:
void detoured(float strength, float distance) {
// I want to be immortal!
if (mHealthPoints < 10) mHealthPoints += 1000.0f;
original_function(strength, distance);
}
}
};
```

### Attaching/detaching detours
Expand Down
17 changes: 8 additions & 9 deletions inc/CppRevEng.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include <detours.h>

/*
This class is used to simplify the process of detouring functions. When using DLL injection,
This header is used to simplify the process of detouring functions. When using DLL injection,
detouring is the most important technique: it allows you to hook the executable methods and
override them with your own functionality.
Expand All @@ -33,8 +33,7 @@ This header file defines 3 macros:
- `member_detour`: for non-static, non-virtual methods.
- `virtual_detour`: for non-static, virtual methods.
These macros are class declarations, so you must follow them with a {}; - don't forget the final semicolon!
Also, use the `public:` keyword when defining the detoured method.
These macros are struct declarations, so you must follow them with a {}; - don't forget the final semicolon!
All macros take at least two parameters:
- `name`: A unique name to identify the detour object. You will use it to attach the detour.
Expand All @@ -44,7 +43,6 @@ All macros take at least two parameters:
Imagine you want to detour a static function that returns a float, and takes a const char* as parameter. You would do it like this:
static_detour(MyDetour1, float(const char*)) {
public:
float detoured(const char* pString) {
if (pString[0] == '_') return -1.0f;
else {
Expand All @@ -65,7 +63,6 @@ class ClassManager {
}
member_detour(MyDetour2, ClassManager, void()) {
public:
void detoured() {
MessageBox("Objects are being deleted");
// You can access the public and protected fields:
Expand All @@ -85,13 +82,12 @@ class cCreature : public IObject, public ICombatant {
}
virtual_detour(MyDetour3, cCreature, ICombatant, void(float, float)) {
public:
void detoured(float strength, float distance) {
// I want to be immortal!
if (mHealthPoints < 10) mHealthPoints += 1000.0f;
original_function(strength, distance);
}
}
};
## Attaching/detaching detours
Expand Down Expand Up @@ -180,7 +176,7 @@ class member_detour_;
/// @tparam Result The return type of the function to detour, can be void.
/// @tparam Parameters The parameter types of the function to detour, can be empty.
template<typename DetourClass, typename Result, typename ... Parameters>
class static_detour_<DetourClass, Result(Parameters...)>
struct static_detour_<DetourClass, Result(Parameters...)>
{
public:
/// The type of function pointer used to keep track of the original function.
Expand Down Expand Up @@ -208,13 +204,16 @@ class static_detour_<DetourClass, Result(Parameters...)>
}
};

/// The object used for detouring non-static functions.
/// This is internal, for real usage check member_detour or virtual_detour macros.
///
/// @tparam DetourClass The name of the class that is extending this object and is being used to hold the detour.
/// @tparam BaseClass The name of the class this object is simulating, must contain the method (or any of its superclasses must)
/// @tparam VirtualClass The name of the base class where the method was defined. For non-virtual methods, this must be the same as BaseClass.
/// @tparam Result The return type of the function to detour, can be void.
/// @tparam Parameters The parameter types of the function to detour, can be empty.
template<typename DetourClass, typename BaseClass, typename VirtualClass, typename Result, typename ... Arguments>
class member_detour_<DetourClass, BaseClass, VirtualClass, Result(Arguments...)> : public BaseClass
struct member_detour_<DetourClass, BaseClass, VirtualClass, Result(Arguments...)> : public BaseClass
{
public:
/// The type of function pointer used to keep track of the original function.
Expand Down
2 changes: 1 addition & 1 deletion inc/CppRevEngBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ inline void InitCppRevEng() {
// namespaceName: The name of the namespace that stores the addresses.
// name: The name of the function declared in the header.
// returnType: The return type of the function, can be void.
#define RedirectStaticMethod_noargs(namespaceName, name, returnTyp) returnType namespaceName::name() { return ((returnType(*)()) GetAddress(namespaceName, name))(); }
#define RedirectStaticMethod_noargs(namespaceName, name, returnType) returnType namespaceName::name() { return ((returnType(*)()) GetAddress(namespaceName, name))(); }

// Defines the body of a redirected static function whose address is stored in an address namespace.
// This should only be used if it returns a big struct (i.e. sizeof() > 8) by value. Examples:
Expand Down

0 comments on commit 6369bb2

Please sign in to comment.