Improvements for VencordDesktop (#847)

This commit is contained in:
V 2023-04-09 04:04:02 +02:00 committed by GitHub
parent bfa20f2634
commit 08822dd190
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 85 additions and 147 deletions

View File

@ -27,12 +27,14 @@ export { PlainSettings, Settings };
import "./utils/quickCss";
import "./webpack/patchWebpack";
import { relaunch } from "@utils/native";
import { showNotification } from "./api/Notifications";
import { PlainSettings, Settings } from "./api/settings";
import { patches, PMLogger, startAllPlugins } from "./plugins";
import { localStorage } from "./utils/localStorage";
import { getCloudSettings, putCloudSettings } from "./utils/settingsSync";
import { checkForUpdates, rebuild, update, UpdateLogger } from "./utils/updater";
import { checkForUpdates, rebuild, update,UpdateLogger } from "./utils/updater";
import { onceReady } from "./webpack";
import { SettingsRouter } from "./webpack/common";
@ -75,23 +77,14 @@ async function init() {
if (Settings.autoUpdate) {
await update();
const needsFullRestart = await rebuild();
await rebuild();
if (Settings.autoUpdateNotification)
setTimeout(() => showNotification({
title: "Vencord has been updated!",
body: "Click here to restart",
permanent: true,
noPersist: true,
onClick() {
if (needsFullRestart) {
if (IS_DISCORD_DESKTOP)
window.DiscordNative.app.relaunch();
else
window.VencordDesktop.app.relaunch();
}
else
location.reload();
}
onClick: relaunch
}), 10_000);
return;
}

View File

@ -125,7 +125,7 @@ function Updatable(props: CommonProps) {
onClick={withDispatcher(setIsUpdating, async () => {
if (await update()) {
setUpdates([]);
const needFullRestart = await rebuild();
await rebuild();
await new Promise<void>(r => {
Alerts.show({
title: "Update Success!",
@ -133,10 +133,7 @@ function Updatable(props: CommonProps) {
confirmText: "Restart",
cancelText: "Not now!",
onConfirm() {
if (needFullRestart)
relaunch();
else
location.reload();
relaunch();
r();
},
onCancel: r

1
src/globals.d.ts vendored
View File

@ -57,6 +57,7 @@ declare global {
*/
export var DiscordNative: any;
export var VencordDesktop: any;
export var VencordDesktopNative: any;
interface Window {
webpackChunkdiscord_app: {

View File

@ -32,9 +32,10 @@ if (IS_VENCORD_DESKTOP || !IS_VANILLA) {
if (url.endsWith("/")) url = url.slice(0, -1);
switch (url) {
case "renderer.js.map":
case "vencordDesktopRenderer.js.map":
case "preload.js.map":
case "patcher.js.map": // doubt
case "main.js.map":
case "patcher.js.map":
case "vencordDesktopMain.js.map":
cb(join(__dirname, url));
break;
default:

View File

@ -16,28 +16,13 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { createHash } from "crypto";
import { createReadStream } from "fs";
import { join } from "path";
export async function calculateHashes() {
const hashes = {} as Record<string, string>;
await Promise.all(
[IS_DISCORD_DESKTOP ? "patcher.js" : "main.js", "preload.js", "renderer.js", "renderer.css"].map(file => new Promise<void>(r => {
const fis = createReadStream(join(__dirname, file));
const hash = createHash("sha1", { encoding: "hex" });
fis.once("end", () => {
hash.end();
hashes[file] = hash.read();
r();
});
fis.pipe(hash);
}))
);
return hashes;
}
export const VENCORD_FILES = [
IS_DISCORD_DESKTOP ? "patcher.js" : "vencordDesktopMain.js",
"preload.js",
IS_DISCORD_DESKTOP ? "renderer.js" : "vencordDesktopRenderer.js",
"renderer.css"
];
export function serializeErrors(func: (...args: any[]) => any) {
return async function () {

View File

@ -22,7 +22,7 @@ import { ipcMain } from "electron";
import { join } from "path";
import { promisify } from "util";
import { calculateHashes, serializeErrors } from "./common";
import { serializeErrors } from "./common";
const VENCORD_SRC_DIR = join(__dirname, "..");
@ -76,7 +76,6 @@ async function build() {
return !res.stderr.includes("Build failed");
}
ipcMain.handle(IpcEvents.GET_HASHES, serializeErrors(calculateHashes));
ipcMain.handle(IpcEvents.GET_REPO, serializeErrors(getRepo));
ipcMain.handle(IpcEvents.GET_UPDATES, serializeErrors(calculateGitChanges));
ipcMain.handle(IpcEvents.UPDATE, serializeErrors(pull));

View File

@ -26,7 +26,7 @@ import gitHash from "~git-hash";
import gitRemote from "~git-remote";
import { get } from "../utils/simpleGet";
import { calculateHashes, serializeErrors } from "./common";
import { serializeErrors, VENCORD_FILES } from "./common";
const API_BASE = `https://api.github.com/repos/${gitRemote}`;
let PendingUpdates = [] as [string, string][];
@ -57,13 +57,6 @@ async function calculateGitChanges() {
}));
}
const FILES_TO_DOWNLOAD = [
IS_DISCORD_DESKTOP ? "patcher.js" : "vencordDesktopMain.js",
"preload.js",
IS_DISCORD_DESKTOP ? "renderer.js" : "vencordDesktopRenderer.js",
"renderer.css"
];
async function fetchUpdates() {
const release = await githubGet("/releases/latest");
@ -73,7 +66,7 @@ async function fetchUpdates() {
return false;
data.assets.forEach(({ name, browser_download_url }) => {
if (FILES_TO_DOWNLOAD.some(s => name.startsWith(s))) {
if (VENCORD_FILES.some(s => name.startsWith(s))) {
PendingUpdates.push([name, browser_download_url]);
}
});
@ -83,13 +76,7 @@ async function fetchUpdates() {
async function applyUpdates() {
await Promise.all(PendingUpdates.map(
async ([name, data]) => writeFile(
join(
__dirname,
IS_VENCORD_DESKTOP
// vencordDesktopRenderer.js -> renderer.js
? name.replace(/vencordDesktop(\w)/, (_, c) => c.toLowerCase())
: name
),
join(__dirname, name),
await get(data)
)
));
@ -97,7 +84,6 @@ async function applyUpdates() {
return true;
}
ipcMain.handle(IpcEvents.GET_HASHES, serializeErrors(calculateHashes));
ipcMain.handle(IpcEvents.GET_REPO, serializeErrors(() => `https://github.com/${gitRemote}`));
ipcMain.handle(IpcEvents.GET_UPDATES, serializeErrors(calculateGitChanges));
ipcMain.handle(IpcEvents.UPDATE, serializeErrors(fetchUpdates));

View File

@ -71,66 +71,63 @@ export default definePlugin({
makeSettingsCategories({ ID }: { ID: Record<string, unknown>; }) {
const makeOnClick = (tab: string) => () => SettingsRouter.open(tab);
const cats = [
return [
{
section: ID.HEADER,
label: "Vencord"
}, {
},
{
section: "VencordSettings",
label: "Vencord",
element: () => <SettingsComponent tab="VencordSettings" />,
onClick: makeOnClick("VencordSettings")
}, {
},
{
section: "VencordPlugins",
label: "Plugins",
element: () => <SettingsComponent tab="VencordPlugins" />,
onClick: makeOnClick("VencordPlugins")
}, {
},
{
section: "VencordThemes",
label: "Themes",
element: () => <SettingsComponent tab="VencordThemes" />,
onClick: makeOnClick("VencordThemes")
}
] as Array<{
section: unknown,
label?: string;
element?: React.ComponentType;
onClick?(): void;
}>;
if (!IS_WEB)
cats.push({
},
!IS_WEB && {
section: "VencordUpdater",
label: "Updater",
element: () => <SettingsComponent tab="VencordUpdater" />,
onClick: makeOnClick("VencordUpdater")
});
cats.push({
section: "VencordCloud",
label: "Cloud",
element: () => <SettingsComponent tab="VencordCloud" />,
onClick: makeOnClick("VencordCloud")
});
cats.push({
section: "VencordSettingsSync",
label: "Backup & Restore",
element: () => <SettingsComponent tab="VencordSettingsSync" />,
onClick: makeOnClick("VencordSettingsSync")
});
if (IS_DEV)
cats.push({
},
{
section: "VencordCloud",
label: "Cloud",
element: () => <SettingsComponent tab="VencordCloud" />,
onClick: makeOnClick("VencordCloud")
},
{
section: "VencordSettingsSync",
label: "Backup & Restore",
element: () => <SettingsComponent tab="VencordSettingsSync" />,
onClick: makeOnClick("VencordSettingsSync")
},
IS_DEV && {
section: "VencordPatchHelper",
label: "Patch Helper",
element: PatchHelper!,
onClick: makeOnClick("VencordPatchHelper")
});
cats.push({ section: ID.DIVIDER });
return cats;
},
IS_VENCORD_DESKTOP && {
section: "VencordDesktop",
label: "Desktop Settings",
element: VencordDesktop.Components.Settings,
onClick: makeOnClick("VencordDesktop")
},
{
section: ID.DIVIDER
}
].filter(Boolean);
},
options: {
@ -149,14 +146,6 @@ export default definePlugin({
},
},
tabs: {
vencord: () => <SettingsComponent tab="VencordSettings" />,
plugins: () => <SettingsComponent tab="VencordPlugins" />,
themes: () => <SettingsComponent tab="VencordThemes" />,
updater: () => <SettingsComponent tab="VencordUpdater" />,
sync: () => <SettingsComponent tab="VencordSettingsSync" />
},
get electronVersion() {
return VencordNative.getVersions().electron || window.armcord?.electron || null;
},
@ -175,7 +164,7 @@ export default definePlugin({
get additionalInfo() {
if (IS_DEV) return " (Dev)";
if (IS_WEB) return " (Web)";
if (IS_VENCORD_DESKTOP) return " (Vencord Desktop)";
if (IS_VENCORD_DESKTOP) return ` (VencordDesktop v${VencordDesktopNative.app.getVersion()})`;
if (IS_STANDALONE) return " (Standalone)";
return "";
},

View File

@ -44,11 +44,18 @@ export default definePlugin({
execute() {
const { RELEASE_CHANNEL } = window.GLOBAL_ENV;
const client = (() => {
if (IS_DISCORD_DESKTOP) return `Desktop v${DiscordNative.app.getVersion()}`;
if (IS_VENCORD_DESKTOP) return `Vencord Desktop v${VencordDesktopNative.app.getVersion()}`;
if ("armcord" in window) return `ArmCord v${window.armcord.version}`;
return `Web (${navigator.userAgent})`;
})();
const debugInfo = `
**Vencord Debug Info**
> Discord Branch: ${RELEASE_CHANNEL}
> Client: ${typeof DiscordNative === "undefined" ? window.armcord ? "Armcord" : `Web (${navigator.userAgent})` : `Desktop (Electron v${settings.electronVersion})`}
> Client: ${client}
> Platform: ${window.navigator.platform}
> Vencord Version: ${gitHash}${settings.additionalInfo}
> Outdated: ${isOutdated}

View File

@ -26,27 +26,23 @@ import VencordNative from "./VencordNative";
contextBridge.exposeInMainWorld("VencordNative", VencordNative);
// Discord
if (location.protocol !== "data:") {
// Discord
webFrame.executeJavaScript(readFileSync(join(__dirname, "renderer.js"), "utf-8"));
// #region cssInsert
const rendererCss = join(__dirname, "renderer.css");
function insertCss(css: string) {
const style = document.createElement("style");
style.id = "vencord-css-core";
style.textContent = css;
const style = document.createElement("style");
style.id = "vencord-css-core";
style.textContent = readFileSync(rendererCss, "utf-8");
if (document.readyState === "complete") {
document.documentElement.appendChild(style);
} else {
document.addEventListener("DOMContentLoaded", () => document.documentElement.appendChild(style), {
once: true
});
}
if (document.readyState === "complete") {
document.documentElement.appendChild(style);
} else {
document.addEventListener("DOMContentLoaded", () => document.documentElement.appendChild(style), {
once: true
});
}
const css = readFileSync(rendererCss, "utf-8");
insertCss(css);
if (IS_DEV) {
// persistent means keep process running if watcher is the only thing still running
// which we obviously don't want
@ -54,11 +50,14 @@ if (location.protocol !== "data:") {
document.getElementById("vencord-css-core")!.textContent = readFileSync(rendererCss, "utf-8");
});
}
// #endregion
if (process.env.DISCORD_PRELOAD)
if (process.env.DISCORD_PRELOAD) {
webFrame.executeJavaScript(readFileSync(join(__dirname, "renderer.js"), "utf-8"));
require(process.env.DISCORD_PRELOAD);
} else {
// Monaco Popout
}
} // Monaco popout
else {
contextBridge.exposeInMainWorld("setCss", debounce(s => VencordNative.ipc.invoke(IpcEvents.SET_QUICK_CSS, s)));
contextBridge.exposeInMainWorld("getCurrentCss", () => VencordNative.ipc.invoke(IpcEvents.GET_QUICK_CSS));
// shrug

View File

@ -40,7 +40,6 @@ export default strEnum({
OPEN_QUICKCSS: "VencordOpenQuickCss",
GET_UPDATES: "VencordGetUpdates",
GET_REPO: "VencordGetRepo",
GET_HASHES: "VencordGetHashes",
UPDATE: "VencordUpdate",
BUILD: "VencordBuild",
OPEN_MONACO_EDITOR: "VencordOpenMonacoEditor",

View File

@ -20,12 +20,12 @@ export function relaunch() {
if (IS_DISCORD_DESKTOP)
window.DiscordNative.app.relaunch();
else
window.VencordDesktop.app.relaunch();
window.VencordDesktopNative.app.relaunch();
}
export function showItemInFolder(path: string) {
if (IS_DISCORD_DESKTOP)
window.DiscordNative.fileManager.showItemInFolder(path);
else
window.VencordDesktop.fileManager.showItemInFolder(path);
window.VencordDesktopNative.fileManager.showItemInFolder(path);
}

View File

@ -62,24 +62,9 @@ export function getRepo() {
return Unwrap(VencordNative.ipc.invoke<IpcRes<string>>(IpcEvents.GET_REPO));
}
type Hashes = Record<"patcher.js" | "main.js" | "preload.js" | "renderer.js" | "renderer.css", string>;
/**
* @returns true if hard restart is required
*/
export async function rebuild() {
const oldHashes = await Unwrap(VencordNative.ipc.invoke<IpcRes<Hashes>>(IpcEvents.GET_HASHES));
if (!await Unwrap(VencordNative.ipc.invoke<IpcRes<boolean>>(IpcEvents.BUILD)))
throw new Error("The Build failed. Please try manually building the new update");
const newHashes = await Unwrap(VencordNative.ipc.invoke<IpcRes<Hashes>>(IpcEvents.GET_HASHES));
if (oldHashes["preload.js"] !== newHashes["preload.js"]) return true;
if (IS_DISCORD_DESKTOP && oldHashes["patcher.js"] !== newHashes["patcher.js"]) return true;
if (IS_VENCORD_DESKTOP && oldHashes["main.js"] !== newHashes["main.js"]) return true;
return false;
}
export async function maybePromptToUpdate(confirmMessage: string, checkForDev = false) {
@ -93,11 +78,8 @@ export async function maybePromptToUpdate(confirmMessage: string, checkForDev =
if (wantsUpdate && isNewer) return alert("Your local copy has more recent commits. Please stash or reset them.");
if (wantsUpdate) {
await update();
const needFullRestart = await rebuild();
if (needFullRestart)
relaunch();
else
location.reload();
await rebuild();
relaunch();
}
}
} catch (err) {