Skip to content

Commit

Permalink
implement custom mod adding/removing/toggling
Browse files Browse the repository at this point in the history
  • Loading branch information
SenkJu committed Dec 27, 2023
1 parent 0607ab6 commit bf8b1b6
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 25 deletions.
7 changes: 3 additions & 4 deletions src-tauri/src/app/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ async fn get_custom_mods(branch: &str, mc_version: &str) -> Result<Vec<LoaderMod
// todo: pull name from JAR manifest
let file_name_without_extension = file_name.replace(".jar", "");

mods.push(LoaderMod { required: true, enabled: true, name: file_name_without_extension, source: ModSource::Local { file_name } });
mods.push(LoaderMod { required: false, enabled: true, name: file_name_without_extension, source: ModSource::Local { file_name } });
}
}

Expand All @@ -163,9 +163,8 @@ async fn install_custom_mod(branch: &str, mc_version: &str, path: PathBuf) -> Re
fs::create_dir_all(&mod_cache_path).await.unwrap();
}

if let Some(path) = path.file_name() {
let file_name = path.to_str().unwrap();
let dest_path = mod_cache_path.join(file_name);
if let Some(file_name) = path.file_name() {
let dest_path = mod_cache_path.join(file_name.to_str().unwrap());

fs::copy(path, dest_path).await
.map_err(|e| format!("unable to copy custom mod: {:?}", e))?;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/main/LaunchArea.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
</pre>

<div class="version-selector">
<ButtonVersion icon="lb" title={lbVersion.title} subtitle="LiquidBounce" on:click={() => dispatch("showVersionSelect")} />
<ButtonVersion icon="mc" title={mcVersion.title} subtitle="Minecraft" on:click={() => dispatch("showVersionSelect")} />
<ButtonVersion icon="lb" title={lbVersion} subtitle="LiquidBounce" on:click={() => dispatch("showVersionSelect")} />
<ButtonVersion icon="mc" title={mcVersion} subtitle="Minecraft" on:click={() => dispatch("showVersionSelect")} />
</div>

{#if running}
Expand Down
65 changes: 49 additions & 16 deletions src/lib/main/MainScreen.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import LauncherVersion from "../settings/LauncherVersion.svelte";
import IconButtonSetting from "../settings/IconButtonSetting.svelte";
import CustomModSetting from "../settings/CustomModSetting.svelte";
import { open as dialogOpen } from "@tauri-apps/api/dialog";
export let options;
Expand Down Expand Up @@ -87,8 +88,15 @@
return builds.find((build) => build.buildId === options.preferredBuild);
}
let lbVersion = {};
let mcVersion = {};
let lbVersion = "";
let mcVersion = "";
let branch = "";
let additionalModsTitle = "";
$: {
additionalModsTitle = `Additional mods for ${branch} ${mcVersion}`;
}
let mods = [];
let customMods = [];
Expand Down Expand Up @@ -116,14 +124,9 @@
let b = getBuild();
console.debug("Updating build data", b);
lbVersion = {
date: b.date,
title: b.lbVersion
};
mcVersion = {
date: "", // todo: No date for MC version
title: b.mcVersion
};
branch = b.branch;
lbVersion = b.lbVersion;
mcVersion = b.mcVersion;
// Update changelog
invoke("fetch_changelog", { buildId: b.buildId })
Expand Down Expand Up @@ -159,11 +162,10 @@
invoke("get_custom_mods", { branch, mcVersion })
.then(result => {
console.log("Fetched custom mods", result);
customMods = result;
if (branchOptions) {
mods.forEach(mod => {
customMods.forEach(mod => {
mod.enabled = branchOptions.customModStates[mod.name] ?? mod.enabled;
});
}
Expand Down Expand Up @@ -207,6 +209,8 @@
let build = getBuild();
console.debug("Running build", build);
console.log([...mods, ...customMods])
await invoke("run_client", { buildId: build.buildId, accountData: options.currentAccount, options: options, mods: [...mods, ...customMods] });
}
Expand Down Expand Up @@ -234,7 +238,12 @@
return map;
}, {});
console.debug("Updated mod states", branchOptions.modStates);
branchOptions.customModStates = customMods.reduce(function(map, mod) {
map[mod.name] = mod.enabled;
return map;
}, {});
console.log("Updated mod states", branchOptions);
options.branchOptions[options.preferredBranch] = branchOptions;
options.store();
}
Expand All @@ -259,6 +268,30 @@
alert("Failed to get data folder: " + e);
console.error(e)
});
async function handleCustomModDelete(e) {
const { branch, mcVersion, subsystem } = getBuild();
await invoke("delete_custom_mod", { branch, mcVersion, modName: `${e.detail.name}.jar` });
requestMods(branch, mcVersion, subsystem);
}
async function handleInstallMod(e) {
const { branch, mcVersion, subsystem } = getBuild();
const selected = await dialogOpen({
directory: false,
multiple: false,
filters: [{ name: "", extensions: ["jar"] }],
title: "Select a custom mod to install"
});
if (selected) {
await invoke("install_custom_mod", { branch, mcVersion, path: selected });
requestMods(branch, mcVersion, subsystem);
}
}
</script>
{#if clientLogShown}
Expand Down Expand Up @@ -288,13 +321,13 @@
<ToggleSetting title={m.name} bind:value={m.enabled} disabled={m.required} on:change={updateModStates} />
{/each}
</SettingWrapper>
<SettingWrapper title="Additional mods for nextgen-1.20.4">
<SettingWrapper title={additionalModsTitle}>
<div slot="title-element">
<IconButtonSetting text="Install" icon="icon-plus" />
<IconButtonSetting text="Install" icon="icon-plus" on:click={handleInstallMod} />
</div>
{#each customMods as m}
<CustomModSetting title={m.name} value={m.enabled} on:change={updateModStates} />
<CustomModSetting title={m.name} bind:value={m.enabled} on:change={updateModStates} on:delete={handleCustomModDelete} />
{/each}
</SettingWrapper>
</SettingsContainer>
Expand Down
7 changes: 5 additions & 2 deletions src/lib/settings/CustomModSetting.svelte
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<script>
import { createEventDispatcher } from "svelte";
import ToggleSetting from "./ToggleSetting.svelte";
export let value;
export let title;
const dispatch = createEventDispatcher();
</script>

<div class="custom-mod-setting">
<ToggleSetting {value} {title} disabled={false} />
<button class="button-delete">
<ToggleSetting bind:value={value} {title} disabled={false} on:change />
<button class="button-delete" on:click={() => dispatch("delete", { name: title })}>
<img src="img/icon/icon-button-close.svg" alt="delete" title="Remove mod">
</button>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/lib/settings/IconButtonSetting.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
const dispatch = createEventDispatcher();
</script>

<button class="button" type="button" on:click={e => dispatch("click", e)}>
<button class="button" type="button" on:click={() => dispatch("click")}>
<img class="icon" src="img/icon/{icon}.svg" alt={icon}>
<span>{text}</span>
</button>
Expand Down

0 comments on commit bf8b1b6

Please sign in to comment.