From 615b3e938d89eab88ccd21eda2352a605f25bf5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9?= Date: Thu, 1 Oct 2026 07:52:58 +0500 Subject: [PATCH] Game client: download and update through the Manager PLAY turns into UPDATE CLIENT when a tracked client is outdated; with no client linked it offers choosing an existing folder or downloading from the community launcher servers (resumable, hash-verified, free-space checked, progress and stop). Files the player changed are kept on request or moved aside, never silently replaced. Existing clients can be adopted from Settings. Fresh downloads get a realmlist for each installed locale. Fixes zero-length files in the downloader (found by the live probe). Co-Authored-By: Claude Sonnet 5.5 --- README.md | 10 +- crates/coa-core/examples/client_probe.rs | 31 ++ crates/coa-core/src/client.rs | 44 ++ crates/coa-core/src/clientdl.rs | 576 +++++++++++++++++++++++ crates/coa-core/src/download.rs | 27 +- crates/coa-core/src/lib.rs | 1 + docs/ARCHITECTURE.md | 34 +- src-tauri/src/lib.rs | 186 +++++++- src/dev/mock.ts | 17 +- src/i18n/locales/de.ts | 50 ++ src/i18n/locales/en.ts | 50 ++ src/i18n/locales/es.ts | 50 ++ src/i18n/locales/fr.ts | 50 ++ src/i18n/locales/ru.ts | 50 ++ src/lib/api.ts | 39 ++ src/lib/clientUpdate.ts | 51 ++ src/lib/utils.ts | 12 + src/screens/ClientCard.tsx | 38 +- src/screens/ClientDialog.tsx | 300 ++++++++++++ src/screens/Overview.tsx | 31 +- src/screens/Shell.tsx | 12 +- 21 files changed, 1632 insertions(+), 27 deletions(-) create mode 100644 crates/coa-core/examples/client_probe.rs create mode 100644 crates/coa-core/src/clientdl.rs create mode 100644 src/lib/clientUpdate.ts create mode 100644 src/screens/ClientDialog.tsx diff --git a/README.md b/README.md index 2236468..cfbcb79 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,9 @@ no console, no config files, no database tools. Tauri 2 + Rust + React. database and creates your first account. Or import a server you already have. * **Start / Stop / PLAY.** One button for the whole server (database, auth, world) with live status, uptime and players online. PLAY starts the game client with the right realmlist. +* **Game client.** Use the client you already have, or let the Manager download it (about 43 GB, resumable, every file + verified). A client the Manager looks after is checked for updates like the server: PLAY turns into **UPDATE CLIENT**, + and files you changed yourself are never replaced unless you say so. * **Safe updates.** The Manager checks for a new server version on start and every five minutes; the main button turns into **UPDATE**. Updates are authenticated (Ed25519), back up first, keep files you changed and can be rolled back. * **Backups.** Characters, accounts and settings on demand, automatically before every update; verify and restore @@ -38,15 +41,14 @@ no console, no config files, no database tools. Tauri 2 + Rust + React. ## Install Download the installer from the [Releases](https://github.com/Corfirean/coa-server-manager/releases) page -and run it. You need about 10 GB of free disk space for the server. The Manager does not include the game client; -use your existing Conquest of Azeroth client. +and run it. You need about 10 GB of free disk space for the server. The game client (about 43 GB) is separate: point the +Manager at the one you have, or let it download one for you. ## Status Phases 0–10 of the plan in [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md) are done: foundation, import, start/stop, config engine, backups, clean install from signed packages, updates, companions, client launch, friends, polish. -Remaining work (client download/update from the Manager, translation review by native speakers) is listed in -section 9 of that document. Translation notes: [docs/TRANSLATIONS.md](docs/TRANSLATIONS.md). +Remaining work (translation review by native speakers, optional extras) is listed in section 9 of that document. Translation notes: [docs/TRANSLATIONS.md](docs/TRANSLATIONS.md). ## Develop ``` diff --git a/crates/coa-core/examples/client_probe.rs b/crates/coa-core/examples/client_probe.rs new file mode 100644 index 0000000..1ebfb1b --- /dev/null +++ b/crates/coa-core/examples/client_probe.rs @@ -0,0 +1,31 @@ +//! Live check of the client download path against the community launcher's public server: +//! reads the manifest and downloads the few smallest files into a temp folder (never a real client). +//! Usage: cargo run -p coa-core --example client_probe (PROBE_SKIP=n skips the n smallest files) + +use coa_core::clientdl::{self, Apply, Manifest}; +use coa_core::download::{Cancel, HttpTransport}; + +fn main() { + let full = clientdl::fetch_latest().expect("manifest"); + println!("version {} · {} files · {:.1} GB", full.version, full.files.len(), full.total_bytes() as f64 / 1e9); + let mut small = full.files.clone(); + small.sort_by_key(|f| f.size); + let skip: usize = std::env::var("PROBE_SKIP").ok().and_then(|v| v.parse().ok()).unwrap_or(0); + let small: Vec<_> = small.into_iter().skip(skip).take(6).collect(); + let manifest = Manifest { version: full.version.clone(), published_at: None, files: small }; + + let dir = std::env::temp_dir().join(format!("coa-client-probe-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let cancel = Cancel::default(); + let plan = clientdl::plan(&dir, &manifest, &Default::default(), &cancel, &|_| {}).unwrap(); + println!("plan: {} files, {} bytes", plan.items.len(), plan.download_bytes); + let transport = HttpTransport::new().unwrap(); + clientdl::apply( + &Apply { client: &dir, manifest: &manifest, plan: &plan, keep_modified: false, transport: &transport, objects_url: clientdl::OBJECTS_URL, cancel: &cancel }, + &|s| println!("{} {}/{} {}", s.phase, s.done, s.total, s.file.unwrap_or_default()), + ) + .expect("apply"); + let again = clientdl::plan(&dir, &manifest, &clientdl::load_state(&dir).unwrap(), &cancel, &|_| {}).unwrap(); + println!("second plan: {} to do, {} up to date, version {:?}", again.items.len(), again.up_to_date_files, clientdl::local(&dir).version); + std::fs::remove_dir_all(&dir).unwrap(); +} diff --git a/crates/coa-core/src/client.rs b/crates/coa-core/src/client.rs index 39af351..c2846c4 100644 --- a/crates/coa-core/src/client.rs +++ b/crates/coa-core/src/client.rs @@ -122,12 +122,40 @@ fn host_ok(h: &str) -> bool { !h.is_empty() && h.len() <= 253 && h.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '-' | ':')) && !h.starts_with('-') } +/// Locales a client has game data for (`Data//locale-.MPQ`), e.g. `enUS`. +fn installed_locales(client: &Path) -> Vec { + let mut v: Vec = fs::read_dir(client.join("Data")) + .map(|rd| { + rd.flatten() + .filter(|e| e.path().is_dir()) + .filter_map(|e| e.file_name().into_string().ok()) + .filter(|n| n.len() == 4 && n[..2].bytes().all(|b| b.is_ascii_lowercase()) && n[2..].bytes().all(|b| b.is_ascii_uppercase())) + .filter(|n| client.join("Data").join(n).join(format!("locale-{n}.MPQ")).is_file()) + .collect() + }) + .unwrap_or_default(); + v.sort(); + v +} + /// Point every locale's realmlist at `host`. Each changed file is copied to the backup folder first. +/// A freshly downloaded client has no realmlist yet; one is created for each installed locale. pub fn set_realmlist(client: &Path, meta: &Path, host: &str) -> Result> { if !host_ok(host) { return Err(Error::Invalid("That address is not valid.".into())); } let mut changed = Vec::new(); + if realmlist_files(client).is_empty() { + for loc in installed_locales(client) { + let file = client.join("Data").join(&loc).join("realmlist.wtf"); + fsx::atomic_write(&file, format!("set realmlist {host} +").as_bytes())?; + changed.push(file.to_string_lossy().into_owned()); + } + if !changed.is_empty() { + return Ok(changed); + } + } let stamp = chrono::Utc::now().format("%Y%m%d-%H%M%S").to_string(); for file in realmlist_files(client) { let old = fs::read(&file)?; @@ -261,6 +289,22 @@ mod tests { assert!(set_realmlist(&c, &meta, "bad host; rm -rf").is_err()); } + #[test] + fn a_fresh_client_gets_a_realmlist_for_each_installed_locale() { + let d = tempfile::tempdir().unwrap(); + let c = d.path().join("fresh"); + fs::create_dir_all(c.join("Data/enUS")).unwrap(); + fs::create_dir_all(c.join("Data/Content")).unwrap(); + fs::write(c.join("Data/enUS/locale-enUS.MPQ"), b"x").unwrap(); + fs::write(c.join("Ascension.exe"), b"exe").unwrap(); + let changed = set_realmlist(&c, &d.path().join("meta"), "127.0.0.1").unwrap(); + assert_eq!(changed.len(), 1); + assert_eq!(fs::read_to_string(c.join("Data/enUS/realmlist.wtf")).unwrap(), "set realmlist 127.0.0.1 +"); + assert!(!c.join("Data/Content/realmlist.wtf").exists()); + assert_eq!(detect(&c, None).unwrap().realmlists[0].host.as_deref(), Some("127.0.0.1")); + } + #[test] fn addon_install_touches_only_its_own_folder_and_backs_up_a_different_version() { let d = tempfile::tempdir().unwrap(); diff --git a/crates/coa-core/src/clientdl.rs b/crates/coa-core/src/clientdl.rs new file mode 100644 index 0000000..f6ca7be --- /dev/null +++ b/crates/coa-core/src/clientdl.rs @@ -0,0 +1,576 @@ +//! Download and update of the game client from the community launcher's public manifest. +//! +//! The manifest lists every file with its size and SHA-256; each file is fetched by hash and verified before it is +//! moved into place (`download::fetch_with`, resumable). The Manager keeps a small state file inside the client +//! folder (`.coa-manager/client-state.json`) so that later checks are cheap and so that files the player changed +//! are recognised: they are never overwritten without being asked, and a replaced file is moved aside, not deleted. +//! WTF, Cache, other addons, logs and `realmlist.wtf` are not part of what the Manager synchronises. + +use std::collections::{BTreeMap, HashMap}; +use std::fs::{self, File}; +use std::io::Read; +use std::path::{Path, PathBuf}; +use std::time::{Instant, UNIX_EPOCH}; + +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; + +use crate::download::{self, Cancel, HttpTransport, Job, Transport}; +use crate::error::{Error, Result}; +use crate::fsx; + +pub const MANIFEST_URL: &str = "https://launcher-api.coa-development.org/downloads/client/latest.json"; +pub const OBJECTS_URL: &str = "https://launcher-api.coa-development.org/downloads/client/objects"; +const STATE_DIR: &str = ".coa-manager"; +const MAX_MANIFEST_BYTES: u64 = 16 * 1024 * 1024; +const MAX_FILE_BYTES: u64 = 16 * 1024 * 1024 * 1024; + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct ManifestFile { + pub path: String, + pub size: u64, + pub sha256: String, +} + +#[derive(Debug, Clone, Serialize)] +pub struct Manifest { + pub version: String, + pub published_at: Option, + pub files: Vec, +} + +impl Manifest { + pub fn total_bytes(&self) -> u64 { + self.files.iter().map(|f| f.size).sum() + } +} + +#[derive(Deserialize)] +struct RawManifest { + schema: u32, + version: String, + #[serde(rename = "publishedAt")] + published_at: Option, + files: Vec, +} + +/// Things the Manager never writes, even if a manifest lists them: logs, the realmlist, the player's own settings, +/// caches and the companion addon (those have their own safe paths). +fn is_excluded(path: &str) -> bool { + let p = path.replace('\\', "/").to_ascii_lowercase(); + p.ends_with(".log") + || p.rsplit('/').next() == Some("realmlist.wtf") + || p.starts_with("wtf/") + || p.starts_with("cache/") + || p.starts_with("errors/") + || p.starts_with("logs/") + || p.starts_with(".coa-manager/") + || p.starts_with("interface/addons/coabotui/") +} + +pub fn parse_manifest(text: &str) -> Result { + let raw: RawManifest = serde_json::from_str(text).map_err(|e| Error::Invalid(format!("The client list could not be read: {e}")))?; + if raw.schema != 1 { + return Err(Error::Invalid(format!("The client list uses a newer format ({}); update the Manager.", raw.schema))); + } + if raw.version.trim().is_empty() || raw.version.len() > 64 { + return Err(Error::Invalid("The client list has no version.".into())); + } + let root = Path::new("client-root"); + let mut files = Vec::with_capacity(raw.files.len()); + for f in raw.files { + fsx::safe_join(root, &f.path)?; + let hash_ok = f.sha256.len() == 64 && f.sha256.bytes().all(|b| b.is_ascii_hexdigit()); + if !hash_ok || f.size > MAX_FILE_BYTES { + return Err(Error::Invalid(format!("The client list has a bad entry for {}.", f.path))); + } + if is_excluded(&f.path) { + continue; + } + files.push(ManifestFile { path: f.path.replace('\\', "/"), size: f.size, sha256: f.sha256.to_ascii_lowercase() }); + } + if files.is_empty() { + return Err(Error::Invalid("The client list is empty.".into())); + } + Ok(Manifest { version: raw.version, published_at: raw.published_at, files }) +} + +pub fn fetch_manifest(transport: &dyn Transport, url: &str) -> Result { + let mut reply = transport.get(url, 0).map_err(|e| Error::Invalid(format!("Could not reach the client download server: {e}")))?; + if reply.status != 200 { + return Err(Error::Invalid(format!("The client download server answered {}.", reply.status))); + } + let mut text = String::new(); + reply.body.by_ref().take(MAX_MANIFEST_BYTES).read_to_string(&mut text).map_err(|e| Error::Invalid(format!("Could not read the client list: {e}")))?; + parse_manifest(&text) +} + +pub fn fetch_latest() -> Result { + download::check_url(MANIFEST_URL)?; + fetch_manifest(&HttpTransport::with_total_timeout(30)?, MANIFEST_URL) +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Entry { + pub size: u64, + pub mtime: u64, + pub sha256: String, + /// The player chose to keep this file although the manifest has a different one (this is the manifest hash + /// they were asked about; a newer manifest asks again). + #[serde(default)] + pub kept_against: Option, +} + +#[derive(Debug, Clone, Default, Serialize, Deserialize)] +pub struct State { + /// The manifest version this folder was brought to; None while a download is unfinished. + pub version: Option, + pub files: BTreeMap, +} + +fn state_path(client: &Path) -> PathBuf { + client.join(STATE_DIR).join("client-state.json") +} + +pub fn load_state(client: &Path) -> Option { + fsx::read_json(&state_path(client)).ok() +} + +fn save_state(client: &Path, state: &State) -> Result<()> { + fsx::atomic_write_json(&state_path(client), state) +} + +#[derive(Debug, Clone, Serialize)] +pub struct Local { + /// The Manager downloaded or adopted this folder (a state file exists). + pub managed: bool, + pub version: Option, +} + +pub fn local(client: &Path) -> Local { + let s = load_state(client); + Local { managed: s.is_some(), version: s.and_then(|s| s.version) } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum Kind { + /// Not in the folder. + Missing, + /// Still as the Manager left it; the manifest has a newer file. + Changed, + /// Different from the manifest and not as the Manager left it (changed by the player or by something else). + Modified, +} + +#[derive(Debug, Clone, Serialize)] +pub struct Item { + pub path: String, + pub size: u64, + pub kind: Kind, + #[serde(skip)] + sha256: String, +} + +#[derive(Debug, Clone, Serialize)] +pub struct Plan { + pub version: String, + pub items: Vec, + pub download_bytes: u64, + pub total_files: usize, + pub up_to_date_files: usize, + pub kept_files: usize, + #[serde(skip)] + confirmed: BTreeMap, +} + +impl Plan { + pub fn modified(&self) -> Vec<&Item> { + self.items.iter().filter(|i| i.kind == Kind::Modified).collect() + } +} + +#[derive(Debug, Clone, Serialize)] +pub struct Step { + /// "scan" while comparing files, "download" while fetching, "finish" at the end. + pub phase: &'static str, + pub done: u64, + pub total: u64, + pub bytes_per_sec: u64, + pub file: Option, +} + +fn cancelled() -> Error { + Error::Invalid("Download cancelled.".into()) +} + +fn mtime_secs(m: &fs::Metadata) -> u64 { + m.modified().ok().and_then(|t| t.duration_since(UNIX_EPOCH).ok()).map(|d| d.as_secs()).unwrap_or(0) +} + +fn hash_file(path: &Path, cancel: &Cancel, on_bytes: &mut dyn FnMut(u64)) -> Result { + let mut f = File::open(path)?; + let mut hasher = Sha256::new(); + let mut buf = vec![0u8; 1 << 20]; + loop { + if cancel.is_set() { + return Err(cancelled()); + } + let n = f.read(&mut buf)?; + if n == 0 { + return Ok(hex::encode(hasher.finalize())); + } + hasher.update(&buf[..n]); + on_bytes(n as u64); + } +} + +/// Compare the folder with the manifest. Files are hashed only when their size or modification time no longer +/// matches what the state file recorded, so a routine check of a managed folder is quick. +pub fn plan(client: &Path, m: &Manifest, state: &State, cancel: &Cancel, on_step: &dyn Fn(Step)) -> Result { + let total = m.total_bytes(); + let mut done = 0u64; + let mut items = Vec::new(); + let mut confirmed = BTreeMap::new(); + let (mut up_to_date, mut kept) = (0usize, 0usize); + for f in &m.files { + if cancel.is_set() { + return Err(cancelled()); + } + on_step(Step { phase: "scan", done, total, bytes_per_sec: 0, file: Some(f.path.clone()) }); + let path = fsx::safe_join(client, &f.path)?; + let meta = match fs::metadata(&path) { + Ok(meta) if meta.is_file() => meta, + _ => { + items.push(Item { path: f.path.clone(), size: f.size, kind: Kind::Missing, sha256: f.sha256.clone() }); + done += f.size; + continue; + } + }; + let (size, mtime) = (meta.len(), mtime_secs(&meta)); + let entry = state.files.get(&f.path); + let trusted = entry.filter(|e| e.size == size && e.mtime == mtime); + let local: Option = if let Some(e) = trusted { + Some(e.sha256.clone()) + } else if size == f.size || entry.map(|e| e.size == size).unwrap_or(false) { + let base = done; + let mut seen = 0u64; + let h = hash_file(&path, cancel, &mut |n| { + seen += n; + on_step(Step { phase: "scan", done: base + seen.min(f.size), total, bytes_per_sec: 0, file: Some(f.path.clone()) }); + })?; + Some(h) + } else { + None + }; + done += f.size; + if local.as_deref() == Some(f.sha256.as_str()) { + up_to_date += 1; + confirmed.insert(f.path.clone(), Entry { size, mtime, sha256: f.sha256.clone(), kept_against: None }); + continue; + } + let as_left = entry.filter(|e| local.as_deref() == Some(e.sha256.as_str())); + match as_left { + Some(e) if e.kept_against.as_deref() == Some(f.sha256.as_str()) => { + kept += 1; + confirmed.insert(f.path.clone(), Entry { size, mtime, sha256: e.sha256.clone(), kept_against: e.kept_against.clone() }); + } + Some(e) if e.kept_against.is_none() => { + items.push(Item { path: f.path.clone(), size: f.size, kind: Kind::Changed, sha256: f.sha256.clone() }); + } + _ => items.push(Item { path: f.path.clone(), size: f.size, kind: Kind::Modified, sha256: f.sha256.clone() }), + } + } + on_step(Step { phase: "scan", done: total, total, bytes_per_sec: 0, file: None }); + let download_bytes = items.iter().map(|i| i.size).sum(); + Ok(Plan { version: m.version.clone(), items, download_bytes, total_files: m.files.len(), up_to_date_files: up_to_date, kept_files: kept, confirmed }) +} + +pub struct Apply<'a> { + pub client: &'a Path, + pub manifest: &'a Manifest, + pub plan: &'a Plan, + /// Leave files the player changed alone (they are remembered, and asked about again only when the manifest changes). + pub keep_modified: bool, + pub transport: &'a dyn Transport, + pub objects_url: &'a str, + pub cancel: &'a Cancel, +} + +fn blocked(path: &str, e: std::io::Error) -> Error { + Error::Invalid(format!("Could not replace {path}: {e}. Close the game and anything else using the client, then try again.")) +} + +/// Download what the plan lists. Progress is saved after every file, so an interrupted run resumes where it stopped. +pub fn apply(a: &Apply, on_step: &dyn Fn(Step)) -> Result<()> { + let mut state = State { version: None, files: a.plan.confirmed.clone() }; + save_state(a.client, &state)?; + + let mut todo = Vec::new(); + for item in &a.plan.items { + if item.kind == Kind::Modified && a.keep_modified { + let path = fsx::safe_join(a.client, &item.path)?; + let meta = fs::metadata(&path)?; + let sha = hash_file(&path, a.cancel, &mut |_| {})?; + state.files.insert(item.path.clone(), Entry { size: meta.len(), mtime: mtime_secs(&meta), sha256: sha, kept_against: Some(item.sha256.clone()) }); + } else { + todo.push(item); + } + } + let total: u64 = todo.iter().map(|i| i.size).sum(); + fsx::require_space(a.client, total)?; + + let staging = a.client.join(STATE_DIR).join("staging"); + let replaced = a.client.join(STATE_DIR).join("replaced").join(chrono::Utc::now().format("%Y%m%d-%H%M%S").to_string()); + let mut done = 0u64; + let mut placed: HashMap<&str, PathBuf> = HashMap::new(); + for item in todo { + if a.cancel.is_set() { + save_state(a.client, &state)?; + return Err(cancelled()); + } + let target = fsx::safe_join(a.client, &item.path)?; + let started = Instant::now(); + let staged = staging.join(&item.sha256); + let copy_from = placed.get(item.sha256.as_str()).cloned(); + if copy_from.is_none() { + let job = Job { url: format!("{}/{}", a.objects_url, item.sha256), dest: staged.clone(), sha256: item.sha256.clone(), size: item.size }; + let base = done; + let result = download::fetch_with(a.transport, &job, a.cancel, &|p| { + on_step(Step { phase: "download", done: base + p.downloaded, total, bytes_per_sec: p.bytes_per_sec, file: Some(item.path.clone()) }); + }); + if let Err(e) = result { + let _ = save_state(a.client, &state); + return Err(e); + } + } + if let Some(parent) = target.parent() { + fs::create_dir_all(parent)?; + } + if target.exists() { + if item.kind == Kind::Modified { + let keep = fsx::safe_join(&replaced, &item.path)?; + if let Some(parent) = keep.parent() { + fs::create_dir_all(parent)?; + } + fs::rename(&target, &keep).map_err(|e| blocked(&item.path, e))?; + } else { + fs::remove_file(&target).map_err(|e| blocked(&item.path, e))?; + } + } + match copy_from { + Some(src) => { + fs::copy(&src, &target).map_err(|e| blocked(&item.path, e))?; + } + None => fs::rename(&staged, &target).map_err(|e| blocked(&item.path, e))?, + } + placed.insert(&item.sha256, target.clone()); + let meta = fs::metadata(&target)?; + state.files.insert(item.path.clone(), Entry { size: meta.len(), mtime: mtime_secs(&meta), sha256: item.sha256.clone(), kept_against: None }); + save_state(a.client, &state)?; + done += item.size; + let secs = started.elapsed().as_secs_f64().max(0.001); + on_step(Step { phase: "download", done, total, bytes_per_sec: (item.size as f64 / secs) as u64, file: Some(item.path.clone()) }); + } + state.version = Some(a.manifest.version.clone()); + save_state(a.client, &state)?; + let _ = fs::remove_dir_all(&staging); + on_step(Step { phase: "finish", done: total, total, bytes_per_sec: 0, file: None }); + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::download::Reply; + use std::io::Cursor; + use std::sync::atomic::{AtomicUsize, Ordering}; + use std::sync::Mutex; + + /// Serves objects by hash; remembers what was requested and can fail after N objects. + struct Server { + objects: Mutex>>, + requested: Mutex>, + cancel_after: Option<(AtomicUsize, Cancel)>, + } + + impl Server { + fn new(files: &[(&str, &[u8])]) -> Self { + let s = Server { objects: Default::default(), requested: Default::default(), cancel_after: None }; + for (_, body) in files { + s.objects.lock().unwrap().insert(fsx::sha256_bytes(body), body.to_vec()); + } + s + } + } + + impl Transport for Server { + fn get(&self, url: &str, from: u64) -> std::result::Result { + let sha = url.rsplit('/').next().unwrap().to_string(); + self.requested.lock().unwrap().push(sha.clone()); + if let Some((left, cancel)) = &self.cancel_after { + if left.fetch_sub(1, Ordering::SeqCst) == 1 { + cancel.cancel(); + } + } + let body = self.objects.lock().unwrap().get(&sha).cloned().ok_or("404")?; + let (status, start) = if from > 0 { (206, from as usize) } else { (200, 0) }; + let cr = (from > 0).then(|| format!("bytes {from}-{}/{}", body.len() - 1, body.len())); + Ok(Reply { status, content_range: cr, body: Box::new(Cursor::new(body[start..].to_vec())) }) + } + } + + fn manifest(version: &str, files: &[(&str, &[u8])]) -> Manifest { + Manifest { + version: version.into(), + published_at: None, + files: files.iter().map(|(p, b)| ManifestFile { path: p.to_string(), size: b.len() as u64, sha256: fsx::sha256_bytes(b) }).collect(), + } + } + + const V1: [(&str, &[u8]); 4] = [("Wow.exe", b"exe-1"), ("Data/common.MPQ", b"common-mpq-data"), ("Data/patch-A.MPQ", b"patch-a-1"), ("Data/enUS/locale.MPQ", b"locale")]; + + fn sync(dir: &Path, m: &Manifest, server: &Server, keep: bool) -> Result { + let state = load_state(dir).unwrap_or_default(); + let cancel = Cancel::default(); + let p = plan(dir, m, &state, &cancel, &|_| {})?; + apply(&Apply { client: dir, manifest: m, plan: &p, keep_modified: keep, transport: server, objects_url: "https://x.invalid/objects", cancel: &cancel }, &|_| {})?; + Ok(p) + } + + #[test] + fn manifest_rejects_traversal_and_bad_entries_and_skips_personal_files() { + let ok = r#"{"schema":1,"version":"v1","files":[ + {"path":"Wow.exe","size":3,"sha256":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, + {"path":"Data/enUS/realmlist.wtf","size":3,"sha256":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, + {"path":"WTF/Config.wtf","size":3,"sha256":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, + {"path":"Interface/AddOns/CoABotUI/x.lua","size":3,"sha256":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, + {"path":"MemoryBridge.log","size":3,"sha256":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}]}"#; + let m = parse_manifest(ok).unwrap(); + assert_eq!(m.files.iter().map(|f| f.path.as_str()).collect::>(), ["Wow.exe"]); + for bad in [ + r#"{"schema":1,"version":"v","files":[{"path":"../evil.dll","size":1,"sha256":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}]}"#, + r#"{"schema":1,"version":"v","files":[{"path":"C:/evil.dll","size":1,"sha256":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}]}"#, + r#"{"schema":1,"version":"v","files":[{"path":"a.dll","size":1,"sha256":"short"}]}"#, + r#"{"schema":2,"version":"v","files":[]}"#, + r#"{"schema":1,"version":"v","files":[]}"#, + ] { + assert!(parse_manifest(bad).is_err(), "{bad}"); + } + } + + #[test] + fn fresh_download_installs_every_file_verified_and_leaves_nothing_behind() { + let d = tempfile::tempdir().unwrap(); + let (m, s) = (manifest("v1", &V1), Server::new(&V1)); + let p = sync(d.path(), &m, &s, false).unwrap(); + assert_eq!(p.items.len(), 4); + for (path, body) in V1 { + assert_eq!(fs::read(d.path().join(path)).unwrap(), body); + } + let st = load_state(d.path()).unwrap(); + assert_eq!(st.version.as_deref(), Some("v1")); + assert_eq!(st.files.len(), 4); + assert!(!d.path().join(".coa-manager/staging").exists()); + assert!(local(d.path()).managed); + } + + #[test] + fn an_update_downloads_only_what_changed_and_a_second_check_is_empty() { + let d = tempfile::tempdir().unwrap(); + sync(d.path(), &manifest("v1", &V1), &Server::new(&V1), false).unwrap(); + let mut v2 = V1.to_vec(); + v2[2] = ("Data/patch-A.MPQ", b"patch-a-2-longer"); + v2.push(("Data/new.MPQ", b"brand new")); + let (m2, s2) = (manifest("v2", &v2), Server::new(&v2)); + let p = sync(d.path(), &m2, &s2, false).unwrap(); + let kinds: Vec<_> = p.items.iter().map(|i| (i.path.as_str(), i.kind)).collect(); + assert_eq!(kinds, [("Data/patch-A.MPQ", Kind::Changed), ("Data/new.MPQ", Kind::Missing)]); + assert_eq!(s2.requested.lock().unwrap().len(), 2, "unchanged files were not downloaded again"); + assert_eq!(fs::read(d.path().join("Data/patch-A.MPQ")).unwrap(), b"patch-a-2-longer"); + assert!(!d.path().join(".coa-manager/replaced").exists(), "a file the Manager itself wrote needs no backup"); + let again = sync(d.path(), &m2, &s2, false).unwrap(); + assert!(again.items.is_empty() && again.up_to_date_files == 5); + assert_eq!(load_state(d.path()).unwrap().version.as_deref(), Some("v2")); + } + + #[test] + fn a_file_the_player_changed_is_asked_about_kept_when_told_to_and_saved_when_replaced() { + let d = tempfile::tempdir().unwrap(); + sync(d.path(), &manifest("v1", &V1), &Server::new(&V1), false).unwrap(); + fs::write(d.path().join("Wow.exe"), b"my patched exe").unwrap(); + let mut v2 = V1.to_vec(); + v2[0] = ("Wow.exe", b"exe-2"); + let (m2, s2) = (manifest("v2", &v2), Server::new(&v2)); + + let p = sync(d.path(), &m2, &s2, true).unwrap(); + assert_eq!(p.modified().len(), 1); + assert_eq!(fs::read(d.path().join("Wow.exe")).unwrap(), b"my patched exe", "kept"); + assert!(s2.requested.lock().unwrap().is_empty(), "nothing needed downloading"); + let quiet = sync(d.path(), &m2, &s2, true).unwrap(); + assert!(quiet.items.is_empty() && quiet.kept_files == 1, "the decision is remembered"); + + let mut v3 = V1.to_vec(); + v3[0] = ("Wow.exe", b"exe-3"); + let (m3, s3) = (manifest("v3", &v3), Server::new(&v3)); + let again = sync(d.path(), &m3, &s3, false).unwrap(); + assert_eq!(again.modified().len(), 1, "a newer client asks again"); + assert_eq!(fs::read(d.path().join("Wow.exe")).unwrap(), b"exe-3"); + let saved = fs::read_dir(d.path().join(".coa-manager/replaced")).unwrap().next().unwrap().unwrap().path(); + assert_eq!(fs::read(saved.join("Wow.exe")).unwrap(), b"my patched exe", "the player's version was moved aside, not lost"); + } + + #[test] + fn an_existing_client_without_state_is_compared_by_content() { + let d = tempfile::tempdir().unwrap(); + for (p, b) in V1 { + let path = d.path().join(p); + fs::create_dir_all(path.parent().unwrap()).unwrap(); + fs::write(path, b).unwrap(); + } + fs::write(d.path().join("Data/patch-A.MPQ"), b"patch-a-X").unwrap(); // same size, different content + fs::remove_file(d.path().join("Data/enUS/locale.MPQ")).unwrap(); + fs::create_dir_all(d.path().join("WTF")).unwrap(); + fs::write(d.path().join("WTF/Config.wtf"), b"mine").unwrap(); + let m = manifest("v1", &V1); + let p = plan(d.path(), &m, &State::default(), &Cancel::default(), &|_| {}).unwrap(); + let kinds: Vec<_> = p.items.iter().map(|i| (i.path.as_str(), i.kind)).collect(); + assert_eq!(kinds, [("Data/patch-A.MPQ", Kind::Modified), ("Data/enUS/locale.MPQ", Kind::Missing)]); + assert_eq!(p.up_to_date_files, 2); + assert_eq!(fs::read(d.path().join("WTF/Config.wtf")).unwrap(), b"mine"); + } + + #[test] + fn cancel_keeps_finished_files_and_the_next_run_only_fetches_the_rest() { + let d = tempfile::tempdir().unwrap(); + let m = manifest("v1", &V1); + let mut s = Server::new(&V1); + let cancel = Cancel::default(); + s.cancel_after = Some((AtomicUsize::new(3), cancel.clone())); + let state = State::default(); + let p = plan(d.path(), &m, &state, &cancel, &|_| {}).unwrap(); + let r = apply(&Apply { client: d.path(), manifest: &m, plan: &p, keep_modified: false, transport: &s, objects_url: "https://x.invalid/o", cancel: &cancel }, &|_| {}); + assert!(r.is_err()); + let st = load_state(d.path()).unwrap(); + assert!(st.version.is_none() && !st.files.is_empty() && st.files.len() < 4, "unfinished but remembered: {}", st.files.len()); + assert!(local(d.path()).managed && local(d.path()).version.is_none()); + + let s2 = Server::new(&V1); + sync(d.path(), &m, &s2, false).unwrap(); + assert_eq!(s2.requested.lock().unwrap().len(), 4 - st.files.len(), "only the missing files were requested"); + assert_eq!(load_state(d.path()).unwrap().version.as_deref(), Some("v1")); + } + + #[test] + fn a_wrong_object_never_reaches_the_client_folder() { + let d = tempfile::tempdir().unwrap(); + let m = manifest("v1", &V1); + let s = Server::new(&V1); + // the server returns different bytes under the right hash + let sha = fsx::sha256_bytes(b"exe-1"); + s.objects.lock().unwrap().insert(sha, b"EVIL!".to_vec()); + assert!(sync(d.path(), &m, &s, false).is_err()); + assert!(!d.path().join("Wow.exe").exists()); + assert!(load_state(d.path()).unwrap().version.is_none()); + } +} diff --git a/crates/coa-core/src/download.rs b/crates/coa-core/src/download.rs index 565c451..a7a3eaf 100644 --- a/crates/coa-core/src/download.rs +++ b/crates/coa-core/src/download.rs @@ -35,7 +35,7 @@ impl Cancel { pub fn cancel(&self) { self.0.store(true, Ordering::SeqCst); } - fn is_set(&self) -> bool { + pub fn is_set(&self) -> bool { self.0.load(Ordering::SeqCst) } } @@ -70,6 +70,17 @@ pub trait Transport { pub struct HttpTransport(reqwest::blocking::Client); impl HttpTransport { + /// For small documents (manifests): gives up when the server stalls instead of waiting forever. + pub fn with_total_timeout(secs: u64) -> Result { + reqwest::blocking::Client::builder() + .connect_timeout(Duration::from_secs(15)) + .timeout(Duration::from_secs(secs)) + .redirect(reqwest::redirect::Policy::limited(5)) + .build() + .map(HttpTransport) + .map_err(|e| Error::Invalid(e.to_string())) + } + pub fn new() -> Result { reqwest::blocking::Client::builder() .connect_timeout(Duration::from_secs(15)) @@ -130,6 +141,10 @@ fn attempt(client: &dyn Transport, job: &Job, part: &Path, cancel: &Cancel, on_p have = 0; } if have == job.size { + if !part.exists() { + // an empty file: nothing to request + File::create(part)?; + } return finish(job, part, hasher); } @@ -378,6 +393,16 @@ mod tests { assert!(part_path(&j.dest).exists(), "partial data is kept for the next try"); } + #[test] + fn an_empty_file_needs_no_request_and_is_still_created() { + let dir = tempfile::tempdir().unwrap(); + let j = job(dir.path(), b""); + let m = Mock::new(Vec::new(), 0, true); + fetch_with(&m, &j, &Cancel::default(), &|_| {}).unwrap(); + assert_eq!(fs::read(&j.dest).unwrap(), Vec::::new()); + assert_eq!(m.calls.load(Ordering::SeqCst), 0); + } + #[test] fn only_https_or_loopback_http_is_allowed() { assert!(check_url("https://github.com/x").is_ok()); diff --git a/crates/coa-core/src/lib.rs b/crates/coa-core/src/lib.rs index a0f00e3..30ac248 100644 --- a/crates/coa-core/src/lib.rs +++ b/crates/coa-core/src/lib.rs @@ -2,6 +2,7 @@ pub mod backup; pub mod cleanbase; pub mod companions; pub mod client; +pub mod clientdl; pub mod config; pub mod console; pub mod db; diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 89e69b8..65d621a 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -277,9 +277,10 @@ Advanced override, snapshot first. ### D9. Client integration -The client is a separate object. Manager only: detect (`Wow.exe`/`Ascension.exe` + `Data\`), read/write -`realmlist.wtf` after backing it up, and copy `Interface\AddOns\CoABotUI` (backing that one directory up only if -its files differ from the manifest). No touching of `WTF`, `Cache`, other AddOns, or the exe. +The client is a separate object. By default the Manager only detects it (`Wow.exe`/`Ascension.exe` + `Data\`), reads/writes +`realmlist.wtf` after backing it up, and copies `Interface\AddOns\CoABotUI` (backing that one directory up only if +its files differ from the manifest). No touching of `WTF`, `Cache`, other AddOns, or the exe. Downloading or updating +a client is opt-in and described in section 9 (game client download); an existing client is never changed without asking. *Reference machine note:* `C:\games\Ascension` is a heavily modified Ascension client (custom `d3d9.dll`, renderer files, many `.bak`s) — client detection must be strictly read-only + additive. @@ -518,13 +519,22 @@ The reference install classifies as **Healthy (customised)**: repack shape, bina Fixed: `clean-base` restores quiet templates, `assemble-tree` ships `coa.conf.dist`, and the Manager creates missing module `.conf` files from their `.dist` on install and before each start of a Manager-installed server. -* **Game client download through the Manager** (idea 2026-10-01, researched, not built). The community PTR launcher - ("Conquest of AzerothCore" Electron app from `ConquestOfAzerothSetup-0.6.9.exe`) reads its client from +* **Game client download and update through the Manager** (built 2026-10-01, `coa-core::clientdl`). The community PTR launcher + ("Conquest of AzerothCore" Electron app) reads its client from `https://launcher-api.coa-development.org/downloads/client/latest.json` (`{schema, version, publishedAt, files:[{path,size,sha256}]}`; - 200 files, 43.2 GiB: `Ascension.exe`, DLLs and ~40 big `Data/*.MPQ`) and fetches each file as the content-addressed object - `.../downloads/client/objects/` (Cloudflare, `Accept-Ranges`, immutable cache), 3 attempts, 30 s stall timeout, `.part` - files, sha256 check, resume via `Range`. The Manager already has everything needed (resumable verified downloads, free-space - check): a "Download game client" card with folder choice, overall progress, update check against `latest.json`, and the - existing safety rule (the Manager only ever writes its own client folder, never an existing one without asking). Open points: - ask the launcher/community maintainers before putting load on their API; DBCs live inside the MPQ patch archives (extracting - them would need an MPQ reader), so the server's `Data\dbc` still comes from our `base` package for now. + ~200 files, 43 GiB: `Ascension.exe`, DLLs and ~40 big `Data/*.MPQ`) and fetches each file as the content-addressed object + `.../downloads/client/objects/`. The Manager uses the same endpoints with its own resumable, hash-verified downloader: + * **State** lives in the client folder (`.coa-manager/client-state.json`: manifest version, per file size/mtime/sha256). A + routine check (every 5 minutes, like the server) is one small manifest request compared with the recorded version; no game file is read. + * **Plan, then apply** (like server updates): files are compared by size and mtime against the state, and hashed only when those + no longer match. Each file is `Missing`, `Changed` (still as the Manager left it) or `Modified` (the player changed it, e.g. a + custom `d3d9.dll` or a patched exe). Modified files are kept on request (and remembered: asked again only when the published file + changes) or replaced after being moved to `.coa-manager/replaced/