@@ -122,6 +122,18 @@ void NetCommandList::reset() {
122122 m_lastMessageInserted = nullptr ;
123123}
124124
125+ static bool isCommandIdNewer (UnsignedShort newVal, UnsignedShort oldVal)
126+ {
127+ #if RETAIL_COMPATIBLE_NETWORKING
128+ return newVal > oldVal;
129+ #else
130+ // TheSuperHackers @bugfix Caball009 14/06/2026 Ensure messages are sorted
131+ // chronologically by including a command id overflow check.
132+ const UnsignedShort diff = newVal - oldVal;
133+ return diff != 0 && diff < 0x8000 ;
134+ #endif
135+ }
136+
125137/* *
126138 * Insert sorts msg. Assumes that all the previous message inserts were done using this function.
127139 * The message is sorted in based first on command type, then player id, and then command id.
@@ -157,10 +169,10 @@ NetCommandRef * NetCommandList::addMessage(NetCommandRef *&msg) {
157169 NetCommandRef *theNext = m_lastMessageInserted->getNext ();
158170 if ((m_lastMessageInserted->getCommand ()->getNetCommandType () == msg->getCommand ()->getNetCommandType ()) &&
159171 (m_lastMessageInserted->getCommand ()->getPlayerID () == msg->getCommand ()->getPlayerID ()) &&
160- (m_lastMessageInserted ->getCommand ()->getID () < msg ->getCommand ()->getID ()) &&
172+ isCommandIdNewer (msg ->getCommand ()->getID (), m_lastMessageInserted ->getCommand ()->getID ()) &&
161173 ((theNext == nullptr ) || ((theNext->getCommand ()->getNetCommandType () > msg->getCommand ()->getNetCommandType ()) ||
162174 (theNext->getCommand ()->getPlayerID () > msg->getCommand ()->getPlayerID ()) ||
163- (theNext->getCommand ()->getID () > msg->getCommand ()->getID ())))) {
175+ isCommandIdNewer (theNext->getCommand ()->getID (), msg->getCommand ()->getID ())))) {
164176
165177 // Make sure this command isn't already in the list.
166178 if (isEqualCommandMsg (m_lastMessageInserted->getCommand (), msg->getCommand ())) {
@@ -281,7 +293,10 @@ NetCommandRef * NetCommandList::addMessage(NetCommandRef *&msg) {
281293
282294 // Find the position within the player's section based on the command ID.
283295 // If the command type doesn't require a command ID, sort by whatever it should be sorted by.
284- while ((tempmsg != nullptr ) && (msg->getCommand ()->getNetCommandType () == tempmsg->getCommand ()->getNetCommandType ()) && (msg->getCommand ()->getPlayerID () == tempmsg->getCommand ()->getPlayerID ()) && (msg->getCommand ()->getSortNumber () > tempmsg->getCommand ()->getSortNumber ())) {
296+ while (tempmsg != nullptr
297+ && msg->getCommand ()->getNetCommandType () == tempmsg->getCommand ()->getNetCommandType ()
298+ && msg->getCommand ()->getPlayerID () == tempmsg->getCommand ()->getPlayerID ()
299+ && isCommandIdNewer (msg->getCommand ()->getSortNumber (), tempmsg->getCommand ()->getSortNumber ())) {
285300 tempmsg = tempmsg->getNext ();
286301 }
287302
0 commit comments