Skip to content

Commit

Permalink
Fix CoD4 native functions / restructuring / cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kungfooman committed Apr 18, 2014
1 parent c68dca1 commit e1f16ce
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 108 deletions.
13 changes: 13 additions & 0 deletions cracking.cpp
Expand Up @@ -118,4 +118,17 @@ int cracking_write_hex(int address, char *hex)
ptr[i] = buffer[i];

return bytes;
}


cHook::cHook(int from, int to) {
this->from = from;
this->to = to;
}
void cHook::hook() {
memcpy((void *)oldCode, (void *)from, 5);
cracking_hook_function(from, to);
}
void cHook::unhook() {
memcpy((void *)from, (void *)oldCode, 5);
}
27 changes: 20 additions & 7 deletions cracking.hpp
Expand Up @@ -5,18 +5,31 @@
extern "C" {
#endif

#include <string.h>
#include <string.h>

int cracking_nop(int from, int to);
void cracking_hook_function(int from, int to);
void cracking_hook_call(int from, int to);
int cracking_write_hex(int address, char *hex);
int cracking_nop(int from, int to);
void cracking_hook_function(int from, int to);
void cracking_hook_call(int from, int to);
int cracking_write_hex(int address, char *hex);

int singleHexToNumber(char hexchar);
int hexToBuffer(char *hex, char *buffer, int bufferLen);
int singleHexToNumber(char hexchar);
int hexToBuffer(char *hex, char *buffer, int bufferLen);

#ifdef __cplusplus
}
#endif

#ifdef __cplusplus
class cHook
{
public:
int from;
int to;
unsigned char oldCode[5];
cHook(int from, int to);
void hook();
void unhook();
};
#endif

#endif
37 changes: 35 additions & 2 deletions gsc.cpp
Expand Up @@ -15,8 +15,41 @@
Scr_GetFunction_t Scr_GetFunction = (Scr_GetFunction_t)0x08117CB2;
Scr_GetMethod_t Scr_GetMethod = (Scr_GetMethod_t)0x08117DEA;
#elif COD_VERSION == COD4_1_7
Scr_GetFunction_t Scr_GetFunction = (Scr_GetFunction_t)0x080BD238;
Scr_GetMethod_t Scr_GetMethod = (Scr_GetMethod_t)0x080BFEF4;
//Scr_GetFunction_t Scr_GetFunction = (Scr_GetFunction_t)0x080BD238;
//Scr_GetMethod_t Scr_GetMethod = (Scr_GetMethod_t)0x080BFEF4;

cHook *hook_Scr_GetFunction;
cHook *hook_Scr_GetMethod;

Scr_FunctionCall Scr_GetFunction(const char **fname, int *fdev) {
//printf("CoD4 Scr_GetFunction: fdev=%d fname=%s\n", *fdev, *fname);

hook_Scr_GetFunction->unhook();
int (*sig)(const char **fname, int *fdev);
*(int *)&sig = hook_Scr_GetFunction->from;
int m = sig(fname, fdev);
hook_Scr_GetFunction->hook();
/*
when I use the real return-type instead of int (*sig), this errors occurs:
##### LINK libcod4_1_7.so #####
objects_cod4_1_7/libcod.opp: In function `Scr_GetFunction_CoD4(char const**, int*)':
libcod.cpp:(.text+0x132b): undefined reference to `sig(char const**, int*)'
/usr/bin/ld: objects_cod4_1_7/libcod.opp: relocation R_386_GOTOFF against undefined hidden symbol `_Z3sigPPKcPi' can not be used when making a shared object
/usr/bin/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status
k_cod4_nodlzom@Debian-70-wheezy-64-LAMP:~/libcod$ rm objects_cod4_1_7/libcod.opp
*/
return (Scr_FunctionCall)m;
}
Scr_MethodCall Scr_GetMethod(const char **fname, int *fdev) {
//printf("CoD4 Scr_GetMethod: fdev=%d fname=%s\n", *fdev, *fname);
hook_Scr_GetMethod->unhook();
int (*sig)(const char **fname, int *fdev);
*(int *)&sig = hook_Scr_GetMethod->from;
int m = sig(fname, fdev);
hook_Scr_GetMethod->hook();
return (Scr_MethodCall)m;
}
#else
#warning Scr_GetFunction_t Scr_GetFunction = (Scr_GetFunction_t)NULL;
#warning Scr_GetMethod_t Scr_GetMethod = (Scr_GetMethod_t)NULL;
Expand Down
4 changes: 2 additions & 2 deletions gsc.hpp
Expand Up @@ -217,7 +217,7 @@ typedef struct {

typedef Scr_FunctionCall (*Scr_GetFunction_t)(const char **fname, int *fdev);

Scr_FunctionCall Scr_GetCustomFunction(const char **fname, int *fdev);
Scr_FunctionCall Scr_GetCustomFunction(const char **fname, int *fdev); // could be made obsolete to remove the cracking_hook_call()-stuff

// methods
typedef void (*Scr_MethodCall)(int);
Expand All @@ -230,7 +230,7 @@ typedef struct {

typedef Scr_MethodCall (*Scr_GetMethod_t)(const char**, int*);

Scr_MethodCall Scr_GetCustomMethod(const char **fname, int *fdev);
Scr_MethodCall Scr_GetCustomMethod(const char **fname, int *fdev); // could be made obsolete to remove the cracking_hook_call()-stuff

#ifdef __cplusplus
}
Expand Down
113 changes: 16 additions & 97 deletions libcod.cpp
Expand Up @@ -5,9 +5,11 @@
#include <sys/mman.h> // mprotect
#include <execinfo.h> // stacktrace

#include "cracking.hpp"
#include "gsc.hpp" /* cdecl_injected_closer() cdecl_cod2_player_damage_new() */
#include "server.hpp" /* startServerAsThread() */


#pragma GCC visibility push(hidden)

/*
Expand Down Expand Up @@ -1071,32 +1073,6 @@ int hook_player_eject(int player) // player 0 = 0x08679380 + 0x11c = 0x0867949c
return 0;
}

class cHook
{
public:
int from;
int to;
unsigned char oldCode[5];
cHook(int from, int to)
{
this->from = from;
this->to = to;
}

void hook()
{
memcpy((void *)oldCode, (void *)from, 5);
cracking_hook_function(from, to);
}

void unhook()
{

memcpy((void *)from, (void *)oldCode, 5);
}
};


#define _DWORD int
#define __cdecl
#if COD_VERSION == COD1_1_5
Expand Down Expand Up @@ -1809,80 +1785,19 @@ class cCallOfDuty2Pro

#if COD_VERSION == COD4_1_7
cracking_hook_function(0x0804AB6C, (int)hook_recvfrom);
return;

#endif

// NEEDED FOR ZOMBOTS/BOTZOMS???
// lol, i dont know why, but this made sniper/rifle-shots stick to 90 damage, very annoying
#if 0
// SET binary.damage TO c.damage
{
int from = 0x08101C58;
int to = (int)cdecl_cod2_player_damage_new;
int relative = to - (from+5); // +5 is the position of next opcode
memset((void *)from, 0xE9, 1); // JMP-OPCODE
memcpy((void *)(from+1), &relative, 4); // set relative address with endian
}
#endif
if (0) cracking_hook_function(0x08101C58, (int)cdecl_cod2_player_damage_new); // SET binary.damage TO c.damage

// SET calc
{
#if 0
int from = 0x08078FB2;
int to = (int)cdecl_calc_hash_of_string;
int relative = to - (from+5); // +5 is the position of next opcode
memset((void *)from, 0xE9, 1); // JMP-OPCODE
memcpy((void *)(from+1), &relative, 4); // set relative address with endian
#endif
}

// radiant keys
{
#if 0
int from = 0x0807F840;
int to = (int)cdecl_sub_807F840;
int relative = to - (from+5); // +5 is the position of next opcode
memset((void *)from, 0xE9, 1); // JMP-OPCODE
memcpy((void *)(from+1), &relative, 4); // set relative address with endian
#endif
}

// gsc_cast_to_bool
{
#if 0
int from = 0x0807D288;
int to = (int)cdecl_gsc_cast_to_bool;
int relative = to - (from+5); // +5 is the position of next opcode
memset((void *)from, 0xE9, 1); // JMP-OPCODE
memcpy((void *)(from+1), &relative, 4); // set relative address with endian
#endif
}

// gsc_set_field_of_struct
{
#if 0
int from = 0x0807C6F8;
int to = (int)cdecl_gsc_set_field_of_struct;
int relative = to - (from+5); // +5 is the position of next opcode
memset((void *)from, 0xE9, 1); // JMP-OPCODE
memcpy((void *)(from+1), &relative, 4); // set relative address with endian
#endif
}

// gsc_new_variable_807AB64
{
#if 0
int from = 0x0807AB64;
int to = (int)gsc_new_variable_807AB64;
int relative = to - (from+5); // +5 is the position of next opcode
memset((void *)from, 0xE9, 1); // JMP-OPCODE
memcpy((void *)(from+1), &relative, 4); // set relative address with endian
#endif
}

// BSP HOOK for fraction
if (0)
cracking_hook_function(0x0805B894, (int)trace_calc_fraction_805B894);
if (0) cracking_hook_function(0x08078FB2, (int)cdecl_calc_hash_of_string);
if (0) cracking_hook_function(0x0807F840, (int)cdecl_sub_807F840); // radiant keys
if (0) cracking_hook_function(0x0807D288, (int)cdecl_gsc_cast_to_bool);
if (0) cracking_hook_function(0x0807C6F8, (int)cdecl_gsc_set_field_of_struct);
if (0) cracking_hook_function(0x0807AB64, (int)gsc_new_variable_807AB64);
if (0) cracking_hook_function(0x0805B894, (int)trace_calc_fraction_805B894); // BSP HOOK for fraction

if (0)
{
Expand Down Expand Up @@ -2014,8 +1929,12 @@ class cCallOfDuty2Pro
cracking_hook_call(0x08070BE7, (int)Scr_GetCustomFunction);
cracking_hook_call(0x08070E0B, (int)Scr_GetCustomMethod);
#elif COD_VERSION == COD4_1_7
cracking_hook_call(0x08147664, (int)Scr_GetCustomFunction);
cracking_hook_call(0x081467D1, (int)Scr_GetCustomMethod);
extern cHook *hook_Scr_GetFunction;
extern cHook *hook_Scr_GetMethod;
hook_Scr_GetFunction = new cHook(0x080bd238, (int)Scr_GetCustomFunction);
hook_Scr_GetMethod = new cHook(0x080bfef4, (int)Scr_GetCustomMethod);
hook_Scr_GetFunction->hook();
hook_Scr_GetMethod->hook();
#endif

#if COD_VERSION == COD2_1_0 || COD_VERSION == COD2_1_2 || COD_VERSION == COD2_1_3
Expand Down

0 comments on commit e1f16ce

Please sign in to comment.