Skip to content

Commit

Permalink
Some quality of life and download cores in JS (1fpga#38)
Browse files Browse the repository at this point in the history
In preparation for a full GUI.
  • Loading branch information
hansl authored May 13, 2024
1 parent 6dd7fee commit e1fd39d
Show file tree
Hide file tree
Showing 28 changed files with 1,827 additions and 40 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "golem-fpga",
"workspaces": [
"src/golem-frontend",
"scripts/patreon"
]
}
6 changes: 1 addition & 5 deletions src/golem-frontend/.swcrc
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@
},
"mangle": true
},
"paths": {
"golem/*": [
"./types/golem/*"
]
}
"paths": {}
},
"module": {
"type": "es6",
Expand Down
87 changes: 87 additions & 0 deletions src/golem-frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/golem-frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"build": "rollup -c",
"compile": "swc src --out-dir dist",
"retronomicon-update": "openapi-typescript https://retronomicon.land/api/v1/openapi.json -o ./types/retronomicon.d.ts"
},
"keywords": [],
"author": "",
Expand All @@ -15,6 +17,7 @@
"@rollup/plugin-typescript": "^11.1.6",
"@swc/cli": "^0.3.12",
"@swc/core": "^1.5.5",
"openapi-typescript": "^6.7.5",
"prettier": "^3.2.5",
"rollup": "^4.17.2",
"tslib": "^2.6.2",
Expand Down
3 changes: 2 additions & 1 deletion src/golem-frontend/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ export default {
file: "dist/main.js",
format: "es",
},
plugins: [typescript(), terser()],
plugins: [typescript()],
external: [],
};
4 changes: 2 additions & 2 deletions src/golem-frontend/src/about.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// The about page.
import * as ui from "golem/ui";
import { default as p } from "golem/patrons";
import * as ui from "@/golem/ui";
import { default as p } from "@/golem/patrons";

export function about() {
let { patrons, tiers } = p;
Expand Down
35 changes: 32 additions & 3 deletions src/golem-frontend/src/cores.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
import * as core from "golem/core";
import * as db from "golem/db";
import * as ui from "golem/ui";
import * as core from "@/golem/core";
import * as db from "@/golem/db";
import * as ui from "@/golem/ui";
import * as retronomicon from "./retronomicon";

function download_cores() {
ui.show("Downloading Cores, please wait...");
let cores = retronomicon.cores();

ui.textMenu({
title: "Download Cores",
back: () => true,
items: cores.map((core) => ({
label: "" + core.name,
select: () => {
let releases = retronomicon.releases(core.id);
console.log(1);
console.log(JSON.stringify(core));
console.log(JSON.stringify(releases));
console.log(3);
return true;
//
// db.execute(
// "INSERT OR REPLACE INTO cores (id, name, system_slug, path) VALUES (?, ?, ?, ?)",
// [core.id, core.name, core.system.slug, core.path],
// );
},
})),
});
}

function start_core(db_core: { path: string }) {
core.run({
Expand Down Expand Up @@ -33,6 +60,8 @@ export function cores_menu() {
})),
"-",
{ label: "Select File...", select: select_core_file },
"-",
{ label: "Download Cores...", select: download_cores },
],
});
}
6 changes: 3 additions & 3 deletions src/golem-frontend/src/games.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as core from "golem/core";
import * as db from "golem/db";
import * as ui from "golem/ui";
import * as core from "@/golem/core";
import * as db from "@/golem/db";
import * as ui from "@/golem/ui";

function start_game(game_id: number) {
const db_game = db.queryOne("SELECT * FROM games WHERE id = ?", [game_id])!;
Expand Down
10 changes: 5 additions & 5 deletions src/golem-frontend/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// The root file being executed by Golem by default.
import * as db from "golem/db";
import * as ui from "golem/ui";
import * as db from "@/golem/db";
import * as ui from "@/golem/ui";

import { games_menu } from "./games";
import { cores_menu } from "./cores";
Expand All @@ -14,7 +14,7 @@ function downloads_menu() {
ui.alert("Downloads", "Not implemented yet.");
}

async function main_menu() {
function main_menu() {
const nb_games = db.queryOne("SELECT COUNT(*) as count FROM games")
?.count as number;
const nb_cores = db.queryOne("SELECT COUNT(*) as count FROM cores")
Expand All @@ -39,10 +39,10 @@ async function main_menu() {
});
}

export async function main() {
export function main() {
while (true) {
try {
return await main_menu();
return main_menu();
} catch (e: any) {
console.error(e);
ui.alert("Error", e.message);
Expand Down
18 changes: 18 additions & 0 deletions src/golem-frontend/src/retronomicon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type * as retronomicon from "@/retronomicon";
import * as net from "@/golem/net";

export type CoreList =
retronomicon.paths["/cores"]["get"]["responses"]["200"]["content"]["application/json"];

export type ReleaseList =
retronomicon.paths["/cores/{core_id}/releases"]["get"]["responses"]["200"]["content"]["application/json"];

export function cores(): CoreList {
return net.fetchJson<CoreList>("https://retronomicon.land/api/v1/cores");
}

export function releases(coreId: number): ReleaseList {
return net.fetchJson<ReleaseList>(
`https://retronomicon.land/api/v1/cores/${coreId}/releases`,
);
}
5 changes: 1 addition & 4 deletions src/golem-frontend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
"moduleResolution": "Bundler",
"paths": {
"@/*": [
"src/*"
],
"golem/*": [
"types/golem/*"
"types/*"
]
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/golem-frontend/types/golem/core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* This module provides functions to interact with cores in Golem Script.
*/
declare module "golem/core" {
declare module "@/golem/core" {
/**
* A path to a core file.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/golem-frontend/types/golem/db.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* It is free form, so you can execute any SQL query you want. Be careful with
* updates and schema changes.
*/
declare module "golem/db" {
declare module "@/golem/db" {
/**
* A value that can be bound to or returned from a SQL query. This can be a string,
* number, boolean, or NULL.
Expand Down
22 changes: 22 additions & 0 deletions src/golem-frontend/types/golem/net.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Network access type definitions.

/**
* This module provides functions for accessing the internet. It can download
* JSON data or files from URLs.
*/
declare module "@/golem/net" {
/**
* Download a JSON file from a URL. Returns the parsed JSON.
* Will throw an error if the request fails or the response is not JSON.
*
* @param url The URL to download.
*/
export function fetchJson<R extends object = any>(url: string): R;

/**
* Download a file from a URL. Returns the path to the downloaded file.
*
* @param url The URL to download.
*/
export function downloadFile(url: string): string;
}
2 changes: 1 addition & 1 deletion src/golem-frontend/types/golem/patrons.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ declare const PATRONS: {
};
};

declare module "golem/patrons" {
declare module "@/golem/patrons" {
export default PATRONS;
}
Loading

0 comments on commit e1fd39d

Please sign in to comment.