Skip to content

Commit

Permalink
Fix column inconsistencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Avanatiker committed Aug 8, 2023
1 parent 4b33733 commit 52cffa8
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ internal object PacketLogViewer: LabelHud(
displayText.addLine("PacketLog Viewer", secondaryColor)
synchronized(logs) {
logs.forEach { log ->
displayText.addLine(log, primaryColor)
log.split(",").forEachIndexed { i, s ->
displayText.add(s, if (i % 2 == 0) secondaryColor else primaryColor)
}
displayText.addLine("")
}
}
}
Expand Down
280 changes: 149 additions & 131 deletions src/main/kotlin/com/lambda/client/module/modules/player/PacketLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ import net.minecraft.util.text.TextFormatting
import net.minecraftforge.fml.common.gameevent.TickEvent
import java.io.File
import java.io.FileWriter
import java.text.SimpleDateFormat
import java.time.LocalTime
import java.time.format.DateTimeFormatter
import java.util.*
import kotlin.collections.ArrayList
import kotlin.time.DurationUnit
import kotlin.time.toDuration

object PacketLogger : Module(
name = "PacketLogger",
Expand All @@ -39,7 +43,7 @@ object PacketLogger : Module(
private val page by setting("Page", Page.GENERAL)
private val categorySetting by setting("Category", CategorySlider.PLAYER, { page == Page.CLIENT || page == Page.SERVER })
private val packetSide by setting("Packet Side", PacketSide.BOTH, description = "Log packets from the server, from the client, or both.", visibility = { page == Page.GENERAL })
private val absoluteTime by setting("Absolute Time", true, description = "Show absolute time.", visibility = { page == Page.GENERAL })
private val timestamp by setting("Timestamp", true, description = "Show absolute time.", visibility = { page == Page.GENERAL })
private val startDelta by setting("Start Time Delta", false, visibility = { page == Page.GENERAL })
private val lastDelta by setting("Last Time Delta", false, visibility = { page == Page.GENERAL })
private val showClientTicks by setting("Show Client Ticks", false, description = "Show timestamps of client ticks.", visibility = { page == Page.GENERAL })
Expand Down Expand Up @@ -344,7 +348,7 @@ object PacketLogger : Module(
private var sPacketSpawnPainting by setting("SPacketSpawnPainting", true, visibility = { page == Page.SERVER && categorySetting == CategorySlider.ENTITY })
private var sPacketSpawnPlayer by setting("SPacketSpawnPlayer", true, visibility = { page == Page.SERVER && categorySetting == CategorySlider.ENTITY })

private val fileTimeFormatter = DateTimeFormatter.ofPattern("HH-mm-ss_SSS")
private val dateFormat = SimpleDateFormat("yyyyMMdd_HHmmss")

private var start = 0L
private var last = 0L
Expand Down Expand Up @@ -382,10 +386,16 @@ object PacketLogger : Module(
onEnable {
PacketLogViewer.clear()
start = System.currentTimeMillis()
filename = "${fileTimeFormatter.format(LocalTime.now())}.csv"
filename = "${dateFormat.format(Date())}.csv"

synchronized(this) {
lines.add("From,Packet Name,Time Since Start (ms),Time Since Last (ms),Data\n")
lines.add(buildString {
append("Source,Packet")
if (timestamp) append(",Timestamp")
if (startDelta) append(",Time Since Start")
if (lastDelta) append(",Time Since Last")
append(",Data\n")
})
}
}

Expand All @@ -399,7 +409,21 @@ object PacketLogger : Module(
if (showClientTicks) {
synchronized(this@PacketLogger) {
val current = System.currentTimeMillis()
val line = "Tick Pulse,,${current - start},${current - lastTick}\n"

val line = buildString {
append("Tick Pulse,")
if (timestamp) {
append(",${LocalTime.now()}")
}
if (startDelta) {
append(",${(System.currentTimeMillis() - start).toDuration(DurationUnit.MILLISECONDS)}")
}
if (lastDelta) {
append(",${(System.currentTimeMillis() - last).toDuration(DurationUnit.MILLISECONDS)}")
}
append(",Client Tick\n")
}

if (logMode == LogMode.CHAT_AND_FILE || logMode == LogMode.FILE || logMode == LogMode.ALL) {
lines.add(line)
}
Expand All @@ -416,9 +440,7 @@ object PacketLogger : Module(
}

/* Don't let lines get too big, write periodically to the file */
if (lines.size >= 500) {
write()
}
if (lines.size >= 500) write()
}

listener<ConnectionEvent.Disconnect> {
Expand Down Expand Up @@ -454,6 +476,124 @@ object PacketLogger : Module(
}
}

private fun write() {
if (logMode != LogMode.FILE && logMode != LogMode.CHAT_AND_FILE && logMode != LogMode.ALL) {
lines.clear()
return
}

val lines = synchronized(this) {
val cache = lines
lines = ArrayList()
cache
}

defaultScope.launch(Dispatchers.IO) {
try {
with(File(FolderUtils.packetLogFolder)) {
if (!exists()) mkdir()
}

FileWriter("${FolderUtils.packetLogFolder}${filename}", true).buffered().use {
for (line in lines) it.write(line)
}
runSafe {
MessageSendHelper.sendChatMessage("$chatName Log saved at ${TextFormatting.GREEN}${FolderUtils.packetLogFolder}${filename}")
}
} catch (e: Exception) {
LambdaMod.LOG.warn("$chatName Failed saving packet log!", e)
}
}
}

private inline fun logClient(packet: Packet<*>, block: PacketLogBuilder.() -> Unit) {
PacketLogBuilder(PacketSide.CLIENT, packet).apply(block).build()
}

private inline fun logServer(packet: Packet<*>, block: PacketLogBuilder.() -> Unit) {
PacketLogBuilder(PacketSide.SERVER, packet).apply(block).build()
}

private class PacketLogBuilder(val side: PacketSide, val packet: Packet<*>) {
private val stringBuilder = StringBuilder()

init {
stringBuilder.apply {
append(side.displayName)
append(',')

append(packet.javaClass.simpleName)
if (timestamp) {
append(",${LocalTime.now()}")
}
if (startDelta) {
append(",${(System.currentTimeMillis() - start).toDuration(DurationUnit.MILLISECONDS)}")
}
if (lastDelta) {
append(",${(System.currentTimeMillis() - last).toDuration(DurationUnit.MILLISECONDS)}")
}
append(',')
}
}

operator fun String.unaryPlus() {
stringBuilder.append(this)
}

infix fun String.to(value: Any?) {
if (value != null) {
add(this, value.toString())
}
}

infix fun String.to(value: String?) {
if (value != null) {
add(this, value)
}
}

infix fun String.to(value: BlockPos?) {
if (value != null) {
add("x", value.x.toString())
add("y", value.y.toString())
add("z", value.z.toString())
}
}

fun add(key: String, value: String) {
stringBuilder.apply {
append(key)
append(": ")
append(value)
append(' ')
}
}

fun build() {
val string = stringBuilder.run {
append('\n')
toString()
}

if (logMode == LogMode.CHAT_AND_FILE || logMode == LogMode.FILE || logMode == LogMode.ALL) {
synchronized(PacketLogger) {
lines.add(string)
last = System.currentTimeMillis()
}
}

if (logMode == LogMode.CHAT_AND_FILE || logMode == LogMode.CHAT || logMode == LogMode.ALL) {
MessageSendHelper.sendChatMessage(string)
}

if (logMode == LogMode.ONLY_HUD || logMode == LogMode.ALL) {
if (PacketLogViewer.visible) {
PacketLogViewer.addPacketLog(string.replace("\n", ""))
}
}
}
}

private fun sendPacket(packet: Packet<*>) {
if (packetSide == PacketSide.CLIENT || packetSide == PacketSide.BOTH) {
when (packet) {
Expand Down Expand Up @@ -1626,126 +1766,4 @@ object PacketLogger : Module(
}
}
}


private fun write() {
if (logMode != LogMode.FILE && logMode != LogMode.CHAT_AND_FILE && logMode != LogMode.ALL) {
lines.clear()
return
}

val lines = synchronized(this) {
val cache = lines
lines = ArrayList()
cache
}

defaultScope.launch(Dispatchers.IO) {
try {
with(File(FolderUtils.packetLogFolder)) {
if (!exists()) mkdir()
}

FileWriter("${FolderUtils.packetLogFolder}${filename}", true).buffered().use {
for (line in lines) it.write(line)
}
runSafe {
MessageSendHelper.sendChatMessage("$chatName Log saved at ${TextFormatting.GREEN}${FolderUtils.packetLogFolder}${filename}")
}
} catch (e: Exception) {
LambdaMod.LOG.warn("$chatName Failed saving packet log!", e)
}
}
}

private inline fun logClient(packet: Packet<*>, block: PacketLogBuilder.() -> Unit) {
PacketLogBuilder(PacketSide.CLIENT, packet).apply(block).build()
}

private inline fun logServer(packet: Packet<*>, block: PacketLogBuilder.() -> Unit) {
PacketLogBuilder(PacketSide.SERVER, packet).apply(block).build()
}

private class PacketLogBuilder(val side: PacketSide, val packet: Packet<*>) {
private val stringBuilder = StringBuilder()

init {
stringBuilder.apply {
append(side.displayName)
append(',')

append(packet.javaClass.simpleName)
if (absoluteTime) {
append(',')
append(System.currentTimeMillis())
}
if (startDelta) {
append(',')
append(System.currentTimeMillis() - start)
}
if (lastDelta) {
append(',')
append(System.currentTimeMillis() - last)
}
append(": ")
}
}

operator fun String.unaryPlus() {
stringBuilder.append(this)
}

infix fun String.to(value: Any?) {
if (value != null) {
add(this, value.toString())
}
}

infix fun String.to(value: String?) {
if (value != null) {
add(this, value)
}
}

infix fun String.to(value: BlockPos?) {
if (value != null) {
add("x", value.x.toString())
add("y", value.y.toString())
add("z", value.z.toString())
}
}

fun add(key: String, value: String) {
stringBuilder.apply {
append(key)
append(": ")
append(value)
append(' ')
}
}

fun build() {
val string = stringBuilder.run {
append('\n')
toString()
}

if (logMode == LogMode.CHAT_AND_FILE || logMode == LogMode.FILE || logMode == LogMode.ALL) {
synchronized(PacketLogger) {
lines.add(string)
last = System.currentTimeMillis()
}
}

if (logMode == LogMode.CHAT_AND_FILE || logMode == LogMode.CHAT || logMode == LogMode.ALL) {
MessageSendHelper.sendChatMessage(string)
}

if (logMode == LogMode.ONLY_HUD || logMode == LogMode.ALL) {
if (PacketLogViewer.visible) {
PacketLogViewer.addPacketLog(string.replace("\n", ""))
}
}
}
}
}

0 comments on commit 52cffa8

Please sign in to comment.