modde-sources 0.2.1

Download source implementations for modde
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//! High-level entry points that drive a complete Wabbajack install: build the
//! HTTP client, wire up sources, run the installer, register a profile, and
//! configure Wine DLL overrides.

use std::io::Read as _;
use std::path::{Path, PathBuf};

use anyhow::{Context, Result};
use tokio::sync::mpsc;
use tracing::{info, warn};

use modde_core::manifest::wabbajack::{
    WabbajackManifest, cache_wabbajack_file, compute_manifest_hash,
};
use modde_core::paths;
use modde_core::profile::{
    EnabledMod, LoadOrderLock, LockReason, Profile, ProfileManager, ProfileSource,
};
use modde_core::resolver::GameId;

use crate::direct::DirectSource;
use crate::nexus::NexusSource;
use crate::wabbajack::cdn::WabbajackCdnSource;
use crate::wabbajack::diagnostics::{WabbajackDiagnostics, WabbajackDiagnosticsOptions};
use crate::wabbajack::impact::{MissingArchiveImpact, MissingArchivePolicy};
use crate::wabbajack::installer::{ArchiveRetentionPolicy, InstallProgress, WabbajackInstaller};
use crate::wabbajack::staging::{StagingStore, is_compressed_path, logical_path_from_compressed};

/// User-supplied options controlling a single Wabbajack install run.
#[derive(Debug, Clone)]
pub struct WabbajackInstallOptions {
    pub path: PathBuf,
    pub profile_name: Option<String>,
    pub game_dir: Option<PathBuf>,
    pub force: bool,
    /// When true, stage the modlist but skip the final copy/deploy into
    /// `game_dir`. The directory is still used to read `GameFileSource` archives
    /// and to register a profile.
    pub no_deploy: bool,
    pub safety: WabbajackInstallSafety,
    pub diagnostics: Option<WabbajackDiagnosticsOptions>,
    pub archive_retention: ArchiveRetentionPolicy,
    pub missing_archive_policy: MissingArchivePolicy,
}

/// Safety toggles that relax or skip parts of the install pipeline.
#[derive(Debug, Clone, Copy, Default)]
pub struct WabbajackInstallSafety {
    /// When true, log per-archive download / per-directive apply failures
    /// instead of aborting. Useful for very large modlists with a handful of
    /// archives that need manual intervention.
    pub continue_on_error: bool,
    /// Explicitly discard existing staging instead of adopting it.
    pub reset_staging: bool,
    /// Skip post-apply validation before deploy.
    pub skip_validate: bool,
}

/// Result summary returned after a successful install.
#[derive(Debug, Clone)]
pub struct WabbajackInstallSummary {
    pub profile_name: String,
    pub modlist_name: String,
    pub game_id: GameId,
    pub mod_count: usize,
    pub manifest_hash: String,
}

/// Build the shared HTTP client used for Wabbajack downloads, with sensible
/// connect and request timeouts.
///
/// # Errors
///
/// Returns an error if the underlying `reqwest` client cannot be built.
pub fn build_http_client() -> Result<reqwest::Client> {
    reqwest::Client::builder()
        .timeout(std::time::Duration::from_mins(5))
        .connect_timeout(std::time::Duration::from_secs(30))
        .build()
        .context("failed to build HTTP client")
}

/// Parse the JSON manifest out of the `.wabbajack` archive at `path`.
///
/// # Errors
///
/// Returns an error if the file cannot be opened, read as a zip, or parsed.
pub fn parse_wabbajack_manifest(path: &Path) -> Result<WabbajackManifest> {
    let file = std::fs::File::open(path)
        .with_context(|| format!("failed to open wabbajack file: {}", path.display()))?;
    let mut archive = zip::ZipArchive::new(file)
        .with_context(|| format!("failed to read wabbajack archive: {}", path.display()))?;

    let entry_name = if archive.by_name("modlist").is_ok() {
        "modlist"
    } else {
        "modlist.json"
    };
    let mut entry = archive
        .by_name(entry_name)
        .context("wabbajack archive missing modlist entry")?;
    let mut json = String::new();
    entry
        .read_to_string(&mut json)
        .context("failed to read modlist entry")?;
    serde_json::from_str(&json).context("failed to parse modlist JSON")
}

