created base config & a lot of refactoring
This commit is contained in:
231
src/basic_types.rs
Normal file
231
src/basic_types.rs
Normal file
@@ -0,0 +1,231 @@
|
||||
use quick_xml::se;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{
|
||||
error::Error,
|
||||
fs::{self, read_to_string},
|
||||
io::{self, Write},
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
conflict_resolver::{Conflict, ConflictSolver},
|
||||
fomod::{FileType, FileTypeEnum},
|
||||
utils::{path_to_lowercase, walk_files_recursive},
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, PartialEq)]
|
||||
pub struct RootConfig {
|
||||
/// Available games
|
||||
pub games: Vec<Game>,
|
||||
|
||||
/// Where all mods are stored
|
||||
pub mod_location: PathBuf,
|
||||
|
||||
/// All available mods
|
||||
pub mods: Vec<ModConfig>,
|
||||
}
|
||||
|
||||
impl RootConfig {
|
||||
pub fn load_from_file(path: impl AsRef<Path>) -> Result<Self, Box<dyn Error>> {
|
||||
let data = read_to_string(path)?;
|
||||
let config = toml::from_str(&data)?;
|
||||
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_mod_location(&self, mod_config: &ModConfig) -> PathBuf {
|
||||
self.mod_location.join(mod_config.source.clone())
|
||||
}
|
||||
|
||||
pub fn get_mod_by_id(&self, id: &str) -> Option<ModConfig> {
|
||||
self.mods.iter().find(|e| e.id == id).cloned()
|
||||
}
|
||||
}
|
||||
|
||||
/// Available game
|
||||
#[derive(Debug, Clone, Deserialize, PartialEq)]
|
||||
pub struct Game {
|
||||
pub install_location: PathBuf,
|
||||
}
|
||||
|
||||
/// Config for an available mod
|
||||
#[derive(Debug, Clone, Deserialize, PartialEq)]
|
||||
pub struct ModConfig {
|
||||
/// ID of the mod
|
||||
pub id: String,
|
||||
|
||||
/// Relative to the mod_location from root config
|
||||
pub source: PathBuf,
|
||||
}
|
||||
|
||||
impl ModConfig {
|
||||
pub fn new(id: &str, source: impl AsRef<Path>) -> Self {
|
||||
Self {
|
||||
id: id.to_owned(),
|
||||
source: source.as_ref().to_owned(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// An modded game with all plugins and files
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
|
||||
pub struct ModdedInstance {
|
||||
pub name: String,
|
||||
pub mods: Vec<InstalledMod>,
|
||||
}
|
||||
|
||||
impl ModdedInstance {
|
||||
pub fn new(name: &str) -> Self {
|
||||
Self {
|
||||
name: name.to_owned(),
|
||||
mods: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load_from_file(path: impl AsRef<Path>) -> Result<Self, Box<dyn Error>> {
|
||||
let data = read_to_string(path)?;
|
||||
let config = toml::from_str(&data)?;
|
||||
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
pub fn save_to_file(&self, path: impl AsRef<Path>) -> Result<(), Box<dyn Error>> {
|
||||
let content = toml::to_string_pretty(self)?;
|
||||
let mut file = fs::File::create(path)?;
|
||||
write!(file, "{}", content)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn add_mod(&mut self, from_mod: &ModConfig, priority: isize, files: &[ModFile]) {
|
||||
let mut new_mod = InstalledMod::new(from_mod, priority);
|
||||
let mut solver = ConflictSolver::new();
|
||||
|
||||
// Add all the files form the instance. Unchecked because it is already checked.
|
||||
let mut already_installed_files: Vec<(ModFile, &InstalledMod)> = Vec::new();
|
||||
|
||||
for installed_mod in &self.mods {
|
||||
for (src, dst) in &installed_mod.files {
|
||||
already_installed_files.push((ModFile::new(src, dst, 0), installed_mod));
|
||||
}
|
||||
}
|
||||
|
||||
for (present_file, present_mod) in &already_installed_files {
|
||||
solver.add_file_unchecked(present_file, present_mod);
|
||||
}
|
||||
|
||||
// Now add the new files and check for conflicts
|
||||
for file in files {
|
||||
if let Some(conflict) = solver.add_file(file, &new_mod) {
|
||||
// FIXME: Find a way to display conflict to user
|
||||
println!("{:?}", conflict);
|
||||
panic!("Conflict")
|
||||
}
|
||||
}
|
||||
|
||||
// No conflicts. Add files.
|
||||
for file in files {
|
||||
new_mod.add_file(file);
|
||||
}
|
||||
|
||||
self.mods.push(new_mod);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
|
||||
pub struct InstalledMod {
|
||||
id: String,
|
||||
files: Vec<(PathBuf, PathBuf)>,
|
||||
priority: isize,
|
||||
}
|
||||
|
||||
impl InstalledMod {
|
||||
pub fn new(from_mod: &ModConfig, priority: isize) -> Self {
|
||||
Self {
|
||||
id: from_mod.id.clone(),
|
||||
files: Vec::new(),
|
||||
priority,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_file(&mut self, file: &ModFile) {
|
||||
self.files.push((file.source.clone(), file.dest.clone()));
|
||||
}
|
||||
|
||||
/// The priority over other mods. Only used when 2 files conflict.
|
||||
pub fn priority(&self) -> isize {
|
||||
self.priority
|
||||
}
|
||||
|
||||
/// The selected files
|
||||
pub fn files(&self) -> Vec<(PathBuf, PathBuf)> {
|
||||
self.files.clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
|
||||
pub struct ModFile {
|
||||
/// Relative path in the mod
|
||||
source: PathBuf,
|
||||
|
||||
/// Relative path on where to install file in game dir
|
||||
dest: PathBuf,
|
||||
|
||||
/// Internal priority inside the mod itself. In case the mod overwrites internal files.
|
||||
internal_priority: isize,
|
||||
}
|
||||
|
||||
impl ModFile {
|
||||
pub fn new(src: impl AsRef<Path>, dst: impl AsRef<Path>, prio: isize) -> Self {
|
||||
Self {
|
||||
source: src.as_ref().to_owned(),
|
||||
dest: dst.as_ref().to_owned(),
|
||||
internal_priority: prio,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_from_installer(file: FileType) -> Self {
|
||||
let dest: PathBuf = file.destination.unwrap_or_default().into();
|
||||
|
||||
ModFile {
|
||||
source: file.source.into(),
|
||||
dest: dest.to_owned(),
|
||||
internal_priority: file.priority.unwrap_or(0),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_installer(
|
||||
entry: FileTypeEnum,
|
||||
source: impl AsRef<Path>,
|
||||
) -> Result<Vec<Self>, std::io::Error> {
|
||||
match entry {
|
||||
FileTypeEnum::File(file_type) => Ok(vec![Self::new_from_installer(file_type)]),
|
||||
FileTypeEnum::Folder(dir_type) => {
|
||||
let source_root = source.as_ref().join(&dir_type.source);
|
||||
let priority = dir_type.priority.unwrap_or(0);
|
||||
let dest_base: PathBuf =
|
||||
Path::new("Data").join(PathBuf::from(dir_type.destination.unwrap_or_default()));
|
||||
|
||||
Ok(walk_files_recursive(&source_root)?
|
||||
.map(|file| Self {
|
||||
internal_priority: priority,
|
||||
source: file.path().strip_prefix(&source).unwrap().to_owned(),
|
||||
dest: dest_base.join(file.path().strip_prefix(&source_root).unwrap()),
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the realtive path this file should be installed
|
||||
#[inline]
|
||||
pub fn destination(&self) -> PathBuf {
|
||||
self.dest.clone()
|
||||
}
|
||||
|
||||
/// Get the iternal priority. Only used when 2 files conflict.
|
||||
#[inline]
|
||||
pub fn internal_priority(&self) -> isize {
|
||||
self.internal_priority
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user