added libloot and WIP module for it
This commit is contained in:
98
src/lootorder.rs
Normal file
98
src/lootorder.rs
Normal file
@@ -0,0 +1,98 @@
|
||||
use std::{
|
||||
fmt::Display,
|
||||
fs,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use libloot::{
|
||||
Game, GameType,
|
||||
error::{GameHandleCreationError, LoadPluginsError, SortPluginsError},
|
||||
};
|
||||
|
||||
use crate::{Mod, ModFile};
|
||||
|
||||
pub struct LootOrder {
|
||||
game: libloot::Game,
|
||||
target: PathBuf,
|
||||
}
|
||||
|
||||
impl LootOrder {
|
||||
pub fn new(install_dir: &Path, game_type: GameType) -> Result<Self, LootOrderError> {
|
||||
Ok(Self {
|
||||
game: Game::with_local_path(game_type, install_dir, &install_dir.join("appdata"))?,
|
||||
target: install_dir.to_owned(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn add_plugin(&mut self, file: &ModFile) -> Result<(), LootOrderError> {
|
||||
if !Self::is_plugin_file(file.dest.to_str().unwrap_or_default()) {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let rel_path = self.target.join(&file.dest);
|
||||
let absolute_path = fs::canonicalize(rel_path).unwrap();
|
||||
|
||||
self.game.load_plugins(&[&absolute_path])?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn is_plugin_file(filename: &str) -> bool {
|
||||
filename.ends_with(".esp") || filename.ends_with(".esm") || filename.ends_with(".esl")
|
||||
}
|
||||
|
||||
pub fn load_order(&self) -> Result<(), LootOrderError> {
|
||||
let all_plugins = self.game.loaded_plugins();
|
||||
let plugins_names: Vec<&str> = all_plugins.iter().map(|e| e.name()).collect();
|
||||
|
||||
let sorted = self.game.sort_plugins(&plugins_names)?;
|
||||
|
||||
dbg!(sorted);
|
||||
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum LootOrderError {
|
||||
Create(GameHandleCreationError),
|
||||
Load(LoadPluginsError),
|
||||
Sort(SortPluginsError),
|
||||
}
|
||||
|
||||
impl std::error::Error for LootOrderError {}
|
||||
|
||||
impl Display for LootOrderError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
LootOrderError::Create(game_handle_creation_error) => write!(
|
||||
f,
|
||||
"Failed to create LootOrder: {}",
|
||||
game_handle_creation_error
|
||||
),
|
||||
LootOrderError::Load(load_plugins_error) => {
|
||||
write!(f, "Failed to load plugin: {}", load_plugins_error)
|
||||
}
|
||||
LootOrderError::Sort(sort_plugins_error) => {
|
||||
write!(f, "Failed to sort: {}", sort_plugins_error)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<GameHandleCreationError> for LootOrderError {
|
||||
fn from(e: GameHandleCreationError) -> Self {
|
||||
Self::Create(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<LoadPluginsError> for LootOrderError {
|
||||
fn from(e: LoadPluginsError) -> Self {
|
||||
Self::Load(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<SortPluginsError> for LootOrderError {
|
||||
fn from(e: SortPluginsError) -> Self {
|
||||
Self::Sort(e)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user