Skip to content

refactor(metaevent): Split MetaEventTranslator::translateGameMessage() into smaller functions#2758

Open
xezon wants to merge 3 commits into
TheSuperHackers:mainfrom
xezon:xezon/refactor-metaevent
Open

refactor(metaevent): Split MetaEventTranslator::translateGameMessage() into smaller functions#2758
xezon wants to merge 3 commits into
TheSuperHackers:mainfrom
xezon:xezon/refactor-metaevent

Conversation

@xezon
Copy link
Copy Markdown

@xezon xezon commented May 30, 2026

This change splits MetaEventTranslator::translateGameMessage() into smaller functions for better maintainability.

Logically it should function the same.

TODO

  • Replicate in Generals

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

greptile-apps Bot commented May 30, 2026

Greptile Summary

This PR refactors MetaEventTranslator::translateGameMessage() by extracting its key-event and mouse-event handling into dedicated private helper methods (onMouseEvent, onKeyEvent, onKeyModStateRemoved, onKeyPressed, getActionKeyType, getKeyModState). The logic is equivalent to the original; the split improves readability and enables finer-grained unit testing.

  • getActionKeyType and getKeyModState are extracted as static helpers, isolating the modifier-key classification and mod-state bit-packing from the main routing logic.
  • onKeyModStateRemoved handles the modifier-key UP path (clearing tracked key+mod-state pairs and firing UP transitions), while onKeyPressed covers all other key-event routing including autorepeat suppression and the fast-forward replay hack.
  • All new helpers are correctly scoped private, addressing the earlier review feedback.

Confidence Score: 5/5

Safe to merge — the change is a pure structural refactoring with no observable behavioral differences.

Every helper method is a direct extraction of the corresponding block from the original monolithic function. The routing conditions, loop structures, break placement, and state-mutation paths are all preserved. The previously raised concern about KEY_STATE_DOWN vs MSG_RAW_KEY_DOWN was confirmed acceptable by the developer and is consistent with how messages are constructed upstream. All new methods are correctly marked private.

No files require special attention.

Important Files Changed

Filename Overview
GeneralsMD/Code/GameEngine/Include/GameClient/MetaEvent.h Adds six private helper method declarations to MetaEventTranslator; correctly placed under a new private: section.
GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/MetaEvent.cpp Implements the six new helper methods extracted from translateGameMessage(). Logic is semantically equivalent to the original; the refactoring improves cohesion without introducing new behavioral changes.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["translateGameMessage(msg)"] --> B{msg type?}
    B -->|RAW_KEY_DOWN / RAW_KEY_UP| C["onKeyEvent(msg, disp)"]
    B -->|RAW_MOUSE_*| D["onMouseEvent(msg)"]
    B -->|other| E[return KEEP_MESSAGE]

    C --> F["getActionKeyType(systemKey) → MappableKeyType"]
    C --> G["getKeyModState(systemKeyState) → MappableKeyModState"]
    C --> H{keyType == MK_NONE AND RAW_KEY_UP?}
    H -->|yes – modifier released| I["onKeyModStateRemoved(disp, keyModState)"]
    H -->|no – normal key event| J["onKeyPressed(disp, systemKeyState, keyType, keyModState)"]

    D --> K{Mouse message type}
    K -->|BUTTON_DOWN| L[Record mouseDownPosition]
    K -->|DOUBLE_CLICK| M[Set doubleClick flag]
    K -->|BUTTON_UP| N[Insert CLICK or DOUBLE_CLICK into stream]
Loading

Reviews (3): Last reviewed commit: "Refactor long conditions" | Re-trigger Greptile

Comment thread GeneralsMD/Code/GameEngine/Include/GameClient/MetaEvent.h
Copy link
Copy Markdown

@RikuAnt RikuAnt left a comment

Choose a reason for hiding this comment

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

Logically sound change. The focus is clearly to just relocate the code into new helper methods, but I would recommend some minor refactors as well.

)
)
{
if( systemKeyState & KEY_STATE_AUTOREPEAT )
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Extract condition and invert to eliminate unnecessary code block

const bool isAutorepeat = (systemKeyState & KEY_STATE_AUTOREPEAT) != 0;
if ( !isAutorepeat ) ...

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.

It is a reasonable suggestions but I will leave it as is in this particular refactor because there is a reasoned comment and commented code in this branch.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The comment could be moved before the inverted test to explain the autorepeat case. Also I would argue whether debug log is worthy commented code or not.

In any case, these are minor remarks so I'm fine if the code remains as is.

//DEBUG_LOG(("Frame %d: MetaEventTranslator::translateGameMessage() normal: %s", TheGameLogic->getFrame(), findGameMessageNameByType(map->m_meta)));
}
if (!(map->m_key == keyDown && map->m_modState == keyDownModState && map->m_transition == UP))
continue;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Consider extracting this condition into a local variable (for example, "isMatchingKeyUp") to improve readability.

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.

Fixed

if (map->m_key == keyType &&
map->m_modState == keyModState && (
(map->m_transition == UP && (systemKeyState & KEY_STATE_UP))
|| (map->m_transition == DOWN && (systemKeyState & KEY_STATE_DOWN))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Consider extracting this condition into a local variable (for example, "isMatchingKeyPressReleaseEdge") to improve readability.

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.

Fixed

@RikuAnt
Copy link
Copy Markdown

RikuAnt commented Jun 1, 2026

I don't have permission to resolve my own comments, so feel free to resolve them. Applies also for approval, so someone elses blessing is needed here. FYI @xezon

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