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

Ortham / libloadorder / 9704827472

27 Jun 2024 11:12PM UTC coverage: 91.807%. Remained the same
9704827472

push

github

Ortham
Update terminology: normal plugin to full plugin

Full is how Starfield refers to non-medium, non-light plugins, so use that as no previous game gave an official term for them.

14 of 16 new or added lines in 4 files covered. (87.5%)

3 existing lines in 1 file now uncovered.

7373 of 8031 relevant lines covered (91.81%)

73188.42 hits per line

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

98.72
/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> {
1,918✔
52
        Plugin::with_active(filename, game_settings, false)
1,918✔
53
    }
1,918✔
54

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

62
        let filepath = if active {
13,529✔
63
            filepath.unghost()?
11,418✔
64
        } else {
65
            filepath.resolve_path()?
2,111✔
66
        };
67

68
        Plugin::with_path(&filepath, game_settings.id(), active)
13,523✔
69
    }
13,529✔
70

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

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

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

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

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

99
    pub fn name(&self) -> &str {
21,674,182✔
100
        &self.name
21,674,182✔
101
    }
21,674,182✔
102

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

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

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

115
    pub fn is_master_file(&self) -> bool {
30,720,489✔
116
        self.data.is_master_file()
30,720,489✔
117
    }
30,720,489✔
118

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

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

127
    pub fn masters(&self) -> Result<Vec<String>, Error> {
34,838✔
128
        self.data
34,838✔
129
            .masters()
34,838✔
130
            .map_err(|e| file_error(self.data.path(), e))
34,838✔
131
    }
34,838✔
132

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

39✔
143
        File::options()
39✔
144
            .write(true)
39✔
145
            .open(self.data.path())
39✔
146
            .and_then(|f| f.set_times(times))
39✔
147
            .map_err(|e| Error::IoError(self.data.path().to_path_buf(), e))?;
39✔
148

149
        self.modification_time = time;
39✔
150
        Ok(())
39✔
151
    }
39✔
152

153
    pub fn activate(&mut self) -> Result<(), Error> {
10,293✔
154
        if !self.is_active() {
10,293✔
155
            if self.data.path().is_ghosted() {
10,289✔
156
                let new_path = self.data.path().unghost()?;
1✔
157

158
                self.data = esplugin::Plugin::new(self.data.game_id(), &new_path);
1✔
159
                self.data
1✔
160
                    .parse_file(ParseOptions::header_only())
1✔
161
                    .map_err(|e| file_error(self.data.path(), e))?;
1✔
162
                let modification_time = self.modification_time();
1✔
163
                self.set_modification_time(modification_time)?;
1✔
164
            }
10,288✔
165

166
            self.active = true;
10,289✔
167
        }
4✔
168
        Ok(())
10,293✔
169
    }
10,293✔
170

171
    pub fn deactivate(&mut self) {
11,183✔
172
        self.active = false;
11,183✔
173
    }
11,183✔
174
}
175

176
pub fn has_plugin_extension(filename: &str, game: GameId) -> bool {
24,925✔
177
    let valid_extensions = if game.supports_light_plugins() {
24,925✔
178
        VALID_EXTENSIONS_WITH_ESL
23,507✔
179
    } else {
180
        VALID_EXTENSIONS
1,418✔
181
    };
182

183
    valid_extensions
24,925✔
184
        .iter()
24,925✔
185
        .any(|e| iends_with_ascii(filename, e))
47,710✔
186
}
24,925✔
187

188
fn iends_with_ascii(string: &str, suffix: &str) -> bool {
21,752,724✔
189
    // as_bytes().into_iter() is faster than bytes().
21,752,724✔
190
    string.len() >= suffix.len()
21,752,724✔
191
        && string
21,752,262✔
192
            .as_bytes()
21,752,262✔
193
            .iter()
21,752,262✔
194
            .rev()
21,752,262✔
195
            .zip(suffix.as_bytes().iter().rev())
21,752,262✔
196
            .all(|(string_byte, suffix_byte)| string_byte.eq_ignore_ascii_case(suffix_byte))
21,827,239✔
197
}
21,752,724✔
198

