mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-22 14:26:00 +00:00
feat(stores): download Amazon engine on demand
This commit is contained in:
1 parent
61482ffee7
commit
9222a1fe4d
28 files changed
+2194
-38
No files matched your search
@@ -146,5 +146,7 @@ internal class AddonPack(
|
||||
listOf("version", "xodus-cli", "xodus-service"))
|
||||
val COMET = AddonPack("comet", BuildConfig.PACK_COMET_SHA256, BuildConfig.PACK_COMET_SIZE,
|
||||
listOf("version", "comet"))
|
||||
val AMAZON = AddonPack("amazon", BuildConfig.PACK_AMAZON_SHA256, BuildConfig.PACK_AMAZON_SIZE,
|
||||
listOf("engine.json", "libdroiddeckamazon.so"), reuseDeviceCopy = false)
|
||||
}
|
||||
}
|
||||
@@ -130,6 +130,7 @@ class StoreLoginActivity : ComponentActivity() {
|
||||
private fun newFlow(state: String): Flow? {
|
||||
val url = clientUrl
|
||||
if (url != null) return com.droiddeck.launcher.stores.epic.EpicClient.loginFlow(url, chromeUa)
|
||||
if (store == Store.AMAZON && !com.droiddeck.launcher.stores.amazon.AmazonNative.loadInstalled(applicationContext)) return null
|
||||
return store?.let { LoginFlows.forStore(it, state) }
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.droiddeck.launcher.stores.amazon
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import com.droiddeck.launcher.R
|
||||
import com.droiddeck.launcher.stores.CatalogItem
|
||||
import com.droiddeck.launcher.stores.InstalledStoreGame
|
||||
import com.droiddeck.launcher.stores.LoginFlows
|
||||
@@ -21,20 +22,26 @@ object AmazonBackend : StoreBackend {
|
||||
LoginFlows.amazonFlow = { AmazonLogin.create() }
|
||||
}
|
||||
|
||||
private fun engineMissing(): Boolean {
|
||||
if (AmazonNative.ready()) return false
|
||||
StoresState.logLine(AmazonNative.ENGINE_MISSING)
|
||||
StoresState.post { StoresState.problems[store] = AmazonNative.ENGINE_MISSING }
|
||||
return true
|
||||
}
|
||||
|
||||
override fun signIn(context: Context) {
|
||||
if (engineMissing()) return
|
||||
context.startActivity(
|
||||
Intent(context, StoreLoginActivity::class.java)
|
||||
.putExtra(StoreLoginActivity.EXTRA_STORE, store.id)
|
||||
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
|
||||
)
|
||||
val app = context.applicationContext
|
||||
Thread({
|
||||
val available = AmazonNative.prepare(app) { state ->
|
||||
if (state is com.droiddeck.launcher.core.AddonPack.State.Downloading) {
|
||||
StoresState.post { StoresState.status[store] = app.getString(R.string.stores_engine_downloading, store.label, (state.progress * 100).toInt()) }
|
||||
}
|
||||
}
|
||||
StoresState.post {
|
||||
StoresState.status.remove(store)
|
||||
if (available) {
|
||||
StoresState.problems.remove(store)
|
||||
app.startActivity(Intent(app, StoreLoginActivity::class.java)
|
||||
.putExtra(StoreLoginActivity.EXTRA_STORE, store.id)
|
||||
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
|
||||
} else {
|
||||
StoresState.problems[store] = app.getString(R.string.stores_engine_failed, store.label)
|
||||
}
|
||||
}
|
||||
}, "amazon-engine").start()
|
||||
}
|
||||
|
||||
override fun signOut(context: Context) {
|
||||
|
||||
@@ -75,6 +75,7 @@ object AmazonCredentialStore {
|
||||
|
||||
private fun refreshLocked(context: Context, a: Account): String? {
|
||||
if (a.refreshToken.isEmpty()) return null
|
||||
if (!AmazonNative.prepare(context)) return null
|
||||
val answer = AmazonNative.call("auth.refresh", JSONObject().put("refreshToken", a.refreshToken))
|
||||
val access = answer?.takeIf { !it.has("error") }?.optString(ACCESS_TOKEN, "").orEmpty()
|
||||
if (access.isEmpty()) {
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.droiddeck.launcher.stores.amazon
|
||||
|
||||
import java.io.File
|
||||
import java.security.MessageDigest
|
||||
import org.json.JSONObject
|
||||
|
||||
internal object AmazonEngine {
|
||||
fun verify(directory: File): Boolean = runCatching {
|
||||
val manifest = File(directory, "engine.json")
|
||||
val library = File(directory, "libdroiddeckamazon.so")
|
||||
check(manifest.canonicalFile == manifest.absoluteFile && library.canonicalFile == library.absoluteFile)
|
||||
check(manifest.length() in 1..131_072 && library.isFile)
|
||||
val data = JSONObject(manifest.readText())
|
||||
check(data.getInt("schema") == 1 && data.getInt("abi") == 1)
|
||||
check(data.getString("engine") == "amazon" && data.getString("target") == "aarch64-linux-android")
|
||||
val hash = MessageDigest.getInstance("SHA-256")
|
||||
library.inputStream().use { input ->
|
||||
val block = ByteArray(64 * 1024)
|
||||
while (true) {
|
||||
val count = input.read(block)
|
||||
if (count < 0) break
|
||||
hash.update(block, 0, count)
|
||||
}
|
||||
}
|
||||
val actual = hash.digest().joinToString("") { "%02x".format(it.toInt() and 0xff) }
|
||||
check(actual == data.getString("library_sha256"))
|
||||
true
|
||||
}.getOrDefault(false)
|
||||
}
|
||||
@@ -24,6 +24,11 @@ internal class AmazonInstaller(
|
||||
private val cancelled = AtomicBoolean(false)
|
||||
|
||||
override fun run(handle: DownloadQueue.JobHandle): String? {
|
||||
handle.stage(DownloadStage.MANIFEST, app.getString(R.string.stores_engine_preparing, Store.AMAZON.label))
|
||||
if (!AmazonNative.prepare(app, { !cancelled.get() })) {
|
||||
if (cancelled.get()) return null
|
||||
throw IllegalStateException(AmazonNative.ENGINE_MISSING)
|
||||
}
|
||||
handle.stage(DownloadStage.MANIFEST, app.getString(R.string.stores_sync_sign_in))
|
||||
val token = AmazonCredentialStore.getValidAccessToken(app) ?: throw IllegalStateException("Not signed in to Amazon Games")
|
||||
val entitlementId = item.extra["entitlementId"].orEmpty()
|
||||
|
||||
@@ -163,7 +163,6 @@ object AmazonLibrary {
|
||||
}
|
||||
try {
|
||||
if (cache.isNotEmpty()) publish(app, cache)
|
||||
if (!AmazonNative.ready()) return problem(AmazonNative.ENGINE_MISSING)
|
||||
val syncedAt = prefs.getLong(KEY_SYNCED_AT, 0L)
|
||||
if (!force && cache.isNotEmpty() && System.currentTimeMillis() - syncedAt in 0 until THROTTLE_MS) {
|
||||
backfillPosters(app, cache)
|
||||
@@ -173,6 +172,7 @@ object AmazonLibrary {
|
||||
|
||||
status(R.string.stores_sync_sign_in)
|
||||
val account = AmazonCredentialStore.load(app) ?: return problem(StoresState.notSignedInLine(store))
|
||||
if (!AmazonNative.prepare(app)) return problem(AmazonNative.ENGINE_MISSING)
|
||||
val token = AmazonCredentialStore.getValidAccessToken(app) ?: return problem(StoresState.notSignedInLine(store))
|
||||
status(R.string.stores_sync_game_list)
|
||||
var answer = entitlements(token, account.deviceSerial)
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
package com.droiddeck.launcher.stores.amazon
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import com.droiddeck.launcher.BuildConfig
|
||||
import com.droiddeck.launcher.stores.StoresNative
|
||||
import com.droiddeck.launcher.core.AddonPack
|
||||
import org.json.JSONObject
|
||||
import java.util.concurrent.CountDownLatch
|
||||
import java.util.concurrent.TimeUnit
|
||||
@@ -16,16 +17,32 @@ object AmazonNative {
|
||||
|
||||
@Volatile private var configured = false
|
||||
|
||||
fun ready(): Boolean {
|
||||
if (!StoresNative.ensureLoaded()) return false
|
||||
if (!configured) synchronized(this) {
|
||||
if (!configured) {
|
||||
runCatching { nativeConfigure(BuildConfig.VERSION_NAME) }
|
||||
.onFailure { Log.w(TAG, "configure: ${it.javaClass.simpleName}") }
|
||||
configured = true
|
||||
@Volatile private var loaded = false
|
||||
|
||||
fun ready(): Boolean = loaded && configured
|
||||
|
||||
internal fun prepare(context: Context, running: () -> Boolean = { true },
|
||||
onState: (AddonPack.State) -> Unit = {}): Boolean {
|
||||
if (ready()) return true
|
||||
if (!AddonPack.AMAZON.await(context, running, onState)) return false
|
||||
return loadInstalled(context)
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
internal fun loadInstalled(context: Context): Boolean {
|
||||
if (ready()) return true
|
||||
if (!AddonPack.AMAZON.installed(context)) return false
|
||||
return runCatching {
|
||||
check(AmazonEngine.verify(AddonPack.AMAZON.dir(context))) { "Amazon engine verification failed" }
|
||||
if (!loaded) {
|
||||
System.load(AddonPack.AMAZON.file(context, "libdroiddeckamazon.so").absolutePath)
|
||||
check(nativeEngineAbi() == 1) { "Incompatible Amazon engine" }
|
||||
loaded = true
|
||||
}
|
||||
}
|
||||
return true
|
||||
nativeConfigure(BuildConfig.VERSION_NAME)
|
||||
configured = true
|
||||
true
|
||||
}.onFailure { Log.w(TAG, "Amazon engine could not load", it) }.getOrDefault(false)
|
||||
}
|
||||
|
||||
fun call(op: String, request: JSONObject = JSONObject()): JSONObject? {
|
||||
@@ -109,6 +126,7 @@ object AmazonNative {
|
||||
return holder[0] ?: InstallOutcome(false, false, "The Amazon install ended without a result", null)
|
||||
}
|
||||
|
||||
@JvmStatic private external fun nativeEngineAbi(): Int
|
||||
@JvmStatic private external fun nativeConfigure(appVersion: String)
|
||||
@JvmStatic private external fun nativeCall(op: String, request: String): String?
|
||||
@JvmStatic private external fun nativeFetch(op: String, request: String): ByteArray?
|
||||
|
||||
@@ -3064,4 +3064,7 @@
|
||||
<string name="spatial_saved_views_hint">Saved views can be recalculated. Start a new calibration to capture in the current tracking frame.</string>
|
||||
<string name="spatial_model_checksum_error">Hand model checksum mismatch</string>
|
||||
<string name="spatial_opencv_load_error">OpenCV could not load</string>
|
||||
<string name="stores_engine_downloading">Downloading %1$s support… %2$d%%</string>
|
||||
<string name="stores_engine_preparing">Preparing %1$s support…</string>
|
||||
<string name="stores_engine_failed">%1$s support could not be downloaded or loaded. Try again.</string>
|
||||
</resources>
|
||||
Generated
+1759
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,35 @@
|
||||
[package]
|
||||
name = "droiddeckamazon"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
license = "GPL-3.0-or-later"
|
||||
publish = false
|
||||
|
||||
[lib]
|
||||
name = "droiddeckamazon"
|
||||
crate-type = ["cdylib"]
|
||||
path = "../stores/src/lib.rs"
|
||||
|
||||
[features]
|
||||
default = ["amazon-pack"]
|
||||
gog = []
|
||||
epic = []
|
||||
amazon = []
|
||||
amazon-pack = ["amazon"]
|
||||
|
||||
[dependencies]
|
||||
jni = "0.21"
|
||||
libc = "0.2"
|
||||
lzma-rs = { version = "0.3", default-features = false }
|
||||
flate2 = { version = "1", default-features = false, features = ["rust_backend"] }
|
||||
reqwest = { version = "0.12", default-features = false, features = ["rustls-tls", "gzip"] }
|
||||
tokio = { version = "1", default-features = false, features = ["rt", "net", "time", "sync"] }
|
||||
futures-util = { version = "0.3", default-features = false, features = ["std"] }
|
||||
serde_json = { version = "1", features = ["preserve_order"] }
|
||||
sha1 = "0.10"
|
||||
sha2 = "0.10"
|
||||
|
||||
[profile.release]
|
||||
codegen-units = 1
|
||||
lto = "thin"
|
||||
strip = "symbols"
|
||||
@@ -9,6 +9,13 @@ publish = false
|
||||
name = "droiddeckstores"
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[features]
|
||||
default = ["gog", "epic", "amazon"]
|
||||
gog = []
|
||||
epic = []
|
||||
amazon = []
|
||||
amazon-pack = ["amazon"]
|
||||
|
||||
[dependencies]
|
||||
jni = "0.21"
|
||||
libc = "0.2"
|
||||
|
||||
@@ -24,6 +24,14 @@ fn guarded<T>(fallback: T, f: impl FnOnce() -> T) -> T {
|
||||
catch_unwind(AssertUnwindSafe(f)).unwrap_or(fallback)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "system" fn Java_com_droiddeck_launcher_stores_amazon_AmazonNative_nativeEngineAbi(
|
||||
_env: JNIEnv,
|
||||
_class: JClass,
|
||||
) -> jni::sys::jint {
|
||||
1
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "system" fn Java_com_droiddeck_launcher_stores_amazon_AmazonNative_nativeConfigure(
|
||||
mut env: JNIEnv,
|
||||
|
||||
@@ -2,10 +2,13 @@
|
||||
|
||||
#![allow(clippy::missing_safety_doc, clippy::result_large_err)]
|
||||
|
||||
#[cfg(feature = "amazon")]
|
||||
pub mod amazon;
|
||||
#[cfg(feature = "epic")]
|
||||
pub mod epic;
|
||||
pub mod fetch_core;
|
||||
pub mod fetch_tuning;
|
||||
#[cfg(feature = "gog")]
|
||||
pub mod gog;
|
||||
pub mod md5_small;
|
||||
pub mod priority;
|
||||
|
||||
@@ -6,6 +6,7 @@ use jni::JNIEnv;
|
||||
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
|
||||
#[no_mangle]
|
||||
#[cfg(not(feature = "amazon-pack"))]
|
||||
pub extern "system" fn Java_com_droiddeck_launcher_stores_StoresNative_nativeVersion(
|
||||
env: JNIEnv,
|
||||
_class: JClass,
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.droiddeck.launcher.stores.amazon
|
||||
|
||||
import java.io.File
|
||||
import java.security.MessageDigest
|
||||
import org.json.JSONObject
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.rules.TemporaryFolder
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
class AmazonEngineTest {
|
||||
@get:Rule val temp = TemporaryFolder()
|
||||
|
||||
private fun prepared(): File = temp.newFolder().apply {
|
||||
val library = File(this, "libdroiddeckamazon.so").apply { writeBytes(byteArrayOf(1, 2, 3)) }
|
||||
val hash = MessageDigest.getInstance("SHA-256").digest(library.readBytes())
|
||||
.joinToString("") { "%02x".format(it.toInt() and 0xff) }
|
||||
File(this, "engine.json").writeText(JSONObject().put("schema", 1).put("abi", 1)
|
||||
.put("engine", "amazon").put("target", "aarch64-linux-android")
|
||||
.put("library_sha256", hash).toString())
|
||||
}
|
||||
|
||||
@Test fun installedEngineWorksRepeatedlyWithoutNetwork() {
|
||||
val directory = prepared()
|
||||
assertTrue(AmazonEngine.verify(directory))
|
||||
assertTrue(AmazonEngine.verify(directory))
|
||||
}
|
||||
|
||||
@Test fun changedLibraryIsRejected() {
|
||||
val directory = prepared()
|
||||
File(directory, "libdroiddeckamazon.so").appendText("tampered")
|
||||
assertFalse(AmazonEngine.verify(directory))
|
||||
}
|
||||
|
||||
@Test fun incompatibleInterfaceIsRejected() {
|
||||
val directory = prepared()
|
||||
val manifest = File(directory, "engine.json")
|
||||
manifest.writeText(JSONObject(manifest.readText()).put("abi", 2).toString())
|
||||
assertFalse(AmazonEngine.verify(directory))
|
||||
}
|
||||
|
||||
@Test fun incompleteAndMalformedPacksAreRejected() {
|
||||
val directory = prepared()
|
||||
File(directory, "libdroiddeckamazon.so").delete()
|
||||
assertFalse(AmazonEngine.verify(directory))
|
||||
val malformed = prepared()
|
||||
File(malformed, "engine.json").writeText("not json")
|
||||
assertFalse(AmazonEngine.verify(malformed))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user