Skip to content

refactor(selectionxlat): Split switch cases of SelectionTranslator::translateGameMessage into separate functions#2819

Merged
xezon merged 3 commits into
TheSuperHackers:mainfrom
xezon:xezon/refactor-selectionxlat
Jun 29, 2026
Merged

refactor(selectionxlat): Split switch cases of SelectionTranslator::translateGameMessage into separate functions#2819
xezon merged 3 commits into
TheSuperHackers:mainfrom
xezon:xezon/refactor-selectionxlat

Conversation

@xezon

@xezon xezon commented Jun 21, 2026

Copy link
Copy Markdown

This change splits the switch cases of SelectionTranslator::translateGameMessage into separate functions to ease readability and maintainability for humans.

I tested all regular messages and it worked correctly.

Tip: Disable whitespace in diff viewer to make it reviewable.

@xezon xezon added Minor Severity: Minor < Major < Critical < Blocker Refactor Edits the code with insignificant behavior changes, is never user facing labels Jun 21, 2026
@greptile-apps

greptile-apps Bot commented Jun 21, 2026

Copy link
Copy Markdown

Greptile Summary

This PR refactors SelectionTranslator::translateGameMessage by extracting each switch case body into a dedicated private member function, converting break/disp = DESTROY_MESSAGE patterns into explicit return KEEP_MESSAGE / return DESTROY_MESSAGE statements. Logic within each extracted handler is semantically identical to the original.

  • Each switch case in translateGameMessage now delegates to a named handler (e.g., onMouseLeftClick, onRawMousePosition), improving readability and making individual handlers independently unit-testable in the future.
  • The conditional-compilation guards (_ALLOW_DEBUG_CHEATS_IN_RELEASE, RTS_DEBUG) are correctly mirrored in both the header declarations and the .cpp implementations, including the corresponding #endif comments for clarity.
  • The private: section for the new handlers is added after the existing public: block in the header, resulting in two disjoint private: sections — valid C++ but slightly unconventional.

Confidence Score: 5/5

Safe to merge — this is a pure structural refactoring with no logic changes; all message-handling paths are faithfully preserved.

Every switch-case body was mechanically lifted into a named function with return KEEP_MESSAGE / return DESTROY_MESSAGE replacing break and disp = DESTROY_MESSAGE; break. The most complex function (onMouseLeftClick) correctly preserves the selective skipping of clearAttackMoveToMode() for early-exit paths (quit menu visible, hand-of-god, hurt mode, debug selection), and the msg->getType() substitution for the original local t variable is equivalent. No behavioral differences were found after tracing all critical control flows.

No files require special attention. SelectionXlat.cpp carries the bulk of the change but all handler logic was verified against the original.

Important Files Changed

Filename Overview
Core/GameEngine/Include/GameClient/SelectionXlat.h Adds 19 new private handler method declarations with correct preprocessor guards; uses #pragma once per team convention. Two separate private: sections now exist (one at the top, one at the bottom) which is valid but slightly unconventional layout.
Core/GameEngine/Source/GameClient/MessageStream/SelectionXlat.cpp Extracts all switch-case bodies into named handler functions. Early-exit break statements translated to return KEEP_MESSAGE; disp = DESTROY_MESSAGE; break translated to return DESTROY_MESSAGE. The critical clearAttackMoveToMode() call in onMouseLeftClick is preserved — it is only reachable via the normal selection path, matching the original flow. Message type extraction (t - MSG_META_) correctly replaced with msg->getType() - MSG_META_.

Sequence Diagram

