From 8604c198e9548f8c4aac971af8e3fc75cec70f6d Mon Sep 17 00:00:00 2001 From: ac-stef Date: Wed, 12 Nov 2014 17:06:35 +0100 Subject: [PATCH] cleaning up cubescript hooks - provide a function to easily assemble and execute the command - context-selective execution of hooks - restrict potentially exploitable or harmful hooks to singleplayer-modes --- source/src/client.cpp | 13 +++--------- source/src/clientgame.cpp | 43 +++++++++------------------------------ source/src/clients2c.cpp | 39 +++++++---------------------------- source/src/command.cpp | 18 ++++++++++++++++ source/src/console.cpp | 12 ++--------- source/src/entities.cpp | 7 +------ source/src/main.cpp | 2 +- source/src/protos.h | 2 ++ source/src/weapon.cpp | 19 +++-------------- source/src/world.cpp | 2 +- 10 files changed, 48 insertions(+), 109 deletions(-) diff --git a/source/src/client.cpp b/source/src/client.cpp index 166e6ff9..4ac6cb64 100644 --- a/source/src/client.cpp +++ b/source/src/client.cpp @@ -208,11 +208,8 @@ void disconnect(int onlyclean, int async) } #endif if(!onlyclean) localconnect(); - if(identexists("onDisconnect")) - { - defformatstring(ondisconnect)("onDisconnect %d", -1); - execute(ondisconnect); - }} + exechook(HOOK_SP_MP, "onDisconnect", "%d", -1); +} void trydisconnect() { @@ -611,11 +608,7 @@ void gets2c() // get updates from the server connpeer = NULL; connected = 1; conoutf(_("connected to server")); - if(identexists("onConnect")) - { - defformatstring(onconnect)("onConnect %d", -1); - execute(onconnect); - } + exechook(HOOK_SP_MP, "onConnect", "%d", -1); throttle(); if(editmode) toggleedit(true); break; diff --git a/source/src/clientgame.cpp b/source/src/clientgame.cpp index 2d69fc4b..bbba7a37 100644 --- a/source/src/clientgame.cpp +++ b/source/src/clientgame.cpp @@ -162,11 +162,7 @@ void newname(const char *name) { string tmpname; filtertext(tmpname, name, FTXT__PLAYERNAME, MAXNAMELEN); - if(identexists("onNameChange")) - { - defformatstring(onnamechange)("onNameChange %d \"%s\"", player1->clientnum, tmpname); - execute(onnamechange); - } + exechook(HOOK_SP, "onNameChange", "%d \"%s\"", player1->clientnum, tmpname); copystring(player1->name, tmpname);//12345678901234// if(!player1->name[0]) copystring(player1->name, "unarmed"); updateclientname(player1); @@ -706,11 +702,7 @@ void findplayerstart(playerent *d, bool mapcenter, int arenaspawn) d->o.z = 4; } entinmap(d); - if(identexists("onSpawn")/* && (m_teammode && d->team == player1->team)*/) - { - defformatstring(onspawn)("onSpawn %d", d->clientnum); - execute(onspawn); - } + exechook(HOOK_SP, "onSpawn", "%d", d->clientnum); } void spawnplayer(playerent *d) @@ -852,11 +844,8 @@ void dodamage(int damage, playerent *pl, playerent *actor, int gun, bool gib, bo // I suppose someone wanted to play the hitsound for player1 or spectated player (lucas:2011may22) playerent *h = player1->isspectating() && player1->followplayercn >= 0 && (player1->spectatemode == SM_FOLLOW1ST || player1->spectatemode == SM_FOLLOW3RD || player1->spectatemode == SM_FOLLOW3RD_TRANSPARENT) ? getclient(player1->followplayercn) : NULL; if(!h) h = player1; - if(identexists("onHit")) - { - defformatstring(o)("onHit %d %d %d %d %d", actor->clientnum, pl->clientnum, damage, gun, gib ? 1 : 0); - execute(o); - } + exechook(HOOK_SP, "onHit", "%d %d %d %d %d", actor->clientnum, pl->clientnum, damage, gun, gib ? 1 : 0); + if(actor==h && pl!=actor) { if( (hitsound == 1 || (hitsound && h != player1) ) && lasthit != lastmillis) audiomgr.playsound(S_HITSOUND, SP_HIGH); @@ -888,11 +877,7 @@ void dokill(playerent *pl, playerent *act, bool gib, int gun) { if(pl->state!=CS_ALIVE || intermission) return; - if(identexists("onKill")) - { - defformatstring(killevent)("onKill %d %d %d %d", act->clientnum, pl->clientnum, gun, gib ? 1 : 0); - execute(killevent); - } + exechook(HOOK_SP, "onKill", "%d %d %d %d", act->clientnum, pl->clientnum, gun, gib ? 1 : 0); string pname, aname, death; copystring(pname, pl==player1 ? "you" : colorname(pl)); @@ -976,7 +961,7 @@ void timeupdate(int milliscur, int millismax) consolescores(); ((sniperrifle *)player1->weaponsel)->scoped = false; showscores(true); - if(identexists("start_intermission")) execute("start_intermission"); + exechook(HOOK_SP_MP, "start_intermission", ""); } else { @@ -985,7 +970,7 @@ void timeupdate(int milliscur, int millismax) { audiomgr.musicsuggest(M_LASTMINUTE1 + rnd(2), 70*1000, true); hudoutf("1 minute left!"); - if(identexists("onLastMin")) execute("onLastMin"); + exechook(HOOK_SP_MP, "onLastMin", ""); } else if(clockdisplay==0) conoutf(_("time remaining: %d minutes"), minutesremaining); } @@ -1217,11 +1202,7 @@ void flagmsg(int flag, int message, int actor, int flagtime) const char *teamstr = m_ktf ? "the" : neutral ? ownerstr : own ? "your" : "the enemy"; const char *flagteam = (m_ktf && !neutral) ? (teammate ? "your teammate " : "your enemy ") : ""; - if(identexists("onFlag")) - { - defformatstring(onflagevent)("onFlag %d %d %d", message, actor, flag); - execute(onflagevent); - } + exechook(HOOK_SP, "onFlag", "%d %d %d", message, actor, flag); switch(message) { @@ -1421,11 +1402,7 @@ void callvote(int type, const char *arg1, const char *arg2, const char *arg3) break; } sendpackettoserv(1, p.finalize()); - if(identexists("onCallVote")) - { - defformatstring(runas)("%s %d %d [%s] [%s]", "onCallVote", type, player1->clientnum, arg1, arg2); - execute(runas); - } + exechook(HOOK_SP_MP, "onCallVote", "%d %d [%s] [%s]", type, player1->clientnum, arg1, arg2); } else conoutf(_("%c3invalid vote"), CC); } @@ -1520,7 +1497,7 @@ void voteresult(int v) curvote->millis = totalmillis + 5000; conoutf(_("vote %s"), v == VOTE_YES ? _("passed") : _("failed")); if(multiplayer(false)) audiomgr.playsound(v == VOTE_YES ? S_VOTEPASS : S_VOTEFAIL, SP_HIGH); - if(identexists("onVoteEnd")) execute("onVoteEnd"); + exechook(HOOK_SP_MP, "onVoteEnd", ""); votepending = 0; } } diff --git a/source/src/clients2c.cpp b/source/src/clients2c.cpp index 44d12371..5cd38e72 100644 --- a/source/src/clients2c.cpp +++ b/source/src/clients2c.cpp @@ -366,20 +366,12 @@ int lastspawn = 0; void onCallVote(int type, int vcn, char *text, char *a) { - if(identexists("onCallVote")) - { - defformatstring(runas)("%s %d %d [%s] [%s]", "onCallVote", type, vcn, text, a); - execute(runas); - } + exechook(HOOK_SP_MP, "onCallVote", "%d %d [%s] [%s]", type, vcn, text, a); } void onChangeVote(int mod, int id) { - if(identexists("onChangeVote")) - { - defformatstring(runas)("%s %d %d", "onChangeVote", mod, id); - execute(runas); - } + exechook(HOOK_SP_MP, "onChangeVote", "%d %d", mod, id); } VARP(voicecomsounds, 0, 1, 2); @@ -525,11 +517,7 @@ void parsemessages(int cn, playerent *d, ucharbuf &p, bool demo = false) { conoutf("%s (PM):\f9 %s", colorname(d), highlight(text)); lastpm = d->clientnum; - if(identexists("onPM")) - { - defformatstring(onpm)("onPM %d [%s]", d->clientnum, text); - execute(onpm); - } + exechook(HOOK_SP_MP, "onPM", "%d [%s]", d->clientnum, text); } break; } @@ -567,13 +555,8 @@ void parsemessages(int cn, playerent *d, ucharbuf &p, bool demo = false) if(!text[0]) copystring(text, "unarmed"); if(d) { - if(strcmp(d->name, text)) - conoutf(_("%s is now known as %s"), colorname(d), colorname(d, text)); - if(identexists("onNameChange")) - { - defformatstring(onnamechange)("onNameChange %d \"%s\"", d->clientnum, text); - execute(onnamechange); - } + if(strcmp(d->name, text)) conoutf(_("%s is now known as %s"), colorname(d), colorname(d, text)); + exechook(HOOK_SP, "onNameChange", "%d \"%s\"", d->clientnum, text); copystring(d->name, text, MAXNAMELEN+1); updateclientname(d); } @@ -616,11 +599,7 @@ void parsemessages(int cn, playerent *d, ucharbuf &p, bool demo = false) conoutf(_("connected: %s"), colorname(d, text)); } copystring(d->name, text, MAXNAMELEN+1); - if(identexists("onConnect")) - { - defformatstring(onconnect)("onConnect %d", d->clientnum); - execute(onconnect); - } + exechook(HOOK_SP_MP, "onConnect", "%d", d->clientnum); loopi(2) d->setskin(i, getint(p)); d->team = getint(p); @@ -642,11 +621,7 @@ void parsemessages(int cn, playerent *d, ucharbuf &p, bool demo = false) if(!d) break; if(d->name[0]) conoutf(_("player %s disconnected"), colorname(d)); zapplayer(players[cn]); - if(identexists("onDisconnect")) - { - defformatstring(ondisconnect)("onDisconnect %d", d->clientnum); - execute(ondisconnect); - } + exechook(HOOK_SP_MP, "onDisconnect", "%d", d->clientnum); break; } diff --git a/source/src/command.cpp b/source/src/command.cpp index f238ad08..b2c7c1c9 100644 --- a/source/src/command.cpp +++ b/source/src/command.cpp @@ -700,6 +700,24 @@ int execute(const char *p) return i; } +bool exechook(int context, const char *ident, const char *body,...) // execute cubescript hook if available and allowed in current context/gamemode +{ // always use one of HOOK_SP_MP, HOOK_SP or HOOK_MP and then OR them (as needed) with HOOK_TEAM, HOOK_NOTEAM, HOOK_BOTMODE, HOOK_FLAGMODE, HOOK_ARENA + if(multiplayer(NULL) && (context & HOOK_FLAGMASK) != HOOK_MP && (context & HOOK_FLAGMASK) != HOOK_SP_MP) return false; // hook is singleplayer-only + if(((context & HOOK_TEAM) && !m_teammode) || + ((context & HOOK_NOTEAM) && m_teammode) || + ((context & HOOK_BOTMODE) && !m_botmode) || + ((context & HOOK_FLAGMODE) && m_flags) || + ((context & HOOK_ARENA) && m_arena)) return false; // wrong gamemode + if(identexists(ident)) + { + defvformatstring(arglist, body, body); + defformatstring(execbody)("%s%c%s", ident, *arglist ? ' ' : '\0', arglist); + execute(execbody); + return true; + } + return false; +} + #ifndef STANDALONE // tab-completion of all idents diff --git a/source/src/console.cpp b/source/src/console.cpp index 62d4adb3..6f4443b1 100644 --- a/source/src/console.cpp +++ b/source/src/console.cpp @@ -612,16 +612,8 @@ void keypress(int code, bool isdown, int cooked, SDLMod mod) { if(haskey) execbind(*haskey, isdown); } - if(isdown && identexists("KEYPRESS")) // TODO: Remove this if its misued. e.x: /KEYPRESS = [ echo You pressed key code: $arg1 ] // Output: You pressed key code: 32 (if you press the spacebar) - { - defformatstring(kpi)("KEYPRESS %d", code); - execute(kpi); - } - if (!isdown && identexists("KEYRELEASE")) - { - defformatstring(kpo)("KEYRELEASE %d", code); - execute(kpo); - } + if(isdown) exechook(HOOK_SP, "KEYPRESS", "KEYPRESS %d", code); + else exechook(HOOK_SP, "KEYRELEASE", "%d", code); } char *getcurcommand() diff --git a/source/src/entities.cpp b/source/src/entities.cpp index d61491b0..43125f3d 100644 --- a/source/src/entities.cpp +++ b/source/src/entities.cpp @@ -311,7 +311,6 @@ void pickupeffects(int n, playerent *d) */ if(identexists("onPickup")) { - string o; itemstat *tmp = NULL; switch(e.type) { @@ -324,11 +323,7 @@ void pickupeffects(int n, playerent *d) case I_ARMOUR: tmp = &powerupstats[e.type-I_HEALTH]; break; default: break; } - if(tmp) - { - formatstring(o)("onPickup %d %d", e.type - 3, m_lss && e.type == I_GRENADE ? 2 : tmp->add); - execute(o); - } + if(tmp) exechook(HOOK_SP, "onPickup", "%d %d", e.type - 3, m_lss && e.type == I_GRENADE ? 2 : tmp->add); } } else audiomgr.playsound(is.sound, d); diff --git a/source/src/main.cpp b/source/src/main.cpp index 4892021c..1c2b39b8 100644 --- a/source/src/main.cpp +++ b/source/src/main.cpp @@ -1238,7 +1238,7 @@ int main(int argc, char **argv) exec("config/defaults.cfg"); firstrun = true; } - if(identexists("afterinit")) execute("afterinit"); + exechook(HOOK_SP_MP, "afterinit", ""); if(compatibilitymode) { per_idents = false; diff --git a/source/src/protos.h b/source/src/protos.h index ad54cd7b..b2682441 100644 --- a/source/src/protos.h +++ b/source/src/protos.h @@ -826,6 +826,8 @@ extern int getvar(const char *name); extern bool identexists(const char *name); extern bool addcommand(const char *name, void (*fun)(), const char *sig); extern int execute(const char *p); +enum { HOOK_SP_MP = 1, HOOK_SP, HOOK_MP, HOOK_FLAGMASK = 0xff, HOOK_TEAM = 0x100, HOOK_NOTEAM = 0x200, HOOK_BOTMODE = 0x400, HOOK_FLAGMODE = 0x800, HOOK_ARENA = 0x1000 }; +extern bool exechook(int context, const char *ident, const char *body,...); // execute cubescript hook if available and allowed in current context/gamemode extern char *executeret(const char *p); extern char *conc(char **w, int n, bool space); extern void intret(int v); diff --git a/source/src/weapon.cpp b/source/src/weapon.cpp index b3238d80..5c6398d3 100644 --- a/source/src/weapon.cpp +++ b/source/src/weapon.cpp @@ -46,12 +46,7 @@ void selectweapon(weapon *w) if(w->type==GUN_PISTOL && akimbo->selectable()) w = akimbo; player1->weaponswitch(w); - if(identexists("onWeaponSwitch")) - { - string o; - formatstring(o)("onWeaponSwitch %d", i); - execute(o); - } + exechook(HOOK_SP, "onWeaponSwitch", "%d", i); } } @@ -395,11 +390,7 @@ hitweap accuracym[NUMGUNS]; inline void attackevent(playerent *owner, int weapon) { - if(owner == player1 && identexists("onAttack")) - { - defformatstring(onattackevent)("onAttack %d", weapon); - execute(onattackevent); - } + if(owner == player1) exechook(HOOK_SP, "onAttack", "%d", weapon); } vector hits; @@ -837,11 +828,7 @@ bool weapon::reload(bool autoreloaded) if(local) { addmsg(SV_RELOAD, "ri2", lastmillis, owner->weaponsel->type); - if(identexists("onReload")) - { - defformatstring(str)("onReload %d", (int)autoreloaded); - execute(str); - } + exechook(HOOK_SP, "onReload", "%d", (int)autoreloaded); } return true; } diff --git a/source/src/world.cpp b/source/src/world.cpp index ae27dbc1..eb3bbff9 100644 --- a/source/src/world.cpp +++ b/source/src/world.cpp @@ -547,7 +547,7 @@ void newmap(int *i) if(empty_world(*i, false)) { addmsg(SV_NEWMAP, "ri", max(*i, 0)); - if(identexists("onNewMap")) execute("onNewMap"); + exechook(HOOK_SP_MP, "onNewMap", ""); } defformatstring(startmillis)("%d", millis_()); alias("gametimestart", startmillis);