1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use std::path::{Path, PathBuf};

use anyhow::{Context, Result};

use crate::block::Block;
use crate::fileset::{FileEntry, FileKind};
use crate::pdxfile::PdxFile;
use crate::report::{advice_info, old_warn, ErrorKey};
use crate::token::Token;

#[allow(dead_code)] // TODO, see below
#[derive(Clone, Debug)]
pub struct ModFile {
    block: Block,
    name: Option<Token>,
    path: Option<Token>,
    replace_paths: Vec<Token>,
    version: Option<Token>,
    // TODO: check that these are tags accepted by steam ?
    tags: Option<Vec<Token>>,
    // TODO: check if the version is compatible with the validator.
    // (Newer means the validator is too old, older means it's not up to date
    // with current CK3)
    supported_version: Option<Token>,
    picture: Option<Token>,
}

fn validate_modfile(block: &Block) -> ModFile {
    let modfile = ModFile {
        block: block.clone(),
        name: block.get_field_value("name").cloned(),
        path: block.get_field_value("path").cloned(),
        replace_paths: block.get_field_values("replace_path").into_iter().cloned().collect(),
        version: block.get_field_value("version").cloned(),
        tags: block.get_field_list("tags"),
        supported_version: block.get_field_value("supported_version").cloned(),
        picture: block.get_field_value("picture").cloned(),
    };

    if let Some(picture) = &modfile.picture {
        if !picture.is("thumbnail.png") {
            old_warn(
                picture,
                ErrorKey::Packaging,
                "Steam ignores picture= and always uses thumbnail.png.",
            );
        }
    }

    for path in &modfile.replace_paths {
        if path.is("history") {
            advice_info(
                path,
                ErrorKey::Unneeded,
                "replace_path only replaces the specific directory, not any directories below it",
                "So replace_path = history is not useful, you should replace the paths under it.",
            );
        }
    }

    // TODO: check if supported_version is newer than validator,
    // or is older than known CK3

    modfile
}

impl ModFile {
    pub fn read(pathname: &Path) -> Result<Self> {
        let entry = FileEntry::new(pathname.to_path_buf(), FileKind::Mod);
        let block = PdxFile::read_optional_bom(&entry, pathname)
            .with_context(|| format!("Could not read .mod file {}", pathname.display()))?;
        Ok(validate_modfile(&block))
    }

    pub fn modpath(&self) -> PathBuf {
        let mut dirpath = self.block.loc.pathname().parent().unwrap_or_else(|| Path::new("."));
        if dirpath.components().count() == 0 {
            dirpath = Path::new(".");
        }

        let modpath = if let Some(path) = &self.path {
            dirpath.join(path.as_str())
        } else {
            dirpath.to_path_buf()
        };

        if modpath.exists() {
            modpath
        } else {
            dirpath.to_path_buf()
        }
    }

    pub fn replace_paths(&self) -> Vec<PathBuf> {
        self.replace_paths.iter().map(|t| PathBuf::from(t.as_str())).collect()
    }

    pub fn display_name_ext(&self) -> String {
        if let Some(name) = &self.name {
            format!(" \"{name}\"")
        } else {
            String::new()
        }
    }
}