199
pub fn trim_dot_ghost(string: &str) -> &str {
21,705,014✔
200
    if iends_with_ascii(string, GHOST_FILE_EXTENSION) {
21,705,014✔
201
        &string[..(string.len() - GHOST_FILE_EXTENSION.len())]
14✔
202
    } else {
203
        string
21,705,000✔
204
    }
205
}
21,705,014✔
206

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

215
#[cfg(test)]
216
mod tests {
217
    use super::*;
218

219
    use crate::tests::copy_to_test_dir;
220
    use std::path::{Path, PathBuf};
221
    use std::time::{Duration, UNIX_EPOCH};
222
    use tempfile::tempdir;
223

224
    fn game_settings(game_id: GameId, game_path: &Path) -> GameSettings {
12✔
225
        GameSettings::with_local_and_my_games_paths(
12✔
226
            game_id,
12✔
227
            game_path,
12✔
228
            &PathBuf::default(),
12✔
229
            PathBuf::default(),
12✔
230
        )
12✔
231
        .unwrap()
12✔
232
    }
12✔
233

234
    #[test]
235
    fn name_should_return_the_plugin_filename_without_any_ghost_extension() {
1✔
236
        let tmp_dir = tempdir().unwrap();
1✔
237
        let game_dir = tmp_dir.path();
1✔
238

1✔
239
        let settings = game_settings(GameId::Oblivion, &game_dir);
1✔
240

1✔
241
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
242
        let plugin = Plugin::new("Blank.esp.ghost", &settings).unwrap();
1✔
243
        assert_eq!("Blank.esp", plugin.name());
1✔
244

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

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

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

1✔
260
        let plugin = Plugin::new("BlanK.esp.GHoSt", &settings).unwrap();
1✔
261
        assert!(plugin.name_matches("Blank.esp"));
1✔
262
    }
1✔
263

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

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

274
    #[test]
275
    fn modification_time_should_return_the_plugin_modification_time_at_creation() {
1✔
276
        let tmp_dir = tempdir().unwrap();
1✔
277
        let game_dir = tmp_dir.path();
1✔
278

1✔
279
        let settings = game_settings(GameId::Oblivion, &game_dir);
1✔
280

1✔
281
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
282
        let plugin_path = game_dir.join("Data").join("Blank.esp");
1✔
283
        let mtime = plugin_path.metadata().unwrap().modified().unwrap();
1✔
284

1✔
285
        let plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
286
        assert_eq!(mtime, plugin.modification_time());
1✔
287
    }
1✔
288

289
    #[test]
290
    fn is_active_should_be_false() {
1✔
291
        let tmp_dir = tempdir().unwrap();
1✔
292
        let game_dir = tmp_dir.path();
1✔
293

1✔
294
        let settings = game_settings(GameId::Oblivion, &game_dir);
1✔
295

1✔
296
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
297
        let plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
298

1✔
299
        assert!(!plugin.is_active());
1✔
300
    }
1✔
301

302
    #[test]
303
    fn is_master_file_should_be_true_if_the_plugin_is_a_master_file() {
1✔
304
        let tmp_dir = tempdir().unwrap();
1✔
305
        let game_dir = tmp_dir.path();
1✔
306

1✔
307
        let settings = game_settings(GameId::Oblivion, &game_dir);
1✔
308

1✔
309
        copy_to_test_dir("Blank.esm", "Blank.esm", &settings);
1✔
310
        let plugin = Plugin::new("Blank.esm", &settings).unwrap();
1✔
311

1✔
312
        assert!(plugin.is_master_file());
1✔
313
    }
1✔
314

315
    #[test]
316
    fn is_master_file_should_be_false_if_the_plugin_is_not_a_master_file() {
1✔
317
        let tmp_dir = tempdir().unwrap();
1✔
318
        let game_dir = tmp_dir.path();
1✔
319

1✔
320
        let settings = game_settings(GameId::Oblivion, &game_dir);
1✔
321

1✔
322
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
323
        let plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
324

1✔
325
        assert!(!plugin.is_master_file());
1✔
326
    }
1✔
327

328
    #[test]
329
    fn is_light_plugin_should_be_true_for_esl_files_only() {
1✔
330
        let tmp_dir = tempdir().unwrap();
1✔
331
        let game_dir = tmp_dir.path();
1✔
332

1✔
333
        let settings = game_settings(GameId::SkyrimSE, &game_dir);
1✔
334

1✔
335
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
336
        let plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
337

1✔
338
        assert!(!plugin.is_master_file());
1✔
339

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

1✔
343
        assert!(!plugin.is_light_plugin());
1✔
344

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

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

350
        copy_to_test_dir("Blank - Different.esp", "Blank - Different.esl", &settings);
1✔
351
        let plugin = Plugin::new("Blank - Different.esl", &settings).unwrap();
1✔
352

1✔
353
        assert!(plugin.is_light_plugin());
1✔
354
    }
1✔
355

356
    #[test]
357
    fn set_modification_time_should_update_the_file_modification_time() {
1✔
358
        let tmp_dir = tempdir().unwrap();
1✔
359
        let game_dir = tmp_dir.path();
1✔
360

1✔
361
        let settings = game_settings(GameId::Oblivion, &game_dir);
1✔
362

1✔
363
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
364

1✔
365
        let path = game_dir.join("Data").join("Blank.esp");
1✔
366
        let file_size = path.metadata().unwrap().len();
1✔
367

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

1✔
370
        assert_ne!(UNIX_EPOCH, plugin.modification_time());
1✔
371
        plugin.set_modification_time(UNIX_EPOCH).unwrap();
1✔
372

1✔
373
        let metadata = path.metadata().unwrap();
1✔
374
        let new_mtime = metadata.modified().unwrap();
1✔
375
        let new_size = metadata.len();
1✔
376

1✔
377
        assert_eq!(UNIX_EPOCH, plugin.modification_time());
1✔
378
        assert_eq!(UNIX_EPOCH, new_mtime);
1✔
379
        assert_eq!(file_size, new_size);
1✔
380
    }
1✔
381

382
    #[test]
383
    fn set_modification_time_should_be_able_to_handle_pre_unix_timestamps() {
1✔
384
        let tmp_dir = tempdir().unwrap();
1✔
385
        let game_dir = tmp_dir.path();
1✔
386

1✔
387
        let settings = game_settings(GameId::Oblivion, &game_dir);
1✔
388

1✔
389
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
390
        let mut plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
391
        let target_mtime = UNIX_EPOCH - Duration::from_secs(1);
1✔
392

1✔
393
        assert_ne!(target_mtime, plugin.modification_time());
1✔
394
        plugin.set_modification_time(target_mtime).unwrap();
1✔
395
        let new_mtime = game_dir
1✔
396
            .join("Data")
1✔
397
            .join("Blank.esp")
1✔
398
            .metadata()
1✔
399
            .unwrap()
1✔
400
            .modified()
1✔
401
            .unwrap();
1✔
402

1✔
403
        assert_eq!(target_mtime, plugin.modification_time());
1✔
404
        assert_eq!(target_mtime, new_mtime);
1✔
405
    }
1✔
406

407
    #[test]
408
    fn activate_should_unghost_a_ghosted_plugin() {
1✔
409
        let tmp_dir = tempdir().unwrap();
1✔
410
        let game_dir = tmp_dir.path();
1✔
411

1✔
412
        let settings = game_settings(GameId::Oblivion, &game_dir);
1✔
413

1✔
414
        copy_to_test_dir("Blank.esp", "Blank.esp.ghost", &settings);
1✔
415
        let mut plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
416

1✔
417
        plugin.activate().unwrap();
1✔
418

1✔
419
        assert!(plugin.is_active());
1✔
420
        assert_eq!("Blank.esp", plugin.name());
1✔
421
        assert!(game_dir.join("Data").join("Blank.esp").exists());
1✔
422
    }
1✔
423

424
    #[test]
425
    fn deactivate_should_not_ghost_a_plugin() {
1✔
426
        let tmp_dir = tempdir().unwrap();
1✔
427
        let game_dir = tmp_dir.path();
1✔
428

1✔
429
        let settings = game_settings(GameId::Oblivion, &game_dir);
1✔
430

1✔
431
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
432
        let mut plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
433

1✔
434
        plugin.deactivate();
1✔
435

1✔
436
        assert!(!plugin.is_active());
1✔
437
        assert!(game_dir.join("Data").join("Blank.esp").exists());
1✔
438
    }
1✔
439
}
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