valheim-mod-manager 0.4.0

Mod manager for Valheim game
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
use crate::error::AppResult;
use crate::target::{GAME, Target};
use thunderstore_engine::client::ThunderstoreClient;
use thunderstore_engine::ecosystem::Ecosystem;
use thunderstore_engine::profile::{self, modlist};

/// Installs each mod and its full dependency closure into the target.
///
/// The engine resolves, downloads, extracts into the shared package cache under
/// `target.base`, installs into `target.dir`, and upserts `mods.yml` plus any
/// `_state` tracker. Nothing here duplicates that pipeline.
///
/// A mod whose recorded version is the one that would be installed, with its
/// files still in place, is skipped and reported as already installed. `force`
/// reinstalls the named mods regardless, removing their existing files first; it
/// never reaches their dependencies, which are still skipped when current.
///
/// `mods` is passed as both the desired set and the explicit set, so a mod
/// named on the command line is left enabled: asking for it by name is a
/// request to have it active. One that was disabled is reported as enabled.
/// Only an incidentally reinstalled dependency keeps its disabled state, which
/// `install_batch` restores after the whole batch, even when the batch failed
/// partway.
pub async fn run(
  client: &ThunderstoreClient,
  eco: &Ecosystem,
  target: &Target,
  mods: &[String],
  force: bool,
) -> AppResult<()> {
  // Read the record first: the planner below reads the same file immediately
  // after, but reading it here first means an unreadable mods.yml speaks in
  // vmm's advice voice instead of the engine's raw error surfacing from inside
  // the planner. It is also the before side of the enabled-state report.
  let before = super::read_modlist(target)?;

  // `InstallBatch` is non-exhaustive, so the planned batch is adjusted in place
  // rather than rebuilt with struct-update syntax.
  let mut batch = profile::plan_install_batch(&target.dir, mods, mods)?;

  batch.force = force;

  let index = client.get_manifest().await?;

  let outcome = profile::install_batch(
    &target.dir,
    &target.base,
    eco,
    &index,
    client,
    GAME,
    &batch,
    thunderstore_engine::profile::modlist::now_millis(),
  )
  .await?;

  // Read once for every line below. The install has already happened, so an
  // unreadable record must not abort the report before `report_batch_failures`
  // gets to say what failed; each name is then printed without its version.
  let recorded = modlist::read(&target.dir).unwrap_or_default();

  super::report_recorded(&recorded, "installed", &outcome.succeeded);
  super::report_unchanged(&recorded, &outcome.unchanged);

  // A named mod that was disabled is enabled by the install, even when it was
  // skipped as current, which would otherwise read as nothing having changed.
  super::report_state_changes(&before, &recorded, &batch.enable_requested);

  for full_name in &batch.protect_disabled {
    if outcome.succeeded.contains(full_name) {
      println!("kept {full_name} disabled");
    }
  }

  super::report_batch_failures(&outcome)
}

#[cfg(test)]
mod tests {
  use super::*;
  use crate::test_support::Fixture;
  use thunderstore_engine::ecosystem::Ecosystem;
  use thunderstore_engine::profile::modlist;
  use tokio::runtime::Runtime;

  #[test]
  fn install_places_files_and_records_mods_yml() {
    let fixture = Fixture::new();
    let target = fixture.target();
    let eco = Ecosystem::bundled();

    Runtime::new()
      .unwrap()
      .block_on(run(
        &fixture.client,
        &eco,
        &target,
        &["Owner-ModA".to_string()],
        false,
      ))
      .unwrap();

    assert!(
      target
        .dir
        .join("BepInEx/plugins/Owner-ModA/ModA.dll")
        .exists()
    );

    let mods = modlist::read(&target.dir).unwrap();

    assert_eq!(mods.len(), 1);
    assert_eq!(mods[0].name, "Owner-ModA");
    assert!(mods[0].enabled);
  }

  #[test]
  fn install_extracts_into_the_base_cache_not_the_target() {
    let fixture = Fixture::new();
    let target = fixture.target();
    let eco = Ecosystem::bundled();

    Runtime::new()
      .unwrap()
      .block_on(run(
        &fixture.client,
        &eco,
        &target,
        &["Owner-ModA".to_string()],
        false,
      ))
      .unwrap();

    // The package cache is shared across targets and must never land inside the
    // game directory.
    let cached = thunderstore_engine::profile::cache::package_cache_dir(
      &target.base,
      crate::target::GAME,
      "Owner-ModA",
      "1.0.0",
    );

    assert!(cached.join("plugins/ModA.dll").exists());
  }

