save instances in map & internalized save path
This commit is contained in:
11
src/main.rs
11
src/main.rs
@@ -41,8 +41,7 @@ fn command_add(root_config: &RootConfig, instance_id: &str, mod_id: &str) -> any
|
||||
|
||||
insert_mod_to_instance(&mut instance, mod_to_install, &files, 0);
|
||||
|
||||
let path = &root_config.get_instance_config(instance_id).unwrap().path;
|
||||
instance.save_to_file(path)?;
|
||||
instance.save_to_file()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -55,13 +54,7 @@ fn command_order(root_config: &RootConfig, instance_id: &str) -> anyhow::Result<
|
||||
|
||||
instance.set_load_order(new_load_order);
|
||||
|
||||
instance.save_to_file(
|
||||
root_config
|
||||
.get_instance_config(instance_id)
|
||||
.ok_or(anyhow!("Failed to find instance"))?
|
||||
.path
|
||||
.clone(),
|
||||
)?;
|
||||
instance.save_to_file()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use std::{
|
||||
fs::{self, read_to_string},
|
||||
io::Write,
|
||||
path::Path,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use log::trace;
|
||||
@@ -22,6 +22,9 @@ pub struct ModdedInstance {
|
||||
|
||||
#[serde(default)]
|
||||
game_file_overrides: Vec<Link>,
|
||||
|
||||
#[serde(skip)]
|
||||
self_path: PathBuf,
|
||||
}
|
||||
|
||||
impl ModdedInstance {
|
||||
@@ -31,20 +34,22 @@ impl ModdedInstance {
|
||||
path.as_ref().to_string_lossy()
|
||||
);
|
||||
|
||||
let data = read_to_string(path)?;
|
||||
let config = toml::from_str(&data)?;
|
||||
let data = read_to_string(&path)?;
|
||||
let mut config: Self = toml::from_str(&data)?;
|
||||
|
||||
config.self_path = path.as_ref().to_owned();
|
||||
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
pub fn save_to_file(&self, path: impl AsRef<Path>) -> Result<(), ConfigReadWriteError> {
|
||||
pub fn save_to_file(&self) -> Result<(), ConfigReadWriteError> {
|
||||
trace!(
|
||||
"Saving ModdedInstance to: {}",
|
||||
path.as_ref().to_string_lossy()
|
||||
self.self_path.to_string_lossy()
|
||||
);
|
||||
|
||||
let content = toml::to_string_pretty(self)?;
|
||||
let mut file = fs::File::create(path)?;
|
||||
let mut file = fs::File::create(&self.self_path)?;
|
||||
write!(file, "{}", content)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ pub struct RootConfig {
|
||||
mod_location: PathBuf,
|
||||
|
||||
#[serde(default)]
|
||||
instances: Vec<InstancePointer>,
|
||||
instances: HashMap<String, InstancePointer>,
|
||||
|
||||
/// All available mods
|
||||
#[serde(default)]
|
||||
@@ -48,23 +48,19 @@ impl RootConfig {
|
||||
|
||||
pub fn load_instance_by_id(&self, id: &str) -> Result<ModdedInstance, ConfigReadWriteError> {
|
||||
let conf = self
|
||||
.get_instance_config(id)
|
||||
.instances
|
||||
.get(id)
|
||||
.ok_or(ConfigReadWriteError::IDNotFound)?;
|
||||
|
||||
ModdedInstance::load_from_file(&conf.path)
|
||||
}
|
||||
|
||||
pub fn get_instance_config(&self, id: &str) -> Option<&InstancePointer> {
|
||||
self.instances.iter().find(|e| e.id == id)
|
||||
}
|
||||
|
||||
pub fn mod_location(&self) -> &Path {
|
||||
&self.mod_location
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct InstancePointer {
|
||||
pub id: String,
|
||||
pub path: PathBuf,
|
||||
struct InstancePointer {
|
||||
path: PathBuf,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user