Skip to content

refactor(network): Clean up utility functions for network commands#2725

Open
Caball009 wants to merge 7 commits into
TheSuperHackers:mainfrom
Caball009:refactor_network_if_else_chains
Open

refactor(network): Clean up utility functions for network commands#2725
Caball009 wants to merge 7 commits into
TheSuperHackers:mainfrom
Caball009:refactor_network_if_else_chains

Conversation

@Caball009
Copy link
Copy Markdown

@Caball009 Caball009 commented May 17, 2026

This PR cleans up some utility functions in the network code, mainly by refactoring them from if/else chains to switch statements. No change should have a functional difference.

See commits for cleaner (per function) diffs.

@Caball009 Caball009 added Minor Severity: Minor < Major < Critical < Blocker Network Anything related to network, servers Refactor Edits the code with insignificant behavior changes, is never user facing labels May 17, 2026
@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented May 17, 2026

Greptile Summary

This PR refactors several utility functions in NetworkUtil.cpp from if/else chains to switch statements, adds const correctness to two function signatures, and cleans up GetNetCommandTypeAsString using a CASE_LABEL stringify macro. The PR author explicitly acknowledges the removed DEBUG_CRASH assertion in the description.

  • DoesCommandRequireACommandID, IsCommandSynchronized, and CommandRequiresDirectSend are converted to switch statements with identical case sets to their predecessors.
  • CommandRequiresAck is simplified to delegate directly to DoesCommandRequireACommandID, which is semantically valid since both functions previously had the same set of covered types.
  • GetNetCommandTypeAsString gains coverage for NETCOMMANDTYPE_DISCONNECTSTART and NETCOMMANDTYPE_DISCONNECTEND (previously absent from the switch) and now places NETCOMMANDTYPE_UNKNOWN as an explicit case before default:, so it no longer triggers the debug crash.

Confidence Score: 5/5

Safe to merge — all refactored functions preserve their original enum-type coverage, and the two previously-flagged issues have been resolved.

Every function's covered set of NetCommandType values matches the original if/else chains exactly. CommandRequiresAck correctly delegates to DoesCommandRequireACommandID whose case list is identical to the former. GetNetCommandTypeAsString now fully covers NETCOMMANDTYPE_DISCONNECTSTART and NETCOMMANDTYPE_DISCONNECTEND which were previously unhandled. No functional regressions were found.

No files require special attention.

Important Files Changed

Filename Overview
Core/GameEngine/Source/GameNetwork/NetworkUtil.cpp Refactors five utility functions from if/else chains to switch statements; adds two previously-missing enum values to GetNetCommandTypeAsString; simplifies CommandRequiresAck to delegate to DoesCommandRequireACommandID. All covered enum sets match the originals.
Core/GameEngine/Include/GameNetwork/networkutil.h Adds const qualifiers to CommandRequiresAck and CommandRequiresDirectSend parameters — a straightforward const-correctness improvement with no semantic change.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Caller] --> B{CommandRequiresAck\nconst NetCommandMsg*}
    B --> C[DoesCommandRequireACommandID\nNetCommandType]
    A --> D{CommandRequiresDirectSend\nconst NetCommandMsg*}
    A --> E{IsCommandSynchronized\nNetCommandType}
    A --> F{GetNetCommandTypeAsString\nNetCommandType}

    C --> G{switch on type}
    G -->|GAMECOMMAND, FRAMEINFO,\nPLAYERLEAVE, RUNAHEAD*,\nDESTROYPLAYER, CHAT,\nLOADCOMPLETE, TIMEOUTSTART,\nWRAPPER, FILE*, FRAMERESENDREQUEST,\nDISCONNECT*| H[return TRUE]
    G -->|default| I[return FALSE]

    D --> J{switch on type}
    J -->|LOADCOMPLETE, TIMEOUTSTART,\nFILE*, FRAMERESENDREQUEST,\nDISCONNECTPLAYER, DISCONNECTVOTE,\nDISCONNECTFRAME, DISCONNECTSCREENOFF| K[return TRUE]
    J -->|default| L[return FALSE]

    E --> M{switch on type}
    M -->|FRAMEINFO, GAMECOMMAND,\nPLAYERLEAVE, RUNAHEAD,\nDESTROYPLAYER| N[return TRUE]
    M -->|default| O[return FALSE]

    F --> P{switch on type}
    P -->|NETCOMMANDTYPE_UNKNOWN| Q[return string name]
    P -->|default| R[DEBUG_CRASH +\nreturn invalid string]
    P -->|all other known types| S[return string name via CASE_LABEL]
Loading

Reviews (5): Last reviewed commit: "Changed default / unknown case for 'GetN..." | Re-trigger Greptile

Comment thread Core/GameEngine/Source/GameNetwork/NetworkUtil.cpp Outdated
Comment thread Core/GameEngine/Source/GameNetwork/NetworkUtil.cpp
Comment thread Core/GameEngine/Source/GameNetwork/NetworkUtil.cpp
Comment thread Core/GameEngine/Source/GameNetwork/NetworkUtil.cpp
Comment thread Core/GameEngine/Source/GameNetwork/NetworkUtil.cpp
@Caball009 Caball009 force-pushed the refactor_network_if_else_chains branch from 4d28261 to 4d43c80 Compare May 18, 2026 17:05
switch (type) {
default: return "NETCOMMANDTYPE_UNKNOWN";
CASE_LABEL(NETCOMMANDTYPE_UNKNOWN)
default:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move this to the very end? Then it is consistent with the other switch cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Minor Severity: Minor < Major < Critical < Blocker Network Anything related to network, servers Refactor Edits the code with insignificant behavior changes, is never user facing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants