aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/utils/ComputerEnvDebug.kt
blob: ca4eeeb71118ae6c5d3f65bfc74fde3733dbec27 (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
package at.hannibal2.skyhanni.utils

import at.hannibal2.skyhanni.events.DebugDataCollectEvent
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.test.command.ErrorManager
import at.hannibal2.skyhanni.utils.RegexUtils.matchMatcher
import at.hannibal2.skyhanni.utils.system.PlatformUtils
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent

@SkyHanniModule
object ComputerEnvDebug {

    @SubscribeEvent
    fun onDebugDataCollect(event: DebugDataCollectEvent) {
        os(event)
        java(event)
        launcher(event)
    }

    private fun launcher(event: DebugDataCollectEvent) {
        event.title("Computer Minecraft Launcher")

        val firstStack = getFirstStack() ?: run {
            event.addData("Could not load data!")
            return
        }

        val (launcher, relevant) = findLauncher(firstStack)

        launcher?.let {
            if (relevant) {
                event.addData(it)
            } else {
                event.addIrrelevant(it)
            }
            return
        }

        event.addData {
            add("Unknown launcher!")
            add("firstStack: '$firstStack'")
        }
    }

    // TODO put into repo
    private fun findLauncher(firstStack: String): Pair<String?, Boolean> {
        if (firstStack.contains("net.fabricmc.devlaunchinjector.Main.main")) {
            return Pair("Dev Env", false)
        }
        if (firstStack.contains("net.minecraft.launchwrapper.Launch.main")) {
            return Pair("Vanilla Launcher", false)
        }
        if (firstStack.contains("org.prismlauncher.EntryPoint.main")) {
            return Pair("Prism", false)
        }
        if (firstStack.contains("net.digitalingot.vendor.")) {
            return Pair("Feather Client", true)
        }
        return Pair(null, true)
    }

    private fun getFirstStack(): String? {
        val firstStack = try {
            Thread.currentThread().stackTrace.last().toString()
        } catch (e: Exception) {
            ErrorManager.logErrorWithData(e, "Failed loading current thread stack trace info")
            null
        }
        return firstStack
    }

    private fun java(event: DebugDataCollectEvent) {
        event.title("Computer Java Version")
        val version = System.getProperty("java.version")
        val pattern = "1\\.8\\.0_(?<update>.*)".toPattern()
        pattern.matchMatcher(version) {
            group("update").toIntOrNull()?.let {
                val devEnvironment = PlatformUtils.isDevEnvironment
                if (it < 300 && !devEnvironment) {
                    event.addData("Old update: $it")
                } else {
                    if (devEnvironment) {
                        event.addIrrelevant("Update version: $it (dev env)")
                    } else {
                        event.addIrrelevant("New update: $it")
                    }
                }
                return
            }
        }
        event.addData("Unknwon java version: '$version'")
    }

    private fun os(event: DebugDataCollectEvent) {
        event.title("Computer Operating System")
        val osType = OSUtils.getOperatingSystem()
        val exactName = OSUtils.getOperatingSystemRaw()
        if (osType != OSUtils.OperatingSystem.UNKNOWN) {
            event.addIrrelevant {
                add("OS type: $osType")
                add("Exact name: $exactName")
            }
        } else {
            event.addData("Unknwon OS: '$exactName'")
        }
    }
}