Skip to content

refactor(netpacket): Simplify NetPacket functions for packet buffer writes and size tests#2329

Open
xezon wants to merge 5 commits intoTheSuperHackers:mainfrom
xezon:xezon/refactor-netpacket-write-size
Open

refactor(netpacket): Simplify NetPacket functions for packet buffer writes and size tests#2329
xezon wants to merge 5 commits intoTheSuperHackers:mainfrom
xezon:xezon/refactor-netpacket-write-size

Conversation

@xezon
Copy link

@xezon xezon commented Feb 19, 2026

Merge with Rebase

This change has 5 commits with refactors/fixes to greatly simplify all Net Packet buffer writes and size tests. All original networking behavior should be correctly preserved.

The only thing left to refactor for a follow up change are the Net Packet read functions.

TODO

  • Test Multiplayer with VC6 clients + Retail clients
  • Test File Transfer
  • Test basic 2 player network matches
  • Add pull id to commits
  • Replicate in Generals
  • Verify each commit compiles on its own

@xezon xezon added this to the Code foundation build up milestone Feb 19, 2026
@xezon xezon added Major 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 labels Feb 19, 2026
@greptile-apps
Copy link

greptile-apps bot commented Feb 19, 2026

Greptile Summary

This PR successfully refactors network packet serialization by introducing a template-based architecture that eliminates thousands of lines of repetitive code.

Key improvements:

  • Removed ~3,400 lines of boilerplate from NetPacket.cpp by replacing command-specific FillBufferWithXXX functions with virtual method dispatch
  • Introduced NetCommandMsgT template base class providing polymorphic serialization interface via getSizeForNetPacket and copyBytesForNetPacket methods
  • Created NetPacketStructs.h/cpp with reusable packet structure templates (NetPacketCommandTemplate, SmallNetPacketCommandTemplate) and helper functions for byte-level operations
  • Consolidated ack message hierarchy by introducing NetAckCommandMsg base class, removing duplicated member variables and methods from NetAckStage1CommandMsg, NetAckStage2CommandMsg, and NetAckBothCommandMsg
  • Added const correctness throughout (getSortNumber, getCommandID, getOriginalPlayerID, getArgumentDataType, etc.)
  • Renamed getPackedByteCount to getSizeForNetPacket for clarity

Network protocol preservation:
The refactoring maintains binary compatibility with existing network protocol by using the same packed structures and field ordering. All original serialization logic is preserved within the new structure-based implementation.

Confidence Score: 5/5

  • Safe to merge - well-structured refactoring that simplifies codebase without changing behavior
  • This is a high-quality refactoring with clear architectural benefits. The changes eliminate significant code duplication through template-based design patterns while preserving network protocol compatibility. The previous issue identified in code review was already fixed. All changes are mechanical transformations with const correctness improvements. The PR author has indicated basic testing has been completed, though the TODO items mention additional testing with VC6 and retail clients plus file transfer testing remain pending.
  • No files require special attention - all changes follow consistent refactoring patterns

Important Files Changed

Filename Overview
Core/GameEngine/Source/GameNetwork/NetPacket.cpp Removed ~3400 lines of boilerplate serialization code, replaced with virtual function calls
Core/GameEngine/Include/GameNetwork/NetPacketStructs.h New file containing network packet structures with helper templates and serialization utilities
Core/GameEngine/Source/GameNetwork/NetPacketStructs.cpp New file implementing packet serialization for all network command types
Core/GameEngine/Include/GameNetwork/NetCommandMsg.h Added template base class, virtual serialization interface, consolidated ack message hierarchy
Core/GameEngine/Source/GameNetwork/NetCommandMsg.cpp Simplified implementation by removing duplicate code in ack messages, added const correctness

Last reviewed commit: 4f72f27

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.

12 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

@xezon xezon force-pushed the xezon/refactor-netpacket-write-size branch 2 times, most recently from aea8b1a to d8f9dc1 Compare February 19, 2026 22:40
@stephanmeesters
Copy link

Would it make sense to use macros? For some of the repeating stuff like:

size_t NetDisconnectPlayerCommandMsg::getSizeForNetPacket() const {
	return NetPacketDisconnectPlayerCommand::getSize(*this);
}

size_t NetDisconnectPlayerCommandMsg::copyBytesForNetPacket(UnsignedByte* buffer, const NetCommandRef& ref) const {
	return NetPacketDisconnectPlayerCommand::copyBytes(buffer, ref);
}

size_t NetDisconnectPlayerCommandMsg::getSizeForSmallNetPacket(const Select* select) const {
	return SmallNetPacketDisconnectPlayerCommand::getSize(*this, select);
}

size_t NetDisconnectPlayerCommandMsg::copyBytesForSmallNetPacket(UnsignedByte* buffer, const NetCommandRef& ref, const Select* select) const {
	return SmallNetPacketDisconnectPlayerCommand::copyBytes(buffer, ref, select);
}
virtual size_t getSizeForNetPacket() const;
virtual size_t copyBytesForNetPacket(UnsignedByte* buffer, const NetCommandRef& ref) const;
virtual size_t getSizeForSmallNetPacket(const Select* select = nullptr) const;
virtual size_t copyBytesForSmallNetPacket(UnsignedByte* buffer, const NetCommandRef& ref, const Select* select = nullptr) const;
virtual Select getSmallNetPacketSelect() const;

@xezon
Copy link
Author

xezon commented Feb 19, 2026

Preferably a template. I will think about it.

@xezon
Copy link
Author

xezon commented Feb 20, 2026

Replicated to Generals.

@xezon xezon force-pushed the xezon/refactor-netpacket-write-size branch from d8f9dc1 to 9c9ffb9 Compare February 20, 2026 22:34
@xezon
Copy link
Author

xezon commented Feb 20, 2026

Would it make sense to use macros? For some of the repeating stuff like:

Simplified in fixup commit with template.

@xezon xezon force-pushed the xezon/refactor-netpacket-write-size branch from 9c9ffb9 to 86faf0d Compare February 23, 2026 18:44
@xezon xezon force-pushed the xezon/refactor-netpacket-write-size branch from 86faf0d to 4f72f27 Compare February 24, 2026 21:48
@xezon
Copy link
Author

xezon commented Mar 2, 2026

Goldfish:

We did already a quicktest with map transfer and 5 min in game without issues.

I was on this patch 1 other guy on retail and then the rest on the superhackers. worked great no issues to be seen. played a like 3 games with it

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

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants