the big refactor
This commit is contained in:
@@ -1,87 +1,69 @@
|
||||
use std::{
|
||||
io,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use libloot::{
|
||||
Game, GameType,
|
||||
error::{GameHandleCreationError, LoadPluginsError, SortPluginsError},
|
||||
};
|
||||
use log::trace;
|
||||
use std::{io, path::Path};
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::{
|
||||
basic_types::{ModdedInstance, RootConfig},
|
||||
basic_types::{self, ModdedInstance, RootConfig},
|
||||
utils::walk_files_recursive,
|
||||
};
|
||||
|
||||
pub struct LoadOrder {
|
||||
game: libloot::Game,
|
||||
target: PathBuf,
|
||||
}
|
||||
pub fn create_loadorder(
|
||||
root_config: &RootConfig,
|
||||
game: &basic_types::Game,
|
||||
instance: &ModdedInstance,
|
||||
) -> Result<Vec<String>, LoadOrderError> {
|
||||
let mut loot_game = Game::new(GameType::SkyrimSE, &game.install_location)?;
|
||||
|
||||
impl LoadOrder {
|
||||
pub fn new(install_dir: impl AsRef<Path>, game_type: GameType) -> Result<Self, LoadOrderError> {
|
||||
Ok(Self {
|
||||
game: Game::with_local_path(
|
||||
game_type,
|
||||
install_dir.as_ref(),
|
||||
&install_dir.as_ref().join("appdata"),
|
||||
)?,
|
||||
target: install_dir.as_ref().to_owned(),
|
||||
})
|
||||
}
|
||||
// Add plugins files from the game install
|
||||
let install_plugins: Vec<_> = walk_files_recursive(game.install_location.join("Data"))?
|
||||
.filter(|f| is_plugin_file(f.path()))
|
||||
.map(|f| f.path())
|
||||
.collect();
|
||||
let refs: Vec<_> = install_plugins.iter().map(|e| e.as_path()).collect();
|
||||
|
||||
pub fn add_plugins_from_instance(
|
||||
&mut self,
|
||||
root_config: &RootConfig,
|
||||
instance: &ModdedInstance,
|
||||
) -> Result<(), LoadOrderError> {
|
||||
for installed_mod in &instance.mods {
|
||||
trace!("Loading {} plugins to game", refs.len());
|
||||
loot_game.load_plugins(&refs)?;
|
||||
|
||||
// Add plugins from the instance
|
||||
let instance_plugins: Vec<_> = instance
|
||||
.mods
|
||||
.iter()
|
||||
.flat_map(|installed_mod| {
|
||||
let mod_config = root_config.get_mod_by_id(&installed_mod.mod_id()).unwrap();
|
||||
let mod_source_root = root_config.get_mod_location(&mod_config);
|
||||
|
||||
let mod_plugins: Vec<PathBuf> = installed_mod
|
||||
installed_mod
|
||||
.files()
|
||||
.iter()
|
||||
.filter(|f| Self::is_plugin_file(&f.dst))
|
||||
.map(|link| mod_source_root.join(&link.src))
|
||||
.collect();
|
||||
.filter(|f| is_plugin_file(&f.dst))
|
||||
.map(move |link| mod_source_root.join(&link.src))
|
||||
})
|
||||
.collect();
|
||||
|
||||
let refs: Vec<_> = mod_plugins.iter().map(|e| e.as_path()).collect();
|
||||
trace!("Loading {} plugins to game", refs.len());
|
||||
self.game.load_plugins(&refs)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
let refs: Vec<_> = instance_plugins.iter().map(|e| e.as_path()).collect();
|
||||
|
||||
pub fn add_plugins_from_install(&mut self) -> Result<(), LoadOrderError> {
|
||||
let plugins: Vec<_> = walk_files_recursive(self.target.join("Data"))?
|
||||
.filter(|f| Self::is_plugin_file(f.path()))
|
||||
.map(|f| f.path())
|
||||
.collect();
|
||||
let refs: Vec<_> = plugins.iter().map(|e| e.as_path()).collect();
|
||||
trace!("Loading {} plugins to game", refs.len());
|
||||
self.game.load_plugins(&refs)?;
|
||||
Ok(())
|
||||
}
|
||||
trace!("Loading {} plugins to game", refs.len());
|
||||
|
||||
fn is_plugin_file(filename: impl AsRef<Path>) -> bool {
|
||||
filename
|
||||
.as_ref()
|
||||
.extension()
|
||||
.is_some_and(|ext| ext == "esp" || ext == "esm" || ext == "esl")
|
||||
}
|
||||
loot_game.load_plugins(&refs)?;
|
||||
|
||||
pub fn load_order(&self) -> Result<Vec<String>, LoadOrderError> {
|
||||
trace!("Generating new load order");
|
||||
let all_plugins = self.game.loaded_plugins();
|
||||
let plugins_names: Vec<&str> = all_plugins.iter().map(|e| e.name()).collect();
|
||||
// Genrate load order
|
||||
let all_plugins = loot_game.loaded_plugins();
|
||||
let plugins_names: Vec<&str> = all_plugins.iter().map(|e| e.name()).collect();
|
||||
|
||||
let sorted = self.game.sort_plugins(&plugins_names)?;
|
||||
let sorted = loot_game.sort_plugins(&plugins_names)?;
|
||||
|
||||
Ok(sorted)
|
||||
}
|
||||
Ok(sorted)
|
||||
}
|
||||
|
||||
fn is_plugin_file(filename: impl AsRef<Path>) -> bool {
|
||||
filename
|
||||
.as_ref()
|
||||
.extension()
|
||||
.is_some_and(|ext| ext == "esp" || ext == "esm" || ext == "esl")
|
||||
}
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
|
||||
Reference in New Issue
Block a user