Files
drop/server/internal/library/index.ts
T
DecDuck 63ac2b8ffc Depot API & v4 (#298)
* feat: nginx + torrential basics & services system

* fix: lint + i18n

* fix: update torrential to remove openssl

* feat: add torrential to Docker build

* feat: move to self hosted runner

* fix: move off self-hosted runner

* fix: update nginx.conf

* feat: torrential cache invalidation

* fix: update torrential for cache invalidation

* feat: integrity check task

* fix: lint

* feat: move to version ids

* fix: client fixes and client-side checks

* feat: new depot apis and version id fixes

* feat: update torrential

* feat: droplet bump and remove unsafe update functions

* fix: lint

* feat: v4 featureset: emulators, multi-launch commands

* fix: lint

* fix: mobile ui for game editor

* feat: launch options

* fix: lint

* fix: remove axios, use $fetch

* feat: metadata and task api improvements

* feat: task actions

* fix: slight styling issue

* feat: fix style and lints

* feat: totp backend routes

* feat: oidc groups

* fix: update drop-base

* feat: creation of passkeys & totp

* feat: totp signin

* feat: webauthn mfa/signin

* feat: launch selecting ui

* fix: manually running tasks

* feat: update add company game modal to use new SelectorGame

* feat: executor selector

* fix(docker): update rust to rust nightly for torrential build (#305)

* feat: new version ui

* feat: move package lookup to build time to allow for deno dev

* fix: lint

* feat: localisation cleanup

* feat: apply localisation cleanup

* feat: potential i18n refactor logic

* feat: remove args from commands

* fix: lint

* fix: lockfile

---------

Co-authored-by: Aden Lindsay <140392385+AdenMGB@users.noreply.github.com>
2026-01-13 15:32:39 +11:00

438 lines
12 KiB
TypeScript

/**
* The Library Manager keeps track of games in Drop's library and their various states.
* It uses path relative to the library, so it can moved without issue
*
* It also provides the endpoints with information about unmatched games
*/
import path from "path";
import prisma from "../db/database";
import { fuzzy } from "fast-fuzzy";
import taskHandler from "../tasks";
import notificationSystem from "../notifications";
import { GameNotFoundError, type LibraryProvider } from "./provider";
import { logger } from "../logging";
import type { GameModel } from "~/prisma/client/models";
import { createHash } from "node:crypto";
import type { WorkingLibrarySource } from "~/server/api/v1/admin/library/sources/index.get";
import gameSizeManager from "~/server/internal/gamesize";
import { TORRENTIAL_SERVICE } from "../services/services/torrential";
import type { ImportVersion } from "~/server/api/v1/admin/import/version/index.post";
export function createGameImportTaskId(libraryId: string, libraryPath: string) {
return createHash("md5")
.update(`import:${libraryId}:${libraryPath}`)
.digest("hex");
}
export function createVersionImportTaskKey(
gameId: string,
versionName: string,
) {
return createHash("md5")
.update(`import:${gameId}:${versionName}`)
.digest("hex");
}
class LibraryManager {
private libraries: Map<string, LibraryProvider<unknown>> = new Map();
addLibrary(library: LibraryProvider<unknown>) {
this.libraries.set(library.id(), library);
}
removeLibrary(id: string) {
this.libraries.delete(id);
}
getLibrary(libraryId: string): LibraryProvider<unknown> | undefined {
return this.libraries.get(libraryId);
}
async fetchLibraries(): Promise<WorkingLibrarySource[]> {
const libraries = await prisma.library.findMany({});
const libraryWithMetadata = libraries.map(async (library) => {
const theLibrary = this.libraries.get(library.id);
const working = this.libraries.has(library.id);
return {
...library,
working,
fsStats: working ? theLibrary?.fsStats() : undefined,
};
});
return await Promise.all(libraryWithMetadata);
}
async fetchGamesByLibrary() {
const results: { [key: string]: { [key: string]: GameModel } } = {};
const games = await prisma.game.findMany({});
for (const game of games) {
const libraryId = game.libraryId!;
const libraryPath = game.libraryPath!;
results[libraryId] ??= {};
results[libraryId][libraryPath] = game;
}
return results;
}
async fetchUnimportedGames() {
const unimportedGames: { [key: string]: string[] } = {};
const instanceGames = await this.fetchGamesByLibrary();
for (const [id, library] of this.libraries.entries()) {
const providerGames = await library.listGames();
const providerUnimportedGames = providerGames.filter(
(libraryPath) =>
!instanceGames[id]?.[libraryPath] &&
!taskHandler.hasTaskKey(createGameImportTaskId(id, libraryPath)),
);
unimportedGames[id] = providerUnimportedGames;
}
return unimportedGames;
}
async fetchUnimportedGameVersions(libraryId: string, libraryPath: string) {
const provider = this.libraries.get(libraryId);
if (!provider) return undefined;
const game = await prisma.game.findUnique({
where: {
libraryKey: {
libraryId,
libraryPath,
},
},
select: {
id: true,
versions: true,
},
});
if (!game) return undefined;
try {
const versions = await provider.listVersions(
libraryPath,
game.versions.map((v) => v.versionPath),
);
const unimportedVersions = versions.filter(
(e) =>
game.versions.findIndex((v) => v.versionPath == e) == -1 &&
!taskHandler.hasTaskKey(createVersionImportTaskKey(game.id, e)),
);
return unimportedVersions;
} catch (e) {
if (e instanceof GameNotFoundError) {
logger.warn(e);
return undefined;
}
throw e;
}
}
async fetchGamesWithStatus() {
const games = await prisma.game.findMany({
include: {
library: true,
versions: true,
},
orderBy: {
mName: "asc",
},
});
return await Promise.all(
games.map(async (e) => {
const versions = await this.fetchUnimportedGameVersions(
e.libraryId ?? "",
e.libraryPath,
);
return {
game: e,
status: versions
? {
noVersions: e.versions.length == 0,
unimportedVersions: versions,
}
: ("offline" as const),
};
}),
);
}
/**
* Fetches recommendations and extra data about the version. Doesn't actually check if it's been imported.
* @param gameId
* @param versionName
* @returns
*/
async fetchUnimportedVersionInformation(gameId: string, versionName: string) {
const game = await prisma.game.findUnique({
where: { id: gameId },
select: { libraryPath: true, libraryId: true, mName: true },
});
if (!game || !game.libraryId) return undefined;
const library = this.libraries.get(game.libraryId);
if (!library) return undefined;
const fileExts: { [key: string]: string[] } = {
Linux: [
// Ext for Unity games
".x86_64",
// Shell scripts
".sh",
// No extension is common for Linux binaries
"",
// AppImages
".appimage",
],
Windows: [".exe", ".bat"],
macOS: [
// App files
".app",
],
};
const options: Array<{
filename: string;
platform: string;
match: number;
}> = [];
const files = await library.versionReaddir(game.libraryPath, versionName);
for (const filename of files) {
const basename = path.basename(filename);
const dotLocation = filename.lastIndexOf(".");
const ext =
dotLocation == -1 ? "" : filename.slice(dotLocation).toLowerCase();
for (const [platform, checkExts] of Object.entries(fileExts)) {
for (const checkExt of checkExts) {
if (checkExt != ext) continue;
const fuzzyValue = fuzzy(basename, game.mName);
options.push({
filename: filename.replaceAll(" ", "\\ "),
platform,
match: fuzzyValue,
});
}
}
}
const sortedOptions = options.sort((a, b) => b.match - a.match);
return sortedOptions;
}
// Checks are done in least to most expensive order
async checkUnimportedGamePath(libraryId: string, libraryPath: string) {
const hasGame =
(await prisma.game.count({
where: { libraryId, libraryPath },
})) > 0;
if (hasGame) return false;
return true;
}
/*
Game creation happens in metadata, because it's primarily a metadata object
async createGame(libraryId: string, libraryPath: string, game: Omit<Game, "libraryId" | "libraryPath">) {
}
*/
async importVersion(
gameId: string,
versionPath: string,
metadata: typeof ImportVersion.infer,
) {
const taskKey = createVersionImportTaskKey(gameId, versionPath);
const game = await prisma.game.findUnique({
where: { id: gameId },
select: { mName: true, libraryId: true, libraryPath: true },
});
if (!game || !game.libraryId) return undefined;
const library = this.libraries.get(game.libraryId);
if (!library) return undefined;
return await taskHandler.create({
key: taskKey,
taskGroup: "import:game",
name: `Importing version ${versionPath} for ${game.mName}`,
acls: ["system:import:version:read"],
async run({ progress, logger }) {
// First, create the manifest via droplet.
// This takes up 90% of our progress, so we wrap it in a *0.9
const manifest = await library.generateDropletManifest(
game.libraryPath,
versionPath,
(err, value) => {
if (err) throw err;
progress(value * 0.9);
},
(err, value) => {
if (err) throw err;
logger.info(value);
},
);
logger.info("Created manifest successfully!");
const currentIndex = await prisma.gameVersion.count({
where: { gameId: gameId },
});
// Then, create the database object
await prisma.gameVersion.create({
data: {
game: {
connect: {
id: gameId,
},
},
displayName: metadata.displayName ?? null,
versionPath,
dropletManifest: manifest,
versionIndex: currentIndex,
delta: metadata.delta,
onlySetup: metadata.onlySetup,
setups: {
createMany: {
data: metadata.setups.map((v) => ({
command: v.launch,
platform: v.platform,
})),
},
},
launches: {
createMany: !metadata.onlySetup
? {
data: metadata.launches.map((v) => ({
name: v.name,
command: v.launch,
platform: v.platform,
...(v.executorId
? { executorId: v.executorId }
: undefined),
})),
}
: { data: [] },
},
},
});
logger.info("Successfully created version!");
notificationSystem.systemPush({
nonce: `version-create-${gameId}-${versionPath}`,
title: `'${game.mName}' ('${versionPath}') finished importing.`,
description: `Drop finished importing version ${versionPath} for ${game.mName}.`,
actions: [`View|/admin/library/${gameId}`],
acls: ["system:import:version:read"],
});
await libraryManager.cacheCombinedGameSize(gameId);
await libraryManager.cacheGameVersionSize(gameId, versionPath);
await TORRENTIAL_SERVICE.utils().invalidate(gameId, versionPath);
progress(100);
},
});
}
async peekFile(
libraryId: string,
game: string,
version: string,
filename: string,
) {
const library = this.libraries.get(libraryId);
if (!library) return undefined;
return await library.peekFile(game, version, filename);
}
async readFile(
libraryId: string,
game: string,
version: string,
filename: string,
options?: { start?: number; end?: number },
) {
const library = this.libraries.get(libraryId);
if (!library) return undefined;
return await library.readFile(game, version, filename, options);
}
async deleteGameVersion(gameId: string, version: string) {
await prisma.gameVersion.deleteMany({
where: {
gameId: gameId,
versionId: version,
},
});
await gameSizeManager.deleteGameVersion(gameId, version);
}
async deleteGame(gameId: string) {
await prisma.game.deleteMany({
where: {
id: gameId,
},
});
await gameSizeManager.deleteGame(gameId);
}
async getGameVersionSize(
gameId: string,
versionName?: string,
): Promise<number | null> {
return gameSizeManager.getGameVersionSize(gameId, versionName);
}
async getBiggestGamesCombinedVersions(top: number) {
if (await gameSizeManager.isGameSizesCacheEmpty()) {
await gameSizeManager.cacheAllCombinedGames();
}
return gameSizeManager.getBiggestGamesAllVersions(top);
}
async getBiggestGamesLatestVersions(top: number) {
if (await gameSizeManager.isGameVersionsSizesCacheEmpty()) {
await gameSizeManager.cacheAllGameVersions();
}
return gameSizeManager.getBiggestGamesLatestVersion(top);
}
async cacheCombinedGameSize(gameId: string) {
const game = await prisma.game.findFirst({ where: { id: gameId } });
if (!game) {
return;
}
await gameSizeManager.cacheCombinedGame(game);
}
async cacheGameVersionSize(gameId: string, versionName: string) {
const game = await prisma.game.findFirst({
where: { id: gameId },
include: { versions: true },
});
if (!game) {
return;
}
await gameSizeManager.cacheGameVersion(game, versionName);
}
}
export const libraryManager = new LibraryManager();
export default libraryManager;