diff --git a/cstrike/addons/amxmodx/scripting/ReDeathmatch.sma b/cstrike/addons/amxmodx/scripting/ReDeathmatch.sma index 9ac6f56..72c6c19 100644 --- a/cstrike/addons/amxmodx/scripting/ReDeathmatch.sma +++ b/cstrike/addons/amxmodx/scripting/ReDeathmatch.sma @@ -72,9 +72,9 @@ public plugin_cfg() { CvarsHandler_Init() Features_Init() SpawnManager_Init() - RoundModes_Init() EquipManager_Init() Config_Init() + RoundModes_Init() SetActive(redm_active) } diff --git a/cstrike/addons/amxmodx/scripting/ReDeathmatch/Modes/Menus.inc b/cstrike/addons/amxmodx/scripting/ReDeathmatch/Modes/Menus.inc index 2511cde..792131c 100644 --- a/cstrike/addons/amxmodx/scripting/ReDeathmatch/Modes/Menus.inc +++ b/cstrike/addons/amxmodx/scripting/ReDeathmatch/Modes/Menus.inc @@ -24,18 +24,11 @@ public ClCmd_redm_modes_menu(const player, const level, const commandId) { } static bool: Menu_Modes(const player) { - if (!json_object_has_value(Config_GetCurrent(), "modes")) + new count + new JSON: arrModes = RoundModes_GetModesArr(count) + if (!count) return false - new JSON: arrModes = json_object_get_value(Config_GetCurrent(), "modes") - - new count = json_array_get_count(arrModes) - if (!count) { - json_free(arrModes) - - return false - } - new callback if (!callback) callback = menu_makecallback("MenuCallback_Modes") @@ -78,8 +71,6 @@ static bool: Menu_Modes(const player) { json_free(objMode) } - - json_free(arrModes) menu_setprop(menu, MPROP_BACKNAME, fmt("%l", "BACK")) menu_setprop(menu, MPROP_NEXTNAME, fmt("%l", "MORE")) @@ -129,27 +120,17 @@ public MenuHandler_Modes(const player, const menu, const item) { } Menu_GetVoteItemsCount() { - if (!json_object_has_value(Config_GetCurrent(), "modes")) - return false - - new JSON: arrModes = json_object_get_value(Config_GetCurrent(), "modes") - new count = json_array_get_count(arrModes) - json_free(arrModes) + new count + RoundModes_GetModesArr(count) return count } bool: Menu_VoteMode(const player = 0) { - if (!json_object_has_value(Config_GetCurrent(), "modes")) - return false - - new JSON: arrModes = json_object_get_value(Config_GetCurrent(), "modes") - - new count = json_array_get_count(arrModes) - if (!count) { - json_free(arrModes) + new count + new JSON: arrModes = RoundModes_GetModesArr(count) + if (!count) return false - } new menu = menu_create("Choose", "MenuHandler_ChooseNextMode") diff --git a/cstrike/addons/amxmodx/scripting/ReDeathmatch/ReDM_config.inc b/cstrike/addons/amxmodx/scripting/ReDeathmatch/ReDM_config.inc index 30bdfff..389e6e2 100644 --- a/cstrike/addons/amxmodx/scripting/ReDeathmatch/ReDM_config.inc +++ b/cstrike/addons/amxmodx/scripting/ReDeathmatch/ReDM_config.inc @@ -128,6 +128,7 @@ Config_Reload() { equipConfig = Config_GetMainEquip() EquipManager_LoadConfig(equipConfig) + RoundModes_ReadModes() json_free(equipConfig) } @@ -206,30 +207,18 @@ JSON: Config_GetCurrent() { * @return The equipment configuration for the current game mode as a JSON object. */ JSON: Config_GetModeEquip() { - if (!json_object_has_value(Config_GetCurrent(), "modes")) { + new count + new JSON: arrModes = RoundModes_GetModesArr() + if (!count) return Invalid_JSON - } - - new JSON: arrModes = json_object_get_value(Config_GetCurrent(), "modes") - new count = json_array_get_count(arrModes) - if (!count) { - json_free(arrModes) - - return Invalid_JSON - } new currentModeIdx = RoundModes_GetCurrentMode() - if (currentModeIdx < 0) { - json_free(arrModes) - + if (currentModeIdx < 0) return Invalid_JSON - } - new JSON: objMode = json_array_get_value(arrModes, currentModeIdx) if (!json_object_has_value(objMode, "equip")) { json_free(objMode) - json_free(arrModes) return Invalid_JSON } @@ -239,7 +228,6 @@ JSON: Config_GetModeEquip() { json_free(objEquip) json_free(objMode) - json_free(arrModes) return obj } diff --git a/cstrike/addons/amxmodx/scripting/ReDeathmatch/ReDM_round_modes.inc b/cstrike/addons/amxmodx/scripting/ReDeathmatch/ReDM_round_modes.inc index 45b1f6f..10b808d 100644 --- a/cstrike/addons/amxmodx/scripting/ReDeathmatch/ReDM_round_modes.inc +++ b/cstrike/addons/amxmodx/scripting/ReDeathmatch/ReDM_round_modes.inc @@ -5,6 +5,8 @@ enum { RoundMode_NotSet = -1, /**< Round game-mode is not set */ } +static JSON: g_arrModes = Invalid_JSON + static g_currentRoundModeIdx = RoundMode_NotSet static g_forcedModeIdx = RoundMode_NotSet @@ -27,22 +29,41 @@ RoundModes_Init() { ModesMenus_Init() ModeVote_Init() + + RoundModes_ReadModes() +} + +RoundModes_ReadModes() { + if (g_arrModes != Invalid_JSON) + json_free(g_arrModes) + + if (!json_object_has_value(Config_GetCurrent(), "modes")) + return + + g_arrModes = json_object_get_value(Config_GetCurrent(), "modes") } /** * Handles mode voting and mode switching. */ RoundModes_RoundEnd() { - new bool: voteStarted = ModeVote_RoundEnd_Post() + if (strcmp(redm_modes_switch, "disable") == 0) + return - if (voteStarted) { - client_print(0, print_center, "%l %l.", "Selecting", "NextGameMode") + new count + RoundModes_GetModesArr(count) + if (!count) { + LogMessageEx(Warning, "RoundModes_RoundEnd(): Can't load modes list from config!") return } - if (strcmp(redm_modes_switch, "disable") == 0) + new bool: voteStarted = ModeVote_RoundEnd_Post() + if (voteStarted) { + client_print(0, print_center, "%l %l.", "Selecting", "NextGameMode") + return + } new nextModeIdx = GetNextMode() if (nextModeIdx == RoundMode_NotSet) @@ -123,20 +144,15 @@ public RoundInfoMessage(const player) { * * @return A reference to the JSON array containing game modes, or `Invalid_JSON` if not found. */ -static JSON: GetModesArr(& count = 0) { - if (!json_object_has_value(Config_GetCurrent(), "modes")) +JSON: RoundModes_GetModesArr(& count = 0) { + if (g_arrModes == Invalid_JSON) return Invalid_JSON - new JSON: arrModes = json_object_get_value(Config_GetCurrent(), "modes") - - count = json_array_get_count(arrModes) - if (!count) { - json_free(arrModes) - + count = json_array_get_count(g_arrModes) + if (!count) return Invalid_JSON - } - - return arrModes + + return g_arrModes } /** @@ -150,20 +166,16 @@ static JSON: GetModesArr(& count = 0) { */ bool: RoundModes_GetModeInfo(const modeIdx, name[] = "", len = 0) { new count - new JSON: arrModes = GetModesArr(count) - if (arrModes == Invalid_JSON) + new JSON: arrModes = RoundModes_GetModesArr(count) + if (!count) return false - if (modeIdx < 0 || modeIdx >= count) { - json_free(arrModes) - + if (modeIdx < 0 || modeIdx >= count) return false - } new JSON: objMode = json_array_get_value(arrModes, modeIdx) json_object_get_string(objMode, "name", name, len) - json_free(arrModes) json_free(objMode) return true @@ -171,15 +183,13 @@ bool: RoundModes_GetModeInfo(const modeIdx, name[] = "", len = 0) { static GetNextMode() { new count - new JSON: arrModes = GetModesArr(count) - if (arrModes == Invalid_JSON) - return RoundMode_NotSet + RoundModes_GetModesArr(count) - if (RoundModes_IsModeForced()) { - json_free(arrModes) + if (!count) + return RoundMode_NotSet + if (RoundModes_IsModeForced()) return g_forcedModeIdx - } new currentIdx = g_currentRoundModeIdx @@ -204,7 +214,7 @@ static bool: ApplyMode(const modeIdx) { Config_Reload() - new JSON: arrModes = GetModesArr() + new JSON: arrModes = RoundModes_GetModesArr() if (arrModes == Invalid_JSON) return false @@ -218,7 +228,6 @@ static bool: ApplyMode(const modeIdx) { } json_free(objMode) - json_free(arrModes) return true }