Implement a rudimentary CrashReportSysInfoDesktop (#6400)

* Implement a rudimentary CrashReportSysInfoDesktop

* Now gets Windows info the hard way
This commit is contained in:
SomeTroglodyte 2022-03-25 15:29:09 +01:00 committed by GitHub
parent 2f035e55f7
commit 7f3b075ac4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 102 additions and 1 deletions

View File

@ -0,0 +1,100 @@
package com.unciv.app.desktop
import com.badlogic.gdx.files.FileHandle
import com.unciv.ui.utils.CrashReportSysInfo
import java.nio.charset.Charset
class CrashReportSysInfoDesktop : CrashReportSysInfo {
override fun getInfo(): String {
val builder = StringBuilder()
// Operating system
val osName = System.getProperty("os.name") ?: "Unknown"
val isWindows = osName.startsWith("Windows", ignoreCase = true)
builder.append("OS: $osName")
if (!isWindows) {
val osInfo = listOfNotNull(System.getProperty("os.arch"), System.getProperty("os.version")).joinToString()
if (osInfo.isNotEmpty()) builder.append(" ($osInfo)")
}
builder.appendLine()
// Specific release info
val osRelease = if (isWindows) getWinVer() else getLinuxDistro()
if (osRelease.isNotEmpty())
builder.appendLine("\t$osRelease")
// Java runtime version
val javaVendor: String? = System.getProperty("java.vendor")
if (javaVendor != null) {
val javaVersion: String = System.getProperty("java.vendor.version") ?: System.getProperty("java.vm.version") ?: ""
builder.appendLine("Java: $javaVendor $javaVersion")
}
// Java VM memory limit as set by -Xmx
val maxMemory = try {
Runtime.getRuntime().maxMemory() / 1024 / 1024
} catch (ex: Throwable) { -1L }
if (maxMemory > 0) {
builder.append('\t')
builder.appendLine("Max Memory: $maxMemory MB")
}
return builder.toString()
}
companion object {
@Suppress("SpellCheckingInspection")
private val winVerCommand = """
cmd /c
reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v ProductName &&
reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v ReleaseId &&
reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v CurrentBuild &&
reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v DisplayVersion
""".trimIndent().replace('\n', ' ')
/** Kludge to get the important Windows version info (no easier way than the registry AFAIK)
* using a subprocess running reg query. Other methods would involve nasty reflection
* to break java.util.prefs.Preferences out of its Sandbox, or JNA requiring new bindings.
*/
fun getWinVer(): String {
val entries: Map<String,String> = try {
val process = Runtime.getRuntime().exec(winVerCommand)
process.waitFor()
val output = process.inputStream.readAllBytes().toString(Charset.defaultCharset())
val goodLines = output.split('\n').mapNotNull {
it.removeSuffix("\r").run {
if (startsWith(" ") || startsWith("\t")) trim() else null
}
}
goodLines.map { it.split("REG_SZ") }
.filter { it.size == 2 }
.associate { it[0].trim() to it[1].trim() }
} catch (ex: Throwable) { mapOf() }
if ("ProductName" !in entries) return ""
return entries["ProductName"]!! +
((entries["DisplayVersion"] ?: entries["ReleaseId"])?.run { " Version $this" } ?: "") +
(entries["CurrentBuild"]?.run { " (Build $this)" } ?: "")
}
/** Get linux Distribution out of the /etc/os-release file (ini-style)
* Should be safely silent on systems not supporting that file.
*/
fun getLinuxDistro(): String {
val osRelease: Map<String,String> = try {
FileHandle("/etc/os-release")
.readString()
.split('\n')
.map { it.split('=') }
.filter { it.size == 2 }
.associate { it[0] to it[1].removeSuffix("\"").removePrefix("\"") }
} catch (ex: Throwable) { mapOf() }
if ("NAME" !in osRelease) return ""
return osRelease["PRETTY_NAME"] ?: "${osRelease["NAME"]} ${osRelease["VERSION"]}"
}
}
}

View File

@ -49,7 +49,8 @@ internal object DesktopLauncher {
versionFromJar,
cancelDiscordEvent = { discordTimer?.cancel() },
fontImplementation = NativeFontDesktop(Fonts.ORIGINAL_FONT_SIZE.toInt(), settings.fontFamily),
customSaveLocationHelper = CustomSaveLocationHelperDesktop()
customSaveLocationHelper = CustomSaveLocationHelperDesktop(),
crashReportSysInfo = CrashReportSysInfoDesktop()
)
val game = UncivGame(desktopParameters)