use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::string::ToString;
use anyhow::{Context, Result};
use crate::block::Block;
use crate::fileset::{FileEntry, FileKind};
use crate::parse::ParserMemory;
use crate::pdxfile::PdxFile;
use crate::report::{untidy, warn, ErrorKey};
use crate::token::Token;
use crate::util::fix_slashes_for_target_platform;
#[allow(dead_code)] #[derive(Clone, Debug)]
pub struct ModFile {
block: Block,
name: Option<Token>,
path: Option<Token>,
replace_paths: Vec<Token>,
version: Option<Token>,
tags: Option<Vec<Token>>,
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") {
let msg = "Steam ignores picture= and always uses thumbnail.png.";
warn(ErrorKey::Packaging).msg(msg).loc(picture).push();
}
}
for path in &modfile.replace_paths {
if path.is("history") {
let msg =
"replace_path only replaces the specific directory, not any directories below it";
let info =
"So replace_path = history is not useful, you should replace the paths under it.";
untidy(ErrorKey::Unneeded).msg(msg).info(info).loc(path).push();
}
}
modfile
}
impl ModFile {
pub fn read(pathname: &Path) -> Result<Self> {
let entry = FileEntry::new(pathname.to_path_buf(), FileKind::Mod, pathname.to_path_buf());
let block = PdxFile::read_optional_bom(&entry, &ParserMemory::default())
.with_context(|| format!("Could not read .mod file {}", pathname.display()))?;
Ok(validate_modfile(&block))
}
#[allow(clippy::missing_panics_doc)] pub fn modpath(&self) -> PathBuf {
let modpathname = self.block.loc.pathname();
let mut dirpath = modpathname.parent().unwrap_or_else(|| Path::new("."));
if dirpath.components().count() == 0 {
dirpath = Path::new(".");
}
if modpathname.file_name() == Some(OsStr::new("descriptor.mod")) {
return dirpath.to_path_buf();
}
if dirpath.ends_with("mod") {
dirpath = dirpath.parent().unwrap();
}
let modpath = if let Some(path) = &self.path {
fix_slashes_for_target_platform(dirpath.join(path.as_str()))
} else {
eprintln!("No mod path found in modfile {}", modpathname.display());
dirpath.to_path_buf()
};
if modpath.exists() {
modpath
} else {
eprintln!("Deduced mod path not found: {}", modpath.display());
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(&self) -> Option<String> {
self.name.as_ref().map(ToString::to_string)
}
}