From 49c5852fc1d3fbac6f504de7d93a416f2d0ce649 Mon Sep 17 00:00:00 2001 From: Sergey Shorokhov Date: Tue, 23 Jan 2024 15:22:42 +0300 Subject: [PATCH] refactor(editor): refactor Editor_ConvertSpawns function (#80) Refactored the `Editor_ConvertSpawns` function to improve code readability and maintainability. Renamed the function from `static bool: Editor_ConvertSpawns()` to `static ConvertOldSpawnsFile(const file[])`. Modified the function to return the count of successfully converted spawns instead of a boolean value. Introduced conditional checks and log messages to handle edge cases, such as when no valid spawn files are found or when an invalid argument count is encountered. Updated the commit message to reflect these changes. --- .../addons/amxmodx/scripting/redm_spawns.sma | 54 +++++++++++++++---- 1 file changed, 44 insertions(+), 10 deletions(-) diff --git a/cstrike/addons/amxmodx/scripting/redm_spawns.sma b/cstrike/addons/amxmodx/scripting/redm_spawns.sma index 38cd597..8b9e860 100644 --- a/cstrike/addons/amxmodx/scripting/redm_spawns.sma +++ b/cstrike/addons/amxmodx/scripting/redm_spawns.sma @@ -685,7 +685,7 @@ static JSON: Editor_LoadSpawns() { return arrSpawns } -static Editor_ConvertSpawns() { +static bool: Editor_ConvertSpawns() { new configsDir[PLATFORM_MAX_PATH] get_configsdir(configsDir, charsmax(configsDir)) @@ -709,24 +709,40 @@ static Editor_ConvertSpawns() { return false } - new count + new filesCount while (next_file(dir, fileName, charsmax(fileName))) { new bool: isSpawnsFile = contain(fileName, ".spawns.cfg") != -1 if (!isSpawnsFile) continue - ConvertOldSpawnsFile(fmt("%s/%s", csdmSpawnsDir, fileName)) - ++count - } + new spawnsCount = ConvertOldSpawnsFile(fmt("%s/%s", csdmSpawnsDir, fileName)) + if (spawnsCount == 0) { + LogMessageEx(Warning, "Editor_ConvertSpawns: File `%s/%s` skipped! Not found valid spawns.", + csdmSpawnsDir, fileName + ) + + continue + } - LogMessageEx(Info, "Editor_ConvertSpawns: Succefully convert `%i` old spawn files.", count) + ++filesCount + } close_dir(dir) + if (!filesCount) { + LogMessageEx(Warning, "Editor_ConvertSpawns: Can't find old spawn files! Check `%s` folder!", + csdmSpawnsDir + ) + + return false + } + + LogMessageEx(Info, "Editor_ConvertSpawns: Succefully convert `%i` old spawn files.", filesCount) + return true } -static bool: ConvertOldSpawnsFile(const file[]) { +static ConvertOldSpawnsFile(const file[]) { new fileName[PLATFORM_MAX_PATH] remove_filepath(file, fileName, charsmax(fileName)) @@ -737,7 +753,7 @@ static bool: ConvertOldSpawnsFile(const file[]) { if (!f) { LogMessageEx(Info, "ConvertOldSpawnsFile: Can't open file `%s`.", file) - return false + return 0 } new JSON: arrSpawns = json_init_array() @@ -746,6 +762,13 @@ static bool: ConvertOldSpawnsFile(const file[]) { fgets(f, data, charsmax(data)) trim(data) + if (!strlen(data)) + continue + + #define IsCommentLine(%1) (%1[0] == ';' || %1[0] == '#' || (%1[0] == '/' && %1[1] == '/')) + if (IsCommentLine(data)) + continue + new strOrigin[3][8], strAngles[3][8], strTeam[3], strVAngles[3][8] new argC = parse( data, @@ -761,8 +784,15 @@ static bool: ConvertOldSpawnsFile(const file[]) { strVAngles[2], charsmax(strVAngles[]) ) - if (argC != 10) + if (argC != 10 && argC != 6 && argC != 3) { + LogMessageEx(Warning, "ConvertOldSpawnsFile: File:`%s`, spawnIdx:#%i: Invalid arguments count: %i.", + file, + json_array_get_count(arrSpawns) + 1, + argC + ) + continue + } { new JSON: spawn = json_init_object() @@ -797,6 +827,10 @@ static bool: ConvertOldSpawnsFile(const file[]) { fclose(f) + new spawnsCount = json_array_get_count(arrSpawns) + if (spawnsCount == 0) + return 0 + new JSON: objSpawns = json_init_object() json_object_set_value(objSpawns, "spawns", arrSpawns) json_free(arrSpawns) @@ -816,7 +850,7 @@ static bool: ConvertOldSpawnsFile(const file[]) { set_fail_state("Can't create file `%s`", filePath) json_free(objSpawns) - return true + return spawnsCount } static GameDLLSpawnsCountFix() {