Switch to using an instance directory

This commit is contained in:
Warren Hood 2024-09-01 13:45:44 +02:00
parent 042bfb8baa
commit db4b2bd694
3 changed files with 53 additions and 50 deletions

View file

@ -53,7 +53,7 @@ enum Commands {
#[arg(long)] #[arg(long)]
providers: Vec<ModProvider>, providers: Vec<ModProvider>,
}, },
/// Add a new mod to the modpack /// Add a new mod or to the modpack
Add { Add {
/// Name of the mod to add to the project, optionally including a version /// Name of the mod to add to the project, optionally including a version
name: String, name: String,
@ -134,9 +134,9 @@ enum ProfileCommands {
/// A local file path to a modpack directory or a git repo url prefixed with 'git+' /// A local file path to a modpack directory or a git repo url prefixed with 'git+'
#[arg(long, short)] #[arg(long, short)]
pack_source: PackSource, pack_source: PackSource,
/// Mods directory /// Instance directory (containing a mods folder)
#[arg(long, short)] #[arg(long, short)]
mods_directory: PathBuf, instance_directory: PathBuf,
}, },
/// Install a profile /// Install a profile
Install { Install {
@ -405,10 +405,10 @@ async fn main() -> anyhow::Result<()> {
name, name,
side, side,
pack_source, pack_source,
mods_directory, instance_directory,
} => { } => {
let mut userdata = profiles::Data::load()?; let mut userdata = profiles::Data::load()?;
let profile = Profile::new(&mods_directory, pack_source, side); let profile = Profile::new(&instance_directory, pack_source, side);
userdata.add_profile(&name, profile); userdata.add_profile(&name, profile);
userdata.save()?; userdata.save()?;
println!("Saved profile '{name}'"); println!("Saved profile '{name}'");
@ -442,7 +442,7 @@ async fn main() -> anyhow::Result<()> {
anyhow::bail!("Profile '{name}' does not exist") anyhow::bail!("Profile '{name}' does not exist")
}; };
println!("Profile name : {name}"); println!("Profile name : {name}");
println!("Mods folder : {}", profile.mods_folder.display()); println!("Instance folder : {}", profile.instance_folder.display());
println!("Modpack source: {}", profile.pack_source); println!("Modpack source: {}", profile.pack_source);
println!("Side : {}", profile.side); println!("Side : {}", profile.side);
} }

View file

@ -43,15 +43,15 @@ impl Display for PackSource {
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Profile { pub struct Profile {
pub mods_folder: PathBuf, pub instance_folder: PathBuf,
pub pack_source: PackSource, pub pack_source: PackSource,
pub side: DownloadSide, pub side: DownloadSide,
} }
impl Profile { impl Profile {
pub fn new(mods_folder: &Path, pack_source: PackSource, side: DownloadSide) -> Self { pub fn new(instance_folder: &Path, pack_source: PackSource, side: DownloadSide) -> Self {
Self { Self {
mods_folder: mods_folder.into(), instance_folder: instance_folder.into(),
pack_source, pack_source,
side, side,
} }
@ -70,7 +70,7 @@ impl Profile {
}; };
pack_lock pack_lock
.download_mods(&self.mods_folder, self.side) .download_mods(&self.instance_folder.join("mods"), self.side)
.await?; .await?;
Ok(()) Ok(())
} }
@ -118,8 +118,7 @@ impl Data {
if let Some(home_dir) = home_dir { if let Some(home_dir) = home_dir {
Ok(home_dir) Ok(home_dir)
} } else {
else {
anyhow::bail!("Unable to locate home directory") anyhow::bail!("Unable to locate home directory")
} }
} }

View file

@ -25,7 +25,7 @@ struct ManagerGUI {
previous_view: ManagerView, previous_view: ManagerView,
profile_edit_settings: ProfileSettings, profile_edit_settings: ProfileSettings,
profile_save_error: Option<String>, profile_save_error: Option<String>,
current_install_status: ProfileInstallStatus current_install_status: ProfileInstallStatus,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -41,7 +41,7 @@ enum ManagerView {
/// The current application view /// The current application view
struct ProfileSettings { struct ProfileSettings {
name: String, name: String,
mods_dir: Option<PathBuf>, instance_dir: Option<PathBuf>,
pack_source: String, pack_source: String,
side: DownloadSide, side: DownloadSide,
} }
@ -50,7 +50,7 @@ impl Default for ProfileSettings {
fn default() -> Self { fn default() -> Self {
Self { Self {
name: Default::default(), name: Default::default(),
mods_dir: Default::default(), instance_dir: Default::default(),
pack_source: Default::default(), pack_source: Default::default(),
side: DownloadSide::Client, side: DownloadSide::Client,
} }
@ -60,12 +60,15 @@ impl Default for ProfileSettings {
impl TryFrom<ProfileSettings> for profiles::Profile { impl TryFrom<ProfileSettings> for profiles::Profile {
type Error = String; type Error = String;
fn try_from(value: ProfileSettings) -> Result<Self, Self::Error> { fn try_from(value: ProfileSettings) -> Result<Self, Self::Error> {
let mods_dir = value let instance_dir = value
.mods_dir .instance_dir
.ok_or(format!("A mods directory is required"))?; .ok_or(format!("An instance directory is required"))?;
if !instance_dir.join("mods").exists() {
return Err(format!("Instance folder {} does not seem to contain a mods directory. Are you sure this is a valid instance directory?", instance_dir.display()));
}
let pack_source = value.pack_source; let pack_source = value.pack_source;
Ok(profiles::Profile::new( Ok(profiles::Profile::new(
&mods_dir, &instance_dir,
profiles::PackSource::from_str(&pack_source)?, profiles::PackSource::from_str(&pack_source)?,
value.side, value.side,
)) ))
@ -81,13 +84,13 @@ impl Default for ManagerView {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
enum Message { enum Message {
SwitchView(ManagerView), SwitchView(ManagerView),
BrowseModsDir, BrowseInstanceDir,
EditProfileName(String), EditProfileName(String),
EditPackSource(String), EditPackSource(String),
SaveProfile, SaveProfile,
DeleteProfile(String), DeleteProfile(String),
InstallProfile(String), InstallProfile(String),
ProfileInstalled(ProfileInstallStatus) ProfileInstalled(ProfileInstallStatus),
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -95,7 +98,7 @@ enum ProfileInstallStatus {
NotStarted, NotStarted,
Installing, Installing,
Success, Success,
Error(String) Error(String),
} }
impl Default for ProfileInstallStatus { impl Default for ProfileInstallStatus {
@ -158,8 +161,8 @@ impl Application for ManagerGUI {
self.profile_edit_settings.name = profile.trim().into(); self.profile_edit_settings.name = profile.trim().into();
if let Some(loaded_profile) = loaded_profile { if let Some(loaded_profile) = loaded_profile {
self.profile_edit_settings.name = profile.into(); self.profile_edit_settings.name = profile.into();
self.profile_edit_settings.mods_dir = self.profile_edit_settings.instance_dir =
Some(loaded_profile.mods_folder.clone()); Some(loaded_profile.instance_folder.clone());
self.profile_edit_settings.pack_source = self.profile_edit_settings.pack_source =
loaded_profile.pack_source.to_string(); loaded_profile.pack_source.to_string();
self.profile_edit_settings.side = loaded_profile.side; self.profile_edit_settings.side = loaded_profile.side;
@ -172,9 +175,9 @@ impl Application for ManagerGUI {
self.current_view = view; self.current_view = view;
Command::none() Command::none()
} }
Message::BrowseModsDir => { Message::BrowseInstanceDir => {
self.profile_edit_settings.mods_dir = rfd::FileDialog::new() self.profile_edit_settings.instance_dir = rfd::FileDialog::new()
.set_title("Select your mods folder") .set_title("Select your instance folder")
.pick_folder(); .pick_folder();
Command::none() Command::none()
} }
@ -235,24 +238,21 @@ impl Application for ManagerGUI {
let result = profile.install().await; let result = profile.install().await;
if let Err(err) = result { if let Err(err) = result {
ProfileInstallStatus::Error(format!("{}", err)) ProfileInstallStatus::Error(format!("{}", err))
} } else {
else {
ProfileInstallStatus::Success ProfileInstallStatus::Success
} }
} } else {
else {
ProfileInstallStatus::Error(format!("Profile '{}' doesn't exist", name)) ProfileInstallStatus::Error(format!("Profile '{}' doesn't exist", name))
} }
}, },
Message::ProfileInstalled Message::ProfileInstalled,
) )
}, }
Message::ProfileInstalled(result) => { Message::ProfileInstalled(result) => {
self.current_install_status = result; self.current_install_status = result;
Command::none() Command::none()
}, }
} }
} }
@ -325,8 +325,11 @@ impl ManagerGUI {
] ]
.spacing(5), .spacing(5),
row![ row![
"Mods directory", "Instance folder",
text_input("Mods directory", &profile.mods_folder.display().to_string()), text_input(
"Instance folder",
&profile.instance_folder.display().to_string()
),
] ]
.spacing(20), .spacing(20),
row!["Mods to download", text(profile.side),].spacing(5), row!["Mods to download", text(profile.side),].spacing(5),
@ -354,17 +357,18 @@ impl ManagerGUI {
} }
match &self.current_install_status { match &self.current_install_status {
ProfileInstallStatus::NotStarted => {}, ProfileInstallStatus::NotStarted => {}
ProfileInstallStatus::Installing => { ProfileInstallStatus::Installing => {
profile_view = profile_view.push(text("Installing...")); profile_view = profile_view.push(text("Installing..."));
}, }
ProfileInstallStatus::Success => { ProfileInstallStatus::Success => {
profile_view = profile_view.push(text("Installed")); profile_view = profile_view.push(text("Installed"));
}, }
ProfileInstallStatus::Error(err) => { ProfileInstallStatus::Error(err) => {
profile_view = profile_view.push(text(format!("Failed to install profile: {}", err))); profile_view =
}, profile_view.push(text(format!("Failed to install profile: {}", err)));
}; }
};
profile_view profile_view
.align_items(Alignment::Center) .align_items(Alignment::Center)
@ -379,8 +383,8 @@ impl ManagerGUI {
previous_view: ManagerView, previous_view: ManagerView,
can_edit_name: bool, can_edit_name: bool,
) -> Element<Message> { ) -> Element<Message> {
let current_mods_directory_display = match &self.profile_edit_settings.mods_dir { let current_instance_directory_display = match &self.profile_edit_settings.instance_dir {
Some(mods_dir) => mods_dir.display().to_string(), Some(instance_dir) => instance_dir.display().to_string(),
None => String::from(""), None => String::from(""),
}; };
let mut profile_editor = column![ let mut profile_editor = column![
@ -405,12 +409,12 @@ impl ManagerGUI {
] ]
.spacing(5), .spacing(5),
row![ row![
"Mods directory", "Instance directory",
text_input( text_input(
"Browse for your MC instance's mods directory", "Browse for your MC instance directory (contains your mods folder)",
&current_mods_directory_display &current_instance_directory_display
), ),
button("Browse").on_press(Message::BrowseModsDir) button("Browse").on_press(Message::BrowseInstanceDir)
] ]
.spacing(5), .spacing(5),
row![ row![