Skip to content

refactor(netpacket): Simplify initialization of net packet field types#2288

Merged
xezon merged 2 commits intoTheSuperHackers:mainfrom
xezon:xezon/refactor-netpacket-header-types
Feb 13, 2026
Merged

refactor(netpacket): Simplify initialization of net packet field types#2288
xezon merged 2 commits intoTheSuperHackers:mainfrom
xezon:xezon/refactor-netpacket-header-types

Conversation

@xezon
Copy link

@xezon xezon commented Feb 10, 2026

Merge with Rebase

This change has 2 commits:

  1. Streamlines the order of the net packet fields for easier reading

  2. Simplifies the initialization of the net packet field types by using constructor initialization and the placement new operator

TODO

  • Add pull id's to commit titles
  • Fix undefined behavior

@xezon xezon added Minor Severity: Minor < Major < Critical < Blocker Network Anything related to network, servers Gen Relates to Generals ZH Relates to Zero Hour Refactor Edits the code with insignificant behavior changes, is never user facing ThisProject The issue was introduced by this project, or this task is specific to this project labels Feb 10, 2026
@greptile-apps
Copy link

greptile-apps bot commented Feb 10, 2026

Greptile Overview

Greptile Summary

Simplified network packet field initialization by adding constructors to field structs that automatically set field type identifiers, eliminating repetitive manual assignments across 23 packet serialization functions.

Key Changes:

  • Reordered NetPacketFieldTypes constants to match the order in which fields appear in packet structs for easier reading
  • Renamed header field to fieldType in all packet field structs for clearer semantics
  • Added constructors to initialize fieldType automatically using member initializer lists
  • Replaced reinterpret_cast with placement new syntax to leverage constructors
  • Removed 69 lines of manual field type assignments (e.g., packet->commandType.header = NetPacketFieldTypes::CommandType)

Technical Correctness:
The placement new approach is safe here because all structs use #pragma pack(1) for 1-byte alignment, ensuring deterministic memory layout without padding. The constructors initialize only the fieldType member via initializer lists before the constructor body runs, leaving other fields for explicit assignment as intended.

Confidence Score: 5/5

  • This PR is safe to merge with no concerns
  • The refactoring is well-designed and mechanically correct. The use of placement new with constructors is safer than the previous reinterpret_cast approach, as it properly constructs objects. The 1-byte packing ensures memory layout compatibility. The change reduces code duplication and eliminates a common source of bugs (forgetting to initialize field types). All 23 functions follow the same pattern consistently.
  • No files require special attention

Important Files Changed

Filename Overview
Core/GameEngine/Include/GameNetwork/NetPacketStructs.h Reordered field type constants to match struct usage order and renamed header to fieldType in all structs; added constructors to initialize field types
Core/GameEngine/Source/GameNetwork/NetPacket.cpp Replaced reinterpret_cast with placement new to leverage struct constructors for automatic field type initialization, removing manual header assignments

Sequence Diagram

sequenceDiagram
    participant Caller
    participant NetPacket
    participant Buffer as UnsignedByte* buffer
    participant Struct as NetPacket*Command

    Caller->>NetPacket: FillBufferWithXXXCommand(buffer, msg)
    Note over NetPacket: Extract command data from msg
    NetPacket->>Buffer: new (buffer) NetPacketXXXCommand
    Note over Buffer,Struct: Placement new constructs object at buffer location
    Struct-->>NetPacket: packet pointer
    Note over Struct: Constructor auto-initializes fieldType
    NetPacket->>Struct: packet->commandType.commandType = value
    NetPacket->>Struct: packet->frame.frame = value
    NetPacket->>Struct: packet->relay.relay = value
    NetPacket->>Struct: packet->playerId.playerId = value
    Note over Struct: Other fields explicitly assigned
    NetPacket-->>Caller: Buffer filled with serialized packet
Loading

Last reviewed commit: a0b6d3f

@xezon xezon force-pushed the xezon/refactor-netpacket-header-types branch from 9e33c4f to a0b6d3f Compare February 12, 2026 21:58
Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

2 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

@xezon xezon requested a review from Mauller February 12, 2026 22:41
Copy link

@Mauller Mauller left a comment

Choose a reason for hiding this comment

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

Looks good, just needs that followup

@xezon
Copy link
Author

xezon commented Feb 13, 2026

@greptileai

@xezon xezon merged commit 6105ba9 into TheSuperHackers:main Feb 13, 2026
25 checks passed
@xezon xezon deleted the xezon/refactor-netpacket-header-types branch February 13, 2026 18:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Gen Relates to Generals Minor Severity: Minor < Major < Critical < Blocker Network Anything related to network, servers Refactor Edits the code with insignificant behavior changes, is never user facing ThisProject The issue was introduced by this project, or this task is specific to this project ZH Relates to Zero Hour

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants