From 81851a402212c22c61155fdc86db464187858c31 Mon Sep 17 00:00:00 2001 From: Alexey Ilin Date: Wed, 8 Feb 2017 07:05:02 +0300 Subject: [PATCH] Ship weapon functions as a separate module Also remove doubtful (and broken on 1.2) functions: setdefaultweapon() ingoreweapon() resetignoreweapons() --- .gitignore | 2 + config.hpp | 1 + doit.sh | 3 + gsc.cpp | 40 +++--- gsc.hpp | 1 + gsc_utils.cpp | 372 ------------------------------------------------ gsc_utils.hpp | 24 ---- gsc_weapons.cpp | 244 +++++++++++++++++++++++++++++++ gsc_weapons.hpp | 38 +++++ libcod.cpp | 5 - 10 files changed, 309 insertions(+), 421 deletions(-) create mode 100644 gsc_weapons.cpp create mode 100644 gsc_weapons.hpp diff --git a/.gitignore b/.gitignore index ddb386f..419abb1 100755 --- a/.gitignore +++ b/.gitignore @@ -20,5 +20,7 @@ !gsc_player.hpp !gsc_utils.cpp !gsc_utils.hpp +!gsc_weapons.cpp +!gsc_weapons.hpp !libcod.cpp !README.md \ No newline at end of file diff --git a/config.hpp b/config.hpp index 8ed37bf..31766d6 100644 --- a/config.hpp +++ b/config.hpp @@ -8,6 +8,7 @@ #define COMPILE_BOTS 1 #define COMPILE_MEMORY 1 #define COMPILE_UTILS 1 +#define COMPILE_WEAPONS 1 // RATE LIMITER #define COMPILE_RATELIMITER 1 diff --git a/doit.sh b/doit.sh index 19b18ac..05d7a87 100755 --- a/doit.sh +++ b/doit.sh @@ -100,6 +100,9 @@ $cc $options $constants -c gsc_player.cpp -o objects_$1/gsc_player.opp echo "##### COMPILE $1 GSC_UTILS.CPP #####" $cc $options $constants -c gsc_utils.cpp -o objects_$1/gsc_utils.opp +echo "##### COMPILE $1 GSC_WEAPONS.CPP #####" +$cc $options $constants -c gsc_weapons.cpp -o objects_$1/gsc_weapons.opp + if [ -d extra ]; then echo "##### COMPILE $1 EXTRAS #####" cd extra diff --git a/gsc.cpp b/gsc.cpp index 2e192ba..2dc7042 100644 --- a/gsc.cpp +++ b/gsc.cpp @@ -196,30 +196,30 @@ scr_function_t scriptFunctions[] = {"dlopen", gsc_dlopen, 0}, {"dlsym", gsc_dlsym, 0}, {"dlclose", gsc_dlclose, 0}, - {"resetignoredweapons", gsc_utils_resetignoredweapons, 0}, - {"ignoreweapon", gsc_utils_ignoreweapon, 0}, - {"setdefaultweapon", gsc_utils_setdefaultweapon, 0}, - {"getweaponmaxammo", gsc_utils_getweaponmaxammo, 0}, - {"getweaponclipsize", gsc_utils_getweaponclipsize, 0}, - {"getweapondamage", gsc_utils_getweapondamage, 0}, - {"setweapondamage", gsc_utils_setweapondamage, 0}, - {"getweaponmeleedamage", gsc_utils_getweaponmeleedamage, 0}, - {"setweaponmeleedamage", gsc_utils_setweaponmeleedamage, 0}, - {"getweaponfiretime", gsc_utils_getweaponfiretime, 0}, - {"setweaponfiretime", gsc_utils_setweaponfiretime, 0}, - {"getweaponmeleetime", gsc_utils_getweaponmeleetime, 0}, - {"setweaponmeleetime", gsc_utils_setweaponmeleetime, 0}, - {"getweaponreloadtime", gsc_utils_getweaponreloadtime, 0}, - {"setweaponreloadtime", gsc_utils_setweaponreloadtime, 0}, - {"getweaponreloademptytime", gsc_utils_getweaponreloademptytime, 0}, - {"setweaponreloademptytime", gsc_utils_setweaponreloademptytime, 0}, - {"getweaponhitlocmultiplier", gsc_utils_getweaponhitlocmultiplier, 0}, - {"setweaponhitlocmultiplier", gsc_utils_setweaponhitlocmultiplier, 0}, - {"getloadedweapons", gsc_utils_getloadedweapons, 0}, {"sqrt", gsc_utils_sqrt, 0}, {"sqrtInv", gsc_utils_sqrtInv, 0}, #endif +#if COMPILE_WEAPONS == 1 + {"getweaponmaxammo", gsc_weapons_getweaponmaxammo, 0}, + {"getweaponclipsize", gsc_weapons_getweaponclipsize, 0}, + {"getweapondamage", gsc_weapons_getweapondamage, 0}, + {"setweapondamage", gsc_weapons_setweapondamage, 0}, + {"getweaponmeleedamage", gsc_weapons_getweaponmeleedamage, 0}, + {"setweaponmeleedamage", gsc_weapons_setweaponmeleedamage, 0}, + {"getweaponfiretime", gsc_weapons_getweaponfiretime, 0}, + {"setweaponfiretime", gsc_weapons_setweaponfiretime, 0}, + {"getweaponmeleetime", gsc_weapons_getweaponmeleetime, 0}, + {"setweaponmeleetime", gsc_weapons_setweaponmeleetime, 0}, + {"getweaponreloadtime", gsc_weapons_getweaponreloadtime, 0}, + {"setweaponreloadtime", gsc_weapons_setweaponreloadtime, 0}, + {"getweaponreloademptytime", gsc_weapons_getweaponreloademptytime, 0}, + {"setweaponreloademptytime", gsc_weapons_setweaponreloademptytime, 0}, + {"getweaponhitlocmultiplier", gsc_weapons_getweaponhitlocmultiplier, 0}, + {"setweaponhitlocmultiplier", gsc_weapons_setweaponhitlocmultiplier, 0}, + {"getloadedweapons", gsc_weapons_getloadedweapons, 0}, +#endif + #ifdef EXTRA_FUNCTIONS_INC #include "extra/functions.hpp" #endif diff --git a/gsc.hpp b/gsc.hpp index 0a025c6..230572e 100644 --- a/gsc.hpp +++ b/gsc.hpp @@ -26,6 +26,7 @@ extern "C" { #include "gsc_mysql.hpp" #include "gsc_memory.hpp" #include "gsc_utils.hpp" +#include "gsc_weapons.hpp" #include "cracking.hpp" #ifdef EXTRA_INCLUDES_INC diff --git a/gsc_utils.cpp b/gsc_utils.cpp index dfc8222..57cf52e 100644 --- a/gsc_utils.cpp +++ b/gsc_utils.cpp @@ -742,378 +742,6 @@ void gsc_dlclose() stackPushInt(ret); } -#define MAX_WEAPON_IGNORE_SIZE 20 -#define MAX_WEAPON_NAME_SIZE 32 -char* defaultweapon_mp = NULL; -char ignoredWeapons[MAX_WEAPON_IGNORE_SIZE][MAX_WEAPON_NAME_SIZE]; -int ignoredWeaponCount = 0; - -void gsc_utils_init() -{ - if(defaultweapon_mp == NULL) - defaultweapon_mp = (char*)malloc(MAX_WEAPON_NAME_SIZE); - if(defaultweapon_mp == NULL) - stackError("gsc_utils_init() failed to malloc defaultweapon_mp"); - - strcpy(defaultweapon_mp, "defaultweapon_mp"); - defaultweapon_mp[strlen(defaultweapon_mp)] = '\0'; -} - -void gsc_utils_free() -{ - free(defaultweapon_mp); -} - -bool isOnIgnoreList(char* weapon) -{ - if(ignoredWeaponCount == 0) - return false; - - for(int i=0; i MAX_WEAPON_NAME_SIZE - 1) - { - stackError("gsc_utils_ignoreweapon() weapon name is too long"); - stackPushUndefined(); - return; - } - - if(ignoredWeaponCount >= MAX_WEAPON_IGNORE_SIZE) - { - stackError("Exceeded MAX_WEAPON_IGNORE_SIZE %d", MAX_WEAPON_IGNORE_SIZE); - stackPushUndefined(); - return; - } - - strcpy(ignoredWeapons[ignoredWeaponCount], weapon); - ignoredWeapons[ignoredWeaponCount][strlen(weapon)] = '\0'; - ignoredWeaponCount++; - stackPushInt(1); -} - -void gsc_utils_setdefaultweapon() -{ - char* weapon; - if ( ! stackGetParams("s", &weapon)) - { - stackError("gsc_utils_setdefaultweapon() argument is undefined or has a wrong type"); - stackPushUndefined(); - return; - } - - if(strlen(weapon) > MAX_WEAPON_NAME_SIZE - 1) - { - stackError("gsc_utils_setdefaultweapon() weapon name is too long"); - stackPushUndefined(); - return; - } - - if(strcmp(defaultweapon_mp, weapon) == 0) - { - stackPushInt(2); - return; - } - - strcpy(defaultweapon_mp, weapon); - defaultweapon_mp[strlen(weapon)] = '\0'; -#if COD_VERSION == COD2_1_0 - memcpy((void*)0x0811E929, &defaultweapon_mp, 4); // default - memcpy((void*)0x080E8AAD, &defaultweapon_mp, 4); // not found - //memcpy((void*)0x080F014D, &defaultweapon_mp, 4); // not found backup - memcpy((void*)0x080E928A, &defaultweapon_mp, 4); // unknown -#elif COD_VERSION == COD2_1_2 - memcpy((void*)0x08120C5A, &defaultweapon_mp, 4); // default - memcpy((void*)0x080EB09D, &defaultweapon_mp, 4); // not found - //memcpy((void*)0x080F273D, &defaultweapon_mp, 4); // not found backup - memcpy((void*)0x080EB87A, &defaultweapon_mp, 4); // unknown -#elif COD_VERSION == COD2_1_3 - memcpy((void*)0x08120DB9, &defaultweapon_mp, 4); // default - memcpy((void*)0x080EB1E1, &defaultweapon_mp, 4); // not found - //memcpy((void*)0x080F2881, &defaultweapon_mp, 4); // not found backup - memcpy((void*)0x080EB9BE, &defaultweapon_mp, 4); // unknown -#endif - stackPushInt(1); -} - -int weaponCount() -{ -#if COD_VERSION == COD2_1_0 - return *(int*)0x08576140; -#elif COD_VERSION == COD2_1_2 - return *(int*)0x0858A000; -#elif COD_VERSION == COD2_1_3 - return *(int*)0x08627080; // see 80EBFFE (cod2 1.3) -#endif -} - -int getWeapon(int index) -{ - typedef int (*get_weapon_t)(int index); - -#if COD_VERSION == COD2_1_0 - get_weapon_t get_weapon = (get_weapon_t)0x080E9270; -#elif COD_VERSION == COD2_1_2 - get_weapon_t get_weapon = (get_weapon_t)0x080EB860; -#elif COD_VERSION == COD2_1_3 - get_weapon_t get_weapon = (get_weapon_t)0x080EB9A4; -#endif - - return get_weapon(index); -} - -bool isValidWeaponId(int id) -{ - int weps = weaponCount(); - if(id >= weps || id < 0 || weps == 0) - return false; - - return true; -} - -void gsc_utils_getweaponoffsetint(char* funcname, int offset) -{ - int id; - if ( ! stackGetParams("i", &id)) - { - stackError("wrongs args for: %s(id)", funcname); - stackPushInt(0); - return; - } - - if(!isValidWeaponId(id)) - { - stackError("index is out of bounds: %s(id)", funcname); - stackPushInt(0); - return; - } - - int value = *(int*)(getWeapon(id) + offset); - stackPushInt(value); -} - -void gsc_utils_setweaponoffsetint(char* funcname, int offset) -{ - int id; - int value; - if ( ! stackGetParams("ii", &id, &value)) - { - stackError("scriptengine> wrongs args for: %s(id, value)", funcname); - stackPushInt(0); - return; - } - - if(!isValidWeaponId(id)) - { - stackError("index is out of bounds: %s(id, value)", funcname); - stackPushInt(0); - return; - } - - int* index = (int*)(getWeapon(id) + offset); - *index = value; - stackPushInt(1); -} - -void gsc_utils_getweaponmaxammo() -{ - gsc_utils_getweaponoffsetint("getweaponmaxammo", 468); -} - -void gsc_utils_getweaponclipsize() -{ - gsc_utils_getweaponoffsetint("getweaponclipsize", 472); -} - -void gsc_utils_getweapondamage() -{ - gsc_utils_getweaponoffsetint("getweapondamage", 492); -} - -void gsc_utils_setweapondamage() -{ - gsc_utils_setweaponoffsetint("setweapondamage", 492); -} - -void gsc_utils_getweaponmeleedamage() -{ - gsc_utils_getweaponoffsetint("getweapondamagemelee", 500); -} - -void gsc_utils_setweaponmeleedamage() -{ - gsc_utils_setweaponoffsetint("setweapondamagemelee", 500); -} - -void gsc_utils_getweaponfiretime() -{ - gsc_utils_getweaponoffsetint("getweaponfiretime", 516); -} - -void gsc_utils_setweaponfiretime() -{ - gsc_utils_setweaponoffsetint("setweaponfiretime", 516); // see 80EF58A -} - -void gsc_utils_getweaponmeleetime() -{ - gsc_utils_getweaponoffsetint("getweaponmeleetime", 532); -} - -void gsc_utils_setweaponmeleetime() -{ - gsc_utils_setweaponoffsetint("setweaponmeleetime", 532); -} - -void gsc_utils_getweaponreloadtime() -{ - gsc_utils_getweaponoffsetint("getweaponreloadtime", 536); -} - -void gsc_utils_setweaponreloadtime() -{ - gsc_utils_setweaponoffsetint("setweaponreloadtime", 536); -} - -void gsc_utils_getweaponreloademptytime() -{ - gsc_utils_getweaponoffsetint("getweaponreloademptytime", 540); -} - -void gsc_utils_setweaponreloademptytime() -{ - gsc_utils_setweaponoffsetint("setweaponreloademptytime", 540); -} - -char* hitlocs[] = { "none", "helmet", "head", "neck", "torso_upper", "torso_lower", "right_arm_upper", "right_arm_lower", "right_hand", "left_arm_upper", "left_arm_lower", "left_hand", "right_leg_upper", "right_leg_lower", "right_foot", "left_leg_upper", "left_leg_lower", "left_foot", "gun" }; -int getHitLocOffset(char* hitloc) -{ - int offset = 0; // none - for (int i = 0; i < int( sizeof(hitlocs) / sizeof(hitlocs[0]) ); i++) // prevent out of bound - { - if (strcmp(hitlocs[i], hitloc) == 0) - { - offset = i; - break; - } - } - return offset; -} - -void gsc_utils_getweaponhitlocmultiplier() -{ - int id; - char* hitloc; - if ( ! stackGetParams("is", &id, &hitloc)) - { - stackError("gsc_utils_getweaponhitlocmultiplier() one or more arguments is undefined or has a wrong type"); - stackPushInt(0); - return; - } - - if(!isValidWeaponId(id)) - { - stackError("index is out of bounds: getweaponhitlocmultiplier(id, hitloc)"); - stackPushInt(0); - return; - } - - int offset = getHitLocOffset(hitloc); - float multiplier = *(float*)(getWeapon(id) + 4 * offset + 1456); - stackPushFloat(multiplier); -} - -void gsc_utils_setweaponhitlocmultiplier() -{ - int id; - float multiplier; - char* hitloc; - if ( ! stackGetParams("isf", &id, &hitloc, &multiplier)) - { - stackError("gsc_utils_setweaponhitlocmultiplier() one or more arguments is undefined or has a wrong type"); - stackPushInt(0); - return; - } - - if(!isValidWeaponId(id)) - { - stackError("index out of bounds: getweaponhitlocmultiplier(id, hitloc, multiplier)"); - stackPushInt(0); - return; - } - - int offset = getHitLocOffset(hitloc); - float* multiPointer = (float*)(getWeapon(id) + 4 * offset + 1456); - *multiPointer = multiplier; - stackPushFloat(1); -} - -void gsc_utils_getloadedweapons() -{ - stackPushArray(); - int weps = weaponCount(); - if(weps == 0) - return; - - for(int i=0; i= weps || id < 0 || weps == 0) + return false; + + return true; +} + +void gsc_weapons_getweaponoffsetint(char* funcname, int offset) +{ + int id; + if ( ! stackGetParams("i", &id)) + { + stackError("wrongs args for: %s(id)", funcname); + stackPushInt(0); + return; + } + + if(!isValidWeaponId(id)) + { + stackError("index is out of bounds: %s(id)", funcname); + stackPushInt(0); + return; + } + + int value = *(int*)(getWeapon(id) + offset); + stackPushInt(value); +} + +void gsc_weapons_setweaponoffsetint(char* funcname, int offset) +{ + int id; + int value; + if ( ! stackGetParams("ii", &id, &value)) + { + stackError("scriptengine> wrongs args for: %s(id, value)", funcname); + stackPushInt(0); + return; + } + + if(!isValidWeaponId(id)) + { + stackError("index is out of bounds: %s(id, value)", funcname); + stackPushInt(0); + return; + } + + int* index = (int*)(getWeapon(id) + offset); + *index = value; + stackPushInt(1); +} + +void gsc_weapons_getweaponmaxammo() +{ + gsc_weapons_getweaponoffsetint("getweaponmaxammo", 468); +} + +void gsc_weapons_getweaponclipsize() +{ + gsc_weapons_getweaponoffsetint("getweaponclipsize", 472); +} + +void gsc_weapons_getweapondamage() +{ + gsc_weapons_getweaponoffsetint("getweapondamage", 492); +} + +void gsc_weapons_setweapondamage() +{ + gsc_weapons_setweaponoffsetint("setweapondamage", 492); +} + +void gsc_weapons_getweaponmeleedamage() +{ + gsc_weapons_getweaponoffsetint("getweapondamagemelee", 500); +} + +void gsc_weapons_setweaponmeleedamage() +{ + gsc_weapons_setweaponoffsetint("setweapondamagemelee", 500); +} + +void gsc_weapons_getweaponfiretime() +{ + gsc_weapons_getweaponoffsetint("getweaponfiretime", 516); +} + +void gsc_weapons_setweaponfiretime() +{ + gsc_weapons_setweaponoffsetint("setweaponfiretime", 516); // see 80EF58A +} + +void gsc_weapons_getweaponmeleetime() +{ + gsc_weapons_getweaponoffsetint("getweaponmeleetime", 532); +} + +void gsc_weapons_setweaponmeleetime() +{ + gsc_weapons_setweaponoffsetint("setweaponmeleetime", 532); +} + +void gsc_weapons_getweaponreloadtime() +{ + gsc_weapons_getweaponoffsetint("getweaponreloadtime", 536); +} + +void gsc_weapons_setweaponreloadtime() +{ + gsc_weapons_setweaponoffsetint("setweaponreloadtime", 536); +} + +void gsc_weapons_getweaponreloademptytime() +{ + gsc_weapons_getweaponoffsetint("getweaponreloademptytime", 540); +} + +void gsc_weapons_setweaponreloademptytime() +{ + gsc_weapons_setweaponoffsetint("setweaponreloademptytime", 540); +} + +char* hitlocs[] = { "none", "helmet", "head", "neck", "torso_upper", "torso_lower", "right_arm_upper", "right_arm_lower", "right_hand", "left_arm_upper", "left_arm_lower", "left_hand", "right_leg_upper", "right_leg_lower", "right_foot", "left_leg_upper", "left_leg_lower", "left_foot", "gun" }; +int getHitLocOffset(char* hitloc) +{ + int offset = 0; // none + for (int i = 0; i < int( sizeof(hitlocs) / sizeof(hitlocs[0]) ); i++) // prevent out of bound + { + if (strcmp(hitlocs[i], hitloc) == 0) + { + offset = i; + break; + } + } + return offset; +} + +void gsc_weapons_getweaponhitlocmultiplier() +{ + int id; + char* hitloc; + if ( ! stackGetParams("is", &id, &hitloc)) + { + stackError("gsc_weapons_getweaponhitlocmultiplier() one or more arguments is undefined or has a wrong type"); + stackPushInt(0); + return; + } + + if(!isValidWeaponId(id)) + { + stackError("index is out of bounds: getweaponhitlocmultiplier(id, hitloc)"); + stackPushInt(0); + return; + } + + int offset = getHitLocOffset(hitloc); + float multiplier = *(float*)(getWeapon(id) + 4 * offset + 1456); + stackPushFloat(multiplier); +} + +void gsc_weapons_setweaponhitlocmultiplier() +{ + int id; + float multiplier; + char* hitloc; + if ( ! stackGetParams("isf", &id, &hitloc, &multiplier)) + { + stackError("gsc_weapons_setweaponhitlocmultiplier() one or more arguments is undefined or has a wrong type"); + stackPushInt(0); + return; + } + + if(!isValidWeaponId(id)) + { + stackError("index out of bounds: getweaponhitlocmultiplier(id, hitloc, multiplier)"); + stackPushInt(0); + return; + } + + int offset = getHitLocOffset(hitloc); + float* multiPointer = (float*)(getWeapon(id) + 4 * offset + 1456); + *multiPointer = multiplier; + stackPushFloat(1); +} + +void gsc_weapons_getloadedweapons() +{ + stackPushArray(); + int weps = weaponCount(); + if(weps == 0) + return; + + for(int i=0; i +#include +#include + +/* gsc functions */ +#include "gsc.hpp" + +void gsc_weapons_getweaponmaxammo(); +void gsc_weapons_getweaponclipsize(); +void gsc_weapons_getweapondamage(); +void gsc_weapons_setweapondamage(); +void gsc_weapons_getweaponmeleedamage(); +void gsc_weapons_setweaponmeleedamage(); +void gsc_weapons_getweaponfiretime(); +void gsc_weapons_setweaponfiretime(); +void gsc_weapons_getweaponmeleetime(); +void gsc_weapons_setweaponmeleetime(); +void gsc_weapons_getweaponreloadtime(); +void gsc_weapons_setweaponreloadtime(); +void gsc_weapons_getweaponreloademptytime(); +void gsc_weapons_setweaponreloademptytime(); +void gsc_weapons_getweaponhitlocmultiplier(); +void gsc_weapons_setweaponhitlocmultiplier(); +void gsc_weapons_getloadedweapons(); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/libcod.cpp b/libcod.cpp index 82bbc21..15b432a 100644 --- a/libcod.cpp +++ b/libcod.cpp @@ -1168,7 +1168,6 @@ class cCallOfDuty2Pro cracking_hook_call(0x0808F412, (int)hook_vid_restart); cracking_hook_call(0x080DFF66, (int)hook_player_setmovespeed); cracking_hook_call(0x080F50AB, (int)hook_player_g_speed); - cracking_hook_call(0x080E9524, (int)hook_findWeaponIndex); cracking_hook_call(0x08081CFE, (int)hook_scriptError); hook_gametype_scripts = new cHook(0x0810DDEE, (int)hook_codscript_gametype_scripts); hook_gametype_scripts->hook(); @@ -1217,7 +1216,6 @@ class cCallOfDuty2Pro cracking_hook_call(0x08090CA2, (int)hook_vid_restart); cracking_hook_call(0x080E2546, (int)hook_player_setmovespeed); cracking_hook_call(0x080F76BF, (int)hook_player_g_speed); - cracking_hook_call(0x080EBB14, (int)hook_findWeaponIndex); cracking_hook_call(0x0808227A, (int)hook_scriptError); hook_gametype_scripts = new cHook(0x0811012A, (int)hook_codscript_gametype_scripts); hook_gametype_scripts->hook(); @@ -1266,7 +1264,6 @@ class cCallOfDuty2Pro cracking_hook_call(0x08090D36, (int)hook_vid_restart); cracking_hook_call(0x080E268A, (int)hook_player_setmovespeed); cracking_hook_call(0x080F7803, (int)hook_player_g_speed); - cracking_hook_call(0x080EBC58, (int)hook_findWeaponIndex); cracking_hook_call(0x08082346, (int)hook_scriptError); hook_gametype_scripts = new cHook(0x08110286, (int)hook_codscript_gametype_scripts); hook_gametype_scripts->hook(); @@ -1313,14 +1310,12 @@ class cCallOfDuty2Pro fs_library = Cvar_RegisterString("fs_library", "", 0x1000u); sv_downloadMessage = Cvar_RegisterString("sv_downloadMessage", "", 0x1000u); - gsc_utils_init(); setenv("LD_PRELOAD", "", 1); // dont inherit lib of parent printf("> [PLUGIN LOADED]\n"); } ~cCallOfDuty2Pro() { - gsc_utils_free(); printf("> [PLUGIN UNLOADED]\n"); } };