libloadorder 18.8.0

A cross-platform library for manipulating the load order and active status of plugins for the Elder Scrolls and Fallout games.
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
use std::{collections::HashSet, path::PathBuf};

use unicase::UniCase;

use crate::{
    load_order::mutable::filename_str,
    openmw_config::{non_user_additional_data_paths, read_active_plugin_names, write_openmw_cfg},
    plugin::{iends_with_ascii, Plugin},
    Error, GameId, GameSettings,
};

use super::{
    mutable::MutableLoadOrder,
    readable::{ReadableLoadOrder, ReadableLoadOrderBase},
    writable::{activate, add, deactivate, remove, set_active_plugins},
    WritableLoadOrder,
};

#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub(crate) struct OpenMWLoadOrder {
    game_settings: GameSettings,
    plugins: Vec<Plugin>,
}

impl OpenMWLoadOrder {
    pub(crate) fn new(game_settings: GameSettings) -> Self {
        Self {
            game_settings,
            plugins: Vec::new(),
        }
    }

    fn read_from_active_plugins_file(&self) -> Result<Vec<(String, bool)>, Error> {
        let path = self.game_settings().active_plugins_file();

        let active_plugin_tuples: Vec<_> = read_active_plugin_names(path)?
            .into_iter()
            .map(|v| (v, true))
            .collect();

        Ok(active_plugin_tuples)
    }

    fn apply_load_order(&mut self, active_plugins: &[(String, bool)]) {
        // This takes a similar approach to that of the OpenMW Launcher so that
        // the load order that libloadorder reads should be the same as
        // displayed in the OpenMW Launcher, though the Launcher hides some
        // plugins when their master is inactive.
        // For reference the launcher implementation is at:
        // <https://gitlab.com/OpenMW/openmw/-/blob/openmw-0.49.0/components/contentselector/model/contentmodel.cpp?ref_type=tags#L538>

        // At this point libloadorder has inserted the early loaders in order at
        // the top of the load order, but everything else in their file path
        // order.

        // The launcher does a few things at once:
        //
        // - it moves Tribunal.esm before Bloodmoon.esm
        // - it moves the game file that's currently selected in the Launcher to
        //   the top of the load order
        // - it moves plugins so that they load immediately before the earliest
        //   plugin that has them as a master.

        // From testing, it seems that the Launcher defaults the game file to
        // the first active game file in the load order (which is a little
        // awkward because it removes the current game file from the list, so
        // you can't see where it actually loads in relation to the others).
        // A plugin is a game file if it has no masters and ends with .esm or
        // .omwgame.
        let game_file_name = self
            .plugins()
            .iter()
            .find(|p| {
                (iends_with_ascii(p.name(), ".esm") || iends_with_ascii(p.name(), ".omwgame"))
                    && p.masters().unwrap_or_default().is_empty()
            })
            .map(|p| p.name().to_owned());

        let first_modifiable_index = self
            .plugins
            .iter()
            .position(|p| !self.game_settings.loads_early(p.name()))
            .unwrap_or(self.plugins.len());

        // This is adapted from OpenMW's logic at
        // <https://gitlab.com/OpenMW/openmw/-/blob/openmw-0.49.0/components/contentselector/model/contentmodel.cpp?ref_type=tags#L575>
        let mut moved = HashSet::new();

        if self.plugins.is_empty() {
            // Return early to prevent underflow panic if there are no plugins
            // loaded.
            return;
        }

        let mut i = self.plugins.len() - 1;
        while i > first_modifiable_index {
            let Some(later_plugin) = self.plugins.get(i) else {
                // This should never happen.
                break;
            };

            let key = UniCase::new(later_plugin.name().to_owned());
            if !moved.contains(&key) {
                let index = self
                    .plugins
                    .iter()
                    .skip(first_modifiable_index)
                    .take(i - first_modifiable_index)
                    .position(|earlier_plugin| {
                        game_file_name
                            .as_ref()
                            .is_some_and(|n| n == later_plugin.name())
                            || (later_plugin.name_matches("Tribunal.esm")
                                && earlier_plugin.name_matches("Bloodmoon.esm"))
                            || earlier_plugin.has_master(later_plugin.name())
                    });

                if let Some(index) = index {
                    let plugin = self.plugins.remove(i);
                    self.plugins.insert(first_modifiable_index + index, plugin);
                    moved.insert(key);
                    continue;
                }
            }
            i -= 1;
            moved.clear();
        }

        // Finally, sort the active plugins according to their defined load
        // order. This is equivalent to the approach that the OpenMW Launcher
        // takes:
        // <https://gitlab.com/OpenMW/openmw/-/blob/openmw-0.49.0/components/contentselector/model/contentmodel.cpp?ref_type=tags#L655>
        let mut previous_index = 0;
        for name_tuple in active_plugins {
            if !name_tuple.1 {
                // The name tuples should all be for active plugins, but check
                // just in case.
                continue;
            }

            if let Some(current_index) = self
                .plugins
                .iter()
                .position(|p| p.name_matches(&name_tuple.0))
            {
                if current_index < previous_index {
                    let plugin = self.plugins.remove(current_index);
                    self.plugins.insert(previous_index, plugin);
                } else {
                    previous_index = current_index;
                }
            }
        }
    }
}

impl ReadableLoadOrderBase for OpenMWLoadOrder {
    fn plugins(&self) -> &[Plugin] {
        &self.plugins
    }

    fn game_settings_base(&self) -> &GameSettings {
        &self.game_settings
    }
}

impl MutableLoadOrder for OpenMWLoadOrder {
    fn plugins_mut(&mut self) -> &mut Vec<Plugin> {
        &mut self.plugins
    }

    fn max_active_full_plugins(&self) -> usize {
        // Stated as the limit in the FAQs here:
        // <https://openmw.org/faq/>
        // The code shows that plugin indexes are stored as int32_t, with 0
        // being used as a null value and presumably save data taking up
        // another slot like in other games:
        // <https://gitlab.com/OpenMW/openmw/-/blob/openmw-49-rc3/components/esm/formid.hpp?ref_type=tags#L16>
        0x7FFF_FFFE
    }

    fn total_insertion_order(
        defined_load_order: &[(String, bool)],
        installed_files: &[PathBuf],
        _: GameId,
    ) -> Vec<(String, bool)> {
        // The OpenMW Launcher lists files by the order of their data
        // directories, and sorting files by case-sensitive name within each
        // directory. That's already handled by GameSettings::find_plugins().
        // The OpenMW Launcher implementation is here:
        // <https://gitlab.com/OpenMW/openmw/-/blob/openmw-0.49.0/apps/launcher/datafilespage.cpp?ref_type=tags#L348>

        fn get_key_from_filename(filename: &str) -> UniCase<&str> {
            UniCase::new(filename)
        }

        let active_set: HashSet<_> = defined_load_order
            .iter()
            .map(|(n, _)| get_key_from_filename(n))
            .collect();

        let mut set: HashSet<_> = HashSet::with_capacity(installed_files.len());

        // If multiple file paths have the same filename, keep the first
        // occurrence. The file path used to load the plugin is the last one,
        // but that's handled by GameSettings::plugin_path().
        let unique_tuples: Vec<_> = installed_files
            .iter()
            .filter_map(|p| filename_str(p))
            .filter_map(|f| {
                let key = get_key_from_filename(f);
                set.insert(key)
                    .then_some((f.to_owned(), active_set.contains(&key)))
            })
            .collect();

        // At this point the active plugins are not in their load order, but the
        // OpenMW Launcher doesn't apply the load order until after it has done
        // a few things, like move the early loaders into their proper
        // positions. libloadorder moves early loaders after this function
        // completes, so defer setting the rest of the load order until later.

        unique_tuples
    }
}

