Skip to content

fix(network): correct dest inversion in newPacket(nbt) and null packet size (BUG-058, BUG-059)#54

Open
ximaks00-hue wants to merge 1 commit into
akki697222:dev-MC1.20from
ximaks00-hue:fix/bug-058-059-network-packet
Open

fix(network): correct dest inversion in newPacket(nbt) and null packet size (BUG-058, BUG-059)#54
ximaks00-hue wants to merge 1 commit into
akki697222:dev-MC1.20from
ximaks00-hue:fix/bug-058-059-network-packet

Conversation

@ximaks00-hue

Copy link
Copy Markdown
Contributor

Summary

Two regressions in Network.scala affecting packet routing and size calculation.


BUG-058 — newPacket(nbt) inverts dest key check

File: src/main/scala/li/cil/oc/server/network/Network.scala:567-569

Root cause: The condition loading the destination field from NBT is inverted:

// CE (broken)
val destination =
  if (nbt.contains("dest")) null      // "dest" present → null (broadcast!)
  else nbt.getString("dest")          // "dest" absent  → "" (broadcast too)

Save side (line 727) correctly writes "dest" only when destination != null && !destination.isEmpty. So:

  • Targeted packet saved with dest = "node-abc" → NBT contains key "dest"
  • On load: nbt.contains("dest")truedestination = null → packet becomes broadcast

Impact: Any targeted modem packet (linked card, relay with unicast) saved in a hub/switch queue is silently converted to a broadcast after world reload. Routing breaks for all targeted transmissions that survive a reload cycle.

Note: This bug is identical in Original 1.12.2 (if (nbt.hasKey("dest")) null else getString). It is not a CE regression per se, but the CE code inherits it — correcting it here brings CE ahead of Original.

Fix: Swap the branches:

val destination =
  if (nbt.contains("dest")) nbt.getString("dest")
  else null

BUG-059 — Packet.size counts null/None/unit as 4 bytes instead of 1

File: src/main/scala/li/cil/oc/server/network/Network.scala:709

Root cause:

// CE (broken)
case null | ResultWrapper.unit | None => 4
// Original
case null | Unit | None => 1

In OC's packet size calculation, a null/None/unit value represents an empty/void slot. Original counted it as 1 byte. CE counts it as 4 bytes — a 4x inflation for void slots.

Impact:

  • Packets with many sparse null / None values hit maxNetworkPacketSize ~4x sooner
  • Relay/linked card energy cost calculation (based on packet size) is inflated
  • Packets that Original 1.12 would accept are rejected by CE

Fix: => 1 (matches Original).


Verification

  • BUG-058: save writes "dest" when targeted → contains is true on load → inverted condition assigns null → routing bug confirmed.
  • BUG-059: 10 null values → CE size 40 bytes; Original 10 bytes. At maxNetworkPacketSize = 8192, CE rejects packets that are mostly sparse data.

Test plan

  • Send targeted modem message, save/reload world, verify packet delivered to correct recipient (not broadcast)
  • Send packet with null slots across relay — verify energy cost matches expected

@akki697222

Copy link
Copy Markdown
Owner

This is a complex network change, so we'll wait for testing.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants