Files
DroidDeck/tools/ubisoft-launcher/src/main.rs
T
Kurt Himebauch 123c431b8e feat(ea, ubisoft): cloud saves and a kept Ubisoft Denuvo token (#2824)
* feat(ubisoft): keep Denuvo game tokens per account and product until the game invalidates them

* feat(ubisoft): sync cloud saves through Ubisoft's cloud save service

* fix(xbox): turning cloud saves off passes the offline mode the game save runtime reads

* fix(ubisoft): Ubisoft games list their shared prefix, so they get a saves and cloud tab

* feat(ea): sync cloud saves through EA's cloud sync service

* fix(ea): sync at EA launch and exit, keep one rule snapshot, and check the cloud copy under the write lock

* fix(ubisoft): exact slot versions, scoped save export, and a Denuvo cache bound to its account

* fix(ea): fresh rules per sync, checksums required, backup before restore, and sign-in shown on the saves tab

* fix(ubisoft): launch-time sync on the shared prefix, saves scoped to the game, and Denuvo bound to each launch's account

* fix(ea): back up only the saves a restore replaces, refuse truncated manifests, and need a master title id

* fix(ubisoft): treat a slot whose bytes changed since the last sync as a conflict before uploading over it

* fix(ea): one resolved save folder per sync, refuse case collisions, and walk each folder once

* fix(ubisoft): sync only identified slots, keep live saves and backups safe, and tie a pending eviction to its account

* fix(cloud): a forced download fetches every saved conflict and keeps the ones it could not write

* test(ea): keep the cloud save cases in EaTest

* fix(ubisoft): report only written slots, keep drift conflicts, read whole headers, and never rename a named eviction

* fix(ea): keep real save folders in the inventory, back up any restored file, refuse duplicate or malformed cloud listings

* fix(cloud): commit Ubisoft slots one at a time and stop on a newer cloud write, name skipped and blocked slots, keep unsent conflicts

* fix(ubisoft): acknowledge an invalidation only once its marker is published and never serve a kept token after it

* fix(cloud): a cloud request the launcher cannot write still starts the game and keeps the exit upload

* fix(cloud): scope Ubisoft sync state to the account and product, compare alike slots, keep committed progress, and name partial slots

* fix(ea): ask for sign-in after a rejected refresh, upload saves reached through folder aliases

* fix(cloud): a launch that stops waiting for the cloud download claims it back, so no save is written once the game starts

* fix(ubisoft): name the account in an invalidation marker and never hold the token lock across a pipe write

* fix(ea): only invalid_grant asks for sign-in again, and case collisions count only among synced saves

* test(epic): let the launcher's ask doubles take the claim flag

* fix(ea): apply save rules in their order, match bare patterns inside the save folder, and resolve LocalLow and install folders

* fix(ubisoft): stamp a fresh identifier into a new save that has none, so it can sync

* fix(ubisoft): answer a burn ticket request as unavailable instead of leaving the game waiting

* fix(cloud): read EA manifests with the XML parser, clear conflicts whose sides match, and bound the claim and stall tests
2026-11-18 18:32:06 -05:00

408 lines
15 KiB
Rust

//! Stands in for Ubisoft Connect on the local pipes a Ubisoft game talks to, and starts the game.
//!
//! Usage: droiddeck-upc.exe <launch.json>
#![cfg_attr(windows, windows_subsystem = "windows")]
mod bridge;
mod launcher;
mod pb;
#[cfg(windows)]
mod win;
#[cfg(not(windows))]
fn main() {
eprintln!("droiddeck-upc runs inside Wine");
std::process::exit(2);
}
#[cfg(windows)]
fn main() {
std::process::exit(service::run());
}
#[cfg(windows)]
mod service {
use crate::bridge::{self, Generation};
use crate::launcher::{self, AuxReq, Config, SessionReq};
use crate::win::{self, Handle};
use std::fs::OpenOptions;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::{Duration, Instant};
const MAX_FRAME: usize = 16 << 20;
const ANNOUNCE_WAIT: Duration = Duration::from_secs(120);
const RESTART_WAIT: Duration = Duration::from_secs(15);
const DENUVO_WAIT: Duration = Duration::from_secs(90);
const SERVE_WAIT: Duration = Duration::from_secs(10);
const MARKER_TRIES: u32 = 25;
const MARKER_PAUSE: Duration = Duration::from_millis(200);
struct State {
cfg: Config,
prefix: String,
log: Mutex<Option<std::fs::File>>,
event: Handle,
game: AtomicU32,
stale_token: AtomicBool,
generation: Generation,
started: Instant,
}
impl State {
fn say(&self, line: &str) {
let text = format!("[{:7.2}] {line}\n", self.started.elapsed().as_secs_f32());
eprint!("droiddeck-upc: {text}");
if let Some(f) = self.log.lock().unwrap().as_mut() {
let _ = f.write_all(text.as_bytes());
}
}
}
struct Frames<'a> {
pipe: &'a Handle,
buf: Vec<u8>,
}
impl<'a> Frames<'a> {
fn new(pipe: &'a Handle) -> Self {
Frames { pipe, buf: Vec::new() }
}
fn fill(&mut self) -> bool {
let mut chunk = vec![0u8; 65536];
match self.pipe.read(&mut chunk) {
Some(n) => {
self.buf.extend_from_slice(&chunk[..n]);
true
}
None => false,
}
}
/// The next message without its 4-byte big-endian length.
fn next(&mut self) -> Option<Vec<u8>> {
while self.buf.len() < 4 {
if !self.fill() {
return None;
}
}
let n = u32::from_be_bytes(self.buf[..4].try_into().unwrap()) as usize;
if n > MAX_FRAME {
return None;
}
while self.buf.len() < 4 + n {
if !self.fill() {
return None;
}
}
let body = self.buf[4..4 + n].to_vec();
self.buf.drain(..4 + n);
Some(body)
}
}
fn send(pipe: &Handle, body: &[u8]) -> bool {
let mut framed = (body.len() as u32).to_be_bytes().to_vec();
framed.extend_from_slice(body);
pipe.write_all(&framed)
}
fn hold(state: &State, name: &str, frames: &mut Frames) {
while let Some(body) = frames.next() {
state.say(&format!("{name}: {} B left unanswered", body.len()));
}
}
fn game_start(state: &State, pipe: Handle) {
let mut frames = Frames::new(&pipe);
let Some(req) = frames.next() else { return };
send(&pipe, &state.cfg.game_start(&req));
state.say("game_start: start granted");
hold(state, "game_start", &mut frames);
}
fn api_process(state: &State, pipe: Handle) {
let mut frames = Frames::new(&pipe);
let Some(req) = frames.next() else { return };
match launcher::announced(&req) {
Some((product, named)) => {
let product = product.unwrap_or(state.cfg.product);
match named.or_else(|| pipe.client_pid()) {
Some(pid) => {
state.say(&format!("api_process: product {product} is process {pid}"));
state.game.store(pid, Ordering::SeqCst);
}
None => state.say(&format!("api_process: product {product} named no process")),
}
}
None => state.say(&format!("api_process: {} B announcement not understood {}", req.len(), hex(&req, 32))),
}
send(&pipe, &state.cfg.init_process());
state.event.set();
hold(state, "api_process", &mut frames);
}
fn session(state: &State, pipe: Handle) {
let mut frames = Frames::new(&pipe);
let mut pushes = 0;
while let Some(req) = frames.next() {
match launcher::session_request(&req) {
SessionReq::Init(wants) => {
pushes += 1;
send(&pipe, &state.cfg.session(wants));
let asked = wants.map_or("no subsystems named".to_string(), |w| format!("install {}, storage {}", w.install, w.storage));
state.say(&format!("session: init answered ({pushes}; {asked}; {})", hex(&req, 48)));
}
SessionReq::Installer => {
send(&pipe, &state.cfg.installer());
state.say("session: installer init answered");
}
SessionReq::Credentials(id) => {
send(&pipe, &state.cfg.credentials(id));
state.say(&format!("session: credentials {id} answered"));
}
SessionReq::ProductList(id) => {
send(&pipe, &state.cfg.product_list(id));
state.say(&format!("session: product list {id} answered"));
}
SessionReq::Other => match launcher::acknowledge(&req) {
Some((answer, what)) => {
send(&pipe, &answer);
state.say(&format!("session: {what}"));
}
None => state.say(&format!("session: {} B request {} not handled", req.len(), hex(&req, 24))),
},
}
}
state.say("session: closed");
}
/// The game token for an activation, with the invalidation count it was asked under when it may be a kept one.
fn denuvo(state: &State, rid: u64, challenge: &[u8], fresh: bool) -> Option<(Vec<u8>, Option<u64>)> {
if state.cfg.bridge.is_empty() {
return None;
}
let dir = PathBuf::from(&state.cfg.bridge);
let _ = std::fs::create_dir_all(&dir);
let rsp = dir.join(format!("denuvo-{rid}.rsp"));
let _ = std::fs::remove_file(&rsp);
let part = dir.join(format!("denuvo-{rid}.req.part"));
std::fs::write(&part, challenge).ok()?;
let seen = state.generation.current();
let stale = state.stale_token.swap(false, Ordering::SeqCst) || fresh;
let kind = if stale { "req" } else { "cache.req" };
if std::fs::rename(&part, dir.join(format!("denuvo-{rid}.{kind}"))).is_err() {
if stale {
state.stale_token.store(true, Ordering::SeqCst);
}
return None;
}
let deadline = Instant::now() + DENUVO_WAIT;
while Instant::now() < deadline {
if let Ok(token) = std::fs::read(&rsp) {
let _ = std::fs::remove_file(&rsp);
return (!token.is_empty()).then_some((token, (!stale).then_some(seen)));
}
thread::sleep(Duration::from_millis(200));
}
None
}
fn aux(state: &State, pipe: Handle) {
let mut frames = Frames::new(&pipe);
while let Some(req) = frames.next() {
match launcher::aux_request(&req) {
AuxReq::Init(rid) => {
send(&pipe, &launcher::aux_init(rid, win::pid()));
state.say("aux: init answered");
}
AuxReq::Activate(rid, challenge) => {
state.say(&format!("aux: activation {rid} with a {} B challenge", challenge.len()));
let mut fresh = false;
loop {
let Some((token, seen)) = denuvo(state, rid, challenge, fresh) else {
state.say(&format!("aux: no game token for activation {rid}"));
break;
};
let answer = || send(&pipe, &launcher::aux_token(rid, &token));
let served = match seen {
Some(g) => state.generation.serve(g, answer).is_some(),
None => {
answer();
true
}
};
if served {
state.say(&format!("aux: activation {rid} answered ({} B)", token.len()));
break;
}
state.say(&format!("aux: kept token for activation {rid} came back after an invalidation; asking afresh"));
fresh = true;
}
}
AuxReq::Invalidate(rid) => {
state.stale_token.store(true, Ordering::SeqCst);
let published = state.generation.invalidate(SERVE_WAIT, || {
let published = state.cfg.bridge.is_empty()
|| bridge::publish_invalidation(Path::new(&state.cfg.bridge), &state.cfg.user_id, MARKER_TRIES, MARKER_PAUSE);
if published {
send(&pipe, &launcher::aux_invalidated(rid));
}
published
});
if published {
state.say(&format!("aux: token invalidated by request {rid}"));
} else {
state.say(&format!("aux: invalidation {rid} not acknowledged; the kept token could not be marked stale"));
}
}
AuxReq::BurnTicket(rid) => {
send(&pipe, &launcher::aux_no_burn_ticket(rid));
state.say(&format!("aux: burn ticket {rid} answered as unavailable"));
}
AuxReq::Other(rid) => state.say(&format!("aux: request {rid} not handled ({} B)", req.len())),
}
}
}
fn service(state: &State, pipe: Handle) {
send(&pipe, &state.cfg.service_hello());
let mut chunk = vec![0u8; 65536];
while pipe.read(&mut chunk).is_some() {}
}
fn orbit(state: &State, pipe: Handle) {
let mut frames = Frames::new(&pipe);
while let Some(req) = frames.next() {
match state.cfg.orbit(&req) {
Some((answer, what)) => {
send(&pipe, &answer);
state.say(&format!("orbit: {what}"));
}
None => state.say(&format!("orbit: {} B request {} not handled", req.len(), hex(&req, 24))),
}
}
state.say("orbit: closed");
}
fn quiet(state: &State, name: &str, pipe: Handle) {
state.say(&format!("{name}: connected"));
let mut chunk = vec![0u8; 65536];
while let Some(n) = pipe.read(&mut chunk) {
state.say(&format!("{name}: {n} B unanswered {}", hex(&chunk[..n], 64)));
}
}
fn serve(state: Arc<State>, name: &'static str) {
thread::spawn(move || {
let full = format!("{}{name}", state.prefix);
loop {
let Some(pipe) = win::accept(&full) else {
state.say(&format!("{name}: cannot serve the pipe (error {})", win::last_error()));
return;
};
let s = state.clone();
thread::spawn(move || match name {
launcher::GAME_START => game_start(&s, pipe),
launcher::API_PROCESS => api_process(&s, pipe),
launcher::SESSION => session(&s, pipe),
launcher::AUX => aux(&s, pipe),
launcher::SERVICE => service(&s, pipe),
launcher::ORBIT => orbit(&s, pipe),
_ => quiet(&s, name, pipe),
});
}
});
}
fn hex(b: &[u8], n: usize) -> String {
b.iter().take(n).map(|x| format!("{x:02x}")).collect()
}
fn command_line(cfg: &Config) -> String {
let mut c = format!("\"{}\"", cfg.exe);
if !cfg.args.trim().is_empty() {
c.push(' ');
c.push_str(cfg.args.trim());
}
c
}
pub fn run() -> i32 {
let Some(path) = std::env::args().nth(1) else {
eprintln!("usage: droiddeck-upc <launch.json>");
return 2;
};
let parsed = std::fs::read_to_string(&path).map_err(|e| e.to_string()).and_then(|t| Config::parse(&t));
let _ = std::fs::remove_file(&path);
let cfg = match parsed {
Ok(c) => c,
Err(e) => {
eprintln!("droiddeck-upc: {e}");
return 2;
}
};
let log = (!cfg.log.is_empty()).then(|| OpenOptions::new().create(true).append(true).open(&cfg.log).ok()).flatten();
let state = Arc::new(State {
prefix: format!(r"\\.\pipe\terminal_{}_", win::session_id()),
cfg,
log: Mutex::new(log),
event: win::inheritable_event(),
game: AtomicU32::new(0),
stale_token: AtomicBool::new(false),
generation: Generation::default(),
started: Instant::now(),
});
state.say(&format!("product {} on {}*", state.cfg.product, state.prefix));
for name in [launcher::GAME_START, launcher::API_PROCESS, launcher::SESSION, launcher::AUX, launcher::SERVICE, launcher::ORBIT]
.into_iter()
.chain(launcher::QUIET.iter().copied())
{
serve(state.clone(), name);
}
thread::sleep(Duration::from_millis(300));
let (child, pid) = match win::spawn(&command_line(&state.cfg), &state.cfg.workdir) {
Ok(c) => c,
Err(e) => {
state.say(&format!("cannot start {} (error {e})", state.cfg.exe));
return 1;
}
};
state.say(&format!("started {} as process {pid}", state.cfg.exe));
while !child.wait(1000) {}
let mut code = child.exit_code();
state.say(&format!("process {pid} exited with 0x{code:08x}"));
let mut waiting = Instant::now();
let mut limit = ANNOUNCE_WAIT;
loop {
let game = state.game.swap(0, Ordering::SeqCst);
if game != 0 {
match win::open_process(game) {
Some(h) => {
while !h.wait(1000) {}
code = h.exit_code();
state.say(&format!("game process {game} exited with 0x{code:08x}"));
}
None => state.say(&format!("game process {game} is already gone")),
}
waiting = Instant::now();
limit = RESTART_WAIT;
continue;
}
if waiting.elapsed() >= limit {
break;
}
thread::sleep(Duration::from_millis(500));
}
state.say("done");
code as i32
}
}