Skip to content

Commit

Permalink
Merge pull request #87 from moonlight-mod/notnite/update-checking
Browse files Browse the repository at this point in the history
[moonbase] Update checking

Closes #65
  • Loading branch information
Cynosphere authored Oct 8, 2024
2 parents fc419b4 + fbc9807 commit 094cd83
Show file tree
Hide file tree
Showing 26 changed files with 485 additions and 47 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ jobs:
- name: Build moonlight
env:
NODE_ENV: production
MOONLIGHT_BRANCH: nightly
MOONLIGHT_VERSION: ${{ github.sha }}
run: pnpm run build

- name: Write ref/commit to file
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ jobs:
- name: Build moonlight
env:
NODE_ENV: production
MOONLIGHT_BRANCH: stable
MOONLIGHT_VERSION: ${{ github.ref_name }}
run: pnpm run build
- name: Create archive
run: |
Expand Down
7 changes: 6 additions & 1 deletion build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const watch = process.argv.includes("--watch");
const browser = process.argv.includes("--browser");
const mv2 = process.argv.includes("--mv2");

const buildBranch = process.env.MOONLIGHT_BRANCH ?? "dev";
const buildVersion = process.env.MOONLIGHT_VERSION ?? "dev";

const external = [
"electron",
"fs",
Expand Down Expand Up @@ -96,7 +99,9 @@ async function build(name, entry) {

const define = {
MOONLIGHT_ENV: `"${name}"`,
MOONLIGHT_PROD: prod.toString()
MOONLIGHT_PROD: prod.toString(),
MOONLIGHT_BRANCH: `"${buildBranch}"`,
MOONLIGHT_VERSION: `"${buildVersion}"`
};

for (const iterName of [
Expand Down
1 change: 1 addition & 0 deletions packages/browser/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
],
"host_permissions": [
"https://moonlight-mod.github.io/*",
"https://api.github.com/*",
"https://*.discord.com/*"
],
"content_scripts": [
Expand Down
2 changes: 2 additions & 0 deletions packages/browser/manifestv2.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"scripting",
"webNavigation",
"https://*.discord.com/assets/*.js",
"https://moonlight-mod.github.io/*",
"https://api.github.com/*",
"https://*.discord.com/*"
],
"background": {
Expand Down
6 changes: 6 additions & 0 deletions packages/browser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ window._moonlightBrowserInit = async () => {
processedExtensions,
nativesCache: {},

version: MOONLIGHT_VERSION,
branch: MOONLIGHT_BRANCH,

getConfig,
getConfigOption: <T>(ext: string, name: string) => {
const config = getConfig(ext);
Expand All @@ -116,6 +119,9 @@ window._moonlightBrowserInit = async () => {
return new Logger(id);
},

getMoonlightDir() {
return "/";
},
getExtensionDir: (ext: string) => {
return `/extensions/${ext}`;
},
Expand Down
3 changes: 3 additions & 0 deletions packages/core-extensions/src/moonbase/consts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const githubRepo = "moonlight-mod/moonlight";
export const nightlyRefUrl = "https://moonlight-mod.github.io/moonlight/ref";
export const userAgent = `moonlight/${moonlightNode.version} (https://github.com/moonlight-mod/moonlight)`;
20 changes: 17 additions & 3 deletions packages/core-extensions/src/moonbase/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ExtensionWebExports } from "@moonlight-mod/types";
import { ExtensionWebpackModule } from "@moonlight-mod/types";

export const webpackModules: ExtensionWebExports["webpackModules"] = {
export const webpackModules: Record<string, ExtensionWebpackModule> = {
stores: {
dependencies: [
{ id: "discord/packages/flux" },
Expand Down Expand Up @@ -29,10 +29,24 @@ export const webpackModules: ExtensionWebExports["webpackModules"] = {
{ ext: "moonbase", id: "ui" }
],
entrypoint: true
},

updates: {
dependencies: [
{ id: "react" },
{ ext: "moonbase", id: "stores" },
{ ext: "notices", id: "notices" },
{
ext: "spacepack",
id: "spacepack"
}
],
entrypoint: true
}
};

export const styles = [
".moonbase-settings > :first-child { margin-top: 0px; }",
"textarea.moonbase-resizeable { resize: vertical }"
"textarea.moonbase-resizeable { resize: vertical }",
".moonbase-updates-notice { background-color: #222034; color: #FFFBA6; }"
];
2 changes: 1 addition & 1 deletion packages/core-extensions/src/moonbase/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"tagline": "The official settings UI for moonlight",
"authors": ["Cynosphere", "NotNite"]
},
"dependencies": ["spacepack", "settings", "common"],
"dependencies": ["spacepack", "settings", "common", "notices"],
"settings": {
"sections": {
"displayName": "Split into sections",
Expand Down
44 changes: 42 additions & 2 deletions packages/core-extensions/src/moonbase/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,50 @@ import fs from "fs";
import path from "path";
import extractAsar from "@moonlight-mod/core/asar";
import { repoUrlFile } from "@moonlight-mod/types/constants";
import { githubRepo, userAgent, nightlyRefUrl } from "./consts";

const logger = moonlightNode.getLogger("moonbase");

async function checkForMoonlightUpdate() {
try {
if (moonlightNode.branch === "stable") {
const req = await fetch(
`https://api.github.com/repos/${githubRepo}/releases/latest`,
{
headers: {
"User-Agent": userAgent
}
}
);
const json: { name: string } = await req.json();
return json.name !== moonlightNode.version ? json.name : null;
} else if (moonlightNode.branch === "nightly") {
const req = await fetch(nightlyRefUrl, {
headers: {
"User-Agent": userAgent
}
});
const ref = (await req.text()).split("\n")[0];
return ref !== moonlightNode.version ? ref : null;
}

return null;
} catch (e) {
logger.error("Error checking for moonlight update", e);
return null;
}
}

async function fetchRepositories(repos: string[]) {
const ret: Record<string, RepositoryManifest[]> = {};

for (const repo of repos) {
try {
const req = await fetch(repo);
const req = await fetch(repo, {
headers: {
"User-Agent": userAgent
}
});
const json = await req.json();
ret[repo] = json;
} catch (e) {
Expand All @@ -27,7 +62,11 @@ async function installExtension(
url: string,
repo: string
) {
const req = await fetch(url);
const req = await fetch(url, {
headers: {
"User-Agent": userAgent
}
});

const dir = moonlightNode.getExtensionDir(manifest.id);
// remake it in case of updates
Expand Down Expand Up @@ -63,6 +102,7 @@ function getExtensionConfig(id: string, key: string): any {
}

const exports: MoonbaseNatives = {
checkForMoonlightUpdate,
fetchRepositories,
installExtension,
deleteExtension,
Expand Down
1 change: 1 addition & 0 deletions packages/core-extensions/src/moonbase/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { DetectedExtension, ExtensionManifest } from "types/src";

export type MoonbaseNatives = {
checkForMoonlightUpdate(): Promise<string | null>;
fetchRepositories(
repos: string[]
): Promise<Record<string, RepositoryManifest[]>>;
Expand Down
123 changes: 86 additions & 37 deletions packages/core-extensions/src/moonbase/webpackModules/stores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,44 @@ import { Store } from "@moonlight-mod/wp/discord/packages/flux";
import Dispatcher from "@moonlight-mod/wp/discord/Dispatcher";
import extractAsar from "@moonlight-mod/core/asar";
import { repoUrlFile } from "@moonlight-mod/types/constants";
import { githubRepo, userAgent, nightlyRefUrl } from "../consts";

const logger = moonlight.getLogger("moonbase");

let natives: MoonbaseNatives = moonlight.getNatives("moonbase");
if (window._moonlightBrowserFS != null) {
const browserFS = window._moonlightBrowserFS!;
natives = {
checkForMoonlightUpdate: async () => {
try {
if (moonlight.branch === "stable") {
const req = await fetch(
`https://api.github.com/repos/${githubRepo}/releases/latest`,
{
headers: {
"User-Agent": userAgent
}
}
);
const json: { name: string } = await req.json();
return json.name !== moonlight.version ? json.name : null;
} else if (moonlight.branch === "nightly") {
const req = await fetch(nightlyRefUrl, {
headers: {
"User-Agent": userAgent
}
});
const ref = (await req.text()).split("\n")[0];
return ref !== moonlight.version ? ref : null;
}

return null;
} catch (e) {
logger.error("Error checking for moonlight update", e);
return null;
}
},

fetchRepositories: async (repos) => {
const ret: Record<string, RepositoryManifest[]> = {};

Expand Down Expand Up @@ -77,6 +108,9 @@ class MoonbaseSettingsStore extends Store<any> {
submitting: boolean;
installing: boolean;

newVersion: string | null;
shouldShowNotice: boolean;

extensions: { [id: number]: MoonbaseExtension };
updates: { [id: number]: { version: string; download: string } };

Expand All @@ -91,6 +125,9 @@ class MoonbaseSettingsStore extends Store<any> {
this.submitting = false;
this.installing = false;

this.newVersion = null;
this.shouldShowNotice = false;

this.extensions = {};
this.updates = {};
for (const ext of moonlightNode.extensions) {
Expand All @@ -104,53 +141,65 @@ class MoonbaseSettingsStore extends Store<any> {
};
}

natives!.fetchRepositories(this.config.repositories).then((ret) => {
for (const [repo, exts] of Object.entries(ret)) {
try {
for (const ext of exts) {
const level = ext.apiLevel ?? 1;
if (level !== window.moonlight.apiLevel) continue;

const uniqueId = this.extensionIndex++;
const extensionData = {
id: ext.id,
uniqueId,
manifest: ext,
source: { type: ExtensionLoadSource.Normal, url: repo },
state: ExtensionState.NotDownloaded
};

if (this.alreadyExists(extensionData)) {
// Make sure the download URL is properly updated
for (const [id, e] of Object.entries(this.extensions)) {
if (e.id === ext.id && e.source.url === repo) {
this.extensions[parseInt(id)].manifest = {
...e.manifest,
natives!
.fetchRepositories(this.config.repositories)
.then((ret) => {
for (const [repo, exts] of Object.entries(ret)) {
try {
for (const ext of exts) {
const level = ext.apiLevel ?? 1;
if (level !== window.moonlight.apiLevel) continue;

const uniqueId = this.extensionIndex++;
const extensionData = {
id: ext.id,
uniqueId,
manifest: ext,
source: { type: ExtensionLoadSource.Normal, url: repo },
state: ExtensionState.NotDownloaded
};

if (this.alreadyExists(extensionData)) {
// Make sure the download URL is properly updated
for (const [id, e] of Object.entries(this.extensions)) {
if (e.id === ext.id && e.source.url === repo) {
this.extensions[parseInt(id)].manifest = {
...e.manifest,
download: ext.download
};
break;
}
}

if (this.hasUpdate(extensionData)) {
this.updates[uniqueId] = {
version: ext.version!,
download: ext.download
};
break;
}
}

if (this.hasUpdate(extensionData)) {
this.updates[uniqueId] = {
version: ext.version!,
download: ext.download
};
continue;
}

continue;
this.extensions[uniqueId] = extensionData;
}

this.extensions[uniqueId] = extensionData;
} catch (e) {
logger.error(`Error processing repository ${repo}`, e);
}
} catch (e) {
logger.error(`Error processing repository ${repo}`, e);
}
}

this.emitChange();
});
this.emitChange();
})
.then(natives!.checkForMoonlightUpdate)
.then((version) => {
this.newVersion = version;
this.emitChange();
})
.then(() => {
this.shouldShowNotice =
this.newVersion != null || Object.keys(this.updates).length > 0;
this.emitChange();
});
}

private alreadyExists(ext: MoonbaseExtension) {
Expand Down
Loading

0 comments on commit 094cd83

Please sign in to comment.