feat(ubisoft): Ubisoft Connect store with native sign-in, library, downloads and launch (#2322)

* feat(ubisoft): sign in to Ubisoft Connect and list the owned library

* feat(ubisoft): download games natively through Demux and Ubisoft's CDN

* fix(ubisoft): show the owned games on the store page

* fix(ctl): reach the agent bridge by broadcast when the shell has no content command

* feat(ubisoft): run Ubisoft games in one prefix with Ubisoft Connect installed and the game registered

* feat(ubisoft): launch games through a native stand-in for Ubisoft Connect's local service

* feat(ubisoft): document the launcher pipes, log why owned games have no download, drop the installer after extraction

* feat(ubisoft): Steam-owned Ubisoft games install through Steam, and the launch session carries the game's application id

* feat(ubisoft): answer Denuvo activations through Ubisoft's activation service and log unanswered pipe traffic

* feat(ubisoft): report installed chunks to the game and skip add-ons the account does not own

* fix(ubisoft): the overlay stays off, Ubisoft's install steps pick Windows components, and shared-prefix games keep their own settings

* fix(ubisoft): write the manifest's registry values, and pick the game's exe over Ubisoft's installer stubs

* fix(ubisoft): stream download slices, guard the launch hook, and keep per-game components in the shared prefix

* fix(ubisoft): direct launches in the shared prefix keep their own game id

* ci(ubisoft): build the launcher service after the prepared components are verified

* fix(ubisoft): verify reused files, drop syncs from a signed-out account, and clear activation requests before launch

* fix(ubisoft): fetch again a reused file whose slices cannot be checked
This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-11-10 15:08:37 -05:00
1 parent d8ed02f2cf
commit 3c96df4ab1
49 files changed
+3960 -24

No files matched your search

@@ -61,6 +61,7 @@ class App : Application() {
com.droiddeck.launcher.stores.StoresState.register(com.droiddeck.launcher.stores.xbox.XboxBackend)
com.droiddeck.launcher.sync.Syncthing.init(this)
com.droiddeck.launcher.stores.StoresState.register(com.droiddeck.launcher.stores.battlenet.BattleNetBackend)
com.droiddeck.launcher.stores.StoresState.register(com.droiddeck.launcher.stores.ubisoft.UbisoftBackend)
com.droiddeck.launcher.stores.ea.EaBackend.app = this
com.droiddeck.launcher.stores.StoresState.register(com.droiddeck.launcher.stores.ea.EaBackend)
}
@@ -417,7 +417,7 @@ object AddedGames {
private fun scanGame(context: Context, folder: File, out: MutableList<Game>, single: AddedExes.Entry? = null, reuse: Boolean = false) {
run {
val sidecar = StoreGameSidecar.read(folder)
val sidecar = StoreGameSidecar.read(folder)?.let { com.droiddeck.launcher.stores.StoreLaunch.adopt(folder, it) }
sidecar?.takeIf { it.id.isNotEmpty() }?.let { SessionPrefs.followStoreGame(context, "${it.store.id}:${it.id}", folder.path) { old -> !File(old).exists() } }
val ranking = rank(folder, reuse)
val candidates = ranking.exes
@@ -457,7 +457,11 @@ object AddedGames {
steamId?.toLong() ?: ((appId shl 32) or 0x02000000L), candidates, steamId,
source = sidecar?.store?.id ?: Library.ADDED, storeId = sidecar?.id,
exeUncertain = sidecar == null && chosen == null && adopted == null && ranking.uncertain,
launchOptions = if (sidecar == null) SessionPrefs.addedGameLaunch(context, folder.path) else "",
launchOptions = when {
sidecar == null -> SessionPrefs.addedGameLaunch(context, folder.path)
sidecar.store == Store.UBISOFT -> com.droiddeck.launcher.stores.ubisoft.UbisoftConnect.launchOptions(context)
else -> ""
},
single = single != null))
}
}
@@ -9,7 +9,7 @@ import java.io.File
import java.util.Locale
object WinCompSources {
enum class Origin { STEAM, GOG, EPIC, AMAZON, STEAM_NAME, FOLDER, XBOX }
enum class Origin { STEAM, GOG, EPIC, AMAZON, STEAM_NAME, FOLDER, XBOX, UBISOFT }
data class Finding(val component: String?, val original: String, val origin: Origin, val auto: Boolean, val args: List<String> = emptyList())
@@ -83,6 +83,13 @@ object WinCompSources {
}
}
/** The prerequisite installers a Ubisoft manifest lists, with `$INSTALL_DIR` dropped. */
internal fun ubisoftInstallRuns(setup: String?): List<String> {
val runs = setup?.let { runCatching { org.json.JSONObject(it).optJSONArray("installRuns") }.getOrNull() } ?: return emptyList()
return (0 until runs.length()).mapNotNull { runs.optJSONObject(it)?.optString("exe") }
.map { it.replace('\\', '/').removePrefix("\$INSTALL_DIR").trimStart('/') }.filter { it.isNotEmpty() }
}
fun forGame(context: Context, game: Library.SteamGame): List<Finding> {
val folder = game.gameFiles
val out = ArrayList<Finding>()
@@ -112,6 +119,12 @@ object WinCompSources {
if (parts.isEmpty()) out += Finding(null, original, Origin.AMAZON, false)
parts.forEach { out += Finding(it, original, Origin.AMAZON, true) }
}
store == Store.UBISOFT -> ubisoftInstallRuns(sidecar?.extra?.get(com.droiddeck.launcher.stores.ubisoft.UbisoftLaunch.SETUP)).forEach { exe ->
val parts = installer(exe)
val original = exe.substringAfterLast('/')
if (parts.isEmpty()) out += Finding(null, original, Origin.UBISOFT, false)
parts.forEach { out += Finding(it, original, Origin.UBISOFT, true) }
}
else -> {}
}
if (game.library == Library.ADDED && folder != null) SteamMatch.get(context, folder.path)?.let { match ->
@@ -196,6 +196,7 @@ object SessionFiles {
"usr/local/lib/droiddeck-pipewire.tzst" to "usr/local/lib/droiddeck-pipewire.tzst",
"usr/local/share/licenses/uruntime/LICENSE" to "usr/local/share/licenses/uruntime/LICENSE",
"usr/local/lib/droiddeck-dirt/ws2_32.dll" to "usr/local/lib/droiddeck-dirt/ws2_32.dll",
"usr/local/lib/droiddeck-ubisoft/droiddeck-upc.exe" to "usr/local/lib/droiddeck-ubisoft/droiddeck-upc.exe",
) + mangoapp + xclipse.map { it to it } + maliCompat + msitools.map { it to it } + vr + gpuLoad + fexPreloads).filter { (asset, _) ->
val dir = asset.substringBeforeLast('/')
runCatching { context.assets.list("linuxfs/$dir")?.contains(asset.substringAfterLast('/')) == true }.getOrDefault(false)
@@ -87,7 +87,7 @@ object CloudSaves {
val item = sidecar.extra["catalogItemId"].orEmpty()
if (ns.isEmpty() || item.isEmpty()) "" else EpicApiClient.getCustomAttribute(token, ns, item, "CloudSaveFolder")
}
Store.AMAZON, Store.ITCH, Store.GAMEJOLT, Store.XBOX, Store.BATTLENET, Store.EA -> ""
Store.AMAZON, Store.ITCH, Store.GAMEJOLT, Store.XBOX, Store.BATTLENET, Store.EA, Store.UBISOFT -> ""
} ?: return null
p.edit()
.putString("cloud_template_$id", fetched.ifEmpty { TEMPLATE_NONE })
@@ -148,7 +148,7 @@ object CloudSaves {
val dir = when (store) {
Store.GOG -> CloudSavePaths.gog(template, pfx, folder)
Store.EPIC -> CloudSavePaths.epic(template, pfx, folder, EpicCredentialStore.load(context)?.accountId.orEmpty(), id)
Store.AMAZON, Store.ITCH, Store.GAMEJOLT, Store.XBOX, Store.BATTLENET, Store.EA -> null
Store.AMAZON, Store.ITCH, Store.GAMEJOLT, Store.XBOX, Store.BATTLENET, Store.EA, Store.UBISOFT -> null
}
return dir to (if (dir == null) "unresolved" else "ok")
}
@@ -173,7 +173,7 @@ object CloudSaves {
val account = EpicCredentialStore.load(context)?.accountId.orEmpty()
if (token == null || account.isEmpty()) null else EpicCloud(account, id, token)
}
Store.AMAZON, Store.ITCH, Store.GAMEJOLT, Store.XBOX, Store.BATTLENET, Store.EA -> null
Store.AMAZON, Store.ITCH, Store.GAMEJOLT, Store.XBOX, Store.BATTLENET, Store.EA, Store.UBISOFT -> null
}
private fun gogScopedToken(context: Context, id: String, clientId: String): String? {
@@ -13,12 +13,14 @@ internal object LoginFlows {
Store.EPIC -> epicFlow()
Store.AMAZON -> amazonFlow()
Store.GAMEJOLT -> gameJoltFlow(browserUserAgent)
Store.UBISOFT -> ubisoftFlow(browserUserAgent)
Store.EA -> eaFlow(browserUserAgent)
Store.ITCH, Store.XBOX, Store.BATTLENET -> null
}
@Volatile var epicFlow: () -> StoreLoginActivity.Flow? = { null }
@Volatile var amazonFlow: () -> StoreLoginActivity.Flow? = { null }
@Volatile var ubisoftFlow: (String) -> StoreLoginActivity.Flow? = { null }
@Volatile var eaFlow: (String) -> StoreLoginActivity.Flow? = { null }
@Volatile var gameJoltFlow: (String) -> StoreLoginActivity.Flow? = { null }
@@ -8,6 +8,7 @@ enum class Store(val id: String, val label: String, val shortLabel: String) {
ITCH("itch", "Itch.io", "Itch.io"),
GAMEJOLT("gamejolt", "Game Jolt", "Game Jolt"),
BATTLENET("battlenet", "Battle.net", "Battle.net"),
UBISOFT("ubisoft", "Ubisoft Connect", "Ubisoft"),
EA("ea", "EA", "EA");
val folder: String get() = if (this == ITCH) "itch" else shortLabel
@@ -3,7 +3,8 @@ package com.droiddeck.launcher.stores
import java.io.File
object StoreExe {
private val SKIP = listOf("redist", "unins", "setup", "crash", "report", "helper", "dotnet", "vcredist", "directx", "eac", "easyanticheat", "battleye", "launcher", "updater", "config")
private val SKIP = listOf("redist", "unins", "setup", "crash", "report", "helper", "dotnet", "vcredist", "directx", "eac", "easyanticheat", "battleye", "launcher", "updater", "config", "installer", "autorun")
private val SKIP_DIRS = setOf("installer_resources", "redist", "_commonredist", "support")
fun pick(folder: File, hint: String?, title: String): String {
val hinted = hint?.takeIf { it.isNotBlank() }?.replace('\\', '/')?.trimStart('/')?.let { File(folder, it) }
@@ -28,7 +29,7 @@ object StoreExe {
if (SKIP.none { lower.contains(it) }) out.add(f)
}
}
for (f in files) if (f.isDirectory && !f.name.startsWith(".")) collect(f, out, depth + 1)
for (f in files) if (f.isDirectory && !f.name.startsWith(".") && f.name.lowercase() !in SKIP_DIRS) collect(f, out, depth + 1)
}
private fun depth(root: File, f: File): Int = relative(root, f).count { it == '/' }
@@ -11,12 +11,14 @@ object StoreLaunch {
const val LAUNCHER = ".droiddeck-launch.bat"
const val EPIC_CODE = ".droiddeck-epic-code"
const val EPIC_ARGS = ".droiddeck-epic-args"
const val UBISOFT_LAUNCH = ".droiddeck-ubisoft.json"
private const val UBISOFT_SERVICE = "Z:\\usr\\local\\lib\\droiddeck-ubisoft\\droiddeck-upc.exe"
const val EA_ENV = ".droiddeck-ea-env"
/** A sleeping stand-in that the launch hook installs; older EA SDKs require a running Origin process. */
const val EA_ORIGIN = "C:\\Program Files (x86)\\Origin\\Origin.exe"
fun writeLauncher(folder: File, sidecar: StoreGameSidecar): StoreGameSidecar {
val needs = sidecar.args.isNotEmpty() || sidecar.env.isNotEmpty() || sidecar.store == Store.EPIC || sidecar.store == Store.EA
val needs = sidecar.args.isNotEmpty() || sidecar.env.isNotEmpty() || sidecar.store == Store.EPIC || sidecar.store == Store.EA || sidecar.store == Store.UBISOFT
val file = File(folder, LAUNCHER)
if (!needs) { file.delete(); return sidecar.copy(launcher = null) }
file.writeText(launcherText(sidecar))
@@ -52,6 +54,13 @@ object StoreLaunch {
lines += "del \"%~dp0$EPIC_ARGS\""
lines += "if exist \"%~dp0$EPIC_CODE\" del \"%~dp0$EPIC_CODE\""
lines += "set \"$CLIENT_VAR=\" & \"%~dp0$exe\" %$CLIENT_VAR%"
} else if (sidecar.store == Store.UBISOFT) {
lines += "if not exist \"%~dp0$UBISOFT_LAUNCH\" goto direct"
lines += "if not exist \"$UBISOFT_SERVICE\" goto direct"
lines += "\"$UBISOFT_SERVICE\" \"%~dp0$UBISOFT_LAUNCH\""
lines += "goto :eof"
lines += ":direct"
lines += run
} else if (sidecar.store == Store.EA) {
lines += "if not exist \"%~dp0$EA_ENV\" goto run"
lines += "for /f \"usebackq tokens=1,* delims==\" %%a in (\"%~dp0$EA_ENV\") do set \"%%a=%%b\""
@@ -84,6 +93,17 @@ object StoreLaunch {
return Library.addedGame(context, game)
}
/** Gives a Ubisoft game installed before it needed one the launcher that starts the launcher service. */
fun adopt(folder: File, sidecar: StoreGameSidecar): StoreGameSidecar {
if (sidecar.store != Store.UBISOFT || !sidecar.isInstalled) return sidecar
val wrongExe = !sidecar.exeFile(folder).isFile || sidecar.exe.contains("installer", ignoreCase = true) || sidecar.exe.contains("autorun", ignoreCase = true)
if (sidecar.launcher == LAUNCHER && !wrongExe) return sidecar
val exe = if (wrongExe) StoreExe.pick(folder, null, sidecar.title).ifEmpty { sidecar.exe } else sidecar.exe
if (exe == sidecar.exe && sidecar.launcher == LAUNCHER &&
runCatching { File(folder, LAUNCHER).readText() }.getOrNull() == launcherText(sidecar)) return sidecar
return runCatching { writeLauncher(folder, sidecar.copy(exe = exe)).also { it.write(folder) } }.getOrDefault(sidecar)
}
fun refreshLauncher(folder: File, sidecar: StoreGameSidecar) {
if (sidecar.launcher != LAUNCHER) return
val file = File(folder, LAUNCHER)
@@ -76,6 +76,15 @@ object StoreLaunchRequests {
JSONObject().put("ok", true).put("code", code.written).put("reason", code.reason)
}
}
"ubisoft-launch" -> {
val id = request.optString("id")
if (id.isEmpty()) JSONObject().put("ok", false).put("reason", "no-id")
else {
lastLaunched = Store.UBISOFT to id
runCatching { com.droiddeck.launcher.stores.ubisoft.UbisoftLaunch.prepare(app, id) }
.getOrElse { JSONObject().put("ok", false).put("reason", it.javaClass.simpleName) }
}
}
"cloud-down" -> {
val store = Store.byId(request.optString("store"))
val id = request.optString("id")
@@ -47,6 +47,9 @@ class StoreLoginActivity : ComponentActivity() {
?.takeIf { "Chrome/" in it }
?: "Mozilla/5.0 (Linux; Android ${Build.VERSION.RELEASE}; ${Build.MODEL}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.6533.103 Mobile Safari/537.36"
private const val PAGE_TRIES = 20
private const val PAGE_RETRY_MS = 500L
fun generateState(): String {
val bytes = ByteArray(24)
java.security.SecureRandom().nextBytes(bytes)
@@ -60,6 +63,8 @@ class StoreLoginActivity : ComponentActivity() {
fun isRedirect(url: String): Boolean
fun finish(activity: StoreLoginActivity, view: WebView, url: String): String?
val readsPage: Boolean get() = false
val pageScript: String get() = "(function(){ try { return document.body.innerText; } catch(e){ return ''; } })()"
fun pageReady(result: String): Boolean = true
fun forgetSignIn(done: () -> Unit) = done()
}
@@ -323,6 +328,14 @@ class StoreLoginActivity : ComponentActivity() {
}
}
private fun readPage(view: WebView, f: Flow, left: Int) {
view.evaluateJavascript(f.pageScript) { json ->
val result = json ?: ""
if (!f.pageReady(result) && left > 0) view.postDelayed({ readPage(view, f, left - 1) }, PAGE_RETRY_MS)
else Thread({ settle(f.finish(this, view, result)) }, "store-login").start()
}
}
private fun complete(view: WebView, url: String) {
if (!captured.compareAndSet(false, true)) return
val f = flow ?: return
@@ -332,11 +345,7 @@ class StoreLoginActivity : ComponentActivity() {
titleText.text = getString(R.string.stores_login_finishing)
if (!f.readsPage) view.stopLoading()
}
if (f.readsPage) {
view.evaluateJavascript("(function(){ try { return document.body.innerText; } catch(e){ return ''; } })()") { json ->
Thread({ settle(f.finish(this, view, json ?: "")) }, "store-login").start()
}
} else Thread({ settle(f.finish(this, view, url)) }, "store-login").start()
if (f.readsPage) readPage(view, f, PAGE_TRIES) else Thread({ settle(f.finish(this, view, url)) }, "store-login").start()
}
private fun settle(problem: String?) {
@@ -56,7 +56,7 @@ object StoreSizes {
Store.AMAZON -> AmazonPrefs.get(app).edit().putLong("size_${item.id}", it).apply()
Store.ITCH, Store.GAMEJOLT -> {}
Store.EA -> com.droiddeck.launcher.stores.ea.EaPrefs.get(app).edit().putLong("size_${item.id}", it).apply()
Store.GOG, Store.XBOX, Store.BATTLENET -> {}
Store.GOG, Store.XBOX, Store.BATTLENET, Store.UBISOFT -> {}
}
}
}, pool).exceptionally { error ->
@@ -113,7 +113,7 @@ object StoreSizes {
AmazonManifest.parse(bytes).totalInstallSize
}
Store.EA -> com.droiddeck.launcher.stores.ea.EaSizes.size(context, item)
Store.GOG, Store.XBOX, Store.BATTLENET -> 0L
Store.GOG, Store.XBOX, Store.BATTLENET, Store.UBISOFT -> 0L
Store.ITCH -> com.droiddeck.launcher.stores.itch.ItchLibrary.size(context, item)
Store.GAMEJOLT -> com.droiddeck.launcher.stores.gamejolt.GameJoltBuilds.size(context, item)
} }
@@ -0,0 +1,74 @@
package com.droiddeck.launcher.stores.ubisoft
import android.content.Context
import com.droiddeck.launcher.runtime.LinuxRuntime
import com.droiddeck.launcher.session.SessionState
import com.droiddeck.launcher.stores.StoresState
import org.json.JSONObject
import java.io.File
/**
* Answers Denuvo activations for a running Ubisoft game. droiddeck-upc.exe writes the challenge
* the game's aux DLL sends to `bridge-<product>/denuvo-<id>.req`; the answer goes to `.rsp`.
*/
object UbisoftActivation {
private const val POLL_MS = 250L
private const val IDLE_MS = 60_000L
private val watching = HashMap<Long, Long>()
fun bridge(context: Context, product: Long) = File(LinuxRuntime.rootDir(context), "root/.cache/droiddeck/ubisoft/bridge-$product")
/** Starts answering the product's activations; stale requests are cleared before this returns. */
fun watch(app: Context, product: Long) {
val dir = bridge(app, product)
synchronized(watching) {
val running = watching.containsKey(product)
watching[product] = System.currentTimeMillis()
if (running) return
dir.mkdirs()
dir.listFiles()?.forEach { it.delete() }
}
Thread({
var idleSince = 0L
while (true) {
runCatching { dir.listFiles { f -> f.name.startsWith("denuvo-") && f.name.endsWith(".req") }?.forEach { answer(app, product, it) } }
.onFailure { StoresState.logLine("Ubisoft: activation for $product: ${it.javaClass.simpleName}") }
val now = System.currentTimeMillis()
if (SessionState.running) idleSince = 0L
else if (idleSince == 0L) idleSince = now
else if (now - idleSince > IDLE_MS) synchronized(watching) {
if (now - (watching[product] ?: 0L) > IDLE_MS) { watching.remove(product); return@Thread }
}
runCatching { Thread.sleep(POLL_MS) }
}
}, "ubisoft-activation-$product").start()
}
private fun answer(app: Context, product: Long, request: File) {
val challenge = runCatching { request.readBytes() }.getOrNull() ?: return
request.delete()
val reply = File(request.parentFile, request.name.removeSuffix(".req") + ".rsp")
val token = activate(app, product, challenge)
val part = File(reply.path + ".part")
part.writeBytes(token ?: ByteArray(0))
part.renameTo(reply)
}
private fun activate(app: Context, product: Long, challenge: ByteArray): ByteArray? {
val s = UbisoftAuth.current(app) ?: return null.also { StoresState.logLine("Ubisoft: activation for $product: signed out") }
val answer = UbisoftNative.call("denuvo", JSONObject().put("ticket", s.ticket).put("sessionId", s.sessionId)
.put("productId", product).put("challenge", hex(challenge)))
answer?.optString("error")?.takeIf { it.isNotEmpty() }?.let {
StoresState.logLine("Ubisoft: activation for $product: $it")
return null
}
val token = answer?.optString("token")?.takeIf { it.isNotEmpty() }?.let(::unhex)
StoresState.logLine("Ubisoft: activation for $product: ${token?.size ?: 0} B game token")
return token
}
internal fun hex(b: ByteArray): String = b.joinToString("") { "%02x".format(it) }
internal fun unhex(s: String): ByteArray? =
if (s.length % 2 != 0) null else runCatching { ByteArray(s.length / 2) { s.substring(it * 2, it * 2 + 2).toInt(16).toByte() } }.getOrNull()
}
@@ -0,0 +1,175 @@
package com.droiddeck.launcher.stores.ubisoft
import android.content.Context
import android.util.Log
import com.droiddeck.launcher.stores.Store
import com.droiddeck.launcher.stores.StoreAccounts
import org.json.JSONArray
import org.json.JSONObject
import java.net.HttpURLConnection
import java.net.URL
import java.net.URLEncoder
/** UbiServices sessions: a ticket from Ubisoft's own web sign-in, exchanged for a launcher session that remember-me renews. */
object UbisoftAuth {
private const val TAG = "UbisoftAuth"
const val SESSIONS = "https://public-ubiservices.ubi.com/v3/profiles/sessions"
/** The web app and genome connect.ubisoft.com's sign-in page accepts. */
private const val WEB_APP_ID = "f35adcb5-1911-440c-b1c9-48fdc1701c68"
private const val WEB_GENOME_ID = "5b36b900-65d8-47f3-93c8-86bdaa48ab50"
/** The Ubisoft Connect PC app id; its sessions are accepted by the library GraphQL and Demux. */
const val APP_ID = "f68a4bb5-608a-4ff2-8123-be8ef797e0a6"
private const val REFRESH_MARGIN_MS = 10 * 60 * 1000L
private val lock = Any()
class Session(
val ticket: String,
val sessionId: String,
val profileId: String,
val userId: String,
val name: String,
val expiresAt: Long,
val rememberMe: String,
)
class Reply(val status: Int, val body: String, val headers: Map<String, String>) {
val ok get() = status in 200..299
}
private val loggedIn = "https://connect.ubisoft.com/logged-in.html?appId=$WEB_APP_ID&lang=en-US&genomeId=$WEB_GENOME_ID"
val loginUrl = "https://connect.ubisoft.com/login?appId=$WEB_APP_ID&genomeId=$WEB_GENOME_ID&lang=en-US&nextUrl=" + URLEncoder.encode(loggedIn, "UTF-8")
val SIGN_IN_URLS = listOf("https://connect.ubisoft.com", "https://account.ubisoft.com", "https://ubisoft.com")
fun isLoggedInPage(url: String) = url.startsWith("https://connect.ubisoft.com/logged-in.html")
/** Reads connect.ubisoft.com's stored sign-in; the WebAuth SDK keeps it under keys holding `loginData` and `rememberMe`. */
const val PAGE_SCRIPT = "(function(){try{var o={};for(var i=0;i<localStorage.length;i++){var k=localStorage.key(i);" +
"if(/loginData|rememberMe|lastProfile/i.test(k))o[k]=localStorage.getItem(k);}return JSON.stringify(o);}catch(e){return '';}})()"
internal fun request(url: String, method: String = "GET", authorization: String? = null, sessionId: String? = null, body: String? = null, extra: Map<String, String> = emptyMap()): Reply {
val c = URL(url).openConnection() as HttpURLConnection
c.requestMethod = method
c.connectTimeout = 15_000
c.readTimeout = 25_000
c.setRequestProperty("Ubi-AppId", APP_ID)
c.setRequestProperty("Ubi-RequestedPlatformType", "uplay")
c.setRequestProperty("Content-Type", "application/json")
c.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36")
authorization?.let { c.setRequestProperty("Authorization", it) }
sessionId?.let { c.setRequestProperty("Ubi-SessionId", it) }
for ((k, v) in extra) c.setRequestProperty(k, v)
try {
if (body != null) {
c.doOutput = true
c.outputStream.use { it.write(body.toByteArray()) }
}
val status = c.responseCode
val text = (if (status in 200..299) c.inputStream else c.errorStream)?.use { it.readBytes().toString(Charsets.UTF_8) }.orEmpty()
val headers = c.headerFields.filterKeys { it != null }.mapKeys { it.key.lowercase() }.mapValues { it.value.joinToString(",") }
return Reply(status, text, headers)
} finally {
c.disconnect()
}
}
/** POSTs a session request, replaying it once when UbiServices answers with a challenge. */
private fun session(authorization: String): Reply {
val body = JSONObject().put("rememberMe", true).toString()
val first = request(SESSIONS, "POST", authorization, body = body)
val challenge = first.headers["ubi-challenge"] ?: return first
return request(SESSIONS, "POST", authorization, body = body, extra = mapOf("Ubi-Challenge" to challenge))
}
internal fun parseSession(body: String, rememberFallback: String = ""): Session? {
val o = runCatching { JSONObject(body) }.getOrNull() ?: return null
val ticket = o.optString("ticket").takeIf { it.isNotEmpty() && it != "null" } ?: return null
return Session(
ticket = ticket,
sessionId = o.optString("sessionId"),
profileId = o.optString("profileId"),
userId = o.optString("userId"),
name = o.optString("nameOnPlatform"),
expiresAt = parseTime(o.optString("expiration")),
rememberMe = o.optString("rememberMeTicket").takeIf { it.isNotEmpty() && it != "null" } ?: rememberFallback,
)
}
internal fun parseTime(s: String): Long = runCatching { java.time.OffsetDateTime.parse(s).toInstant().toEpochMilli() }
.recoverCatching { java.time.Instant.parse(s).toEpochMilli() }.getOrDefault(System.currentTimeMillis() + 60 * 60 * 1000L)
/** Values the web sign-in stored, found wherever the SDK nests them. */
internal fun webValues(dump: String): Map<String, String> {
val out = HashMap<String, String>()
fun walk(v: Any?) {
when (v) {
is JSONObject -> v.keys().forEach { k ->
val x = v.opt(k)
if (x is String && x.isNotEmpty() && k !in out) {
val nested = if (x.startsWith("{") || x.startsWith("[")) runCatching { org.json.JSONTokener(x).nextValue() }.getOrNull() else null
if (nested != null) walk(nested) else out[k] = x
} else walk(x)
}
is JSONArray -> for (i in 0 until v.length()) walk(v.opt(i))
}
}
val top = runCatching { org.json.JSONTokener(dump).nextValue() }.getOrNull()
walk(if (top is String) runCatching { JSONObject(top) }.getOrNull() else top)
return out
}
/** Turns the page's stored sign-in into a launcher session; returns null on success or a reason. */
fun completeWebSignIn(context: Context, dump: String): String? {
val web = webValues(dump)
val ticket = web["ticket"]
val remember = web["rememberMeTicket"].orEmpty()
val fresh = when {
ticket != null -> session("ubi_v1 t=$ticket").let { r ->
parseSession(r.body, remember) ?: run { Log.w(TAG, "ticket exchange: ${r.status} ${r.body.take(200)}"); null }
}
else -> null
} ?: remember.takeIf { it.isNotEmpty() }?.let { rm -> parseSession(session("rm_v1 t=$rm").body, rm) }
?: return "no ticket"
save(context, fresh)
return null
}
private fun save(context: Context, s: Session) {
val name = s.name.ifEmpty { profileName(s) }
StoreAccounts.write(context.applicationContext, Store.UBISOFT, JSONObject()
.put("access_token", s.ticket)
.put("session_id", s.sessionId)
.put("profile_id", s.profileId)
.put("user_id", s.userId)
.put("display_name", name)
.put("expires_at", s.expiresAt)
.put("remember_me", s.rememberMe))
}
private fun profileName(s: Session): String = runCatching {
JSONObject(request("https://public-ubiservices.ubi.com/v3/profiles/me", authorization = "ubi_v1 t=${s.ticket}", sessionId = s.sessionId).body).optString("nameOnPlatform")
}.getOrDefault("")
fun load(context: Context): Session? {
val o = StoreAccounts.read(context.applicationContext, Store.UBISOFT) ?: return null
val ticket = o.optString("access_token").takeIf { it.isNotEmpty() } ?: return null
return Session(ticket, o.optString("session_id"), o.optString("profile_id"), o.optString("user_id"), o.optString("display_name"), o.optLong("expires_at"), o.optString("remember_me"))
}
/** A live session, renewed through remember-me shortly before it expires. */
fun current(context: Context): Session? = synchronized(lock) {
val s = load(context) ?: return null
if (s.expiresAt - System.currentTimeMillis() > REFRESH_MARGIN_MS || s.rememberMe.isEmpty()) return s
val renewed = runCatching { session("rm_v1 t=${s.rememberMe}") }.getOrNull()?.let { parseSession(it.body, s.rememberMe) }
if (renewed == null) {
Log.w(TAG, "remember-me renewal failed")
return if (s.expiresAt > System.currentTimeMillis()) s else null
}
save(context, Session(renewed.ticket, renewed.sessionId, renewed.profileId.ifEmpty { s.profileId }, renewed.userId.ifEmpty { s.userId },
renewed.name.ifEmpty { s.name }, renewed.expiresAt, renewed.rememberMe))
load(context)
}
fun signOut(context: Context) = synchronized(lock) { StoreAccounts.clear(context.applicationContext, Store.UBISOFT) }
}
@@ -0,0 +1,246 @@
package com.droiddeck.launcher.stores.ubisoft
import android.content.Context
import android.content.Intent
import android.util.Log
import android.webkit.WebView
import com.droiddeck.launcher.R
import com.droiddeck.launcher.stores.CatalogItem
import com.droiddeck.launcher.stores.InstalledStoreGame
import com.droiddeck.launcher.stores.LoginFlows
import com.droiddeck.launcher.stores.Store
import com.droiddeck.launcher.stores.StoreBackend
import com.droiddeck.launcher.stores.StoreInstallRoot
import com.droiddeck.launcher.stores.download.DownloadEntry
import com.droiddeck.launcher.stores.download.DownloadQueue
import com.droiddeck.launcher.stores.StoreLoginActivity
import com.droiddeck.launcher.stores.StoresState
import com.droiddeck.launcher.stores.WebCookies
import org.json.JSONObject
object UbisoftBackend : StoreBackend {
override val store: Store = Store.UBISOFT
init {
LoginFlows.ubisoftFlow = { ua -> UbisoftLogin(ua) }
}
override fun signIn(context: Context) {
context.startActivity(
Intent(context, StoreLoginActivity::class.java)
.putExtra(StoreLoginActivity.EXTRA_STORE, store.id)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
)
}
override fun signOut(context: Context) {
UbisoftAuth.signOut(context.applicationContext)
UbisoftLibrary.reset()
runCatching { WebCookies.expire(UbisoftAuth.SIGN_IN_URLS, ".ubisoft.com", listOf("/")) {} }
}
override fun syncLibrary(context: Context, force: Boolean) {
val app = context.applicationContext
Thread({ UbisoftLibrary.sync(app, force) }, "ubisoft-sync").start()
}
override fun loadShelves(context: Context, force: Boolean) {}
override fun install(context: Context, item: CatalogItem, root: java.io.File) {
val app = context.applicationContext
item.extra[STEAM_APP]?.takeIf { item.extra["productId"] == null }?.let { steam ->
StoresState.logLine("Ubisoft: ${item.title} is owned on Steam; installing Steam app $steam")
installInSteam(app, steam)
return
}
if (!UbisoftNative.ready()) {
StoresState.logLine(UbisoftNative.ENGINE_MISSING)
StoresState.post { StoresState.problems[store] = UbisoftNative.ENGINE_MISSING }
return
}
val folder = StoreInstallRoot.folderFor(app, store, item.id, item.title, root)
val entry = DownloadEntry(store, item.id, item.title, cover = item.tallImageUrl ?: item.imageUrl,
bytesTotal = item.sizeBytes, location = StoreInstallRoot.labelFor(app, folder))
DownloadQueue.enqueue(app, entry) { UbisoftInstaller(app, item, folder) }
}
override fun uninstall(context: Context, game: InstalledStoreGame) {}
const val STEAM_APP = "steamAppId"
/** Steam asks to install a game it does not have when told to run it. */
private fun installInSteam(app: Context, steamAppId: String) {
if (com.droiddeck.launcher.session.SessionState.running) {
if (!com.droiddeck.launcher.session.SessionService.launchGame(app, steamAppId)) StoresState.logLine("Ubisoft: Steam is busy; install Steam app $steamAppId there")
return
}
app.startActivity(Intent(app, com.droiddeck.launcher.SessionActivity::class.java)
.putExtra(com.droiddeck.launcher.session.SessionService.EXTRA_STEAM_URL, com.droiddeck.launcher.frontend.GameLaunchLink.steamUrl(steamAppId))
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
}
}
internal class UbisoftLogin(override val userAgent: String) : StoreLoginActivity.Flow {
override val startUrl: String get() = UbisoftAuth.loginUrl
override val readsPage: Boolean get() = true
override val pageScript: String get() = UbisoftAuth.PAGE_SCRIPT
override fun pageReady(result: String): Boolean = result.contains("ticket", ignoreCase = true)
override fun isRedirect(url: String): Boolean = UbisoftAuth.isLoggedInPage(url)
override fun forgetSignIn(done: () -> Unit) {
runCatching { WebCookies.expire(UbisoftAuth.SIGN_IN_URLS, ".ubisoft.com", listOf("/")) { done() } }.onFailure { done() }
}
override fun finish(activity: StoreLoginActivity, view: WebView, url: String): String? = try {
UbisoftAuth.completeWebSignIn(activity, url)?.let {
Log.w("UbisoftLogin", "sign-in incomplete: $it")
activity.getString(R.string.stores_login_error_verification)
}
} catch (e: Exception) {
Log.w("UbisoftLogin", "sign-in failed: ${e.javaClass.simpleName}")
activity.getString(R.string.stores_login_error_generic)
}
}
object UbisoftLibrary {
private const val GRAPHQL = "https://public-ubiservices.ubi.com/v1/profiles/me/uplay/graphql"
private const val PAGE = 50
private const val QUERY = "query OwnedGames(\$limit: Int, \$offset: Int) { viewer { games(limit: \$limit, offset: \$offset) { totalCount nodes { id spaceId name coverUrl backgroundUrl bannerUrl releaseDate } } } }"
private val generation = java.util.concurrent.atomic.AtomicInteger()
private val running = java.util.concurrent.atomic.AtomicReference<Int?>(null)
/** Forgets the signed-out account: its sync results are dropped and a new account may sync at once. */
fun reset() {
generation.incrementAndGet()
StoresState.post { StoresState.shelves.remove(Store.UBISOFT) }
}
private fun post(gen: Int, block: () -> Unit) = StoresState.post { if (generation.get() == gen) block() }
internal fun parsePage(body: String): Pair<List<CatalogItem>, Int> {
val o = JSONObject(body)
o.optJSONArray("errors")?.optJSONObject(0)?.let { throw IllegalStateException(it.optString("message")) }
val games = o.optJSONObject("data")?.optJSONObject("viewer")?.optJSONObject("games") ?: return emptyList<CatalogItem>() to 0
val nodes = games.optJSONArray("nodes")
val items = (0 until (nodes?.length() ?: 0)).mapNotNull { nodes!!.optJSONObject(it) }.mapNotNull { n ->
val space = n.optString("spaceId").takeIf { it.isNotEmpty() } ?: return@mapNotNull null
fun s(k: String) = n.optString(k).takeIf { it.isNotEmpty() && it != "null" }
CatalogItem(
Store.UBISOFT, space, n.optString("name").trim(), s("backgroundUrl") ?: s("bannerUrl"),
tallImageUrl = s("coverUrl"), releaseDate = s("releaseDate")?.take(10).orEmpty(), owned = true,
extra = mapOf("spaceId" to space, "gameId" to n.optString("id")),
)
}
return items to games.optInt("totalCount", items.size)
}
/** Base games with a manifest, by UbiServices space; add-ons and bundles share spaces with their game. */
internal fun ownedBySpace(owned: JSONObject?): Map<String, JSONObject> {
val list = owned?.optJSONArray("games") ?: return emptyMap()
val games = (0 until list.length()).mapNotNull { list.optJSONObject(it) }
.filter { it.optString("manifest").isNotEmpty() && it.optInt("productType") == 0 && it.optBoolean("owned", true) }
val out = LinkedHashMap<String, JSONObject>()
for (key in listOf("spaceId")) for (g in games) {
val space = g.optString(key).takeIf { it.isNotEmpty() } ?: continue
out.putIfAbsent(space, g)
}
return out
}
private val STEAM_ID = Regex("""^\s*steam_app_id:\s*['"]?(\d+)""", RegexOption.MULTILINE)
private val THIRD_PARTY = Regex("""^\s*third_party_platform:\s*\r?\n\s+name:\s*['"]?Steam\b""", setOf(RegexOption.MULTILINE, RegexOption.IGNORE_CASE))
/** The Steam app of a product Ubisoft says is owned and installed through Steam. */
internal fun steamApp(configuration: String): String? =
if (THIRD_PARTY.containsMatchIn(configuration)) STEAM_ID.find(configuration)?.groupValues?.get(1) else null
/** Owned games Ubisoft delivers through Steam, by UbiServices space. */
internal fun steamBySpace(owned: JSONObject?): Map<String, String> {
val list = owned?.optJSONArray("games") ?: return emptyMap()
return (0 until list.length()).mapNotNull { list.optJSONObject(it) }
.filter { it.optInt("productType") == 0 && it.optBoolean("owned", true) && it.optString("spaceId").isNotEmpty() }
.mapNotNull { o -> steamApp(o.optString("configuration"))?.let { o.optString("spaceId") to it } }.toMap()
}
private val RELATIVE = Regex("""relative:\s*['"]?([^'"\r\n]+?)['"]?\s*$""", RegexOption.MULTILINE)
private val NAME = Regex("""^\s{1,4}name:\s*['"]?(.+?)['"]?\s*$""", RegexOption.MULTILINE)
/** The product's name from its configuration, following a localisation key such as `l1` to its default text. */
internal fun title(configuration: String): String? {
val raw = NAME.find(configuration)?.groupValues?.get(1)?.trim()?.takeIf { it.isNotEmpty() } ?: return null
if (!Regex("""l\d+""").matches(raw)) return raw
val local = configuration.indexOf("localizations:").takeIf { it >= 0 } ?: return raw
return Regex("""^\s+${raw}:\s*['"]?(.+?)['"]?\s*$""", RegexOption.MULTILINE).find(configuration, local)?.groupValues?.get(1)?.trim() ?: raw
}
/** The first launch path under `start_game` in a product's configuration. */
internal fun executable(configuration: String): String? {
val start = configuration.indexOf("start_game").takeIf { it >= 0 } ?: return null
return RELATIVE.find(configuration, start)?.groupValues?.get(1)?.trim()?.takeIf { it.endsWith(".exe", true) }
}
fun sync(app: Context, force: Boolean) {
val gen = generation.get()
if (running.getAndSet(gen) == gen) return
try {
val s = UbisoftAuth.current(app)
if (s == null) {
post(gen) { StoresState.status.remove(Store.UBISOFT); StoresState.problems[Store.UBISOFT] = app.getString(R.string.stores_sync_sign_in) }
return
}
if (StoresState.library[Store.UBISOFT].isNullOrEmpty()) post(gen) { StoresState.status[Store.UBISOFT] = app.getString(R.string.stores_sync_game_list) }
val all = ArrayList<CatalogItem>()
var offset = 0
while (true) {
val body = JSONObject().put("operationName", "OwnedGames").put("query", QUERY)
.put("variables", JSONObject().put("limit", PAGE).put("offset", offset)).toString()
val reply = UbisoftAuth.request(GRAPHQL, "POST", "Ubi_v1 t=${s.ticket}", s.sessionId, body)
if (reply.status == 401) StoresState.markSignInExpired(Store.UBISOFT)
if (!reply.ok) throw IllegalStateException("library ${reply.status}")
val (items, total) = parsePage(reply.body)
all += items
offset += PAGE
if (items.isEmpty() || offset >= total) break
}
val owned = UbisoftNative.call("owned", JSONObject().put("ticket", s.ticket).put("sessionId", s.sessionId))
owned?.optString("error")?.takeIf { it.isNotEmpty() }?.let { StoresState.logLine("Ubisoft: ownership: $it") }
val bySpace = ownedBySpace(owned)
owned?.takeIf { it.optInt("manifestsAsked") > 0 }?.let {
StoresState.logLine("Ubisoft: latest manifests: ${it.optInt("manifestsFound")} of ${it.optInt("manifestsAsked")} ${it.optString("manifestError")}".trim())
}
fun download(o: JSONObject) = listOfNotNull(
"productId" to o.optLong("productId").toString(),
o.optString("manifest").takeIf { it.isNotEmpty() }?.let { "manifest" to it },
o.optString("appId").takeIf { it.isNotEmpty() }?.let { "appId" to it },
executable(o.optString("configuration"))?.let { "exe" to it },
)
val listed = all.distinctBy { it.id }
val onSteam = steamBySpace(owned)
val matched = listed.map { g -> bySpace[g.id]?.let { g.copy(extra = g.extra + download(it)) } ?: onSteam[g.id]?.let { g.copy(extra = g.extra + (UbisoftBackend.STEAM_APP to it)) } ?: g }
.let { games -> games.filter { g -> g.extra["productId"] == null || games.first { it.extra["productId"] == g.extra["productId"] } === g } }
.let { games -> games.filter { g -> g.extra["productId"] != null || games.none { it !== g && it.title == g.title && it.extra["productId"] != null } } }
val products = matched.mapNotNull { it.extra["productId"] }.toSet()
val unlisted = bySpace.filterKeys { space -> listed.none { it.id == space } }.filterValues { it.optLong("productId").toString() !in products }.mapNotNull { (space, o) ->
val title = title(o.optString("configuration")) ?: return@mapNotNull null
CatalogItem(Store.UBISOFT, space, title, null, owned = true, extra = mapOf("spaceId" to space) + download(o))
}
val games = (matched + unlisted).sortedBy { it.title.lowercase() }
StoresState.logLine("Ubisoft: ${bySpace.size} downloadable products; " +
games.filter { it.extra.containsKey("productId") }.joinToString(", ") { "${it.title}=${it.extra["productId"]}" })
post(gen) {
StoresState.status.remove(Store.UBISOFT)
StoresState.problems.remove(Store.UBISOFT)
StoresState.library[Store.UBISOFT] = games
StoresState.shelves[Store.UBISOFT] = com.droiddeck.launcher.stores.StoreShelves(
trending = games.sortedByDescending { it.extra.containsKey("productId") }, art = games.mapNotNull { it.imageUrl })
}
StoresState.logLine("Ubisoft: ${games.size} owned games; on Steam: " + games.filter { it.extra.containsKey(UbisoftBackend.STEAM_APP) }.joinToString(", ") { "${it.title}=${it.extra[UbisoftBackend.STEAM_APP]}" })
} catch (e: Exception) {
StoresState.logLine("Ubisoft: library sync failed: ${e.javaClass.simpleName} ${e.message.orEmpty().take(200)}")
post(gen) { StoresState.status.remove(Store.UBISOFT); StoresState.problems[Store.UBISOFT] = app.getString(R.string.stores_sync_failed) }
} finally {
running.compareAndSet(gen, null)
}
}
}
@@ -0,0 +1,18 @@
package com.droiddeck.launcher.stores.ubisoft
import android.content.Context
import com.droiddeck.launcher.frontend.AddedGames
import com.droiddeck.launcher.runtime.LinuxRuntime
import java.io.File
/** The one prefix every Ubisoft game runs in, so Ubisoft Connect is installed and signed in once. */
object UbisoftConnect {
const val APP_ID = 2929927072L
fun prefix(context: Context) = File(LinuxRuntime.rootDir(context), "root/.local/share/Steam/steamapps/compatdata/$APP_ID")
fun installed(context: Context) = File(prefix(context), "pfx/drive_c/Program Files (x86)/Ubisoft/Ubisoft Game Launcher/upc.exe").isFile
fun launchOptions(context: Context): String =
AddedGames.guestPath(context, prefix(context))?.let { "STEAM_COMPAT_DATA_PATH=$it %command%" }.orEmpty()
}
@@ -0,0 +1,104 @@
package com.droiddeck.launcher.stores.ubisoft
import android.content.Context
import com.droiddeck.launcher.R
import com.droiddeck.launcher.stores.CatalogItem
import com.droiddeck.launcher.stores.Store
import com.droiddeck.launcher.stores.StoreExe
import com.droiddeck.launcher.stores.StoreGameSidecar
import com.droiddeck.launcher.stores.StoreInstalls
import com.droiddeck.launcher.stores.StoreLanguage
import com.droiddeck.launcher.stores.amazon.AmazonNative
import com.droiddeck.launcher.stores.download.DownloadQueue
import com.droiddeck.launcher.stores.download.DownloadStage
import org.json.JSONObject
import java.io.File
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicLong
internal class UbisoftInstaller(
private val app: Context,
private val item: CatalogItem,
private val folder: File,
) : DownloadQueue.DownloadJob {
private val cancelled = AtomicBoolean(false)
override fun run(handle: DownloadQueue.JobHandle): String? {
handle.stage(DownloadStage.MANIFEST, app.getString(R.string.stores_sync_sign_in))
val s = UbisoftAuth.current(app) ?: throw IllegalStateException("Not signed in to Ubisoft")
val product = item.extra["productId"]?.toLongOrNull() ?: throw IllegalStateException("Ubisoft lists no download for ${item.title} on this account")
val cover = item.tallImageUrl ?: item.imageUrl
StoreInstalls.begin(folder, Store.UBISOFT, item.id, item.title, cover, item.imageUrl)
if (cancelled.get()) return null
val locale = StoreLanguage.localeFor(null, app)
val request = JSONObject()
.put("ticket", s.ticket)
.put("sessionId", s.sessionId)
.put("productId", product)
.put("manifest", item.extra["manifest"].orEmpty())
.put("folder", folder.absolutePath)
.put("language", "${locale.language}-${locale.country}")
val shown = AtomicLong(-1)
val downloading = AtomicBoolean(false)
val listener = object : AmazonNative.InstallListener {
override fun onStatus(text: String) { handle.stage(DownloadStage.MANIFEST, text) }
override fun onCheck(doneBytes: Long, totalBytes: Long, files: Int, filesTotal: Int) {
handle.stageProgress(doneBytes, totalBytes, files, filesTotal, bytes = true)
}
override fun onSizes(pendingBytes: Long, totalBytes: Long) { handle.diskSize(totalBytes) }
override fun onProgress(done: Long, total: Long, files: Int, filesTotal: Int) {
if (downloading.compareAndSet(false, true)) handle.stage(DownloadStage.DOWNLOAD)
while (true) {
val before = shown.get()
if (done < before) return
if (shown.compareAndSet(before, done)) break
}
handle.progress(done, total, "Downloading ($files/$filesTotal files)")
}
override fun onLog(line: String) { handle.log(line) }
override fun beforeReplace(): Boolean = true
override fun onComplete(success: Boolean, cancelled: Boolean, error: String, result: String) = Unit
}
val outcome = UbisoftNative.install(request, listener, cancelled)
if (outcome.cancelled || cancelled.get() && !outcome.success) return null
if (!outcome.success) throw IllegalStateException(outcome.error.ifEmpty { "The Ubisoft install failed" })
handle.stage(DownloadStage.INSTALL, app.getString(R.string.stores_registering))
val configured = resolve(folder, item.extra["exe"].orEmpty())
val exe = configured ?: StoreExe.pick(folder, null, item.title).ifEmpty { "game.exe" }
handle.log("ubisoft: exe=$exe")
val sidecar = StoreGameSidecar(
Store.UBISOFT, item.id, item.title, exe = exe,
installVersion = item.extra["manifest"].orEmpty(), installedAt = System.currentTimeMillis(),
cover = cover, hero = item.imageUrl, cloud = false,
extra = item.extra.filterKeys { it in KEPT } + listOfNotNull(
outcome.result?.optJSONArray("chunks")?.let { UbisoftLaunch.CHUNKS to it.toString() },
outcome.result?.optJSONObject("setup")?.let { UbisoftLaunch.SETUP to it.toString() },
),
)
StoreInstalls.complete(app, folder, sidecar, onStep = { done, steps -> handle.stageProgress(done.toLong(), steps.toLong()) })
return folder.path
}
override fun cancel(deleteFiles: Boolean) {
cancelled.set(true)
if (deleteFiles) StoreInstalls.discard(app, folder, emptyList())
}
internal companion object {
val KEPT = setOf("productId", "spaceId", "manifest", "appId")
/** A configured exe the way Windows finds it: any letter case, else the same file name anywhere in the install. */
fun resolve(folder: File, configured: String): String? {
val rel = configured.replace('\\', '/').trimStart('/').takeIf { it.isNotEmpty() } ?: return null
var at = folder
for (part in rel.split('/')) {
at = at.listFiles()?.firstOrNull { it.name.equals(part, ignoreCase = true) } ?: return byName(folder, rel.substringAfterLast('/'))
}
return if (at.isFile) at.relativeTo(folder).invariantSeparatorsPath else byName(folder, rel.substringAfterLast('/'))
}
private fun byName(folder: File, name: String): String? =
folder.walkTopDown().maxDepth(5).firstOrNull { it.isFile && it.name.equals(name, ignoreCase = true) }?.relativeTo(folder)?.invariantSeparatorsPath
}
}
@@ -0,0 +1,86 @@
package com.droiddeck.launcher.stores.ubisoft
import android.content.Context
import com.droiddeck.launcher.stores.StoreInstallRoot
import com.droiddeck.launcher.stores.Store
import com.droiddeck.launcher.stores.StoreGameSidecar
import com.droiddeck.launcher.stores.StoresState
import org.json.JSONArray
import org.json.JSONObject
/** What the launcher service hands a starting Ubisoft game: the account, its products and a fresh game ticket. */
object UbisoftLaunch {
private const val ENTITLEMENTS = "https://public-ubiservices.ubi.com/v1/profiles/me/global/ubiconnect/entitlement/api/entitlements"
private const val ME = "https://public-ubiservices.ubi.com/v3/users/me"
fun prepare(app: Context, id: String): JSONObject {
val folder = StoreInstallRoot.gameFolders(app).firstOrNull { f ->
StoreGameSidecar.read(f)?.let { it.store == Store.UBISOFT && it.id == id } == true
} ?: return failure("not-installed")
val sidecar = StoreGameSidecar.read(folder) ?: return failure("not-installed")
val product = sidecar.extra["productId"]?.toLongOrNull() ?: return failure("no-product")
val s = UbisoftAuth.current(app) ?: return failure("signed-out")
val native = UbisoftNative.call("launch", JSONObject().put("ticket", s.ticket).put("sessionId", s.sessionId).put("productId", product))
?: return failure("engine-missing")
native.optString("error").takeIf { it.isNotEmpty() }?.let {
StoresState.logLine("Ubisoft: launch ticket for $product: $it")
return failure("ticket: $it")
}
val auth = "Ubi_v1 t=${s.ticket}"
val me = runCatching { UbisoftAuth.request(ME, "GET", auth, s.sessionId) }.getOrNull()?.takeIf { it.ok }
?.let { runCatching { JSONObject(it.body) }.getOrNull() }
val account = JSONObject()
.put("userId", s.userId)
.put("username", me?.optString("username")?.takeIf { it.isNotEmpty() } ?: s.name)
.put("nameOnPlatform", me?.optString("nameOnPlatform")?.takeIf { it.isNotEmpty() } ?: s.name)
.put("email", me?.optString("email").orEmpty())
.put("country", me?.optString("country")?.takeIf { it.isNotEmpty() } ?: "US")
val appId = runCatching { UbisoftAuth.request(ENTITLEMENTS, "GET", auth, s.sessionId) }.getOrNull()?.takeIf { it.ok }
?.let { applicationId(it.body, product) } ?: sidecar.extra["appId"].orEmpty()
StoresState.logLine("Ubisoft: launch ticket for $product ready (${native.optJSONArray("owned")?.length() ?: 0} products, appId ${if (appId.isEmpty()) "unknown" else "found"})")
val language = com.droiddeck.launcher.stores.StoreLanguage.localeFor(null, app).let { "${it.language}-${it.country}" }
val cached = sidecar.extra[CHUNKS]?.let { runCatching { JSONArray(it) }.getOrNull() }
var setup = sidecar.extra[SETUP]?.let { runCatching { JSONObject(it) }.getOrNull() }
val chunks = if (cached != null && setup != null) cached else
UbisoftNative.call("chunks", JSONObject().put("ticket", s.ticket).put("sessionId", s.sessionId).put("productId", product)
.put("manifest", sidecar.extra["manifest"] ?: sidecar.installVersion).put("folder", folder.path).put("language", language))
?.let { r ->
r.optString("error").takeIf { it.isNotEmpty() }?.let { StoresState.logLine("Ubisoft: manifest for $product: $it") }
setup = r.optJSONObject("setup")
r.optJSONArray("chunks")
}?.also { c ->
runCatching {
StoreGameSidecar.read(folder)?.let { sc ->
sc.copy(extra = sc.extra + (CHUNKS to c.toString()) + listOfNotNull(setup?.let { SETUP to it.toString() })).write(folder)
}
com.droiddeck.launcher.stores.StoreLaunch.installedGame(app, Store.UBISOFT, id)
?.let { com.droiddeck.launcher.frontend.AutoComponents.refreshLater(app, it) }
}
} ?: cached
setup?.let { StoresState.logLine("Ubisoft: setup for $product: ${it.toString().take(1500)}") }
UbisoftActivation.watch(app, product)
return JSONObject().put("ok", true)
.put("productId", product)
.put("ticket", native.optString("ticket"))
.put("owned", native.optJSONArray("owned") ?: JSONArray())
.put("products", native.optJSONArray("products") ?: JSONArray())
.put("account", account)
.put("appId", appId)
.put("language", language)
.put("chunks", chunks ?: JSONArray())
.put("registry", setup?.optJSONArray("registry") ?: JSONArray())
}
const val CHUNKS = "chunks"
const val SETUP = "setup"
/** The entitlement's `applicationId` for the product; the list sits under one of a few keys depending on the API version. */
internal fun applicationId(body: String, product: Long): String? {
val root = runCatching { JSONObject(body) }.getOrNull() ?: return null
val list = sequenceOf("items", "entitlements", "products", "games", "results").firstNotNullOfOrNull { root.optJSONArray(it) } ?: return null
return (0 until list.length()).mapNotNull { list.optJSONObject(it) }
.firstOrNull { it.optLong("productId") == product }?.optString("applicationId")?.takeIf { it.isNotEmpty() }
}
private fun failure(reason: String) = JSONObject().put("ok", false).put("reason", reason)
}
@@ -0,0 +1,57 @@
package com.droiddeck.launcher.stores.ubisoft
import android.util.Log
import com.droiddeck.launcher.stores.StoresNative
import com.droiddeck.launcher.stores.amazon.AmazonNative
import org.json.JSONObject
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicBoolean
object UbisoftNative {
private const val TAG = "UbisoftNative"
const val ENGINE_MISSING = "Ubisoft: the store engine is not available"
fun ready(): Boolean = StoresNative.ensureLoaded()
fun call(op: String, request: JSONObject): JSONObject? {
if (!ready()) return null
val text = try { nativeCall(op, request.toString()) } catch (t: Throwable) {
Log.w(TAG, "$op: ${t.javaClass.simpleName}")
null
} ?: return null
return runCatching { JSONObject(text) }.getOrNull()
}
fun install(request: JSONObject, events: AmazonNative.InstallListener, cancelled: AtomicBoolean): AmazonNative.InstallOutcome {
if (!ready()) return AmazonNative.InstallOutcome(false, false, ENGINE_MISSING, null)
val done = CountDownLatch(1)
val holder = arrayOfNulls<AmazonNative.InstallOutcome>(1)
val bridge = object : AmazonNative.InstallListener by events {
override fun onComplete(success: Boolean, cancelled: Boolean, error: String, result: String) {
holder[0] = AmazonNative.InstallOutcome(success, cancelled, error, result.takeIf { it.isNotEmpty() }?.let { runCatching { JSONObject(it) }.getOrNull() })
done.countDown()
}
}
val handle = try { nativeInstall(request.toString(), bridge) } catch (t: Throwable) {
return AmazonNative.InstallOutcome(false, false, "The Ubisoft install could not start: ${t.javaClass.simpleName}", null)
}
if (handle == 0L) return holder[0] ?: AmazonNative.InstallOutcome(false, false, "The Ubisoft install could not start", null)
try {
var forwarded = false
while (true) {
val finished = try { done.await(250, TimeUnit.MILLISECONDS) } catch (e: InterruptedException) { cancelled.set(true); false }
if (finished) break
if (!forwarded && cancelled.get()) { nativeCancel(handle); forwarded = true }
}
} finally {
nativeRelease(handle)
}
return holder[0] ?: AmazonNative.InstallOutcome(false, false, "The Ubisoft install ended without a result", null)
}
@JvmStatic private external fun nativeCall(op: String, request: String): String?
@JvmStatic private external fun nativeInstall(request: String, listener: AmazonNative.InstallListener): Long
@JvmStatic private external fun nativeCancel(handle: Long)
@JvmStatic private external fun nativeRelease(handle: Long)
}
@@ -33,6 +33,7 @@ internal fun sourceColours(source: String): SourceColours = when (source) {
Store.ITCH.id -> SourceColours(Color(0xFF3A1A1E), Color(0xFFFFB3B3), Color(0xFFFF5C5C))
Store.GAMEJOLT.id -> SourceColours(Color(0xFF232E00), Color(0xFFE3FF66), Color(0xFFCCFF00))
Store.BATTLENET.id -> SourceColours(Color(0xFF0A2440), Color(0xFF9DD1FF), Color(0xFF148EFF))
Store.UBISOFT.id -> SourceColours(Color(0xFF0B1E44), Color(0xFFA8C6FF), Color(0xFF2E7DFF))
Store.EA.id -> SourceColours(Color(0xFF3A0E12), Color(0xFFFF9A9A), Color(0xFFFF4747))
Library.SOURCE_STEAM -> SourceColours(Color(0xFF16293D), Color(0xFF7CC4FF), Color(0xFF1A9FFF))
FLATHUB -> SourceColours(Color(0xFF0B2E2B), Color(0xFF9FE3DD), FlathubTeal)
@@ -49,6 +50,7 @@ internal fun sourceLabel(source: String): String = when (source) {
Store.ITCH.id -> Store.ITCH.label
Store.GAMEJOLT.id -> Store.GAMEJOLT.label
Store.BATTLENET.id -> Store.BATTLENET.label
Store.UBISOFT.id -> Store.UBISOFT.shortLabel
Store.EA.id -> Store.EA.label
else -> stringResource(R.string.source_added)
}
@@ -1157,6 +1157,7 @@ internal fun storePalette(base: Palette, store: Store?): Palette {
Store.ITCH -> Color(0xFFFF5C5C) to Color(0xFFD93B3B)
Store.GAMEJOLT -> Color(0xFFCCFF00) to Color(0xFFA3CC00)
Store.BATTLENET -> Color(0xFF148EFF) to Color(0xFF0B6BD3)
Store.UBISOFT -> Color(0xFF2E7DFF) to Color(0xFF1A5BD6)
Store.EA -> Color(0xFFFF4747) to Color(0xFFE03030)
null -> return base
}
@@ -56,7 +56,7 @@ import java.io.File
private fun installable(name: String, all: Map<String, WinComponents.Component>): String = WinComponents.installable(name, all)
@Composable
private fun originName(origin: String): String = if (origin == WinCompSources.Origin.XBOX.name) "Xbox" else stringResource(
private fun originName(origin: String): String = if (origin == WinCompSources.Origin.XBOX.name) "Xbox" else if (origin == WinCompSources.Origin.UBISOFT.name) "Ubisoft Connect" else stringResource(
when (origin) {
WinCompSources.Origin.GOG.name -> R.string.wincomp_src_gog
WinCompSources.Origin.EPIC.name -> R.string.wincomp_src_epic
@@ -71,6 +71,7 @@ private fun autoReason(r: WinComponents.AutoReason): String = when (r.kind) {
WinCompSources.Origin.GOG.name -> stringResource(R.string.wincomp_auto_gog)
WinCompSources.Origin.EPIC.name -> stringResource(R.string.wincomp_auto_epic)
WinCompSources.Origin.AMAZON.name -> stringResource(R.string.wincomp_auto_amazon)
WinCompSources.Origin.UBISOFT.name -> "From Ubisoft's install steps for this game"
WinCompSources.Origin.XBOX.name -> "Xbox games install it with the game (${r.args.getOrElse(0) { "VC14" }})"
else -> ""
}
+18
View File
@@ -195,10 +195,13 @@ dependencies = [
"libc",
"lzma-rs",
"reqwest",
"ruzstd",
"serde_json",
"sha1",
"sha2",
"tokio",
"tokio-rustls",
"webpki-roots",
]
[[package]]
@@ -897,6 +900,15 @@ version = "1.0.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d"
[[package]]
name = "ruzstd"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7c1c839d570d835527c9a5e4db7cb2198683a988cb9d7293fc8674e6bd58fc8"
dependencies = [
"twox-hash",
]
[[package]]
name = "ryu"
version = "1.0.23"
@@ -1243,6 +1255,12 @@ version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b"
[[package]]
name = "twox-hash"
version = "2.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86a801b3cea342a06d468c8710662aa29e5e05e4f5c0d62f00bbb7f2ad7941c2"
[[package]]
name = "typenum"
version = "1.20.0"
+4 -1
View File
@@ -15,11 +15,14 @@ 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"] }
tokio = { version = "1", default-features = false, features = ["rt", "net", "time", "sync", "io-util"] }
futures-util = { version = "0.3", default-features = false, features = ["std"] }
serde_json = { version = "1", features = ["preserve_order"] }
sha1 = "0.10"
sha2 = "0.10"
tokio-rustls = { version = "0.26", default-features = false, features = ["ring", "tls12"] }
webpki-roots = "1"
ruzstd = "0.8"
[build-dependencies]
cc = "1"
+1
View File
@@ -3,6 +3,7 @@
#![allow(clippy::missing_safety_doc, clippy::result_large_err)]
pub mod amazon;
pub mod ubisoft;
pub mod ea;
pub mod epic;
pub mod fetch_core;
@@ -0,0 +1,154 @@
use std::sync::Arc;
use std::time::Duration;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpStream;
use tokio_rustls::client::TlsStream;
use tokio_rustls::rustls::{self, pki_types::ServerName, ClientConfig, RootCertStore};
use tokio_rustls::TlsConnector;
use super::pb::{self, Msg};
const HOST: &str = "dmx.upc.ubisoft.com";
const PORT: u16 = 443;
const CLIENT_ID: &str = "uplay_pc";
/// The Connect build number Demux expects first on a socket; older ones get ClientOutdatedPush and a closed socket.
pub const CLIENT_VERSION: u32 = 13247;
const READ_TIMEOUT: Duration = Duration::from_secs(20);
const MAX_FRAME: usize = 64 << 20;
pub struct Demux {
tls: TlsStream<TcpStream>,
next_id: u32,
}
impl Demux {
pub async fn connect(ticket: &str) -> Result<Self, String> {
let mut roots = RootCertStore::empty();
roots.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
let provider = Arc::new(rustls::crypto::ring::default_provider());
let config = ClientConfig::builder_with_provider(provider)
.with_safe_default_protocol_versions()
.map_err(|e| format!("TLS setup: {e}"))?
.with_root_certificates(roots)
.with_no_client_auth();
let tcp = tokio::time::timeout(READ_TIMEOUT, TcpStream::connect((HOST, PORT)))
.await
.map_err(|_| "Ubisoft's download service did not answer".to_string())?
.map_err(|e| format!("Could not reach Ubisoft's download service: {e}"))?;
let name = ServerName::try_from(HOST).map_err(|e| e.to_string())?.to_owned();
let tls = TlsConnector::from(Arc::new(config))
.connect(name, tcp)
.await
.map_err(|e| format!("TLS with Ubisoft's download service failed: {e}"))?;
let mut d = Demux { tls, next_id: 1 };
d.send(Msg::new().msg(2, Msg::new().msg(4, Msg::new().uint(1, CLIENT_VERSION as u64)))).await?;
let id = d.id();
let auth = Msg::new().uint(1, id as u64).msg(
2,
Msg::new()
.msg(1, Msg::new().text(3, ticket))
.text(2, CLIENT_ID)
.flag(4, false),
);
d.send(Msg::new().msg(1, auth)).await?;
let rsp = d.response(id).await?;
let a = pb::fields(pb::sub(&pb::fields(&rsp)?, 2).ok_or("Ubisoft did not answer the sign-in")?)?;
if pb::num(&a, 1) != Some(1) {
let expired = pb::num(&a, 2) == Some(1);
return Err(if expired { "The Ubisoft sign-in expired".into() } else { "Ubisoft refused the sign-in".into() });
}
Ok(d)
}
fn id(&mut self) -> u32 {
let id = self.next_id;
self.next_id += 1;
id
}
async fn send(&mut self, up: Msg) -> Result<(), String> {
let len = (up.0.len() as u32).to_be_bytes();
self.tls.write_all(&len).await.map_err(|e| e.to_string())?;
self.tls.write_all(&up.0).await.map_err(|e| e.to_string())?;
self.tls.flush().await.map_err(|e| e.to_string())
}
async fn frame(&mut self) -> Result<Vec<u8>, String> {
let read = async {
let mut len = [0u8; 4];
self.tls.read_exact(&mut len).await.map_err(|e| format!("Ubisoft closed the connection: {e}"))?;
let n = u32::from_be_bytes(len) as usize;
if n > MAX_FRAME {
return Err(format!("Ubisoft sent a {n}-byte frame"));
}
let mut body = vec![0u8; n];
self.tls.read_exact(&mut body).await.map_err(|e| e.to_string())?;
Ok(body)
};
tokio::time::timeout(READ_TIMEOUT, read).await.map_err(|_| "Ubisoft's download service stopped answering".to_string())?
}
fn check_push(down: &[(u32, pb::Value<'_>)]) -> Result<(), String> {
if let Some(push) = pb::sub(down, 2) {
let p = pb::fields(push)?;
if pb::first(&p, 5).is_some() {
return Err("Ubisoft says this client version is outdated".into());
}
}
Ok(())
}
async fn response(&mut self, id: u32) -> Result<Vec<u8>, String> {
for _ in 0..64 {
let frame = self.frame().await?;
let down = pb::fields(&frame)?;
Self::check_push(&down)?;
if let Some(rsp) = pb::sub(&down, 1) {
if pb::num(&pb::fields(rsp)?, 1) == Some(id as u64) {
return Ok(rsp.to_vec());
}
}
}
Err("Ubisoft never answered the request".into())
}
pub async fn open(&mut self, service: &str) -> Result<u32, String> {
let id = self.id();
self.send(Msg::new().msg(1, Msg::new().uint(1, id as u64).msg(3, Msg::new().text(2, service)))).await?;
let rsp = self.response(id).await?;
let o = pb::fields(pb::sub(&pb::fields(&rsp)?, 3).ok_or("no connection answer")?)?;
if pb::num(&o, 2) != Some(1) {
return Err(format!("Ubisoft refused the {service} connection"));
}
pb::num(&o, 1).map(|c| c as u32).ok_or_else(|| "no connection id".into())
}
/// One service request on a connection; the payload is length-prefixed inside the DataMessage both ways.
pub async fn call(&mut self, connection: u32, payload: Msg) -> Result<Vec<u8>, String> {
let mut framed = (payload.0.len() as u32).to_be_bytes().to_vec();
framed.extend_from_slice(&payload.0);
self.send(Msg::new().msg(2, Msg::new().msg(1, Msg::new().uint(1, connection as u64).bytes(2, &framed)))).await?;
for _ in 0..64 {
let frame = self.frame().await?;
let down = pb::fields(&frame)?;
Self::check_push(&down)?;
let Some(push) = pb::sub(&down, 2) else { continue };
let p = pb::fields(push)?;
if let Some(closed) = pb::sub(&p, 2) {
let c = pb::fields(closed)?;
if pb::num(&c, 1) == Some(connection as u64) {
return Err(format!("Ubisoft closed the service connection (code {})", pb::num(&c, 2).unwrap_or(0)));
}
}
let Some(data) = pb::sub(&p, 1) else { continue };
let d = pb::fields(data)?;
if pb::num(&d, 1) != Some(connection as u64) {
continue;
}
let body = pb::sub(&d, 2).unwrap_or_default();
return Ok(if body.len() >= 4 { body[4..].to_vec() } else { body.to_vec() });
}
Err("Ubisoft never answered the service request".into())
}
}
+269
View File
@@ -0,0 +1,269 @@
use std::panic::{catch_unwind, AssertUnwindSafe};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use jni::objects::{GlobalRef, JClass, JObject, JString, JValue};
use jni::sys::{jlong, jstring, JNI_FALSE, JNI_TRUE};
use jni::{JNIEnv, JavaVM};
use serde_json::json;
use super::install;
use crate::amazon::install::{Events, Stop};
fn read_string(env: &mut JNIEnv, value: &JString) -> String {
if value.is_null() {
return String::new();
}
env.get_string(value).map(|s| s.to_string_lossy().into_owned()).unwrap_or_default()
}
fn new_string(env: &mut JNIEnv, text: &str) -> jstring {
env.new_string(text).map(|s| s.into_raw()).unwrap_or(std::ptr::null_mut())
}
fn owned(ticket: &str, session: &str) -> Result<serde_json::Value, String> {
let rt = tokio::runtime::Builder::new_current_thread().enable_all().build().map_err(|e| e.to_string())?;
rt.block_on(async {
let mut d = super::demux::Demux::connect(ticket).await?;
let (mut ownership, mut games) = super::service::Ownership::open(&mut d, ticket, session).await?;
let missing: Vec<u32> = games.iter().filter(|g| g.owned && g.product_type == 0 && g.manifest.is_empty()).map(|g| g.product_id).collect();
let (found, manifest_error) = match ownership.latest_manifests(&mut d, &missing).await {
Ok(m) => (m, String::new()),
Err(e) => (Vec::new(), e),
};
for (product, manifest) in &found {
if let Some(g) = games.iter_mut().find(|g| g.product_id == *product) {
g.manifest = manifest.clone();
}
}
Ok(json!({ "games": games.iter().map(|g| g.to_json()).collect::<Vec<_>>(),
"manifestsAsked": missing.len(), "manifestsFound": found.len(), "manifestError": manifest_error }))
})
}
fn launch(ticket: &str, session: &str, product: u32) -> Result<serde_json::Value, String> {
let rt = tokio::runtime::Builder::new_current_thread().enable_all().build().map_err(|e| e.to_string())?;
rt.block_on(async {
let mut d = super::demux::Demux::connect(ticket).await?;
let (mut ownership, games) = super::service::Ownership::open(&mut d, ticket, session).await?;
if !games.iter().any(|g| g.product_id == product && g.owned) {
return Err("Ubisoft says this account does not own the game".into());
}
let pc_ticket = ownership.pc_ticket(&mut d, product, ticket, session).await?;
let owned: Vec<u32> = games.iter().filter(|g| g.in_session()).map(|g| g.product_id).collect();
let products: Vec<_> = games.iter().filter(|g| g.in_session()).map(|g| json!({ "id": g.product_id, "type": g.product_type })).collect();
Ok(json!({ "ticket": pc_ticket, "owned": owned, "products": products }))
})
}
fn unhex(s: &str) -> Result<Vec<u8>, String> {
if s.len() % 2 != 0 {
return Err("odd hex length".into());
}
(0..s.len()).step_by(2).map(|i| u8::from_str_radix(&s[i..i + 2], 16).map_err(|e| e.to_string())).collect()
}
fn denuvo(ticket: &str, session: &str, product: u32, challenge: &str) -> Result<serde_json::Value, String> {
let challenge = unhex(challenge).map_err(|e| format!("challenge: {e}"))?;
let rt = tokio::runtime::Builder::new_current_thread().enable_all().build().map_err(|e| e.to_string())?;
rt.block_on(async {
let mut d = super::demux::Demux::connect(ticket).await?;
let (mut ownership, _) = super::service::Ownership::open(&mut d, ticket, session).await?;
let token = ownership.token(&mut d, product).await?;
let game = super::service::denuvo_token(&mut d, &token, &challenge).await?;
Ok(json!({ "token": game.iter().map(|b| format!("{b:02x}")).collect::<String>() }))
})
}
fn call(op: &str, request: &str) -> String {
let parsed: serde_json::Value = serde_json::from_str(request).unwrap_or(serde_json::Value::Null);
let s = |k: &str| parsed.get(k).and_then(|v| v.as_str()).unwrap_or("").to_string();
let answer = match op {
"owned" => owned(&s("ticket"), &s("sessionId")).unwrap_or_else(|e| json!({ "error": e })),
"denuvo" => denuvo(&s("ticket"), &s("sessionId"), parsed.get("productId").and_then(|v| v.as_u64()).unwrap_or(0) as u32, &s("challenge"))
.unwrap_or_else(|e| json!({ "error": e })),
"chunks" => install::Request::from_json(&parsed)
.and_then(|r| install::chunks(&r))
.map(|m| json!({ "chunks": install::chunks_json(&m.chunks), "setup": install::setup_json(&m) }))
.unwrap_or_else(|e| json!({ "error": e })),
"launch" => launch(&s("ticket"), &s("sessionId"), parsed.get("productId").and_then(|v| v.as_u64()).unwrap_or(0) as u32)
.unwrap_or_else(|e| json!({ "error": e })),
_ => json!({ "error": format!("unknown operation {op}") }),
};
answer.to_string()
}
#[no_mangle]
pub extern "system" fn Java_com_droiddeck_launcher_stores_ubisoft_UbisoftNative_nativeCall(
mut env: JNIEnv,
_class: JClass,
op: JString,
request: JString,
) -> jstring {
let op = read_string(&mut env, &op);
let request = read_string(&mut env, &request);
match catch_unwind(AssertUnwindSafe(|| call(&op, &request))) {
Ok(text) => new_string(&mut env, &text),
Err(_) => std::ptr::null_mut(),
}
}
struct Listener {
vm: JavaVM,
target: GlobalRef,
}
impl Listener {
fn with_env<R>(&self, fallback: R, f: impl FnOnce(&mut JNIEnv) -> R) -> R {
let Ok(mut env) = self.vm.attach_current_thread_as_daemon() else {
return fallback;
};
let out = f(&mut env);
if env.exception_check().unwrap_or(false) {
let _ = env.exception_clear();
}
out
}
fn text(&self, method: &str, text: &str) {
self.with_env((), |env| {
let Ok(s) = env.new_string(text) else { return };
let s = JObject::from(s);
let _ = env.call_method(self.target.as_obj(), method, "(Ljava/lang/String;)V", &[JValue::Object(&s)]);
let _ = env.delete_local_ref(s);
});
}
fn counts(&self, method: &str, a: u64, b: u64, c: u32, d: u32) {
self.with_env((), |env| {
let _ = env.call_method(
self.target.as_obj(),
method,
"(JJII)V",
&[
JValue::Long(a.min(i64::MAX as u64) as i64),
JValue::Long(b.min(i64::MAX as u64) as i64),
JValue::Int(c.min(i32::MAX as u32) as i32),
JValue::Int(d.min(i32::MAX as u32) as i32),
],
);
});
}
fn complete(&self, success: bool, cancelled: bool, error: &str, result: &str) {
self.with_env((), |env| {
let (Ok(e), Ok(r)) = (env.new_string(error), env.new_string(result)) else { return };
let (e, r) = (JObject::from(e), JObject::from(r));
let _ = env.call_method(
self.target.as_obj(),
"onComplete",
"(ZZLjava/lang/String;Ljava/lang/String;)V",
&[
JValue::Bool(if success { JNI_TRUE } else { JNI_FALSE }),
JValue::Bool(if cancelled { JNI_TRUE } else { JNI_FALSE }),
JValue::Object(&e),
JValue::Object(&r),
],
);
let _ = env.delete_local_ref(e);
let _ = env.delete_local_ref(r);
});
}
}
impl Events for Listener {
fn log(&self, line: &str) {
self.text("onLog", &crate::amazon::net::scrub_urls(line));
}
fn status(&self, text: &str) {
self.text("onStatus", text);
}
fn checking(&self, done_bytes: u64, total_bytes: u64, files: u32, files_total: u32) {
self.counts("onCheck", done_bytes, total_bytes, files, files_total);
}
fn sizes(&self, pending_bytes: u64, total_bytes: u64) {
self.with_env((), |env| {
let _ = env.call_method(
self.target.as_obj(),
"onSizes",
"(JJ)V",
&[
JValue::Long(pending_bytes.min(i64::MAX as u64) as i64),
JValue::Long(total_bytes.min(i64::MAX as u64) as i64),
],
);
});
}
fn progress(&self, done: u64, total: u64, files: u32, files_total: u32) {
self.counts("onProgress", done, total, files, files_total);
}
fn before_replace(&self) -> bool {
self.with_env(false, |env| match env.call_method(self.target.as_obj(), "beforeReplace", "()Z", &[]) {
Ok(v) => v.z().unwrap_or(false),
Err(_) => false,
})
}
}
#[no_mangle]
pub extern "system" fn Java_com_droiddeck_launcher_stores_ubisoft_UbisoftNative_nativeInstall(
mut env: JNIEnv,
_class: JClass,
request: JString,
listener: JObject,
) -> jlong {
if listener.is_null() {
return 0;
}
let Ok(vm) = env.get_java_vm() else { return 0 };
let Ok(target) = env.new_global_ref(&listener) else { return 0 };
let listener = Listener { vm, target };
let text = read_string(&mut env, &request);
let parsed = serde_json::from_str::<serde_json::Value>(&text)
.map_err(|_| "The install request could not be read".to_string())
.and_then(|v| install::Request::from_json(&v));
let request = match parsed {
Ok(r) => r,
Err(e) => {
listener.complete(false, false, &e, "");
return 0;
}
};
let cancel = Arc::new(AtomicBool::new(false));
let worker_cancel = Arc::clone(&cancel);
let spawned = std::thread::Builder::new().name("ubisoft-install".into()).spawn(move || {
crate::priority::background();
let result = catch_unwind(AssertUnwindSafe(|| install::run(&request, &worker_cancel, &listener)))
.unwrap_or_else(|_| Err(Stop::Failed("The Ubisoft install stopped on an internal error".into())));
match result {
Ok(done) => listener.complete(true, false, "", &done.to_json().to_string()),
Err(Stop::Cancelled) => listener.complete(false, true, "cancelled", ""),
Err(Stop::Failed(e)) => listener.complete(false, false, &e, ""),
}
});
if spawned.is_err() {
return 0;
}
Box::into_raw(Box::new(cancel)) as jlong
}
#[no_mangle]
pub extern "system" fn Java_com_droiddeck_launcher_stores_ubisoft_UbisoftNative_nativeCancel(_env: JNIEnv, _class: JClass, handle: jlong) {
let ptr = handle as *const Arc<AtomicBool>;
if let Some(cancel) = unsafe { ptr.as_ref() } {
cancel.store(true, Ordering::SeqCst);
}
}
#[no_mangle]
pub extern "system" fn Java_com_droiddeck_launcher_stores_ubisoft_UbisoftNative_nativeRelease(_env: JNIEnv, _class: JClass, handle: jlong) {
let ptr = handle as *mut Arc<AtomicBool>;
if !ptr.is_null() {
drop(unsafe { Box::from_raw(ptr) });
}
}
@@ -0,0 +1,408 @@
use std::collections::HashMap;
use std::io::{Read, Seek, SeekFrom, Write};
use std::path::{Component, Path, PathBuf};
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::Mutex;
use std::time::{Duration, Instant};
use futures_util::stream::{self, StreamExt};
use serde_json::{json, Value as Json};
use sha1::{Digest, Sha1};
use super::demux::Demux;
use super::manifest::{self, File};
use super::service::{Downloads, Ownership};
use crate::amazon::install::{Events, Stop};
const SIGN_BATCH: usize = 200;
const WINDOW: usize = 16;
const TRIES: usize = 4;
pub struct Request {
pub ticket: String,
pub session_id: String,
pub product_id: u32,
pub manifest: String,
pub folder: PathBuf,
pub language: String,
}
impl Request {
pub fn from_json(v: &Json) -> Result<Self, String> {
let s = |k: &str| v.get(k).and_then(|x| x.as_str()).unwrap_or("").to_string();
let product_id = v.get("productId").and_then(|x| x.as_u64()).ok_or("no product id")? as u32;
let folder = PathBuf::from(s("folder"));
if s("ticket").is_empty() || folder.as_os_str().is_empty() {
return Err("The install request is incomplete".into());
}
Ok(Request { ticket: s("ticket"), session_id: s("sessionId"), product_id, manifest: s("manifest"), folder, language: s("language") })
}
}
pub struct Done {
pub files: usize,
pub bytes: u64,
pub chunks: Vec<manifest::Chunk>,
pub setup: Json,
}
pub fn chunks_json(chunks: &[manifest::Chunk]) -> Json {
Json::Array(chunks.iter().map(|c| json!({ "id": c.id, "tags": c.tags })).collect())
}
pub fn setup_json(m: &manifest::Manifest) -> Json {
json!({
"installRuns": m.install_runs.iter().map(|(exe, args, what)| json!({ "exe": exe, "arguments": args, "description": what })).collect::<Vec<_>>(),
"registry": m.registry.iter().map(|(key, values)| json!({ "key": key, "values": values.iter().map(|(n, v)| json!({ "name": n, "value": v })).collect::<Vec<_>>() })).collect::<Vec<_>>(),
})
}
impl Done {
pub fn to_json(&self) -> Json {
json!({ "files": self.files, "bytes": self.bytes, "chunks": chunks_json(&self.chunks), "setup": self.setup.clone() })
}
}
pub fn safe_path(root: &Path, name: &str) -> Result<PathBuf, String> {
let rel = Path::new(name.trim_start_matches('/'));
if rel.components().any(|c| !matches!(c, Component::Normal(_))) {
return Err(format!("The manifest names an unsafe path: {name}"));
}
Ok(root.join(rel))
}
fn sha1(b: &[u8]) -> Vec<u8> {
Sha1::digest(b).to_vec()
}
/// The slice as served and as unpacked must both match the manifest, so a bad proxy or a truncated body never lands in the game.
pub fn checked(method: u64, raw: &[u8], s: &manifest::Slice) -> Result<Vec<u8>, String> {
if let Some(n) = s.download_size {
if raw.len() as u32 != n {
return Err(format!("a slice is {} bytes, the manifest says {n}", raw.len()));
}
}
if !s.download_sha1.is_empty() && sha1(raw) != s.download_sha1 {
return Err("a slice failed its download checksum".into());
}
let out = manifest::decompress(method, raw)?;
if s.size != 0 && out.len() as u32 != s.size {
return Err(format!("a slice unpacked to {} bytes, the manifest says {}", out.len(), s.size));
}
if s.content_sha1.len() == 20 && sha1(&out) != s.content_sha1 {
return Err("a slice failed its content checksum".into());
}
Ok(out)
}
/// Whether a file of the right size already holds every slice the manifest lists for it; a file that cannot be checked is fetched again.
fn already_installed(path: &Path, f: &File) -> bool {
if f.slices.iter().any(|s| s.content_sha1.len() != 20 || s.size == 0) {
return false;
}
let Ok(mut file) = std::fs::File::open(path) else { return false };
let mut buf = Vec::new();
f.slices.iter().all(|s| {
buf.resize(s.size as usize, 0);
file.seek(SeekFrom::Start(s.offset)).is_ok() && file.read_exact(&mut buf).is_ok() && sha1(&buf) == s.content_sha1
})
}
fn slice_hash(s: &manifest::Slice) -> String {
manifest::hex_upper(if s.download_sha1.is_empty() { &s.content_sha1 } else { &s.download_sha1 })
}
struct Job {
file: usize,
slice: usize,
path: String,
}
async fn fetch(client: &reqwest::Client, urls: &[String]) -> Result<Vec<u8>, String> {
let mut last = String::from("no URL");
for attempt in 0..TRIES {
for u in urls {
match client.get(u).send().await {
Ok(r) if r.status().is_success() => match r.bytes().await {
Ok(b) => return Ok(b.to_vec()),
Err(e) => last = e.to_string(),
},
Ok(r) => last = format!("HTTP {}", r.status().as_u16()),
Err(e) => last = e.to_string(),
}
}
tokio::time::sleep(Duration::from_millis(500 << attempt)).await;
}
Err(format!("a slice could not be downloaded ({last})"))
}
/// A Demux login holding a download_service connection for one product. Ubisoft drops idle sockets
/// while slices download, so signing reconnects and retries when the connection is gone.
struct Session {
d: Demux,
dl: Downloads,
}
const RECONNECTS: usize = 4;
impl Session {
/// Opens a session, retrying a few times: Demux drops a connection now and then right after another closes.
async fn open(req: &Request) -> Result<(Self, Vec<super::service::Owned>), String> {
let mut last = String::new();
for attempt in 0..3u32 {
if attempt > 0 {
tokio::time::sleep(Duration::from_secs(2 << attempt)).await;
}
match Self::open_once(req).await {
Ok(s) => return Ok(s),
Err(e) => last = e,
}
}
Err(last)
}
async fn open_once(req: &Request) -> Result<(Self, Vec<super::service::Owned>), String> {
let mut d = Demux::connect(&req.ticket).await?;
let (mut own, owned) = Ownership::open(&mut d, &req.ticket, &req.session_id).await?;
let token = own.token(&mut d, req.product_id).await?;
let dl = Downloads::open(&mut d, &token).await?;
Ok((Session { d, dl }, owned))
}
async fn sign(&mut self, req: &Request, paths: &[String], events: &dyn Events) -> Result<Vec<Vec<String>>, String> {
let mut last = String::new();
for attempt in 0..=RECONNECTS {
if attempt > 0 {
events.log(&format!("ubisoft: reconnecting after: {last}"));
tokio::time::sleep(Duration::from_millis(500 << attempt.min(4))).await;
match Session::open(req).await {
Ok((fresh, _)) => *self = fresh,
Err(e) => {
last = e;
continue;
}
}
}
match self.dl.sign(&mut self.d, req.product_id, paths).await {
Ok(urls) => return Ok(urls),
Err(e) if e.contains("does not own") => return Err(e),
Err(e) => last = e,
}
}
Err(last)
}
}
/// The manifest of an install, without downloading the game.
pub fn chunks(req: &Request) -> Result<manifest::Manifest, String> {
let rt = tokio::runtime::Builder::new_current_thread().enable_all().build().map_err(|e| e.to_string())?;
rt.block_on(async {
let (mut sess, owned) = Session::open(req).await?;
let manifest_id = if req.manifest.is_empty() {
owned.iter().find(|o| o.product_id == req.product_id).map(|o| o.manifest.clone()).unwrap_or_default()
} else {
req.manifest.clone()
};
if manifest_id.is_empty() {
return Err("Ubisoft lists no download for this game".to_string());
}
let client = reqwest::Client::builder().timeout(Duration::from_secs(120)).build().map_err(|e| e.to_string())?;
struct Quiet;
impl Events for Quiet {
fn log(&self, _: &str) {}
fn status(&self, _: &str) {}
fn checking(&self, _: u64, _: u64, _: u32, _: u32) {}
fn sizes(&self, _: u64, _: u64) {}
fn progress(&self, _: u64, _: u64, _: u32, _: u32) {}
fn before_replace(&self) -> bool { false }
}
let urls = sess.sign(req, &[format!("manifests/{manifest_id}.manifest")], &Quiet).await?;
let raw = fetch(&client, urls.first().map(|v| v.as_slice()).unwrap_or_default()).await?;
let language = if req.language.is_empty() { "en-US" } else { req.language.as_str() };
let mut owned_ids: Vec<u32> = owned.iter().filter(|o| o.owned).map(|o| o.product_id).collect();
owned_ids.push(req.product_id);
Ok(manifest::parse_owned(&manifest::decode(&raw)?, language, &owned_ids)?)
})
}
pub fn run(req: &Request, cancel: &AtomicBool, events: &dyn Events) -> Result<Done, Stop> {
let rt = tokio::runtime::Builder::new_current_thread().enable_all().build().map_err(|e| Stop::Failed(e.to_string()))?;
rt.block_on(install(req, cancel, events))
}
async fn install(req: &Request, cancel: &AtomicBool, events: &dyn Events) -> Result<Done, Stop> {
let fail = Stop::Failed;
events.status("Connecting to Ubisoft");
let (mut sess, owned) = Session::open(req).await.map_err(fail)?;
let manifest_id = if req.manifest.is_empty() {
owned.iter().find(|o| o.product_id == req.product_id).map(|o| o.manifest.clone()).unwrap_or_default()
} else {
req.manifest.clone()
};
if manifest_id.is_empty() {
return Err(Stop::Failed("Ubisoft lists no download for this game".into()));
}
let client = reqwest::Client::builder().timeout(Duration::from_secs(120)).build().map_err(|e| Stop::Failed(e.to_string()))?;
events.status("Reading the game's file list");
let urls = sess.sign(req, &[format!("manifests/{manifest_id}.manifest")], events).await.map_err(fail)?;
let raw = fetch(&client, urls.first().map(|v| v.as_slice()).unwrap_or_default()).await.map_err(fail)?;
let decoded = manifest::decode(&raw).map_err(fail)?;
let language = if req.language.is_empty() { "en-US" } else { req.language.as_str() };
let mut owned_ids: Vec<u32> = owned.iter().filter(|o| o.owned).map(|o| o.product_id).collect();
owned_ids.push(req.product_id);
let m = manifest::parse_owned(&decoded, language, &owned_ids).map_err(fail)?;
events.log(&format!("ubisoft: manifest {manifest_id} files={} chunks={} skipped={} compression={}", m.files.len(), m.chunks.len(), m.skipped.len(), m.compression));
for name in &m.skipped {
if let Ok(path) = safe_path(&req.folder, name) {
if path.is_file() && std::fs::remove_file(&path).is_ok() {
events.log(&format!("ubisoft: removed {name}, which this install leaves out"));
}
}
}
std::fs::create_dir_all(&req.folder).map_err(|e| Stop::Failed(format!("Could not create the game folder: {e}")))?;
let total: u64 = m.files.iter().map(|f| f.size).sum();
let files_total = m.files.len() as u32;
let mut pending: Vec<usize> = Vec::new();
let mut done_bytes = 0u64;
let mut done_files = 0u32;
for (i, f) in m.files.iter().enumerate() {
let path = safe_path(&req.folder, &f.name).map_err(fail)?;
if f.dir {
std::fs::create_dir_all(&path).map_err(|e| Stop::Failed(e.to_string()))?;
done_files += 1;
continue;
}
if std::fs::metadata(&path).map(|md| md.len() == f.size).unwrap_or(false) && already_installed(&path, f) {
done_bytes += f.size;
done_files += 1;
continue;
}
pending.push(i);
}
let pending_bytes: u64 = pending.iter().map(|&i| m.files[i].size).sum();
events.sizes(pending_bytes, total);
events.log(&format!("ubisoft: {} of {} files to fetch, {pending_bytes} bytes", pending.len(), m.files.len()));
let bucketed = match pending.iter().flat_map(|&i| m.files[i].slices.first()).next() {
None => true,
Some(s) => {
let h = slice_hash(s);
let probe = sess.sign(req, &[manifest::slice_path(true, &h), manifest::slice_path(false, &h)], events).await.map_err(fail)?;
let mut v3 = true;
for (layout, list) in [true, false].into_iter().zip(probe.iter()) {
if let Some(u) = list.first() {
if client.get(u).send().await.map(|r| r.status().is_success()).unwrap_or(false) {
v3 = layout;
break;
}
}
}
v3
}
};
let mut parts: HashMap<usize, Mutex<std::fs::File>> = HashMap::new();
let mut remaining: HashMap<usize, usize> = HashMap::new();
let mut jobs = Vec::new();
for &i in &pending {
let f: &File = &m.files[i];
let path = safe_path(&req.folder, &f.name).map_err(fail)?;
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).map_err(|e| Stop::Failed(e.to_string()))?;
}
let part = path.with_extension(format!("{}part", path.extension().map(|e| format!("{}.", e.to_string_lossy())).unwrap_or_default()));
let file = std::fs::File::create(&part).map_err(|e| Stop::Failed(format!("Could not write {}: {e}", f.name)))?;
file.set_len(f.size).map_err(|e| Stop::Failed(e.to_string()))?;
parts.insert(i, Mutex::new(file));
remaining.insert(i, f.slices.len());
for (si, s) in f.slices.iter().enumerate() {
jobs.push(Job { file: i, slice: si, path: manifest::slice_path(bucketed, &slice_hash(s)) });
}
}
let progress = AtomicU64::new(done_bytes);
let mut finished_files = done_files;
let mut last_report = Instant::now();
events.status("Downloading");
for batch in jobs.chunks(SIGN_BATCH) {
if cancel.load(Ordering::SeqCst) {
return Err(Stop::Cancelled);
}
let paths: Vec<String> = batch.iter().map(|j| j.path.clone()).collect();
let signed = sess.sign(req, &paths, events).await.map_err(fail)?;
let mut slices = stream::iter(batch.iter().zip(signed.into_iter()))
.map(|(job, urls)| {
let client = &client;
let m = &m;
async move {
let s = &m.files[job.file].slices[job.slice];
let raw = fetch(client, &urls).await?;
let out = checked(m.compression, &raw, s).map_err(|e| format!("{}: {e}", m.files[job.file].name))?;
Ok::<_, String>((job.file, job.slice, out))
}
})
.buffer_unordered(WINDOW);
while let Some(r) = slices.next().await {
if cancel.load(Ordering::SeqCst) {
return Err(Stop::Cancelled);
}
let (fi, si, data) = r.map_err(fail)?;
let s = &m.files[fi].slices[si];
{
let mut f = parts[&fi].lock().unwrap();
f.seek(SeekFrom::Start(s.offset)).and_then(|_| f.write_all(&data)).map_err(|e| Stop::Failed(format!("Could not write {}: {e}", m.files[fi].name)))?;
}
progress.fetch_add(data.len() as u64, Ordering::Relaxed);
let left = remaining.get_mut(&fi).unwrap();
*left -= 1;
if *left == 0 {
let f = parts.remove(&fi).unwrap().into_inner().unwrap();
f.sync_all().map_err(|e| Stop::Failed(e.to_string()))?;
drop(f);
let path = safe_path(&req.folder, &m.files[fi].name).map_err(fail)?;
let part = path.with_extension(format!("{}part", path.extension().map(|e| format!("{}.", e.to_string_lossy())).unwrap_or_default()));
std::fs::rename(&part, &path).map_err(|e| Stop::Failed(format!("Could not finish {}: {e}", m.files[fi].name)))?;
finished_files += 1;
}
if last_report.elapsed() > Duration::from_millis(250) {
events.progress(progress.load(Ordering::Relaxed), total, finished_files, files_total);
last_report = Instant::now();
}
}
}
for i in pending.iter().copied().filter(|i| m.files[*i].slices.is_empty()) {
let path = safe_path(&req.folder, &m.files[i].name).map_err(fail)?;
parts.remove(&i);
let part = path.with_extension(format!("{}part", path.extension().map(|e| format!("{}.", e.to_string_lossy())).unwrap_or_default()));
std::fs::rename(&part, &path).map_err(|e| Stop::Failed(e.to_string()))?;
finished_files += 1;
}
events.progress(total, total, finished_files, files_total);
Ok(Done { files: m.files.len(), bytes: total, chunks: m.chunks.clone(), setup: setup_json(&m) })
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn refuses_paths_that_leave_the_folder() {
let root = Path::new("/games/x");
assert!(safe_path(root, "../evil").is_err());
assert!(safe_path(root, "a/../../evil").is_err());
assert_eq!(safe_path(root, "/bin/game.exe").unwrap(), root.join("bin/game.exe"));
}
#[test]
fn checks_a_slice_as_served_and_unpacked() {
let data = b"slice".to_vec();
let ok = manifest::Slice { size: 5, download_size: Some(5), download_sha1: sha1(&data), content_sha1: Vec::new(), offset: 0 };
assert_eq!(checked(0, &data, &ok).unwrap(), data);
let bad = manifest::Slice { download_sha1: vec![0; 20], ..ok.clone() };
assert!(checked(0, &data, &bad).is_err());
let short = manifest::Slice { download_size: Some(4), ..ok };
assert!(checked(0, &data, &short).is_err());
}
}
@@ -0,0 +1,230 @@
use std::io::Read;
use super::pb;
/// Bytes Ubisoft prepends to a downloaded `.manifest` before the zlib stream.
pub const HEADER: usize = 356;
pub const DEFLATE: u64 = 1;
pub const ZSTD: u64 = 3;
#[derive(Clone, Debug, PartialEq)]
pub struct Slice {
pub size: u32,
pub download_size: Option<u32>,
pub download_sha1: Vec<u8>,
pub content_sha1: Vec<u8>,
pub offset: u64,
}
#[derive(Clone, Debug, PartialEq)]
pub struct File {
pub name: String,
pub size: u64,
pub dir: bool,
pub slices: Vec<Slice>,
}
/// A chunk the install keeps, as the game's launcher session reports it.
#[derive(Clone, Debug, PartialEq)]
pub struct Chunk {
pub id: u32,
pub tags: String,
}
#[derive(Debug)]
pub struct Manifest {
pub compression: u64,
pub files: Vec<File>,
pub chunks: Vec<Chunk>,
/// Files of chunks left out: other languages, and add-ons the account does not own.
pub skipped: Vec<String>,
/// Prerequisite installers Connect runs after an install: exe, arguments, description.
pub install_runs: Vec<(String, String, String)>,
/// Registry keys Connect writes for the game, with their string values as (name, value).
pub registry: Vec<(String, Vec<(String, String)>)>,
}
fn file(b: &[u8]) -> Result<File, String> {
let f = pb::fields(b)?;
let hashes = pb::subs(&f, 4);
let mut offset = 0u64;
let mut slices = Vec::new();
for (i, s) in pb::subs(&f, 7).into_iter().enumerate() {
let s = pb::fields(s)?;
let size = pb::num(&s, 1).unwrap_or(0) as u32;
let at = pb::num(&s, 4).unwrap_or(offset);
slices.push(Slice {
size,
download_size: pb::num(&s, 2).map(|v| v as u32),
download_sha1: pb::sub(&s, 3).unwrap_or_default().to_vec(),
content_sha1: hashes.get(i).map(|h| h.to_vec()).unwrap_or_default(),
offset: at,
});
offset = at + size as u64;
}
if slices.is_empty() {
for h in &hashes {
slices.push(Slice { size: 0, download_size: None, download_sha1: Vec::new(), content_sha1: h.to_vec(), offset: 0 });
}
}
Ok(File {
name: pb::string(&f, 1).unwrap_or_default().replace('\\', "/"),
size: pb::num(&f, 2).unwrap_or(0),
dir: pb::num(&f, 3) == Some(1),
slices,
})
}
/// Files of the chunks to install: those for every language plus the language asked for, of the
/// game itself or an optional add-on in `owned` (every add-on when `owned` is empty).
pub fn parse(decoded: &[u8], language: &str) -> Result<Manifest, String> {
parse_owned(decoded, language, &[])
}
pub fn parse_owned(decoded: &[u8], language: &str, owned: &[u32]) -> Result<Manifest, String> {
let m = pb::fields(decoded)?;
let wanted = language.to_ascii_lowercase();
let prefix = wanted.split(['-', '_']).next().unwrap_or("").to_string();
let mut files = Vec::new();
let mut chunks = Vec::new();
let mut skipped = Vec::new();
for c in pb::subs(&m, 4) {
let c = pb::fields(c)?;
let lang = pb::string(&c, 5).unwrap_or_default().to_ascii_lowercase();
let mut products: Vec<u32> = c.iter().filter_map(|(n, v)| match v { pb::Value::Varint(x) if *n == 8 => Some(*x as u32), _ => None }).collect();
products.extend(pb::num(&c, 4).map(|v| v as u32));
let in_language = lang.is_empty() || lang == wanted || (!prefix.is_empty() && lang.split(['-', '_']).next() == Some(prefix.as_str()));
let optional = pb::num(&c, 2) == Some(1);
let owned_product = !optional || owned.is_empty() || products.is_empty() || products.iter().any(|p| owned.contains(p));
if !(in_language && owned_product) {
for f in pb::subs(&c, 3) {
skipped.push(file(f)?.name);
}
continue;
}
chunks.push(Chunk { id: pb::num(&c, 1).unwrap_or(0) as u32, tags: pb::string(&c, 7).unwrap_or_default() });
for f in pb::subs(&c, 3) {
files.push(file(f)?);
}
}
let kept: std::collections::HashSet<&str> = files.iter().map(|f| f.name.as_str()).collect();
skipped.retain(|n| !kept.contains(n.as_str()));
let mut install_runs = Vec::new();
for r in pb::subs(&m, 2) {
let r = pb::fields(r)?;
install_runs.push((pb::string(&r, 1).unwrap_or_default(), pb::string(&r, 3).unwrap_or_default(), pb::string(&r, 4).unwrap_or_default()));
}
let mut registry = Vec::new();
for r in pb::subs(&m, 5) {
let r = pb::fields(r)?;
let mut values = Vec::new();
for e in pb::subs(&r, 3) {
let e = pb::fields(e)?;
values.push((pb::string(&e, 1).unwrap_or_default(), pb::string(&e, 2).unwrap_or_default()));
}
registry.push((pb::string(&r, 1).unwrap_or_default(), values));
}
Ok(Manifest { compression: pb::num(&m, 21).unwrap_or(0), files, chunks, skipped, install_runs, registry })
}
pub fn decode(downloaded: &[u8]) -> Result<Vec<u8>, String> {
if downloaded.len() <= HEADER {
return Err(format!("the manifest is only {} bytes", downloaded.len()));
}
let mut out = Vec::new();
flate2::read::ZlibDecoder::new(&downloaded[HEADER..]).read_to_end(&mut out).map_err(|e| format!("the manifest did not inflate: {e}"))?;
Ok(out)
}
pub fn hex_upper(b: &[u8]) -> String {
b.iter().map(|x| format!("{x:02X}")).collect()
}
/// The CDN bucket of a slice: the first byte of its hash with nibbles swapped, mapped onto base32.
pub fn bucket(hash_upper: &str) -> char {
const BASE32: &[u8; 32] = b"0123456789abcdefghijklmnopqrstuv";
let b = hash_upper.as_bytes();
let swapped = format!("{}{}", *b.get(1).unwrap_or(&b'0') as char, *b.first().unwrap_or(&b'0') as char);
let v = u8::from_str_radix(&swapped, 16).unwrap_or(0);
BASE32[(v / 16) as usize + if v % 2 == 0 { 0 } else { 16 }] as char
}
pub fn slice_path(bucketed: bool, hash_upper: &str) -> String {
if bucketed { format!("slices_v3/{}/{}", bucket(hash_upper), hash_upper) } else { format!("slices/{hash_upper}") }
}
pub fn decompress(method: u64, data: &[u8]) -> Result<Vec<u8>, String> {
match method {
ZSTD => {
let mut out = Vec::new();
ruzstd::decoding::StreamingDecoder::new(data).map_err(|e| format!("zstd: {e}"))?.read_to_end(&mut out).map_err(|e| format!("zstd: {e}"))?;
Ok(out)
}
DEFLATE => {
let zlib = data.len() >= 2 && data[0] == 0x78;
let inflate = |z: bool| {
let mut out = Vec::new();
let r = if z { flate2::read::ZlibDecoder::new(data).read_to_end(&mut out) } else { flate2::read::DeflateDecoder::new(data).read_to_end(&mut out) };
r.map(|_| out)
};
inflate(zlib).or_else(|_| inflate(!zlib)).map_err(|e| format!("deflate: {e}"))
}
_ => Ok(data.to_vec()),
}
}
#[cfg(test)]
mod tests {
use super::super::pb::Msg;
use super::*;
#[test]
fn buckets_follow_the_swapped_first_byte() {
assert_eq!(bucket("10ABCDEF"), 'g');
assert_eq!(bucket("01ABCDEF"), '1');
assert_eq!(slice_path(true, "01AB"), "slices_v3/1/01AB");
assert_eq!(slice_path(false, "01AB"), "slices/01AB");
}
#[test]
fn keeps_shared_and_wanted_language_chunks() {
let f = |name: &str| Msg::new().text(1, name).uint(2, 10).flag(3, false)
.bytes(4, &[0xAA]).msg(7, Msg::new().uint(1, 10).uint(2, 4).bytes(3, &[0xBB]));
let m = Msg::new()
.msg(4, Msg::new().uint(1, 1).uint(2, 0).msg(3, f("Game\\game.exe")))
.msg(4, Msg::new().uint(1, 2).uint(2, 1).msg(3, f("en.pak")).text(5, "en-US"))
.msg(4, Msg::new().uint(1, 3).uint(2, 1).msg(3, f("fr.pak")).text(5, "fr-FR"))
.uint(21, ZSTD);
let parsed = parse(&m.0, "en-US").unwrap();
let names: Vec<_> = parsed.files.iter().map(|f| f.name.as_str()).collect();
assert_eq!(names, ["Game/game.exe", "en.pak"]);
assert_eq!(parsed.compression, ZSTD);
assert_eq!(parsed.files[0].slices[0], Slice { size: 10, download_size: Some(4), download_sha1: vec![0xBB], content_sha1: vec![0xAA], offset: 0 });
}
#[test]
fn leaves_out_add_ons_the_account_does_not_own() {
let f = |name: &str| Msg::new().text(1, name).uint(2, 10).flag(3, false);
let m = Msg::new()
.msg(4, Msg::new().uint(1, 1).uint(2, 0).msg(3, f("base.forge")).uint(4, 9999))
.msg(4, Msg::new().uint(1, 2).uint(2, 1).msg(3, f("dlc_24/east.forge")).uint(4, 5450))
.msg(4, Msg::new().uint(1, 3).uint(2, 1).msg(3, f("dlc_25/west.forge")).uint(8, 5451).uint(8, 5452))
.uint(21, ZSTD);
let parsed = parse_owned(&m.0, "en-US", &[5405, 5452]).unwrap();
let names: Vec<_> = parsed.files.iter().map(|f| f.name.as_str()).collect();
assert_eq!(names, ["base.forge", "dlc_25/west.forge"]);
assert_eq!(parsed.skipped, ["dlc_24/east.forge"]);
assert_eq!(parsed.chunks.iter().map(|c| c.id).collect::<Vec<_>>(), [1, 3]);
}
#[test]
fn inflates_both_deflate_framings() {
use std::io::Write;
let mut z = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::default());
z.write_all(b"hello").unwrap();
assert_eq!(decompress(DEFLATE, &z.finish().unwrap()).unwrap(), b"hello");
let mut r = flate2::write::DeflateEncoder::new(Vec::new(), flate2::Compression::default());
r.write_all(b"hello").unwrap();
assert_eq!(decompress(DEFLATE, &r.finish().unwrap()).unwrap(), b"hello");
}
}
@@ -0,0 +1,6 @@
pub mod demux;
pub mod ffi;
pub mod install;
pub mod manifest;
pub mod pb;
pub mod service;
+104
View File
@@ -0,0 +1,104 @@
pub use crate::amazon::proto::{Fields, Value};
#[derive(Default)]
pub struct Msg(pub Vec<u8>);
impl Msg {
pub fn new() -> Self {
Msg(Vec::new())
}
fn varint(&mut self, mut v: u64) {
loop {
let b = (v & 0x7f) as u8;
v >>= 7;
if v == 0 {
self.0.push(b);
return;
}
self.0.push(b | 0x80);
}
}
fn key(&mut self, field: u32, wire: u8) {
self.varint((field as u64) << 3 | wire as u64);
}
pub fn uint(mut self, field: u32, v: u64) -> Self {
self.key(field, 0);
self.varint(v);
self
}
pub fn flag(self, field: u32, v: bool) -> Self {
self.uint(field, v as u64)
}
pub fn bytes(mut self, field: u32, data: &[u8]) -> Self {
self.key(field, 2);
self.varint(data.len() as u64);
self.0.extend_from_slice(data);
self
}
pub fn text(self, field: u32, s: &str) -> Self {
self.bytes(field, s.as_bytes())
}
pub fn msg(self, field: u32, m: Msg) -> Self {
self.bytes(field, &m.0)
}
}
/// Every field of a message, in wire order; repeated fields appear once per value.
pub fn fields(data: &[u8]) -> Result<Vec<(u32, Value<'_>)>, String> {
let mut out = Vec::new();
let mut f = Fields::new(data);
while let Some(v) = f.next_field()? {
out.push(v);
}
Ok(out)
}
pub fn first<'a>(all: &[(u32, Value<'a>)], field: u32) -> Option<Value<'a>> {
all.iter().find(|(n, _)| *n == field).map(|(_, v)| *v)
}
pub fn sub<'a>(all: &[(u32, Value<'a>)], field: u32) -> Option<&'a [u8]> {
match first(all, field) {
Some(Value::Bytes(b)) => Some(b),
_ => None,
}
}
pub fn subs<'a>(all: &[(u32, Value<'a>)], field: u32) -> Vec<&'a [u8]> {
all.iter().filter_map(|(n, v)| match (n, v) { (n, Value::Bytes(b)) if *n == field => Some(*b), _ => None }).collect()
}
pub fn num(all: &[(u32, Value<'_>)], field: u32) -> Option<u64> {
match first(all, field) {
Some(Value::Varint(v)) | Some(Value::Fixed64(v)) => Some(v),
Some(Value::Fixed32(v)) => Some(v as u64),
_ => None,
}
}
pub fn string(all: &[(u32, Value<'_>)], field: u32) -> Option<String> {
sub(all, field).map(|b| String::from_utf8_lossy(b).into_owned())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn round_trips_nested_messages() {
let m = Msg::new().uint(1, 300).msg(2, Msg::new().text(1, "a").text(1, "b")).flag(3, true);
let all = fields(&m.0).unwrap();
assert_eq!(num(&all, 1), Some(300));
assert_eq!(num(&all, 3), Some(1));
let inner = fields(sub(&all, 2).unwrap()).unwrap();
let texts: Vec<_> = subs(&inner, 1).iter().map(|b| String::from_utf8_lossy(b).into_owned()).collect();
assert_eq!(texts, ["a", "b"]);
}
}
@@ -0,0 +1,206 @@
use serde_json::{json, Value as Json};
use super::demux::Demux;
use super::pb::{self, Msg};
pub struct Owned {
pub product_id: u32,
pub space_id: String,
pub manifest: String,
pub product_type: u32,
pub state: u32,
pub owned: bool,
pub configuration: String,
pub app_id: String,
pub partner: u32,
pub activation: u32,
pub locked_by_subscription: bool,
pub associations: Vec<u32>,
pub download_id: u32,
}
impl Owned {
/// Whether Connect lists the product among the session's owned ids: owned games and add-ons that are released.
pub fn in_session(&self) -> bool {
self.owned && self.product_type <= 1 && (self.state == 0 || self.state == 3)
}
pub fn to_json(&self) -> Json {
json!({
"productId": self.product_id, "spaceId": self.space_id, "manifest": self.manifest,
"productType": self.product_type, "state": self.state, "owned": self.owned,
"configuration": self.configuration, "appId": self.app_id,
"partner": self.partner, "activation": self.activation, "lockedBySubscription": self.locked_by_subscription,
"associations": self.associations, "downloadId": self.download_id,
})
}
}
pub struct Ownership {
conn: u32,
next: u64,
}
fn owned_game(b: &[u8]) -> Result<Owned, String> {
let f = pb::fields(b)?;
Ok(Owned {
product_id: pb::num(&f, 1).unwrap_or(0) as u32,
space_id: pb::string(&f, 30).unwrap_or_default(),
manifest: pb::string(&f, 21).unwrap_or_default(),
product_type: pb::num(&f, 8).unwrap_or(0) as u32,
state: pb::num(&f, 13).unwrap_or(0) as u32,
owned: pb::num(&f, 20).map(|v| v == 1).unwrap_or(true),
configuration: pb::string(&f, 17).unwrap_or_default(),
app_id: pb::string(&f, 31).unwrap_or_default(),
partner: pb::num(&f, 38).unwrap_or(0) as u32,
activation: pb::num(&f, 36).unwrap_or(0) as u32,
locked_by_subscription: pb::num(&f, 37) == Some(1),
associations: f.iter().filter_map(|(n, v)| match v { pb::Value::Varint(x) if *n == 16 => Some(*x as u32), _ => None }).collect(),
download_id: pb::num(&f, 5).unwrap_or(0) as u32,
})
}
impl Ownership {
/// Opens ownership_service and initializes it, which also returns the account's products.
pub async fn open(d: &mut Demux, ticket: &str, session_id: &str) -> Result<(Self, Vec<Owned>), String> {
let conn = d.open("ownership_service").await?;
let init = Msg::new().flag(2, true).uint(3, 7);
let mut req = Msg::new().uint(1, 1).msg(2, init).text(30, ticket);
if !session_id.is_empty() {
req = req.text(31, session_id);
}
let raw = d.call(conn, Msg::new().msg(1, req)).await?;
let rsp = pb::fields(pb::sub(&pb::fields(&raw)?, 1).ok_or("no ownership answer")?)?;
let init = pb::fields(pb::sub(&rsp, 2).ok_or("no ownership initialize answer")?)?;
if pb::num(&init, 1) != Some(1) {
return Err("Ubisoft would not list the account's games".into());
}
let mut games = Vec::new();
if let Some(list) = pb::sub(&init, 2) {
for g in pb::subs(&pb::fields(list)?, 1) {
games.push(owned_game(g)?);
}
}
Ok((Ownership { conn, next: 2 }, games))
}
/// The latest manifest of each product that has one, for games whose ownership entry leaves it out.
pub async fn latest_manifests(&mut self, d: &mut Demux, products: &[u32]) -> Result<Vec<(u32, String)>, String> {
if products.is_empty() {
return Ok(Vec::new());
}
let id = self.next;
self.next += 1;
let mut req = Msg::new();
for p in products {
req = req.uint(1, *p as u64);
}
let raw = d.call(self.conn, Msg::new().msg(1, Msg::new().uint(1, id).msg(10, req))).await?;
let rsp = pb::fields(pb::sub(&pb::fields(&raw)?, 1).ok_or("no ownership answer")?)?;
let m = pb::fields(pb::sub(&rsp, 10).ok_or("no manifest answer")?)?;
if pb::num(&m, 1) != Some(1) {
return Err("Ubisoft would not list the latest manifests".into());
}
let mut out = Vec::new();
for entry in pb::subs(&m, 2) {
let e = pb::fields(entry)?;
if pb::num(&e, 1) != Some(1) {
continue;
}
if let (Some(product), Some(manifest)) = (pb::num(&e, 2), pb::string(&e, 3).filter(|s| !s.is_empty())) {
out.push((product as u32, manifest));
}
}
Ok(out)
}
/// The ticket a game receives in its launcher session (`GetUplayPCTicket`).
pub async fn pc_ticket(&mut self, d: &mut Demux, product_id: u32, ticket: &str, session_id: &str) -> Result<String, String> {
let id = self.next;
self.next += 1;
let info = Msg::new().uint(1, product_id as u64).uint(2, 0).uint(3, product_id as u64);
let mut req = Msg::new().uint(1, id).msg(14, info).text(30, ticket);
if !session_id.is_empty() {
req = req.text(31, session_id);
}
let raw = d.call(self.conn, Msg::new().msg(1, req)).await?;
let rsp = pb::fields(pb::sub(&pb::fields(&raw)?, 1).ok_or("no ownership answer")?)?;
let t = pb::fields(pb::sub(&rsp, 13).ok_or("no game ticket answer")?)?;
if pb::num(&t, 1) != Some(1) {
return Err(format!("Ubisoft would not issue a game ticket (code {})", pb::num(&t, 3).unwrap_or(0)));
}
pb::string(&t, 2).filter(|s| !s.is_empty()).ok_or_else(|| "Ubisoft sent an empty game ticket".into())
}
pub async fn token(&mut self, d: &mut Demux, product_id: u32) -> Result<String, String> {
let id = self.next;
self.next += 1;
let raw = d.call(self.conn, Msg::new().msg(1, Msg::new().uint(1, id).msg(28, Msg::new().uint(1, product_id as u64)))).await?;
let rsp = pb::fields(pb::sub(&pb::fields(&raw)?, 1).ok_or("no ownership answer")?)?;
let t = pb::fields(pb::sub(&rsp, 27).ok_or("no ownership token answer")?)?;
if pb::num(&t, 1) != Some(1) {
return Err("Ubisoft says this account does not own the game".into());
}
pb::string(&t, 2).filter(|s| !s.is_empty()).ok_or_else(|| "Ubisoft sent an empty ownership token".into())
}
}
pub struct Downloads {
conn: u32,
next: u64,
}
impl Downloads {
pub async fn open(d: &mut Demux, ownership_token: &str) -> Result<Self, String> {
let conn = d.open("download_service").await?;
let raw = d.call(conn, Msg::new().msg(1, Msg::new().uint(1, 1).msg(2, Msg::new().text(5, ownership_token)))).await?;
let rsp = pb::fields(pb::sub(&pb::fields(&raw)?, 1).ok_or("no download service answer")?)?;
let ok = pb::sub(&rsp, 2).map(pb::fields).transpose()?.and_then(|i| pb::num(&i, 1)) == Some(1);
if !ok {
return Err("Ubisoft's download service refused the ownership token".into());
}
Ok(Downloads { conn, next: 2 })
}
/// Signed CDN URLs for paths under the product's download root, in request order; empty where none was given.
pub async fn sign(&mut self, d: &mut Demux, product_id: u32, paths: &[String]) -> Result<Vec<Vec<String>>, String> {
let id = self.next;
self.next += 1;
let mut request = Msg::new().uint(1, product_id as u64);
for p in paths {
request = request.text(2, p);
}
let raw = d.call(self.conn, Msg::new().msg(1, Msg::new().uint(1, id).msg(3, Msg::new().msg(1, request)))).await?;
let rsp = pb::fields(pb::sub(&pb::fields(&raw)?, 1).ok_or("no download service answer")?)?;
let urls = pb::fields(pb::sub(&rsp, 3).ok_or("no signed URLs")?)?;
let mut out = Vec::new();
for r in pb::subs(&urls, 1) {
let r = pb::fields(r)?;
if pb::num(&r, 1).unwrap_or(0) != 0 {
return Err("Ubisoft says this account does not own the game".into());
}
for group in pb::subs(&r, 2) {
out.push(pb::subs(&pb::fields(group)?, 1).iter().map(|u| String::from_utf8_lossy(u).into_owned()).collect());
}
}
if out.len() != paths.len() {
return Err(format!("Ubisoft signed {} of {} paths", out.len(), paths.len()));
}
Ok(out)
}
}
/// Denuvo's activation for a game the account owns: the game's challenge and the product's ownership
/// token go to `denuvo_service`, which answers with the game token the game's aux DLL waits for.
pub async fn denuvo_token(d: &mut Demux, ownership_token: &str, challenge: &[u8]) -> Result<Vec<u8>, String> {
let conn = d.open("denuvo_service").await?;
let req = Msg::new().uint(1, 1).msg(2, Msg::new().text(1, ownership_token).bytes(2, challenge));
let raw = d.call(conn, Msg::new().msg(1, req)).await?;
let rsp = pb::fields(pb::sub(&pb::fields(&raw)?, 1).ok_or("no denuvo answer")?)?;
let result = pb::num(&rsp, 2).unwrap_or(0);
if result != 0 {
return Err(format!("Ubisoft's activation service refused the game (result {result})"));
}
let t = pb::fields(pb::sub(&rsp, 3).ok_or("no game token answer")?)?;
pb::sub(&t, 1).filter(|b| !b.is_empty()).map(<[u8]>::to_vec).ok_or_else(|| "Ubisoft sent an empty game token".into())
}
@@ -0,0 +1,60 @@
package com.droiddeck.launcher.stores.ubisoft
import com.droiddeck.launcher.stores.Store
import com.droiddeck.launcher.stores.StoreGameSidecar
import com.droiddeck.launcher.stores.StoreLaunch
import org.json.JSONArray
import org.json.JSONObject
import org.junit.Assert.assertArrayEquals
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [33], application = android.app.Application::class)
class UbisoftTest {
private val steamConfig = "version: 2.0\r\nroot:\r\n name: Assassin's Creed Syndicate\r\n start_game:\r\n steam:\r\n steam_app_id: 368500\r\n" +
" third_party_platform:\r\n name: Steam\r\n override_uplayid: 1875\r\n"
@Test fun aProductUbisoftDeliversThroughSteamNamesItsSteamApp() {
assertEquals("368500", UbisoftLibrary.steamApp(steamConfig))
assertNull(UbisoftLibrary.steamApp("root:\n start_game:\n online:\n executables:\n - path:\n relative: bin/game.exe\n"))
}
@Test fun steamOwnedGamesAreKeyedByTheirSpace() {
val owned = JSONObject().put("games", JSONArray()
.put(JSONObject().put("productId", 1957).put("spaceId", "space-s").put("productType", 0).put("owned", true).put("configuration", steamConfig))
.put(JSONObject().put("productId", 109).put("spaceId", "space-sc").put("productType", 0).put("owned", true).put("manifest", "AB").put("configuration", "root: {}"))
.put(JSONObject().put("productId", 1961).put("spaceId", "space-addon").put("productType", 1).put("owned", true).put("configuration", steamConfig)))
assertEquals(mapOf("space-s" to "368500"), UbisoftLibrary.steamBySpace(owned))
assertEquals(setOf("space-sc"), UbisoftLibrary.ownedBySpace(owned).keys)
}
@Test fun theEntitlementNamesTheGamesApplication() {
val body = JSONObject().put("items", JSONArray()
.put(JSONObject().put("productId", 82).put("applicationId", "app-82"))
.put(JSONObject().put("productId", 109).put("applicationId", "app-109"))).toString()
assertEquals("app-109", UbisoftLaunch.applicationId(body, 109))
assertNull(UbisoftLaunch.applicationId(body, 1651))
}
@Test fun theLauncherStartsTheServiceOnlyWithAFreshLaunchFile() {
val text = StoreLaunch.launcherText(StoreGameSidecar(Store.UBISOFT, "space-sc", "Splinter Cell", "system/SplinterCell.exe"))
val lines = text.trimEnd().split("\r\n")
assertTrue(lines.contains("if not exist \"%~dp0.droiddeck-ubisoft.json\" goto direct"))
assertTrue(lines.contains("\"Z:\\usr\\local\\lib\\droiddeck-ubisoft\\droiddeck-upc.exe\" \"%~dp0.droiddeck-ubisoft.json\""))
assertEquals("\"%~dp0system\\SplinterCell.exe\"", lines.last())
assertTrue(lines.indexOf(":direct") < lines.lastIndex)
}
@Test fun activationPayloadsSurviveTheHexHop() {
val bytes = byteArrayOf(0, 1, 0x7f, -1, -128, 42)
assertEquals("00017fff802a", UbisoftActivation.hex(bytes))
assertArrayEquals(bytes, UbisoftActivation.unhex("00017fff802a"))
assertNull(UbisoftActivation.unhex("abc"))
}
}