- tools/make_icon.py genere assets/icon/ (police Bangers, aucune image du jeu) - launcher/build.rs compile dtr.rc avec rc.exe du SDK Windows (sans dependance) - la fenetre du launcher prend dtr-256.png au lieu du curseur du jeu Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
264 lines
9.1 KiB
Rust
264 lines
9.1 KiB
Rust
//! Mods et pack HD, côté launcher : voir docs/MODS.md.
|
||
//! - un mod = un sous-dossier de mods/ (ignoré s'il commence par « _ » ou « . ») ;
|
||
//! - mod.ini [mod] name / author / description, lu seulement par le launcher ;
|
||
//! - hd/*.png : décors HD ; files/** : fichiers remplacés.
|
||
//! Les mods s'appliquent dans l'ordre alphabétique : pour un même fichier, le dernier l'emporte.
|
||
|
||
use std::path::{Path, PathBuf};
|
||
|
||
pub struct Mod {
|
||
pub id: String,
|
||
pub path: PathBuf,
|
||
pub name: String,
|
||
pub author: String,
|
||
pub description: String,
|
||
pub hd_images: usize,
|
||
pub files: usize,
|
||
}
|
||
|
||
/// dossier des mods : clé [mods] dir, sinon mods/ à la racine du projet (comme le jeu lancé depuis build\)
|
||
pub fn dir(root: &Path, configured: &str) -> PathBuf {
|
||
let c = configured.trim();
|
||
if c.is_empty() {
|
||
return root.join("mods");
|
||
}
|
||
let p = PathBuf::from(c);
|
||
if p.is_absolute() { p } else { root.join(p) }
|
||
}
|
||
|
||
fn count_files(d: &Path) -> usize {
|
||
std::fs::read_dir(d)
|
||
.map(|rd| {
|
||
rd.flatten()
|
||
.map(|e| if e.path().is_dir() { count_files(&e.path()) } else { 1 })
|
||
.sum()
|
||
})
|
||
.unwrap_or(0)
|
||
}
|
||
|
||
fn count_png(d: &Path) -> usize {
|
||
std::fs::read_dir(d)
|
||
.map(|rd| {
|
||
rd.flatten()
|
||
.filter(|e| e.path().extension().is_some_and(|x| x.eq_ignore_ascii_case("png")))
|
||
.count()
|
||
})
|
||
.unwrap_or(0)
|
||
}
|
||
|
||
fn read_ini(path: &Path) -> (String, String, String) {
|
||
let (mut name, mut author, mut desc) = (String::new(), String::new(), String::new());
|
||
if let Ok(text) = std::fs::read_to_string(path) {
|
||
let mut in_mod = false;
|
||
for line in text.lines() {
|
||
let l = line.trim();
|
||
if l.starts_with('[') {
|
||
in_mod = l.eq_ignore_ascii_case("[mod]");
|
||
continue;
|
||
}
|
||
if !in_mod {
|
||
continue;
|
||
}
|
||
if let Some((k, v)) = l.split_once('=') {
|
||
match k.trim() {
|
||
"name" => name = v.trim().to_string(),
|
||
"author" => author = v.trim().to_string(),
|
||
"description" => desc = v.trim().to_string(),
|
||
_ => {}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
(name, author, desc)
|
||
}
|
||
|
||
pub fn list(mods_dir: &Path) -> Vec<Mod> {
|
||
let mut v: Vec<Mod> = std::fs::read_dir(mods_dir)
|
||
.map(|rd| {
|
||
rd.flatten()
|
||
.filter(|e| e.path().is_dir())
|
||
.filter_map(|e| {
|
||
let id = e.file_name().to_string_lossy().to_string();
|
||
if id.starts_with('_') || id.starts_with('.') {
|
||
return None;
|
||
}
|
||
let path = e.path();
|
||
let (name, author, description) = read_ini(&path.join("mod.ini"));
|
||
Some(Mod {
|
||
name: if name.is_empty() { id.clone() } else { name },
|
||
author,
|
||
description,
|
||
hd_images: count_png(&path.join("hd")),
|
||
files: count_files(&path.join("files")),
|
||
id,
|
||
path,
|
||
})
|
||
})
|
||
.collect()
|
||
})
|
||
.unwrap_or_default();
|
||
v.sort_by(|a, b| a.id.cmp(&b.id));
|
||
v
|
||
}
|
||
|
||
/// image HD d'un PCX (nom sans extension) fournie par le dernier mod actif qui l'a
|
||
pub fn hd_image(mods: &[Mod], disabled: &[String], pcx: &str) -> Option<PathBuf> {
|
||
mods.iter()
|
||
.rev()
|
||
.filter(|m| !disabled.contains(&m.id))
|
||
.find_map(|m| {
|
||
let hd = m.path.join("hd");
|
||
std::fs::read_dir(&hd).ok()?.flatten().map(|e| e.path()).find(|p| {
|
||
p.extension().is_some_and(|x| x.eq_ignore_ascii_case("png"))
|
||
&& p.file_stem().is_some_and(|s| s.to_string_lossy().eq_ignore_ascii_case(pcx))
|
||
})
|
||
})
|
||
}
|
||
|
||
/// PNG → RGBA 8 bits
|
||
pub fn load_png(path: &Path) -> Option<crate::art::Rgba> {
|
||
decode_png(std::io::BufReader::new(std::fs::File::open(path).ok()?))
|
||
}
|
||
|
||
/// PNG (lecteur quelconque) → RGBA 8 bits
|
||
pub fn decode_png<R: std::io::BufRead + std::io::Seek>(f: R) -> Option<crate::art::Rgba> {
|
||
let mut dec = png::Decoder::new(f);
|
||
dec.set_transformations(png::Transformations::normalize_to_color8());
|
||
let mut reader = dec.read_info().ok()?;
|
||
let mut buf = vec![0; reader.output_buffer_size()?];
|
||
let info = reader.next_frame(&mut buf).ok()?;
|
||
let (w, h) = (info.width as usize, info.height as usize);
|
||
let src = &buf[..info.buffer_size()];
|
||
let px: Vec<u8> = match info.color_type {
|
||
png::ColorType::Rgba => src.to_vec(),
|
||
png::ColorType::Rgb => src.chunks(3).flat_map(|c| [c[0], c[1], c[2], 255]).collect(),
|
||
png::ColorType::Grayscale => src.iter().flat_map(|&g| [g, g, g, 255]).collect(),
|
||
png::ColorType::GrayscaleAlpha => src.chunks(2).flat_map(|c| [c[0], c[0], c[0], 255]).collect(),
|
||
png::ColorType::Indexed => return None,
|
||
};
|
||
(px.len() == w * h * 4).then_some(crate::art::Rgba { w, h, px })
|
||
}
|
||
|
||
// ------------------------------------------------------------------ Upscayl
|
||
|
||
/// upscayl-bin.exe, aux mêmes endroits que tools/hd_export.py
|
||
pub fn find_upscayl() -> Option<PathBuf> {
|
||
let mut bases = Vec::new();
|
||
if let Some(v) = std::env::var_os("UPSCAYL_BIN") {
|
||
let p = PathBuf::from(v);
|
||
if p.is_file() {
|
||
return Some(p);
|
||
}
|
||
}
|
||
for var in ["ProgramFiles", "ProgramFiles(x86)"] {
|
||
if let Some(v) = std::env::var_os(var) {
|
||
bases.push(PathBuf::from(v));
|
||
}
|
||
}
|
||
if let Some(v) = std::env::var_os("LOCALAPPDATA") {
|
||
bases.push(PathBuf::from(v).join("Programs"));
|
||
}
|
||
bases
|
||
.into_iter()
|
||
.map(|b| b.join("Upscayl").join("resources").join("bin").join("upscayl-bin.exe"))
|
||
.find(|p| p.is_file())
|
||
}
|
||
|
||
/// modèles disponibles (fichiers .param du dossier models d'Upscayl)
|
||
pub fn upscayl_models(bin: &Path) -> Vec<String> {
|
||
let Some(models) = bin.parent().and_then(Path::parent).map(|p| p.join("models")) else { return Vec::new() };
|
||
let mut v: Vec<String> = std::fs::read_dir(models)
|
||
.map(|rd| {
|
||
rd.flatten()
|
||
.filter_map(|e| {
|
||
let p = e.path();
|
||
if p.extension()? != "param" {
|
||
return None;
|
||
}
|
||
Some(p.file_stem()?.to_string_lossy().to_string())
|
||
})
|
||
.collect()
|
||
})
|
||
.unwrap_or_default();
|
||
v.sort();
|
||
v
|
||
}
|
||
|
||
pub const DEFAULT_MODEL: &str = "digital-art-4x";
|
||
|
||
/// dossier des décors d'origine exportés en PNG : commence par « _ », donc ignoré par le jeu
|
||
pub const EXPORT_DIR: &str = "_decors_originaux";
|
||
|
||
/// nom de dossier d'un mod tiré du nom affiché : lettres, chiffres, « - » et « _ »
|
||
pub fn id_from_name(name: &str) -> String {
|
||
let mut id = String::new();
|
||
for c in name.trim().chars() {
|
||
let c = match c {
|
||
'à' | 'â' | 'ä' => 'a',
|
||
'é' | 'è' | 'ê' | 'ë' => 'e',
|
||
'î' | 'ï' => 'i',
|
||
'ô' | 'ö' => 'o',
|
||
'ù' | 'û' | 'ü' => 'u',
|
||
'ç' => 'c',
|
||
c => c,
|
||
};
|
||
if c.is_ascii_alphanumeric() || c == '-' || c == '_' {
|
||
id.push(c.to_ascii_lowercase());
|
||
} else if !id.ends_with('_') {
|
||
id.push('_');
|
||
}
|
||
}
|
||
id.trim_matches('_').to_string()
|
||
}
|
||
|
||
/// crée mods/<id>/hd/ et son mod.ini ; renvoie le dossier hd
|
||
pub fn create_mod(mods_dir: &Path, name: &str) -> Result<PathBuf, String> {
|
||
let id = id_from_name(name);
|
||
if id.is_empty() {
|
||
return Err("nom vide / empty name".into());
|
||
}
|
||
let dir = mods_dir.join(&id);
|
||
if dir.exists() {
|
||
return Err(format!("mods\\{id} existe déjà / already exists"));
|
||
}
|
||
let hd = dir.join("hd");
|
||
std::fs::create_dir_all(&hd).map_err(|e| e.to_string())?;
|
||
std::fs::write(
|
||
dir.join("mod.ini"),
|
||
format!("[mod]\nname={}\nauthor=\ndescription=Décors HD : déposez les PNG dans hd\\ (même nom que le PCX, ratio 16:10)\n", name.trim()),
|
||
)
|
||
.map_err(|e| e.to_string())?;
|
||
Ok(hd)
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
#[test]
|
||
fn ids() {
|
||
assert_eq!(super::id_from_name(" Décors à la main! "), "decors_a_la_main");
|
||
assert_eq!(super::id_from_name("_x"), "x");
|
||
assert_eq!(super::id_from_name("***"), "");
|
||
}
|
||
}
|
||
|
||
/// dossier du mod créé pour un modèle : hd_upscayl pour le modèle par défaut, hd_upscayl_<modèle> sinon
|
||
pub fn pack_id(model: &str) -> String {
|
||
if model == DEFAULT_MODEL { "hd_upscayl".into() } else { format!("hd_upscayl_{model}") }
|
||
}
|
||
|
||
/// écrit mod.ini s'il n'existe pas encore
|
||
pub fn write_mod_ini(mod_dir: &Path, model: &str) {
|
||
let p = mod_dir.join("mod.ini");
|
||
if p.exists() {
|
||
return;
|
||
}
|
||
let _ = std::fs::create_dir_all(mod_dir);
|
||
let _ = std::fs::write(
|
||
p,
|
||
format!(
|
||
"[mod]\nname=Décors HD (Upscayl, {model})\nauthor=créé par le launcher\n\
|
||
description=Décors du jeu agrandis ×4 avec Upscayl (Real-ESRGAN, modèle {model})\n"
|
||
),
|
||
);
|
||
}
|