Compare commits

..

3 Commits

2 changed files with 39 additions and 2 deletions

View File

@@ -207,6 +207,8 @@ pub struct FileType {
pub always_install: Option<String>, pub always_install: Option<String>,
#[serde(rename = "@installIfUsable", default = "false_bool")] #[serde(rename = "@installIfUsable", default = "false_bool")]
pub install_if_usable: bool, pub install_if_usable: bool,
#[serde(rename = "@priority")]
pub priority: Option<isize>, pub priority: Option<isize>,
} }

View File

@@ -4,6 +4,8 @@ use std::{
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
use crate::fomod::FileTypeEnum;
pub struct Linker { pub struct Linker {
target: PathBuf, target: PathBuf,
} }
@@ -21,7 +23,7 @@ impl Linker {
} }
fn link_file(&self, from: &Path, to: &Path) -> Result<(), LinkerError> { fn link_file(&self, from: &Path, to: &Path) -> Result<(), LinkerError> {
let target = self.install_path().join(to); let target = self.install_path().join(path_to_lowercase(to));
if let Some(parent) = target.parent() { if let Some(parent) = target.parent() {
fs::create_dir_all(parent)?; fs::create_dir_all(parent)?;
@@ -32,7 +34,7 @@ impl Linker {
} }
fn remove_link(&self, to: &Path) -> Result<(), LinkerError> { fn remove_link(&self, to: &Path) -> Result<(), LinkerError> {
let file = self.install_path().join(to); let file = self.install_path().join(path_to_lowercase(to));
let metadata = fs::symlink_metadata(&file)?; let metadata = fs::symlink_metadata(&file)?;
if !metadata.file_type().is_symlink() { if !metadata.file_type().is_symlink() {
@@ -85,6 +87,35 @@ impl Linker {
Ok(()) Ok(())
} }
pub fn link_plugin_files(
&self,
entries: &[FileTypeEnum],
mod_dir: &Path,
) -> Result<(), LinkerError> {
let mut sorted_entries = entries.to_owned();
sorted_entries.sort_by_cached_key(|e| match e {
FileTypeEnum::File(file_type) => file_type.priority.unwrap_or(0),
FileTypeEnum::Folder(file_type) => file_type.priority.unwrap_or(0),
});
for entry in sorted_entries {
match entry {
FileTypeEnum::File(file) => {
let from = mod_dir.join(file.source);
let to = Path::new("Data").join(file.destination.unwrap_or("".to_owned()));
self.link_file(&from, &to)?;
}
FileTypeEnum::Folder(folder) => {
let from = mod_dir.join(folder.source);
let to = Path::new("Data").join(folder.destination.unwrap_or("".to_owned()));
self.link_recursive(&from, &to)?;
}
}
}
Ok(())
}
} }
#[derive(Debug)] #[derive(Debug)]
@@ -109,3 +140,7 @@ impl From<io::Error> for LinkerError {
Self::Io(e) Self::Io(e)
} }
} }
fn path_to_lowercase(path: &Path) -> PathBuf {
PathBuf::from(path.to_string_lossy().to_lowercase())
}