From f89756fe67d432c4bdb4b1c2c663b63502ac0533 Mon Sep 17 00:00:00 2001 From: TimePath Date: Sun, 27 Aug 2017 19:07:38 +1000 Subject: [PATCH] gamemodes: prevent addition and removal at runtime with a single macro --- .../gamemodes/gamemode/nexball/nexball.qc | 11 +--------- .../gamemode/onslaught/sv_onslaught.qh | 17 +--------------- qcsrc/common/mutators/base.qh | 14 +++++++++++++ .../mutators/mutator/gamemode_assault.qh | 20 ++----------------- qcsrc/server/mutators/mutator/gamemode_ca.qh | 11 +--------- qcsrc/server/mutators/mutator/gamemode_ctf.qc | 17 +--------------- qcsrc/server/mutators/mutator/gamemode_cts.qh | 18 +---------------- .../mutators/mutator/gamemode_deathmatch.qh | 20 +------------------ .../mutators/mutator/gamemode_domination.qh | 10 +--------- .../mutators/mutator/gamemode_freezetag.qh | 17 +--------------- .../mutators/mutator/gamemode_invasion.qh | 17 +--------------- .../mutators/mutator/gamemode_keepaway.qh | 17 +--------------- .../mutators/mutator/gamemode_keyhunt.qh | 17 +--------------- qcsrc/server/mutators/mutator/gamemode_lms.qh | 17 +--------------- .../server/mutators/mutator/gamemode_race.qh | 18 +---------------- qcsrc/server/mutators/mutator/gamemode_tdm.qh | 17 +--------------- 16 files changed, 30 insertions(+), 228 deletions(-) diff --git a/qcsrc/common/gamemodes/gamemode/nexball/nexball.qc b/qcsrc/common/gamemodes/gamemode/nexball/nexball.qc index 62280d7c24..f7784db18a 100644 --- a/qcsrc/common/gamemodes/gamemode/nexball/nexball.qc +++ b/qcsrc/common/gamemodes/gamemode/nexball/nexball.qc @@ -1141,6 +1141,7 @@ MUTATOR_HOOKFUNCTION(nb, SendWaypoint) REGISTER_MUTATOR(nb, g_nexball) { + MUTATOR_STATIC(); MUTATOR_ONADD { g_nexball_meter_period = autocvar_g_nexball_meter_period; @@ -1168,17 +1169,7 @@ REGISTER_MUTATOR(nb, g_nexball) MUTATOR_ONROLLBACK_OR_REMOVE { WEP_NEXBALL.spawnflags |= WEP_FLAG_MUTATORBLOCKED; - // we actually cannot roll back nb_delayedinit here - // BUT: we don't need to! If this gets called, adding always - // succeeds. } - - MUTATOR_ONREMOVE - { - LOG_INFO("This is a game type and it cannot be removed at runtime."); - return -1; - } - return 0; } diff --git a/qcsrc/common/gamemodes/gamemode/onslaught/sv_onslaught.qh b/qcsrc/common/gamemodes/gamemode/onslaught/sv_onslaught.qh index 7f2434915e..c1cce29be7 100644 --- a/qcsrc/common/gamemodes/gamemode/onslaught/sv_onslaught.qh +++ b/qcsrc/common/gamemodes/gamemode/onslaught/sv_onslaught.qh @@ -5,29 +5,14 @@ void ons_Initialize(); REGISTER_MUTATOR(ons, false) { + MUTATOR_STATIC(); MUTATOR_ONADD { - if (time > 1) // game loads at time 1 - error("This is a game type and it cannot be added at runtime."); ons_Initialize(); GameRules_teams(true); GameRules_limit_score(autocvar_g_onslaught_point_limit); } - - MUTATOR_ONROLLBACK_OR_REMOVE - { - // we actually cannot roll back ons_Initialize here - // BUT: we don't need to! If this gets called, adding always - // succeeds. - } - - MUTATOR_ONREMOVE - { - LOG_INFO("This is a game type and it cannot be removed at runtime."); - return -1; - } - return false; } diff --git a/qcsrc/common/mutators/base.qh b/qcsrc/common/mutators/base.qh index 7fc47ff9e0..4f940c42b3 100644 --- a/qcsrc/common/mutators/base.qh +++ b/qcsrc/common/mutators/base.qh @@ -280,6 +280,20 @@ STATIC_INIT_LATE(Mutators) { #define MUTATOR_ONADD if (mode == MUTATOR_ADDING) #define MUTATOR_ONREMOVE if (mode == MUTATOR_REMOVING) #define MUTATOR_ONROLLBACK_OR_REMOVE if (mode == MUTATOR_REMOVING || mode == MUTATOR_ROLLING_BACK) + +#define MUTATOR_STATIC() MACRO_BEGIN { \ + MUTATOR_ONADD { \ + /* game loads at time 1 */ \ + if (time > 1) { \ + error("This is a game type and it cannot be added at runtime."); \ + } \ + } \ + MUTATOR_ONREMOVE { \ + LOG_INFO("This is a game type and it cannot be removed at runtime."); \ + return -1; \ + } \ +} MACRO_END + #define MUTATOR_ADD(name) Mutator_Add(MUTATOR_##name) #define MUTATOR_REMOVE(name) Mutator_Remove(MUTATOR_##name) #define MUTATOR_RETURNVALUE CallbackChain_ReturnValue diff --git a/qcsrc/server/mutators/mutator/gamemode_assault.qh b/qcsrc/server/mutators/mutator/gamemode_assault.qh index abe89ecca4..0e279b8dc6 100644 --- a/qcsrc/server/mutators/mutator/gamemode_assault.qh +++ b/qcsrc/server/mutators/mutator/gamemode_assault.qh @@ -6,28 +6,12 @@ void assault_ScoreRules(); REGISTER_MUTATOR(as, false) { - GameRules_teams(true); - + MUTATOR_STATIC(); MUTATOR_ONADD { - if (time > 1) // game loads at time 1 - error("This is a game type and it cannot be added at runtime."); + GameRules_teams(true); assault_ScoreRules(); } - - MUTATOR_ONROLLBACK_OR_REMOVE - { - // we actually cannot roll back assault_Initialize here - // BUT: we don't need to! If this gets called, adding always - // succeeds. - } - - MUTATOR_ONREMOVE - { - LOG_INFO("This is a game type and it cannot be removed at runtime."); - return -1; - } - return 0; } diff --git a/qcsrc/server/mutators/mutator/gamemode_ca.qh b/qcsrc/server/mutators/mutator/gamemode_ca.qh index 5a4a9f8438..bf4a453a3b 100644 --- a/qcsrc/server/mutators/mutator/gamemode_ca.qh +++ b/qcsrc/server/mutators/mutator/gamemode_ca.qh @@ -23,11 +23,9 @@ bool ca_isEliminated(entity e); REGISTER_MUTATOR(ca, false) { + MUTATOR_STATIC(); MUTATOR_ONADD { - // game loads at time 1 - if (time > 1) error("This is a game type and it cannot be added at runtime."); - allowed_to_spawn = true; ca_teams = autocvar_g_ca_teams_override; @@ -50,13 +48,6 @@ REGISTER_MUTATOR(ca, false) GameRules_spawning_teams(autocvar_g_ca_team_spawns); } - - MUTATOR_ONREMOVE - { - LOG_INFO("This is a game type and it cannot be removed at runtime."); - return -1; - } - return 0; } diff --git a/qcsrc/server/mutators/mutator/gamemode_ctf.qc b/qcsrc/server/mutators/mutator/gamemode_ctf.qc index 08162f7ab9..8e11e1d789 100644 --- a/qcsrc/server/mutators/mutator/gamemode_ctf.qc +++ b/qcsrc/server/mutators/mutator/gamemode_ctf.qc @@ -6,30 +6,15 @@ void ctf_Initialize(); REGISTER_MUTATOR(ctf, false) { + MUTATOR_STATIC(); MUTATOR_ONADD { - if (time > 1) // game loads at time 1 - error("This is a game type and it cannot be added at runtime."); ctf_Initialize(); GameRules_teams(true); GameRules_limit_score(autocvar_capturelimit_override); GameRules_limit_lead(autocvar_captureleadlimit_override); } - - MUTATOR_ONROLLBACK_OR_REMOVE - { - // we actually cannot roll back ctf_Initialize here - // BUT: we don't need to! If this gets called, adding always - // succeeds. - } - - MUTATOR_ONREMOVE - { - LOG_INFO("This is a game type and it cannot be removed at runtime."); - return -1; - } - return 0; } #endif diff --git a/qcsrc/server/mutators/mutator/gamemode_cts.qh b/qcsrc/server/mutators/mutator/gamemode_cts.qh index f81ed274ae..c90919e6f4 100644 --- a/qcsrc/server/mutators/mutator/gamemode_cts.qh +++ b/qcsrc/server/mutators/mutator/gamemode_cts.qh @@ -7,11 +7,9 @@ void cts_Initialize(); REGISTER_MUTATOR(cts, false) { + MUTATOR_STATIC(); MUTATOR_ONADD { - if (time > 1) // game loads at time 1 - error("This is a game type and it cannot be added at runtime."); - g_race_qualifying = true; independent_players = 1; GameRules_limit_score(0); @@ -19,20 +17,6 @@ REGISTER_MUTATOR(cts, false) cts_Initialize(); } - - MUTATOR_ONROLLBACK_OR_REMOVE - { - // we actually cannot roll back cts_Initialize here - // BUT: we don't need to! If this gets called, adding always - // succeeds. - } - - MUTATOR_ONREMOVE - { - LOG_INFO("This is a game type and it cannot be removed at runtime."); - return -1; - } - return 0; } diff --git a/qcsrc/server/mutators/mutator/gamemode_deathmatch.qh b/qcsrc/server/mutators/mutator/gamemode_deathmatch.qh index d3cc197eaf..f45b0800f3 100644 --- a/qcsrc/server/mutators/mutator/gamemode_deathmatch.qh +++ b/qcsrc/server/mutators/mutator/gamemode_deathmatch.qh @@ -4,24 +4,6 @@ REGISTER_MUTATOR(dm, false) { - MUTATOR_ONADD - { - if (time > 1) // game loads at time 1 - error("This is a game type and it cannot be added at runtime."); - } - - MUTATOR_ONROLLBACK_OR_REMOVE - { - // we actually cannot roll back dm_Initialize here - // BUT: we don't need to! If this gets called, adding always - // succeeds. - } - - MUTATOR_ONREMOVE - { - error("This is a game type and it cannot be removed at runtime."); - return -1; - } - + MUTATOR_STATIC(); return 0; } diff --git a/qcsrc/server/mutators/mutator/gamemode_domination.qh b/qcsrc/server/mutators/mutator/gamemode_domination.qh index 19ba2f9a3f..8b4ca399ba 100644 --- a/qcsrc/server/mutators/mutator/gamemode_domination.qh +++ b/qcsrc/server/mutators/mutator/gamemode_domination.qh @@ -11,10 +11,9 @@ void dom_Initialize(); REGISTER_MUTATOR(dom, false) { + MUTATOR_STATIC(); MUTATOR_ONADD { - if (time > 1) // game loads at time 1 - error("This is a game type and it cannot be added at runtime."); dom_Initialize(); int fraglimit_override = autocvar_g_domination_point_limit; @@ -25,13 +24,6 @@ REGISTER_MUTATOR(dom, false) GameRules_limit_score(fraglimit_override); GameRules_limit_lead(autocvar_g_domination_point_leadlimit); } - - MUTATOR_ONREMOVE - { - LOG_INFO("This is a game type and it cannot be removed at runtime."); - return -1; - } - return 0; } diff --git a/qcsrc/server/mutators/mutator/gamemode_freezetag.qh b/qcsrc/server/mutators/mutator/gamemode_freezetag.qh index 69b857758f..b84a62201d 100644 --- a/qcsrc/server/mutators/mutator/gamemode_freezetag.qh +++ b/qcsrc/server/mutators/mutator/gamemode_freezetag.qh @@ -9,10 +9,9 @@ void freezetag_Initialize(); REGISTER_MUTATOR(ft, false) { + MUTATOR_STATIC(); MUTATOR_ONADD { - if (time > 1) // game loads at time 1 - error("This is a game type and it cannot be added at runtime."); freezetag_Initialize(); GameRules_teams(true); @@ -21,20 +20,6 @@ REGISTER_MUTATOR(ft, false) GameRules_spawning_teams(autocvar_g_freezetag_team_spawns); } - - MUTATOR_ONROLLBACK_OR_REMOVE - { - // we actually cannot roll back freezetag_Initialize here - // BUT: we don't need to! If this gets called, adding always - // succeeds. - } - - MUTATOR_ONREMOVE - { - LOG_INFO("This is a game type and it cannot be removed at runtime."); - return -1; - } - return 0; } diff --git a/qcsrc/server/mutators/mutator/gamemode_invasion.qh b/qcsrc/server/mutators/mutator/gamemode_invasion.qh index 1b5853d305..e902b5ee60 100644 --- a/qcsrc/server/mutators/mutator/gamemode_invasion.qh +++ b/qcsrc/server/mutators/mutator/gamemode_invasion.qh @@ -11,10 +11,9 @@ void invasion_Initialize(); REGISTER_MUTATOR(inv, false) { + MUTATOR_STATIC(); MUTATOR_ONADD { - if (time > 1) // game loads at time 1 - error("This is a game type and it cannot be added at runtime."); g_invasion = true; invasion_Initialize(); @@ -28,20 +27,6 @@ REGISTER_MUTATOR(inv, false) GameRules_spawning_teams(autocvar_g_invasion_team_spawns); } } - - MUTATOR_ONROLLBACK_OR_REMOVE - { - // we actually cannot roll back invasion_Initialize here - // BUT: we don't need to! If this gets called, adding always - // succeeds. - } - - MUTATOR_ONREMOVE - { - LOG_INFO("This is a game type and it cannot be removed at runtime."); - return -1; - } - return 0; } diff --git a/qcsrc/server/mutators/mutator/gamemode_keepaway.qh b/qcsrc/server/mutators/mutator/gamemode_keepaway.qh index a13ab83a55..21cc3e46d3 100644 --- a/qcsrc/server/mutators/mutator/gamemode_keepaway.qh +++ b/qcsrc/server/mutators/mutator/gamemode_keepaway.qh @@ -6,26 +6,11 @@ void ka_Initialize(); REGISTER_MUTATOR(ka, false) { + MUTATOR_STATIC(); MUTATOR_ONADD { - if (time > 1) // game loads at time 1 - error("This is a game type and it cannot be added at runtime."); ka_Initialize(); } - - MUTATOR_ONROLLBACK_OR_REMOVE - { - // we actually cannot roll back ka_Initialize here - // BUT: we don't need to! If this gets called, adding always - // succeeds. - } - - MUTATOR_ONREMOVE - { - LOG_INFO("This is a game type and it cannot be removed at runtime."); - return -1; - } - return false; } diff --git a/qcsrc/server/mutators/mutator/gamemode_keyhunt.qh b/qcsrc/server/mutators/mutator/gamemode_keyhunt.qh index 5b0fb5bbbd..9e8ded5ac8 100644 --- a/qcsrc/server/mutators/mutator/gamemode_keyhunt.qh +++ b/qcsrc/server/mutators/mutator/gamemode_keyhunt.qh @@ -9,10 +9,9 @@ void kh_Initialize(); REGISTER_MUTATOR(kh, false) { + MUTATOR_STATIC(); MUTATOR_ONADD { - if (time > 1) // game loads at time 1 - error("This is a game type and it cannot be added at runtime."); kh_Initialize(); GameRules_teams(true); @@ -20,20 +19,6 @@ REGISTER_MUTATOR(kh, false) GameRules_limit_lead(autocvar_g_keyhunt_point_leadlimit); GameRules_spawning_teams(autocvar_g_keyhunt_team_spawns); } - - MUTATOR_ONROLLBACK_OR_REMOVE - { - // we actually cannot roll back kh_Initialize here - // BUT: we don't need to! If this gets called, adding always - // succeeds. - } - - MUTATOR_ONREMOVE - { - LOG_INFO("This is a game type and it cannot be removed at runtime."); - return -1; - } - return 0; } diff --git a/qcsrc/server/mutators/mutator/gamemode_lms.qh b/qcsrc/server/mutators/mutator/gamemode_lms.qh index 8c47c92b7c..303dd3bd2f 100644 --- a/qcsrc/server/mutators/mutator/gamemode_lms.qh +++ b/qcsrc/server/mutators/mutator/gamemode_lms.qh @@ -8,29 +8,14 @@ void lms_Initialize(); REGISTER_MUTATOR(lms, false) { + MUTATOR_STATIC(); MUTATOR_ONADD { - if (time > 1) // game loads at time 1 - error("This is a game type and it cannot be added at runtime."); lms_Initialize(); GameRules_limit_score(((!autocvar_g_lms_lives_override) ? -1 : autocvar_g_lms_lives_override)); GameRules_limit_lead(0); } - - MUTATOR_ONROLLBACK_OR_REMOVE - { - // we actually cannot roll back lms_Initialize here - // BUT: we don't need to! If this gets called, adding always - // succeeds. - } - - MUTATOR_ONREMOVE - { - LOG_INFO("This is a game type and it cannot be removed at runtime."); - return -1; - } - return 0; } diff --git a/qcsrc/server/mutators/mutator/gamemode_race.qh b/qcsrc/server/mutators/mutator/gamemode_race.qh index ec71a62d17..c1b700442f 100644 --- a/qcsrc/server/mutators/mutator/gamemode_race.qh +++ b/qcsrc/server/mutators/mutator/gamemode_race.qh @@ -7,27 +7,11 @@ void race_Initialize(); REGISTER_MUTATOR(rc, false) { + MUTATOR_STATIC(); MUTATOR_ONADD { - if (time > 1) // game loads at time 1 - error("This is a game type and it cannot be added at runtime."); - rc_SetLimits(); race_Initialize(); } - - MUTATOR_ONROLLBACK_OR_REMOVE - { - // we actually cannot roll back race_Initialize here - // BUT: we don't need to! If this gets called, adding always - // succeeds. - } - - MUTATOR_ONREMOVE - { - LOG_INFO("This is a game type and it cannot be removed at runtime."); - return -1; - } - return 0; } diff --git a/qcsrc/server/mutators/mutator/gamemode_tdm.qh b/qcsrc/server/mutators/mutator/gamemode_tdm.qh index bfff9cca1b..28331dfefc 100644 --- a/qcsrc/server/mutators/mutator/gamemode_tdm.qh +++ b/qcsrc/server/mutators/mutator/gamemode_tdm.qh @@ -9,10 +9,9 @@ void tdm_DelayedInit(entity this); REGISTER_MUTATOR(tdm, false) { + MUTATOR_STATIC(); MUTATOR_ONADD { - if (time > 1) // game loads at time 1 - error("This is a game type and it cannot be added at runtime."); InitializeEntity(NULL, tdm_DelayedInit, INITPRIO_GAMETYPE); GameRules_teams(true); @@ -20,19 +19,5 @@ REGISTER_MUTATOR(tdm, false) GameRules_limit_lead(autocvar_g_tdm_point_leadlimit); GameRules_spawning_teams(autocvar_g_tdm_team_spawns); } - - MUTATOR_ONROLLBACK_OR_REMOVE - { - // we actually cannot roll back tdm_Initialize here - // BUT: we don't need to! If this gets called, adding always - // succeeds. - } - - MUTATOR_ONREMOVE - { - LOG_INFO("This is a game type and it cannot be removed at runtime."); - return -1; - } - return 0; } -- 2.39.2