|
1 | 1 | import { getToolById, getToolRegistry } from "../toolRegistry.js"; |
2 | 2 | import { |
| 3 | + clearSharedAssetHandoff, |
| 4 | + clearSharedPaletteHandoff, |
3 | 5 | getSharedShellActions, |
4 | 6 | readSharedAssetHandoff, |
5 | 7 | readSharedPaletteHandoff, |
@@ -27,6 +29,7 @@ const TOOLS_PLATFORM_LOGGER = new Logger({ channel: "tools.platform", level: "de |
27 | 29 | const TOOLS_PLATFORM_BOOT_MS = Date.now(); |
28 | 30 | const GAME_ASSET_CATALOG_SCHEMA = "html-js-gaming.game-asset-catalog"; |
29 | 31 | const GAME_ASSET_CATALOG_VERSION = 1; |
| 32 | +const WORKSPACE_LAUNCH_SIGNATURE_STORAGE_KEY = "toolboxaid.toolsPlatform.launchSignature"; |
30 | 33 |
|
31 | 34 | function getPageMode() { |
32 | 35 | return document.body.dataset.toolsPlatformPage || "tool"; |
@@ -99,12 +102,76 @@ function buildWorkspaceHrefFromGameId(gameId) { |
99 | 102 | : ""; |
100 | 103 | } |
101 | 104 |
|
| 105 | +function normalizeSamplePresetPath(value) { |
| 106 | + return normalizeLocalHref(value, ["/samples/", "/games/"]); |
| 107 | +} |
| 108 | + |
| 109 | +function readLaunchPayloadSignature(searchParams) { |
| 110 | + if (!(searchParams instanceof URLSearchParams)) { |
| 111 | + return ""; |
| 112 | + } |
| 113 | + const gameId = normalizeTextValue(searchParams.get("gameId") || searchParams.get("game")); |
| 114 | + const gameHref = normalizeLocalHref(searchParams.get("gameHref"), ["/games/"]); |
| 115 | + const samplePresetPath = normalizeSamplePresetPath(searchParams.get("samplePresetPath")); |
| 116 | + const parts = []; |
| 117 | + if (gameId) { |
| 118 | + parts.push(`gameId=${gameId.toLowerCase()}`); |
| 119 | + } |
| 120 | + if (gameHref) { |
| 121 | + parts.push(`gameHref=${gameHref.toLowerCase()}`); |
| 122 | + } |
| 123 | + if (samplePresetPath) { |
| 124 | + parts.push(`samplePresetPath=${samplePresetPath}`); |
| 125 | + } |
| 126 | + return parts.join("|"); |
| 127 | +} |
| 128 | + |
| 129 | +function readStoredLaunchSignature() { |
| 130 | + if (typeof window === "undefined") { |
| 131 | + return ""; |
| 132 | + } |
| 133 | + try { |
| 134 | + return normalizeTextValue(window.localStorage.getItem(WORKSPACE_LAUNCH_SIGNATURE_STORAGE_KEY)); |
| 135 | + } catch { |
| 136 | + return ""; |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +function writeStoredLaunchSignature(signature) { |
| 141 | + if (typeof window === "undefined") { |
| 142 | + return; |
| 143 | + } |
| 144 | + try { |
| 145 | + if (signature) { |
| 146 | + window.localStorage.setItem(WORKSPACE_LAUNCH_SIGNATURE_STORAGE_KEY, signature); |
| 147 | + return; |
| 148 | + } |
| 149 | + window.localStorage.removeItem(WORKSPACE_LAUNCH_SIGNATURE_STORAGE_KEY); |
| 150 | + } catch { |
| 151 | + // Ignore storage failures and continue. |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +function clearSharedBindingsForNewLaunch(signature) { |
| 156 | + if (!signature) { |
| 157 | + return false; |
| 158 | + } |
| 159 | + const previous = readStoredLaunchSignature(); |
| 160 | + if (previous && previous === signature) { |
| 161 | + return false; |
| 162 | + } |
| 163 | + clearSharedAssetHandoff(); |
| 164 | + clearSharedPaletteHandoff(); |
| 165 | + writeStoredLaunchSignature(signature); |
| 166 | + return true; |
| 167 | +} |
| 168 | + |
102 | 169 | function readGameLaunchContext() { |
103 | 170 | if (typeof window === "undefined") { |
104 | 171 | return null; |
105 | 172 | } |
106 | 173 | const searchParams = new URLSearchParams(window.location.search); |
107 | | - const gameId = normalizeTextValue(searchParams.get("gameId")); |
| 174 | + const gameId = normalizeTextValue(searchParams.get("gameId") || searchParams.get("game")); |
108 | 175 | const gameTitle = normalizeTextValue(searchParams.get("gameTitle")); |
109 | 176 | const gameHref = normalizeLocalHref(searchParams.get("gameHref"), ["/games/"]); |
110 | 177 | const workspaceHrefParam = normalizeLocalHref(searchParams.get("workspaceHref"), ["/tools/Workspace%20Manager/", "/tools/Workspace Manager/"]); |
@@ -1069,10 +1136,12 @@ async function initPlatformShell() { |
1069 | 1136 |
|
1070 | 1137 | const currentToolId = document.body.dataset.toolId || ""; |
1071 | 1138 | const currentTool = currentToolId ? getToolById(currentToolId) : null; |
1072 | | - const launchContext = readGameLaunchContext(); |
1073 | 1139 | const searchParams = typeof window !== "undefined" |
1074 | 1140 | ? new URLSearchParams(window.location.search) |
1075 | 1141 | : new URLSearchParams(); |
| 1142 | + const launchSignature = readLaunchPayloadSignature(searchParams); |
| 1143 | + const clearedForLaunch = clearSharedBindingsForNewLaunch(launchSignature); |
| 1144 | + const launchContext = readGameLaunchContext(); |
1076 | 1145 | const launchedFromSamplePreset = searchParams.has("samplePresetPath"); |
1077 | 1146 |
|
1078 | 1147 | if (currentToolId) { |
@@ -1115,6 +1184,9 @@ async function initPlatformShell() { |
1115 | 1184 | const hydratedAsset = await hydrateSharedAssetFromGameLaunchContext(catalogContext); |
1116 | 1185 | const hydratedPalette = await hydrateSharedPaletteFromGameLaunchContext(catalogContext); |
1117 | 1186 | renderShell(currentTool); |
| 1187 | + if (clearedForLaunch) { |
| 1188 | + TOOLS_PLATFORM_LOGGER.debug("cleared shared workspace bindings for new launch payload"); |
| 1189 | + } |
1118 | 1190 | if (hydratedAsset) { |
1119 | 1191 | TOOLS_PLATFORM_LOGGER.debug("shared asset hydrated from game asset catalog"); |
1120 | 1192 | } |
|
0 commit comments