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

Ortham / libloadorder / 10254629017

05 Aug 2024 07:11PM UTC coverage: 91.872% (+0.04%) from 91.834%
10254629017

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.

Some validation of blueprint plugin positions is still missing, because that requires a new Error enum variant, which is a breaking change.

511 of 516 new or added lines in 4 files covered. (99.03%)

96 existing lines in 6 files now uncovered.

7856 of 8551 relevant lines covered (91.87%)

167394.98 hits per line

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

98.73
/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,114✔
52
        Plugin::with_active(filename, game_settings, false)
20,114✔
53
    }
20,114✔
54

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

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

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

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

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

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

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

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

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

103
    pub fn name_matches(&self, string: &str) -> bool {
30,365,616✔
104
        eq(self.name(), trim_dot_ghost(string))
30,365,616✔
105
    }
30,365,616✔
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,786✔
116
        self.data.is_master_file()
86,943,786✔
117
    }
86,943,786✔
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,372✔
128
        self.is_master_file() && self.data.is_blueprint_plugin()
39,372✔
129
    }
39,372✔
130

131
    pub fn masters(&self) -> Result<Vec<String>, Error> {
43,422,656✔
132
        self.data
43,422,656✔
133
            .masters()
43,422,656✔
134
            .map_err(|e| file_error(self.data.path(), e))
43,422,656✔
135
    }
43,422,656✔
136

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

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

153
        self.modification_time = time;
39✔
154
        Ok(())
39✔
155
    }
39✔
156

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

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

170
            self.active = true;
11,813✔
171
        }
1✔
172
        Ok(())
11,814✔
173
    }
11,814✔
174

175
    pub fn deactivate(&mut self) {
11,963✔
176
        self.active = false;
11,963✔
177
    }
11,963✔
178
}
179

180
pub fn has_plugin_extension(filename: &str, game: GameId) -> bool {
20,990✔
181
    let valid_extensions = if game.supports_light_plugins() {
20,990✔
182
        VALID_EXTENSIONS_WITH_ESL
19,618✔
183
    } else {
184
        VALID_EXTENSIONS
1,372✔
185
    };
186

187
    valid_extensions
20,990✔
188
        .iter()
20,990✔
189
        .any(|e| iends_with_ascii(filename, e))
41,126✔
190
}
20,990✔
191

192
fn iends_with_ascii(string: &str, suffix: &str) -> bool {
30,428,026✔
193
    // as_bytes().into_iter() is faster than bytes().
30,428,026✔
194
    string.len() >= suffix.len()
30,428,026✔
195
        && string
30,428,022✔
196
            .as_bytes()
30,428,022✔
197
            .iter()
30,428,022✔
198
            .rev()
30,428,022✔
199
            .zip(suffix.as_bytes().iter().rev())
30,428,022✔
200
            .all(|(string_byte, suffix_byte)| string_byte.eq_ignore_ascii_case(suffix_byte))
30,491,160✔
201
}
30,428,026✔
202

203
pub fn trim_dot_ghost(string: &str) -> &str {
30,386,900✔
204
    if iends_with_ascii(string, GHOST_FILE_EXTENSION) {
30,386,900✔
205
        &string[..(string.len() - GHOST_FILE_EXTENSION.len())]
12✔
206
    } else {
207
        string
30,386,888✔
208
    }
209
}
30,386,900✔
210

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

