mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 12:28:40 +00:00
feat(library): pick the game resolution for games started without Steam (#2520)
This commit is contained in:
1 parent
1b732c1b65
commit
bb05e20d1b
43 files changed
+332
-2
No files matched your search
@@ -49,6 +49,16 @@ object GameEnvironment {
|
||||
return withScopeValue(config, scope, NO_STEAM, if (on) "1" else null)
|
||||
}
|
||||
|
||||
const val GAME_RESOLUTION = "DROIDDECK_GAME_RESOLUTION"
|
||||
|
||||
fun gameResolution(config: Config, scope: String): GameResolution.Choice? =
|
||||
if (scope.isEmpty()) null else GameResolution.parse(config.games[scope]?.get(GAME_RESOLUTION))
|
||||
|
||||
fun withGameResolution(config: Config, scope: String, choice: GameResolution.Choice?): Config {
|
||||
require(scope.isNotEmpty() && validScope(scope))
|
||||
return withScopeValue(config, scope, GAME_RESOLUTION, choice?.let(GameResolution::format))
|
||||
}
|
||||
|
||||
fun effective(config: Config, preset: String, scope: String, forceSsbs: Boolean = false): Map<String, String?> =
|
||||
defaults(preset, forceSsbs) + config.shared + if (scope.isEmpty()) emptyMap() else config.games[scope].orEmpty()
|
||||
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.droiddeck.launcher.core
|
||||
|
||||
object GameResolution {
|
||||
sealed interface Choice
|
||||
data class Fixed(val width: Int, val height: Int) : Choice
|
||||
data class Scaled(val scale: Float) : Choice
|
||||
|
||||
data class Aspect(val label: String, val sizes: List<Fixed>)
|
||||
|
||||
val aspects = listOf(
|
||||
Aspect("16:9", listOf(Fixed(960, 540), Fixed(1280, 720), Fixed(1600, 900), Fixed(1920, 1080), Fixed(2560, 1440))),
|
||||
Aspect("16:10", listOf(Fixed(1280, 800), Fixed(1440, 900), Fixed(1680, 1050), Fixed(1920, 1200), Fixed(2560, 1600))),
|
||||
Aspect("4:3", listOf(Fixed(640, 480), Fixed(800, 600), Fixed(1024, 768), Fixed(1280, 960), Fixed(1440, 1080))),
|
||||
)
|
||||
|
||||
val scales = listOf(1.5f, 2f, 3f)
|
||||
|
||||
const val MIN_WIDTH = 320
|
||||
const val MIN_HEIGHT = 240
|
||||
const val MAX_SIDE = 7680
|
||||
private const val SCALED_PREFIX = "/"
|
||||
|
||||
fun parse(value: String?): Choice? {
|
||||
val text = value?.trim().orEmpty()
|
||||
if (text.startsWith(SCALED_PREFIX)) {
|
||||
return text.removePrefix(SCALED_PREFIX).toFloatOrNull()?.takeIf { it > 1f && it <= 8f }?.let(::Scaled)
|
||||
}
|
||||
val parts = text.split('x', 'X', '×').map { it.trim().toIntOrNull() }
|
||||
if (parts.size != 2) return null
|
||||
val (w, h) = parts
|
||||
return if (w != null && h != null && valid(w, h)) Fixed(w and 1.inv(), h and 1.inv()) else null
|
||||
}
|
||||
|
||||
fun valid(width: Int, height: Int) = width in MIN_WIDTH..MAX_SIDE && height in MIN_HEIGHT..MAX_SIDE
|
||||
|
||||
fun format(choice: Choice): String = when (choice) {
|
||||
is Fixed -> "${choice.width}x${choice.height}"
|
||||
is Scaled -> SCALED_PREFIX + formatScale(choice.scale)
|
||||
}
|
||||
|
||||
fun formatScale(scale: Float): String =
|
||||
(Math.round(scale * 10) / 10f).let { if (it % 1f == 0f) it.toInt().toString() else it.toString() }
|
||||
|
||||
fun resolve(choice: Choice, output: Pair<Int, Int>): Pair<Int, Int> = when (choice) {
|
||||
is Fixed -> choice.width to choice.height
|
||||
is Scaled -> (Math.round(output.first / choice.scale).coerceAtLeast(MIN_WIDTH) and 1.inv()) to
|
||||
(Math.round(output.second / choice.scale).coerceAtLeast(MIN_HEIGHT) and 1.inv())
|
||||
}
|
||||
|
||||
fun aspectLabel(width: Int, height: Int): String {
|
||||
if (width <= 0 || height <= 0) return ""
|
||||
val ratio = width.toFloat() / height
|
||||
aspects.firstOrNull { a ->
|
||||
val (w, h) = a.label.split(':').map(String::toFloat)
|
||||
kotlin.math.abs(ratio - w / h) < 0.02f
|
||||
}?.let { return it.label }
|
||||
if (kotlin.math.abs(ratio - 64f / 27f) < 0.04f || kotlin.math.abs(ratio - 21f / 9f) < 0.04f) return "21:9"
|
||||
val g = gcd(width, height)
|
||||
return if (width / g <= 32) "${width / g}:${height / g}" else String.format(java.util.Locale.ROOT, "%.2f:1", ratio)
|
||||
}
|
||||
|
||||
fun sizesFor(aspect: Aspect, panel: Pair<Int, Int>): List<Fixed> {
|
||||
val long = maxOf(panel.first, panel.second)
|
||||
val short = minOf(panel.first, panel.second)
|
||||
val fit = aspect.sizes.filter { it.width <= long && it.height <= short }
|
||||
return fit.ifEmpty { aspect.sizes.take(1) }
|
||||
}
|
||||
|
||||
private tailrec fun gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b)
|
||||
}
|
||||
@@ -79,6 +79,13 @@ object GameEnvironmentStore {
|
||||
return config
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun setGameResolution(context: Context, scope: String, choice: com.droiddeck.launcher.core.GameResolution.Choice?): GameEnvironment.Config {
|
||||
val config = GameEnvironment.withGameResolution(read(context), scope, choice)
|
||||
save(context, config)
|
||||
return config
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun setDefault(context: Context, scope: String, name: String, value: String): Boolean {
|
||||
val config = read(context)
|
||||
|
||||
@@ -30,6 +30,25 @@ object NoSteamLaunch {
|
||||
GameEnvironment.gameNoSteam(GameEnvironmentStore.read(context), (appId and 0xFFFFFFFFL).toString())
|
||||
}.getOrDefault(false)
|
||||
|
||||
fun scopeOf(program: String?, args: List<String>, battleNetTitle: (String) -> String? = { null }): String? = when (program) {
|
||||
EPIC -> EpicClient.shortcutAppId.toString()
|
||||
BATTLENET -> args.getOrNull(args.indexOf("launch") + 1)?.takeIf { args.contains("launch") }?.let(battleNetTitle)
|
||||
?: com.droiddeck.launcher.runtime.BattleNet.APP_ID.toString()
|
||||
PROTON_RUN -> args.getOrNull(args.indexOf("--app") + 1)?.takeIf { args.contains("--app") }
|
||||
else -> null
|
||||
}?.takeIf { it.isNotEmpty() && GameEnvironment.validScope(it) }
|
||||
|
||||
fun battleNetTitleScope(context: Context, code: String): String? =
|
||||
AddedGames.scan(context).firstOrNull { game ->
|
||||
game.source == com.droiddeck.launcher.stores.Store.BATTLENET.id &&
|
||||
game.storeId?.let(com.droiddeck.launcher.runtime.BattleNet::title)?.code == code
|
||||
}?.let { settingsId(it).toString() }
|
||||
|
||||
fun resolution(config: GameEnvironment.Config, scope: String?, output: Pair<Int, Int>): Pair<Int, Int>? {
|
||||
val choice = scope?.let { GameEnvironment.gameResolution(config, it) } ?: return null
|
||||
return com.droiddeck.launcher.core.GameResolution.resolve(choice, output)
|
||||
}
|
||||
|
||||
fun command(game: Library.SteamGame, added: AddedGames.Game?): Command? = when {
|
||||
game.library == Library.EPIC_LAUNCHER -> Command(EPIC, listOf("run"), game.name)
|
||||
game.library == Library.ADDED && game.source == com.droiddeck.launcher.stores.Store.BATTLENET.id ->
|
||||
|
||||
@@ -59,7 +59,7 @@ data class SelectedGameProfile(
|
||||
)
|
||||
}
|
||||
val otherEnvironment = environment.games[game.profileKey].orEmpty().keys - FexPreset.environmentKeys -
|
||||
GameEnvironment.NO_STEAM
|
||||
GameEnvironment.NO_STEAM - GameEnvironment.GAME_RESOLUTION
|
||||
return SelectedGameProfile(
|
||||
appId = selectedAppId,
|
||||
name = game.name,
|
||||
|
||||
@@ -588,6 +588,14 @@ class SessionService : Service() {
|
||||
|
||||
guest.add("BL_WIDTH=" + size.first)
|
||||
guest.add("BL_HEIGHT=" + size.second)
|
||||
if (noSteamGame()) runCatching {
|
||||
val scope = NoSteamLaunch.scopeOf(SessionState.program, SessionState.programArgs) { NoSteamLaunch.battleNetTitleScope(this, it) }
|
||||
NoSteamLaunch.resolution(GameEnvironmentStore.read(this), scope, size)
|
||||
}.onFailure { Log.w(TAG, "game resolution", it) }.getOrNull()?.let { (w, h) ->
|
||||
guest.add("BL_GAME_WIDTH=$w")
|
||||
guest.add("BL_GAME_HEIGHT=$h")
|
||||
Log.i(TAG, "game resolution: ${w}x$h on a ${size.first}x${size.second} output")
|
||||
}
|
||||
if (SessionState.followScreen || size.first < size.second ||
|
||||
SessionPrefs.orientation(this) == android.content.pm.ActivityInfo.SCREEN_ORIENTATION_USER)
|
||||
guest.add("BL_FOLLOW_OUTPUT=1")
|
||||
|
||||
@@ -918,12 +918,37 @@ internal fun GameSettingsMenus(host: MenuHost, game: com.droiddeck.launcher.fron
|
||||
val custom = stringResource(R.string.comp_game_fex_custom)
|
||||
val customSync = stringResource(R.string.game_sync_custom)
|
||||
val resetDone = stringResource(R.string.comp_game_reset_done, game.name)
|
||||
val resKey = "game-settings-res"
|
||||
val resolution = current?.let { com.droiddeck.launcher.core.GameEnvironment.gameResolution(it, scope) }
|
||||
val screen = remember { com.droiddeck.launcher.session.SessionDisplay.screenSize(com.droiddeck.launcher.session.SessionDisplay.panelSize(appContext)) }
|
||||
val matchScreen = stringResource(R.string.display_match_screen, screen.first, screen.second)
|
||||
val scaledLabels = com.droiddeck.launcher.core.GameResolution.scales.associateWith {
|
||||
stringResource(R.string.display_follow_screen_scaled, com.droiddeck.launcher.core.GameResolution.formatScale(it))
|
||||
}
|
||||
fun sizeLabel(size: Pair<Int, Int>) = "${size.first}×${size.second} · ${com.droiddeck.launcher.core.GameResolution.aspectLabel(size.first, size.second)}"
|
||||
fun resolutionLabel(choice: com.droiddeck.launcher.core.GameResolution.Choice?): String = when (choice) {
|
||||
null -> matchScreen
|
||||
is com.droiddeck.launcher.core.GameResolution.Scaled -> (scaledLabels[choice.scale]
|
||||
?: "÷" + com.droiddeck.launcher.core.GameResolution.formatScale(choice.scale)) +
|
||||
" · " + sizeLabel(com.droiddeck.launcher.core.GameResolution.resolve(choice, screen))
|
||||
is com.droiddeck.launcher.core.GameResolution.Fixed -> sizeLabel(choice.width to choice.height)
|
||||
}
|
||||
fun pickResolution(choice: com.droiddeck.launcher.core.GameResolution.Choice?) {
|
||||
host.open = key
|
||||
save { com.droiddeck.launcher.session.GameEnvironmentStore.setGameResolution(it, scope, choice) }
|
||||
}
|
||||
var editResolution by remember(scope) { mutableStateOf(false) }
|
||||
AnchoredMenu(host.open == key, onDismiss = { if (host.open == key) host.open = null }, title = stringResource(R.string.game_settings_menu_title, game.name)) { first ->
|
||||
MenuItem(stringResource(R.string.game_no_steam_title), checked = noSteam, enabled = ready,
|
||||
detail = stringResource(R.string.game_no_steam_detail), focusRequester = first) {
|
||||
val on = !noSteam
|
||||
save { com.droiddeck.launcher.session.GameEnvironmentStore.setGameNoSteam(it, scope, on) }
|
||||
}
|
||||
if (noSteam) MenuItem(
|
||||
stringResource(R.string.game_res_title), checked = false, enabled = ready,
|
||||
detail = resolutionLabel(resolution),
|
||||
trailing = { Text("›", fontSize = 16.sp, color = colors.onSurfaceVariant) },
|
||||
) { host.open = resKey }
|
||||
if (!launcher) {
|
||||
val chosen = com.droiddeck.launcher.core.FexPreset.all.firstOrNull { it.id == fex }
|
||||
MenuItem(
|
||||
@@ -960,6 +985,45 @@ internal fun GameSettingsMenus(host: MenuHost, game: com.droiddeck.launcher.fron
|
||||
}
|
||||
}
|
||||
}
|
||||
AnchoredMenu(host.open == resKey, onDismiss = { if (host.open == resKey) host.open = null }, title = stringResource(R.string.game_res_title)) { first ->
|
||||
MenuItem(matchScreen, checked = resolution == null, enabled = ready, focusRequester = first) { pickResolution(null) }
|
||||
com.droiddeck.launcher.core.GameResolution.scales.forEach { scale ->
|
||||
val choice = com.droiddeck.launcher.core.GameResolution.Scaled(scale)
|
||||
MenuItem(scaledLabels.getValue(scale), checked = resolution == choice, enabled = ready,
|
||||
detail = sizeLabel(com.droiddeck.launcher.core.GameResolution.resolve(choice, screen))) { pickResolution(choice) }
|
||||
}
|
||||
com.droiddeck.launcher.core.GameResolution.aspects.forEach { aspect ->
|
||||
val sizes = com.droiddeck.launcher.core.GameResolution.sizesFor(aspect, screen)
|
||||
MenuItem(aspect.label, checked = resolution in aspect.sizes, enabled = ready,
|
||||
detail = sizes.joinToString(" · ") { "${it.width}×${it.height}" },
|
||||
trailing = { Text("›", fontSize = 16.sp, color = colors.onSurfaceVariant) },
|
||||
) { host.open = "$resKey-${aspect.label}" }
|
||||
}
|
||||
val fixed = resolution as? com.droiddeck.launcher.core.GameResolution.Fixed
|
||||
val customValue = fixed?.takeIf { f -> com.droiddeck.launcher.core.GameResolution.aspects.none { f in it.sizes } }
|
||||
MenuItem(
|
||||
if (customValue == null) stringResource(R.string.mode_res_custom) else stringResource(R.string.mode_res_custom_value, customValue.width, customValue.height),
|
||||
checked = customValue != null, enabled = ready,
|
||||
detail = customValue?.let { com.droiddeck.launcher.core.GameResolution.aspectLabel(it.width, it.height) },
|
||||
) { editResolution = true }
|
||||
}
|
||||
com.droiddeck.launcher.core.GameResolution.aspects.forEach { aspect ->
|
||||
val aspectKey = "$resKey-${aspect.label}"
|
||||
AnchoredMenu(host.open == aspectKey, onDismiss = { if (host.open == aspectKey) host.open = null }, title = aspect.label) { first ->
|
||||
com.droiddeck.launcher.core.GameResolution.sizesFor(aspect, screen).forEachIndexed { index, size ->
|
||||
MenuItem("${size.width}×${size.height}", checked = resolution == size, enabled = ready,
|
||||
focusRequester = if (index == 0) first else null) { pickResolution(size) }
|
||||
}
|
||||
}
|
||||
}
|
||||
if (editResolution) CustomResolutionDialog(
|
||||
(resolution as? com.droiddeck.launcher.core.GameResolution.Fixed)?.let { it.width to it.height },
|
||||
onSave = { value ->
|
||||
editResolution = false
|
||||
pickResolution(com.droiddeck.launcher.core.GameResolution.Fixed(value.first, value.second))
|
||||
},
|
||||
onDismiss = { editResolution = false },
|
||||
)
|
||||
AnchoredMenu(host.open == fexKey, onDismiss = { if (host.open == fexKey) host.open = null }, title = stringResource(R.string.fex_preset_title)) { first ->
|
||||
MenuItem(useDefault, checked = fex == null, enabled = ready, detail = stringResource(generalFex.label), focusRequester = first) {
|
||||
host.open = key
|
||||
|
||||
Reference in new issue
Block a user