From d7bdeaa48a6e15225dad8387218fd0c0fa74e141 Mon Sep 17 00:00:00 2001 From: Sapphire Date: Fri, 19 Jun 2026 02:53:59 -0500 Subject: [PATCH] Port #1785 See: https://github.com/SlimeVR/SlimeVR-Server/pull/1785 --- .../java/dev/slimevr/desktop/linux/steam.kt | 81 +++++++++++++++++++ .../java/dev/slimevr/desktop/vrchat/linux.kt | 27 +++---- 2 files changed, 92 insertions(+), 16 deletions(-) create mode 100644 server/desktop/src/main/java/dev/slimevr/desktop/linux/steam.kt diff --git a/server/desktop/src/main/java/dev/slimevr/desktop/linux/steam.kt b/server/desktop/src/main/java/dev/slimevr/desktop/linux/steam.kt new file mode 100644 index 000000000..3330330ea --- /dev/null +++ b/server/desktop/src/main/java/dev/slimevr/desktop/linux/steam.kt @@ -0,0 +1,81 @@ +package dev.slimevr.desktop.linux + +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import java.io.BufferedReader +import java.io.FileNotFoundException +import java.io.FileReader +import java.nio.file.Path +import kotlin.io.path.Path +import kotlin.io.path.exists + +internal val STEAM_PATH = System.getenv("HOME")?.let { home -> + listOf( + Path(home, ".steam", "root"), + Path(home, ".steam", "debian-installation"), + Path(home, ".var", "app", "com.valvesoftware.Steam", "data", "Steam"), + ).firstOrNull { it.exists() } +} +internal val VDF_KEY_VALUES_PATTERN = Regex(""""(\w+)"[ \t]*(?:"(.+)")?""") + +suspend fun findAppLibraryLocation(appId: Int): Path = withContext(Dispatchers.IO) { + if (STEAM_PATH == null) throw FileNotFoundException("Unable to find Steam") + val libraryFoldersPath = STEAM_PATH.resolve("config/libraryfolders.vdf") + if (!libraryFoldersPath.exists()) throw FileNotFoundException("Steam libraryfolders.vdf does not exist") + + BufferedReader(FileReader(STEAM_PATH.resolve("config/libraryfolders.vdf").toFile())).use { reader -> + var depth = 0 + var currentLibraryPath: Path? = null + + while (reader.ready()) { + val line = reader.readLine().trim() + if (line == "{") { + depth += 1 + continue + } + if (line == "}") { + depth -= 1 + continue + } + + VDF_KEY_VALUES_PATTERN.matchEntire(line)?.let { + val key = it.groupValues[1] + val value = it.groupValues[2] + + when (depth) { + 0 -> { + require(key == "libraryfolders") { "root key must be libraryfolders" } + } + + 1 -> { + // start of a library node + } + + 2 -> { + // in a library node + if (key == "path") { + currentLibraryPath = Path(value) + } else if (key == "apps") { + // start of apps node + require(currentLibraryPath != null) { "path must come before apps in node" } + } + } + + 3 -> { + // in apps node + val curAppId = try { + key.toInt() + } catch (_: NumberFormatException) { + null + } + if (curAppId != null && curAppId == appId) { + check(currentLibraryPath != null) { "path must come before apps in node" } + return@withContext currentLibraryPath + } + } + } + } + } + throw RuntimeException("Couldn't find library folder for appid $appId") + } +} diff --git a/server/desktop/src/main/java/dev/slimevr/desktop/vrchat/linux.kt b/server/desktop/src/main/java/dev/slimevr/desktop/vrchat/linux.kt index 828f6333f..3e23fb8b7 100644 --- a/server/desktop/src/main/java/dev/slimevr/desktop/vrchat/linux.kt +++ b/server/desktop/src/main/java/dev/slimevr/desktop/vrchat/linux.kt @@ -1,6 +1,7 @@ package dev.slimevr.desktop.vrchat import dev.slimevr.AppLogger +import dev.slimevr.desktop.linux.findAppLibraryLocation import kotlinx.coroutines.CancellationException import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay @@ -13,8 +14,7 @@ import java.io.FileReader import java.io.InvalidObjectException import java.nio.ByteBuffer import java.nio.ByteOrder -import kotlin.io.path.Path -import kotlin.io.path.exists +import java.nio.file.Path private const val USER_REG_SUBPATH = "steamapps/compatdata/438100/pfx/user.reg" private val KEY_VALUE_PATTERN = Regex(""""(.+)"=(.+)""") @@ -22,22 +22,15 @@ private val HEX_FORMAT = HexFormat { upperCase = false bytes.byteSeparator = "," } +private const val VRCHAT_APPID = 438100 -internal val linuxUserRegPath = System.getenv("HOME")?.let { home -> - listOf( - Path(home, ".steam", "root", USER_REG_SUBPATH), - Path(home, ".steam", "debian-installation", USER_REG_SUBPATH), - Path(home, ".var", "app", "com.valvesoftware.Steam", "data", "Steam", USER_REG_SUBPATH), - ).firstOrNull { it.exists() } -} - -internal suspend fun linuxGetVRChatKeys(path: String, registry: MutableMap): Map { +internal suspend fun linuxGetVRChatKeys(regPath: Path, keyPath: String, registry: MutableMap): Map { val keysMap = mutableMapOf() registry.clear() try { withContext(Dispatchers.IO) { - BufferedReader(FileReader(linuxUserRegPath?.toFile() ?: return@withContext)).use { reader -> - val actualPath = "[${path.replace("\\", """\\""")}]" + BufferedReader(FileReader(regPath.toFile() ?: return@withContext)).use { reader -> + val actualPath = "[${keyPath.replace("\\", """\\""")}]" while (reader.ready()) { val line = reader.readLine() if (!line.startsWith(actualPath)) continue @@ -86,15 +79,17 @@ internal suspend fun linuxGetDwordValue(registry: Map, key: Stri } internal fun linuxVRCConfigFlow(): Flow = flow { - val regPath = linuxUserRegPath ?: run { - AppLogger.vrc.info("[VRChatRegEdit] Couldn't find any VRChat registry file") + val regPath = try { + findAppLibraryLocation(VRCHAT_APPID).resolve(USER_REG_SUBPATH) + } catch (e: Exception) { + AppLogger.vrc.warn(e, "[VRChatRegEdit] Couldn't find any VRChat registry file") return@flow } AppLogger.vrc.info("[VRChatRegEdit] Using VRChat registry file: $regPath") val registry = mutableMapOf() while (true) { - val keys = linuxGetVRChatKeys(VRC_REG_PATH, registry) + val keys = linuxGetVRChatKeys(regPath, VRC_REG_PATH, registry) if (keys.isEmpty()) { emit(null) } else {