added basic unpacker
This commit is contained in:
@@ -19,4 +19,5 @@ pub enum Commands {
|
||||
LoadOrder { instance: String },
|
||||
ApiCheck,
|
||||
Download { url: String },
|
||||
Unpack { id: String, path: String },
|
||||
}
|
||||
|
||||
30
src/main.rs
30
src/main.rs
@@ -9,6 +9,7 @@ use crate::{
|
||||
instance::{files_to_install_mod, insert_mod_to_instance},
|
||||
nexus::{NexusAPI, download_nxm},
|
||||
types::RootConfig,
|
||||
unpacker::unpack,
|
||||
};
|
||||
|
||||
mod activator;
|
||||
@@ -21,6 +22,7 @@ mod load_order;
|
||||
mod mod_config_installer;
|
||||
mod nexus;
|
||||
mod types;
|
||||
mod unpacker;
|
||||
mod utils;
|
||||
|
||||
fn command_activate(
|
||||
@@ -39,9 +41,9 @@ fn command_add(root_config: &RootConfig, instance_id: &str, mod_id: &str) -> any
|
||||
.mod_by_id(mod_id)
|
||||
.ok_or(anyhow!("Can't find mod in config"))?;
|
||||
|
||||
let files = files_to_install_mod(root_config, &instance, mod_to_install)?;
|
||||
let files = files_to_install_mod(root_config, &instance, &mod_to_install)?;
|
||||
|
||||
match insert_mod_to_instance(&mut instance, mod_to_install, &files, 0) {
|
||||
match insert_mod_to_instance(&mut instance, &mod_to_install, &files, 0) {
|
||||
Ok(_) => {
|
||||
instance.save_to_file()?;
|
||||
Ok(())
|
||||
@@ -104,6 +106,25 @@ fn command_download(root_config: &RootConfig, nxm_url: &str) -> anyhow::Result<(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn command_unpack(
|
||||
root_config: &mut RootConfig,
|
||||
id: &str,
|
||||
file: impl AsRef<Path>,
|
||||
) -> anyhow::Result<()> {
|
||||
if root_config.game_by_id(id).is_some() {
|
||||
error!("Mod already present");
|
||||
return Err(anyhow!("Mod already exists"));
|
||||
}
|
||||
|
||||
let new_mod = unpack(root_config, id, file)?;
|
||||
|
||||
root_config.add_mod(&new_mod);
|
||||
|
||||
root_config.save_to_file()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn setup_logger() {
|
||||
env_logger::builder()
|
||||
.filter_level(log::LevelFilter::max())
|
||||
@@ -118,7 +139,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
let args = Args::parse();
|
||||
|
||||
debug!("Loading config from {:?}", args.config);
|
||||
let root_config = RootConfig::load_from_file(args.config)?;
|
||||
let mut root_config = RootConfig::load_from_file(args.config)?;
|
||||
|
||||
match args.command {
|
||||
cli::Commands::Activate { instance, target } => {
|
||||
@@ -137,6 +158,9 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
cli::Commands::Download { url } => {
|
||||
command_download(&root_config, &url)?;
|
||||
}
|
||||
cli::Commands::Unpack { id, path } => {
|
||||
command_unpack(&mut root_config, &id, path)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
28
src/unpacker.rs
Normal file
28
src/unpacker.rs
Normal file
@@ -0,0 +1,28 @@
|
||||
use std::{fs, path::Path};
|
||||
|
||||
use anyhow::anyhow;
|
||||
|
||||
use crate::types::{ModConfig, RootConfig};
|
||||
|
||||
pub fn unpack(
|
||||
root_config: &RootConfig,
|
||||
id: &str,
|
||||
path: impl AsRef<Path>,
|
||||
) -> anyhow::Result<ModConfig> {
|
||||
let extract_to = root_config.mod_location().join(id);
|
||||
|
||||
if fs::exists(&extract_to)? {
|
||||
return Err(anyhow!("File already exists"));
|
||||
}
|
||||
|
||||
unpack_7z_file(path, &extract_to)?;
|
||||
|
||||
let new_mod = ModConfig::new(id, id);
|
||||
|
||||
Ok(new_mod)
|
||||
}
|
||||
|
||||
fn unpack_7z_file(path: impl AsRef<Path>, to: impl AsRef<Path>) -> anyhow::Result<()> {
|
||||
sevenz_rust::decompress_file(path, to)?;
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user