Code:
struct tStatus
{
bool bPatched;
bool bSaved;
};
template <typename Type>
struct tValues
{
unsigned long Adr;
Type OffValue;
Type OnValue;
};
template <typename Type>
class tMemPatch
{
public:
tValues<Type> Values;
tStatus Status;
tMemPatch( unsigned long /* used unsigned long instead of DWORD -> i don't need to include windows.h */ Adr, Type value )
{
Values.Adr = Adr;
Values.OnValue = value;
}
void Patch( )
{
if( !Status.bSaved )
{
Values.OffValue = *(Type*)Values.Adr;
Status.bSaved = true;
}
else if( !Status.bPatched )
{
*(Type*)Values.Adr = Values.OnValue;
Status.bPatched = true;
}
}
void Restore( )
{
if( Status.bSaved && Status.bPatched )
{
*(Type*)Values.Adr = Values.OffValue;
Status.bPatched = false;
}
}
};
Code:
tMemPatch <float> PatchName( 0xAB844F /* address */, 3 /* value */ ); // to modify the address: PatchName.Patch( ); // to restore the old value: PatchName.Restore( );