Game specialisation & delta versions (#323)

* feat: game specialisation, auto-guess extensions

* fix: enforce specialisation specific schema at API level

* fix: lint

* feat: partial work on depot endpoints

* feat: bump torrential

* feat: dummy version creation for depot uploads

* fix: lint

* fix: types

* fix: lint

* feat: depot version import

* fix: lint

* fix: remove any type

* fix: lint

* fix: push update interval

* fix: cpu usage calculation

* feat: delta version support

* feat: style tweaks for selectlaunch.vue

* fix: lint
This commit is contained in:
DecDuck
2026-01-23 05:04:38 +00:00
committed by GitHub
parent d8db5b5b85
commit 00adab21c2
46 changed files with 1164 additions and 347 deletions

View File

@@ -0,0 +1,11 @@
import aclManager from "~/server/internal/acls";
import prisma from "~/server/internal/db/database";
export default defineEventHandler(async (h3) => {
const allowed = await aclManager.allowSystemACL(h3, ["depot:read"]);
if (!allowed) throw createError({ statusCode: 403 });
const depots = await prisma.depot.findMany({});
return depots;
});

View File

@@ -6,7 +6,6 @@ import { castManifest } from "~/server/internal/library/manifest";
const AUTHORIZATION_HEADER_PREFIX = "Bearer ";
const Query = type({
game: "string",
version: "string",
});
@@ -31,10 +30,7 @@ export default defineEventHandler(async (h3) => {
const version = await prisma.gameVersion.findUnique({
where: {
gameId_versionId: {
gameId: query.game,
versionId: query.version,
},
versionId: query.version,
},
select: {
dropletManifest: true,

View File

@@ -11,6 +11,11 @@ export default defineEventHandler(async (h3) => {
select: {
versionId: true,
},
where: {
versionPath: {
not: null
}
}
},
},
});

View File

@@ -0,0 +1,51 @@
import { type } from "arktype";
import { readDropValidatedBody, throwingArktype } from "~/server/arktype";
import aclManager from "~/server/internal/acls";
import prisma from "~/server/internal/db/database";
const UploadManifest = type({
gameId: "string",
versionName: "string",
manifest: type({
version: "'2'",
size: "number",
key: "16 <= number[] <= 16",
chunks: type({
["string"]: {
checksum: "string",
iv: "16 <= number[] <= 16",
files: type({
filename: "string",
start: "number",
length: "number",
permissions: "number",
}).array(),
},
}),
}),
fileList: "string[]",
}).configure(throwingArktype);
export default defineEventHandler(async (h3) => {
const allowed = await aclManager.allowSystemACL(h3, ["depot:upload:new"]);
if (!allowed) throw createError({ statusCode: 403 });
const { gameId, versionName, manifest, fileList } =
await readDropValidatedBody(h3, UploadManifest);
const version = await prisma.unimportedGameVersion.create({
data: {
game: {
connect: {
id: gameId,
},
},
versionName,
manifest,
fileList,
},
});
return { id: version.id };
});

View File

@@ -1,6 +1,7 @@
import type { GameVersion, Prisma } from "~/prisma/client/client";
import aclManager from "~/server/internal/acls";
import prisma from "~/server/internal/db/database";
import type { UnimportedVersionInformation } from "~/server/internal/library";
import libraryManager from "~/server/internal/library";
async function getGameVersionSize<
@@ -59,7 +60,7 @@ export default defineEventHandler<
{ body: never },
Promise<{
game: AdminFetchGameType;
unimportedVersions: string[] | undefined;
unimportedVersions: UnimportedVersionInformation[] | undefined;
}>
>(async (h3) => {
const allowed = await aclManager.allowSystemACL(h3, ["game:read"]);

View File

@@ -1,4 +1,5 @@
import { type } from "arktype";
import { GameType } from "~/prisma/client/enums";
import { readDropValidatedBody, throwingArktype } from "~/server/arktype";
import aclManager from "~/server/internal/acls";
import libraryManager from "~/server/internal/library";
@@ -7,6 +8,7 @@ import metadataHandler from "~/server/internal/metadata";
const ImportGameBody = type({
library: "string",
path: "string",
type: type.valueOf(GameType),
["metadata?"]: {
id: "string",
sourceId: "string",
@@ -19,7 +21,7 @@ export default defineEventHandler<{ body: typeof ImportGameBody.infer }>(
const allowed = await aclManager.allowSystemACL(h3, ["import:game:new"]);
if (!allowed) throw createError({ statusCode: 403 });
const { library, path, metadata } = await readDropValidatedBody(
const { library, path, metadata, type } = await readDropValidatedBody(
h3,
ImportGameBody,
);
@@ -38,8 +40,8 @@ export default defineEventHandler<{ body: typeof ImportGameBody.infer }>(
});
const taskId = metadata
? await metadataHandler.createGame(metadata, library, path)
: await metadataHandler.createGameWithoutMetadata(library, path);
? await metadataHandler.createGame(metadata, library, path, type)
: await metadataHandler.createGameWithoutMetadata(library, path, type);
if (!taskId)
throw createError({

View File

@@ -16,7 +16,7 @@ export default defineEventHandler(async (h3) => {
const game = await prisma.game.findUnique({
where: { id: gameId },
select: { libraryId: true, libraryPath: true },
select: { libraryId: true, libraryPath: true, type: true },
});
if (!game || !game.libraryId)
throw createError({ statusCode: 404, statusMessage: "Game not found" });
@@ -28,5 +28,5 @@ export default defineEventHandler(async (h3) => {
if (!unimportedVersions)
throw createError({ statusCode: 400, statusMessage: "Invalid game ID" });
return unimportedVersions;
return { versions: unimportedVersions, type: game.type };
});

View File

@@ -7,7 +7,11 @@ import libraryManager from "~/server/internal/library";
export const ImportVersion = type({
id: "string",
version: "string",
version: type({
type: "'depot' | 'local'",
identifier: "string",
name: "string",
}),
displayName: "string?",
launches: type({
@@ -16,6 +20,7 @@ export const ImportVersion = type({
launch: "string",
umuId: "string?",
executorId: "string?",
suggestions: "string[]?",
}).array(),
setups: type({
@@ -25,6 +30,10 @@ export const ImportVersion = type({
onlySetup: "boolean = false",
delta: "boolean = false",
requiredContent: type("string")
.array()
.default(() => []),
}).configure(throwingArktype);
export default defineEventHandler(async (h3) => {
@@ -47,7 +56,7 @@ export default defineEventHandler(async (h3) => {
if (validOverlayVersions == 0)
throw createError({
statusCode: 400,
statusMessage: "Update mode requires a pre-existing version.",
statusMessage: `Update mode requires a pre-existing version for platform: ${platformObject.platform}`,
});
}
}

View File

@@ -1,35 +1,43 @@
import { ArkErrors, type } from "arktype";
import aclManager from "~/server/internal/acls";
import libraryManager from "~/server/internal/library";
const Query = type({
id: "string",
type: "'depot' | 'local'",
version: "string",
});
export default defineEventHandler(async (h3) => {
const allowed = await aclManager.allowSystemACL(h3, ["import:version:read"]);
if (!allowed) throw createError({ statusCode: 403 });
const query = await getQuery(h3);
const gameId = query.id?.toString();
const versionName = query.version?.toString();
if (!gameId || !versionName)
const query = Query(getQuery(h3));
if (query instanceof ArkErrors)
throw createError({
statusCode: 400,
statusMessage: "Missing id or version in request params",
message: query.summary,
});
try {
const preload = await libraryManager.fetchUnimportedVersionInformation(
gameId,
versionName,
query.id,
{
type: query.type,
identifier: query.version,
},
);
if (!preload)
throw createError({
statusCode: 400,
statusMessage: "Invalid game or version id/name",
message: "Invalid game or version id/name",
});
return preload;
} catch (e) {
throw createError({
statusCode: 500,
message: `Failed to fetch preload information for ${gameId}: ${e}`,
message: `Failed to fetch preload information for ${query.id}: ${e}`,
});
}
});

View File

@@ -1,14 +1,19 @@
import { ArkErrors, type } from "arktype";
import { GameType } from "~/prisma/client/enums";
import aclManager from "~/server/internal/acls";
import prisma from "~/server/internal/db/database";
import type { GameMetadataSearchResult } from "~/server/internal/metadata/types";
const Query = type({
q: "string",
type: type.valueOf(GameType).optional(),
});
export default defineEventHandler(async (h3) => {
const allowed = await aclManager.allowSystemACL(h3, ["game:read"]);
const allowed = await aclManager.allowSystemACL(h3, [
"game:read",
"depot:read",
]);
if (!allowed) throw createError({ statusCode: 403 });
const query = Query(getQuery(h3));
@@ -22,7 +27,7 @@ export default defineEventHandler(async (h3) => {
mShortDescription: string;
mReleased: string;
}[] =
await prisma.$queryRaw`SELECT id, "mName", "mIconObjectId", "mShortDescription", "mReleased" FROM "Game" WHERE SIMILARITY("mName", ${query.q}) > 0.2 ORDER BY SIMILARITY("mName", ${query.q}) DESC;`;
await prisma.$queryRaw`SELECT id, "mName", "mIconObjectId", "mShortDescription", "mReleased" FROM "Game" WHERE SIMILARITY("mName", ${query.q}) > 0.2 AND (${query.type || "undefined"} = 'undefined' OR type::text = ${query.type}) ORDER BY SIMILARITY("mName", ${query.q}) DESC;`;
const resultsMapped = results.map(
(v) =>

View File

@@ -13,15 +13,24 @@ export default defineClientEventHandler(async (h3) => {
const gameVersion = await prisma.gameVersion.findUnique({
where: {
gameId_versionId: {
gameId: id,
versionId: version,
},
versionId: version,
},
include: {
launches: {
include: {
executor: true,
executor: {
include: {
gameVersion: {
select: {
game: {
select: {
id: true,
},
},
},
},
},
},
},
},
setups: true,
@@ -34,8 +43,22 @@ export default defineClientEventHandler(async (h3) => {
statusMessage: "Game version not found",
});
return {
const gameVersionMapped = {
...gameVersion,
launches: gameVersion.launches.map((launch) => ({
...launch,
executor: launch.executor
? {
...launch.executor,
gameVersion: undefined,
gameId: launch.executor.gameVersion.game.id,
}
: undefined,
})),
};
return {
...gameVersionMapped,
size: libraryManager.getGameVersionSize(id, version),
};
});

View File

@@ -1,24 +1,15 @@
import { defineClientEventHandler } from "~/server/internal/clients/event-handler";
import prisma from "~/server/internal/db/database";
import { createDownloadManifestDetails } from "~/server/internal/library/manifest/index";
export default defineClientEventHandler(async (h3) => {
const query = getQuery(h3);
const id = query.id?.toString();
const version = query.version?.toString();
if (!id || !version)
if (!version)
throw createError({
statusCode: 400,
statusMessage: "Missing id or version in query",
statusMessage: "Missing version ID in query",
});
const manifest = await prisma.gameVersion.findUnique({
where: { gameId_versionId: { gameId: id, versionId: version } },
select: { dropletManifest: true },
});
if (!manifest)
throw createError({
statusCode: 400,
statusMessage: "Invalid game or version, or no versions added.",
});
return manifest.dropletManifest;
const result = await createDownloadManifestDetails(version);
return result;
});

View File

@@ -5,8 +5,8 @@ import gameSizeManager from "~/server/internal/gamesize";
type VersionDownloadOption = {
versionId: string;
displayName?: string;
versionPath: string;
displayName?: string | undefined;
versionPath?: string | undefined;
platform: Platform;
size: number;
requiredContent: Array<{
@@ -106,7 +106,8 @@ export default defineClientEventHandler(async (h3) => {
([platform, requiredContent]) =>
({
versionId: v.versionId,
versionPath: v.versionPath,
displayName: v.displayName || undefined,
versionPath: v.versionPath || undefined,
platform,
requiredContent,
size: size!,

View File

@@ -1,5 +1,6 @@
import { ArkErrors, type } from "arktype";
import type { Prisma } from "~/prisma/client/client";
import { GameType } from "~/prisma/client/enums";
import aclManager from "~/server/internal/acls";
import prisma from "~/server/internal/db/database";
import { parsePlatform } from "~/server/internal/utils/parseplatform";
@@ -100,6 +101,7 @@ export default defineEventHandler(async (h3) => {
...tagFilter,
...platformFilter,
...companyFilter,
type: GameType.Game,
};
const sort: Prisma.GameOrderByWithRelationInput = {};

View File

@@ -108,8 +108,11 @@ export const systemACLDescriptions: ObjectFromList<typeof systemACLs> = {
"settings:update": "Update system settings.",
"depot:read": "Read depot information, and search for games",
"depot:new": "Create a new download depot",
"depot:delete": "Remove a download depot",
"depot:upload:new": "Upload a new version to a depot",
"depot:upload:delete": "Remove a depot version",
"system-data:listen":
"Connect to a websocket to receive system data updates.",

View File

@@ -47,8 +47,11 @@ export type UserACL = Array<(typeof userACLs)[number]>;
export const systemACLs = [
"setup",
"depot:read",
"depot:new",
"depot:delete",
"depot:upload:new",
"depot:upload:delete",
"auth:read",
"auth:simple:invitation:read",

View File

@@ -69,7 +69,7 @@ class GameSizeManager {
}
const { dropletManifest } = (await prisma.gameVersion.findUnique({
where: { gameId_versionId: { versionId, gameId } },
where: { versionId },
}))!;
return castManifest(dropletManifest).size;

View File

@@ -18,6 +18,8 @@ import type { WorkingLibrarySource } from "~/server/api/v1/admin/library/sources
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";
import { GameType, type Platform } from "~/prisma/client/enums";
import { castManifest } from "./manifest";
export function createGameImportTaskId(libraryId: string, libraryPath: string) {
return createHash("md5")
@@ -34,6 +36,30 @@ export function createVersionImportTaskKey(
.digest("hex");
}
export interface ExecutorVersionGuess {
type: "executor";
executorId: string;
icon: string;
gameName: string;
versionName: string;
launchName: string;
platform: Platform;
}
export interface PlatformVersionGuess {
platform: Platform;
type: "platform";
}
export type VersionGuess = {
filename: string;
match: number;
} & (PlatformVersionGuess | ExecutorVersionGuess);
export interface UnimportedVersionInformation {
type: "local" | "depot";
name: string;
identifier: string;
}
class LibraryManager {
private libraries: Map<string, LibraryProvider<unknown>> = new Map();
@@ -95,7 +121,10 @@ class LibraryManager {
return unimportedGames;
}
async fetchUnimportedGameVersions(libraryId: string, libraryPath: string) {
async fetchUnimportedGameVersions(
libraryId: string,
libraryPath: string,
): Promise<UnimportedVersionInformation[] | undefined> {
const provider = this.libraries.get(libraryId);
if (!provider) return undefined;
const game = await prisma.game.findUnique({
@@ -115,14 +144,40 @@ class LibraryManager {
try {
const versions = await provider.listVersions(
libraryPath,
game.versions.map((v) => v.versionPath),
game.versions.map((v) => v.versionPath).filter((v) => v !== null),
);
const unimportedVersions = versions.filter(
(e) =>
game.versions.findIndex((v) => v.versionPath == e) == -1 &&
!taskHandler.hasTaskKey(createVersionImportTaskKey(game.id, e)),
const unimportedVersions = versions
.filter(
(e) =>
game.versions.findIndex((v) => v.versionPath == e) == -1 &&
!taskHandler.hasTaskKey(createVersionImportTaskKey(game.id, e)),
)
.map(
(v) =>
({
type: "local",
name: v,
identifier: v,
}) satisfies UnimportedVersionInformation,
);
const depotVersions = await prisma.unimportedGameVersion.findMany({
where: {
gameId: game.id,
},
select: {
versionName: true,
id: true,
},
});
const mappedDepotVersions = depotVersions.map(
(v) =>
({
type: "depot",
name: v.versionName,
identifier: v.id,
}) satisfies UnimportedVersionInformation,
);
return unimportedVersions;
return [...unimportedVersions, ...mappedDepotVersions];
} catch (e) {
if (e instanceof GameNotFoundError) {
logger.warn(e);
@@ -165,10 +220,13 @@ class LibraryManager {
/**
* Fetches recommendations and extra data about the version. Doesn't actually check if it's been imported.
* @param gameId
* @param versionName
* @param versionIdentifier
* @returns
*/
async fetchUnimportedVersionInformation(gameId: string, versionName: string) {
async fetchUnimportedVersionInformation(
gameId: string,
versionIdentifier: Omit<UnimportedVersionInformation, "name">,
) {
const game = await prisma.game.findUnique({
where: { id: gameId },
select: { libraryPath: true, libraryId: true, mName: true },
@@ -178,7 +236,7 @@ class LibraryManager {
const library = this.libraries.get(game.libraryId);
if (!library) return undefined;
const fileExts: { [key: string]: string[] } = {
const fileExts: { [key in Platform]: string[] } = {
Linux: [
// Ext for Unity games
".x86_64",
@@ -196,13 +254,60 @@ class LibraryManager {
],
};
const options: Array<{
filename: string;
platform: string;
match: number;
}> = [];
const executorSuggestions = await prisma.launchConfiguration.findMany({
where: {
executorSuggestions: {
isEmpty: false,
},
gameVersion: {
game: {
type: GameType.Executor,
},
},
},
select: {
executorSuggestions: true,
gameVersion: {
select: {
game: {
select: {
mIconObjectId: true,
mName: true,
},
},
displayName: true,
versionPath: true,
},
},
name: true,
launchId: true,
platform: true,
},
});
const options: Array<VersionGuess> = [];
let files;
if (versionIdentifier.type === "local") {
files = await library.versionReaddir(
game.libraryPath,
versionIdentifier.identifier,
);
} else if (versionIdentifier.type === "depot") {
const unimported = await prisma.unimportedGameVersion.findUnique({
where: {
id: versionIdentifier.identifier,
},
select: {
fileList: true,
},
});
if (!unimported) return undefined;
files = unimported.fileList;
} else {
return undefined;
}
const files = await library.versionReaddir(game.libraryPath, versionName);
for (const filename of files) {
const basename = path.basename(filename);
const dotLocation = filename.lastIndexOf(".");
@@ -213,12 +318,32 @@ class LibraryManager {
if (checkExt != ext) continue;
const fuzzyValue = fuzzy(basename, game.mName);
options.push({
type: "platform",
filename: filename.replaceAll(" ", "\\ "),
platform,
platform: platform as Platform,
match: fuzzyValue,
});
}
}
for (const executorSuggestion of executorSuggestions) {
for (const suggestion of executorSuggestion.executorSuggestions) {
if (suggestion != ext) continue;
const fuzzyValue = fuzzy(basename, game.mName);
options.push({
type: "executor",
filename: filename.replaceAll(" ", "\\ "),
match: fuzzyValue,
executorId: executorSuggestion.launchId,
icon: executorSuggestion.gameVersion.game.mIconObjectId,
gameName: executorSuggestion.gameVersion.game.mName,
versionName: (executorSuggestion.gameVersion.displayName ??
executorSuggestion.gameVersion.versionPath)!,
launchName: executorSuggestion.name,
platform: executorSuggestion.platform,
});
}
}
}
const sortedOptions = options.sort((a, b) => b.match - a.match);
@@ -247,49 +372,79 @@ class LibraryManager {
async importVersion(
gameId: string,
versionPath: string,
version: UnimportedVersionInformation,
metadata: typeof ImportVersion.infer,
) {
const taskKey = createVersionImportTaskKey(gameId, versionPath);
const taskKey = createVersionImportTaskKey(gameId, version.identifier);
const game = await prisma.game.findUnique({
where: { id: gameId },
select: { mName: true, libraryId: true, libraryPath: true },
select: { mName: true, libraryId: true, libraryPath: true, type: true },
});
if (!game || !game.libraryId) return undefined;
if (game.type === GameType.Redist && !metadata.onlySetup)
throw createError({
statusCode: 400,
message: "Redistributables can only be in setup-only mode.",
});
const library = this.libraries.get(game.libraryId);
if (!library) return undefined;
const unimportedVersion =
version.type === "depot"
? await prisma.unimportedGameVersion.findUnique({
where: { id: version.identifier },
})
: undefined;
return await taskHandler.create({
key: taskKey,
taskGroup: "import:game",
name: `Importing version ${versionPath} for ${game.mName}`,
name: `Importing version ${version.name} 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);
},
);
let versionPath: string | null = null;
let manifest;
let fileList;
logger.info("Created manifest successfully!");
if (version.type === "local") {
versionPath = version.identifier;
// First, create the manifest via droplet.
// This takes up 90% of our progress, so we wrap it in a *0.9
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);
},
);
fileList = await library.versionReaddir(
game.libraryPath,
versionPath,
);
logger.info("Created manifest successfully!");
} else if (version.type === "depot" && unimportedVersion) {
manifest = castManifest(unimportedVersion.manifest);
fileList = unimportedVersion.fileList;
progress(90);
} else {
throw "Could not find or create manifest for this version.";
}
const currentIndex = await prisma.gameVersion.count({
where: { gameId: gameId },
});
// Then, create the database object
await prisma.gameVersion.create({
const newVersion = await prisma.gameVersion.create({
data: {
game: {
connect: {
@@ -301,6 +456,7 @@ class LibraryManager {
versionPath,
dropletManifest: manifest,
fileList,
versionIndex: currentIndex,
delta: metadata.delta,
@@ -321,9 +477,13 @@ class LibraryManager {
name: v.name,
command: v.launch,
platform: v.platform,
...(v.executorId
? { executorId: v.executorId }
...(v.executorId && game.type === "Game"
? {
executorId: v.executorId,
}
: undefined),
executorSuggestions:
game.type === "Executor" ? (v.suggestions ?? []) : [],
})),
}
: { data: [] },
@@ -333,17 +493,30 @@ class LibraryManager {
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}.`,
nonce: `version-create-${gameId}-${version}`,
title: `'${game.mName}' ('${version}') finished importing.`,
description: `Drop finished importing version ${version} for ${game.mName}.`,
actions: [`View|/admin/library/${gameId}`],
acls: ["system:import:version:read"],
});
await libraryManager.cacheCombinedGameSize(gameId);
await libraryManager.cacheGameVersionSize(gameId, versionPath);
await libraryManager.cacheGameVersionSize(gameId, newVersion.versionId);
await TORRENTIAL_SERVICE.utils().invalidate(gameId, versionPath);
await TORRENTIAL_SERVICE.utils().invalidate(
gameId,
newVersion.versionId,
);
if (version.type === "depot") {
// SAFETY: we can only reach this if the type is depot and identifier is valid
// eslint-disable-next-line drop/no-prisma-delete
await prisma.unimportedGameVersion.delete({
where: {
id: version.identifier,
},
});
}
progress(100);
},
});
@@ -390,6 +563,20 @@ class LibraryManager {
},
});
await gameSizeManager.deleteGame(gameId);
// Delete all game versions that depended on this game
await prisma.gameVersion.deleteMany({
where: {
launches: {
some: {
executor: {
gameVersion: {
gameId,
},
},
},
},
},
});
}
async getGameVersionSize(
@@ -421,7 +608,7 @@ class LibraryManager {
await gameSizeManager.cacheCombinedGame(game);
}
async cacheGameVersionSize(gameId: string, versionName: string) {
async cacheGameVersionSize(gameId: string, versionId: string) {
const game = await prisma.game.findFirst({
where: { id: gameId },
include: { versions: true },
@@ -429,7 +616,7 @@ class LibraryManager {
if (!game) {
return;
}
await gameSizeManager.cacheGameVersion(game, versionName);
await gameSizeManager.cacheGameVersion(game, versionId);
}
}

View File

@@ -1,12 +1,12 @@
import type { JsonValue } from "@prisma/client/runtime/library";
export type Manifest = V2Manifest;
export type DropletManifest = V2Manifest;
export type V2Manifest = {
version: "2";
size: number;
key: number[];
chunks: { [key: string]: V2ChunkData[] };
chunks: { [key: string]: V2ChunkData };
};
export type V2ChunkData = {
@@ -22,6 +22,6 @@ export type V2FileEntry = {
permissions: number;
};
export function castManifest(manifest: JsonValue): Manifest {
return JSON.parse(manifest as string) as Manifest;
export function castManifest(manifest: JsonValue): DropletManifest {
return JSON.parse(manifest as string) as DropletManifest;
}

View File

@@ -0,0 +1,100 @@
import prisma from "../../db/database";
import { castManifest, type DropletManifest } from "../manifest";
export type DownloadManifestDetails = {
manifests: { [key: string]: DropletManifest };
fileList: { [key: string]: string };
};
function convertMap<T>(map: Map<string, T>): { [key: string]: T } {
return Object.fromEntries(map.entries().toArray());
}
/**
*
* @param gameId Game ID
* @param versionId Version ID
*/
export async function createDownloadManifestDetails(
versionId: string,
): Promise<DownloadManifestDetails> {
const mainVersion = await prisma.gameVersion.findUnique({
where: { versionId },
select: {
versionId: true,
delta: true,
versionIndex: true,
fileList: true,
negativeFileList: true,
gameId: true,
dropletManifest: true,
},
});
if (!mainVersion)
throw createError({ statusCode: 404, message: "Version not found" });
const collectedVersions = [];
let versionIndex = mainVersion.versionIndex;
while (true) {
const nextVersion = await prisma.gameVersion.findFirst({
where: { gameId: mainVersion.gameId, versionIndex: { lt: versionIndex } },
orderBy: {
versionIndex: "desc",
},
select: {
versionId: true,
versionIndex: true,
delta: true,
fileList: true,
negativeFileList: true,
dropletManifest: true,
},
});
if (!nextVersion)
throw createError({
statusCode: 500,
message: "Delta version without version underneath it.",
});
versionIndex = nextVersion.versionIndex;
collectedVersions.push(nextVersion);
if (!nextVersion.delta) break;
}
collectedVersions.reverse();
// Apply fileList in lowest priority to newest priority
const versionOrder = [...collectedVersions, mainVersion];
const fileList = new Map<string, string>();
for (const version of versionOrder) {
for (const file of version.fileList) {
fileList.set(file, version.versionId);
}
for (const negFile of version.negativeFileList) {
fileList.delete(negFile);
}
}
// Now that we have our file list, filter the manifests
const manifests = new Map<string, DropletManifest>();
for (const version of versionOrder) {
const files = fileList
.entries()
.filter(([, versionId]) => version.versionId === versionId)
.toArray();
if (files.length == 0) continue;
const fileNames = Object.fromEntries(files);
const manifest = castManifest(version.dropletManifest);
const filteredChunks = Object.fromEntries(
Object.entries(manifest.chunks).filter(([, chunkData]) =>
chunkData.files.some((fileEntry) => !!fileNames[fileEntry.filename]),
),
);
manifests.set(version.versionId, {
...manifest,
chunks: filteredChunks,
});
}
return { fileList: convertMap(fileList), manifests: convertMap(manifests) };
}

View File

@@ -1,4 +1,5 @@
import type { Prisma } from "~/prisma/client/client";
import type { GameType } from "~/prisma/client/enums";
import { MetadataSource } from "~/prisma/client/enums";
import prisma from "../db/database";
import type {
@@ -118,7 +119,11 @@ export class MetadataHandler {
return successfulResults;
}
async createGameWithoutMetadata(libraryId: string, libraryPath: string) {
async createGameWithoutMetadata(
libraryId: string,
libraryPath: string,
type: GameType,
) {
return await this.createGame(
{
id: "",
@@ -127,6 +132,7 @@ export class MetadataHandler {
},
libraryId,
libraryPath,
type,
);
}
@@ -174,6 +180,7 @@ export class MetadataHandler {
result: { sourceId: string; id: string; name: string },
libraryId: string,
libraryPath: string,
type: GameType,
) {
const provider = this.providers.get(result.sourceId);
if (!provider)
@@ -286,6 +293,8 @@ export class MetadataHandler {
libraryId,
libraryPath,
type,
},
});

View File

@@ -7,6 +7,33 @@ export type SystemData = {
cpuCores: number;
};
// See https://github.com/oscmejia/os-utils/blob/master/lib/osutils.js
function getCPUInfo() {
const cpus = os.cpus();
let user = 0;
let nice = 0;
let sys = 0;
let idle = 0;
let irq = 0;
for (const cpu in cpus) {
if (!Object.prototype.hasOwnProperty.call(cpus, cpu)) continue;
user += cpus[cpu].times.user;
nice += cpus[cpu].times.nice;
sys += cpus[cpu].times.sys;
irq += cpus[cpu].times.irq;
idle += cpus[cpu].times.idle;
}
const total = user + nice + sys + idle + irq;
return {
idle: idle,
total: total,
};
}
class SystemManager {
// userId to acl to listenerId
private listeners = new Map<
@@ -14,6 +41,20 @@ class SystemManager {
Map<string, { callback: (systemData: SystemData) => void }>
>();
private lastCPUUpdate: { idle: number; total: number } | undefined;
constructor() {
setInterval(() => {
const systemData = this.getSystemData();
if (!systemData) return;
for (const [, map] of this.listeners.entries()) {
for (const [, { callback }] of map.entries()) {
callback(systemData);
}
}
}, 3000);
}
listen(
userId: string,
id: string,
@@ -22,25 +63,17 @@ class SystemManager {
if (!this.listeners.has(userId)) this.listeners.set(userId, new Map());
// eslint-disable-next-line @typescript-eslint/no-extra-non-null-assertion
this.listeners.get(userId)!!.set(id, { callback });
this.pushUpdate(userId, id);
setInterval(() => this.pushUpdate(userId, id), 3000);
}
unlisten(userId: string, id: string) {
this.listeners.get(userId)?.delete(id);
}
private async pushUpdate(userId: string, id: string) {
const listener = this.listeners.get(userId)?.get(id);
if (!listener) {
throw new Error("Failed to catch-up listener: callback does not exist");
}
listener.callback(this.getSystemData());
}
getSystemData(): SystemData {
getSystemData(): SystemData | undefined {
const cpu = this.cpuLoad();
if (!cpu) return undefined;
return {
cpuLoad: this.cpuLoad(),
cpuLoad: cpu * 100,
totalRam: os.totalmem(),
freeRam: os.freemem(),
cpuCores: os.cpus().length,
@@ -48,9 +81,15 @@ class SystemManager {
}
private cpuLoad() {
const [oneMinLoad, _fiveMinLoad, _fiftenMinLoad] = os.loadavg();
const numberCpus = os.cpus().length;
return 100 - ((numberCpus - oneMinLoad) / numberCpus) * 100;
const last = this.lastCPUUpdate;
this.lastCPUUpdate = getCPUInfo();
if (!last) return undefined;
const idle = this.lastCPUUpdate.idle - last.idle;
const total = this.lastCPUUpdate.total - last.total;
const perc = idle / total;
return 1 - perc;
}
}