Move packet data into a dedicated package in network

This commit is contained in:
Firenoo committed 2023-07-27 20:51:47 -07:00
1 parent 2e0a69441c
commit bddf2544e4
6 files changed
+170 -65

No files matched your search

@@ -1,12 +0,0 @@
package com.projecturanus.betterp2p.capability
import com.projecturanus.betterp2p.client.gui.widget.GuiScale
import com.projecturanus.betterp2p.item.BetterMemoryCardModes
import com.projecturanus.betterp2p.network.NONE_SELECTED
const val TUNNEL_ANY: Int = -1
data class MemoryInfo(var selectedEntry: Long = NONE_SELECTED,
var frequency: Long = 0,
var mode: BetterMemoryCardModes = BetterMemoryCardModes.OUTPUT,
var gui: GuiScale = GuiScale.DYNAMIC,
var type: Int = TUNNEL_ANY)
@@ -1,10 +0,0 @@
package com.projecturanus.betterp2p.capability
data class P2PTunnelInfo(
var posX: Int = 0,
var posY: Int = 0,
var posZ: Int = 0,
var world: Int = 0,
var side: Int = 0,
var name:String? = null
)
@@ -1,34 +0,0 @@
package com.projecturanus.betterp2p.network
import com.projecturanus.betterp2p.client.gui.InfoWrapper
import net.minecraftforge.common.util.ForgeDirection
class P2PInfo(val frequency: Long,
val posX: Int,
val posY: Int,
val posZ: Int,
val world: Int,
val facing: ForgeDirection,
val name: String,
val output: Boolean,
val hasChannel: Boolean,
// # of channels if ME P2P, or -1 else
val channels: Int,
// type of p2p, corresponds to registry in proxy
val type: Int) {
override fun hashCode(): Int {
return hashP2P(posX, posY, posZ, facing.ordinal, world).hashCode()
}
override fun equals(other: Any?): Boolean {
return if (other is InfoWrapper) {
this.posX == other.posX &&
this.posY == other.posY &&
this.posZ == other.posZ &&
this.facing == other.facing
} else {
false
}
}
}
@@ -1,8 +1,7 @@
package com.projecturanus.betterp2p.network
package com.projecturanus.betterp2p.network.data
import appeng.parts.p2p.PartP2PTunnel
/**
* Generates a 64-bit hash code from X, Y, Z, Facing, and Dimension info.
* bits 0-47: FashHash of (x, y, z) ->
@@ -11,11 +10,11 @@ import appeng.parts.p2p.PartP2PTunnel
* bit 63: Reserved
*/
fun hashP2P(x: Int, y: Int, z: Int, facing: Int, dim: Int): Long {
var ret = facing.toULong() shl 61
val ret = facing.toULong() shl 60
val lo: ULong = x.toULong() or (y.toULong() shl 32)
val hi: ULong = z.toULong() or (dim.toULong() shl 32)
var hash = hashLen16(lo, hi)
hash = hash xor (hash shr 60)
hash = hash xor (hash shr 59)
return (ret or hash).toLong()
}
@@ -54,8 +53,3 @@ private fun hashLen16(lo: ULong, hi: ULong): ULong {
return f
}
/**
* Using `0x80000000` to represent none is selected (aka MSB set)
*/
const val NONE_SELECTED: Long = Long.MIN_VALUE
@@ -0,0 +1,58 @@
package com.projecturanus.betterp2p.network.data
import com.projecturanus.betterp2p.client.gui.widget.GuiScale
import com.projecturanus.betterp2p.item.BetterMemoryCardModes
import io.netty.buffer.ByteBuf
/**
* Using `-1` to represent any tunnel type.
*/
const val TUNNEL_ANY: Int = -1
/**
* A data class for serializing p2p
* @param selectedEntry Points to the index of the selected p2p in the list.
* @param frequency Selected Frequency. 0 = not selected
* @param mode The mode
* @param guiScale The saved gui scale
* @param type The P2P type, indexed in TunnelInfo.kt
*/
data class MemoryInfo(
var selectedEntry: P2PLocation? = null,
var frequency: Long = 0,
var mode: BetterMemoryCardModes = BetterMemoryCardModes.OUTPUT,
var guiScale: GuiScale = GuiScale.DYNAMIC,
var type: Int = TUNNEL_ANY)
fun readMemoryInfo(buf: ByteBuf): MemoryInfo {
var selectedEntry: P2PLocation? = null
if (buf.readBoolean()) {
selectedEntry = readP2PLocation(buf)
}
val frequency = buf.readLong()
val mode = try {
BetterMemoryCardModes.values()[buf.readInt()]
} catch (e: Exception) {
BetterMemoryCardModes.OUTPUT
}
val guiScale = try {
GuiScale.values()[buf.readByte().toInt()]
} catch (e: ArrayIndexOutOfBoundsException) {
GuiScale.DYNAMIC
}
val type = buf.readByte().toInt()
return MemoryInfo(selectedEntry, frequency, mode, guiScale, type)
}
fun writeMemoryInfo(buf: ByteBuf, info: MemoryInfo) {
val hasSelected = info.selectedEntry != null
buf.writeBoolean(hasSelected)
if (hasSelected) {
writeP2PLocation(buf, info.selectedEntry!!)
}
buf.writeLong(info.frequency)
buf.writeInt(info.mode.ordinal)
buf.writeByte(info.guiScale.ordinal)
buf.writeByte(info.type)
}
@@ -0,0 +1,109 @@
package com.projecturanus.betterp2p.network.data
import net.minecraftforge.common.util.ForgeDirection
import com.projecturanus.betterp2p.util.p2p.getTypeIndex
import com.projecturanus.betterp2p.util.p2p.hasChannel
import appeng.me.GridNode
import appeng.parts.p2p.PartP2PTunnel
import io.netty.buffer.ByteBuf
/**
* Sent over the network to clients.
*/
data class P2PInfo(
val frequency: Long,
val posX: Int,
val posY: Int,
val posZ: Int,
val dim: Int,
val facing: ForgeDirection,
val name: String,
val output: Boolean,
val hasChannel: Boolean,
// # of channels if ME P2P, or -1 else
val channels: Int,
// type of p2p, corresponds to registry in proxy
val type: Int) {
override fun hashCode(): Int {
return hashP2P(posX, posY, posZ, facing.ordinal, dim).hashCode()
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as P2PInfo
if (this.posX != other.posX) return false
if (this.posY != other.posY) return false
if (this.posZ != other.posZ) return false
if (facing != other.facing) return false
return dim == other.dim
}
}
/**
* Deserialize the P2P info over network traffic.
*/
fun readP2PInfo(buf: ByteBuf): P2PInfo? {
try {
val freq = buf.readLong()
val posX = buf.readInt()
val posY = buf.readInt()
val posZ = buf.readInt()
val world = buf.readInt()
val facing = ForgeDirection.values()[buf.readInt()]
val nameLength = buf.readShort() - 1
val name: StringBuilder = StringBuilder()
for (i in 0..nameLength) {
name.append(buf.readChar())
}
val output = buf.readBoolean()
val hasChannel = buf.readBoolean()
val channels = buf.readByte().toInt()
val type = buf.readByte().toInt()
return P2PInfo(freq, posX, posY, posZ,world, facing, name.toString(), output, hasChannel, channels, type)
} catch (e: Exception) {
e.printStackTrace()
return null
}
}
/**
* Serialize the P2P over the network
*/
fun writeP2PInfo(buf: ByteBuf, info: P2PInfo) {
buf.writeLong(info.frequency)
buf.writeInt(info.posX)
buf.writeInt(info.posY)
buf.writeInt(info.posZ)
buf.writeInt(info.dim)
buf.writeInt(info.facing.ordinal)
buf.writeShort(info.name.length)
for (c in info.name) {
buf.writeChar(c.code)
}
buf.writeBoolean(info.output)
buf.writeBoolean(info.hasChannel)
buf.writeByte(info.channels)
buf.writeByte(info.type)
}
/**
* Convert to [P2PInfo] by extracting the necessary fields
*/
fun PartP2PTunnel<*>.toInfo() = P2PInfo(
frequency,
location.x,
location.y,
location.z,
location.dimension,
side,
customName,
isOutput,
hasChannel,
(externalFacingNode as? GridNode)?.usedChannels() ?: -1,
getTypeIndex())