tiger_lib/
fileset.rs

1//! Track all the files (vanilla and mods) that are relevant to the current validation.
2
3use std::borrow::ToOwned;
4use std::cmp::Ordering;
5use std::ffi::OsStr;
6use std::fmt::{Display, Formatter};
7use std::path::{Path, PathBuf};
8use std::string::ToString;
9use std::sync::RwLock;
10
11use anyhow::{bail, Result};
12use rayon::prelude::*;
13use walkdir::WalkDir;
14
15use crate::block::Block;
16use crate::everything::{Everything, FilesError};
17use crate::game::Game;
18use crate::helpers::TigerHashSet;
19use crate::item::Item;
20#[cfg(feature = "vic3")]
21use crate::mod_metadata::ModMetadata;
22#[cfg(any(feature = "ck3", feature = "imperator", feature = "hoi4"))]
23use crate::modfile::ModFile;
24use crate::parse::ParserMemory;
25use crate::pathtable::{PathTable, PathTableIndex};
26use crate::report::{
27    add_loaded_dlc_root, add_loaded_mod_root, err, fatal, report, ErrorKey, Severity,
28};
29use crate::token::Token;
30use crate::util::fix_slashes_for_target_platform;
31
32/// Note that ordering of these enum values matters.
33/// Files later in the order will override files of the same name before them,
34/// and the warnings about duplicates take that into account.
35// TODO: verify the relative order of `Clausewitz` and `Jomini`
36#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
37pub enum FileKind {
38    /// `Internal` is for parsing tiger's own data. The user should not see warnings from this.
39    Internal,
40    /// `Clausewitz` and `Jomini` are directories bundled with the base game.
41    Clausewitz,
42    Jomini,
43    /// The base game files.
44    Vanilla,
45    /// Downloadable content present on the user's system.
46    Dlc(u8),
47    /// Other mods loaded as directed by the config file. 0-based indexing.
48    LoadedMod(u8),
49    /// The mod under scrutiny. Usually, warnings are not emitted unless they touch `Mod` files.
50    Mod,
51}
52
53impl FileKind {
54    pub fn counts_as_vanilla(&self) -> bool {
55        match self {
56            FileKind::Clausewitz | FileKind::Jomini | FileKind::Vanilla | FileKind::Dlc(_) => true,
57            FileKind::Internal | FileKind::LoadedMod(_) | FileKind::Mod => false,
58        }
59    }
60}
61
62#[derive(Clone, Debug, PartialEq, Eq)]
63pub struct FileEntry {
64    /// Pathname components below the mod directory or the vanilla game dir
65    /// Must not be empty.
66    path: PathBuf,
67    /// Whether it's a vanilla or mod file
68    kind: FileKind,
69    /// Index into the `PathTable`. Used to initialize `Loc`, which doesn't carry a copy of the pathbuf.
70    /// A `FileEntry` might not have this index, because `FileEntry` needs to be usable before the (ordered)
71    /// path table is created.
72    idx: Option<PathTableIndex>,
73    /// The full filesystem path of this entry. Not used for ordering or equality.
74    fullpath: PathBuf,
75}
76
77impl FileEntry {
78    pub fn new(path: PathBuf, kind: FileKind, fullpath: PathBuf) -> Self {
79        assert!(path.file_name().is_some());
80        Self { path, kind, idx: None, fullpath }
81    }
82
83    pub fn kind(&self) -> FileKind {
84        self.kind
85    }
86
87    pub fn path(&self) -> &Path {
88        &self.path
89    }
90
91    pub fn fullpath(&self) -> &Path {
92        &self.fullpath
93    }
94
95    /// Convenience function
96    /// Won't panic because `FileEntry` with empty filename is not allowed.
97    #[allow(clippy::missing_panics_doc)]
98    pub fn filename(&self) -> &OsStr {
99        self.path.file_name().unwrap()
100    }
101
102    fn store_in_pathtable(&mut self) {
103        assert!(self.idx.is_none());
104        self.idx = Some(PathTable::store(self.path.clone(), self.fullpath.clone()));
105    }
106
107    pub fn path_idx(&self) -> Option<PathTableIndex> {
108        self.idx
109    }
110}
111
112impl Display for FileEntry {
113    fn fmt(&self, fmt: &mut Formatter) -> Result<(), std::fmt::Error> {
114        write!(fmt, "{}", self.path.display())
115    }
116}
117
118impl PartialOrd for FileEntry {
119    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
120        Some(self.cmp(other))
121    }
122}
123
124impl Ord for FileEntry {
125    fn cmp(&self, other: &Self) -> Ordering {
126        // Compare idx if available (for speed), otherwise compare the paths.
127        let path_ord = if self.idx.is_some() && other.idx.is_some() {
128            self.idx.unwrap().cmp(&other.idx.unwrap())
129        } else {
130            self.path.cmp(&other.path)
131        };
132
133        // For same paths, the later [`FileKind`] wins.
134        if path_ord == Ordering::Equal {
135            self.kind.cmp(&other.kind)
136        } else {
137            path_ord
138        }
139    }
140}
141
142/// A trait for a submodule that can process files.
143pub trait FileHandler<T: Send>: Sync + Send {
144    /// The `FileHandler` can read settings it needs from the ck3-tiger config.
145    fn config(&mut self, _config: &Block) {}
146
147    /// Which files this handler is interested in.
148    /// This is a directory prefix of files it wants to handle,
149    /// relative to the mod or vanilla root.
150    fn subpath(&self) -> PathBuf;
151
152    /// This is called for each matching file, in arbitrary order.
153    /// If a `T` is returned, it will be passed to `handle_file` later.
154    /// Since `load_file` is executed multi-threaded while `handle_file`
155    /// is single-threaded, try to do the heavy work in this function.
156    fn load_file(&self, entry: &FileEntry, parser: &ParserMemory) -> Option<T>;
157
158    /// This is called for each matching file in turn, in lexical order.
159    /// That's the order in which the CK3 game engine loads them too.
160    fn handle_file(&mut self, entry: &FileEntry, loaded: T);
161
162    /// This is called after all files have been handled.
163    /// The `FileHandler` can generate indexes, perform full-data checks, etc.
164    fn finalize(&mut self) {}
165}
166
167#[derive(Clone, Debug)]
168pub struct LoadedMod {
169    /// The `FileKind` to use for file entries from this mod.
170    kind: FileKind,
171
172    /// The tag used for this mod in error messages.
173    #[allow(dead_code)]
174    label: String,
175
176    /// The location of this mod in the filesystem.
177    root: PathBuf,
178
179    /// A list of directories that should not be read from vanilla or previous mods.
180    replace_paths: Vec<PathBuf>,
181}
182
183impl LoadedMod {
184    fn new_main_mod(root: PathBuf, replace_paths: Vec<PathBuf>) -> Self {
185        Self { kind: FileKind::Mod, label: "MOD".to_string(), root, replace_paths }
186    }
187
188    fn new(kind: FileKind, label: String, root: PathBuf, replace_paths: Vec<PathBuf>) -> Self {
189        Self { kind, label, root, replace_paths }
190    }
191
192    pub fn root(&self) -> &Path {
193        &self.root
194    }
195
196    pub fn kind(&self) -> FileKind {
197        self.kind
198    }
199
200    pub fn should_replace(&self, path: &Path) -> bool {
201        self.replace_paths.iter().any(|p| p == path)
202    }
203}
204
205#[derive(Debug)]
206pub struct Fileset {
207    /// The CK3 game directory.
208    vanilla_root: Option<PathBuf>,
209
210    /// Extra CK3 directory loaded before vanilla.
211    #[cfg(feature = "jomini")]
212    clausewitz_root: Option<PathBuf>,
213
214    /// Extra CK3 directory loaded before vanilla.
215    #[cfg(feature = "jomini")]
216    jomini_root: Option<PathBuf>,
217
218    /// The mod being analyzed.
219    the_mod: LoadedMod,
220
221    /// Other mods to be loaded before `mod`, in order.
222    pub loaded_mods: Vec<LoadedMod>,
223
224    /// DLC directories to be loaded after vanilla, in order.
225    loaded_dlcs: Vec<LoadedMod>,
226
227    /// The ck3-tiger config.
228    config: Option<Block>,
229
230    /// The CK3 and mod files in arbitrary order (will be empty after `finalize`).
231    files: Vec<FileEntry>,
232
233    /// The CK3 and mod files in the order the game would load them.
234    ordered_files: Vec<FileEntry>,
235
236    /// Filename Tokens for the files in `ordered_files`.
237    /// Used for [`Fileset::iter_keys()`].
238    filename_tokens: Vec<Token>,
239
240    /// All filenames from `ordered_files`, for quick lookup.
241    filenames: TigerHashSet<PathBuf>,
242
243    /// All directories that have been looked up, for quick lookup.
244    directories: RwLock<TigerHashSet<PathBuf>>,
245
246    /// Filenames that have been looked up during validation. Used to filter the --unused output.
247    used: RwLock<TigerHashSet<String>>,
248}
249
250impl Fileset {
251    pub fn new(vanilla_dir: Option<&Path>, mod_root: PathBuf, replace_paths: Vec<PathBuf>) -> Self {
252        let vanilla_root = if Game::is_jomini() {
253            vanilla_dir.map(|dir| dir.join("game"))
254        } else {
255            vanilla_dir.map(ToOwned::to_owned)
256        };
257        #[cfg(feature = "jomini")]
258        let clausewitz_root = vanilla_dir.map(|dir| dir.join("clausewitz"));
259        #[cfg(feature = "jomini")]
260        let jomini_root = vanilla_dir.map(|dir| dir.join("jomini"));
261
262        Fileset {
263            vanilla_root,
264            #[cfg(feature = "jomini")]
265            clausewitz_root,
266            #[cfg(feature = "jomini")]
267            jomini_root,
268            the_mod: LoadedMod::new_main_mod(mod_root, replace_paths),
269            loaded_mods: Vec::new(),
270            loaded_dlcs: Vec::new(),
271            config: None,
272            files: Vec::new(),
273            ordered_files: Vec::new(),
274            filename_tokens: Vec::new(),
275            filenames: TigerHashSet::default(),
276            directories: RwLock::new(TigerHashSet::default()),
277            used: RwLock::new(TigerHashSet::default()),
278        }
279    }
280
281    pub fn config(
282        &mut self,
283        config: Block,
284        #[allow(unused_variables)] workshop_dir: Option<&Path>,
285        #[allow(unused_variables)] paradox_dir: Option<&Path>,
286    ) -> Result<()> {
287        let config_path = config.loc.fullpath();
288        for block in config.get_field_blocks("load_mod") {
289            let mod_idx;
290            if let Ok(idx) = u8::try_from(self.loaded_mods.len()) {
291                mod_idx = idx;
292            } else {
293                bail!("too many loaded mods, cannot process more");
294            }
295
296            let default_label = || format!("MOD{mod_idx}");
297            let label =
298                block.get_field_value("label").map_or_else(default_label, ToString::to_string);
299
300            if Game::is_ck3() || Game::is_imperator() || Game::is_hoi4() {
301                #[cfg(any(feature = "ck3", feature = "imperator", feature = "hoi4"))]
302                if let Some(path) = get_modfile(&label, config_path, block, paradox_dir) {
303                    let modfile = ModFile::read(&path)?;
304                    eprintln!(
305                        "Loading secondary mod {label} from: {}{}",
306                        modfile.modpath().display(),
307                        modfile
308                            .display_name()
309                            .map_or_else(String::new, |name| format!(" \"{name}\"")),
310                    );
311                    let kind = FileKind::LoadedMod(mod_idx);
312                    let loaded_mod = LoadedMod::new(
313                        kind,
314                        label.clone(),
315                        modfile.modpath().clone(),
316                        modfile.replace_paths(),
317                    );
318                    add_loaded_mod_root(label);
319                    self.loaded_mods.push(loaded_mod);
320                } else {
321                    bail!("could not load secondary mod from config; missing valid `modfile` or `workshop_id` field");
322                }
323            } else if Game::is_vic3() {
324                #[cfg(feature = "vic3")]
325                if let Some(pathdir) = get_mod(&label, config_path, block, workshop_dir) {
326                    match ModMetadata::read(&pathdir) {
327                        Ok(metadata) => {
328                            eprintln!(
329                                "Loading secondary mod {label} from: {}{}",
330                                pathdir.display(),
331                                metadata
332                                    .display_name()
333                                    .map_or_else(String::new, |name| format!(" \"{name}\"")),
334                            );
335                            let kind = FileKind::LoadedMod(mod_idx);
336                            let loaded_mod = LoadedMod::new(
337                                kind,
338                                label.clone(),
339                                pathdir,
340                                metadata.replace_paths(),
341                            );
342                            add_loaded_mod_root(label);
343                            self.loaded_mods.push(loaded_mod);
344                        }
345                        Err(e) => {
346                            eprintln!(
347                                "could not load secondary mod {label} from: {}",
348                                pathdir.display()
349                            );
350                            eprintln!("  because: {e}");
351                        }
352                    }
353                } else {
354                    bail!("could not load secondary mod from config; missing valid `mod` or `workshop_id` field");
355                }
356            }
357        }
358        self.config = Some(config);
359        Ok(())
360    }
361
362    fn should_replace(&self, path: &Path, kind: FileKind) -> bool {
363        if kind == FileKind::Mod {
364            return false;
365        }
366        if kind < FileKind::Mod && self.the_mod.should_replace(path) {
367            return true;
368        }
369        for loaded_mod in &self.loaded_mods {
370            if kind < loaded_mod.kind && loaded_mod.should_replace(path) {
371                return true;
372            }
373        }
374        false
375    }
376
377    fn scan(&mut self, path: &Path, kind: FileKind) -> Result<(), walkdir::Error> {
378        for entry in WalkDir::new(path) {
379            let entry = entry?;
380            if entry.depth() == 0 || !entry.file_type().is_file() {
381                continue;
382            }
383            // unwrap is safe here because WalkDir gives us paths with this prefix.
384            let inner_path = entry.path().strip_prefix(path).unwrap();
385            if inner_path.starts_with(".git") {
386                continue;
387            }
388            let inner_dir = inner_path.parent().unwrap_or_else(|| Path::new(""));
389            if self.should_replace(inner_dir, kind) {
390                continue;
391            }
392            self.files.push(FileEntry::new(
393                inner_path.to_path_buf(),
394                kind,
395                entry.path().to_path_buf(),
396            ));
397        }
398        Ok(())
399    }
400
401    pub fn scan_all(&mut self) -> Result<(), FilesError> {
402        #[cfg(feature = "jomini")]
403        if let Some(clausewitz_root) = self.clausewitz_root.clone() {
404            self.scan(&clausewitz_root.clone(), FileKind::Clausewitz).map_err(|e| {
405                FilesError::VanillaUnreadable { path: clausewitz_root.clone(), source: e }
406            })?;
407        }
408        #[cfg(feature = "jomini")]
409        if let Some(jomini_root) = &self.jomini_root.clone() {
410            self.scan(&jomini_root.clone(), FileKind::Jomini).map_err(|e| {
411                FilesError::VanillaUnreadable { path: jomini_root.clone(), source: e }
412            })?;
413        }
414        if let Some(vanilla_root) = &self.vanilla_root.clone() {
415            self.scan(&vanilla_root.clone(), FileKind::Vanilla).map_err(|e| {
416                FilesError::VanillaUnreadable { path: vanilla_root.clone(), source: e }
417            })?;
418            #[cfg(feature = "hoi4")]
419            if Game::is_hoi4() {
420                self.load_dlcs(&vanilla_root.join("integrated_dlc"))?;
421            }
422            self.load_dlcs(&vanilla_root.join("dlc"))?;
423        }
424        // loaded_mods is cloned here for the borrow checker
425        for loaded_mod in &self.loaded_mods.clone() {
426            self.scan(loaded_mod.root(), loaded_mod.kind()).map_err(|e| {
427                FilesError::ModUnreadable { path: loaded_mod.root().to_path_buf(), source: e }
428            })?;
429        }
430        #[allow(clippy::unnecessary_to_owned)] // borrow checker requires to_path_buf here
431        self.scan(&self.the_mod.root().to_path_buf(), FileKind::Mod).map_err(|e| {
432            FilesError::ModUnreadable { path: self.the_mod.root().to_path_buf(), source: e }
433        })?;
434        Ok(())
435    }
436
437    pub fn load_dlcs(&mut self, dlc_root: &Path) -> Result<(), FilesError> {
438        for entry in WalkDir::new(dlc_root).max_depth(1).sort_by_file_name().into_iter().flatten() {
439            if entry.depth() == 1 && entry.file_type().is_dir() {
440                let label = entry.file_name().to_string_lossy().to_string();
441                let idx =
442                    u8::try_from(self.loaded_dlcs.len()).expect("more than 256 DLCs installed");
443                let dlc = LoadedMod::new(
444                    FileKind::Dlc(idx),
445                    label.clone(),
446                    entry.path().to_path_buf(),
447                    Vec::new(),
448                );
449                self.scan(dlc.root(), dlc.kind()).map_err(|e| FilesError::VanillaUnreadable {
450                    path: dlc.root().to_path_buf(),
451                    source: e,
452                })?;
453                self.loaded_dlcs.push(dlc);
454                add_loaded_dlc_root(label);
455            }
456        }
457        Ok(())
458    }
459
460    pub fn finalize(&mut self) {
461        // This sorts by pathname but where pathnames are equal it places `Mod` entries after `Vanilla` entries
462        // and `LoadedMod` entries between them in order
463        self.files.sort();
464
465        // When there are identical paths, only keep the last entry of them.
466        for entry in self.files.drain(..) {
467            if let Some(prev) = self.ordered_files.last_mut() {
468                if entry.path == prev.path {
469                    *prev = entry;
470                } else {
471                    self.ordered_files.push(entry);
472                }
473            } else {
474                self.ordered_files.push(entry);
475            }
476        }
477
478        for entry in &mut self.ordered_files {
479            let token = Token::new(&entry.filename().to_string_lossy(), (&*entry).into());
480            self.filename_tokens.push(token);
481            entry.store_in_pathtable();
482            self.filenames.insert(entry.path.clone());
483        }
484    }
485
486    pub fn get_files_under<'a>(&'a self, subpath: &'a Path) -> &'a [FileEntry] {
487        let start = self.ordered_files.partition_point(|entry| entry.path < subpath);
488        let end = start
489            + self.ordered_files[start..].partition_point(|entry| entry.path.starts_with(subpath));
490        &self.ordered_files[start..end]
491    }
492
493    pub fn filter_map_under<F, T>(&self, subpath: &Path, f: F) -> Vec<T>
494    where
495        F: Fn(&FileEntry) -> Option<T> + Sync + Send,
496        T: Send,
497    {
498        self.get_files_under(subpath).par_iter().filter_map(f).collect()
499    }
500
501    pub fn handle<T: Send, H: FileHandler<T>>(&self, handler: &mut H, parser: &ParserMemory) {
502        if let Some(config) = &self.config {
503            handler.config(config);
504        }
505        let subpath = handler.subpath();
506        let entries = self.filter_map_under(&subpath, |entry| {
507            handler.load_file(entry, parser).map(|loaded| (entry.clone(), loaded))
508        });
509        for (entry, loaded) in entries {
510            handler.handle_file(&entry, loaded);
511        }
512        handler.finalize();
513    }
514
515    pub fn mark_used(&self, file: &str) {
516        let file = file.strip_prefix('/').unwrap_or(file);
517        self.used.write().unwrap().insert(file.to_string());
518    }
519
520    pub fn exists(&self, key: &str) -> bool {
521        let key = key.strip_prefix('/').unwrap_or(key);
522        let filepath = if Game::is_hoi4() && key.contains('\\') {
523            PathBuf::from(key.replace('\\', "/"))
524        } else {
525            PathBuf::from(key)
526        };
527        self.filenames.contains(&filepath)
528    }
529
530    pub fn iter_keys(&self) -> impl Iterator<Item = &Token> {
531        self.filename_tokens.iter()
532    }
533
534    pub fn entry_exists(&self, key: &str) -> bool {
535        // file exists
536        if self.exists(key) {
537            return true;
538        }
539
540        // directory lookup - check if there are any files within the directory
541        let dir = key.strip_prefix('/').unwrap_or(key);
542        let dirpath = Path::new(dir);
543
544        if self.directories.read().unwrap().contains(dirpath) {
545            return true;
546        }
547
548        match self.ordered_files.binary_search_by_key(&dirpath, |fe| fe.path.as_path()) {
549            // should be handled in `exists` already; something must be wrong
550            Ok(_) => unreachable!(),
551            Err(idx) => {
552                // there exists a file in the given directory
553                if self.ordered_files[idx].path.starts_with(dirpath) {
554                    self.directories.write().unwrap().insert(dirpath.to_path_buf());
555                    return true;
556                }
557            }
558        }
559        false
560    }
561
562    pub fn verify_entry_exists(&self, entry: &str, token: &Token, max_sev: Severity) {
563        self.mark_used(&entry.replace("//", "/"));
564        if !self.entry_exists(entry) {
565            let msg = format!("file or directory {entry} does not exist");
566            report(ErrorKey::MissingFile, Item::File.severity().at_most(max_sev))
567                .msg(msg)
568                .loc(token)
569                .push();
570        }
571    }
572
573    #[cfg(feature = "ck3")] // vic3 happens not to use
574    pub fn verify_exists(&self, file: &Token) {
575        self.mark_used(&file.as_str().replace("//", "/"));
576        if !self.exists(file.as_str()) {
577            let msg = "referenced file does not exist";
578            report(ErrorKey::MissingFile, Item::File.severity()).msg(msg).loc(file).push();
579        }
580    }
581
582    pub fn verify_exists_implied(&self, file: &str, t: &Token, max_sev: Severity) {
583        self.mark_used(&file.replace("//", "/"));
584        if !self.exists(file) {
585            let msg = format!("file {file} does not exist");
586            report(ErrorKey::MissingFile, Item::File.severity().at_most(max_sev))
587                .msg(msg)
588                .loc(t)
589                .push();
590        }
591    }
592
593    pub fn verify_exists_implied_crashes(&self, file: &str, t: &Token) {
594        self.mark_used(&file.replace("//", "/"));
595        if !self.exists(file) {
596            let msg = format!("file {file} does not exist");
597            fatal(ErrorKey::Crash).msg(msg).loc(t).push();
598        }
599    }
600
601    pub fn validate(&self, _data: &Everything) {
602        let common_dirs = match Game::game() {
603            #[cfg(feature = "ck3")]
604            Game::Ck3 => crate::ck3::tables::misc::COMMON_DIRS,
605            #[cfg(feature = "vic3")]
606            Game::Vic3 => crate::vic3::tables::misc::COMMON_DIRS,
607            #[cfg(feature = "imperator")]
608            Game::Imperator => crate::imperator::tables::misc::COMMON_DIRS,
609            #[cfg(feature = "hoi4")]
610            Game::Hoi4 => crate::hoi4::tables::misc::COMMON_DIRS,
611        };
612        let common_subdirs_ok = match Game::game() {
613            #[cfg(feature = "ck3")]
614            Game::Ck3 => crate::ck3::tables::misc::COMMON_SUBDIRS_OK,
615            #[cfg(feature = "vic3")]
616            Game::Vic3 => crate::vic3::tables::misc::COMMON_SUBDIRS_OK,
617            #[cfg(feature = "imperator")]
618            Game::Imperator => crate::imperator::tables::misc::COMMON_SUBDIRS_OK,
619            #[cfg(feature = "hoi4")]
620            Game::Hoi4 => crate::hoi4::tables::misc::COMMON_SUBDIRS_OK,
621        };
622        // Check the files in directories in common/ to make sure they are in known directories
623        let mut warned: Vec<&Path> = Vec::new();
624        'outer: for entry in &self.ordered_files {
625            if !entry.path.to_string_lossy().ends_with(".txt") {
626                continue;
627            }
628            if entry.path == PathBuf::from("common/achievement_groups.txt") {
629                continue;
630            }
631            #[cfg(feature = "hoi4")]
632            if Game::is_hoi4() {
633                for valid in crate::hoi4::tables::misc::COMMON_FILES {
634                    if <&str as AsRef<Path>>::as_ref(valid) == entry.path {
635                        continue 'outer;
636                    }
637                }
638            }
639            let dirname = entry.path.parent().unwrap();
640            if warned.contains(&dirname) {
641                continue;
642            }
643            if !entry.path.starts_with("common") {
644                // Check if the modder forgot the common/ part
645                let joined = Path::new("common").join(&entry.path);
646                for valid in common_dirs {
647                    if joined.starts_with(valid) {
648                        let msg = format!("file in unexpected directory {}", dirname.display());
649                        let info = format!("did you mean common/{} ?", dirname.display());
650                        err(ErrorKey::Filename).msg(msg).info(info).loc(entry).push();
651                        warned.push(dirname);
652                        continue 'outer;
653                    }
654                }
655                continue;
656            }
657
658            for valid in common_subdirs_ok {
659                if entry.path.starts_with(valid) {
660                    continue 'outer;
661                }
662            }
663
664            for valid in common_dirs {
665                if <&str as AsRef<Path>>::as_ref(valid) == dirname {
666                    continue 'outer;
667                }
668            }
669
670            if entry.path.starts_with("common/scripted_values") {
671                let msg = "file should be in common/script_values/";
672                err(ErrorKey::Filename).msg(msg).loc(entry).push();
673            } else if (Game::is_ck3() || Game::is_imperator())
674                && entry.path.starts_with("common/on_actions")
675            {
676                let msg = "file should be in common/on_action/";
677                err(ErrorKey::Filename).msg(msg).loc(entry).push();
678            } else if (Game::is_vic3() || Game::is_hoi4())
679                && entry.path.starts_with("common/on_action")
680            {
681                let msg = "file should be in common/on_actions/";
682                err(ErrorKey::Filename).msg(msg).loc(entry).push();
683            } else if Game::is_vic3() && entry.path.starts_with("common/modifiers") {
684                let msg = "file should be in common/static_modifiers since 1.7";
685                err(ErrorKey::Filename).msg(msg).loc(entry).push();
686            } else if Game::is_ck3() && entry.path.starts_with("common/vassal_contracts") {
687                let msg = "common/vassal_contracts was replaced with common/subject_contracts/contracts/ in 1.16";
688                err(ErrorKey::Filename).msg(msg).loc(entry).push();
689            } else {
690                let msg = format!("file in unexpected directory `{}`", dirname.display());
691                err(ErrorKey::Filename).msg(msg).loc(entry).push();
692            }
693            warned.push(dirname);
694        }
695    }
696
697    pub fn check_unused_dds(&self, _data: &Everything) {
698        let mut vec = Vec::new();
699        for entry in &self.ordered_files {
700            let pathname = entry.path.to_string_lossy();
701            if entry.path.extension().is_some_and(|ext| ext.eq_ignore_ascii_case("dds"))
702                && !entry.path.starts_with("gfx/interface/illustrations/loading_screens")
703                && !self.used.read().unwrap().contains(pathname.as_ref())
704            {
705                vec.push(entry);
706            }
707        }
708        for entry in vec {
709            report(ErrorKey::UnusedFile, Severity::Untidy)
710                .msg("Unused DDS files")
711                .abbreviated(entry)
712                .push();
713        }
714    }
715}
716
717#[cfg(any(feature = "ck3", feature = "imperator", feature = "hoi4"))]
718fn get_modfile(
719    label: &String,
720    config_path: &Path,
721    block: &Block,
722    paradox_dir: Option<&Path>,
723) -> Option<PathBuf> {
724    let mut path: Option<PathBuf> = None;
725    if let Some(modfile) = block.get_field_value("modfile") {
726        let modfile_path = fix_slashes_for_target_platform(
727            config_path
728                .parent()
729                .unwrap() // SAFETY: known to be for a file in a directory
730                .join(modfile.as_str()),
731        );
732        if modfile_path.exists() {
733            path = Some(modfile_path);
734        } else {
735            eprintln!("Could not find mod {label} at: {}", modfile_path.display());
736        }
737    }
738    if path.is_none() {
739        if let Some(workshop_id) = block.get_field_value("workshop_id") {
740            match paradox_dir {
741                Some(p) => {
742                    path = Some(fix_slashes_for_target_platform(
743                        p.join(format!("mod/ugc_{workshop_id}.mod")),
744                    ));
745                }
746                None => eprintln!("workshop_id defined, but could not find paradox directory"),
747            }
748        }
749    }
750    path
751}
752
753#[cfg(feature = "vic3")]
754fn get_mod(
755    label: &String,
756    config_path: &Path,
757    block: &Block,
758    workshop_dir: Option<&Path>,
759) -> Option<PathBuf> {
760    let mut path: Option<PathBuf> = None;
761    if let Some(modfile) = block.get_field_value("mod") {
762        let mod_path = fix_slashes_for_target_platform(
763            config_path
764                .parent()
765                .unwrap() // SAFETY: known to be for a file in a directory
766                .join(modfile.as_str()),
767        );
768        if mod_path.exists() {
769            path = Some(mod_path);
770        } else {
771            eprintln!("Could not find mod {label} at: {}", mod_path.display());
772        }
773    }
774    if path.is_none() {
775        if let Some(workshop_id) = block.get_field_value("workshop_id") {
776            match workshop_dir {
777                Some(w) => {
778                    path = Some(fix_slashes_for_target_platform(w.join(workshop_id.as_str())));
779                }
780                None => eprintln!("workshop_id defined, but could not find workshop"),
781            }
782        }
783    }
784    path
785}