impl WritableLoadOrder for OpenMWLoadOrder {
    fn game_settings_mut(&mut self) -> &mut GameSettings {
        &mut self.game_settings
    }

    fn load(&mut self) -> Result<(), Error> {
        self.plugins_mut().clear();

        let plugin_tuples = self.read_from_active_plugins_file()?;
        let paths = self.game_settings.find_plugins();

        self.load_unique_plugins(&plugin_tuples, &paths);

        self.add_implicitly_active_plugins()?;

        self.apply_load_order(&plugin_tuples);

        Ok(())
    }

    fn save(&mut self) -> Result<(), Error> {
        let read_only_data_paths: HashSet<_> =
            non_user_additional_data_paths(self.game_settings.game_path())?
                .into_iter()
                .collect();

        // Filter out the additional plugins dirs that come from read-only
        // sources (e.g. hardcoded values or global config).
        let data_paths: Vec<_> = self
            .game_settings
            .additional_plugins_directories()
            .iter()
            .filter(|p| !read_only_data_paths.contains(p.as_path()))
            .cloned()
            .collect();

        let cfg_path = self.game_settings.active_plugins_file();
        write_openmw_cfg(cfg_path, &data_paths, &self.active_plugin_names())?;

        Ok(())
    }

    fn add(&mut self, plugin_name: &str) -> Result<usize, Error> {
        add(self, plugin_name)
    }

    fn remove(&mut self, plugin_name: &str) -> Result<(), Error> {
        remove(self, plugin_name)
    }

    fn set_load_order(&mut self, plugin_names: &[&str]) -> Result<(), Error> {
        self.replace_plugins(plugin_names)
    }

    fn set_plugin_index(&mut self, plugin_name: &str, position: usize) -> Result<usize, Error> {
        MutableLoadOrder::set_plugin_index(self, plugin_name, position)
    }

    fn is_self_consistent(&self) -> Result<bool, Error> {
        Ok(true)
    }

    fn is_ambiguous(&self) -> Result<bool, Error> {
        Ok(false)
    }

    fn activate(&mut self, plugin_name: &str) -> Result<(), Error> {
        activate(self, plugin_name)
    }

    fn deactivate(&mut self, plugin_name: &str) -> Result<(), Error> {
        deactivate(self, plugin_name)
    }

    fn set_active_plugins(&mut self, active_plugin_names: &[&str]) -> Result<(), Error> {
        set_active_plugins(self, active_plugin_names)
    }
}

#[cfg(test)]
mod tests {
    use std::{
        fs::{create_dir_all, write},
        path::Path,
    };

    use tempfile::tempdir;

    use crate::{
        load_order::tests::{game_settings_for_test, mock_game_files, prepare_bulk_full_plugins},
        plugin::ActiveState,
        tests::{copy_to_dir, create_file, NON_ASCII},
    };

    use super::*;

    fn cfg_path(tmp_path: &Path) -> PathBuf {
        tmp_path.join("my games").join("openmw.cfg")
    }

    fn prepare(tmp_path: &Path) -> OpenMWLoadOrder {
        let mut game_settings = game_settings_for_test(GameId::OpenMW, tmp_path);
        mock_game_files(&mut game_settings);

        OpenMWLoadOrder {
            game_settings,
            plugins: Vec::new(),
        }
    }

    fn write_cfg(cfg_path: &Path, data_paths: &[&str], content: &[&str]) {
        use std::fmt::Write;

        let mut file_content = String::new();
        for data_path in data_paths {
            writeln!(file_content, "data=\"{data_path}\"").unwrap();
        }

        for entry in content {
            writeln!(file_content, "content={entry}").unwrap();
        }

        if !cfg_path.exists() {
            create_dir_all(cfg_path.parent().unwrap()).unwrap();
        }

        write(cfg_path, file_content).unwrap();
    }

    fn read_lines(path: &Path) -> Vec<String> {
        let content = std::fs::read_to_string(path).unwrap();
        content.lines().map(ToOwned::to_owned).collect()
    }