pub async fn install_wabbajack(
    options: WabbajackInstallOptions,
    progress_tx: Option<mpsc::UnboundedSender<InstallProgress>>,
) -> Result<WabbajackInstallSummary> {
    info!(path = %options.path.display(), ?options.profile_name, "installing Wabbajack modlist");

    let manifest = parse_wabbajack_manifest(&options.path)?;
    let modlist_name = manifest.name.clone();
    let game_id = GameId::from(
        modde_games::normalize_wabbajack_game(&manifest.game)
            .map_or_else(|| manifest.game.to_lowercase(), String::from),
    );
    let profile_name = options
        .profile_name
        .clone()
        .unwrap_or_else(|| modlist_name.clone());

    let store = paths::store_dir();
    let staging = paths::staging_dir().join(&profile_name);
    std::fs::create_dir_all(&store)?;
    let staging_store = StagingStore::new(&staging);
    let prepare_status = if options.safety.reset_staging {
        staging_store.reset_and_prepare().await
    } else {
        staging_store.prepare_resumable().await
    }
    .context("failed to prepare Wabbajack staging layout")?;
    info!(?prepare_status, staging = %staging.display(), "prepared Wabbajack staging");

    let manifest_hash = compute_manifest_hash(&manifest);
    let client = build_http_client()?;
    let mut installer = WabbajackInstaller::new(
        manifest.clone(),
        options.path.clone(),
        store.clone(),
        staging.clone(),
    );
    if let Some(game_dir) = options.game_dir.clone() {
        installer.set_game_dir(game_dir);
    }
    installer.set_continue_on_error(options.safety.continue_on_error);
    installer.set_archive_retention(options.archive_retention);
    installer.set_missing_archive_policy(options.missing_archive_policy);
    if let Some(diagnostics_options) = options.diagnostics.clone() {
        let diagnostics = WabbajackDiagnostics::new(diagnostics_options).await?;
        installer.set_diagnostics(diagnostics);
    }

    match NexusSource::new(client.clone()) {
        Ok(nexus) => installer.add_source(crate::AnySource::Nexus(nexus)),
        Err(e) => warn!("failed to create Nexus source (no API key?): {e:#}"),
    }
    installer.add_source(crate::AnySource::WabbajackCdn(WabbajackCdnSource::new(
        client.clone(),
    )));
    installer.add_source(crate::AnySource::GitHub(crate::github::GitHubSource::new(
        client.clone(),
    )));
    installer.add_source(crate::AnySource::GoogleDrive(
        crate::gdrive::GoogleDriveSource::new(client.clone()),
    ));
    installer.add_source(crate::AnySource::Mega(crate::mega::MegaSource::new(
        client.clone(),
    )));
    installer.add_source(crate::AnySource::MediaFire(
        crate::mediafire::MediaFireSource::new(client.clone()),
    ));
    installer.add_source(crate::AnySource::Manual(crate::manual::ManualSource::new()));
    installer.add_source(crate::AnySource::Direct(DirectSource::new(client)));

    let skip_install =
        !options.force && crate::wabbajack::validator::preflight_staging(&manifest, &staging).await;

    let launcher_progress_tx = progress_tx.clone();

    if skip_install {
        if let Some(tx) = &progress_tx {
            tx.send(InstallProgress::Complete).ok();
        }
    } else {
        let (fallback_tx, mut fallback_rx) = mpsc::unbounded_channel();
        let tx = progress_tx.unwrap_or(fallback_tx);
        let drain = tokio::spawn(async move { while fallback_rx.recv().await.is_some() {} });
        installer
            .install(tx)
            .await
            .context("wabbajack install pipeline failed")?;
        drain.abort();
    }

    if !options.safety.skip_validate {
        let report = crate::wabbajack::validator::validate_install(&manifest, &staging)
            .await
            .context("Wabbajack staging validation failed")?;
        let skip_plan = MissingArchiveImpact::analyze(&manifest, &store)
            .skip_plan(&manifest, options.missing_archive_policy);
        let missing = report
            .missing
            .iter()
            .filter(|path| !skip_plan.should_omit_path(path))
            .collect::<Vec<_>>();
        let mismatches = report
            .mismatches
            .iter()
            .filter(|mismatch| !skip_plan.should_omit_path(&mismatch.path))
            .collect::<Vec<_>>();
        if !missing.is_empty() || !mismatches.is_empty() {
            anyhow::bail!(
                "Wabbajack staging validation found {} missing and {} mismatched file(s)",
                missing.len(),
                mismatches.len()
            );
        }
        info!(
            total_files = report.total_files,
            verified = report.verified,
            "Wabbajack staging validation passed"
        );
    }

    if let Some(ref game_dir) = options.game_dir
        && !options.no_deploy
    {
        deploy_mo2_to_game(&staging, game_dir, options.force)
            .await
            .context("failed to deploy mods to game directory")?;
        let launcher_report = configure_wine_overrides(&game_id, game_dir, &staging)?;
        if !launcher_report.is_empty()
            && let Some(tx) = &launcher_progress_tx
        {
            tx.send(InstallProgress::LauncherConfigured {
                report: launcher_report,
            })
            .ok();
        }
    } else if options.no_deploy {
        info!("--no-deploy set: skipping copy of staging into game directory");
    }

    let mut enabled_mods = Vec::new();
    for archive in &manifest.archives {
        enabled_mods.push(EnabledMod {
            mod_id: modde_core::scanner::archive_mod_id(archive),
            enabled: true,
            version: None,
            fomod_config: None,
            ..Default::default()
        });
    }

    let pm = ProfileManager::open().context("failed to open profile database")?;
    let profile = Profile {
        id: None,
        name: profile_name.clone(),
        game_id: game_id.clone(),
        source: ProfileSource::Wabbajack {
            manifest_hash: manifest_hash.clone(),
        },
        mods: enabled_mods,
        overrides: ProfileManager::default_overrides(&profile_name),
        load_order_rules: smallvec::SmallVec::new(),
        load_order_lock: Some(LoadOrderLock::now(LockReason::Wabbajack {
            manifest_hash: manifest_hash.clone(),
        })),
    };

    save_profile_and_settings(&pm, &profile, options.game_dir.as_deref())?;

    if let Err(e) = cache_wabbajack_file(&options.path, &manifest_hash) {
        warn!("failed to cache wabbajack source file: {e:#}");
    }

    Ok(WabbajackInstallSummary {
        profile_name,
        modlist_name,
        game_id,
        mod_count: profile.mods.len(),
        manifest_hash,
    })
}

