|
| 1 | +export function isRecord(value) { |
| 2 | + return Boolean(value) && typeof value === "object" && !Array.isArray(value); |
| 3 | +} |
| 4 | + |
| 5 | +function parseJson(rawValue, sourceLabel) { |
| 6 | + try { |
| 7 | + const parsed = JSON.parse(rawValue); |
| 8 | + return isRecord(parsed) |
| 9 | + ? { ok: true, manifest: parsed, sourceLabel } |
| 10 | + : { ok: false, message: `${sourceLabel} must contain a JSON object.` }; |
| 11 | + } catch (error) { |
| 12 | + return { ok: false, message: `${sourceLabel} contains invalid JSON: ${error.message}` }; |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +function numberValue(value, fallback = 0) { |
| 17 | + const parsed = Number(value); |
| 18 | + return Number.isNaN(parsed) || Math.abs(parsed) === Infinity ? fallback : parsed; |
| 19 | +} |
| 20 | + |
| 21 | +function positiveInteger(value) { |
| 22 | + const parsed = Math.floor(numberValue(value)); |
| 23 | + return parsed > 0 ? parsed : 0; |
| 24 | +} |
| 25 | + |
| 26 | +export function resolveManifestScreenDimensions(manifest) { |
| 27 | + const screen = isRecord(manifest?.screen) ? manifest.screen : null; |
| 28 | + const width = positiveInteger(screen?.width); |
| 29 | + const height = positiveInteger(screen?.height); |
| 30 | + if (!width || !height) { |
| 31 | + return { |
| 32 | + ok: false, |
| 33 | + message: "Manifest screen dimensions are required at root.screen.width and root.screen.height." |
| 34 | + }; |
| 35 | + } |
| 36 | + return { ok: true, width, height }; |
| 37 | +} |
| 38 | + |
| 39 | +export class GameManifestLoader { |
| 40 | + constructor({ |
| 41 | + fetchRef = null, |
| 42 | + pathParams = ["manifestPath", "gameManifestPath"], |
| 43 | + sessionStorageRef = null, |
| 44 | + windowRef = window |
| 45 | + } = {}) { |
| 46 | + this.fetch = fetchRef || windowRef.fetch?.bind(windowRef) || null; |
| 47 | + this.pathParams = pathParams; |
| 48 | + this.sessionStorage = sessionStorageRef || windowRef.sessionStorage || null; |
| 49 | + this.window = windowRef; |
| 50 | + } |
| 51 | + |
| 52 | + isWorkspaceLaunch(params = new URLSearchParams(this.window.location.search || "")) { |
| 53 | + return params.get("launch") === "workspace" |
| 54 | + && params.get("fromTool") === "workspace-manager-v2" |
| 55 | + && Boolean(params.get("hostContextId")); |
| 56 | + } |
| 57 | + |
| 58 | + async loadInitialManifest() { |
| 59 | + const params = new URLSearchParams(this.window.location.search || ""); |
| 60 | + if (this.isWorkspaceLaunch(params)) { |
| 61 | + return this.loadFromWorkspaceContext(params.get("hostContextId") || ""); |
| 62 | + } |
| 63 | + const manifestPath = this.pathParams.map((key) => params.get(key) || "").find(Boolean) || ""; |
| 64 | + if (manifestPath) { |
| 65 | + return this.loadFromPath(manifestPath, "URL manifest path"); |
| 66 | + } |
| 67 | + return { ok: false, skipped: true }; |
| 68 | + } |
| 69 | + |
| 70 | + loadFromWorkspaceContext(hostContextId) { |
| 71 | + if (!hostContextId) { |
| 72 | + return { ok: false, message: "Workspace launch did not include hostContextId." }; |
| 73 | + } |
| 74 | + const rawValue = this.sessionStorage?.getItem(hostContextId) || ""; |
| 75 | + if (!rawValue) { |
| 76 | + return { ok: false, message: `Workspace manifest context was not found in sessionStorage: ${hostContextId}.` }; |
| 77 | + } |
| 78 | + return parseJson(rawValue, `workspace:${hostContextId}`); |
| 79 | + } |
| 80 | + |
| 81 | + async loadFromPath(manifestPath, sourceLabel = manifestPath) { |
| 82 | + if (typeof this.fetch !== "function") { |
| 83 | + return { ok: false, message: "Fetch API is unavailable for manifest loading." }; |
| 84 | + } |
| 85 | + try { |
| 86 | + const response = await this.fetch(manifestPath, { cache: "no-store" }); |
| 87 | + if (!response.ok) { |
| 88 | + return { ok: false, message: `Manifest load failed from ${manifestPath}: ${response.status} ${response.statusText}` }; |
| 89 | + } |
| 90 | + const manifest = await response.json(); |
| 91 | + return isRecord(manifest) |
| 92 | + ? { ok: true, manifest, sourceLabel: sourceLabel || manifestPath } |
| 93 | + : { ok: false, message: `${sourceLabel || manifestPath} must contain a JSON object.` }; |
| 94 | + } catch (error) { |
| 95 | + return { ok: false, message: `Manifest load failed from ${manifestPath}: ${error.message}` }; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + async loadFromFile(file) { |
| 100 | + if (!file) { |
| 101 | + return { ok: false, skipped: true }; |
| 102 | + } |
| 103 | + try { |
| 104 | + return parseJson(await file.text(), file.name || "selected manifest file"); |
| 105 | + } catch (error) { |
| 106 | + return { ok: false, message: `Manifest file could not be read: ${error.message}` }; |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments