From 9031facaece6a31ba3331c78e15dd9f4b180901d Mon Sep 17 00:00:00 2001 From: Greyscale Date: Tue, 14 Apr 2009 04:58:05 +0200 Subject: [PATCH] Weapons module completed, added logging, thorough validations, and finished default configs. --- .../configs/zr/weapons/weapongroups.txt | 4 + .../sourcemod/configs/zr/weapons/weapons.txt | 31 +++--- .../translations/zombiereloaded.phrases.txt | 11 +++ src/zr/commands.inc | 36 ++----- src/zr/weapons/menu_weapons.inc | 22 ++++- src/zr/weapons/restrict.inc | 99 +++++++++++++++---- src/zr/weapons/weapons.inc | 81 +++++++-------- src/zr/zombiereloaded.inc | 4 +- 8 files changed, 177 insertions(+), 111 deletions(-) diff --git a/cstrike/addons/sourcemod/configs/zr/weapons/weapongroups.txt b/cstrike/addons/sourcemod/configs/zr/weapons/weapongroups.txt index c238bc8..bb36deb 100644 --- a/cstrike/addons/sourcemod/configs/zr/weapons/weapongroups.txt +++ b/cstrike/addons/sourcemod/configs/zr/weapons/weapongroups.txt @@ -10,6 +10,10 @@ // without these brackets the weapon will be skipped. // Invalid weapons are logged and skipped. // } +// +// Notes: +// +// Invalid weapons (not in weapons.txt) will be logged in the error logs and ignored in-game "weapongroups" { diff --git a/cstrike/addons/sourcemod/configs/zr/weapons/weapons.txt b/cstrike/addons/sourcemod/configs/zr/weapons/weapons.txt index 49cbfe0..568f068 100644 --- a/cstrike/addons/sourcemod/configs/zr/weapons/weapons.txt +++ b/cstrike/addons/sourcemod/configs/zr/weapons/weapons.txt @@ -8,6 +8,15 @@ // "menu" "yes" (default) // Allows admins to toggle restriction with the menu // } // +// Notes: +// +// This is a list of valid weapons for your server, unlisted weapons will +// be seen as invalid by the weapon restrict module. +// +// A config setting set to something other than "yes" or "no" will be assumed as "no." +// +// A missing config setting will be assumed to be its default value (documented above). +// // Duplicate weapon entries will show up separately in restrict menu, but // only the first one's options are used. @@ -15,20 +24,20 @@ { "Glock" { - "restrict" "no" + //"restrict" "no" "menu" "yes" } "USP" { "restrict" "no" - "menu" "yes" + //"menu" "yes" } "P228" { - "restrict" "no" - "menu" "yes" + //"restrict" "no" + //"menu" "yes" } "Deagle" @@ -135,19 +144,19 @@ "SG550" { - "restrict" "no" + "restrict" "yes" "menu" "yes" } "G3SG1" { - "restrict" "no" + "restrict" "yes" "menu" "yes" } "AWP" { - "restrict" "no" + "restrict" "yes" "menu" "yes" } @@ -165,14 +174,14 @@ "flashbang" { - "restrict" "no" - "menu" "yes" + "restrict" "yes" + "menu" "no" } "smokegrenade" { - "restrict" "no" - "menu" "yes" + "restrict" "yes" + "menu" "no" } "NVGs" diff --git a/cstrike/addons/sourcemod/translations/zombiereloaded.phrases.txt b/cstrike/addons/sourcemod/translations/zombiereloaded.phrases.txt index 4d5dbf3..cba6bb9 100644 --- a/cstrike/addons/sourcemod/translations/zombiereloaded.phrases.txt +++ b/cstrike/addons/sourcemod/translations/zombiereloaded.phrases.txt @@ -164,6 +164,17 @@ "ru" "Отображение здоровья включено." } + // =========================== + // Generic + // =========================== + + // Menu + + "Menu empty" + { + "en" "(Empty)" + } + // =========================== // Weapons // =========================== diff --git a/src/zr/commands.inc b/src/zr/commands.inc index b65a622..476ffec 100644 --- a/src/zr/commands.inc +++ b/src/zr/commands.inc @@ -135,6 +135,7 @@ public Action:Command_Respawn(client, argc) } /** + * Command callback for client command "zr_restrict" * @param client The client index. * @param argc Argument count. */ @@ -159,24 +160,14 @@ public Action:Command_Restrict(client, argc) new WpnRestrictQuery:output = RestrictRestrict(arg1, display); RestrictPrintRestrictOutput(client, output, display, true); - if (LogFlagCheck(LOG_GAME_EVENTS, LOG_MODULE_COMMANDS)) - { - decl String:client_name[64]; - - if (client > 0) - { - GetClientName(client, client_name, sizeof(client_name)); - } - else - { - client_name = "Console\0"; - } - ZR_LogMessageFormatted(client, "admin commands", "weapon restict", "\"%s\" restricted weapon (group): \"%s\".", true, client_name, arg1); - } - return Plugin_Handled; } +/** + * Command callback for client command "zr_unrestrict" + * @param client The client index. + * @param argc Argument count. +*/ public Action:Command_Unrestrict(client, argc) { // If plugin is disabled then stop @@ -198,21 +189,6 @@ public Action:Command_Unrestrict(client, argc) new WpnRestrictQuery:output = RestrictUnrestrict(arg1, display); RestrictPrintUnrestrictOutput(client, output, display, true); - if (LogFlagCheck(LOG_GAME_EVENTS, LOG_MODULE_COMMANDS)) - { - decl String:client_name[64]; - - if (client > 0) - { - GetClientName(client, client_name, sizeof(client_name)); - } - else - { - client_name = "Console\0"; - } - ZR_LogMessageFormatted(client, "admin commands", "weapon restict", "\"%s\" unrestricted weapon (group): \"%s\".", true, client_name, arg1); - } - return Plugin_Handled; } diff --git a/src/zr/weapons/menu_weapons.inc b/src/zr/weapons/menu_weapons.inc index f543488..cf7f2ac 100644 --- a/src/zr/weapons/menu_weapons.inc +++ b/src/zr/weapons/menu_weapons.inc @@ -161,6 +161,15 @@ WeaponsMenuWeapons(client, WeaponsMenu:type) } } + // If there are no weapons, add an "(Empty)" line + if (size == 0) + { + decl String:empty[64]; + Format(empty, sizeof(empty), "%t", "Menu empty"); + + AddMenuItem(menu_weapons_weapons, "empty", empty, ITEMDRAW_DISABLED); + } + // Kill the array handle CloseHandle(arrayWeapons); } @@ -170,7 +179,7 @@ WeaponsMenuWeapons(client, WeaponsMenu:type) SetMenuTitle(menu_weapons_weapons, "%t\n ", "Weapons menu weapons group title"); decl String:weapongroup[WEAPONS_MAX_LENGTH]; - decl String:display[WEAPONS_MAX_LENGTH + 1]; + decl String:display[WEAPONS_MAX_LENGTH + 2]; new Handle:arrayWeaponGroups = INVALID_HANDLE; new size = RestrictCreateGroupArray(arrayWeaponGroups); @@ -193,6 +202,15 @@ WeaponsMenuWeapons(client, WeaponsMenu:type) AddMenuItem(menu_weapons_weapons, weapongroup, display); } + // If there are no weapons, add an "(Empty)" line + if (size == 0) + { + decl String:empty[64]; + Format(empty, sizeof(empty), "%t", "Menu empty"); + + AddMenuItem(menu_weapons_weapons, "empty", empty, ITEMDRAW_DISABLED); + } + // Kill the array handle CloseHandle(arrayWeaponGroups); } @@ -404,7 +422,7 @@ WeaponsMenuMarket(client) decl String:togglebuyzone[64]; decl String:curSetting[8]; - BoolToConfigOption(GetConVarBool(gCvars[CVAR_ZMARKET_BUYZONE]), curSetting, sizeof(curSetting)); + BoolToConfigSetting(GetConVarBool(gCvars[CVAR_ZMARKET_BUYZONE]), curSetting, sizeof(curSetting)); Format(togglebuyzone, sizeof(togglebuyzone), "%t", "Weapons menu market toggle buyzone", curSetting); diff --git a/src/zr/weapons/restrict.inc b/src/zr/weapons/restrict.inc index a30bade..2304120 100644 --- a/src/zr/weapons/restrict.inc +++ b/src/zr/weapons/restrict.inc @@ -46,9 +46,9 @@ RestrictInit() } /** - * Loads weapon data from file. - */ -RestrictOnMapStart() + * Clears weapon restrict data + */ +RestrictClearData() { // Clear restricted weapons RestrictWeaponUnrestrictAll(); @@ -60,6 +60,15 @@ RestrictOnMapStart() } kvWeaponGroups = CreateKeyValues("weapongroups"); +} + +/** + * Loads weapon data from file. + */ +RestrictOnMapStart() +{ + // Restrict default restrictions (set in weapons.txt) + RestrictDefaultRestrictions(); decl String:path[PLATFORM_MAX_PATH]; BuildPath(Path_SM, path, sizeof(path), "configs/zr/weapons/weapongroups.txt"); @@ -67,10 +76,14 @@ RestrictOnMapStart() // If file isn't found, stop plugin if (!FileToKeyValues(kvWeaponGroups, path)) { - SetFailState("\"%s\" missing from server", path); + if (LogFlagCheck(LOG_CORE_EVENTS, LOG_MODULE_WEAPONS)) + { + ZR_LogMessageFormatted(-1, "Weapons", "Config Validation", "Missing file weapongroups.txt.", LOG_FORMAT_TYPE_FULL); + } + + return; } - RestrictDefaultRestrictions(); RestrictValidateWeaponGroups(); } @@ -84,17 +97,25 @@ RestrictDefaultRestrictions() if (KvGotoFirstSubKey(kvWeapons)) { decl String:weapon[WEAPONS_MAX_LENGTH]; - decl String:display[WEAPONS_MAX_LENGTH + 1]; + decl String:display[WEAPONS_MAX_LENGTH]; do { KvGetSectionName(kvWeapons, weapon, sizeof(weapon)); // If weapon is defaulted to restricted, then restrict weapon - if (WeaponsIsRestrict(weapon)) + decl String:restrict[8]; + KvGetString(kvWeapons, "restrict", restrict, sizeof(restrict), "no"); + + if (ConfigSettingToBool(restrict)) { new WpnRestrictQuery:output = RestrictRestrict(weapon, display); RestrictPrintRestrictOutput(0, output, display, true); + + // Function calls above screwed with the keyvalue stack, so we have to set it back + // to where it was before those calls + KvRewind(kvWeapons); + KvJumpToKey(kvWeapons, weapon); } } while (KvGotoNextKey(kvWeapons)); } @@ -105,29 +126,41 @@ RestrictDefaultRestrictions() */ RestrictValidateWeaponGroups() { + // If log flag check fails, don't log + if (!LogFlagCheck(LOG_CORE_EVENTS, LOG_MODULE_WEAPONS)) + { + return; + } + KvRewind(kvWeaponGroups); if (KvGotoFirstSubKey(kvWeaponGroups)) { + decl String:weapongroup[WEAPONS_MAX_LENGTH]; + decl String:groupweapon[WEAPONS_MAX_LENGTH]; + do { + KvGetSectionName(kvWeaponGroups, weapongroup, sizeof(weapongroup)); + if (KvGotoFirstSubKey(kvWeaponGroups)) { - decl String:groupweapon[WEAPONS_MAX_LENGTH]; - do { - KvGetSectionName(kvWeaponGroups, groupweapon, sizeof(groupweapon)); if (!WeaponsIsValidWeapon(groupweapon)) { - // VALIDATE + ZR_LogMessageFormatted(-1, "Weapon Restrict", "Config Validation", "Invalid weapon \"%s\" in group \"%s\" configured in weapongroups.txt.", LOG_FORMAT_TYPE_ERROR, groupweapon, weapongroup); } } while (KvGotoNextKey(kvWeaponGroups)); KvGoBack(kvWeaponGroups); } + else + { + ZR_LogMessageFormatted(-1, "Weapon Restrict", "Config Validation", "No weapons listed in weapon group \"%s\" in weapongroups.txt.", LOG_FORMAT_TYPE_ERROR, weapongroup); + } } while (KvGotoNextKey(kvWeaponGroups)); } } @@ -208,6 +241,8 @@ public Action:RestrictBuyHook(client, argc) * Restricts a weapon. * * @param weapon The weapon/group name. + * @param display String set to the name set in weapons.txt + * Set to the value of 'weapon' if invalid * @return Successful_Weapon: The call successfully restricted a weapon. * Successful_Group: The call successfully restricted a weapon group. * Failed_Weapon: The call failed to restrict a weapon. @@ -252,13 +287,15 @@ WpnRestrictQuery:RestrictRestrict(const String:weapon[], String:display[] = "") return Successful_Group; } - WeaponGetDisplayName(weapon, display); - if (!WeaponsIsValidWeapon(weapon)) { + strcopy(display, WEAPONS_MAX_LENGTH, weapon); + return Invalid; } + WeaponGetDisplayName(weapon, display); + if (RestrictIsWeaponRestricted(weapon)) { return Failed_Weapon; @@ -273,6 +310,8 @@ WpnRestrictQuery:RestrictRestrict(const String:weapon[], String:display[] = "") * Unrestricts a weapon. * * @param weapon The weapon/group name. + * @param display String set to the name set in weapons.txt + * Set to the value of 'weapon' if invalid * @return Successful_Weapon: The call successfully restricted a weapon. * Successful_Group: The call successfully restricted a weapon group. * Failed_Weapon: The call failed to restrict a weapon. @@ -321,13 +360,15 @@ WpnRestrictQuery:RestrictUnrestrict(const String:weapon[], String:display[] = "" return Successful_Group; } - WeaponGetDisplayName(weapon, display); - if (!WeaponsIsValidWeapon(weapon)) { + strcopy(display, WEAPONS_MAX_LENGTH, weapon); + return Invalid; } + WeaponGetDisplayName(weapon, display); + if (!RestrictIsWeaponRestricted(weapon)) { return Failed_Weapon; @@ -352,10 +393,15 @@ WpnRestrictQuery:RestrictUnrestrict(const String:weapon[], String:display[] = "" RestrictPrintRestrictOutput(client, WpnRestrictQuery:output, const String:weapon[], bool:reply) { switch(output) - { + { case Successful_Weapon: { ZR_PrintToChat(0, "Restrict weapon", weapon); + + if (LogFlagCheck(LOG_GAME_EVENTS, LOG_MODULE_WEAPONS)) + { + ZR_LogMessageFormatted(client, "Weapon Restrict", "Restrict", "\"%L\" restricted weapon: \"%s\".", LOG_FORMAT_TYPE_FULL, client, weapon); + } } case Successful_Group: { @@ -363,6 +409,11 @@ RestrictPrintRestrictOutput(client, WpnRestrictQuery:output, const String:weapon RestrictGetGroupWeapons(weapon, weaponlist, sizeof(weaponlist), ", "); ZR_PrintToChat(0, "Restrict custom weapon group", weapon, weaponlist); + + if (LogFlagCheck(LOG_GAME_EVENTS, LOG_MODULE_WEAPONS)) + { + ZR_LogMessageFormatted(client, "Weapon Restrict", "Restrict", "\"%L\" restricted weapon group: \"%s\".", LOG_FORMAT_TYPE_FULL, client, weapon); + } } case Failed_Weapon: { @@ -417,6 +468,11 @@ RestrictPrintUnrestrictOutput(client, WpnRestrictQuery:output, const String:weap case Successful_Weapon: { ZR_PrintToChat(0, "Unrestrict weapon", weapon); + + if (LogFlagCheck(LOG_GAME_EVENTS, LOG_MODULE_WEAPONS)) + { + ZR_LogMessageFormatted(client, "Weapon Restrict", "Unrestrict", "\"%L\" unrestricted weapon: \"%s\".", LOG_FORMAT_TYPE_FULL, client, weapon); + } } case Successful_Group: { @@ -424,6 +480,11 @@ RestrictPrintUnrestrictOutput(client, WpnRestrictQuery:output, const String:weap RestrictGetGroupWeapons(weapon, weaponlist, sizeof(weaponlist), ", "); ZR_PrintToChat(0, "Unrestrict custom weapon group", weapon, weaponlist); + + if (LogFlagCheck(LOG_GAME_EVENTS, LOG_MODULE_WEAPONS)) + { + ZR_LogMessageFormatted(client, "Weapon Restrict", "Unrestrict", "\"%L\" unrestricted weapon group: \"%s\".", LOG_FORMAT_TYPE_FULL, client, weapon); + } } case Failed_Weapon: { @@ -471,10 +532,11 @@ RestrictPrintUnrestrictOutput(client, WpnRestrictQuery:output, const String:weap */ bool:RestrictIsWeaponRestricted(const String:weapon[]) { + decl String:restrictedweapon[WEAPONS_MAX_LENGTH]; + new size = GetArraySize(gRestrictedWeapons); for (new x = 0; x < size; x++) { - decl String:restrictedweapon[WEAPONS_MAX_LENGTH]; GetArrayString(gRestrictedWeapons, x, restrictedweapon, sizeof(restrictedweapon)); if (StrEqual(weapon, restrictedweapon, false)) @@ -567,10 +629,11 @@ bool:RestrictIsPartialRestricted(const String:weapongroup[]) */ RestrictGetIndex(const String:weapon[]) { + decl String:restrictedweapon[WEAPONS_MAX_LENGTH]; + new size = GetArraySize(gRestrictedWeapons); for (new x = 0; x < size; x++) { - decl String:restrictedweapon[WEAPONS_MAX_LENGTH]; GetArrayString(gRestrictedWeapons, x, restrictedweapon, sizeof(restrictedweapon)); if (StrEqual(weapon, restrictedweapon, false)) diff --git a/src/zr/weapons/weapons.inc b/src/zr/weapons/weapons.inc index e64bc37..75db19d 100644 --- a/src/zr/weapons/weapons.inc +++ b/src/zr/weapons/weapons.inc @@ -24,6 +24,9 @@ new Handle:kvWeapons = INVALID_HANDLE; #include "zr/weapons/markethandler" #include "zr/weapons/menu_weapons" +/** + * Weapons module init function + */ WeaponsInit() { // Forward event to sub-module @@ -31,9 +34,9 @@ WeaponsInit() } /** - * Loads weapon data from file. + * Clears weapon data */ -WeaponsOnMapStart() +WeaponsClearData() { // Load weapon data if (kvWeapons != INVALID_HANDLE) @@ -42,6 +45,18 @@ WeaponsOnMapStart() } kvWeapons = CreateKeyValues("weapons"); +} + +/** + * Loads weapon data from file. + */ +WeaponsOnMapStart() +{ + // Clear weapon data + WeaponsClearData(); + + // Clear weapon restrict data + RestrictClearData(); decl String:path[PLATFORM_MAX_PATH]; BuildPath(Path_SM, path, sizeof(path), "configs/zr/weapons/weapons.txt"); @@ -49,7 +64,12 @@ WeaponsOnMapStart() // If file isn't found, stop plugin if (!FileToKeyValues(kvWeapons, path)) { - SetFailState("\"%s\" missing from server", path); + if (LogFlagCheck(LOG_CORE_EVENTS, LOG_MODULE_WEAPONS)) + { + ZR_LogMessageFormatted(-1, "Weapons", "Config Validation", "Missing file weapons.txt, disabling weapons-based modules.", LOG_FORMAT_TYPE_FULL); + } + + return; } // Validate weapons config @@ -61,22 +81,16 @@ WeaponsOnMapStart() WeaponsValidateWeaponsConfig() { - KvRewind(kvWeapons); - if (KvGotoFirstSubKey(kvWeapons)) + // If log flag check fails, don't log + if (!LogFlagCheck(LOG_CORE_EVENTS, LOG_MODULE_WEAPONS)) { - decl String:weapon[WEAPONS_MAX_LENGTH]; - decl String:restrict[8]; - decl String:menu[8]; - - do - { - KvGetSectionName(kvWeapons, weapon, sizeof(weapon)); - KvGetString(kvWeapons, "restrict", restrict, sizeof(restrict), "no"); - KvGetString(kvWeapons, "menu", menu, sizeof(menu), "yes"); - - // VALIDATE - - } while (KvGotoNextKey(kvWeapons)); + return; + } + + KvRewind(kvWeapons); + if (!KvGotoFirstSubKey(kvWeapons)) + { + ZR_LogMessageFormatted(-1, "Weapons", "Config Validation", "No weapons listed in weapons.txt, disabling weapons-based modules.", LOG_FORMAT_TYPE_FULL); } } @@ -171,35 +185,6 @@ WeaponGetDisplayName(const String:weapon[], String:display[]) } while (KvGotoNextKey(kvWeapons)); } } - -/** - * Checks if a weapon is restricted by default. - * @param weapon The weapon name. - * @return Returns true if restricted, false it not. - */ -bool:WeaponsIsRestrict(const String:weapon[]) -{ - KvRewind(kvWeapons); - if (KvGotoFirstSubKey(kvWeapons)) - { - decl String:validweapon[WEAPONS_MAX_LENGTH]; - decl String:restrict[8]; - - do - { - KvGetSectionName(kvWeapons, validweapon, sizeof(validweapon)); - - if (StrEqual(validweapon, weapon, false)) - { - KvGetString(kvWeapons, "restrict", restrict, sizeof(restrict), "no"); - - return ConfigOptionToBool(restrict); - } - } while (KvGotoNextKey(kvWeapons)); - } - - return false; -} /** * Checks if a weapon restriction can be toggled by the admin menu. @@ -222,7 +207,7 @@ bool:WeaponsIsWeaponMenu(const String:weapon[]) { KvGetString(kvWeapons, "menu", menu, sizeof(menu), "yes"); - return ConfigOptionToBool(menu); + return ConfigSettingToBool(menu); } } while (KvGotoNextKey(kvWeapons)); } diff --git a/src/zr/zombiereloaded.inc b/src/zr/zombiereloaded.inc index 5f899b7..aa9c2ca 100644 --- a/src/zr/zombiereloaded.inc +++ b/src/zr/zombiereloaded.inc @@ -113,7 +113,7 @@ new Handle:tHandles[MAXPLAYERS + 1][MAXTIMERS]; new QueryCookie:mat_dxlevel; -bool:ConfigOptionToBool(const String:option[]) +bool:ConfigSettingToBool(const String:option[]) { if (StrEqual(option, "yes", false)) { @@ -123,7 +123,7 @@ bool:ConfigOptionToBool(const String:option[]) return false; } -BoolToConfigOption(bool:bOption, String:option[], maxlen) +BoolToConfigSetting(bool:bOption, String:option[], maxlen) { if (bOption) {