• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Ortham / libloadorder / 10167599482

30 Jul 2024 04:58PM UTC coverage: 91.834% (+0.03%) from 91.807%
10167599482

push

github

Ortham
Fix handling of blueprint plugins

Blueprint plugins were introduced by Starfield.

Plugins that are both blueprint-flagged and master-flagged get loaded after all other plugins and aren't hoisted by non-blueprint plugins.

594 of 605 new or added lines in 6 files covered. (98.18%)

47 existing lines in 5 files now uncovered.

7940 of 8646 relevant lines covered (91.83%)

165386.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.75
/src/plugin.rs
1
use std::ffi::OsStr;
2
/*
3
 * This file is part of libloadorder
4
 *
5
 * Copyright (C) 2017 Oliver Hamlet
6
 *
7
 * libloadorder is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * libloadorder is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with libloadorder. If not, see <http://www.gnu.org/licenses/>.
19
 */
20
use std::fs::{File, FileTimes};
21
use std::path::Path;
22
use std::time::SystemTime;
23

24
use esplugin::ParseOptions;
25
use unicase::eq;
26

27
use crate::enums::{Error, GameId};
28
use crate::game_settings::GameSettings;
29
use crate::ghostable_path::{GhostablePath, GHOST_FILE_EXTENSION};
30

31
const VALID_EXTENSIONS: &[&str] = &[".esp", ".esm", ".esp.ghost", ".esm.ghost"];
32

33
const VALID_EXTENSIONS_WITH_ESL: &[&str] = &[
34
    ".esp",
35
    ".esm",
36
    ".esp.ghost",
37
    ".esm.ghost",
38
    ".esl",
39
    ".esl.ghost",
40
];
41

42
#[derive(Clone, Debug)]
43
pub struct Plugin {
44
    active: bool,
45
    modification_time: SystemTime,
46
    data: esplugin::Plugin,
47
    name: String,
48
}
49

