mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-21 13:55:58 +00:00
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:
1 parent
d8ed02f2cf
commit
3c96df4ab1
49 files changed
+3960
-24
No files matched your search
Generated
+18
@@ -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"
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
@@ -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())
|
||||
}
|
||||
Reference in new issue
Block a user