  #[test]
  fn install_pulls_in_the_dependency_closure() {
    let fixture = Fixture::new();
    let target = fixture.target();
    let eco = Ecosystem::bundled();

    Runtime::new()
      .unwrap()
      .block_on(run(
        &fixture.client,
        &eco,
        &target,
        &["Owner-ModC".to_string()],
        false,
      ))
      .unwrap();

    // Owner-ModC depends on Owner-ModA-1.0.0, so both install.
    let mods = modlist::read(&target.dir).unwrap();

    assert_eq!(mods.len(), 2);
    assert!(modlist::find(&mods, "Owner-ModA").is_some());
    assert!(modlist::find(&mods, "Owner-ModC").is_some());
    assert!(
      target
        .dir
        .join("BepInEx/plugins/Owner-ModA/ModA.dll")
        .exists()
    );
    assert!(
      target
        .dir
        .join("BepInEx/plugins/Owner-ModC/ModC.dll")
        .exists()
    );
  }

  #[test]
  fn installing_a_disabled_mod_by_name_re_enables_it() {
    let fixture = Fixture::new();
    let target = fixture.target();
    let eco = Ecosystem::bundled();
    let runtime = Runtime::new().unwrap();

    runtime
      .block_on(run(
        &fixture.client,
        &eco,
        &target,
        &["Owner-ModA".to_string()],
        false,
      ))
      .unwrap();

    thunderstore_engine::profile::set_enabled_in(
      &target.dir,
      &eco,
      crate::target::GAME,
      "Owner-ModA",
      false,
    )
    .unwrap();

    // A reinstall clears the mod's folder first, so this surviving shows the
    // mod was skipped rather than reinstalled.
    let stray = target.dir.join("BepInEx/plugins/Owner-ModA/stray.json");

    std::fs::write(&stray, b"left behind").unwrap();

    // Naming a mod explicitly is a request to have it active. It is already at
    // the recorded version, so it is skipped rather than reinstalled, and is
    // enabled anyway: `run` reports that so the change is not silent.
    runtime
      .block_on(run(
        &fixture.client,
        &eco,
        &target,
        &["Owner-ModA".to_string()],
        false,
      ))
      .unwrap();

    let mods = modlist::read(&target.dir).unwrap();

    assert!(stray.exists(), "a current mod should be skipped");
    assert!(modlist::find(&mods, "Owner-ModA").unwrap().enabled);
    assert!(
      target
        .dir
        .join("BepInEx/plugins/Owner-ModA/ModA.dll")
        .exists()
    );
  }

  #[test]
  fn a_disabled_mod_reinstalled_as_a_dependency_stays_disabled() {
    let fixture = Fixture::new();
    let target = fixture.target();
    let eco = Ecosystem::bundled();
    let runtime = Runtime::new().unwrap();

    runtime
      .block_on(run(
        &fixture.client,
        &eco,
        &target,
        &["Owner-ModA".to_string()],
        false,
      ))
      .unwrap();

    thunderstore_engine::profile::set_enabled_in(
      &target.dir,
      &eco,
      crate::target::GAME,
      "Owner-ModA",
      false,
    )
    .unwrap();

    // The other half of `installing_a_disabled_mod_by_name_re_enables_it`.
    // Owner-ModC depends on Owner-ModA, so this reinstalls ModA without anyone
    // naming it, and nothing about installing ModC is a request to turn ModA
    // back on. The batch protects that state, and `run` reports each mod it kept
    // disabled so a silently inert dependency is not left to be discovered in
    // game.
    runtime
      .block_on(run(
        &fixture.client,
        &eco,
        &target,
        &["Owner-ModC".to_string()],
        false,
      ))
      .unwrap();

    let mods = modlist::read(&target.dir).unwrap();

    assert!(!modlist::find(&mods, "Owner-ModA").unwrap().enabled);
    assert!(modlist::find(&mods, "Owner-ModC").unwrap().enabled);
  }

  #[test]
  fn install_into_a_profile_never_writes_to_the_game_dir() {
    let fixture = Fixture::new();
    let target = fixture.profile_target("experiment");
    let eco = Ecosystem::bundled();

    Runtime::new()
      .unwrap()
      .block_on(run(
        &fixture.client,
        &eco,
        &target,
        &["Owner-ModA".to_string()],
        false,
      ))
      .unwrap();

    assert!(
      target
        .dir
        .join("BepInEx/plugins/Owner-ModA/ModA.dll")
        .exists()
    );
    assert_eq!(modlist::read(&target.dir).unwrap().len(), 1);
    // The point of profile mode: the game directory is untouched.
    assert!(
      !fixture
        .game_dir
        .path()
        .join("BepInEx/plugins/Owner-ModA")
        .exists()
    );
    assert!(!fixture.game_dir.path().join("mods.yml").exists());
  }