%%{init: {'theme': 'neutral'}}%%
sequenceDiagram
    participant Caller
    participant translateGameMessage
    participant Handler

    Caller->>translateGameMessage: msg
    translateGameMessage->>translateGameMessage: "t = msg->getType()"
    alt MSG_META_BEGIN_FORCEATTACK
        translateGameMessage->>Handler: onMetaBeginForceAttack(msg)
        Handler-->>translateGameMessage: KEEP_MESSAGE
    else MSG_RAW_MOUSE_POSITION
        translateGameMessage->>Handler: onRawMousePosition(msg)
        Handler-->>translateGameMessage: KEEP_MESSAGE
    else MSG_MOUSE_LEFT_DOUBLE_CLICK
        translateGameMessage->>Handler: onMouseLeftDoubleClick(msg)
        Handler-->>translateGameMessage: KEEP_MESSAGE or DESTROY_MESSAGE
    else MSG_MOUSEOVER_DRAWABLE_HINT
        translateGameMessage->>Handler: onMouseoverDrawableHint(msg)
        Handler-->>translateGameMessage: KEEP_MESSAGE or DESTROY_MESSAGE
    else MSG_MOUSE_LEFT_CLICK
        translateGameMessage->>Handler: onMouseLeftClick(msg)
        Handler-->>translateGameMessage: KEEP_MESSAGE or DESTROY_MESSAGE
    else MSG_RAW_MOUSE_LEFT_BUTTON_DOWN / UP
        translateGameMessage->>Handler: "onRawMouseLeftButton[Down|Up](msg)"
        Handler-->>translateGameMessage: KEEP_MESSAGE
    else MSG_RAW_MOUSE_RIGHT_BUTTON_DOWN / UP
        translateGameMessage->>Handler: "onRawMouseRightButton[Down|Up](msg)"
        Handler-->>translateGameMessage: KEEP_MESSAGE or DESTROY_MESSAGE
    else MSG_META_CREATE/SELECT/ADD/VIEW_TEAMx
        translateGameMessage->>Handler: onMetaCreateTeam / onMetaSelectTeam / onMetaAddTeam / onMetaViewTeam
        Handler-->>translateGameMessage: DESTROY_MESSAGE
    else MSG_META_OPTIONS
        translateGameMessage->>Handler: onMetaOptions(msg)
        Handler-->>translateGameMessage: KEEP_MESSAGE
    else Debug/Cheat messages
        translateGameMessage->>Handler: "onCheatToggleHandOfGodMode / onMetaDemoToggle* / onMetaDemoDebugSelection"
        Handler-->>translateGameMessage: KEEP_MESSAGE or DESTROY_MESSAGE
    end
    translateGameMessage-->>Caller: disp
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
sequenceDiagram
    participant Caller
    participant translateGameMessage
    participant Handler

    Caller->>translateGameMessage: msg
    translateGameMessage->>translateGameMessage: "t = msg->getType()"
    alt MSG_META_BEGIN_FORCEATTACK
        translateGameMessage->>Handler: onMetaBeginForceAttack(msg)
        Handler-->>translateGameMessage: KEEP_MESSAGE
    else MSG_RAW_MOUSE_POSITION
        translateGameMessage->>Handler: onRawMousePosition(msg)
        Handler-->>translateGameMessage: KEEP_MESSAGE
    else MSG_MOUSE_LEFT_DOUBLE_CLICK
        translateGameMessage->>Handler: onMouseLeftDoubleClick(msg)
        Handler-->>translateGameMessage: KEEP_MESSAGE or DESTROY_MESSAGE
    else MSG_MOUSEOVER_DRAWABLE_HINT
        translateGameMessage->>Handler: onMouseoverDrawableHint(msg)
        Handler-->>translateGameMessage: KEEP_MESSAGE or DESTROY_MESSAGE
    else MSG_MOUSE_LEFT_CLICK
        translateGameMessage->>Handler: onMouseLeftClick(msg)
        Handler-->>translateGameMessage: KEEP_MESSAGE or DESTROY_MESSAGE
    else MSG_RAW_MOUSE_LEFT_BUTTON_DOWN / UP
        translateGameMessage->>Handler: "onRawMouseLeftButton[Down|Up](msg)"
        Handler-->>translateGameMessage: KEEP_MESSAGE
    else MSG_RAW_MOUSE_RIGHT_BUTTON_DOWN / UP
        translateGameMessage->>Handler: "onRawMouseRightButton[Down|Up](msg)"
        Handler-->>translateGameMessage: KEEP_MESSAGE or DESTROY_MESSAGE
    else MSG_META_CREATE/SELECT/ADD/VIEW_TEAMx
        translateGameMessage->>Handler: onMetaCreateTeam / onMetaSelectTeam / onMetaAddTeam / onMetaViewTeam
        Handler-->>translateGameMessage: DESTROY_MESSAGE
    else MSG_META_OPTIONS
        translateGameMessage->>Handler: onMetaOptions(msg)
        Handler-->>translateGameMessage: KEEP_MESSAGE
    else Debug/Cheat messages
        translateGameMessage->>Handler: "onCheatToggleHandOfGodMode / onMetaDemoToggle* / onMetaDemoDebugSelection"
        Handler-->>translateGameMessage: KEEP_MESSAGE or DESTROY_MESSAGE
    end
    translateGameMessage-->>Caller: disp
Loading

Reviews (4): Last reviewed commit: "Rename ForceAttack" | Re-trigger Greptile

Comment thread Core/GameEngine/Include/GameClient/SelectionXlat.h Outdated
Comment thread Core/GameEngine/Source/GameClient/MessageStream/SelectionXlat.cpp Outdated
Comment thread Core/GameEngine/Source/GameClient/MessageStream/SelectionXlat.cpp
Comment thread Core/GameEngine/Source/GameClient/MessageStream/SelectionXlat.cpp Outdated
@xezon xezon force-pushed the xezon/refactor-selectionxlat branch from 9295007 to e0f1d54 Compare June 28, 2026 17:39
@xezon

xezon commented Jun 28, 2026

Copy link
Copy Markdown
Author

Rebased on main. 2 merge conflicts.

@Caball009 Caball009 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It was a bit tricky to check all the return statements. Looks good to me.

if (pickedObj == nullptr || !pickedObj->isLocallyControlled())
break;
// Single point. If there's a unit in there, double click will select all of them.
Bool singlePoint = region.height() == 0 && region.width() == 0;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

const :)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I left it non-const because the various other locals in this function aren't either.

@xezon xezon merged commit f7d680f into TheSuperHackers:main Jun 29, 2026
17 checks passed
@xezon xezon deleted the xezon/refactor-selectionxlat branch June 29, 2026 16:03
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 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.

2 participants