aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/io/github/moulberry/notenoughupdates/commands/dev/NEUStatsCommand.kt
blob: fee66cbe2f4c5c8753d592b8a7f35811f43ef1c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
 * Copyright (C) 2023-2024 NotEnoughUpdates contributors
 *
 * This file is part of NotEnoughUpdates.
 *
 * NotEnoughUpdates is free software: you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *
 * NotEnoughUpdates is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
 */

package io.github.moulberry.notenoughupdates.commands.dev

import com.mojang.brigadier.context.CommandContext
import com.sun.management.OperatingSystemMXBean
import com.sun.management.UnixOperatingSystemMXBean
import io.github.moulberry.notenoughupdates.NotEnoughUpdates
import io.github.moulberry.notenoughupdates.autosubscribe.NEUAutoSubscribe
import io.github.moulberry.notenoughupdates.events.RegisterBrigadierCommandEvent
import io.github.moulberry.notenoughupdates.util.*
import io.github.moulberry.notenoughupdates.util.brigadier.reply
import io.github.moulberry.notenoughupdates.util.brigadier.thenExecute
import io.github.moulberry.notenoughupdates.util.brigadier.thenLiteralExecute
import io.github.moulberry.notenoughupdates.util.brigadier.withHelp
import net.minecraft.client.Minecraft
import net.minecraft.client.renderer.OpenGlHelper
import net.minecraft.command.ICommandSender
import net.minecraft.util.EnumChatFormatting.DARK_RED
import net.minecraft.util.EnumChatFormatting.GREEN
import net.minecraftforge.common.ForgeVersion
import net.minecraftforge.fml.client.FMLClientHandler
import net.minecraftforge.fml.common.Loader
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import org.lwjgl.opengl.Display
import org.lwjgl.opengl.GL11
import java.awt.datatransfer.StringSelection
import java.lang.management.ManagementFactory
import javax.management.JMX
import javax.management.ObjectName

@NEUAutoSubscribe
class NEUStatsCommand {
    @SubscribeEvent
    fun onCommands(event: RegisterBrigadierCommandEvent) {
        event.command( "neustats") {
            thenLiteralExecute("modlist") {
                clipboardAndSendMessage(
                    DiscordMarkdownBuilder()
                        .also(::appendModList)
                        .toString()
                )
            }.withHelp("Copy the mod list to your clipboard")
            thenLiteralExecute("tablist") {
                clipboardAndSendMessage("```\n${TabListUtils.getTabList()}\n```")
            }.withHelp("Copy the current tab list to your clipboard")
            thenLiteralExecute("repo") {
                clipboardAndSendMessage(
                    DiscordMarkdownBuilder()
                        .also(::appendRepoStats)
                        .also(::appendAdvancedRepoStats)
                        .toString()
                )
            }.withHelp("Copy the repo stats to your clipboard")
            thenLiteralExecute("full") {
                clipboardAndSendMessage(
                    DiscordMarkdownBuilder()
                        .also(::appendStats)
                        .also(::appendAdvancedRepoStats)
                        .also(::appendModList)
                        .toString()
                )
            }.withHelp("Copy the full list of all NEU stats and your mod list to your clipboard")
            thenLiteralExecute("dump") {
                reply("${GREEN}This will copy a summary of the loaded Java classes to your clipboard. Please paste this into your discord support ticket by pressing CTRL+V.")
                generateDataUsage().copyToClipboard()
            }.withHelp("Dump all loaded classes and their memory usage and copy that to your clipboard.")
            thenExecute {
                clipboardAndSendMessage(
                    DiscordMarkdownBuilder()
                        .also(::appendStats)
                        .also {
                            if (Loader.instance().activeModList.size <= 15) appendModList(it)
                        }
                        .toString()
                )
            }
        }.withHelp("Copy a list of NEU relevant stats to your clipboard for debugging purposes")
    }

    interface DiagnosticCommandMXBean {
        fun gcClassHistogram(array: Array<String>): String
    }

    private fun generateDataUsage(): String {
        val server = ManagementFactory.getPlatformMBeanServer()
        val objectName = ObjectName.getInstance("com.sun.management:type=DiagnosticCommand")
        val proxy = JMX.newMXBeanProxy(
            server,
            objectName,
            DiagnosticCommandMXBean::class.java
        )
        return proxy.gcClassHistogram(emptyArray()).replace("[", "[]")
    }


    private fun getMemorySize(): Long {
        try {
            return (ManagementFactory.getOperatingSystemMXBean() as OperatingSystemMXBean).totalPhysicalMemorySize
        } catch (e: java.lang.Exception) {
            try {
                return (ManagementFactory.getOperatingSystemMXBean() as UnixOperatingSystemMXBean).totalPhysicalMemorySize
            } catch (ignored: java.lang.Exception) { /*IGNORE*/
            }
        }
        return -1
    }