    #[test]
    fn load_should_not_panic_if_no_plugins_are_installed() {
        let tmp_dir = tempdir().unwrap();
        let mut load_order = OpenMWLoadOrder {
            game_settings: game_settings_for_test(GameId::OpenMW, tmp_dir.path()),
            plugins: Vec::new(),
        };

        load_order.load().unwrap();

        assert!(load_order.plugins.is_empty());
    }

    #[test]
    fn load_should_read_active_plugin_load_order_from_cfg_file() {
        let tmp_dir = tempdir().unwrap();
        let mut load_order = prepare(tmp_dir.path());

        let cfg_path = cfg_path(tmp_dir.path());
        let active_plugin_names = &[
            "Blank.esm",
            "Blank - Master Dependent.esp",
            "Blank - Different.esp",
            NON_ASCII,
            "Blank.esp",
        ];
        write_cfg(&cfg_path, &[], active_plugin_names);

        load_order.load().unwrap();

        assert_eq!(active_plugin_names, load_order.plugin_names().as_slice());
    }

    #[test]
    fn load_should_list_inactive_plugins_in_the_same_data_path_in_case_sensitive_lexicographical_order_unless_overridden(
    ) {
        let tmp_dir = tempdir().unwrap();
        let mut load_order = prepare(tmp_dir.path());

        load_order.load().unwrap();

        // Blank.esm is moved up because it looks like a game file.
        let plugin_names = &[
            "Blank.esm",
            "Blank - Different.esp",
            "Blank - Master Dependent.esp",
            "Blank.esp",
            NON_ASCII,
        ];

        assert_eq!(plugin_names, load_order.plugin_names().as_slice());
    }

    #[test]
    fn load_should_list_inactive_plugins_in_order_of_their_data_paths() {
        let tmp_dir = tempdir().unwrap();

        let other_dir_1 = tmp_dir.path().join("other1");
        let other_dir_2 = tmp_dir.path().join("other2");
        create_dir_all(&other_dir_1).unwrap();
        create_dir_all(&other_dir_2).unwrap();

        let cfg_path = cfg_path(tmp_dir.path());
        write_cfg(
            &cfg_path,
            &[other_dir_1.to_str().unwrap(), other_dir_2.to_str().unwrap()],
            &[],
        );

        let mut load_order = prepare(tmp_dir.path());
        let main_dir = load_order.game_settings.plugins_directory();

        copy_to_dir(
            "Blank - Different Master Dependent.esp",
            &main_dir,
            "Blank - Different Master Dependent.esp",
            GameId::OpenMW,
        );
        copy_to_dir(
            "Blank.esm",
            &main_dir,
            "Blank - Different.esm",
            GameId::OpenMW,
        );
        copy_to_dir("Blank.esp", &other_dir_1, "Blank.esp", GameId::OpenMW);
        copy_to_dir(
            "Blank - Different.esp",
            &other_dir_2,
            "Blank - Different.esp",
            GameId::OpenMW,
        );
        copy_to_dir("Blank.esp", &other_dir_2, NON_ASCII, GameId::OpenMW);

        // std::fs::remove_file(main_dir.join("Blank.esm")).unwrap();
        std::fs::remove_file(main_dir.join("Blank.esp")).unwrap();
        std::fs::remove_file(main_dir.join("Blank - Different.esp")).unwrap();
        std::fs::remove_file(main_dir.join(NON_ASCII)).unwrap();

        load_order.load().unwrap();

        // Blank - Different.esm is moved up because it looks like a game file.
        // Blank.esm is moved up because it's a master of
        // Blank - Master Dependent.esp.
        let plugin_names = &[
            "Blank - Different.esm",
            "Blank - Different Master Dependent.esp",
            "Blank.esm",
            "Blank - Master Dependent.esp",
            "Blank.esp",
            "Blank - Different.esp",
            NON_ASCII,
        ];

        assert_eq!(plugin_names, load_order.plugin_names().as_slice());
    }

