Skip to content

Commit

Permalink
refactor: misc changes (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
lewisakura authored Sep 5, 2023
1 parent 399ecac commit f334f84
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 120 deletions.
3 changes: 2 additions & 1 deletion frontend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ dist/
node_modules/

# Auto generated Wails bindings
wailsjs/
wailsjs/
package.json.md5
54 changes: 5 additions & 49 deletions frontend/src/components/installer/Actions.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import FailureModal from "./FailureModal.svelte";
import SuccessModal from "./SuccessModal.svelte";
import * as Installer from "../../../wailsjs/go/installer/Installer";
import * as IPC from "./ipc";
type IPCCall = (typeof Installer)["Patch" | "Repair" | "Unpatch" | "InstallOpenAsar" | "UninstallOpenAsar"];
import * as Installer from "../../../wailsjs/go/installer/Installer";
export let path: string;
export let isOpenAsar: boolean = false;
Expand All @@ -21,54 +21,10 @@
export let busy = false;
function getOpPastTense(op: IPCCall) {
if (op === Installer.Patch) return "installed";
if (op === Installer.Repair) return "repaired";
if (op === Installer.Unpatch) return "uninstalled";
if (op === Installer.InstallOpenAsar) return "installed OpenAsar";
if (op === Installer.UninstallOpenAsar) return "uninstalled OpenAsar";
return "did something?";
}
async function doAction(op: IPCCall) {
async function doAction(op: IPC.IPCCall) {
busy = true;
await op(path)
.then(() => {
openWindow(
SuccessModal,
{
verb: getOpPastTense(op)
},
{
title: "Success",
width: 400,
height: 510
}
);
})
.catch(error => {
const message = typeof error === "string" ? error : null;
if (!message) console.error(error);
openWindow(
FailureModal,
{
message,
path,
op,
getOpPastTense,
onAction
},
{
title: "MASSIVE FAILURE !!!",
width: 450,
height: 200
}
);
})
.finally(() => {
busy = false;
onAction?.();
});
await IPC.doAction(op, path, onAction);
busy = false;
}
</script>

Expand Down
98 changes: 37 additions & 61 deletions frontend/src/components/installer/FailureModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,89 +5,65 @@
-->

<script lang="ts">
import { Environment, BrowserOpenURL } from "../../../wailsjs/runtime/runtime.js";
import * as Installer from "../../../wailsjs/go/installer/Installer.js";
import SuccessModal from "./SuccessModal.svelte";
import Heading from "../text/Heading.svelte";
import Button from "../Button.svelte";
import { closeWindow, openWindow } from "../windows/index.js";
import { closeWindow, resizeWindow } from "../windows/index.js";
import { BrowserOpenURL } from "../../../wailsjs/runtime/runtime.js";
type IPCCall = (typeof Installer)["Patch" | "Repair" | "Unpatch" | "InstallOpenAsar" | "UninstallOpenAsar"];
import * as IPC from "./ipc";
export let message: string | null = null;
export let path: string = "";
export let op: IPCCall;
export let getOpPastTense: (IPCCall) => string;
export let op: IPC.IPCCall;
export let onAction: () => void;
export let _windowId: string;
async function runAndShowSuccess() {
await op(path);
openWindow(
SuccessModal,
{
verb: getOpPastTense(op)
},
{
title: "Success",
width: 400,
height: 510
}
);
// closeWindow(id);
onAction();
if (!await Installer.PromptForChown(path)) return;
closeWindow(_windowId);
setTimeout(() => IPC.doAction(op, path, onAction));
}
let buttonPress: () => void;
let isOwned = true;
// svelte hacks...
async function CheckOwnership() {
isOwned = await Installer.CheckForOwnershipDarwin(path);
}
function waitForButton() {
return new Promise<void>(res => {
buttonPress = res;
});
$: if (!isOwned) {
resizeWindow(_windowId, 450, 410);
} else {
resizeWindow(_windowId, 450, 200);
}
</script>

<section role="dialog">
<Heading tag="h6" --color="var(--accent-red)">Oh no!</Heading>
<p>Unable to do the thing u were trying to do :&lpar;&lpar;&lpar;&lpar;&lpar;</p>
<p>Something went wrong!</p>
{#if message}
{#if message.includes("file exists") || message.includes("permission denied")}
{#await Installer.CheckForOwnershipDarwin(path)}
{#await CheckOwnership()}
<p>{message}</p>
{:then isOwned}
{:then}
{#if !isOwned}
{#await Environment()}
<p>{message}</p>
{:then env}
{#if env.platform === "darwin"}
{#await waitForButton()}
<p>Please allow vencord to fix discord's permissions</p>
<Button on:click={buttonPress}>Try again as admin</Button>
{:then}
{#await Installer.PromptForChown(path)}
<p>Please allow vencord to fix discord's permissions</p>
<Button on:click={() => {}}>Try again as admin</Button>
{:then}
{#await runAndShowSuccess()}
<p>Working...</p>
{:then}
<p>Success! You can close this window</p>
{:catch}
<p>Please grant the installer full disk access</p>
<Button
on:click={() =>
BrowserOpenURL(
"x-apple.systempreferences:com.apple.preference.security?Privacy_AllFiles"
)}>Open Security Settings</Button
>
{/await}
{:catch error}
<p>{error}</p>
{/await}
{/await}
{:else}
<p>{message}</p>
{/if}
{/await}
<p>Hmm... seems like you've encountered a Mac-specific problem! Usually, this is one of two things:</p>
<p>
Your Discord installation's permissions appear to be broken. Luckily, we offer a simple
tool to fix this.
</p>
<Button on:click={runAndShowSuccess}>Repair Permissions</Button>
<p>
Sometimes the installer needs Full Disk Access, though usually the above should suffice.
</p>
<Button on:click={() =>
BrowserOpenURL("x-apple.systempreferences:com.apple.preference.security?Privacy_AllFiles")}>
Open Security Settings
</Button>
<p>Or sometimes it's both! Try enabling Full Disk Access, then repairing permissions.</p>
{:else}
<p>{message}</p>
{/if}
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/installer/SuccessModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</script>

<section role="dialog">
<Heading tag="h6" --color="var(--accent-green)">Successfully {verb}!!</Heading>
<Heading tag="h6" --color="var(--accent-green)">Successfully {verb}!</Heading>
<p>Enjoy this shiggy:</p>
<img src="https://media.discordapp.net/stickers/1039992459209490513.png?size=240" alt="shiggy" />
</section>
Expand Down
55 changes: 55 additions & 0 deletions frontend/src/components/installer/ipc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import FailureModal from "./FailureModal.svelte";
import SuccessModal from "./SuccessModal.svelte";

import { openWindow } from "../windows";

import * as Installer from "../../../wailsjs/go/installer/Installer";

export type IPCCall = (typeof Installer)["Patch" | "Repair" | "Unpatch" | "InstallOpenAsar" | "UninstallOpenAsar"];

export function getOpPastTense(op: IPCCall) {
if (op === Installer.Patch) return "installed";
if (op === Installer.Repair) return "repaired";
if (op === Installer.Unpatch) return "uninstalled";
if (op === Installer.InstallOpenAsar) return "installed OpenAsar";
if (op === Installer.UninstallOpenAsar) return "uninstalled OpenAsar";
return "did something?";
}

export async function doAction(op: IPCCall, path: string, onAction: () => void) {
await op(path)
.then(() => {
openWindow(
SuccessModal,
{
verb: getOpPastTense(op)
},
{
title: "Woohoo!",
width: 400,
height: 510
}
);
})
.catch(error => {
const message = typeof error === "string" ? error : null;
if (!message) console.error(error);
openWindow(
FailureModal,
{
message,
path,
op,
onAction
},
{
title: "Uh oh!",
width: 450,
height: 200
}
);
})
.finally(() => {
onAction?.();
});
}
18 changes: 14 additions & 4 deletions frontend/src/components/windows/Window.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@
<div class="frame" class:maximized use:resize on:mousedown={onFocus} {style} in:transition out:transition>
<!-- svelte-ignore a11y-no-noninteractive-element-interactions -->
<div class="titlebar" role="application" on:mousedown={onDragStart}>
<div class="icon">
{#if icon}
{#if icon}
<div class="icon">
<svelte:component this={icon} />
{/if}
</div>
</div>
{/if}
<div class="title body sm">{title}</div>
<div class="spacer"></div>
<!-- svelte-ignore a11y-no-static-element-interactions -->
Expand Down Expand Up @@ -185,6 +185,16 @@
height: 1.25rem;
margin: 0 1rem;
}
.title {
margin-left: 1rem;
}
/* don't add margin if icon exists, as the icon will do it */
.icon + .title {
margin-left: 0;
}
.frame.maximized .icon,
.frame.maximized .title {
opacity: 0;
Expand Down
16 changes: 13 additions & 3 deletions frontend/src/components/windows/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,23 @@ export const windowStore = writable<Record<string, WindowInstance>>({});

export const openWindow = <T extends SvelteComponent>(
component: Constructor<T>,
props: ComponentProps<T>,
props: Omit<ComponentProps<T>, "_windowId">,
windowOptions: SetOptional<ComponentProps<Window>, "id">
) => {
const id = Math.random().toString(36).substring(7);
const newWindow: WindowInstance<T> = {
props: {
id: Math.random().toString(36).substring(7),
id,
...windowOptions
},
content: component,
contentProps: props
contentProps: { ...props, _windowId: id } as ComponentProps<T>
};
windowStore.update(windows => {
windows[newWindow.props.id] = newWindow;
return windows;
});
return newWindow.props.id;
};

export const closeWindow = (id: string) => {
Expand All @@ -46,6 +48,14 @@ export const closeWindow = (id: string) => {
});
};

export const resizeWindow = (id: string, w: number, h: number) => {
windowStore.update(windows => {
windows[id].props.width = w;
windows[id].props.height = h;
return windows;
});
}

let zIndex = 0;
export const getFocusZIndex = () => {
zIndex++;
Expand Down
5 changes: 4 additions & 1 deletion installer/find_discord_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"os/exec"
path "path/filepath"
"strings"
"syscall"
)

var windowsNames = map[string]string{
Expand Down Expand Up @@ -88,7 +89,9 @@ func PreparePatch(di *DiscordInstall) {
name := windowsNames[di.branch]
fmt.Println("Killing " + name + "...")

_ = exec.Command("powershell", "Stop-Process -Name "+name).Run()
kill_cmd := exec.Command("powershell", "Stop-Process -Name "+name)
kill_cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
kill_cmd.Run()
}

func FixOwnership(_ string) error {
Expand Down

0 comments on commit f334f84

Please sign in to comment.