fn save_profile_and_settings(
    pm: &ProfileManager,
    profile: &Profile,
    game_dir: Option<&Path>,
) -> Result<()> {
    pm.create_or_update(profile)?;
    let mut settings = modde_core::settings::AppSettings::load();
    if let Some(gd) = game_dir {
        settings.set_game_path(&profile.game_id, gd.to_path_buf());
    }
    settings.selected_game = Some(profile.game_id.to_string());
    settings.save();
    Ok(())
}

/// Detect DLLs that need Wine `dll-override` entries for the installed modlist
/// and apply them, returning a report of what was configured.
///
/// # Errors
///
/// Returns an error if launcher configuration fails.
pub fn configure_wine_overrides(
    game_id: &GameId,
    game_dir: &Path,
    staging: &Path,
) -> Result<modde_games::launcher::LauncherConfigurationReport> {
    let mut report = modde_games::launcher::LauncherConfigurationReport::default();
    let Some(plugin) = modde_games::resolve_game_plugin(game_id.as_str()) else {
        info!(%game_id, "no game plugin found, skipping Wine DLL override detection");
        return Ok(report);
    };

    let mut overrides = plugin.wine_dll_overrides(game_dir);
    for dll in plugin.wine_dll_overrides_from_staging(staging) {
        if !overrides.contains(&dll) {
            overrides.push(dll);
        }
    }
    if overrides.is_empty() {
        return Ok(report);
    }

    let launcher = modde_games::launcher::detect_launcher(game_dir);
    #[cfg(target_os = "linux")]
    {
        report.wine_overrides = modde_games::launcher::apply_wine_overrides(&launcher, &overrides)?;
    }

    let tool_env_vars = match modde_core::db::ModdeDb::open() {
        Ok(db) => modde_games::launcher::collect_tool_env_vars(game_id, &db).unwrap_or_default(),
        Err(_) => Vec::new(),
    };

    if let Some(wrapper) =
        modde_games::launcher::generate_launch_wrapper(game_dir, staging, game_id, &tool_env_vars)?
    {
        report.wrapper_registration =
            modde_games::launcher::register_heroic_wrapper(&launcher, &wrapper.path)?;
        report.launch_wrapper = Some(wrapper);
    }

    Ok(report)
}