50
impl Plugin {
51
    pub fn new(filename: &str, game_settings: &GameSettings) -> Result<Plugin, Error> {
20,120✔
52
        Plugin::with_active(filename, game_settings, false)
20,120✔
53
    }
20,120✔
54

55
    pub fn with_active(
20,664✔
56
        filename: &str,
20,664✔
57
        game_settings: &GameSettings,
20,664✔
58
        active: bool,
20,664✔
59
    ) -> Result<Plugin, Error> {
20,664✔
60
        let filepath = game_settings.plugin_path(filename);
20,664✔
61

62
        let filepath = if active {
20,664✔
63
            filepath.unghost()?
362✔
64
        } else {
65
            filepath.resolve_path()?
20,302✔
66
        };
67

68
        Plugin::with_path(&filepath, game_settings.id(), active)
20,658✔
69
    }
20,664✔
70

71
    pub(crate) fn with_path(path: &Path, game_id: GameId, active: bool) -> Result<Plugin, Error> {
20,677✔
72
        let filename = match path.file_name().and_then(OsStr::to_str) {
20,677✔
73
            Some(n) => n,
20,677✔
74
            None => return Err(Error::NoFilename(path.to_path_buf())),
×
75
        };
76

77
        if !has_plugin_extension(filename, game_id) {
20,677✔
78
            return Err(Error::InvalidPath(path.to_path_buf()));
×
79
        }
20,677✔
80

81
        let file = File::open(path).map_err(|e| Error::IoError(path.to_path_buf(), e))?;
20,677✔
82
        let modification_time = file
20,562✔
83
            .metadata()
20,562✔
84
            .and_then(|m| m.modified())
20,562✔
85
            .map_err(|e| Error::IoError(path.to_path_buf(), e))?;
20,562✔
86

87
        let mut data = esplugin::Plugin::new(game_id.to_esplugin_id(), path);
20,562✔
88
        data.parse_reader(file, ParseOptions::header_only())
20,562✔
89
            .map_err(|e| file_error(path, e))?;
20,562✔
90

91
        Ok(Plugin {
20,556✔
92
            active,
20,556✔
93
            modification_time,
20,556✔
94
            data,
20,556✔
95
            name: trim_dot_ghost(filename).to_string(),
20,556✔
96
        })
20,556✔
97
    }
20,677✔
98

99
    pub fn name(&self) -> &str {
30,297,461✔
100
        &self.name
30,297,461✔
101
    }
30,297,461✔
102

103
    pub fn name_matches(&self, string: &str) -> bool {
30,290,046✔
104
        eq(self.name(), trim_dot_ghost(string))
30,290,046✔
105
    }
30,290,046✔
106

107
    pub fn modification_time(&self) -> SystemTime {
178✔
108
        self.modification_time
178✔
109
    }
178✔
110

111
    pub fn is_active(&self) -> bool {
528,080✔
112
        self.active
528,080✔
113
    }
528,080✔
114

115
    pub fn is_master_file(&self) -> bool {
86,943,870✔
116
        self.data.is_master_file()
86,943,870✔
117
    }
86,943,870✔
118

119
    pub fn is_light_plugin(&self) -> bool {
273,516✔
120
        self.data.is_light_plugin()
273,516✔
121
    }
273,516✔
122

123
    pub fn is_medium_plugin(&self) -> bool {
236,644✔
124
        self.data.is_medium_plugin()
236,644✔
125
    }
236,644✔
126

127
    pub fn is_blueprint_master(&self) -> bool {
39,452✔
128
        // Non-master blueprint plugins are treated as non-blueprint plugins by
39,452✔
129
        // the game (but not by the CK, which is why this logic isn't in
39,452✔
130
        // esplugin).
39,452✔
131
        self.is_master_file() && self.data.is_blueprint_plugin()
39,452✔
132
    }
39,452✔
133

134
    pub fn masters(&self) -> Result<Vec<String>, Error> {
43,422,655✔
135
        self.data
43,422,655✔
136
            .masters()
43,422,655✔
137
            .map_err(|e| file_error(self.data.path(), e))
43,422,655✔
138
    }
43,422,655✔
139

140
    pub fn set_modification_time(&mut self, time: SystemTime) -> Result<(), Error> {
39✔
141
        // Always write the file time. This has a huge performance impact, but
39✔
142
        // is important for correctness, as otherwise external changes to plugin
39✔
143
        // timestamps between calls to WritableLoadOrder::load() and
39✔
144
        // WritableLoadOrder::save() could lead to libloadorder not setting all
39✔
145
        // the timestamps it needs to and producing an incorrect load order.
39✔
146
        let times = FileTimes::new()
39✔
147
            .set_accessed(SystemTime::now())
39✔
148
            .set_modified(time);
39✔
149

39✔
150
        File::options()
39✔
151
            .write(true)
39✔
152
            .open(self.data.path())
39✔
153
            .and_then(|f| f.set_times(times))
39✔
154
            .map_err(|e| Error::IoError(self.data.path().to_path_buf(), e))?;
39✔
155

156
        self.modification_time = time;
39✔
157
        Ok(())
39✔
158
    }
39✔
159

160
    pub fn activate(&mut self) -> Result<(), Error> {
11,814✔
161
        if !self.is_active() {
11,814✔
162
            if self.data.path().is_ghosted() {
11,813✔
163
                let new_path = self.data.path().unghost()?;
1✔
164

165
                self.data = esplugin::Plugin::new(self.data.game_id(), &new_path);
1✔
166
                self.data
1✔
167
                    .parse_file(ParseOptions::header_only())
1✔
168
                    .map_err(|e| file_error(self.data.path(), e))?;
1✔
169
                let modification_time = self.modification_time();
1✔
170
                self.set_modification_time(modification_time)?;
1✔
171
            }
11,812✔
172

173
            self.active = true;
11,813✔
174
        }
1✔
175
        Ok(())
11,814✔
176
    }
11,814✔
177

178
    pub fn deactivate(&mut self) {
11,963✔
179
        self.active = false;
11,963✔
180
    }
11,963✔
181
}
182