    #[test]
    fn load_should_move_game_file_immediately_below_last_early_loader() {
        let tmp_dir = tempdir().unwrap();
        let mut load_order = prepare(tmp_dir.path());
        let main_dir = load_order.game_settings.plugins_directory();

        create_file(&main_dir.join("builtin.omwscripts"));

        load_order.load().unwrap();

        // Blank.esm is moved up because it looks like a game file.
        let plugin_names = &[
            "builtin.omwscripts",
            "Blank.esm",
            "Blank - Different.esp",
            "Blank - Master Dependent.esp",
            "Blank.esp",
            NON_ASCII,
        ];

        assert_eq!(plugin_names, load_order.plugin_names().as_slice());
    }

    #[test]
    fn load_should_list_tribunal_before_bloodmoon() {
        let tmp_dir = tempdir().unwrap();
        let mut load_order = prepare(tmp_dir.path());

        let main_dir = load_order.game_settings.plugins_directory();
        copy_to_dir("Blank.esm", &main_dir, "Tribunal.esm", GameId::OpenMW);
        copy_to_dir("Blank.esm", &main_dir, "Bloodmoon.esm", GameId::OpenMW);

        load_order.load().unwrap();

        // Blank.esm is moved up because it looks like a game file.
        let plugin_names = &[
            "Blank.esm",
            "Blank - Different.esp",
            "Blank - Master Dependent.esp",
            "Blank.esp",
            "Tribunal.esm",
            "Bloodmoon.esm",
            NON_ASCII,
        ];

        assert_eq!(plugin_names, load_order.plugin_names().as_slice());
    }

    #[test]
    fn load_should_interleave_active_and_inactive_plugins_by_parent_path_and_lexicographically() {
        let tmp_dir = tempdir().unwrap();

        let other_dir_1 = tmp_dir.path().join("other1");
        let other_dir_2 = tmp_dir.path().join("other2");
        create_dir_all(&other_dir_1).unwrap();
        create_dir_all(&other_dir_2).unwrap();

        let cfg_path = cfg_path(tmp_dir.path());
        write_cfg(
            &cfg_path,
            &[other_dir_1.to_str().unwrap(), other_dir_2.to_str().unwrap()],
            &["Blank - Different.esp", "Blank - Master Dependent.esp"],
        );

        copy_to_dir("Blank.esm", &other_dir_1, "Blank.esm", GameId::OpenMW);
        copy_to_dir("Blank.esp", &other_dir_1, "Blank.esp", GameId::OpenMW);
        copy_to_dir(
            "Blank - Different.esp",
            &other_dir_2,
            "Blank - Different.esp",
            GameId::OpenMW,
        );
        copy_to_dir("Blank.esp", &other_dir_2, NON_ASCII, GameId::OpenMW);

        let mut load_order = prepare(tmp_dir.path());
        let main_dir = load_order.game_settings.plugins_directory();

        std::fs::remove_file(main_dir.join("Blank.esp")).unwrap();
        std::fs::remove_file(main_dir.join("Blank - Different.esp")).unwrap();
        std::fs::remove_file(main_dir.join(NON_ASCII)).unwrap();

        load_order.load().unwrap();

        let plugin_names = &[
            "Blank.esm",
            "Blank.esp",
            "Blank - Different.esp",
            "Blank - Master Dependent.esp",
            NON_ASCII,
        ];

        assert_eq!(plugin_names, load_order.plugin_names().as_slice());
    }

