Skip to content

Commit

Permalink
feat: prompt for launcher on first launch
Browse files Browse the repository at this point in the history
  • Loading branch information
Commander07 committed Jan 17, 2024
1 parent 6916daf commit 1be756a
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
100 changes: 99 additions & 1 deletion src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,91 @@ fn Settings<'a>(cx: Scope<'a, SettingsProps<'a>>) -> Element {
})
}

#[derive(Props, PartialEq)]
struct LauncherProps<'a> {
config: &'a UseRef<super::Config>,
first_launch: &'a UseState<bool>,
config_path: &'a PathBuf,
error: &'a UseRef<Option<String>>,
b64_id: String,
}

fn Launcher<'a>(cx: Scope<'a, LauncherProps<'a>>) -> Element {
let mut vanilla = None;
let mut multimc = None;
let mut prism = None;
let launcher = cx.props.config.with(|cfg| cfg.launcher.clone());
match &*launcher {
"vanilla" => vanilla = Some("true"),
"multimc-MultiMC" => multimc = Some("true"),
"multimc-PrismLauncher" => prism = Some("true"),
_ => {}
}
cx.render(rsx! {
div {
class: "version-inner-container",
style: "width: 25.25vw;",
div {
class: "container",
style: "width: 24vw;",
form {
id: "settings",
onsubmit: move |event| {
cx.props.config.with_mut(|cfg| cfg.launcher = event.data.values["launcher-select"].clone());
let res = std::fs::write(cx.props.config_path, serde_json::to_vec(&*cx.props.config.read()).unwrap());
match res {
Ok(_) => {},
Err(e) => {
cx.props.error.set(Some(format!("{:#?}", e) + " (Failed to write config!)"));
},
}
cx.props.first_launch.set(false);
},
div {
class: "label",
span {
"Launcher:"
}
select {
name: "launcher-select",
id: "launcher-select",
form: "settings",
class: "credits-button",
if super::get_minecraft_folder().is_dir() {
rsx!(option {
value: "vanilla",
selected: vanilla,
"Vanilla"
})
}
if super::get_multimc_folder("MultiMC").is_ok() {
rsx!(option {
value: "multimc-MultiMC",
selected: multimc,
"MultiMC"
})
}
if super::get_multimc_folder("PrismLauncher").is_ok() {
rsx!(option {
value: "multimc-PrismLauncher",
selected: prism,
"Prism Launcher"
})
}
}
}
input {
r#type: "submit",
value: "Continue",
class: "install-button",
id: "save"
}
}
}
}
})
}

fn feature_change(
installer_profile: &UseRef<InstallerProfile>,
modify: &UseState<bool>,
Expand Down Expand Up @@ -585,6 +670,7 @@ pub(crate) fn App<'a>(cx: Scope<'a, AppProps>) -> Element<'a> {
let branches = &cx.props.branches;
let config: &UseRef<super::Config> = use_ref(cx, || cx.props.config.clone());
let settings: &UseState<bool> = use_state(cx, || false);
let first_launch: &UseState<bool> = use_state(cx, || config.with(|x| x.first_launch.unwrap_or(true)));
let cog = String::from("data:image/png;base64,") + include_str!("assets/cog_icon.png.base64");
let err: &UseRef<Option<String>> = use_ref(cx, || None);
let name = use_ref(cx, || String::default());
Expand Down Expand Up @@ -622,7 +708,19 @@ pub(crate) fn App<'a>(cx: Scope<'a, AppProps>) -> Element<'a> {
}
}
}
} else {
} else if **first_launch {
rsx!(div {
class: "fake-body",
Launcher {
config: config,
first_launch: first_launch,
config_path: &cx.props.config_path,
error: err,
b64_id: engine::general_purpose::URL_SAFE_NO_PAD.encode(modpack_source)
}
})
}
else {
rsx!{
button {
class: "settings-button",
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ trait Downloadable {
#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)]
struct Config {
launcher: String,
first_launch: Option<bool>, // option for backwars compatibiliy
}

#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)]
Expand Down Expand Up @@ -1577,6 +1578,7 @@ fn main() {
} else {
config = Config {
launcher: String::from("vanilla"),
first_launch: Some(true),
};
fs::create_dir_all(config_path.parent().unwrap()).expect("Failed to create config dir!");
fs::write(&config_path, serde_json::to_vec(&config).unwrap())
Expand Down

0 comments on commit 1be756a

Please sign in to comment.