183
pub fn has_plugin_extension(filename: &str, game: GameId) -> bool {
20,998✔
184
    let valid_extensions = if game.supports_light_plugins() {
20,998✔
185
        VALID_EXTENSIONS_WITH_ESL
19,626✔
186
    } else {
187
        VALID_EXTENSIONS
1,372✔
188
    };
189

190
    valid_extensions
20,998✔
191
        .iter()
20,998✔
192
        .any(|e| iends_with_ascii(filename, e))
41,139✔
193
}
20,998✔
194

195
fn iends_with_ascii(string: &str, suffix: &str) -> bool {
30,352,477✔
196
    // as_bytes().into_iter() is faster than bytes().
30,352,477✔
197
    string.len() >= suffix.len()
30,352,477✔
198
        && string
30,352,473✔
199
            .as_bytes()
30,352,473✔
200
            .iter()
30,352,473✔
201
            .rev()
30,352,473✔
202
            .zip(suffix.as_bytes().iter().rev())
30,352,473✔
203
            .all(|(string_byte, suffix_byte)| string_byte.eq_ignore_ascii_case(suffix_byte))
30,415,635✔
204
}
30,352,477✔
205

206
pub fn trim_dot_ghost(string: &str) -> &str {
30,311,338✔
207
    if iends_with_ascii(string, GHOST_FILE_EXTENSION) {
30,311,338✔
208
        &string[..(string.len() - GHOST_FILE_EXTENSION.len())]
12✔
209
    } else {
210
        string
30,311,326✔
211
    }
212
}
30,311,338✔
213

214
fn file_error(file_path: &Path, error: esplugin::Error) -> Error {
6✔
215
    match error {
6✔
216
        esplugin::Error::IoError(x) => Error::IoError(file_path.to_path_buf(), x),
6✔
UNCOV
217
        esplugin::Error::NoFilename(_) => Error::NoFilename(file_path.to_path_buf()),
×
UNCOV
218
        e => Error::PluginParsingError(file_path.to_path_buf(), Box::new(e)),
×
219
    }
220
}
6✔
221