  #[test]
  fn the_fixture_can_install_a_recognised_mod_loader() {
    let fixture = Fixture::new();
    let target = fixture.target();
    let eco = Ecosystem::bundled();

    Runtime::new()
      .unwrap()
      .block_on(run(
        &fixture.client,
        &eco,
        &target,
        &["denikson-BepInExPack_Valheim".to_string()],
        false,
      ))
      .unwrap();

    // The ecosystem must recognise it as a loader, or every loader-specific
    // code path stays untested.
    assert!(
      eco
        .modloader_package("denikson-BepInExPack_Valheim")
        .is_some()
    );
    // A loader pack unpacks to the target root with its `rootFolder` stripped,
    // and is recorded as `State` so it has an install record.
    assert!(target.dir.join("winhttp.dll").exists());
    assert!(target.dir.join("BepInEx/core/BepInEx.dll").exists());
    assert!(
      thunderstore_engine::install::state_file_path(&target.dir, "denikson-BepInExPack_Valheim")
        .exists()
    );
  }

  #[test]
  fn an_unreadable_mods_yml_reports_its_path_before_installing() {
    let fixture = Fixture::new();
    let target = fixture.target();
    let eco = Ecosystem::bundled();

    std::fs::write(target.mods_yml(), "not: [valid").unwrap();

    let message = Runtime::new()
      .unwrap()
      .block_on(run(
        &fixture.client,
        &eco,
        &target,
        &["Owner-ModA".to_string()],
        false,
      ))
      .unwrap_err()
      .to_string();

    // vmm's own advice voice, the same shape `list`/`update`/`uninstall` use,
    // not the engine's raw parse error leaking through the planner.
    assert!(message.contains("the install record for"), "got: {message}");
    assert!(message.contains(&target.mods_yml().display().to_string()));
  }

  #[test]
  fn install_reports_an_unknown_mod() {
    let fixture = Fixture::new();
    let target = fixture.target();
    let eco = Ecosystem::bundled();

    let result = Runtime::new().unwrap().block_on(run(
      &fixture.client,
      &eco,
      &target,
      &["Owner-Nonexistent".to_string()],
      false,
    ));

    assert!(result.is_err());
  }

  #[test]
  fn reinstalling_a_recorded_version_without_force_leaves_it_unchanged() {
    let fixture = Fixture::new();
    let target = fixture.target();
    let eco = Ecosystem::bundled();
    let runtime = Runtime::new().unwrap();
    let mods = ["Owner-ModA".to_string()];

    runtime
      .block_on(run(&fixture.client, &eco, &target, &mods, false))
      .unwrap();

    let folder = target.dir.join("BepInEx/plugins/Owner-ModA");
    let plugin = folder.join("ModA.dll");
    let stray = folder.join("stray.json");

    std::fs::write(&plugin, b"edited").unwrap();
    std::fs::write(&stray, b"left behind").unwrap();

    runtime
      .block_on(run(&fixture.client, &eco, &target, &mods, false))
      .unwrap();

    // Already at the recorded version, so nothing is reinstalled and nothing on
    // disk is touched, including files the install never placed.
    assert_eq!(std::fs::read(&plugin).unwrap(), b"edited");
    assert!(stray.exists());
  }

  #[test]
  fn force_reinstalls_a_recorded_version_from_a_clean_folder() {
    let fixture = Fixture::new();
    let target = fixture.target();
    let eco = Ecosystem::bundled();
    let runtime = Runtime::new().unwrap();
    let mods = ["Owner-ModA".to_string()];

    runtime
      .block_on(run(&fixture.client, &eco, &target, &mods, false))
      .unwrap();

    let folder = target.dir.join("BepInEx/plugins/Owner-ModA");
    let plugin = folder.join("ModA.dll");
    // The shape issue #23 left behind: a file that belongs in a subfolder,
    // installed flat into the mod's namespaced folder.
    let stray = folder.join("german.json");

    std::fs::write(&plugin, b"edited").unwrap();
    std::fs::write(&stray, b"flattened").unwrap();

    runtime
      .block_on(run(&fixture.client, &eco, &target, &mods, true))
      .unwrap();

    assert!(!stray.exists(), "--force should clear the old folder first");
    assert_eq!(std::fs::read(&plugin).unwrap(), b"dll-bytes");
    assert!(
      modlist::find(&modlist::read(&target.dir).unwrap(), "Owner-ModA")
        .unwrap()
        .enabled
    );
  }

