Skip to content

Commit

Permalink
Block patching until moonlight is installed
Browse files Browse the repository at this point in the history
  • Loading branch information
NotNite committed Dec 6, 2023
1 parent 03e002b commit 8add202
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 11 deletions.
21 changes: 17 additions & 4 deletions src-tauri/src/commands/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
types::{Error, MoonlightBranch},
version::{download_nightly, download_stable, get_nightly_version, get_stable_version},
};
use tauri::AppHandle;
use tauri::{AppHandle, Manager};

#[tauri::command]
pub fn get_moonlight_branch(app_handle: AppHandle) -> MoonlightBranch {
Expand Down Expand Up @@ -39,19 +39,29 @@ pub fn set_moonlight_branch(app_handle: AppHandle, branch: MoonlightBranch) -> R
Ok(())
}

fn installed_version_changed(app_handle: &AppHandle, version: Option<String>) {
app_handle
.emit_all("installed_version_changed", version)
.unwrap();
}

#[tauri::command]
pub fn get_downloaded_moonlight(app_handle: AppHandle) -> Option<String> {
let data_dir = get_data_dir(&app_handle);
if data_dir.is_err() {
installed_version_changed(&app_handle, None);
return None;
}

let version = data_dir.unwrap().join("version.txt");
if !version.exists() {
installed_version_changed(&app_handle, None);
return None;
}

std::fs::read_to_string(version).ok()
let version = std::fs::read_to_string(version).ok();
installed_version_changed(&app_handle, version.clone());
version
}

#[tauri::command]
Expand All @@ -75,9 +85,12 @@ pub fn download_moonlight(app_handle: AppHandle, branch: MoonlightBranch) -> Res
std::fs::create_dir_all(&dir)?;

match branch {
MoonlightBranch::Stable => download_stable(version_txt, dir)?,
MoonlightBranch::Nightly => download_nightly(version_txt, dir)?,
MoonlightBranch::Stable => download_stable(version_txt.clone(), dir)?,
MoonlightBranch::Nightly => download_nightly(version_txt.clone(), dir)?,
}

let installed_version = std::fs::read_to_string(version_txt)?;
installed_version_changed(&app_handle, Some(installed_version));

Ok(())
}
32 changes: 27 additions & 5 deletions src/components/Patchers.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { invoke } from "@tauri-apps/api";
import { DetectedInstall } from "../types";
import React from "react";
import { listen } from "@tauri-apps/api/event";

function Install({ install }: { install: DetectedInstall }) {
const [patched, setPatched] = React.useState<boolean | null>(null);
const [locked, setLocked] = React.useState<boolean>(false);
const [installedVersion, setInstalledVersion] = React.useState<string | null>(
null
);

React.useEffect(() => {
async function updatePatched() {
Expand All @@ -15,6 +19,21 @@ function Install({ install }: { install: DetectedInstall }) {
updatePatched();
}, []);

// how the fuck do Tauri events work
React.useEffect(() => {
let unlisten: () => void = () => {};

listen("installed_version_changed", (event) => {
setInstalledVersion(event.payload as string | null);
}).then((unlistenFn) => {
unlisten = unlistenFn;
});

return () => {
unlisten();
};
}, []);

async function togglePatch() {
setLocked(true);

Expand All @@ -33,12 +52,13 @@ function Install({ install }: { install: DetectedInstall }) {

return (
<div className="install">
<h3>
Discord {install.branch}
</h3>
<h3>Discord {install.branch}</h3>

{install != null && (
<button onClick={togglePatch} disabled={locked}>
<button
onClick={togglePatch}
disabled={locked || installedVersion == null}
>
{patched ? "Unpatch" : "Patch"}
</button>
)}
Expand All @@ -59,7 +79,9 @@ export default function Patchers() {

return (
<div className="install-list">
{installs.map((install, i) => <Install install={install} key={i} />)}
{installs.map((install, i) => (
<Install install={install} key={i} />
))}
</div>
);
}
7 changes: 5 additions & 2 deletions src/components/Updater.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,11 @@ export default function Updater() {
</select>
)}

{branch != null && currentVersion != null && (
<span>Installed version: {preview(currentVersion)}</span>
{branch != null && (
<span>
Installed version:{" "}
{currentVersion == null ? "None" : preview(currentVersion)}
</span>
)}
{branch != null && latestVersion != null && (
<span>Latest version: {preview(latestVersion)}</span>
Expand Down
10 changes: 10 additions & 0 deletions src/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ button:hover {
background-color: var(--color0);
}

button:disabled {
color: var(--fg);
outline: 2px solid var(--purple);
background-color: var(--color0);
}

button:disabled:hover {
cursor: not-allowed;
}

h3 {
margin: 0;
}

0 comments on commit 8add202

Please sign in to comment.