    val ONE_MB = 1024L * 1024L
    private fun appendStats(builder: DiscordMarkdownBuilder) {
        val maxMemory = Runtime.getRuntime().maxMemory()
        val totalMemory = Runtime.getRuntime().totalMemory()
        val freeMemory = Runtime.getRuntime().freeMemory()
        val currentMemory = totalMemory - freeMemory
        builder.category("System Stats")
        builder.append("OS", System.getProperty("os.name"))
        builder.append("CPU", OpenGlHelper.getCpu())
        builder.append(
            "Display",
            String.format("%dx%d (%s)", Display.getWidth(), Display.getHeight(), GL11.glGetString(GL11.GL_VENDOR))
        )
        builder.append("GPU", GL11.glGetString(GL11.GL_RENDERER))
        builder.append("GPU Driver", GL11.glGetString(GL11.GL_VERSION))
        if (getMemorySize() > 0)
            builder.append(
                "Maximum Memory",
                "${getMemorySize() / ONE_MB}MB"
            )
        builder.append("Shaders", ("" + OpenGlHelper.isFramebufferEnabled()).uppercase())
        builder.category("Java Stats")
        builder.append(
            "Java",
            "${System.getProperty("java.version")} ${if (Minecraft.getMinecraft().isJava64bit) 64 else 32}bit",
        )
        builder.append(
            "Memory", String.format(
                "% 2d%% %03d/%03dMB",
                currentMemory * 100L / maxMemory,
                currentMemory / ONE_MB,
                maxMemory / ONE_MB
            )
        )
        builder.append(
            "Memory Allocated",
            String.format("% 2d%% %03dMB", totalMemory * 100L / maxMemory, totalMemory / ONE_MB)
        )
        builder.category("Game Stats")
        builder.append("FPS", Minecraft.getDebugFPS().toString())
        builder.append("Loaded Mods", Loader.instance().activeModList.size)
        builder.append("Forge", ForgeVersion.getVersion())
        builder.append("Optifine", if (FMLClientHandler.instance().hasOptifine()) "TRUE" else "FALSE")
        builder.category("Neu Settings")
        builder.append("On SkyBlock", if (NotEnoughUpdates.INSTANCE.hasSkyblockScoreboard()) "TRUE" else "FALSE")
        builder.append(
            "Mod Version",
            Loader.instance().indexedModList[NotEnoughUpdates.MODID]!!.displayVersion
        )
        builder.append(
            "Version Id",
            NotEnoughUpdates.VERSION_ID
        )
        builder.append("SB Profile", SBInfo.getInstance().currentProfile)
            .also(::appendRepoStats)
    }

    private fun appendModList(builder: DiscordMarkdownBuilder): DiscordMarkdownBuilder {
        builder.category("Mods Loaded")
        Loader.instance().activeModList.forEach {
            builder.append(it.name, "${it.source.name} (${it.displayVersion})")
        }
        return builder
    }

    private fun appendRepoStats(builder: DiscordMarkdownBuilder): DiscordMarkdownBuilder {
        val apiData = NotEnoughUpdates.INSTANCE.config.apiData
        if (apiData.repoUser.isEmpty() || apiData.repoName.isEmpty() || apiData.repoBranch.isEmpty()) {
            apiData.repoUser = "NotEnoughUpdates"
            apiData.repoName = "NotEnoughUpdates-REPO"
            apiData.repoBranch = "master"
            builder.category("Reset Repository location")
        } else {
            builder.category("Repo Stats")
            builder.append("Last Commit", NotEnoughUpdates.INSTANCE.manager.latestRepoCommit)
            builder.append("Repo Location", "https://github.com/${apiData.repoUser}/${apiData.repoName}/tree/${apiData.repoBranch}")
        }
        builder.append("Using Backup", NotEnoughUpdates.INSTANCE.manager.onBackupRepo)
        builder.append("Loaded Items", NotEnoughUpdates.INSTANCE.manager.itemInformation.size.toString())
        if (apiData.moulberryCodesApi.isEmpty()) {
            apiData.moulberryCodesApi = "moulberry.codes"
            builder.category("Reset API location")
        } else {
            builder.append("Lowest Bin API Location", apiData.moulberryCodesApi)
        }
        return builder
    }

    private fun appendAdvancedRepoStats(builder: DiscordMarkdownBuilder): DiscordMarkdownBuilder {
        if (NotEnoughUpdates.INSTANCE.manager.repoLocation.isDirectory) {
            val files = NotEnoughUpdates.INSTANCE.manager.repoLocation.listFiles()
            builder.category("Repo Files")
            files?.forEach { file ->
                if (file.isDirectory) {
                    builder.append(file.name, file.listFiles()?.size)
                } else if (file.isFile) {
                    builder.append("", file.name)
                }
            }
        } else {
            builder.category("Repo folder not found!")
        }
        return builder
    }

    fun CommandContext<ICommandSender>.clipboardAndSendMessage(data: String?) {
        if (data == null) {
            reply("${DARK_RED}Error occurred trying to perform command.")
            return
        }
        try {
            val clipboard = StringSelection(data)
            Utils.copyToClipboard(clipboard, null)
            reply("