Files
SlimeVR-Server/server/desktop/src/main/java/dev/slimevr/platform.kt
T

155 lines
3.8 KiB
Kotlin

package dev.slimevr
import com.sun.jna.platform.win32.Kernel32
import com.sun.jna.platform.win32.Kernel32Util
import com.sun.jna.platform.win32.Shell32
import com.sun.jna.platform.win32.WinUser
import java.io.File
import java.nio.file.Path
import java.util.*
import kotlin.io.path.Path
import kotlin.io.path.exists
const val SLIMEVR_IDENTIFIER = "dev.slimevr.SlimeVR"
enum class Platform {
LINUX,
WINDOWS,
OSX,
UNKNOWN,
}
val CURRENT_PLATFORM: Platform = detectPlatform()
private fun detectPlatform(): Platform {
val os = System.getProperty("os.name").lowercase(Locale.getDefault())
if (os.contains("win")) return Platform.WINDOWS
if (os.contains("mac") || os.contains("darwin")) return Platform.OSX
if (os.contains("linux") || os.contains("unix")) return Platform.LINUX
return Platform.UNKNOWN
}
fun getJavaExecutable(forceConsole: Boolean): String {
val bin = System.getProperty("java.home") + File.separator + "bin" + File.separator
if (CURRENT_PLATFORM == Platform.WINDOWS && !forceConsole) {
val javaw = bin + "javaw.exe"
if (File(javaw).isFile) return javaw
}
if (CURRENT_PLATFORM == Platform.WINDOWS) return bin + "java.exe"
return bin + "java"
}
fun tryOpenUri(uri: String) {
when (CURRENT_PLATFORM) {
Platform.LINUX -> {
ProcessBuilder("xdg-open", uri).start()
}
Platform.WINDOWS -> {
val ret = Shell32.INSTANCE.ShellExecute(
null,
null,
uri,
null,
null,
WinUser.SW_SHOWNORMAL,
).toLong()
// According to Win32 documentation:
// If the function succeeds, it returns a value greater than 32.
// If the function fails, it returns an error value that indicates the cause of the failure.
if (ret <= 32) {
val err = Kernel32.INSTANCE.GetLastError()
throw RuntimeException(
"Failed to open URI $uri: ${
Kernel32Util.formatMessage(
err,
)
} (ret $ret)",
)
}
}
else -> Unit
}
}
fun getSocketDirectory(): String {
val envDir = System.getenv("SLIMEVR_SOCKET_DIR")
if (envDir != null) return envDir
if (CURRENT_PLATFORM == Platform.LINUX) {
val isPressureVessel = System.getenv("PRESSURE_VESSEL_RUNTIME")?.isNotEmpty() == true
if (isPressureVessel) {
val dir = System.getenv("XDG_DATA_HOME")?.let { Path(it, SLIMEVR_IDENTIFIER).toString() }
?: System.getenv("HOME")?.let { Path(it, ".local", "share", SLIMEVR_IDENTIFIER).toString() }
if (dir != null) return dir
}
val xdg = System.getenv("XDG_RUNTIME_DIR")
if (xdg != null) return xdg
}
return System.getProperty("java.io.tmpdir")
}
fun resolveConfigDirectory(): Path? {
if (Path("config/").exists()) { // this is only for dev
return Path("config/")
}
val home = System.getenv("HOME")
return when (CURRENT_PLATFORM) {
Platform.WINDOWS -> {
val appData = System.getenv("AppData")
if (appData != null) Path(appData, SLIMEVR_IDENTIFIER) else null
}
Platform.LINUX -> {
val xdg = System.getenv("XDG_CONFIG_HOME")
if (xdg != null) {
Path(xdg, SLIMEVR_IDENTIFIER)
} else if (home != null) {
Path(home, ".config", SLIMEVR_IDENTIFIER)
} else {
null
}
}
Platform.OSX -> {
if (home != null) Path(home, "Library", "Application Support", SLIMEVR_IDENTIFIER) else null
}
else -> null
}
}
fun resolveLogDirectory(): Path? {
if (Path("logs/").exists()) {
return Path("logs/")
}
val home = System.getenv("HOME")
val appData = System.getenv("AppData")
return when (CURRENT_PLATFORM) {
Platform.WINDOWS -> if (appData != null) Path(appData, SLIMEVR_IDENTIFIER, "logs") else null
Platform.OSX -> if (home != null) Path(home, "Library", "Logs", SLIMEVR_IDENTIFIER) else null
Platform.LINUX -> {
val xdg = System.getenv("XDG_DATA_HOME")
if (xdg != null) {
Path(xdg, SLIMEVR_IDENTIFIER, "logs")
} else if (home != null) {
Path(home, ".local", "share", SLIMEVR_IDENTIFIER, "logs")
} else {
null
}
}
else -> null
}
}