219
#[cfg(test)]
220
mod tests {
221
    use super::*;
222

223
    use crate::tests::copy_to_test_dir;
224
    use std::path::{Path, PathBuf};
225
    use std::time::{Duration, UNIX_EPOCH};
226
    use tempfile::tempdir;
227

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

238
    #[test]
239
    fn name_should_return_the_plugin_filename_without_any_ghost_extension() {
1✔
240
        let tmp_dir = tempdir().unwrap();
1✔
241
        let game_dir = tmp_dir.path();
1✔
242

1✔
243
        let settings = game_settings(GameId::Oblivion, &game_dir);
1✔
244

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

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

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

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

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

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

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

278
    #[test]
279
    fn modification_time_should_return_the_plugin_modification_time_at_creation() {
1✔
280
        let tmp_dir = tempdir().unwrap();
1✔
281
        let game_dir = tmp_dir.path();
1✔
282

1✔
283
        let settings = game_settings(GameId::Oblivion, &game_dir);
1✔
284

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

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

293
    #[test]
294
    fn is_active_should_be_false() {
1✔
295
        let tmp_dir = tempdir().unwrap();
1✔
296
        let game_dir = tmp_dir.path();
1✔
297

1✔
298
        let settings = game_settings(GameId::Oblivion, &game_dir);
1✔
299

1✔
300
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
301
        let plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
302

1✔
303
        assert!(!plugin.is_active());
1✔
304
    }
1✔
305

306
    #[test]
307
    fn is_master_file_should_be_true_if_the_plugin_is_a_master_file() {
1✔
308
        let tmp_dir = tempdir().unwrap();
1✔
309
        let game_dir = tmp_dir.path();
1✔
310

1✔
311
        let settings = game_settings(GameId::Oblivion, &game_dir);
1✔
312

1✔
313
        copy_to_test_dir("Blank.esm", "Blank.esm", &settings);
1✔
314
        let plugin = Plugin::new("Blank.esm", &settings).unwrap();
1✔
315

1✔
316
        assert!(plugin.is_master_file());
1✔
317
    }
1✔
318

319
    #[test]
320
    fn is_master_file_should_be_false_if_the_plugin_is_not_a_master_file() {
1✔
321
        let tmp_dir = tempdir().unwrap();
1✔
322
        let game_dir = tmp_dir.path();
1✔
323

1✔
324
        let settings = game_settings(GameId::Oblivion, &game_dir);
1✔
325

1✔
326
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
327
        let plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
328

1✔
329
        assert!(!plugin.is_master_file());
1✔
330
    }
1✔
331

332
    #[test]
333
    fn is_light_plugin_should_be_true_for_esl_files_only() {
1✔
334
        let tmp_dir = tempdir().unwrap();
1✔
335
        let game_dir = tmp_dir.path();
1✔
336

1✔
337
        let settings = game_settings(GameId::SkyrimSE, &game_dir);
1✔
338

1✔
339
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
340
        let plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
341

1✔
342
        assert!(!plugin.is_master_file());
1✔
343

344
        copy_to_test_dir("Blank.esm", "Blank.esm", &settings);
1✔
345
        let plugin = Plugin::new("Blank.esm", &settings).unwrap();
1✔
346

1✔
347
        assert!(!plugin.is_light_plugin());
1✔
348

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

1✔
352
        assert!(plugin.is_light_plugin());
1✔
353

354
        copy_to_test_dir("Blank - Different.esp", "Blank - Different.esl", &settings);
1✔
355
        let plugin = Plugin::new("Blank - Different.esl", &settings).unwrap();
1✔
356

1✔
357
        assert!(plugin.is_light_plugin());
1✔
358
    }
1✔
359

360
    #[test]
361
    fn set_modification_time_should_update_the_file_modification_time() {
1✔
362
        let tmp_dir = tempdir().unwrap();
1✔
363
        let game_dir = tmp_dir.path();
1✔
364

1✔
365
        let settings = game_settings(GameId::Oblivion, &game_dir);
1✔
366

1✔
367
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
368

1✔
369
        let path = game_dir.join("Data").join("Blank.esp");
1✔
370
        let file_size = path.metadata().unwrap().len();
1✔
371

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

1✔
374
        assert_ne!(UNIX_EPOCH, plugin.modification_time());
1✔
375
        plugin.set_modification_time(UNIX_EPOCH).unwrap();
1✔
376

1✔
377
        let metadata = path.metadata().unwrap();
1✔
378
        let new_mtime = metadata.modified().unwrap();
1✔
379
        let new_size = metadata.len();
1✔
380

1✔
381
        assert_eq!(UNIX_EPOCH, plugin.modification_time());
1✔
382
        assert_eq!(UNIX_EPOCH, new_mtime);
1✔
383
        assert_eq!(file_size, new_size);
1✔
384
    }
1✔
385

386
    #[test]
387
    fn set_modification_time_should_be_able_to_handle_pre_unix_timestamps() {
1✔
388
        let tmp_dir = tempdir().unwrap();
1✔
389
        let game_dir = tmp_dir.path();
1✔
390

1✔
391
        let settings = game_settings(GameId::Oblivion, &game_dir);
1✔
392

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

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

1✔
407
        assert_eq!(target_mtime, plugin.modification_time());
1✔
408
        assert_eq!(target_mtime, new_mtime);
1✔
409
    }
1✔
410

411
    #[test]
412
    fn activate_should_unghost_a_ghosted_plugin() {
1✔
413
        let tmp_dir = tempdir().unwrap();
1✔
414
        let game_dir = tmp_dir.path();
1✔
415

1✔
416
        let settings = game_settings(GameId::Oblivion, &game_dir);
1✔
417

1✔
418
        copy_to_test_dir("Blank.esp", "Blank.esp.ghost", &settings);
1✔
419
        let mut plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
420

1✔
421
        plugin.activate().unwrap();
1✔
422

1✔
423
        assert!(plugin.is_active());
1✔
424
        assert_eq!("Blank.esp", plugin.name());
1✔
425
        assert!(game_dir.join("Data").join("Blank.esp").exists());
1✔
426
    }
1✔
427

428
    #[test]
429
    fn deactivate_should_not_ghost_a_plugin() {
1✔
430
        let tmp_dir = tempdir().unwrap();
1✔
431
        let game_dir = tmp_dir.path();
1✔
432

1✔
433
        let settings = game_settings(GameId::Oblivion, &game_dir);
1✔
434

1✔
435
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
436
        let mut plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
437

1✔
438
        plugin.deactivate();
1✔
439

1✔
440
        assert!(!plugin.is_active());
1✔
441
        assert!(game_dir.join("Data").join("Blank.esp").exists());
1✔
442
    }
1✔
443
}
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