mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-20 13:26:02 +00:00
feat(frontend): launch from every frontend file format and store, with per-store game files (#2872)
This commit is contained in:
1 parent
45c31a480a
commit
dc9d1b81cc
10 files changed
+399
-53
No files matched your search
@@ -875,7 +875,7 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
|
||||
runCatching { com.droiddeck.launcher.frontend.AutoComponents.restoreMissing(this) }.onFailure { Log.w(TAG, "components: restore failed", it) }
|
||||
}, "components-restore").start()
|
||||
}
|
||||
private var pendingGameLink: String? = null
|
||||
private var pendingGameLink: com.droiddeck.launcher.frontend.GameRequest? = null
|
||||
private var gameLinkReading = false
|
||||
private var gameLinkTried = false
|
||||
private var gameSyncFolder by mutableStateOf<String?>(null)
|
||||
@@ -947,12 +947,12 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
|
||||
com.droiddeck.launcher.ui.DualScreenPanel.hide(taskId)
|
||||
val copy = Intent(request)
|
||||
Thread({
|
||||
val id = GameLaunchIntent.read(this, copy)
|
||||
val game = GameLaunchIntent.read(this, copy)
|
||||
ui.post {
|
||||
if (intent === request && !isDestroyed) {
|
||||
gameLinkReading = false
|
||||
pendingGameLink = id
|
||||
if (id == null) {
|
||||
pendingGameLink = game
|
||||
if (game == null) {
|
||||
syncDualPanel()
|
||||
android.widget.Toast.makeText(this, R.string.game_link_invalid, android.widget.Toast.LENGTH_LONG).show()
|
||||
} else {
|
||||
@@ -1202,16 +1202,16 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
|
||||
|
||||
private fun handleGameLink(games: List<Library.SteamGame>, scanned: Boolean = true) {
|
||||
if (!lifecycle.currentState.isAtLeast(androidx.lifecycle.Lifecycle.State.RESUMED)) return
|
||||
val id = pendingGameLink ?: return
|
||||
if (!scanned && games.none { it.gameIdString == id }) return
|
||||
val request = pendingGameLink ?: return
|
||||
val game = request.find(games, byTitle = scanned)
|
||||
if (!scanned && game == null) return
|
||||
gameLinkTried = true
|
||||
ui.postDelayed({
|
||||
if (!isDestroyed && lifecycle.currentState.isAtLeast(androidx.lifecycle.Lifecycle.State.RESUMED)) syncDualPanel()
|
||||
}, LINK_PANEL_MS)
|
||||
val game = games.firstOrNull { it.gameIdString == id }
|
||||
val link = intent
|
||||
fun consume() {
|
||||
if (intent !== link || pendingGameLink != id) return
|
||||
if (intent !== link || pendingGameLink !== request) return
|
||||
pendingGameLink = null
|
||||
link.data = null
|
||||
link.action = Intent.ACTION_MAIN
|
||||
@@ -1220,7 +1220,9 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
|
||||
if (game == null) {
|
||||
consume()
|
||||
syncDualPanel()
|
||||
android.widget.Toast.makeText(this, R.string.game_link_missing, android.widget.Toast.LENGTH_LONG).show()
|
||||
android.widget.Toast.makeText(this,
|
||||
if (request.program != null) R.string.game_link_program_unknown else R.string.game_link_missing,
|
||||
android.widget.Toast.LENGTH_LONG).show()
|
||||
} else launchGame(game, returnHome = true, onSettled = ::consume)
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,9 @@ package com.droiddeck.launcher.frontend
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.provider.DocumentsContract
|
||||
import android.provider.OpenableColumns
|
||||
import android.util.AtomicFile
|
||||
import com.droiddeck.launcher.R
|
||||
import androidx.core.util.readText
|
||||
@@ -13,27 +16,65 @@ import java.io.InputStream
|
||||
|
||||
object GameFiles {
|
||||
const val EXTENSION = "steam"
|
||||
private const val MAX_BYTES = 128
|
||||
private const val MAX_BYTES = 4096
|
||||
private const val MAX_NAME_BYTES = 200
|
||||
private const val INVENTORY = ".droiddeck-sync.json"
|
||||
private const val FORMAT = "droiddeck-game-files-v1"
|
||||
|
||||
fun readIntent(context: Context, request: Intent): String? = runCatching {
|
||||
fun readIntent(context: Context, request: Intent): GameRequest? = runCatching {
|
||||
if (request.action != Intent.ACTION_VIEW) return null
|
||||
val uri = request.data ?: return null
|
||||
GameLaunchLink.parse(uri.toString())?.let { return it }
|
||||
GameRequest.link(uri.toString())?.let { return GameRequest(it) }
|
||||
val name = displayName(context, uri)
|
||||
val extension = name?.substringAfterLast('.', "").orEmpty()
|
||||
if (extension.lowercase(java.util.Locale.ROOT) in GameRequest.PROGRAMS) {
|
||||
val program = localFile(context, uri)?.takeIf { it.isFile } ?: return null
|
||||
return GameRequest(title = name?.substringBeforeLast('.'), program = program.canonicalFile)
|
||||
}
|
||||
val stream = when (uri.scheme) {
|
||||
"content" -> context.contentResolver.openInputStream(uri)
|
||||
"file" -> {
|
||||
if (!uri.authority.isNullOrEmpty() || uri.query != null || uri.fragment != null) return null
|
||||
File(uri.path ?: return null).inputStream()
|
||||
}
|
||||
null -> File(uri.toString()).takeIf { it.isAbsolute }?.inputStream()
|
||||
"content" -> runCatching { context.contentResolver.openInputStream(uri) }.getOrNull()
|
||||
?: localFile(context, uri)?.takeIf { it.isFile }?.inputStream()
|
||||
"file", null -> localFile(context, uri)?.inputStream()
|
||||
else -> null
|
||||
} ?: return null
|
||||
stream.use(::read)
|
||||
val id = stream.use(::read) ?: return null
|
||||
GameRequest(id, GameRequest.sourceOf(extension), name?.substringBeforeLast('.'))
|
||||
}.getOrNull()
|
||||
|
||||
private fun displayName(context: Context, uri: Uri): String? = when (uri.scheme) {
|
||||
"content" -> runCatching {
|
||||
context.contentResolver.query(uri, arrayOf(OpenableColumns.DISPLAY_NAME), null, null, null)?.use { c ->
|
||||
if (c.moveToFirst()) c.getString(0) else null
|
||||
}
|
||||
}.getOrNull() ?: uri.lastPathSegment?.substringAfterLast('/')?.substringAfterLast(':')
|
||||
"file" -> uri.path?.let { File(it).name }
|
||||
null -> File(uri.toString()).name
|
||||
else -> null
|
||||
}?.takeIf { it.isNotEmpty() }
|
||||
|
||||
private fun localFile(context: Context, uri: Uri): File? = when (uri.scheme) {
|
||||
"file" -> {
|
||||
if (!uri.authority.isNullOrEmpty() || uri.query != null || uri.fragment != null) null
|
||||
else uri.path?.let(::File)
|
||||
}
|
||||
null -> File(uri.toString()).takeIf { it.isAbsolute }
|
||||
"content" -> storageDocument(context, uri) ?: uri.pathSegments.let { parts ->
|
||||
(0..1).asSequence().map { File("/" + parts.drop(it).joinToString("/")) }.firstOrNull { it.isFile }
|
||||
}
|
||||
else -> null
|
||||
}
|
||||
|
||||
// Frontends pass SAF document URIs or FileProvider URIs whose path mirrors shared storage.
|
||||
private fun storageDocument(context: Context, uri: Uri): File? {
|
||||
if (uri.authority != "com.android.externalstorage.documents" || !DocumentsContract.isDocumentUri(context, uri)) return null
|
||||
val id = DocumentsContract.getDocumentId(uri)
|
||||
val volume = id.substringBefore(':')
|
||||
val path = id.substringAfter(':', "")
|
||||
val root = if (volume.equals("primary", ignoreCase = true)) android.os.Environment.getExternalStorageDirectory()
|
||||
else File("/storage", volume)
|
||||
return File(root, path)
|
||||
}
|
||||
|
||||
fun read(stream: InputStream): String? {
|
||||
val bytes = ByteArray(MAX_BYTES + 1)
|
||||
var count = 0
|
||||
@@ -44,8 +85,7 @@ object GameFiles {
|
||||
count += n
|
||||
}
|
||||
if (count > MAX_BYTES) return null
|
||||
val text = String(bytes, 0, count, Charsets.UTF_8).trim()
|
||||
return text.takeIf(GameLaunchLink::validId)
|
||||
return GameRequest.parse(String(bytes, 0, count, Charsets.UTF_8))
|
||||
}
|
||||
|
||||
fun filename(game: Library.SteamGame, disambiguate: Boolean = false): String {
|
||||
@@ -61,7 +101,7 @@ object GameFiles {
|
||||
}
|
||||
val title = String(points, 0, end).trimEnd { it.isWhitespace() || it == '.' }.ifBlank { "Game" }
|
||||
val suffix = if (disambiguate) " (${game.gameIdString})" else ""
|
||||
return "$title$suffix.$EXTENSION"
|
||||
return "$title$suffix.${GameRequest.extension(game)}"
|
||||
}
|
||||
|
||||
private fun contents(id: String): String {
|
||||
@@ -130,7 +170,7 @@ object GameFiles {
|
||||
}
|
||||
for (name in previous.keys()) {
|
||||
val id = previous.getString(name)
|
||||
if (!next.has(name) && name.endsWith(".$EXTENSION") && GameLaunchLink.validId(id)) {
|
||||
if (!next.has(name) && ours(name) && GameLaunchLink.validId(id)) {
|
||||
val file = child(context, folder, name)
|
||||
if (matches(file, contents(id))) {
|
||||
val retained = exports.any { java.nio.file.Files.isSameFile(it.toPath(), file.toPath()) }
|
||||
@@ -141,6 +181,9 @@ object GameFiles {
|
||||
if (previous.toString() != next.toString()) write(inventory, JSONObject().put("format", FORMAT).put("games", next).toString())
|
||||
}
|
||||
|
||||
private fun ours(name: String) = name.substringAfterLast('.', "").lowercase(java.util.Locale.ROOT)
|
||||
.let { it == EXTENSION || it in GameRequest.extensions }
|
||||
|
||||
private fun inventoryOwns(context: Context, folder: File, record: JSONObject, file: File, id: String): Boolean {
|
||||
if (record.optString(file.name) == id) return true
|
||||
if (!file.exists()) return false
|
||||
|
||||
@@ -11,22 +11,26 @@ object GameLaunchIntent {
|
||||
|
||||
fun accepts(action: String?): Boolean = action == Intent.ACTION_VIEW || action == ACTION || action == GAMENATIVE_ACTION
|
||||
|
||||
fun read(context: Context, intent: Intent): String? = when (intent.action) {
|
||||
fun read(context: Context, intent: Intent): GameRequest? = when (intent.action) {
|
||||
Intent.ACTION_VIEW -> GameFiles.readIntent(context, intent)
|
||||
ACTION, GAMENATIVE_ACTION -> readId(intent)
|
||||
ACTION, GAMENATIVE_ACTION -> readExtras(intent)
|
||||
else -> null
|
||||
}
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
internal fun readId(intent: Intent): String? = runCatching {
|
||||
internal fun readExtras(intent: Intent): GameRequest? = runCatching {
|
||||
if (intent.action != ACTION && intent.action != GAMENATIVE_ACTION) return null
|
||||
val source = intent.extras?.get(EXTRA_GAME_SOURCE)
|
||||
if (source != null && (source !is String || !source.equals("STEAM", ignoreCase = true))) return null
|
||||
val source = when (val value = intent.extras?.get(EXTRA_GAME_SOURCE)) {
|
||||
null -> Library.SOURCE_STEAM
|
||||
is String -> GameRequest.sourceNamed(value) ?: return null
|
||||
else -> return null
|
||||
}
|
||||
val id = when (val value = intent.extras?.get(EXTRA_APP_ID)) {
|
||||
is Int -> value.takeIf { it > 0 }?.toString()
|
||||
is Long -> value.takeIf { it != 0L }?.let(java.lang.Long::toUnsignedString)
|
||||
is String -> value.takeIf(GameLaunchLink::validId)
|
||||
else -> null
|
||||
}
|
||||
id
|
||||
} ?: return null
|
||||
GameRequest(id, source)
|
||||
}.getOrNull()
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package com.droiddeck.launcher.frontend
|
||||
|
||||
import com.droiddeck.launcher.stores.Store
|
||||
import java.io.File
|
||||
import java.util.Locale
|
||||
|
||||
class GameRequest(
|
||||
val id: String? = null,
|
||||
val source: String = Library.SOURCE_STEAM,
|
||||
val title: String? = null,
|
||||
val program: File? = null,
|
||||
) {
|
||||
fun find(games: List<Library.SteamGame>, byTitle: Boolean = true): Library.SteamGame? {
|
||||
program?.let { return owner(games, it) }
|
||||
val exact = id?.let { id -> games.firstOrNull { it.gameIdString == id } }
|
||||
if (source == Library.SOURCE_STEAM) return exact ?: games.firstOrNull { it.appId > 0 && it.appId.toString() == id }
|
||||
exact?.takeIf { it.source == source }?.let { return it }
|
||||
id?.let { id -> games.firstOrNull { it.source == source && it.storeId == id } }?.let { return it }
|
||||
if (!byTitle) return null
|
||||
val key = title?.let(::titleKey)?.takeIf { it.isNotEmpty() } ?: return null
|
||||
return games.firstOrNull { it.source == source && titleKey(it.name) == key }
|
||||
}
|
||||
|
||||
private fun owner(games: List<Library.SteamGame>, program: File): Library.SteamGame? {
|
||||
val path = program.path
|
||||
return games.mapNotNull { game ->
|
||||
val folder = game.gameFiles ?: return@mapNotNull null
|
||||
val dir = folder.absolutePath.takeIf { inside(path, it) }
|
||||
?: runCatching { folder.canonicalPath }.getOrNull()?.takeIf { inside(path, it) }
|
||||
dir?.let { game to it.length }
|
||||
}.maxByOrNull { it.second }?.first
|
||||
}
|
||||
|
||||
companion object {
|
||||
val PROGRAMS = setOf("exe", "bat")
|
||||
private const val CUSTOM_GAME = "CUSTOM_GAME"
|
||||
private const val CUSTOM_EXTENSION = "pcgame"
|
||||
private val DAIJISHOU = setOf("# Daijishou Player Template", "# DST")
|
||||
private val daijishouId = Regex("""(?i)\[steamappid]\s*([0-9]+)""")
|
||||
private val steamLink = Regex("steam://rungameid/([1-9][0-9]{0,19})")
|
||||
|
||||
val extensions: Set<String> = Store.entries.map { it.id }.toSet() + CUSTOM_EXTENSION
|
||||
|
||||
fun extension(game: Library.SteamGame): String = Store.byId(game.source)?.id ?: GameFiles.EXTENSION
|
||||
|
||||
fun sourceNamed(value: String): String? = when {
|
||||
value.equals(Library.SOURCE_STEAM, ignoreCase = true) -> Library.SOURCE_STEAM
|
||||
value.equals(CUSTOM_GAME, ignoreCase = true) -> Library.ADDED
|
||||
else -> Store.entries.firstOrNull { it.name.equals(value, ignoreCase = true) }?.id
|
||||
}
|
||||
|
||||
fun sourceOf(extension: String): String = when (val ext = extension.lowercase(Locale.ROOT)) {
|
||||
CUSTOM_EXTENSION -> Library.ADDED
|
||||
else -> Store.byId(ext)?.id ?: Library.SOURCE_STEAM
|
||||
}
|
||||
|
||||
fun link(text: String): String? = text.takeIf(GameLaunchLink::validId)
|
||||
?: GameLaunchLink.parse(text)
|
||||
?: steamLink.matchEntire(text)?.groupValues?.get(1)?.takeIf(GameLaunchLink::validId)
|
||||
|
||||
fun parse(text: String): String? {
|
||||
link(text.trim())?.let { return it }
|
||||
val lines = text.lines().map { it.trim() }.filter { it.isNotEmpty() }
|
||||
val header = lines.firstOrNull() ?: return null
|
||||
return when {
|
||||
DAIJISHOU.any { header.equals(it, ignoreCase = true) } -> lines.firstNotNullOfOrNull { daijishouId.matchEntire(it) }
|
||||
?.groupValues?.get(1)?.takeIf(GameLaunchLink::validId)
|
||||
header.equals("[InternetShortcut]", ignoreCase = true) -> lines.firstOrNull { it.startsWith("URL=", ignoreCase = true) }
|
||||
?.substring(4)?.trim()?.takeIf { !GameLaunchLink.validId(it) }?.let(::link)
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
internal fun titleKey(name: String): String = name.lowercase(Locale.ROOT).filter { it.isLetterOrDigit() }
|
||||
|
||||
private fun inside(path: String, dir: String) = path.startsWith(dir.trimEnd(File.separatorChar) + File.separator)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user