valheim-mod-manager 0.3.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
use crate::error::{AppError, AppResult};
use crate::target::{GAME, Target};
use thunderstore_engine::ecosystem::Ecosystem;
use thunderstore_engine::profile;

/// Enables or disables an installed mod.
///
/// The engine applies the on-disk mechanics for the mod's tracking method,
/// `.old` renames for subdir-routed files, `_state`-recorded deletion and
/// restore for state-tracked ones, then flips `enabled` in `mods.yml`. A mod
/// loader pack is a no-op, matching r2modman, so this reports that truthfully
/// instead of claiming a change that did not happen. Errors when the mod is
/// not installed.
pub fn run(eco: &Ecosystem, target: &Target, full_name: &str, enabled: bool) -> AppResult<()> {
  // Read the record first, not for its own sake: `set_enabled_in` 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 engine.
  super::read_modlist(target)?;

  profile::set_enabled_in(&target.dir, eco, GAME, full_name, enabled)?;

  if eco.modloader_package(full_name).is_some() {
    println!("vmm: {}", loader_not_toggled_message(full_name));

    return Ok(());
  }

  let verb = if enabled { "enabled" } else { "disabled" };

  println!("{verb} {full_name}");

  Ok(())
}

/// The advice `run` prints instead of a false "enabled"/"disabled" success when
/// `full_name` names the mod loader, matching the voice of
/// [`nothing_toggleable_message`], which already talks about the loader being
/// always active.
fn loader_not_toggled_message(full_name: &str) -> String {
  crate::error::advice_message(
    &format!("{full_name} is the mod loader, so it was not changed."),
    "It is always active with no disabled state, matching r2modman, so it \
     cannot be enabled or disabled.",
    &["vmm list"],
  )
}

/// Enables or disables every mod recorded in the target's `mods.yml`.
///
/// The engine's [`profile::plan_set_enabled_batch`] excludes the mod loader
/// from the batch entirely rather than merely skipping it: a `ToggleBatch`
/// holds a recognised loader apart in `loaders` rather than in `changing` or
/// `already`, so counting it as "already enabled" the way a mere skip would is
/// never on the table.
///
/// One mod's failure does not abort the batch. Returning early would leave a
/// half-applied toggle with nothing reported, so every failure is collected, the
/// successes are still printed, and an error naming the failures is returned at
/// the end so the exit code is non-zero.
pub fn run_all(eco: &Ecosystem, target: &Target, enabled: bool) -> AppResult<()> {
  let action = if enabled { "enable" } else { "disable" };
  let verb = if enabled { "enabled" } else { "disabled" };

  // Read the record first, not for its own sake: 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.
  super::read_modlist(target)?;

  let batch = profile::plan_set_enabled_batch(&target.dir, eco, None, enabled)?;

  if batch.changing.is_empty() && batch.already.is_empty() {
    println!(
      "vmm: {}",
      nothing_toggleable_message(target, action, batch.loaders.is_empty())
    );

    return Ok(());
  }

  let outcome = profile::set_enabled_batch(&target.dir, eco, GAME, &batch)?;

  for name in &outcome.succeeded {
    println!("{verb} {name}");
  }

  let mut failed = Vec::new();

  for (name, error) in &outcome.failed {
    eprintln!("vmm: couldn't {action} {name}: {error}");

    failed.push(name.clone());
  }

  report_toggle_all(verb, outcome.succeeded.len(), batch.already.len(), &failed)
}

/// The no-op advice `run_all` prints when nothing here can be toggled, worded
/// according to whether the target is bare or holds only a mod loader.
///
/// The two cases read differently: a bare target has nothing installed at
/// all, while a loader-only target does have something installed, just
/// nothing with a disabled state. Saying "the mod loader is always active"
/// when there is no loader, or anything else, installed would be a false
/// claim about the target.
fn nothing_toggleable_message(target: &Target, action: &str, mods_is_empty: bool) -> String {
  let detail = if mods_is_empty {
    "Nothing is installed here, so nothing was changed."
  } else {
    "Only the mod loader is installed here, and it is always active with no \
     disabled state, so nothing was changed."
  };

  crate::error::advice_message(
    &format!(
      "there is nothing to {action} in {}.",
      crate::target::describe(target)
    ),
    detail,
    &["vmm list"],
  )
}

/// Summarises a `run_all` pass, erroring when any mod failed.
fn report_toggle_all(
  verb: &str,
  changed: usize,
  unchanged: usize,
  failed: &[String],
) -> AppResult<()> {
  println!("{}.", toggle_summary(verb, changed, unchanged));

  if failed.is_empty() {
    return Ok(());
  }

  Err(AppError::advice(
    format!("{} of the mods could not be {verb}.", failed.len()),
    format!(
      "These were left as they were: {}\n\nEverything else was {verb} \
       successfully.",
      failed.join(", ")
    ),
    &["vmm list"],
  ))
}

/// Builds the summary line [`report_toggle_all`] prints, agreeing in number on
/// both counts it names: `"1 mod"` vs `"N mods"` for the count itself, and
/// `"was"` vs `"were"` for the already-toggled clause.
fn toggle_summary(verb: &str, changed: usize, unchanged: usize) -> String {
  let mut summary = format!("\n{verb} {}", super::describe_count(changed));

  if unchanged > 0 {
    let auxiliary = if unchanged == 1 { "was" } else { "were" };

    summary.push_str(&format!(
      ", {} {auxiliary} already {verb}",
      super::describe_count(unchanged)
    ));
  }

  summary
}

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

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

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

    let plugin = target.dir.join("BepInEx/plugins/Owner-ModA/ModA.dll");
    let disabled = target.dir.join("BepInEx/plugins/Owner-ModA/ModA.dll.old");

    run(&eco, &target, "Owner-ModA", false).unwrap();

    assert!(disabled.exists());
    assert!(!plugin.exists());
    assert!(!modlist::read(&target.dir).unwrap()[0].enabled);

    run(&eco, &target, "Owner-ModA", true).unwrap();

    assert!(plugin.exists());
    assert!(!disabled.exists());
    assert!(modlist::read(&target.dir).unwrap()[0].enabled);
  }

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

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

    run(&eco, &target, "Owner-ModA", false).unwrap();

    assert!(
      target
        .dir
        .join("BepInEx/plugins/Owner-ModA/ModA.dll.old")
        .exists()
    );
    assert!(!modlist::read(&target.dir).unwrap()[0].enabled);
    // The game directory is not this target: nothing was written there.
    assert!(
      !fixture
        .game_dir
        .path()
        .join("BepInEx/plugins/Owner-ModA")
        .exists()
    );
    assert!(!fixture.game_dir.path().join("mods.yml").exists());
  }

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

    assert!(run(&eco, &target, "Owner-Absent", false).is_err());
  }

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

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

    run_all(&eco, &target, false).unwrap();

    assert!(
      target
        .dir
        .join("BepInEx/plugins/Owner-ModA/ModA.dll.old")
        .exists()
    );
    assert!(
      target
        .dir
        .join("BepInEx/plugins/Owner-ModB/ModB.dll.old")
        .exists()
    );
    assert!(
      modlist::read(&target.dir)
        .unwrap()
        .iter()
        .all(|entry| !entry.enabled)
    );

    run_all(&eco, &target, true).unwrap();

    assert!(
      target
        .dir
        .join("BepInEx/plugins/Owner-ModA/ModA.dll")
        .exists()
    );
    assert!(
      modlist::read(&target.dir)
        .unwrap()
        .iter()
        .all(|entry| entry.enabled)
    );
  }

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

    // No mods.yml at all: an empty target must succeed rather than error.
    assert!(run_all(&eco, &target, false).is_ok());
  }

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

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

    let message = run(&eco, &target, "Owner-ModA", false)
      .unwrap_err()
      .to_string();

    // The same advice voice `run_all` and the other target-scoped commands
    // use, not the engine's raw parse error leaking through `set_enabled_in`.
    assert!(message.contains("the install record for"), "got: {message}");
    assert!(message.contains(&target.mods_yml().display().to_string()));
  }

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

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

    let message = run_all(&eco, &target, 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 nothing_toggleable_message_distinguishes_bare_and_loader_only_targets() {
    let fixture = Fixture::new();
    let target = fixture.target();

    let bare = nothing_toggleable_message(&target, "enable", true);

    assert!(bare.contains("Nothing is installed here"), "got: {bare}");
    assert!(
      !bare.contains("mod loader"),
      "a bare target has no loader to claim is always active; got: {bare}"
    );

    let loader_only = nothing_toggleable_message(&target, "enable", false);

    assert!(loader_only.contains("mod loader"), "got: {loader_only}");
    assert!(loader_only.contains("always active"), "got: {loader_only}");
  }

  #[test]
  fn toggle_summary_agrees_in_number_for_a_single_mod() {
    assert_eq!(toggle_summary("enabled", 1, 0), "\nenabled 1 mod");
    assert_eq!(
      toggle_summary("disabled", 1, 1),
      "\ndisabled 1 mod, 1 mod was already disabled"
    );
    assert_eq!(
      toggle_summary("disabled", 3, 2),
      "\ndisabled 3 mods, 2 mods were already disabled"
    );
  }

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

    Runtime::new()
      .unwrap()
      .block_on(crate::commands::install::run(
        &fixture.client,
        &eco,
        &target,
        &[
          "Owner-ModA".to_string(),
          "denikson-BepInExPack_Valheim".to_string(),
        ],
      ))
      .unwrap();

    run_all(&eco, &target, false).unwrap();

    let mods = modlist::read(&target.dir).unwrap();
    let loader = modlist::find(&mods, "denikson-BepInExPack_Valheim").unwrap();
    let regular = modlist::find(&mods, "Owner-ModA").unwrap();

    // The engine no-ops a loader toggle, so its record must stay enabled and
    // its files must stay in place.
    assert!(loader.enabled);
    assert!(target.dir.join("winhttp.dll").exists());
    assert!(!regular.enabled);
  }

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

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

    // Naming the loader directly must not error and must not report a change
    // that did not happen: the engine no-ops a loader toggle, so its record
    // must stay enabled and its files must stay in place.
    run(&eco, &target, "denikson-BepInExPack_Valheim", false).unwrap();

    let mods = modlist::read(&target.dir).unwrap();
    let loader = modlist::find(&mods, "denikson-BepInExPack_Valheim").unwrap();

    assert!(loader.enabled);
    assert!(target.dir.join("winhttp.dll").exists());
  }

  #[test]
  fn loader_not_toggled_message_names_the_loader_and_says_it_is_always_active() {
    let message = loader_not_toggled_message("denikson-BepInExPack_Valheim");

    assert!(
      message.contains("denikson-BepInExPack_Valheim"),
      "got: {message}"
    );
    assert!(message.contains("always active"), "got: {message}");
    assert!(message.contains("was not changed"), "got: {message}");
  }

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

    // Only the loader installed, which cannot be toggled, so there is nothing
    // to do and that is not a failure.
    Runtime::new()
      .unwrap()
      .block_on(crate::commands::install::run(
        &fixture.client,
        &eco,
        &target,
        &["denikson-BepInExPack_Valheim".to_string()],
      ))
      .unwrap();

    assert!(run_all(&eco, &target, false).is_ok());
  }

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

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

    // Owner-ModA is already disabled before the batch runs, so the "were
    // already {verb}" branch runs alongside a mod that actually changes.
    run(&eco, &target, "Owner-ModA", false).unwrap();

    run_all(&eco, &target, false).unwrap();

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

    assert!(!modlist::find(&mods, "Owner-ModA").unwrap().enabled);
    assert!(!modlist::find(&mods, "Owner-ModB").unwrap().enabled);
    assert!(
      target
        .dir
        .join("BepInEx/plugins/Owner-ModB/ModB.dll.old")
        .exists()
    );
  }

  #[cfg(unix)]
  #[test]
  fn a_mid_batch_failure_still_toggles_the_rest_and_names_the_failure() {
    use std::fs;
    use std::os::unix::fs::PermissionsExt;

    let fixture = Fixture::new();
    let target = fixture.target();
    let eco = Ecosystem::bundled();

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

    let blocked_dir = target.dir.join("BepInEx/plugins/Owner-ModA");
    let original_mode = fs::metadata(&blocked_dir).unwrap().permissions().mode();

    // Renaming a file to `.old` needs write permission on its containing
    // directory, not the file itself, so stripping it here makes Owner-ModA's
    // toggle fail with a real EACCES without touching the file.
    fs::set_permissions(&blocked_dir, fs::Permissions::from_mode(0o555)).unwrap();

    let result = run_all(&eco, &target, false);

    // Restored before asserting, so the fixture's TempDir can clean itself up
    // even if an assertion below fails.
    fs::set_permissions(&blocked_dir, fs::Permissions::from_mode(original_mode)).unwrap();

    let error = result.unwrap_err();

    assert!(error.to_string().contains("Owner-ModA"));

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

    // The batch kept going: Owner-ModB still toggled despite Owner-ModA's
    // failure.
    assert!(!modlist::find(&mods, "Owner-ModB").unwrap().enabled);
    assert!(
      target
        .dir
        .join("BepInEx/plugins/Owner-ModB/ModB.dll.old")
        .exists()
    );
  }
}