pub async fn deploy_mo2_to_game(staging: &Path, game_dir: &Path, force: bool) -> Result<()> {
    let mods_dir = staging.join("mods");
    if !mods_dir.exists() {
        info!("no mods/ directory in staging, skipping deployment");
        return Ok(());
    }

    let staging_store = StagingStore::new(staging);
    let mut entries = tokio::fs::read_dir(&mods_dir).await?;
    while let Some(mod_entry) = entries.next_entry().await? {
        if !mod_entry.file_type().await?.is_dir() {
            continue;
        }
        let mod_path = mod_entry.path();
        let mut stack = vec![mod_path.clone()];
        while let Some(dir) = stack.pop() {
            let mut dir_entries = tokio::fs::read_dir(&dir).await?;
            while let Some(entry) = dir_entries.next_entry().await? {
                let file_type = entry.file_type().await?;
                let entry_path = entry.path();
                if file_type.is_dir() {
                    stack.push(entry_path);
                    continue;
                }

                let logical_entry_path = if is_compressed_path(&entry_path) {
                    logical_path_from_compressed(&entry_path)
                } else {
                    entry_path.clone()
                };
                let rel_path = logical_entry_path
                    .strip_prefix(&mod_path)
                    .unwrap_or(&logical_entry_path);
                let filename = rel_path.file_name().unwrap_or_default().to_string_lossy();
                if filename == "meta.ini" || filename == "meta.json" {
                    continue;
                }

                let dest = game_dir.join(rel_path);
                let staging_relative = logical_entry_path
                    .strip_prefix(staging)
                    .unwrap_or(&logical_entry_path)
                    .to_string_lossy()
                    .to_string();
                if is_compressed_path(&entry_path) {
                    staging_store
                        .materialize_logical_file(&staging_relative, &dest)
                        .await?;
                    tracing::debug!(src = %entry_path.display(), dst = %dest.display(), "deployed compressed staging file");
                    continue;
                }

                #[cfg(unix)]
                if !force
                    && let (Ok(src_meta), Ok(dst_meta)) = (
                        tokio::fs::metadata(&entry_path).await,
                        tokio::fs::metadata(&dest).await,
                    )
                {
                    use std::os::unix::fs::MetadataExt;
                    if src_meta.ino() == dst_meta.ino() && src_meta.dev() == dst_meta.dev() {
                        continue;
                    }
                }

                let kind = modde_core::link::link_or_copy(&entry_path, &dest).await?;
                tracing::debug!(src = %entry_path.display(), dst = %dest.display(), ?kind, "deployed file");
            }
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::wabbajack::staging::{StagingCompressionPolicy, StagingStore, compressed_path};

    #[tokio::test]
    async fn deploy_decodes_compressed_files_and_links_plain_files() {
        let temp = tempfile::tempdir().unwrap();
        let staging = temp.path().join("staging");
        let game = temp.path().join("game");
        let store = StagingStore::with_policy(
            &staging,
            StagingCompressionPolicy {
                min_bytes: 1,
                level: 1,
                suffix: ".modde-zst".to_string(),
            },
        );
        store.prepare_fresh().await.unwrap();

        let compressed_rel = "mods/TextureMod/textures/landscape/snow.dds";
        let plain_rel = "mods/PluginMod/plugin.esp";
        let compressed_path_plain = staging.join(compressed_rel);
        let plain_path = staging.join(plain_rel);
        tokio::fs::create_dir_all(compressed_path_plain.parent().unwrap())
            .await
            .unwrap();
        tokio::fs::create_dir_all(plain_path.parent().unwrap())
            .await
            .unwrap();
        let compressed_bytes = vec![3_u8; 128 * 1024];
        tokio::fs::write(&compressed_path_plain, &compressed_bytes)
            .await
            .unwrap();
        tokio::fs::write(&plain_path, b"plugin").await.unwrap();
        store.compress_eligible_files(1).await.unwrap();

        assert!(compressed_path(&compressed_path_plain).exists());
        assert!(!compressed_path_plain.exists());

        deploy_mo2_to_game(&staging, &game, false).await.unwrap();

        assert_eq!(
            tokio::fs::read(game.join("textures/landscape/snow.dds"))
                .await
                .unwrap(),
            compressed_bytes
        );
        assert_eq!(
            tokio::fs::read(game.join("plugin.esp")).await.unwrap(),
            b"plugin"
        );
    }
}