222
#[cfg(test)]
223
mod tests {
224
    use super::*;
225

226
    use crate::tests::copy_to_test_dir;
227
    use std::path::{Path, PathBuf};
228
    use std::time::{Duration, UNIX_EPOCH};
229
    use tempfile::tempdir;
230

231
    fn game_settings(game_id: GameId, game_path: &Path) -> GameSettings {
12✔
232
        GameSettings::with_local_and_my_games_paths(
12✔
233
            game_id,
12✔
234
            game_path,
12✔
235
            &PathBuf::default(),
12✔
236
            PathBuf::default(),
12✔
237
        )
12✔
238
        .unwrap()
12✔
239
    }
12✔
240

241
    #[test]
242
    fn name_should_return_the_plugin_filename_without_any_ghost_extension() {
1✔
243
        let tmp_dir = tempdir().unwrap();
1✔
244
        let game_dir = tmp_dir.path();
1✔
245

1✔
246
        let settings = game_settings(GameId::Oblivion, &game_dir);
1✔
247

1✔
248
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
249
        let plugin = Plugin::new("Blank.esp.ghost", &settings).unwrap();
1✔
250
        assert_eq!("Blank.esp", plugin.name());
1✔
251

252
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
253
        let plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
254
        assert_eq!("Blank.esp", plugin.name());
1✔
255

256
        copy_to_test_dir("Blank.esm", "Blank.esm.ghost", &settings);
1✔
257
        let plugin = Plugin::new("Blank.esm", &settings).unwrap();
1✔
258
        assert_eq!("Blank.esm", plugin.name());
1✔
259
    }
1✔
260

261
    #[test]
262
    fn name_matches_should_ignore_plugin_ghost_extension() {
1✔
263
        let tmp_dir = tempdir().unwrap();
1✔
264
        let settings = game_settings(GameId::Skyrim, tmp_dir.path());
1✔
265
        copy_to_test_dir("Blank.esp", "BlanK.esp.GHoSt", &settings);
1✔
266

1✔
267
        let plugin = Plugin::new("BlanK.esp.GHoSt", &settings).unwrap();
1✔
268
        assert!(plugin.name_matches("Blank.esp"));
1✔
269
    }
1✔
270

271
    #[test]
272
    fn name_matches_should_ignore_string_ghost_suffix() {
1✔
273
        let tmp_dir = tempdir().unwrap();
1✔
274
        let settings = game_settings(GameId::Skyrim, tmp_dir.path());
1✔
275
        copy_to_test_dir("Blank.esp", "BlanK.esp", &settings);
1✔
276

1✔
277
        let plugin = Plugin::new("BlanK.esp", &settings).unwrap();
1✔
278
        assert!(plugin.name_matches("Blank.esp.GHoSt"));
1✔
279
    }
1✔
280

281
    #[test]
282
    fn modification_time_should_return_the_plugin_modification_time_at_creation() {
1✔
283
        let tmp_dir = tempdir().unwrap();
1✔
284
        let game_dir = tmp_dir.path();
1✔
285

1✔
286
        let settings = game_settings(GameId::Oblivion, &game_dir);
1✔
287

1✔
288
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
289
        let plugin_path = game_dir.join("Data").join("Blank.esp");
1✔
290
        let mtime = plugin_path.metadata().unwrap().modified().unwrap();
1✔
291

1✔
292
        let plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
293
        assert_eq!(mtime, plugin.modification_time());
1✔
294
    }
1✔
295

296
    #[test]
297
    fn is_active_should_be_false() {
1✔
298
        let tmp_dir = tempdir().unwrap();
1✔
299
        let game_dir = tmp_dir.path();
1✔
300

1✔
301
        let settings = game_settings(GameId::Oblivion, &game_dir);
1✔
302

1✔
303
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
304
        let plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
305

1✔
306
        assert!(!plugin.is_active());
1✔
307
    }
1✔
308

309
    #[test]
310
    fn is_master_file_should_be_true_if_the_plugin_is_a_master_file() {
1✔
311
        let tmp_dir = tempdir().unwrap();
1✔
312
        let game_dir = tmp_dir.path();
1✔
313

1✔
314
        let settings = game_settings(GameId::Oblivion, &game_dir);
1✔
315

1✔
316
        copy_to_test_dir("Blank.esm", "Blank.esm", &settings);
1✔
317
        let plugin = Plugin::new("Blank.esm", &settings).unwrap();
1✔
318

1✔
319
        assert!(plugin.is_master_file());
1✔
320
    }
1✔
321

322
    #[test]
323
    fn is_master_file_should_be_false_if_the_plugin_is_not_a_master_file() {
1✔
324
        let tmp_dir = tempdir().unwrap();
1✔
325
        let game_dir = tmp_dir.path();
1✔
326

1✔
327
        let settings = game_settings(GameId::Oblivion, &game_dir);
1✔
328

1✔
329
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
330
        let plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
331

1✔
332
        assert!(!plugin.is_master_file());
1✔
333
    }
1✔
334

335
    #[test]
336
    fn is_light_plugin_should_be_true_for_esl_files_only() {
1✔
337
        let tmp_dir = tempdir().unwrap();
1✔
338
        let game_dir = tmp_dir.path();
1✔
339

1✔
340
        let settings = game_settings(GameId::SkyrimSE, &game_dir);
1✔
341

1✔
342
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
343
        let plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
344

1✔
345
        assert!(!plugin.is_master_file());
1✔
346

347
        copy_to_test_dir("Blank.esm", "Blank.esm", &settings);
1✔
348
        let plugin = Plugin::new("Blank.esm", &settings).unwrap();
1✔
349

1✔
350
        assert!(!plugin.is_light_plugin());
1✔
351

352
        copy_to_test_dir("Blank.esm", "Blank.esl", &settings);
1✔
353
        let plugin = Plugin::new("Blank.esl", &settings).unwrap();
1✔
354

1✔
355
        assert!(plugin.is_light_plugin());
1✔
356

357
        copy_to_test_dir("Blank - Different.esp", "Blank - Different.esl", &settings);
1✔
358
        let plugin = Plugin::new("Blank - Different.esl", &settings).unwrap();
1✔
359

1✔
360
        assert!(plugin.is_light_plugin());
1✔
361
    }
1✔
362

363
    #[test]
364
    fn set_modification_time_should_update_the_file_modification_time() {
1✔
365
        let tmp_dir = tempdir().unwrap();
1✔
366
        let game_dir = tmp_dir.path();
1✔
367

1✔
368
        let settings = game_settings(GameId::Oblivion, &game_dir);
1✔
369

1✔
370
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
371

1✔
372
        let path = game_dir.join("Data").join("Blank.esp");
1✔
373
        let file_size = path.metadata().unwrap().len();
1✔
374

1✔
375
        let mut plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
376

1✔
377
        assert_ne!(UNIX_EPOCH, plugin.modification_time());
1✔
378
        plugin.set_modification_time(UNIX_EPOCH).unwrap();
1✔
379

1✔
380
        let metadata = path.metadata().unwrap();
1✔
381
        let new_mtime = metadata.modified().unwrap();
1✔
382
        let new_size = metadata.len();
1✔
383

1✔
384
        assert_eq!(UNIX_EPOCH, plugin.modification_time());
1✔
385
        assert_eq!(UNIX_EPOCH, new_mtime);
1✔
386
        assert_eq!(file_size, new_size);
1✔
387
    }
1✔
388

389
    #[test]
390
    fn set_modification_time_should_be_able_to_handle_pre_unix_timestamps() {
1✔
391
        let tmp_dir = tempdir().unwrap();
1✔
392
        let game_dir = tmp_dir.path();
1✔
393

1✔
394
        let settings = game_settings(GameId::Oblivion, &game_dir);
1✔
395

1✔
396
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
397
        let mut plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
398
        let target_mtime = UNIX_EPOCH - Duration::from_secs(1);
1✔
399

1✔
400
        assert_ne!(target_mtime, plugin.modification_time());
1✔
401
        plugin.set_modification_time(target_mtime).unwrap();
1✔
402
        let new_mtime = game_dir
1✔
403
            .join("Data")
1✔
404
            .join("Blank.esp")
1✔
405
            .metadata()
1✔
406
            .unwrap()
1✔
407
            .modified()
1✔
408
            .unwrap();
1✔
409

1✔
410
        assert_eq!(target_mtime, plugin.modification_time());
1✔
411
        assert_eq!(target_mtime, new_mtime);
1✔
412
    }
1✔
413

414
    #[test]
415
    fn activate_should_unghost_a_ghosted_plugin() {
1✔
416
        let tmp_dir = tempdir().unwrap();
1✔
417
        let game_dir = tmp_dir.path();
1✔
418

1✔
419
        let settings = game_settings(GameId::Oblivion, &game_dir);
1✔
420

1✔
421
        copy_to_test_dir("Blank.esp", "Blank.esp.ghost", &settings);
1✔
422
        let mut plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
423

1✔
424
        plugin.activate().unwrap();
1✔
425

1✔
426
        assert!(plugin.is_active());
1✔
427
        assert_eq!("Blank.esp", plugin.name());
1✔
428
        assert!(game_dir.join("Data").join("Blank.esp").exists());
1✔
429
    }
1✔
430

431
    #[test]
432
    fn deactivate_should_not_ghost_a_plugin() {
1✔
433
        let tmp_dir = tempdir().unwrap();
1✔
434
        let game_dir = tmp_dir.path();
1✔
435

1✔
436
        let settings = game_settings(GameId::Oblivion, &game_dir);
1✔
437

1✔
438
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
439
        let mut plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
440

1✔
441
        plugin.deactivate();
1✔
442

1✔
443
        assert!(!plugin.is_active());
1✔
444
        assert!(game_dir.join("Data").join("Blank.esp").exists());
1✔
445
    }
1✔
446
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc