Skip to content

Commit

Permalink
refactor(editor): refactor Editor_ConvertSpawns function (#80)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
SergeyShorokhov authored Jan 23, 2024
1 parent fb39945 commit 49c5852
Showing 1 changed file with 44 additions and 10 deletions.
54 changes: 44 additions & 10 deletions cstrike/addons/amxmodx/scripting/redm_spawns.sma
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand All @@ -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))

Expand All @@ -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()
Expand All @@ -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,
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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() {
Expand Down

0 comments on commit 49c5852

Please sign in to comment.