@@ -28,8 +28,12 @@ import net.minecraft.util.text.TextFormatting
2828import net.minecraftforge.fml.common.gameevent.TickEvent
2929import java.io.File
3030import java.io.FileWriter
31+ import java.text.SimpleDateFormat
3132import java.time.LocalTime
32- import java.time.format.DateTimeFormatter
33+ import java.util.*
34+ import kotlin.collections.ArrayList
35+ import kotlin.time.DurationUnit
36+ import kotlin.time.toDuration
3337
3438object PacketLogger : Module(
3539 name = " PacketLogger" ,
@@ -39,7 +43,7 @@ object PacketLogger : Module(
3943 private val page by setting(" Page" , Page .GENERAL )
4044 private val categorySetting by setting(" Category" , CategorySlider .PLAYER , { page == Page .CLIENT || page == Page .SERVER })
4145 private val packetSide by setting(" Packet Side" , PacketSide .BOTH , description = " Log packets from the server, from the client, or both." , visibility = { page == Page .GENERAL })
42- private val absoluteTime by setting(" Absolute Time " , true , description = " Show absolute time." , visibility = { page == Page .GENERAL })
46+ private val timestamp by setting(" Timestamp " , true , description = " Show absolute time." , visibility = { page == Page .GENERAL })
4347 private val startDelta by setting(" Start Time Delta" , false , visibility = { page == Page .GENERAL })
4448 private val lastDelta by setting(" Last Time Delta" , false , visibility = { page == Page .GENERAL })
4549 private val showClientTicks by setting(" Show Client Ticks" , false , description = " Show timestamps of client ticks." , visibility = { page == Page .GENERAL })
@@ -344,7 +348,7 @@ object PacketLogger : Module(
344348 private var sPacketSpawnPainting by setting(" SPacketSpawnPainting" , true , visibility = { page == Page .SERVER && categorySetting == CategorySlider .ENTITY })
345349 private var sPacketSpawnPlayer by setting(" SPacketSpawnPlayer" , true , visibility = { page == Page .SERVER && categorySetting == CategorySlider .ENTITY })
346350
347- private val fileTimeFormatter = DateTimeFormatter .ofPattern( " HH-mm-ss_SSS " )
351+ private val dateFormat = SimpleDateFormat ( " yyyyMMdd_HHmmss " )
348352
349353 private var start = 0L
350354 private var last = 0L
@@ -382,10 +386,16 @@ object PacketLogger : Module(
382386 onEnable {
383387 PacketLogViewer .clear()
384388 start = System .currentTimeMillis()
385- filename = " ${fileTimeFormatter .format(LocalTime .now ())} .csv"
389+ filename = " ${dateFormat .format(Date ())} .csv"
386390
387391 synchronized(this ) {
388- lines.add(" From,Packet Name,Time Since Start (ms),Time Since Last (ms),Data\n " )
392+ lines.add(buildString {
393+ append(" Source,Packet" )
394+ if (timestamp) append(" ,Timestamp" )
395+ if (startDelta) append(" ,Time Since Start" )
396+ if (lastDelta) append(" ,Time Since Last" )
397+ append(" ,Data\n " )
398+ })
389399 }
390400 }
391401
@@ -399,7 +409,21 @@ object PacketLogger : Module(
399409 if (showClientTicks) {
400410 synchronized(this @PacketLogger) {
401411 val current = System .currentTimeMillis()
402- val line = " Tick Pulse,,${current - start} ,${current - lastTick} \n "
412+
413+ val line = buildString {
414+ append(" Tick Pulse," )
415+ if (timestamp) {
416+ append(" ,${LocalTime .now()} " )
417+ }
418+ if (startDelta) {
419+ append(" ,${(System .currentTimeMillis() - start).toDuration(DurationUnit .MILLISECONDS )} " )
420+ }
421+ if (lastDelta) {
422+ append(" ,${(System .currentTimeMillis() - last).toDuration(DurationUnit .MILLISECONDS )} " )
423+ }
424+ append(" ,Client Tick\n " )
425+ }
426+
403427 if (logMode == LogMode .CHAT_AND_FILE || logMode == LogMode .FILE || logMode == LogMode .ALL ) {
404428 lines.add(line)
405429 }
@@ -416,9 +440,7 @@ object PacketLogger : Module(
416440 }
417441
418442 /* Don't let lines get too big, write periodically to the file */
419- if (lines.size >= 500 ) {
420- write()
421- }
443+ if (lines.size >= 500 ) write()
422444 }
423445
424446 listener<ConnectionEvent .Disconnect > {
@@ -454,6 +476,124 @@ object PacketLogger : Module(
454476 }
455477 }
456478
479+ private fun write () {
480+ if (logMode != LogMode .FILE && logMode != LogMode .CHAT_AND_FILE && logMode != LogMode .ALL ) {
481+ lines.clear()
482+ return
483+ }
484+
485+ val lines = synchronized(this ) {
486+ val cache = lines
487+ lines = ArrayList ()
488+ cache
489+ }
490+
491+ defaultScope.launch(Dispatchers .IO ) {
492+ try {
493+ with (File (FolderUtils .packetLogFolder)) {
494+ if (! exists()) mkdir()
495+ }
496+
497+ FileWriter (" ${FolderUtils .packetLogFolder}${filename} " , true ).buffered().use {
498+ for (line in lines) it.write(line)
499+ }
500+ runSafe {
501+ MessageSendHelper .sendChatMessage(" $chatName Log saved at ${TextFormatting .GREEN }${FolderUtils .packetLogFolder}${filename} " )
502+ }
503+ } catch (e: Exception ) {
504+ LambdaMod .LOG .warn(" $chatName Failed saving packet log!" , e)
505+ }
506+ }
507+ }
508+
509+ private inline fun logClient (packet : Packet <* >, block : PacketLogBuilder .() -> Unit ) {
510+ PacketLogBuilder (PacketSide .CLIENT , packet).apply (block).build()
511+ }
512+
513+ private inline fun logServer (packet : Packet <* >, block : PacketLogBuilder .() -> Unit ) {
514+ PacketLogBuilder (PacketSide .SERVER , packet).apply (block).build()
515+ }
516+
517+ private class PacketLogBuilder (val side : PacketSide , val packet : Packet <* >) {
518+ private val stringBuilder = StringBuilder ()
519+
520+ init {
521+ stringBuilder.apply {
522+ append(side.displayName)
523+ append(' ,' )
524+
525+ append(packet.javaClass.simpleName)
526+ if (timestamp) {
527+ append(" ,${LocalTime .now()} " )
528+ }
529+ if (startDelta) {
530+ append(" ,${(System .currentTimeMillis() - start).toDuration(DurationUnit .MILLISECONDS )} " )
531+ }
532+ if (lastDelta) {
533+ append(" ,${(System .currentTimeMillis() - last).toDuration(DurationUnit .MILLISECONDS )} " )
534+ }
535+ append(' ,' )
536+ }
537+ }
538+
539+ operator fun String.unaryPlus () {
540+ stringBuilder.append(this )
541+ }
542+
543+ infix fun String.to (value : Any? ) {
544+ if (value != null ) {
545+ add(this , value.toString())
546+ }
547+ }
548+
549+ infix fun String.to (value : String? ) {
550+ if (value != null ) {
551+ add(this , value)
552+ }
553+ }
554+
555+ infix fun String.to (value : BlockPos ? ) {
556+ if (value != null ) {
557+ add(" x" , value.x.toString())
558+ add(" y" , value.y.toString())
559+ add(" z" , value.z.toString())
560+ }
561+ }
562+
563+ fun add (key : String , value : String ) {
564+ stringBuilder.apply {
565+ append(key)
566+ append(" : " )
567+ append(value)
568+ append(' ' )
569+ }
570+ }
571+
572+ fun build () {
573+ val string = stringBuilder.run {
574+ append(' \n ' )
575+ toString()
576+ }
577+
578+ if (logMode == LogMode .CHAT_AND_FILE || logMode == LogMode .FILE || logMode == LogMode .ALL ) {
579+ synchronized(PacketLogger ) {
580+ lines.add(string)
581+ last = System .currentTimeMillis()
582+ }
583+ }
584+
585+ if (logMode == LogMode .CHAT_AND_FILE || logMode == LogMode .CHAT || logMode == LogMode .ALL ) {
586+ MessageSendHelper .sendChatMessage(string)
587+ }
588+
589+ if (logMode == LogMode .ONLY_HUD || logMode == LogMode .ALL ) {
590+ if (PacketLogViewer .visible) {
591+ PacketLogViewer .addPacketLog(string.replace(" \n " , " " ))
592+ }
593+ }
594+ }
595+ }
596+
457597 private fun sendPacket (packet : Packet <* >) {
458598 if (packetSide == PacketSide .CLIENT || packetSide == PacketSide .BOTH ) {
459599 when (packet) {
@@ -1626,126 +1766,4 @@ object PacketLogger : Module(
16261766 }
16271767 }
16281768 }
1629-
1630-
1631- private fun write () {
1632- if (logMode != LogMode .FILE && logMode != LogMode .CHAT_AND_FILE && logMode != LogMode .ALL ) {
1633- lines.clear()
1634- return
1635- }
1636-
1637- val lines = synchronized(this ) {
1638- val cache = lines
1639- lines = ArrayList ()
1640- cache
1641- }
1642-
1643- defaultScope.launch(Dispatchers .IO ) {
1644- try {
1645- with (File (FolderUtils .packetLogFolder)) {
1646- if (! exists()) mkdir()
1647- }
1648-
1649- FileWriter (" ${FolderUtils .packetLogFolder}${filename} " , true ).buffered().use {
1650- for (line in lines) it.write(line)
1651- }
1652- runSafe {
1653- MessageSendHelper .sendChatMessage(" $chatName Log saved at ${TextFormatting .GREEN }${FolderUtils .packetLogFolder}${filename} " )
1654- }
1655- } catch (e: Exception ) {
1656- LambdaMod .LOG .warn(" $chatName Failed saving packet log!" , e)
1657- }
1658- }
1659- }
1660-
1661- private inline fun logClient (packet : Packet <* >, block : PacketLogBuilder .() -> Unit ) {
1662- PacketLogBuilder (PacketSide .CLIENT , packet).apply (block).build()
1663- }
1664-
1665- private inline fun logServer (packet : Packet <* >, block : PacketLogBuilder .() -> Unit ) {
1666- PacketLogBuilder (PacketSide .SERVER , packet).apply (block).build()
1667- }
1668-
1669- private class PacketLogBuilder (val side : PacketSide , val packet : Packet <* >) {
1670- private val stringBuilder = StringBuilder ()
1671-
1672- init {
1673- stringBuilder.apply {
1674- append(side.displayName)
1675- append(' ,' )
1676-
1677- append(packet.javaClass.simpleName)
1678- if (absoluteTime) {
1679- append(' ,' )
1680- append(System .currentTimeMillis())
1681- }
1682- if (startDelta) {
1683- append(' ,' )
1684- append(System .currentTimeMillis() - start)
1685- }
1686- if (lastDelta) {
1687- append(' ,' )
1688- append(System .currentTimeMillis() - last)
1689- }
1690- append(" : " )
1691- }
1692- }
1693-
1694- operator fun String.unaryPlus () {
1695- stringBuilder.append(this )
1696- }
1697-
1698- infix fun String.to (value : Any? ) {
1699- if (value != null ) {
1700- add(this , value.toString())
1701- }
1702- }
1703-
1704- infix fun String.to (value : String? ) {
1705- if (value != null ) {
1706- add(this , value)
1707- }
1708- }
1709-
1710- infix fun String.to (value : BlockPos ? ) {
1711- if (value != null ) {
1712- add(" x" , value.x.toString())
1713- add(" y" , value.y.toString())
1714- add(" z" , value.z.toString())
1715- }
1716- }
1717-
1718- fun add (key : String , value : String ) {
1719- stringBuilder.apply {
1720- append(key)
1721- append(" : " )
1722- append(value)
1723- append(' ' )
1724- }
1725- }
1726-
1727- fun build () {
1728- val string = stringBuilder.run {
1729- append(' \n ' )
1730- toString()
1731- }
1732-
1733- if (logMode == LogMode .CHAT_AND_FILE || logMode == LogMode .FILE || logMode == LogMode .ALL ) {
1734- synchronized(PacketLogger ) {
1735- lines.add(string)
1736- last = System .currentTimeMillis()
1737- }
1738- }
1739-
1740- if (logMode == LogMode .CHAT_AND_FILE || logMode == LogMode .CHAT || logMode == LogMode .ALL ) {
1741- MessageSendHelper .sendChatMessage(string)
1742- }
1743-
1744- if (logMode == LogMode .ONLY_HUD || logMode == LogMode .ALL ) {
1745- if (PacketLogViewer .visible) {
1746- PacketLogViewer .addPacketLog(string.replace(" \n " , " " ))
1747- }
1748- }
1749- }
1750- }
17511769}
0 commit comments