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

Ortham / libloadorder / 9632819558

23 Jun 2024 10:39AM UTC coverage: 91.661% (-0.02%) from 91.684%
9632819558

push

github

Ortham
Fix Windows CI build

7321 of 7987 relevant lines covered (91.66%)

73469.36 hits per line

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

97.78
/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 unicase::eq;
25

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

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

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

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

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

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

61
        let filepath = if active {
13,510✔
62
            filepath.unghost()?
11,403✔
63
        } else {
64
            filepath.resolve_path()?
2,107✔
65
        };
66

67
        Plugin::with_path(&filepath, game_settings.id(), active)
13,504✔
68
    }
13,510✔
69

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

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

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

86
        let mut data = esplugin::Plugin::new(game_id.to_esplugin_id(), path);
13,394✔
87
        data.parse_open_file(file, true)
13,394✔
88
            .map_err(|e| file_error(path, e))?;
13,394✔
89

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

98
    pub fn name(&self) -> &str {
21,628,975✔
99
        &self.name
21,628,975✔
100
    }
21,628,975✔
101

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

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

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

114
    pub fn is_master_file(&self) -> bool {
30,719,951✔
115
        self.data.is_master_file()
30,719,951✔
116
    }
30,719,951✔
117

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

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

126
    pub fn is_override_plugin(&self) -> bool {
×
127
        self.data.is_override_plugin()
×
128
    }
×
129

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

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

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

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

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

161
                self.data = esplugin::Plugin::new(*self.data.game_id(), &new_path);
1✔
162
                self.data
1✔
163
                    .parse_file(true)
1✔
164
                    .map_err(|e| file_error(self.data.path(), e))?;
1✔
165
                let modification_time = self.modification_time();
1✔
166
                self.set_modification_time(modification_time)?;
1✔
167
            }
10,288✔
168

169
            self.active = true;
10,289✔
170
        }
3✔
171
        Ok(())
10,292✔
172
    }
10,292✔
173

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

179
pub fn has_plugin_extension(filename: &str, game: GameId) -> bool {
24,898✔
180
    let valid_extensions = if game.supports_light_plugins() {
24,898✔
181
        VALID_EXTENSIONS_WITH_ESL
23,480✔
182
    } else {
183
        VALID_EXTENSIONS
1,418✔
184
    };
185

186
    valid_extensions
24,898✔
187
        .iter()
24,898✔
188
        .any(|e| iends_with_ascii(filename, e))
47,672✔
189
}
24,898✔
190

191
fn iends_with_ascii(string: &str, suffix: &str) -> bool {
21,707,457✔
192
    // as_bytes().into_iter() is faster than bytes().
21,707,457✔
193
    string.len() >= suffix.len()
21,707,457✔
194
        && string
21,707,077✔
195
            .as_bytes()
21,707,077✔
196
            .iter()
21,707,077✔
197
            .rev()
21,707,077✔
198
            .zip(suffix.as_bytes().iter().rev())
21,707,077✔
199
            .all(|(string_byte, suffix_byte)| string_byte.eq_ignore_ascii_case(suffix_byte))
21,781,973✔
200
}
21,707,457✔
201

202
pub fn trim_dot_ghost(string: &str) -> &str {
21,659,785✔
203
    if iends_with_ascii(string, GHOST_FILE_EXTENSION) {
21,659,785✔
204
        &string[..(string.len() - GHOST_FILE_EXTENSION.len())]
14✔
205
    } else {
206
        string
21,659,771✔
207
    }
208
}
21,659,785✔
209

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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