  #[test]
  fn force_does_not_reinstall_the_named_mods_dependencies() {
    let fixture = Fixture::new();
    let target = fixture.target();
    let eco = Ecosystem::bundled();
    let runtime = Runtime::new().unwrap();
    let mods = ["Owner-ModC".to_string()];

    runtime
      .block_on(run(&fixture.client, &eco, &target, &mods, false))
      .unwrap();

    let plugins = target.dir.join("BepInEx/plugins");
    let named_stray = plugins.join("Owner-ModC/stray.json");
    let dependency_stray = plugins.join("Owner-ModA/stray.json");

    std::fs::write(&named_stray, b"left behind").unwrap();
    std::fs::write(&dependency_stray, b"left behind").unwrap();

    runtime
      .block_on(run(&fixture.client, &eco, &target, &mods, true))
      .unwrap();

    // Owner-ModC is named, so it is reinstalled from a clean folder. Owner-ModA
    // only comes along as its dependency, and is current, so it is left alone.
    assert!(!named_stray.exists());
    assert!(dependency_stray.exists());
  }

  /// The version `mods.yml` records for `name` in `target`.
  fn recorded_version(target: &Target, name: &str) -> String {
    let mods = modlist::read(&target.dir).unwrap();

    modlist::find(&mods, name)
      .unwrap()
      .version_number
      .to_string()
  }

  #[test]
  fn installing_a_named_version_installs_exactly_that_version() {
    let fixture = Fixture::new();
    let target = fixture.target();
    let eco = Ecosystem::bundled();

    Runtime::new()
      .unwrap()
      .block_on(run(
        &fixture.client,
        &eco,
        &target,
        &["Owner-ModD-1.0.0".to_string()],
        false,
      ))
      .unwrap();

    // Owner-ModD's latest is 2.0.0, so 1.0.0 shows the requested version was
    // honored rather than discarded.
    assert_eq!(recorded_version(&target, "Owner-ModD"), "1.0.0");
  }

  #[test]
  fn installing_an_older_version_downgrades_a_newer_install() {
    let fixture = Fixture::new();
    let target = fixture.target();
    let eco = Ecosystem::bundled();
    let runtime = Runtime::new().unwrap();

    runtime
      .block_on(run(
        &fixture.client,
        &eco,
        &target,
        &["Owner-ModD".to_string()],
        false,
      ))
      .unwrap();

    assert_eq!(recorded_version(&target, "Owner-ModD"), "2.0.0");

    runtime
      .block_on(run(
        &fixture.client,
        &eco,
        &target,
        &["Owner-ModD-1.0.0".to_string()],
        false,
      ))
      .unwrap();

    assert_eq!(recorded_version(&target, "Owner-ModD"), "1.0.0");
  }

  #[test]
  fn installing_a_version_thunderstore_does_not_publish_fails() {
    let fixture = Fixture::new();
    let target = fixture.target();
    let eco = Ecosystem::bundled();

    let message = Runtime::new()
      .unwrap()
      .block_on(run(
        &fixture.client,
        &eco,
        &target,
        &["Owner-ModD-9.9.9".to_string()],
        false,
      ))
      .unwrap_err()
      .to_string();

    // Fails naming the missing version, rather than quietly installing the
    // latest in its place.
    assert!(
      message.contains("Owner-ModD has no version 9.9.9"),
      "got: {message}"
    );
    assert!(
      modlist::find(
        &modlist::read(&target.dir).unwrap_or_default(),
        "Owner-ModD"
      )
      .is_none()
    );
  }

  #[test]
  fn naming_a_disabled_mod_with_a_version_re_enables_it() {
    let fixture = Fixture::new();
    let target = fixture.target();
    let eco = Ecosystem::bundled();
    let runtime = Runtime::new().unwrap();
    let mods = ["Owner-ModD-1.0.0".to_string()];

    runtime
      .block_on(run(&fixture.client, &eco, &target, &mods, false))
      .unwrap();

    thunderstore_engine::profile::set_enabled_in(
      &target.dir,
      &eco,
      crate::target::GAME,
      "Owner-ModD",
      false,
    )
    .unwrap();

    // `installing_a_disabled_mod_by_name_re_enables_it`, with the name carrying
    // a version. The record keys on `Owner-ModD`, so the request has to match it
    // by name alone to count as explicit rather than as a dependency to keep
    // disabled.
    runtime
      .block_on(run(&fixture.client, &eco, &target, &mods, false))
      .unwrap();

    let recorded = modlist::read(&target.dir).unwrap();

    assert!(modlist::find(&recorded, "Owner-ModD").unwrap().enabled);
    assert_eq!(recorded_version(&target, "Owner-ModD"), "1.0.0");
  }
}