    #[test]
    fn load_should_support_openmw_plugins() {
        let tmp_dir = tempdir().unwrap();
        let mut load_order = prepare(tmp_dir.path());

        let parent_path = load_order.game_settings.plugins_directory();
        let cfg_path = cfg_path(tmp_dir.path());
        let active_plugin_names = &[
            "Blank.omwgame",
            "Blank - Master Dependent.esp",
            "Blank - Different.omwaddon",
            NON_ASCII,
            "Blank.omwscripts",
            "Blank.esp",
        ];
        write_cfg(&cfg_path, &[], active_plugin_names);

        std::fs::rename(
            parent_path.join("Blank.esm"),
            parent_path.join("Blank.omwgame"),
        )
        .unwrap();
        std::fs::rename(
            parent_path.join("Blank - Different.esp"),
            parent_path.join("Blank - Different.omwaddon"),
        )
        .unwrap();
        std::fs::write(parent_path.join("Blank.omwscripts"), "").unwrap();

        load_order.load().unwrap();

        assert_eq!(active_plugin_names, load_order.plugin_names().as_slice());
    }

    #[test]
    fn save_should_write_active_openmw_plugin_positions() {
        let tmp_dir = tempdir().unwrap();
        let mut load_order = prepare(tmp_dir.path());

        let cfg_path = cfg_path(tmp_dir.path());

        let parent_path = load_order.game_settings.plugins_directory();
        let active_plugin_names = &[
            "Blank.omwgame",
            "Blank - Master Dependent.esp",
            "Blank - Different.omwaddon",
            NON_ASCII,
            "Blank.omwscripts",
            "Blank.esp",
        ];

        std::fs::rename(
            parent_path.join("Blank.esm"),
            parent_path.join("Blank.omwgame"),
        )
        .unwrap();
        std::fs::rename(
            parent_path.join("Blank - Different.esp"),
            parent_path.join("Blank - Different.omwaddon"),
        )
        .unwrap();
        std::fs::write(parent_path.join("Blank.omwscripts"), "").unwrap();

        for plugin_name in active_plugin_names {
            let plugin = Plugin::with_active(
                plugin_name,
                load_order.game_settings(),
                ActiveState::ExplicitlyActive,
            )
            .unwrap();
            load_order.plugins.push(plugin);
        }

        load_order.save().unwrap();

        let lines = read_lines(&cfg_path);

        let expected_lines: Vec<_> = active_plugin_names
            .iter()
            .map(|n| format!("content={n}"))
            .collect();

        assert_eq!(expected_lines, lines);
    }

    #[test]
    fn save_should_write_data_paths() {
        let tmp_dir = tempdir().unwrap();
        let mut load_order = prepare(tmp_dir.path());

        let cfg_path = cfg_path(tmp_dir.path());

        write_cfg(
            &cfg_path,
            &["C:\\Games\\Morrowind\\Data Files", "C:\\Other\\Directory"],
            &["a.esm", "b.esm"],
        );

        load_order
            .game_settings
            .set_additional_plugins_directories(vec!["C:\\Path\\&\"a&&\\Data Files".into()]);

        load_order.save().unwrap();

        let lines = read_lines(&cfg_path);

        let expected_lines = vec!["data=\"C:\\Path\\&&&\"a&&&&\\Data Files\"".to_owned()];

        assert_eq!(expected_lines, lines);
    }

    #[test]
    fn save_should_skip_writing_data_paths_that_are_in_global_config() {
        let tmp_dir = tempdir().unwrap();
        let mut load_order = prepare(tmp_dir.path());

        let global_cfg_path = load_order.game_settings.game_path().join("openmw.cfg");

        write_cfg(
            &global_cfg_path,
            &["C:\\Games\\Morrowind\\Data Files", "C:\\Other\\Directory"],
            &["a.esm", "b.esm"],
        );

        load_order.save().unwrap();

        let lines = read_lines(&cfg_path(tmp_dir.path()));

        assert!(lines.is_empty());
    }

    #[test]
    fn activate_should_allow_more_than_255_plugins_to_be_active() {
        // The limit is over 2 billion, don't test activating that many plugins.
        let tmp_dir = tempdir().unwrap();
        let mut load_order = prepare(tmp_dir.path());

        let plugins = prepare_bulk_full_plugins(&mut load_order);
        for plugin in &plugins[..260] {
            load_order.activate(plugin).unwrap();
            assert!(load_order.is_active(plugin));
        }
    }
}