fix: Fixed romm login, now uses token

feat: Moved romm to internal plugin
fix: Made focusing and navigation more reliable
fix: Loading errors on first time launch
This commit is contained in:
Simeon Radivoev 2026-03-28 17:32:51 +02:00
parent 7c10f4e4c2
commit 816d50ae4d
Signed by: simeonradivoev
GPG key ID: 7611A451D2A5D37A
81 changed files with 1659 additions and 1097 deletions

View file

@ -13,7 +13,6 @@ import { client } from "@clients/romm/client.gen";
import * as schema from "@schema/app";
import cacheSchema from "@schema/cache";
import * as emulatorSchema from "@schema/emulators";
import { login, logout } from "./auth";
import os from 'node:os';
import EventEmitter from "node:events";
import { appPath } from "../utils";
@ -63,7 +62,6 @@ const emulatorsSqlite = new Database(appPath(`./vendors/es-de/emulators.${os.pla
export const emulatorsDb = drizzle(emulatorsSqlite, { schema: emulatorSchema });
export const taskQueue = new TaskQueue();
config.onDidChange('rommAddress', v => client.setConfig({ baseUrl: v }));
await login();
export const plugins = new PluginManager();
registerPlugins(plugins);
export const events = new EventEmitter<AppEventMap>();
@ -74,7 +72,6 @@ export async function cleanup ()
{
await taskQueue.close();
sqlite.close();
await logout();
emulatorsSqlite.close();
}

View file

@ -1,8 +1,7 @@
import Elysia, { status } from "elysia";
import { config, events, jar, taskQueue } from "./app";
import { config, events, jar, plugins, taskQueue } from "./app";
import z from "zod";
import { client } from "@clients/romm/client.gen";
import { loginApiLoginPost, logoutApiLogoutPost } from "@clients/romm";
import { getCurrentUserApiUsersMeGet, tokenApiTokenPost, UserSchema } from "@clients/romm";
import secrets from '../api/secrets';
import { LoginJob } from "./jobs/login-job";
import TwitchLoginJob from "./jobs/twitch-login-job";
@ -43,6 +42,8 @@ export default new Elysia()
await secrets.delete({ service: 'gamflow_twitch', name: 'refresh_token' });
await secrets.delete({ service: 'gamflow_twitch', name: 'expires_in' });
await plugins.hooks.auth.loginComplete.promise({ service: 'twitch' });
return status(res.status, res.statusText);
})
.get('/login/twitch', async () =>
@ -93,6 +94,8 @@ export default new Elysia()
await secrets.set({ service: 'gamflow_twitch', name: 'refresh_token', value: data.refresh_token });
await secrets.set({ service: 'gamflow_twitch', name: 'expires_in', value: new Date(new Date().getTime() + data.expires_in).toString() });
await plugins.hooks.auth.loginComplete.promise({ service: 'twitch' });
events.emit('notification', { message: "Twitch Refresh Successful", type: 'success' });
const res = await fetch('https://id.twitch.tv/oauth2/validate', { headers: { Authorization: `OAuth ${data.access_token}` } });
@ -104,7 +107,7 @@ export default new Elysia()
return status(400, res.statusText);
})
.post('/login/romm', async () =>
.post('/login/romm/qr', async () =>
{
if (taskQueue.hasActiveOfType(LoginJob))
{
@ -113,117 +116,87 @@ export default new Elysia()
return taskQueue.enqueue(LoginJob.id, new LoginJob());
})
.post('/login', async ({ body }) => tryLoginAndSave(body), { body: z.object({ host: z.url(), username: z.string(), password: z.string() }) })
.get('/login', async () =>
.get('/user/romm', async () =>
{
const credentials = await secrets.get({ service: 'gameflow', name: 'romm' });
return { hasPassword: !!credentials };
}, { response: z.object({ hasPassword: z.boolean() }) })
.post('/logout', async () =>
const data = await getCurrentUserApiUsersMeGet();
if (data.error) return status("Internal Server Error", data.response.statusText);
return data.data as UserSchema;
})
.post('/login/romm', async ({ body }) => tryLoginAndSave(body), { body: z.object({ host: z.url(), username: z.string(), password: z.string() }) })
.get('/login/romm', async () =>
{
await secrets.delete({ service: 'gameflow', name: 'romm' });
await logout();
const rommAddress = config.get('rommAddress');
if (rommAddress)
const access_token = await secrets.get({ service: 'gameflow', name: 'romm_access_token' });
if (!access_token)
{
const cookies = await jar.getCookies(rommAddress);
cookies.map(c => jar.store.removeCookie(c.domain, c.path, c.key));
return { hasLogin: false };
}
const expires_in = await secrets.get({ service: 'gameflow', name: "romm_expires_in" });
if (expires_in)
{
const date = new Date(expires_in);
if (date > new Date())
{
return { hasLogin: true };
}
}
const refresh_token = await secrets.get({ service: 'gameflow', name: "romm_refresh_token" });
if (!refresh_token)
{
return { hasLogin: false };
}
const refreshResponse = await tokenApiTokenPost({ body: { grant_type: "refresh_token", refresh_token: refresh_token } });
if (refreshResponse.response.ok && refreshResponse.data)
{
await secrets.set({ service: 'gameflow', name: 'romm_access_token', value: refreshResponse.data.access_token });
if (refreshResponse.data.refresh_token)
await secrets.set({ service: 'gameflow', name: 'romm_refresh_token', value: refreshResponse.data.refresh_token });
await secrets.set({ service: 'gameflow', name: 'romm_expires_in', value: new Date(new Date().getTime() + refreshResponse.data.expires * 1000).toString() });
await plugins.hooks.auth.loginComplete.promise({ service: 'romm' });
events.emit('notification', { message: "Romm Refresh Successful", type: 'success' });
return { hasLogin: true };
}
return status(refreshResponse.response.status, refreshResponse.response.statusText) as any;
},
{ response: z.object({ hasLogin: z.boolean() }) })
.post('/logout/romm', async () =>
{
await secrets.delete({ service: 'gameflow', name: 'romm_access_token' });
await secrets.delete({ service: 'gameflow', name: 'romm_refresh_token' });
await secrets.delete({ service: 'gameflow', name: 'romm_expires_in' });
return status(200);
}, { response: z.any() });
async function updateClient ()
{
client.setConfig({
baseUrl: config.get('rommAddress'), headers: {
cookie: await jar.getCookieString(config.get('rommAddress') ?? '')
}
});
}
export async function tryLoginAndSave ({ host, username, password }: { host: string, username: string, password: string; })
{
if (config.has('rommAddress') && config.has('rommUser'))
const response = await tokenApiTokenPost({
body: {
password,
username,
scope: 'me.read roms.read platforms.read assets.read firmware.read roms.user.read collections.read me.write roms.user.write'
}, baseUrl: host
});
if (response.response.ok && response.data)
{
await logout();
const oldRommAddress = config.get('rommAddress');
if (oldRommAddress)
await secrets.set({ service: 'gameflow', name: 'romm_access_token', value: response.data.access_token });
await secrets.set({ service: 'gameflow', name: 'romm_expires_in', value: new Date(new Date().getTime() + response.data.expires * 1000).toString() });
if (response.data.refresh_token)
{
const cookies = await jar.getCookies(oldRommAddress);
await Promise.all(cookies.map(c => jar.store.removeCookie(c.domain, c.path, c.key)));
await secrets.set({ service: 'gameflow', name: 'romm_refresh_token', value: response.data.refresh_token });
}
}
const response = await login({ rommAddress: host, rommUser: username, rommPassword: password });
if (response?.code === 200)
{
config.set('rommAddress', host);
config.set('rommUser', username);
await secrets.set({ service: 'gameflow', name: 'romm', value: password });
await plugins.hooks.auth.loginComplete.promise({ service: 'twitch' });
}
return response;
}
export async function logout ()
{
if (!config.has('rommAddress'))
{
return;
}
const rommAddress = config.get('rommAddress');
if (rommAddress)
{
console.log("Logging Out of ROMM");
try
{
await logoutApiLogoutPost({
baseUrl: rommAddress, headers: {
'cookie': await jar.getCookieString(rommAddress)
}
});
await jar.store.removeCookie(new URL(rommAddress).host, null, "romm_session");
} catch (error)
{
console.error("Failed to logout of ROMM ", error);
}
}
}
export async function login (data?: { rommAddress?: string, rommUser?: string, rommPassword?: string; })
{
const address = data?.rommAddress ?? config.get('rommAddress');
const user = data?.rommUser ?? config.get('rommUser');
const password = data?.rommPassword ?? await secrets.get({ service: 'gameflow', name: "romm" });
if (!address || !user)
{
console.warn("Romm not setup");
return status(404);
}
const rommAddress = config.get('rommAddress');
const rommUser = config.get('rommUser');
if (rommAddress && rommUser)
{
console.log("Logging In to ROMM");
if (password === null)
{
return status(404, "No Found Password");
}
const loginResponse = await loginApiLoginPost({ baseUrl: rommAddress, auth: `${rommUser}:${password}` });
if (loginResponse.response.status === 200)
{
loginResponse.response.headers.getSetCookie().map(c => jar.setCookie(c, rommAddress));
await updateClient();
return status(200, loginResponse.response.statusText);
} else
{
console.error("Could not Login to Romm: ", loginResponse.response.statusText);
return status(loginResponse.response.status, loginResponse.response.statusText);
}
}
}
}

View file

@ -4,9 +4,10 @@ import { config, jar } from "./app";
import games from "./games/games";
import platforms from "./games/platforms";
import auth from "./auth";
import collections from "./games/collections";
export default new Elysia({ prefix: "/api/romm" })
.use([games, platforms, auth])
.use([games, platforms, collections, auth])
.all("/*", async ({ request, set }) =>
{
set.headers["cross-origin-resource-policy"] = 'cross-origin';

View file

@ -0,0 +1,15 @@
import Elysia, { status } from "elysia";
import { plugins } from "../app";
export default new Elysia()
.get('/collections', async () =>
{
const collections: FrontEndCollection[] = [];
await plugins.hooks.games.fetchCollections.promise({ collections });
return collections;
}).get('/collection/:source/:id', async ({ params: { source, id } }) =>
{
const collection = await plugins.hooks.games.fetchCollection.promise({ source, id });
if (!collection) return status("Not Found");
return collection;
});

View file

@ -1,14 +1,13 @@
import Elysia, { status } from "elysia";
import { config, db, emulatorsDb, taskQueue } from "../app";
import { config, db, emulatorsDb, plugins, taskQueue } from "../app";
import { and, eq, getTableColumns, inArray, sql } from "drizzle-orm";
import z from "zod";
import * as schema from "@schema/app";
import fs from "node:fs/promises";
import { GameListFilterSchema, SERVER_URL } from "@shared/constants";
import { getPlatformsApiPlatformsGet, getRomsApiRomsGet } from "@clients/romm";
import { InstallJob } from "../jobs/install-job";
import path from "node:path";
import { convertLocalToFrontend, convertRomToFrontend, convertStoreToFrontend, getLocalGameMatch, getSourceGameDetailed } from "./services/utils";
import { convertLocalToFrontend, convertStoreToFrontend, getLocalGameMatch, getSourceGameDetailed } from "./services/utils";
import buildStatusResponse, { getValidLaunchCommandsForGame } from "./services/statusService";
import { errorToResponse } from "elysia/adapter/bun/handler";
import { getEmulatorsForSystem, launchCommand } from "./services/launchGameService";
@ -19,7 +18,6 @@ import webp from "@jimp/wasm-webp";
import * as emulatorSchema from '@schema/emulators';
import { buildStoreFrontendEmulatorSystems, getShuffledStoreGames, getStoreEmulatorPackage, getStoreGameFromPath, getStoreGameManifest } from "../store/services/gamesService";
import { convertStoreEmulatorToFrontend } from "../store/services/emulatorsService";
import { CACHE_KEYS, getOrCached } from "../cache";
import { host } from "@/bun/utils/host";
import { LaunchGameJob } from "../jobs/launch-game-job";
@ -34,7 +32,7 @@ async function processImage (img: string | Buffer | ArrayBuffer, { blur, width,
try
{
if ((blur && !noBlur) || width || height)
if ((blur && !noBlur))
{
const jimp = await Jimp.read(img);
@ -50,7 +48,7 @@ async function processImage (img: string | Buffer | ArrayBuffer, { blur, width,
{
jimp.resize({ w: width, h: height });
}
return jimp.getBuffer('image/webp');
return jimp.getBuffer('image/png');
}
} catch (e)
{
@ -174,6 +172,17 @@ export default new Elysia()
if (query.platform_slug)
{
where.push(eq(schema.platforms.slug, query.platform_slug));
} else if (query.platform_id && query.platform_source === 'local')
{
where.push(eq(schema.platforms.id, query.platform_id));
}
else if (query.platform_id && query.platform_source)
{
const platform = await plugins.hooks.games.platformLookup.promise({ source: query.platform_source, id: String(query.platform_id) });
if (platform)
{
where.push(eq(schema.platforms.slug, platform?.slug));
}
}
if (query.source)
@ -190,37 +199,54 @@ export default new Elysia()
.leftJoin(schema.platforms, eq(schema.platforms.id, schema.games.platform_id))
.leftJoin(schema.screenshots, eq(schema.screenshots.game_id, schema.games.id))
.groupBy(schema.games.id)
.offset(query.offset ?? 0)
.limit(query.limit ?? 50)
.where(and(...where));
localGamesSet = new Set(localGames.filter(g => !!g.source_id && !!g.source).map(g => `${g.source}@${g.source_id}`));
if (!query.collection_id)
{
games.push(...localGames.map(g =>
games.push(...localGames.slice(query.offset, query.limit ? query.offset ?? 0 + query.limit : undefined).map(g =>
{
return convertLocalToFrontend(g);
}));
}
if (((!query.platform_source || query.platform_source === 'romm') || !!query.collection_id) && (!query.source || query.source === 'romm'))
const remoteGames: FrontEndGameType[] = [];
await plugins.hooks.games.fetchGames.promise({ query, games: remoteGames }).catch(e => console.error(e));
games.push(...remoteGames.filter(g => !localGamesSet?.has(`${g.id.source}@${g.id.id}`)));
} else
{
const rommGames = await getRomsApiRomsGet({
query: {
platform_ids: query.platform_id ? [query.platform_id] : undefined,
collection_id: query.collection_id,
limit: query.limit,
offset: query.offset
}, throwOnError: true
});
games.push(...rommGames.data.items.filter(g => !localGamesSet?.has(`romm@${g.id}`)).map(g =>
const remoteGames: FrontEndGameType[] = [];
await plugins.hooks.games.fetchGames.promise({ query, games: remoteGames }).catch(e => console.error(e));
games.push(...remoteGames.map(g =>
{
return convertRomToFrontend(g);
if (localGamesSet?.has(`${g.id.source}@${g.id.id}`))
{
return convertLocalToFrontend(localGames.find(l => l.source === g.id.source && l.source_id === g.id.id)!);
} else
{
return g;
}
}));
}
}
if (query.orderBy)
{
switch (query.orderBy)
{
case 'added':
games.sort((a, b) => b.updated_at.getTime() - a.updated_at.getTime());
break;
case 'activity':
games.sort((a, b) => Math.max(b.updated_at.getTime(), b.last_played?.getTime() ?? 0) - Math.max(a.updated_at.getTime(), a.last_played?.getTime() ?? 0));
break;
case 'name':
games.sort((a, b) => (a.name ?? '').localeCompare(b.name ?? ''));
break;
}
}
return { games };
}, {
query: GameListFilterSchema,
@ -274,7 +300,7 @@ export default new Elysia()
{
return {
name: 'EMULATORJS',
validSource: { binPath: SERVER_URL(host), type: 'embedded', exists: true },
validSources: [{ binPath: SERVER_URL(host), type: 'embedded', exists: true }],
logo: `/api/romm/image?url=${encodeURIComponent('https://emulatorjs.org/logo/EmulatorJS.png')}`,
systems: [],
gameCount: 0
@ -286,7 +312,8 @@ export default new Elysia()
name: name,
logo: "",
systems: [],
gameCount: 0
gameCount: 0,
validSources: []
} satisfies FrontEndGameTypeDetailedEmulator;
}
@ -323,7 +350,7 @@ export default new Elysia()
{
if (source === 'romm' || source === 'store')
{
taskQueue.enqueue(InstallJob.query({ source, id }), new InstallJob(id, source, id, { dryRun: true }));
taskQueue.enqueue(InstallJob.query({ source, id }), new InstallJob(id, source));
return status(200);
}
@ -338,7 +365,7 @@ export default new Elysia()
})
.delete('/game/:source/:id/install', async ({ params: { id, source } }) =>
{
const job = taskQueue.findJob(`install-rom-${source}-${id}`, InstallJob);
const job = taskQueue.findJob(InstallJob.query({ source, id }), InstallJob);
if (job)
{
job.abort('cancel');
@ -408,7 +435,7 @@ export default new Elysia()
if (!emulator) return status("Not Found");
const systems = await buildStoreFrontendEmulatorSystems(emulator);
const systemsIdSet = new Set(systems.map(s => s.id));
const systemsRommSlugSet = new Set(systems.filter(s => s.romm_slug).map(s => s.romm_slug!));
const games: FrontEndGameType[] = [];
@ -431,31 +458,9 @@ export default new Elysia()
return convertLocalToFrontend(g);
}).slice(0, 3));
const rommPlatforms = await getOrCached(CACHE_KEYS.ROM_PLATFORMS, () => getPlatformsApiPlatformsGet({ throwOnError: true }), { expireMs: 60 * 60 * 1000 }).then(d => d.data).catch(e => console.error(e));
if (rommPlatforms)
{
const platformIds = rommPlatforms.filter(p => systemsRommSlugSet.has(p.slug)).map(s => s.id);
if (platformIds.length > 0)
{
const rommGames = await getRomsApiRomsGet({
query: {
platform_ids: platformIds
}
});
let gamesPerSystem = Math.round(3 / systemsRommSlugSet.size);
for (const slug of systemsRommSlugSet)
{
const systemRommGames = rommGames.data?.items.filter(g => !localGamesSet?.has(`romm@${g.id}`) && slug === g.platform_slug).map(g =>
{
return convertRomToFrontend(g);
}).slice(0, gamesPerSystem) ?? [];
games.push(...systemRommGames);
}
}
}
const remoteGames: FrontEndGameType[] = [];
await plugins.hooks.games.fetchRecommendedGamesForEmulator.promise({ emulator, systems, games: remoteGames });
games.push(...remoteGames.filter(g => !localGamesSet?.has(`${g.id.source}@${g.id.id}`)));
const gamesManifest = await getStoreGameManifest();
const storeGames = await Promise.all(gamesManifest
@ -502,20 +507,6 @@ export default new Elysia()
games.push(...localGames.map(g => ({ ...convertLocalToFrontend(g), metadata: g.metadata })));
const rommPlatforms = await getOrCached(CACHE_KEYS.ROM_PLATFORMS, () => getPlatformsApiPlatformsGet({ throwOnError: true }), { expireMs: 60 * 60 * 1000 }).then(d => d.data).catch(e => console.error(e));
if (rommPlatforms)
{
const rommPlatform = rommPlatforms.find(p => p.slug === sourceData.platform_slug);
if (rommPlatform)
{
const rommGames = await getRomsApiRomsGet({ query: { genres: sourceData.genres, genres_logic: 'any' } });
if (rommGames.data)
{
games.push(...rommGames.data.items.filter(g => !localGamesSourceSet.has(`romm@${g.id}`)).map(g => ({ ...convertRomToFrontend(g), metadata: g.metadatum })));
}
}
}
const shuffledGames = await getShuffledStoreGames();
const storeGames = await Promise.all(shuffledGames
.filter(g =>
@ -546,6 +537,13 @@ export default new Elysia()
games.push(...storeGames.slice(0, 3));
}
const remoteGames: (FrontEndGameType & { metadata?: any; })[] = [];
plugins.hooks.games.fetchRecommendedGamesForGame.promise({
game: sourceData, games: remoteGames
});
games.push(...remoteGames.filter(g => !localGamesSourceSet.has(`${g.id.source}@${g.id.id}`)));
const random = new SeededRandom(Math.round(new Date().getTime() / 1000 / 60 / 60));
const rankedGames = games.filter(g =>

View file

@ -1,18 +1,12 @@
import Elysia, { status } from "elysia";
import { getPlatformApiPlatformsIdGet, getPlatformsApiPlatformsGet, getRomsApiRomsGet } from "@clients/romm";
import z from "zod";
import { and, count, eq, getTableColumns, not } from "drizzle-orm";
import { db } from "../app";
import { db, plugins } from "../app";
import * as schema from "@schema/app";
import { CACHE_KEYS, getOrCached } from "../cache";
export default new Elysia()
.get('/platforms', async () =>
{
const platforms: FrontEndPlatformType[] = [];
let rommPlatformsSet: Set<string> | undefined;
const rommPlatforms = await getOrCached(CACHE_KEYS.ROM_PLATFORMS, () => getPlatformsApiPlatformsGet({ throwOnError: true }), { expireMs: 60 * 60 * 1000 }).then(d => d.data).catch(e => console.error(e));
const localPlatforms = await db.select({ ...getTableColumns(schema.platforms), game_count: count(schema.games.id) })
.from(schema.platforms)
.leftJoin(schema.games, eq(schema.games.platform_id, schema.platforms.id))
@ -20,49 +14,31 @@ export default new Elysia()
const localPlatformSet = new Set(localPlatforms.filter(p => p.game_count > 0).map(p => p.slug));
if (rommPlatforms)
const remotePlatforms: FrontEndPlatformType[] = [];
await plugins.hooks.games.fetchPlatforms.promise({ platforms: remotePlatforms });
await Promise.all(remotePlatforms.map(async p =>
{
const frontEndPlatforms = await Promise.all(rommPlatforms.map(async p =>
p.hasLocal = localPlatformSet.has(p.slug);
if (p.paths_screenshots.length <= 0)
{
const screenshots: string[] = [];
const rommGames = await getRomsApiRomsGet({ query: { platform_ids: [p.id], limit: 3 } }).then(d => d.data);
if (rommGames)
{
const rommScreenshots = rommGames.items.find(i => i.merged_screenshots.length > 0)?.merged_screenshots.map(s => `/api/romm/image/romm/${s}`);
if (rommScreenshots)
screenshots.push(...rommScreenshots);
}
const localScreenshots = await db.select({ id: schema.screenshots.id }).from(schema.games).leftJoin(schema.platforms, eq(schema.platforms.id, schema.games.platform_id)).where(eq(schema.platforms.slug, p.slug)).leftJoin(schema.screenshots, eq(schema.screenshots.game_id, schema.games.id)).limit(1);
if (screenshots.length <= 0)
{
const localScreenshots = await db.select({ id: schema.screenshots.id }).from(schema.games).leftJoin(schema.platforms, eq(schema.platforms.id, schema.games.platform_id)).where(eq(schema.platforms.slug, p.slug)).leftJoin(schema.screenshots, eq(schema.screenshots.game_id, schema.games.id)).limit(1);
if (localScreenshots)
p.paths_screenshots.push(...localScreenshots.map(s => `/api/romm/screenshot/${s.id}`));
}
if (localScreenshots)
screenshots.push(...localScreenshots.map(s => `/api/romm/screenshot/${s.id}`));
}
const localGames = await db.select({ id: schema.games.id, source: schema.games.source, souceId: schema.games.source_id }).from(schema.games).leftJoin(schema.platforms, eq(schema.platforms.id, schema.games.platform_id)).where(and(eq(schema.platforms.slug, p.slug), not(eq(schema.games.source, 'romm')))).groupBy(schema.games.id);
p.game_count += localGames.length;
}));
const localGames = await db.select({ id: schema.games.id, source: schema.games.source, souceId: schema.games.source_id }).from(schema.games).leftJoin(schema.platforms, eq(schema.platforms.id, schema.games.platform_id)).where(and(eq(schema.platforms.slug, p.slug), not(eq(schema.games.source, 'romm')))).groupBy(schema.games.id);
const platformSlugSet = new Set(remotePlatforms.map(p => p.slug));
const platform: FrontEndPlatformType = {
slug: p.slug,
name: p.display_name,
family_name: p.family_name,
path_cover: `/api/romm/image/romm/assets/platforms/${p.slug}.svg`,
game_count: p.rom_count + localGames.length,
updated_at: new Date(p.updated_at),
id: { source: 'romm', id: String(p.id) },
hasLocal: localPlatformSet.has(p.slug),
paths_screenshots: screenshots
};
return platform;
}));
rommPlatformsSet = new Set(rommPlatforms.map(p => p.slug));
platforms.push(...frontEndPlatforms);
}
platforms.push(...await Promise.all(localPlatforms.filter(p => !rommPlatformsSet?.has(p.slug)).map(async p =>
const platforms: FrontEndPlatformType[] = [];
platforms.push(...remotePlatforms);
platforms.push(...await Promise.all(localPlatforms.filter(p => !platformSlugSet?.has(p.slug)).map(async p =>
{
const game = await db.query.games.findFirst({ where: eq(schema.games.platform_id, p.id) });
let screenshots: { id: number; }[] = [];
@ -90,31 +66,9 @@ export default new Elysia()
return { platforms };
}).get('/platforms/:source/:id', async ({ params: { source, id } }) =>
{
if (source === 'romm')
if (source === 'local')
{
const { data: rommPlatform, response } = await getPlatformApiPlatformsIdGet({ path: { id } });
if (rommPlatform)
{
const platform: FrontEndPlatformType = {
slug: rommPlatform.slug,
name: rommPlatform.display_name,
family_name: rommPlatform.family_name,
path_cover: `/api/romm/image/romm/assets/platforms/${rommPlatform.slug}.svg`,
game_count: rommPlatform.rom_count,
updated_at: new Date(rommPlatform.updated_at),
id: { source: 'romm', id: String(rommPlatform.id) },
paths_screenshots: [],
hasLocal: false
};
return platform;
}
return status("Not Found", response);
}
else if (source === 'local')
{
const localPlatform = await db.query.platforms.findFirst({ where: eq(schema.platforms.id, id) });
const localPlatform = await db.query.platforms.findFirst({ where: eq(schema.platforms.id, Number(id)) });
if (localPlatform)
{
const platform: FrontEndPlatformType = {
@ -133,10 +87,13 @@ export default new Elysia()
}
return status("Not Found");
} else
{
const remotePlatform = await plugins.hooks.games.fetchPlatform.promise({ source, id });
if (!remotePlatform) return status("Not Found");
return remotePlatform;
}
return status("Not Implemented");
}, { params: z.object({ source: z.string(), id: z.coerce.number() }) }).get('/platform/local/:id/cover', async ({ params: { id }, set }) =>
}, { params: z.object({ source: z.string(), id: z.string() }) }).get('/platform/local/:id/cover', async ({ params: { id }, set }) =>
{
set.headers["cross-origin-resource-policy"] = 'cross-origin';

View file

@ -1,11 +1,10 @@
import { RPC_URL, } from "@shared/constants";
import { config, customEmulators, db, taskQueue } from "../../app";
import { config, customEmulators, db, emulatorsDb, plugins, taskQueue } from "../../app";
import { getValidLaunchCommands } from "./launchGameService";
import * as schema from '@schema/app';
import { eq } from "drizzle-orm";
import { getErrorMessage } from "@/bun/utils";
import { getLocalGameMatch } from "./utils";
import { getRomApiRomsIdGet } from "@/clients/romm";
import * as emulatorSchema from '@schema/emulators';
import { and, eq } from "drizzle-orm";
import { getErrorMessage, hashFile } from "@/bun/utils";
import { checkFiles, getLocalGameMatch } from "./utils";
import fs from 'node:fs/promises';
import { getStoreGameFromId } from "../../store/services/gamesService";
import { cores } from "../../emulatorjs/emulatorjs";
@ -26,17 +25,15 @@ class CommandSearchError extends Error
export async function getLocalGame (source: string, id: string)
{
const localGames = await db.select({ id: schema.games.id, path_fs: schema.games.path_fs, platform_slug: schema.platforms.es_slug })
.from(schema.games)
.where(getLocalGameMatch(id, source))
.leftJoin(schema.platforms, eq(schema.games.platform_id, schema.platforms.id));
const localGame = await db.query.games.findFirst({
columns: { id: true, path_fs: true },
where: getLocalGameMatch(id, source),
with: {
platform: { columns: { slug: true } }
}
});
if (localGames.length > 0)
{
return localGames[0];
}
return undefined;
return localGame;
}
export async function getValidLaunchCommandsForGame (source: string, id: string)
@ -44,22 +41,24 @@ export async function getValidLaunchCommandsForGame (source: string, id: string)
const localGame = await getLocalGame(source, id);
if (localGame)
{
if (localGame.platform_slug)
const rommPlatform = localGame.platform.slug;
const esPlatform = await emulatorsDb.query.systemMappings.findFirst({ where: and(eq(emulatorSchema.systemMappings.sourceSlug, rommPlatform), eq(emulatorSchema.systemMappings.source, 'romm')) });
if (esPlatform)
{
if (localGame.path_fs)
{
try
{
const commands = await getValidLaunchCommands({ systemSlug: localGame.platform_slug, customEmulatorConfig: customEmulators, gamePath: localGame.path_fs });
const commands = await getValidLaunchCommands({ systemSlug: esPlatform.system, customEmulatorConfig: customEmulators, gamePath: localGame.path_fs });
if (cores[localGame.platform_slug])
if (cores[esPlatform.system])
{
const gameUrl = `${RPC_URL(host)}/api/romm/rom/${source}/${id}`;
commands.push({
id: 'EMULATORJS',
label: "Emulator JS",
command: `core=${cores[localGame.platform_slug]}&gameUrl=${encodeURIComponent(gameUrl)}`,
command: `core=${cores[esPlatform.system]}&gameUrl=${encodeURIComponent(gameUrl)}`,
valid: true,
emulator: 'EMULATORJS',
metadata: {
@ -90,7 +89,7 @@ export async function getValidLaunchCommandsForGame (source: string, id: string)
}
else
{
return new CommandSearchError('error', 'Missing Platform');
return new CommandSearchError('error', `Missing Platform ${localGame.platform.slug}`);
}
}
@ -107,6 +106,7 @@ export default function buildStatusResponse ()
z.object({ status: z.literal(['refresh', 'queued']) }),
z.object({ status: z.literal('playing'), details: z.string() }),
z.object({ status: z.literal('install'), details: z.string() }),
z.object({ status: z.literal('present'), details: z.string() }),
z.object({ status: z.literal(['download', 'extract']), progress: z.number() }),
]),
message (ws, data)
@ -158,20 +158,6 @@ export default function buildStatusResponse ()
});
}
}
else if (ws.data.params.source === 'romm')
{
// TODO: Add Caching
const remoteGame = await getRomApiRomsIdGet({ path: { id: Number(ws.data.params.id) } });
const stats = await fs.statfs(config.get('downloadPath'));
if (remoteGame.data?.fs_size_bytes && remoteGame.data?.fs_size_bytes > stats.bsize * stats.bavail)
{
ws.send({ status: 'error', error: "Not Enough Free Space" });
} else
{
ws.send({ status: 'install', details: 'Install' });
}
} else if (ws.data.params.source === 'store')
{
const storeGame = await getStoreGameFromId(ws.data.params.id);
@ -186,6 +172,41 @@ export default function buildStatusResponse ()
{
ws.send({ status: 'install', details: 'Install' });
}
} else
{
const files = await plugins.hooks.games.fetchDownloads.promise({
source: ws.data.params.source,
id: ws.data.params.id
});
let filesChecked: LocalDownloadFileEntry[] | undefined;
if (files)
{
filesChecked = await checkFiles(files.files, !!files.extract_path);
}
if (filesChecked && !filesChecked.some(f => f.exists === false || f.matches === false))
{
ws.send({ status: 'present', details: "Files Exist On Disk, Import" });
} else
{
const size = filesChecked?.filter(f => f.exists !== true || f.matches !== true).reduce((p, f) => p += f.size ?? 0, 0);
const stats = await fs.statfs(config.get('downloadPath'));
if (size && size > stats.bsize * stats.bavail)
{
ws.send({ status: 'error', error: "Not Enough Free Space" });
} else if (filesChecked?.some(f => f.exists === true && f.matches === false))
{
ws.send({ status: 'install', details: 'Some Files Present, Install' });
}
else
{
ws.send({ status: 'install', details: 'Install' });
}
}
}
}
}

View file

@ -1,14 +1,14 @@
import getFolderSize from "get-folder-size";
import fs from "node:fs/promises";
import path from "node:path";
import { config, db, emulatorsDb } from "../../app";
import { config, db, emulatorsDb, plugins } from "../../app";
import { and, eq } from "drizzle-orm";
import * as schema from "@schema/app";
import { StoreGameType } from "@shared/constants";
import { DetailedRomSchema, getCurrentUserApiUsersMeGet, getRomApiRomsIdGet, SimpleRomSchema } from "@clients/romm";
import * as emulatorSchema from "@schema/emulators";
import { extractStoreGameSourceId, getStoreGame } from "../../store/services/gamesService";
import { isSteamDeck, isSteamDeckGameMode } from "@/bun/utils";
import { hashFile, isSteamDeck, isSteamDeckGameMode } from "@/bun/utils";
export async function calculateSize (installPath: string | null)
{
@ -27,29 +27,6 @@ export function getLocalGameMatch (id: string, source: string)
return source !== 'local' ? and(eq(schema.games.source_id, id), eq(schema.games.source, source)) : eq(schema.games.id, Number(id));
}
export function convertRomToFrontend (rom: SimpleRomSchema): FrontEndGameType
{
const steamDeck = isSteamDeckGameMode();
const game: FrontEndGameType = {
id: { id: String(rom.id), source: 'romm' },
path_cover: `/api/romm/image/romm${steamDeck ? rom.path_cover_small : rom.path_cover_large}`,
last_played: rom.rom_user.last_played ? new Date(rom.rom_user.last_played) : null,
updated_at: new Date(rom.updated_at),
slug: rom.slug,
platform_id: rom.platform_id,
platform_display_name: rom.platform_display_name,
name: rom.name,
path_fs: null,
path_platform_cover: `/api/romm/image/romm/assets/platforms/${rom.platform_slug}.svg`,
source: null,
source_id: null,
paths_screenshots: rom.merged_screenshots.map(s => `/api/romm/image/romm/${s}`),
platform_slug: rom.platform_slug
};
return game;
}
export function convertLocalToFrontend (g: typeof schema.games.$inferSelect & {
platform?: typeof schema.platforms.$inferSelect | null;
screenshotIds?: number[];
@ -160,49 +137,6 @@ export async function convertStoreToFrontendDetailed (system: string, id: string
return detailed;
}
export async function convertRomToFrontendDetailed (rom: DetailedRomSchema)
{
const detailed: FrontEndGameTypeDetailed = {
...convertRomToFrontend(rom),
summary: rom.summary,
fs_size_bytes: rom.fs_size_bytes,
local: false,
missing: rom.missing_from_fs,
genres: rom.metadatum.genres,
companies: rom.metadatum.companies,
release_date: rom.metadatum.first_release_date ? new Date(rom.metadatum.first_release_date) : undefined
};
const userData = await getCurrentUserApiUsersMeGet();
const gameAchievements = userData.data?.ra_progression?.results?.find(p => p.rom_ra_id == rom.ra_id);
if (rom.merged_ra_metadata?.achievements)
{
const earnedMap = new Map<string, { date: Date; date_hardcode?: Date; }>(gameAchievements?.earned_achievements.map(a => [a.id, { date: new Date(a.date), date_hardcore: a.date_hardcore ? new Date(a.date_hardcore) : undefined }]));
detailed.achievements = {
unlocked: gameAchievements?.num_awarded ?? 0,
entires: rom.merged_ra_metadata.achievements.map(a =>
{
const earned = a.badge_id ? earnedMap.get(a.badge_id) : undefined;
const ach: FrontEndGameTypeDetailedAchievement = {
id: a.badge_id ?? String(a.ra_id) ?? 'unknown',
title: a.title ?? "Unknown",
badge_url: (earned ? a.badge_url : a.badge_url_lock) ?? undefined,
date: earned?.date,
date_hardcode: earned?.date_hardcode,
description: a.description ?? undefined,
display_order: a.display_order ?? 0,
type: a.type ?? undefined
};
return ach;
}).sort((a, b) => a.display_order - b.display_order),
total: rom.merged_ra_metadata.achievements.length
};
}
return detailed;
}
export async function getLocalGameDetailed (match: any)
{
const localGame = await db.query.games.findFirst({
@ -254,29 +188,8 @@ export async function getSourceGameDetailed (source: string, id: string)
else
{
const localGame = await getLocalGameDetailed(getLocalGameMatch(id, source));
if (source === 'romm')
{
const rom = await getRomApiRomsIdGet({ path: { id: Number(id) } });
if (rom.data)
{
const romGame = await convertRomToFrontendDetailed(rom.data);
if (localGame)
{
return {
...romGame,
...localGame,
};
}
return romGame;
}
else if (localGame)
{
return localGame;
}
return undefined;
}
else if (source === 'store')
if (source === 'store')
{
const gameId = extractStoreGameSourceId(id);
const storeGame = await getStoreGame(gameId.system, gameId.id);
@ -287,11 +200,45 @@ export async function getSourceGameDetailed (source: string, id: string)
return { ...storeFrontendGame, ...localGame };
}
return storeFrontendGame;
} else if (localGame)
} else
{
return localGame;
const remoteGame = await plugins.hooks.games.fetchGame.promise({ source, id, localGame });
if (remoteGame)
{
return remoteGame;
} else if (localGame)
{
return localGame;
}
}
return undefined;
}
}
export async function checkFiles (files: DownloadFileEntry[], isArchive: boolean): Promise<LocalDownloadFileEntry[]>
{
return Promise.all(files.map(async f =>
{
// file is either zip or doesn't support sha checking
if (!f.sha1 || isArchive) return { ...f, exists: false, matches: false } satisfies LocalDownloadFileEntry;
const localPath = path.join(f.file_path, f.file_name);
if (await fs.exists(localPath))
{
if (f.size && f.size !== (await fs.stat(localPath)).size)
{
return { ...f, exists: true, matches: false } satisfies LocalDownloadFileEntry;
}
const existingHash = await hashFile(localPath, 'sha1');
if (existingHash === f.sha1)
{
return { ...f, exists: true, matches: true } satisfies LocalDownloadFileEntry;
} else
{
return { ...f, exists: true, matches: false } satisfies LocalDownloadFileEntry;
}
}
return { ...f, exists: false, matches: false } satisfies LocalDownloadFileEntry;
}));
}

View file

@ -1,6 +1,10 @@
import { GameHooks } from "./emulators";
import { AuthHooks } from "./auth";
import { EmulatorHooks } from "./emulators";
import { GameHooks } from "./games";
export class GameflowHooks
{
games = new GameHooks();
emulators = new EmulatorHooks();
auth = new AuthHooks();
}

View file

@ -0,0 +1,8 @@
import { AsyncSeriesHook } from "tapable";
export class AuthHooks
{
loginComplete = new AsyncSeriesHook<[ctx: {
service: string;
}], { auth?: string, files: DownloadFileEntry[]; } | undefined>(['ctx']);
}

View file

@ -1,21 +1,10 @@
import { SyncBailHook, AsyncSeriesHook, SyncWaterfallHook, AsyncSeriesBailHook } from 'tapable';
import { AsyncSeriesBailHook } from "tapable";
export class GameHooks
export class EmulatorHooks
{
/** override the launch command for an emulator
* @param ctx.autoValidCommands The auto generated command for example based on the ES-DE listing
* @param ctx.emulator The emulator ID if any
* @param ctx.game.source The source of the game
* @param ctx.game.sourceId The ID of the source. This could be for example the ROMM ID the game was
* @returns The argument list to be used when running the emulator.
* If no emulator bin in the command entry is found the actual command will be used as the bin.
*/
emulatorLaunch = new AsyncSeriesBailHook<[ctx: {
autoValidCommand: CommandEntry;
game: {
source: string;
sourceId: string;
id: number;
};
}], string[] | undefined>(['ctx']);
fetchBiosDownload = new AsyncSeriesBailHook<[ctx: {
emulator: string;
systems: EmulatorSystem[];
biosFolder: string;
}], { auth?: string, files: DownloadFileEntry[]; } | undefined>(['ctx']);
}

View file

@ -0,0 +1,64 @@
import { EmulatorPackageType, GameListFilterType } from '@/shared/constants';
import { SyncBailHook, AsyncSeriesHook, SyncWaterfallHook, AsyncSeriesBailHook, AsyncHook, AsyncParallelHook, SyncHook, AsyncSeriesWaterfallHook } from 'tapable';
export class GameHooks
{
/** override the launch command for an emulator
* @param ctx.autoValidCommands The auto generated command for example based on the ES-DE listing
* @param ctx.emulator The emulator ID if any
* @param ctx.game.source The source of the game
* @param ctx.game.sourceId The ID of the source. This could be for example the ROMM ID the game was
* @returns The argument list to be used when running the emulator.
* If no emulator bin in the command entry is found the actual command will be used as the bin.
*/
emulatorLaunch = new AsyncSeriesBailHook<[ctx: {
autoValidCommand: CommandEntry;
game: {
source: string;
id: number;
};
}], string[] | undefined>(['ctx']);
/**
* Fetches and returns a list of games converted to frontend.
* @param ctx.localGameIds This is local game ids in the format '<source>@<sourceId>'
*/
fetchGames = new AsyncSeriesHook<[ctx: {
query: GameListFilterType;
games: FrontEndGameType[];
}]>(['ctx']);
fetchGame = new AsyncSeriesBailHook<[ctx: {
source: string;
localGame?: FrontEndGameTypeDetailed;
id: string;
}], FrontEndGameTypeDetailed | undefined>(['ctx']);
/** Get download file URLs
* @param ctx.checksum Check if file already exists using checksums
*/
fetchDownloads = new AsyncSeriesBailHook<[ctx: {
source: string;
id: string;
}], DownloadInfo | undefined>(['ctx']);
fetchRecommendedGamesForGame = new AsyncSeriesHook<[ctx: {
game: FrontEndGameTypeDetailed,
games: (FrontEndGameType & { metadata?: any; })[];
}]>(['ctx']);
fetchRecommendedGamesForEmulator = new AsyncSeriesHook<[cts: {
emulator: EmulatorPackageType;
systems: EmulatorSystem[];
games: FrontEndGameType[];
}]>(['ctx']);
fetchPlatform = new AsyncSeriesBailHook<[ctx: {
source: string;
id: string;
}], FrontEndPlatformType | undefined>(['ctx']);
platformLookup = new AsyncSeriesBailHook<[ctx: {
source: string;
id: string;
}], { slug: string; } | undefined>(['ctx']);
fetchPlatforms = new AsyncSeriesHook<[ctx: {
platforms: FrontEndPlatformType[];
}]>(['ctx']);
updatePlayed = new AsyncSeriesWaterfallHook<[ctx: { source: string, id: string; }], boolean>(["ctx"]);
fetchCollections = new AsyncSeriesHook<[ctx: { collections: FrontEndCollection[]; }]>(['ctx']);
fetchCollection = new AsyncSeriesBailHook<[ctx: { source: string, id: string; }], FrontEndCollection | undefined>(['ctx']);
}

View file

@ -1,11 +1,8 @@
import z from "zod";
import { IJob, JobContext } from "../task-queue";
import { CACHE_KEYS, getOrCached } from "../cache";
import { config } from "../app";
import { getPlatformFirmwareApiFirmwareGet, getPlatformsApiPlatformsGet } from "@/clients/romm";
import fs from 'node:fs/promises';
import { hashFile, simulateProgress } from "@/bun/utils";
import { Downloader, FileEntry } from "@/bun/utils/downloader";
import { config, plugins } from "../app";
import { simulateProgress } from "@/bun/utils";
import { Downloader } from "@/bun/utils/downloader";
import path from 'node:path';
import { ensureDir } from "fs-extra";
import { buildStoreFrontendEmulatorSystems, getStoreEmulatorPackage } from "../store/services/gamesService";
@ -27,46 +24,27 @@ export class BiosDownloadJob implements IJob<z.infer<typeof BiosDownloadJob.data
async start (context: JobContext<IJob<never, "download">, never, "download">)
{
const allRommPlatforms = await getOrCached(CACHE_KEYS.ROM_PLATFORMS, () => getPlatformsApiPlatformsGet({ throwOnError: true }), { expireMs: 60 * 60 * 1000 }).then(d => d.data);
const emulator = await getStoreEmulatorPackage(this.emulator);
if (!emulator) throw new Error("Could Not Find Emulator");
const systems = await buildStoreFrontendEmulatorSystems(emulator);
const biosFolder = path.join(config.get('downloadPath'), "bios", this.emulator);
await ensureDir(biosFolder);
const rommPlatforms = systems.filter(s => s.romm_slug).map(s => allRommPlatforms.find(p => p.slug == s.romm_slug)).filter(r => !!r);
const files = await plugins.hooks.emulators.fetchBiosDownload.promise({ emulator: this.emulator, systems, biosFolder });
const firmwaresToDownload: FileEntry[] = [];
for (const rommPlatform of rommPlatforms)
{
const firmwares = await getPlatformFirmwareApiFirmwareGet({ query: { platform_id: rommPlatform.id } }).then(d => d.data);
if (firmwares)
{
for (const firmware of firmwares)
{
const firmwarePath = path.join(biosFolder, firmware.file_name);
const exists = await fs.exists(firmwarePath);
if (exists && await hashFile(firmwarePath, 'sha1'))
{
return;
}
firmwaresToDownload.push({ file_name: firmware.file_name, file_path: '', url: new URL(`http://romm.simeonradivoev.com/api/firmware/${firmware.id}/content/${encodeURIComponent(firmware.file_name)}`) });
}
}
}
if (!files) throw new Error("Could not find source to download from");
if (this.dryRun)
{
await simulateProgress((p) => context.setProgress(p, 'download'), context.abortSignal);
} else
{
const downloader = new Downloader('bios-download', firmwaresToDownload, biosFolder, {
const headers: Record<string, string> = {};
if (files.auth)
headers['Authorization'] = files.auth;
const downloader = new Downloader('bios-download', files.files, biosFolder, {
signal: context.abortSignal,
headers,
onProgress (stats)
{
context.setProgress(stats.progress, "download");
@ -75,7 +53,6 @@ export class BiosDownloadJob implements IJob<z.infer<typeof BiosDownloadJob.data
await downloader.start();
}
}
exposeData ()

View file

@ -5,15 +5,17 @@ import fs from 'node:fs/promises';
import * as schema from "@schema/app";
import * as emulatorSchema from "@schema/emulators";
import path from 'node:path';
import { getPlatformApiPlatformsIdGet, getRomApiRomsIdGet, PlatformSchema } from "@clients/romm";
import { config, db, emulatorsDb, events } from "../app";
import { config, db, emulatorsDb, events, plugins } from "../app";
import { extractStoreGameSourceId, getStoreGameFromId } from "../store/services/gamesService";
import * as igdb from 'ts-igdb-client';
import secrets from "../secrets";
import { hashFile, simulateProgress } from "@/bun/utils";
import { simulateProgress } from "@/bun/utils";
import { Downloader } from "@/bun/utils/downloader";
import _7z from '7zip-min';
import z from "zod";
import { checkFiles } from "../games/services/utils";
import { ensureDir } from "fs-extra";
import { getAuthToken } from "@/clients/romm/core/auth.gen";
interface JobConfig
{
@ -30,16 +32,14 @@ export class InstallJob implements IJob<never, InstallJobStates>
static dataSchema = z.never();
public gameId: string;
public source: string;
public sourceId: string;
public config?: JobConfig;
public group = InstallJob.id;
constructor(id: string, source: string, sourceId: string, config?: JobConfig)
constructor(id: string, source: string, config?: JobConfig)
{
this.gameId = id;
this.config = config;
this.sourceId = sourceId;
this.source = source;
}
@ -49,102 +49,53 @@ export class InstallJob implements IJob<never, InstallJobStates>
fs.mkdir(config.get('downloadPath'), { recursive: true });
const downloadPath = config.get('downloadPath');
let files: {
url: URL,
file_path: string;
file_name: string;
size?: number;
}[] = [];
let screenshotUrls: string[];
let coverUrl: string;
let rommPlatform: PlatformSchema | undefined;
let slug: string | null;
let path_fs: string | undefined;
let summary: string | null;
let name: string | null;
let last_played: Date | null;
let igdb_id: number | null;
let ra_id: number | null;
let source_id: string;
let system_slug: string;
let extract_path: string;
let metadata: any | undefined;
let info: DownloadInfo | undefined;
switch (this.source)
{
case 'romm':
const rom = (await getRomApiRomsIdGet({ path: { id: Number(this.gameId) }, throwOnError: true })).data;
rommPlatform = (await getPlatformApiPlatformsIdGet({ path: { id: rom.platform_id }, throwOnError: true })).data;
const rommAddress = config.get('rommAddress');
coverUrl = `${rommAddress}${rom.path_cover_large}`;
screenshotUrls = rom.merged_screenshots.map(s => `${config.get('rommAddress')}${s}`);
last_played = rom.rom_user.last_played ? new Date(rom.rom_user.last_played) : null;
igdb_id = rom.igdb_id;
ra_id = rom.ra_id;
summary = rom.summary;
name = rom.name;
path_fs = path.join(rom.fs_path, rom.fs_name);
source_id = String(rom.id);
slug = rom.slug;
system_slug = rommPlatform.slug;
extract_path = '';
metadata = rom.metadatum;
const rommFiles = await Promise.all(rom.files.map(async f =>
{
const localPath = path.join(config.get('downloadPath'), f.full_path);
if (f.md5_hash && await fs.exists(localPath))
{
const existingHash = await hashFile(localPath, 'sha1');
if (existingHash === f.md5_hash)
{
console.log("File Already Present: ", f.full_path);
return undefined;
}
console.warn("File ", f.full_path, 'with hash', existingHash, 'has different hash than', f.sha1_hash);
}
return {
url: new URL(`${config.get('rommAddress')}/api/romsfiles/${f.id}/content/${f.file_name}`),
file_name: f.file_name,
file_path: path.join(config.get('downloadPath'), f.file_path),
size: f.file_size_bytes
};
}));
files.push(...rommFiles.filter(f => f !== undefined));
break;
case 'store':
const game = await getStoreGameFromId(this.gameId);
const gameId = extractStoreGameSourceId(this.gameId);
coverUrl = game.pictures.titlescreens[0];
screenshotUrls = game.pictures.screenshots;
files.push({ url: new URL(game.file), file_path: `roms/${game.system}`, file_name: path.basename(decodeURI(game.file)) });
slug = this.gameId;
source_id = this.gameId;
name = game.title;
summary = game.description;
system_slug = gameId.system;
extract_path = path.join('roms', gameId.system);
info = {
coverUrl: game.pictures.titlescreens[0],
screenshotUrls: game.pictures.screenshots,
files: [{
url: new URL(game.file),
file_path: `roms/${game.system}`,
file_name: path.basename(decodeURI(game.file)),
size: 0
}],
slug: this.gameId,
source_id: this.gameId,
name: game.title,
summary: game.description,
system_slug: gameId.system,
extract_path: path.join('roms', gameId.system),
};
break;
default:
throw new Error("Unsupported source");
info = await plugins.hooks.games.fetchDownloads.promise({ source: this.source, id: this.gameId });
break;
}
if (!info) throw new Error(`Could not find downloader for source ${this.source}`);
const files = await checkFiles(info.files, !!info.extract_path);
if (this.config?.dryRun !== true)
{
if (this.config?.dryDownload !== true)
if (this.config?.dryDownload !== true && files.some(f => !f.exists || !f.matches))
{
const headers: Record<string, string> = {};
if (info.auth)
headers['Authorization'] = info.auth;
const downloader = new Downloader(`game-${this.source}-${this.gameId}`,
files,
files.filter(f => !f.exists || !f.matches),
config.get('downloadPath'),
{
signal: cx.abortSignal,
headers,
onProgress (stats)
{
cx.setProgress(stats.progress, 'download');
@ -152,21 +103,21 @@ export class InstallJob implements IJob<never, InstallJobStates>
});
const downloadedFiles = await downloader.start();
if (extract_path && downloadedFiles)
if (info.extract_path && downloadedFiles)
{
for (const path of downloadedFiles)
{
await _7z.unpack(path, extract_path);
await _7z.unpack(path, info.extract_path);
}
}
}
if (this.config?.dryDownload === true)
if (this.config?.dryDownload === true && info.extract_path)
{
await mkdir(path.join(downloadPath, extract_path), { recursive: true });
await ensureDir(path.join(downloadPath, info.extract_path));
}
const coverResponse = await fetch(coverUrl);
const coverResponse = await fetch(info.coverUrl);
const cover = Buffer.from(await coverResponse.arrayBuffer());
if (cx.abortSignal.aborted) return;
@ -174,18 +125,18 @@ export class InstallJob implements IJob<never, InstallJobStates>
await db.transaction(async (tx) =>
{
// Search for existing platform
const platformSearch = [eq(schema.platforms.slug, system_slug)];
const esPlatformSearch = [eq(emulatorSchema.systemMappings.system, system_slug)];
const platformSearch = [eq(schema.platforms.slug, info.system_slug)];
const esPlatformSearch = [eq(emulatorSchema.systemMappings.system, info.system_slug)];
if (rommPlatform)
if (info.platform)
{
if (rommPlatform.igdb_id) platformSearch.push(eq(schema.platforms.igdb_id, rommPlatform.igdb_id));
if (rommPlatform.igdb_slug) platformSearch.push(eq(schema.platforms.igdb_slug, rommPlatform.igdb_slug));
if (rommPlatform.ra_id) platformSearch.push(eq(schema.platforms.ra_id, rommPlatform.ra_id));
if (rommPlatform.moby_id) platformSearch.push(eq(schema.platforms.moby_id, rommPlatform.moby_id));
if (info.platform.igdb_id) platformSearch.push(eq(schema.platforms.igdb_id, info.platform.igdb_id));
if (info.platform.igdb_slug) platformSearch.push(eq(schema.platforms.igdb_slug, info.platform.igdb_slug));
if (info.platform.ra_id) platformSearch.push(eq(schema.platforms.ra_id, info.platform.ra_id));
if (info.platform.moby_id) platformSearch.push(eq(schema.platforms.moby_id, info.platform.moby_id));
esPlatformSearch.push(eq(emulatorSchema.systemMappings.source, 'romm'));
esPlatformSearch.push(eq(emulatorSchema.systemMappings.sourceSlug, rommPlatform.slug));
esPlatformSearch.push(eq(emulatorSchema.systemMappings.sourceSlug, info.platform.slug));
}
const esPlatform = await emulatorsDb.query.systemMappings.findFirst({
@ -201,9 +152,9 @@ export class InstallJob implements IJob<never, InstallJobStates>
if (!existingPlatform)
{
// TODO: use something else than the romm demo as CDN
const platformCover = await fetch(`https://demo.romm.app/assets/platforms/${system_slug}.svg`);
const platformCover = await fetch(`https://demo.romm.app/assets/platforms/${info.system_slug}.svg`);
if (!esPlatform && !rommPlatform)
if (!esPlatform && !info.platform)
{
// go to unknown platform
existingPlatform = await tx.query.platforms.findFirst({ where: eq(schema.platforms.slug, "unknown") });
@ -223,14 +174,14 @@ export class InstallJob implements IJob<never, InstallJobStates>
{
// Create new local platform
const platform: typeof schema.platforms.$inferInsert = {
slug: rommPlatform?.slug ?? esPlatform?.system.name ?? '',
igdb_id: rommPlatform?.igdb_id,
igdb_slug: rommPlatform?.igdb_slug,
ra_id: rommPlatform?.ra_id,
slug: info.platform?.slug ?? esPlatform?.system.name ?? '',
igdb_id: info.platform?.igdb_id,
igdb_slug: info.platform?.igdb_slug,
ra_id: info.platform?.ra_id,
cover: Buffer.from(await platformCover.arrayBuffer()),
cover_type: platformCover.headers.get('content-type'),
name: rommPlatform?.name ?? esPlatform?.system.fullname ?? '',
family_name: rommPlatform?.family_name,
name: info.platform?.name ?? esPlatform?.system.fullname ?? '',
family_name: info.platform?.family_name,
es_slug: esPlatform?.system.name ?? undefined
};
@ -246,38 +197,38 @@ export class InstallJob implements IJob<never, InstallJobStates>
// create the rom
const game: typeof schema.games.$inferInsert = {
source_id,
source_id: info.source_id,
source: this.source,
slug,
path_fs,
last_played: last_played,
slug: info.slug,
path_fs: info.path_fs,
last_played: info.last_played,
platform_id: platformId,
igdb_id: igdb_id,
ra_id: ra_id,
summary: summary,
name,
igdb_id: info.igdb_id,
ra_id: info.ra_id,
summary: info.summary,
name: info.name,
cover,
cover_type: coverResponse.headers.get('content-type'),
metadata
metadata: info.metadata
};
const [{ id }] = await tx.insert(schema.games).values(game).returning({ id: schema.games.id });
if (screenshotUrls.length <= 0 && process.env.TWITCH_CLIENT_ID)
if (info.screenshotUrls.length <= 0 && process.env.TWITCH_CLIENT_ID)
{
const access_token = await secrets.get({ service: 'gamflow_twitch', name: 'access_token' });
if (access_token)
{
const client = igdb.igdb(process.env.TWITCH_CLIENT_ID, access_token);
const { data } = await client.request('artworks').pipe(igdb.fields(['game', 'url']), igdb.where('game', '=', igdb_id)).execute();
const { data } = await client.request('artworks').pipe(igdb.fields(['game', 'url']), igdb.where('game', '=', info.igdb_id)).execute();
screenshotUrls.push(...data.filter(s => s.url).map(s => s.url!));
info.screenshotUrls.push(...data.filter(s => s.url).map(s => s.url!));
}
}
// pre-fetch screenshots
const screenshots = await Promise.all(screenshotUrls.map(s => fetch(s)));
const screenshots = await Promise.all(info.screenshotUrls.map(s => fetch(s)));
if (screenshots.length > 0)
{
@ -300,6 +251,6 @@ export class InstallJob implements IJob<never, InstallJobStates>
}
events.emit('notification', { message: `${name}: Installed`, type: 'success', duration: 8000 });
events.emit('notification', { message: `${info.name}: Installed`, type: 'success', duration: 8000 });
}
}

View file

@ -3,9 +3,8 @@ import { IJob, JobContext } from "../task-queue";
import { ActiveGameSchema, ActiveGameType } from "@/bun/types/typesc.schema";
import { db, events, plugins } from "../app";
import * as appSchema from "@schema/app";
import { eq } from "drizzle-orm";
import { eq, sql } from "drizzle-orm";
import { spawn } from 'node:child_process';
import { updateRomUserApiRomsIdPropsPut } from '@/clients/romm';
export class LaunchGameJob implements IJob<z.infer<typeof LaunchGameJob.dataSchema>, "playing">
{
@ -38,7 +37,7 @@ export class LaunchGameJob implements IJob<z.infer<typeof LaunchGameJob.dataSche
const commandArgs = await plugins.hooks.games.emulatorLaunch.promise({
autoValidCommand: this.validCommand,
game: { source: this.gameSource, sourceId: this.gameSourceId, id: this.gameId }
game: { source: this.gameSource, id: this.gameId }
});
const command = commandArgs ? this.validCommand.metadata.emulatorBin ?? this.validCommand.command : this.validCommand.command;
@ -68,19 +67,22 @@ export class LaunchGameJob implements IJob<z.infer<typeof LaunchGameJob.dataSche
command: this.validCommand
};
function updateRommProps (id: number)
const updatePlayed = async (source: string, id: string) =>
{
updateRomUserApiRomsIdPropsPut({ path: { id }, body: { update_last_played: true } });
events.emit('notification', { message: "Updated Last Played", type: 'success' });
}
await db.update(appSchema.games).set({ last_played: new Date() }).where(eq(appSchema.games.id, this.gameId));
await plugins.hooks.games.updatePlayed.promise({ source, id }).then(v =>
{
if (v) events.emit('notification', { message: "Updated Last Played", type: 'success' });
});
};
if (this.gameSource === 'romm')
if (this.gameSource !== 'local')
{
updateRommProps(Number(this.gameSourceId));
updatePlayed(this.gameSource, this.gameSourceId);
}
else if (localGame?.source === 'romm' && localGame.source_id)
else if (localGame?.source && localGame?.source !== 'local' && localGame.source_id)
{
updateRommProps(Number(localGame.source_id));
updatePlayed(localGame.source, localGame.source_id);
}
});

View file

@ -37,7 +37,7 @@ export class LoginJob implements IJob<z.infer<typeof LoginJob.dataSchema>, "base
.post(`/login`, async ({ body }) =>
{
const response = await tryLoginAndSave(body as any);
if (response?.code === 200)
if (response.response.ok)
{
context.abort("success");
return status("Accepted");

View file

@ -3,6 +3,7 @@ import secrets from "../secrets";
import open from "open";
import z from "zod";
import { delay } from "@/shared/utils";
import { plugins } from "../app";
interface TwitchDevice
@ -94,6 +95,8 @@ export default class TwitchLoginJob implements IJob<z.infer<typeof TwitchLoginJo
secrets.set({ service: 'gamflow_twitch', name: 'access_token', value: data.access_token });
secrets.set({ service: 'gamflow_twitch', name: 'refresh_token', value: data.refresh_token });
secrets.set({ service: 'gamflow_twitch', name: 'expires_in', value: new Date(new Date().getTime() + data.expires_in).toString() });
await plugins.hooks.auth.loginComplete.promise({ service: 'twitch' });
break;
}
else if (res.status !== 400)

View file

@ -47,9 +47,4 @@ export default class PCSX2Integration implements PluginType
}
});
}
async downloadBios (id: number)
{
}
}

View file

@ -0,0 +1,12 @@
{
"name": "com.simeonradivoev.gameflow.romm",
"displayName": "ROMM Integration",
"version": "0.0.1",
"description": "ROMM Server Integration",
"main": "./romm.ts",
"icon": "https://romm.app/_ipx/q_80/images/blocks/logos/romm.svg",
"keywords": [
"integration",
"romm"
]
}

View file

@ -0,0 +1,408 @@
import { PluginContextType, PluginType } from "@/bun/types/typesc.schema";
import desc from './package.json';
import { DetailedRomSchema, getCollectionApiCollectionsIdGet, getCollectionsApiCollectionsGet, getCurrentUserApiUsersMeGet, getPlatformApiPlatformsIdGet, getPlatformFirmwareApiFirmwareGet, getPlatformsApiPlatformsGet, getRomApiRomsIdGet, getRomsApiRomsGet, SimpleRomSchema, updateRomUserApiRomsIdPropsPut } from "@/clients/romm";
import { config } from "@/bun/api/app";
import path from 'node:path';
import fs from 'node:fs/promises';
import { hashFile, isSteamDeckGameMode } from "@/bun/utils";
import { CACHE_KEYS, getOrCached } from "@/bun/api/cache";
import secrets from "@/bun/api/secrets";
import { getAuthToken } from "@/clients/romm/core/auth.gen";
import { client } from "@/clients/romm/client.gen";
export default class RommIntegration implements PluginType
{
isSteamDeck = false;
async updateClient ()
{
client.setConfig({
baseUrl: config.get('rommAddress'),
async auth (auth)
{
if (auth.scheme === 'bearer')
{
return (await secrets.get({ service: 'gameflow', name: 'romm_access_token' })) ?? undefined;
}
}
});
}
async getAuthToken ()
{
return getAuthToken({
scheme: 'bearer',
type: "http"
}, async (a) => (await secrets.get({ service: "gameflow", name: 'romm_access_token' })) ?? undefined);
}
async getAllRommPlatforms ()
{
return getOrCached(CACHE_KEYS.ROM_PLATFORMS, () => getPlatformsApiPlatformsGet({ throwOnError: true }), { expireMs: 60 * 60 * 1000 }).then(d => d.data);
}
convertRomToFrontend (rom: SimpleRomSchema)
{
const game: FrontEndGameType = {
id: { id: String(rom.id), source: 'romm' },
path_cover: `/api/romm/image/romm${this.isSteamDeck ? rom.path_cover_small : rom.path_cover_large}`,
last_played: rom.rom_user.last_played ? new Date(rom.rom_user.last_played) : null,
updated_at: new Date(rom.created_at),
slug: rom.slug,
platform_id: rom.platform_id,
platform_display_name: rom.platform_display_name,
name: rom.name,
path_fs: null,
path_platform_cover: `/api/romm/image/romm/assets/platforms/${rom.platform_slug}.svg`,
source: null,
source_id: null,
paths_screenshots: rom.merged_screenshots.map(s => `/api/romm/image/romm/${s}`),
platform_slug: rom.platform_slug
};
return game;
}
async convertRomToFrontendDetailed (rom: DetailedRomSchema)
{
const detailed: FrontEndGameTypeDetailed = {
...this.convertRomToFrontend(rom),
summary: rom.summary,
fs_size_bytes: rom.fs_size_bytes,
local: false,
missing: rom.missing_from_fs,
genres: rom.metadatum.genres,
companies: rom.metadatum.companies,
release_date: rom.metadatum.first_release_date ? new Date(rom.metadatum.first_release_date) : undefined
};
const userData = await getCurrentUserApiUsersMeGet();
const gameAchievements = userData.data?.ra_progression?.results?.find(p => p.rom_ra_id == rom.ra_id);
if (rom.merged_ra_metadata?.achievements)
{
const earnedMap = new Map<string, { date: Date; date_hardcode?: Date; }>(gameAchievements?.earned_achievements.map(a => [a.id, { date: new Date(a.date), date_hardcore: a.date_hardcore ? new Date(a.date_hardcore) : undefined }]));
detailed.achievements = {
unlocked: gameAchievements?.num_awarded ?? 0,
entires: rom.merged_ra_metadata.achievements.map(a =>
{
const earned = a.badge_id ? earnedMap.get(a.badge_id) : undefined;
const ach: FrontEndGameTypeDetailedAchievement = {
id: a.badge_id ?? String(a.ra_id) ?? 'unknown',
title: a.title ?? "Unknown",
badge_url: (earned ? a.badge_url : a.badge_url_lock) ?? undefined,
date: earned?.date,
date_hardcode: earned?.date_hardcode,
description: a.description ?? undefined,
display_order: a.display_order ?? 0,
type: a.type ?? undefined
};
return ach;
}).sort((a, b) => a.display_order - b.display_order),
total: rom.merged_ra_metadata.achievements.length
};
}
return detailed;
}
async setup ()
{
this.isSteamDeck = isSteamDeckGameMode();
await this.updateClient();
}
load (ctx: PluginContextType)
{
ctx.hooks.games.fetchGames.tapPromise(desc.name, async ({ query, games }) =>
{
if (((!query.platform_source || query.platform_source === 'romm') || !!query.collection_id) && (!query.source || query.source === 'romm'))
{
const orderByMap: Record<string, string> = {
added: "created_at",
activity: "created_at",
name: "name"
};
const rommGames = await getRomsApiRomsGet({
query: {
platform_ids: query.platform_id ? [query.platform_id] : undefined,
collection_id: query.collection_id,
limit: query.limit,
offset: query.offset,
order_by: orderByMap[query.orderBy ?? '']
}, throwOnError: true
});
games.push(...rommGames.data.items.map(g =>
{
return this.convertRomToFrontend(g);
}));
}
});
ctx.hooks.auth.loginComplete.tapPromise(desc.name, async ({ service }) =>
{
if (service !== 'romm') return;
await this.updateClient();
});
ctx.hooks.games.fetchGame.tapPromise(desc.name, async ({ source, id, localGame }) =>
{
if (source !== 'romm') return;
const rom = await getRomApiRomsIdGet({ path: { id: Number(id) } });
if (rom.data)
{
const romGame = await this.convertRomToFrontendDetailed(rom.data);
if (localGame)
{
return {
...romGame,
...localGame,
};
}
return romGame;
}
return undefined;
});
ctx.hooks.games.fetchDownloads.tapPromise(desc.name, async ({ source, id }) =>
{
if (source !== 'romm') return;
const rom = (await getRomApiRomsIdGet({ path: { id: Number(id) }, throwOnError: true })).data;
const rommPlatform = (await getPlatformApiPlatformsIdGet({ path: { id: rom.platform_id }, throwOnError: true })).data;
const rommAddress = config.get('rommAddress');
if (!rommAddress) throw new Error("Romm Address Not Defined");
const files = await Promise.all(rom.files.map(async f =>
{
const file: DownloadFileEntry = {
url: new URL(`${config.get('rommAddress')}/api/romsfiles/${f.id}/content/${f.file_name}`),
file_name: f.file_name,
file_path: path.join(config.get('downloadPath'), f.file_path),
size: f.file_size_bytes,
sha1: f.sha1_hash ?? undefined
};
return file;
}));
const info: DownloadInfo = {
platform: {
slug: rommPlatform.slug,
name: rommPlatform.name,
family_name: rommPlatform.family_name ?? undefined
},
coverUrl: `${rommAddress}${rom.path_cover_large}`,
screenshotUrls: rom.merged_screenshots.map(s => `${config.get('rommAddress')}${s}`),
last_played: rom.rom_user.last_played ? new Date(rom.rom_user.last_played) : undefined,
igdb_id: rom.igdb_id ?? undefined,
ra_id: rom.ra_id ?? undefined,
summary: rom.summary ?? undefined,
name: rom.name ?? "Unknown",
path_fs: path.join(rom.fs_path, rom.fs_name),
source_id: String(rom.id),
slug: rom.slug ?? undefined,
system_slug: rommPlatform.slug,
metadata: rom.metadatum,
files,
auth: await this.getAuthToken()
};
return info;
});
ctx.hooks.emulators.fetchBiosDownload.tapPromise(desc.name, async ({ systems, biosFolder }) =>
{
const files: DownloadFileEntry[] = [];
const allRommPlatforms = await this.getAllRommPlatforms();
const rommPlatforms = systems.filter(s => s.romm_slug).map(s => allRommPlatforms.find(p => p.slug == s.romm_slug)).filter(r => !!r);
for (const rommPlatform of rommPlatforms)
{
const firmwares = await getPlatformFirmwareApiFirmwareGet({ query: { platform_id: rommPlatform.id } }).then(d => d.data);
if (firmwares)
{
for (const firmware of firmwares)
{
const firmwarePath = path.join(biosFolder, firmware.file_name);
const exists = await fs.exists(firmwarePath);
if (exists && await hashFile(firmwarePath, 'sha1'))
{
return;
}
files.push({ file_name: firmware.file_name, file_path: '', url: new URL(`http://romm.simeonradivoev.com/api/firmware/${firmware.id}/content/${encodeURIComponent(firmware.file_name)}`) });
}
}
}
if (files.length > 0) return { files, auth: await this.getAuthToken() };
});
ctx.hooks.games.fetchRecommendedGamesForGame.tapPromise(desc.name, async ({ game, games }) =>
{
const rommPlatforms = await this.getAllRommPlatforms();
if (rommPlatforms)
{
const rommPlatform = rommPlatforms.find(p => p.slug === game.platform_slug);
if (rommPlatform)
{
const rommGames = await getRomsApiRomsGet({ query: { genres: game.genres, genres_logic: 'any' } });
if (rommGames.data)
{
games.push(...rommGames.data.items.map(g => ({ ...this.convertRomToFrontend(g), metadata: g.metadatum })));
}
}
}
});
ctx.hooks.games.fetchRecommendedGamesForEmulator.tapPromise(desc.name, async ({ emulator, games, systems }) =>
{
const rommPlatforms = await this.getAllRommPlatforms();
const systemsRommSlugSet = new Set(systems.filter(s => s.romm_slug).map(s => s.romm_slug!));
if (rommPlatforms)
{
const platformIds = rommPlatforms.filter(p => systemsRommSlugSet.has(p.slug)).map(s => s.id);
if (platformIds.length > 0)
{
const rommGames = await getRomsApiRomsGet({
query: {
platform_ids: platformIds
}
});
let gamesPerSystem = Math.round(3 / systemsRommSlugSet.size);
for (const slug of systemsRommSlugSet)
{
const systemRommGames = rommGames.data?.items.filter(g => slug === g.platform_slug).map(g =>
{
return this.convertRomToFrontend(g);
}).slice(0, gamesPerSystem) ?? [];
games.push(...systemRommGames);
}
}
}
});
ctx.hooks.games.fetchPlatform.tapPromise(desc.name, async ({ source, id }) =>
{
if (source !== 'romm') return;
const { data: rommPlatform } = await getPlatformApiPlatformsIdGet({ path: { id: Number(id) } });
if (rommPlatform)
{
const platform: FrontEndPlatformType = {
slug: rommPlatform.slug,
name: rommPlatform.display_name,
family_name: rommPlatform.family_name,
path_cover: `/api/romm/image/romm/assets/platforms/${rommPlatform.slug}.svg`,
game_count: rommPlatform.rom_count,
updated_at: new Date(rommPlatform.updated_at),
id: { source: 'romm', id: String(rommPlatform.id) },
paths_screenshots: [],
hasLocal: false
};
return platform;
}
});
ctx.hooks.games.fetchPlatforms.tapPromise(desc.name, async ({ platforms }) =>
{
const rommPlatforms = await this.getAllRommPlatforms();
if (rommPlatforms)
{
const frontEndPlatforms = await Promise.all(rommPlatforms.map(async p =>
{
const screenshots: string[] = [];
const rommGames = await getRomsApiRomsGet({ query: { platform_ids: [p.id], limit: 3 } }).then(d => d.data);
if (rommGames)
{
const rommScreenshots = rommGames.items.find(i => i.merged_screenshots.length > 0)?.merged_screenshots.map(s => `/api/romm/image/romm/${s}`);
if (rommScreenshots)
screenshots.push(...rommScreenshots);
}
const platform: FrontEndPlatformType = {
slug: p.slug,
name: p.display_name,
family_name: p.family_name,
path_cover: `/api/romm/image/romm/assets/platforms/${p.slug}.svg`,
game_count: p.rom_count,
updated_at: new Date(p.updated_at),
id: { source: 'romm', id: String(p.id) },
hasLocal: false,
paths_screenshots: screenshots
};
return platform;
}));
platforms.push(...frontEndPlatforms);
}
});
ctx.hooks.games.updatePlayed.tapPromise(desc.name, async ({ source, id }) =>
{
if (source !== 'romm') return false;
const resp = await updateRomUserApiRomsIdPropsPut({ path: { id: Number(id) }, body: { update_last_played: true } });
if (resp.error) console.error(resp.error);
return resp.response.ok;
});
ctx.hooks.games.fetchCollections.tapPromise(desc.name, async ({ collections }) =>
{
const rommCollections = await getCollectionsApiCollectionsGet();
if (rommCollections.response.ok && rommCollections.data)
{
collections.push(...rommCollections.data.map(c =>
{
const collection: FrontEndCollection = {
id: { source: 'romm', id: String(c.id) },
name: c.name,
description: c.description,
game_count: c.rom_count,
path_platform_cover: `/api/romm/image/romm${this.isSteamDeck ? c.path_covers_small ?? c.path_covers_small[0] : c.path_cover_large ?? c.path_covers_large[0]}`
};
return collection;
}));
}
});
ctx.hooks.games.fetchCollection.tapPromise(desc.name, async ({ source, id }) =>
{
if (source !== 'romm') return;
const collection = await getCollectionApiCollectionsIdGet({ path: { id: Number(id) } });
if (collection.data)
{
const col: FrontEndCollection = {
id: { source: 'romm', id: String(id) },
name: collection.data.name,
description: collection.data.owner_username,
path_platform_cover: `/api/romm/image/romm${this.isSteamDeck ? collection.data.path_covers_small ?? collection.data.path_covers_small[0] : collection.data.path_cover_large ?? collection.data.path_covers_large[0]}`,
game_count: collection.data.rom_count
};
return col;
}
});
ctx.hooks.games.platformLookup.tapPromise(desc.name, async ({ source, id }) =>
{
if (source !== 'romm') return;
const platforms = await this.getAllRommPlatforms();
return platforms.find(p => p.id === Number(id));
});
}
}

View file

@ -27,7 +27,8 @@ export class PluginManager
if (plugin.setup) await plugin.setup();
this.plugins[description.name] = {
enabled: !config.get('disabledPlugins').includes(description.name),
loaded: false, plugin: plugin,
loaded: false,
plugin: plugin,
source: source,
description: description
};

View file

@ -1,25 +1,26 @@
import { PluginManager } from "./plugin-manager";
import pcsx2 from './builtin/emulators/com.simeonradivoev.gameflow.pcsx2/package.json';
import romm from './builtin/sources/com.simeonradivoev.gameflow.romm/package.json';
import { PluginDescriptionSchema, PluginDescriptionType, PluginSchema } from "@/bun/types/typesc.schema";
import path from "node:path";
export default async function register (pluginManager: PluginManager)
{
const plugins: (PluginDescriptionType & { main: string; root: string; })[] = [
{ ...pcsx2, root: './builtin/emulators/com.simeonradivoev.gameflow.pcsx2' }
const plugins: (PluginDescriptionType & { main: string; load: () => Promise<any>; })[] = [
{ ...pcsx2, load: () => import('./builtin/emulators/com.simeonradivoev.gameflow.pcsx2/pcsx2') },
{ ...romm, load: () => import('./builtin/sources/com.simeonradivoev.gameflow.romm/romm') },
];
await Promise.all(plugins.map(async (pluginPackage) =>
{
const file = await import(`./${path.join(pluginPackage.root, pluginPackage.main)}`);
const file = await pluginPackage.load();
if (file.default && typeof file.default === 'function')
{
const pluginInstance = new file.default();
const plugin = await PluginSchema.parseAsync(pluginInstance);
await PluginSchema.parseAsync(pluginInstance);
const description = await PluginDescriptionSchema.parseAsync(pluginPackage);
pluginManager.register(plugin, description, 'builtin');
pluginManager.register(pluginInstance, description, 'builtin');
}
}));
}

View file

@ -54,7 +54,6 @@ export async function getRelevantEmulators ()
const finalEmulators = await Promise.all(Array.from(groupedEmulators.entries()).map(async ([emulator, system_slug]) =>
{
const execPaths = await findExecsByName(emulator);
const validExecPath = execPaths.find(e => e.exists);
let platform: number | null | undefined = null;
const validSystemSlug = system_slug.find(s => s.system);
@ -63,7 +62,7 @@ export async function getRelevantEmulators ()
platform = platformLookup.get(validSystemSlug.system)?.platform_id;
}
const systems = Array.from(new Set(system_slug.filter(s => s.system).map(s => s.system!)));
if (validExecPath)
if (execPaths.some(p => p.exists))
{
systems.forEach(s => platformViability.set(s, true));
}
@ -71,10 +70,10 @@ export async function getRelevantEmulators ()
const em: FrontEndEmulator & { isCritical: boolean; } = {
name: emulator,
logo: platform ? `/api/romm/platform/local/${platform}/cover` : '',
systems: systems.map(s => platformLookup.get(s)).filter(s => !!s).map(e => ({ icon: `/api/romm/image/romm/assets/platforms/${e.es_slug}.svg`, name: e.platform_name ?? 'Unknown', id: e.es_slug ?? '' })),
systems: systems.map(s => platformLookup.get(s)).filter(s => !!s).map(e => ({ iconUrl: `/api/romm/image/romm/assets/platforms/${e.es_slug}.svg`, name: e.platform_name ?? 'Unknown', id: e.es_slug ?? '' })),
gameCount: 0,
isCritical: false,
validSource: validExecPath
validSources: execPaths
};
return em;
@ -82,11 +81,12 @@ export async function getRelevantEmulators ()
finalEmulators.push({
name: 'EMULATORJS',
validSource: { binPath: `${SERVER_URL(host)}`, type: 'js', exists: true },
validSources: [{ binPath: `${SERVER_URL(host)}`, type: 'embedded', exists: true }],
logo: `/api/romm/image?url=${encodeURIComponent('https://emulatorjs.org/logo/EmulatorJS.png')}`,
systems: [],
gameCount: 0,
isCritical: false,
description: "Embedded Emulator. Uses Retroarch Cores"
});
return finalEmulators.map(e =>

View file

@ -4,19 +4,15 @@ import * as emulatorSchema from '@schema/emulators';
import { findExecs } from "../../games/services/launchGameService";
import { eq } from "drizzle-orm";
export async function convertStoreEmulatorToFrontend (emulator: EmulatorPackageType, gameCount: number, systems: {
id: string;
name: string;
icon: string;
}[])
export async function convertStoreEmulatorToFrontend (emulator: EmulatorPackageType, gameCount: number, systems: EmulatorSystem[])
{
let execPath: EmulatorSourceEntryType | undefined;
const execPaths: EmulatorSourceEntryType[] = [];
const esEmulator = await emulatorsDb.query.emulators.findFirst({ where: eq(emulatorSchema.emulators.name, emulator.name) });
if (esEmulator)
{
const allExecs = await findExecs(emulator.name, esEmulator);
if (allExecs.length > 0) execPath = allExecs[0];
execPaths.push(...allExecs);
}
const em: FrontEndEmulator = {
@ -24,7 +20,7 @@ export async function convertStoreEmulatorToFrontend (emulator: EmulatorPackageT
logo: emulator.logo,
systems,
gameCount,
validSource: execPath,
validSources: execPaths,
integration: findEmulatorPluginIntegration(emulator.name)
};

View file

@ -113,7 +113,7 @@ export async function getAllStoreEmulatorPackages ()
return emulatesParsed;
}
export async function buildStoreFrontendEmulatorSystems (emulator: EmulatorPackageType)
export async function buildStoreFrontendEmulatorSystems (emulator: EmulatorPackageType): Promise<EmulatorSystem[]>
{
const systems = await Promise.all(emulator.systems.map(async system =>
{
@ -125,7 +125,7 @@ export async function buildStoreFrontendEmulatorSystems (emulator: EmulatorPacka
let icon: string = `/api/romm/image/romm/assets/platforms/${rommSystem?.sourceSlug ?? system}.svg`;
return { id: system, romm_slug: rommSystem?.sourceSlug, name: esSystem?.fullname ?? system, icon: icon };
return { id: system, romm_slug: rommSystem?.sourceSlug ?? undefined, name: esSystem?.fullname ?? system, iconUrl: icon } satisfies EmulatorSystem;
}));
return systems;

View file

@ -48,7 +48,12 @@ export const store = new Elysia({ prefix: '/api/store' })
if (query.missing)
{
frontEndEmulators = frontEndEmulators.filter(e => !e.validSource);
frontEndEmulators = frontEndEmulators.filter(e =>
{
if (e.validSources.some(s => s.exists)) return false;
if (query.related && e.name === query.related) return false;
return true;
});
}
if (query.orderBy === 'importance')
@ -72,7 +77,8 @@ export const store = new Elysia({ prefix: '/api/store' })
query: z.object({
limit: z.coerce.number().optional(),
missing: z.stringbool().optional().describe("Show Only Non Installed emulators"),
orderBy: z.enum(['name', 'recently_updated', 'importance']).optional()
orderBy: z.enum(['name', 'recently_updated', 'importance']).optional(),
related: z.string().optional()
})
})
.get('/games/featured', async () =>
@ -112,7 +118,6 @@ export const store = new Elysia({ prefix: '/api/store' })
const emulatorScreenshotsPath = path.join(getStoreFolder(), "media", "screenshots", id);
const screenshots = await fs.exists(emulatorScreenshotsPath) ? await fs.readdir(emulatorScreenshotsPath) : [];
const validExec = execPaths.find(p => p.exists);
const biosDirPath = path.join(config.get('downloadPath'), 'bios', id);
const biosFiles = await fs.exists(biosDirPath) ? await fs.readdir(biosDirPath) : [];
@ -120,7 +125,7 @@ export const store = new Elysia({ prefix: '/api/store' })
name: emulatorPackage.name,
description: emulatorPackage.description,
systems,
validSource: validExec,
validSources: execPaths,
screenshots: screenshots.map(s => `/api/store/screenshot/emulator/${id}/${s}`),
gameCount: 0,
homepage: emulatorPackage.homepage,

View file

@ -7,10 +7,10 @@ import { isSteamDeck, openExternal } from "../utils";
import fs from 'node:fs/promises';
import buildNotificationsStream from "./notifications";
import path, { dirname } from "node:path";
import { DirSchema } from "@/shared/constants";
import { DirSchema, SystemInfoSchema } from "@/shared/constants";
import { getDevices, getDevicesCurated } from "./drives";
import getFolderSize from "get-folder-size";
import si from 'systeminformation';
import si, { battery } from 'systeminformation';
import { getStoreFolder } from "./store/services/gamesService";
export const system = new Elysia({ prefix: '/api/system' })
@ -61,6 +61,33 @@ export const system = new Elysia({ prefix: '/api/system' })
set.headers['connection'] = 'keep-alive';
return new Response(buildNotificationsStream());
})
.ws('/info/system', {
response: SystemInfoSchema,
async open (ws)
{
const valuesObject = {
battery: 'percent, isCharging, acConnected, hasBattery'
};
const battery = await si.battery();
const wifi = await si.wifiConnections();
const bluetooth = await si.bluetoothDevices();
ws.send({
battery: battery,
wifiConnections: wifi,
bluetoothDevices: bluetooth
}, true);
(ws.data as any).observer = si.observe(valuesObject, 1000 * 30, (data) =>
{
ws.send(data);
});
},
close (ws)
{
clearInterval((ws.data as any).observer);
}
})
.get('/info/battery', async () =>
{
return si.battery();