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