Skip to content

Commit 582ea50

Browse files
committed
Packetlogger: More granular scoping
1 parent 1cc6ea2 commit 582ea50

File tree

2 files changed

+47
-17
lines changed

2 files changed

+47
-17
lines changed

common/src/main/kotlin/com/lambda/module/modules/Packetlogger.kt

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import com.lambda.event.events.TickEvent
88
import com.lambda.event.listener.UnsafeListener.Companion.unsafeConcurrentListener
99
import com.lambda.event.listener.UnsafeListener.Companion.unsafeListener
1010
import com.lambda.module.Module
11+
import com.lambda.module.tag.ModuleTag
1112
import com.lambda.util.Communication
1213
import com.lambda.util.Communication.info
1314
import com.lambda.util.DynamicReflectionSerializer.dynamicString
@@ -26,23 +27,41 @@ import kotlin.io.path.pathString
2627

2728
object Packetlogger : Module(
2829
name = "Packetlogger",
29-
description = "Logs packets",
30-
defaultTags = setOf()
30+
description = "Serializes network traffic and persists it for later analysis",
31+
defaultTags = setOf(ModuleTag.DEBUG)
3132
) {
32-
private val logIncoming by setting("Log Incoming", true, "Log incoming packets")
33-
private val logOutgoing by setting("Log Outgoing", true, "Log outgoing packets")
33+
private val logToChat by setting("Log To Chat", false, "Log packets to chat")
34+
// ToDo: Implement HUD logging when HUD is done
35+
// private val logToHUD by setting("Log To HUD", false, "Log packets to HUD")
36+
private val networkSide by setting("Network Side", NetworkSide.ANY, "Side of the network to log packets from")
3437
private val logTicks by setting("Log Ticks", true, "Show game ticks in the log")
35-
private val logWhitelist by setting("Whitelist", false, "Only log packets from the whitelist")
36-
private val whitelist by setting("Whitelist Packets", emptyList<String>(), "Packets to whitelist")
37-
private val logBlacklist by setting("Blacklist", false, "Log all packets except those from the blacklist")
38-
private val blacklist by setting("Blacklist Packets", emptyList<String>(), "Packets to blacklist")
38+
private val scope by setting("Scope", Scope.ANY, "Scope of packets to log")
39+
private val whitelist by setting("Whitelist Packets", emptyList<String>(), "Packets to whitelist") { scope == Scope.WHITELIST }
40+
private val blacklist by setting("Blacklist Packets", emptyList<String>(), "Packets to blacklist") { scope == Scope.BLACKLIST }
3941
private val maxRecursionDepth by setting("Max Recursion Depth", 6, 1..10, 1, "Maximum recursion depth for packet serialization")
4042
private val logConcurrent by setting("Build Data Concurrent", false, "Whether to serialize packets concurrently. Will not save packets in chronological order but wont lag the game.")
4143

4244
private var file: File? = null
4345
private val entryFormatter: DateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss.SSSS")
4446
private val fileFormatter: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss.SSSS")
4547

48+
enum class NetworkSide {
49+
ANY, CLIENT, SERVER;
50+
51+
fun shouldLog(networkSide: NetworkSide) =
52+
this == ANY || this == networkSide
53+
}
54+
55+
enum class Scope {
56+
ANY, WHITELIST, BLACKLIST;
57+
58+
fun shouldLog(packet: Packet<*>) = when (this) {
59+
ANY -> true
60+
WHITELIST -> packet::class.simpleName in whitelist
61+
BLACKLIST -> packet::class.simpleName !in blacklist
62+
}
63+
}
64+
4665
private val storageFlow = MutableSharedFlow<String>(
4766
extraBufferCapacity = 1000,
4867
onBufferOverflow = BufferOverflow.DROP_OLDEST
@@ -53,6 +72,7 @@ object Packetlogger : Module(
5372
lambdaScope.launch(Dispatchers.IO) {
5473
storageFlow.collect { entry ->
5574
file?.appendText(entry)
75+
if (logToChat) this@Packetlogger.info(entry)
5676
}
5777
}
5878

@@ -117,29 +137,42 @@ object Packetlogger : Module(
117137
unsafeListener<TickEvent.Pre> {
118138
if (!logTicks) return@unsafeListener
119139

120-
storageFlow.tryEmit("\nStarted tick at ${getTime(entryFormatter)}\n")
140+
storageFlow.tryEmit("Started tick at ${getTime(entryFormatter)}\n\n")
121141
}
122142

123143
unsafeListener<PacketEvent.Receive.Pre> {
124-
if (logConcurrent || !logIncoming || it.packet.notLog()) return@unsafeListener
144+
if (logConcurrent
145+
|| !scope.shouldLog(it.packet)
146+
|| !networkSide.shouldLog(NetworkSide.SERVER)
147+
) return@unsafeListener
125148

126149
it.packet.logReceived()
127150
}
128151

129152
unsafeListener<PacketEvent.Send.Pre> {
130-
if (logConcurrent || !logOutgoing || it.packet.notLog()) return@unsafeListener
153+
if (logConcurrent
154+
|| !scope.shouldLog(it.packet)
155+
|| !networkSide.shouldLog(NetworkSide.CLIENT)
156+
) return@unsafeListener
157+
131158

132159
it.packet.logSent()
133160
}
134161

135162
unsafeConcurrentListener<PacketEvent.Receive.Pre> {
136-
if (!logConcurrent || !logIncoming || it.packet.notLog()) return@unsafeConcurrentListener
163+
if (!logConcurrent
164+
|| !scope.shouldLog(it.packet)
165+
|| !networkSide.shouldLog(NetworkSide.SERVER)
166+
) return@unsafeConcurrentListener
137167

138168
it.packet.logReceived()
139169
}
140170

141171
unsafeConcurrentListener<PacketEvent.Send.Pre> {
142-
if (!logConcurrent || !logOutgoing || it.packet.notLog()) return@unsafeConcurrentListener
172+
if (!logConcurrent
173+
|| !scope.shouldLog(it.packet)
174+
|| !networkSide.shouldLog(NetworkSide.CLIENT)
175+
) return@unsafeConcurrentListener
143176

144177
it.packet.logSent()
145178
}
@@ -152,8 +185,4 @@ object Packetlogger : Module(
152185
private fun Packet<*>.logSent() {
153186
storageFlow.tryEmit("Sent at ${getTime(entryFormatter)}\n${dynamicString(maxRecursionDepth)}\n")
154187
}
155-
156-
private fun Packet<*>.notLog() =
157-
(logWhitelist && this::class.simpleName !in whitelist)
158-
|| (logBlacklist && this::class.simpleName in blacklist)
159188
}

common/src/main/kotlin/com/lambda/module/tag/ModuleTag.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ class ModuleTag(override val name: String) : Nameable {
2727
val HIDDEN = ModuleTag("Hidden")
2828
val GRIM = ModuleTag("Grim")
2929
val BYPASS = ModuleTag("Bypass")
30+
val DEBUG = ModuleTag("Debug")
3031
}
3132
}

0 commit comments

Comments
 (0)