|
| 1 | +import { |
| 2 | + resolveManifestChromeAssetPaths, |
| 3 | + resolveRuntimeAssetUrl |
| 4 | +} from "/src/engine/runtime/gameImageConvention.js"; |
| 5 | + |
| 6 | +function normalizeText(value) { |
| 7 | + return typeof value === "string" ? value.trim() : ""; |
| 8 | +} |
| 9 | + |
| 10 | +function normalizePath(value) { |
| 11 | + return normalizeText(value).replace(/\\/g, "/"); |
| 12 | +} |
| 13 | + |
| 14 | +function hasProtocol(value) { |
| 15 | + return /^[a-z][a-z0-9+.-]*:/i.test(value); |
| 16 | +} |
| 17 | + |
| 18 | +function asArray(value) { |
| 19 | + return Array.isArray(value) ? value : []; |
| 20 | +} |
| 21 | + |
| 22 | +function documentRefOrNull(documentRef) { |
| 23 | + return documentRef || globalThis.document || null; |
| 24 | +} |
| 25 | + |
| 26 | +async function waitForDocumentReady(documentRef) { |
| 27 | + if (!documentRef || documentRef.readyState !== "loading") { |
| 28 | + return; |
| 29 | + } |
| 30 | + await new Promise((resolve) => { |
| 31 | + documentRef.addEventListener("DOMContentLoaded", resolve, { once: true }); |
| 32 | + }); |
| 33 | +} |
| 34 | + |
| 35 | +export function manifestPathFromGameHref(href) { |
| 36 | + const normalized = normalizePath(href); |
| 37 | + if (!normalized || hasProtocol(normalized) || normalized.includes("..")) { |
| 38 | + return ""; |
| 39 | + } |
| 40 | + const match = normalized.match(/^\/?games\/([^/]+)\//i); |
| 41 | + return match ? `/games/${match[1]}/game.manifest.json` : ""; |
| 42 | +} |
| 43 | + |
| 44 | +export function manifestPathForGame(game) { |
| 45 | + const explicitPath = normalizePath(game?.manifestPath || game?.gameManifestPath); |
| 46 | + if (explicitPath && !hasProtocol(explicitPath) && !explicitPath.startsWith("//") && !explicitPath.includes("..")) { |
| 47 | + return explicitPath.startsWith("/") ? explicitPath : `/${explicitPath}`; |
| 48 | + } |
| 49 | + return manifestPathFromGameHref(game?.href); |
| 50 | +} |
| 51 | + |
| 52 | +export async function resolveGameManifestPreview(options = {}) { |
| 53 | + const documentRef = documentRefOrNull(options.documentRef); |
| 54 | + const manifestPath = normalizePath(options.manifestPath); |
| 55 | + if (!manifestPath) { |
| 56 | + return { |
| 57 | + manifestPath: "", |
| 58 | + previewPath: "", |
| 59 | + previewUrl: "" |
| 60 | + }; |
| 61 | + } |
| 62 | + const resolved = await resolveManifestChromeAssetPaths({ |
| 63 | + gameId: options.gameId, |
| 64 | + manifestPath, |
| 65 | + manifestPayload: options.manifestPayload, |
| 66 | + documentRef |
| 67 | + }); |
| 68 | + const previewPath = normalizePath(resolved.previewPath); |
| 69 | + return { |
| 70 | + manifestPath: resolved.manifestPath || manifestPath, |
| 71 | + previewPath, |
| 72 | + previewUrl: previewPath ? resolveRuntimeAssetUrl(previewPath, documentRef) : "" |
| 73 | + }; |
| 74 | +} |
| 75 | + |
| 76 | +export async function resolveGamePreviewMap(games, options = {}) { |
| 77 | + const entries = await Promise.all(asArray(games).map(async (game) => { |
| 78 | + const gameId = normalizeText(game?.id); |
| 79 | + const manifestPath = manifestPathForGame(game); |
| 80 | + if (!gameId || !manifestPath) { |
| 81 | + return null; |
| 82 | + } |
| 83 | + const resolved = await resolveGameManifestPreview({ |
| 84 | + gameId, |
| 85 | + manifestPath, |
| 86 | + documentRef: options.documentRef |
| 87 | + }); |
| 88 | + return resolved.previewUrl ? [gameId, resolved.previewUrl] : null; |
| 89 | + })); |
| 90 | + return new Map(entries.filter(Boolean)); |
| 91 | +} |
| 92 | + |
| 93 | +export async function hydrateGameManifestPreviewImage(options = {}) { |
| 94 | + const documentRef = documentRefOrNull(options.documentRef); |
| 95 | + if (!documentRef) { |
| 96 | + return null; |
| 97 | + } |
| 98 | + await waitForDocumentReady(documentRef); |
| 99 | + const image = typeof options.imageSelector === "string" |
| 100 | + ? documentRef.querySelector(options.imageSelector) |
| 101 | + : options.image; |
| 102 | + const imageCtor = documentRef.defaultView?.HTMLImageElement || globalThis.HTMLImageElement; |
| 103 | + const isImage = imageCtor |
| 104 | + ? image instanceof imageCtor |
| 105 | + : image?.tagName === "IMG"; |
| 106 | + if (!isImage) { |
| 107 | + return null; |
| 108 | + } |
| 109 | + const placeholder = typeof options.placeholderSelector === "string" |
| 110 | + ? documentRef.querySelector(options.placeholderSelector) |
| 111 | + : options.placeholder; |
| 112 | + const gameId = normalizeText(options.gameId); |
| 113 | + const resolved = await resolveGameManifestPreview({ |
| 114 | + gameId, |
| 115 | + manifestPath: normalizePath(options.manifestPath) || (gameId ? `/games/${gameId}/game.manifest.json` : ""), |
| 116 | + documentRef |
| 117 | + }); |
| 118 | + if (resolved.previewUrl) { |
| 119 | + image.src = resolved.previewUrl; |
| 120 | + image.hidden = false; |
| 121 | + image.dataset.gamePreviewStatus = "ready"; |
| 122 | + placeholder?.setAttribute("hidden", ""); |
| 123 | + } else { |
| 124 | + image.removeAttribute("src"); |
| 125 | + image.hidden = true; |
| 126 | + image.dataset.gamePreviewStatus = "missing"; |
| 127 | + placeholder?.removeAttribute("hidden"); |
| 128 | + } |
| 129 | + return resolved; |
| 130 | +} |
0 commit comments