diff --git a/README.md b/README.md index 9aed7f3..95b2f70 100644 --- a/README.md +++ b/README.md @@ -193,12 +193,14 @@ Yandex dropped the Service Worker description page in their docs, but it still a ```ini [yagames] +sdk_url = /sdk.js sdk_init_options = {} sdk_init_snippet = console.log("Yandex Games SDK is ready!"); service_worker_url = sw.js manifest_url = yandex-manifest.json ``` +* `sdk_url` - Sets the URL of the Yandex.Games SDK. In July 2024 the platform changed the URL of its SDK and now it can be of two kinds. First is the local `/sdk.js` for games you upload as an archive (default, **suitable for 99% of games**). The second is for iFrame games - `https://sdk.games.s3.yandex.net/sdk.js`. * `sdk_init_options` - JavaScript Object that is passed as-is into the Yandex Games SDK initialization options for [the JS `YaGames.init` function](https://yandex.ru/dev/games/doc/dg/sdk/sdk-about.html?lang=en). Example: `{ orientation: { value: "landscape", lock: true } }`. * `sdk_init_snippet` - JavaScript code that is passed as-is and called when the `ysdk` variable becomes available. Example: `console.log(ysdk);`. **Use with care, and don't forget to put a semicolon `;` at the end.** * `service_worker_url` - Relative URL to the Service Worker file. Usually it's `sw.js`. Set the URL to enable Service Worker. diff --git a/game.project b/game.project index a548880..ef9a72f 100755 --- a/game.project +++ b/game.project @@ -11,7 +11,7 @@ high_dpi = 1 [project] title = yagames -version = 0.13.0 +version = 0.14.0 developer = Indiesoft LLC bundle_resources = example/bundle/ dependencies#0 = https://github.com/subsoap/defos/archive/v2.5.0.zip @@ -39,8 +39,5 @@ max_characters = 30000 game_binding = /example/input/game.input_bindingc [yagames] -sdk_init_options = { orientation: { value: "portrait", lock: true } } -sdk_init_snippet = console.log("Yandex Games SDK is ready!"); -service_worker_url = sw.js -manifest_url = yandex-manifest.json +sdk_init_snippet = console.log("Yandex Games SDK is ready! (this message is to test the option `yagames.sdk_init_snippet`)"); diff --git a/yagames/manifests/web/engine_template.html b/yagames/manifests/web/engine_template.html index 6b27f99..c98bd58 100644 --- a/yagames/manifests/web/engine_template.html +++ b/yagames/manifests/web/engine_template.html @@ -16,7 +16,11 @@ var t = d.getElementsByTagName("script")[0]; var s = d.createElement("script"); - s.src = "https://yandex.ru/games/sdk/v2"; + var url; + // {{#yagames.sdk_url}} + url = "{{{yagames.sdk_url}}}"; + // {{/yagames.sdk_url}} + s.src = url || "/sdk.js"; s.async = true; t.parentNode.insertBefore(s, t); s.onload = function () { @@ -25,46 +29,51 @@ options = {{{yagames.sdk_init_options}}}; // {{/yagames.sdk_init_options}} - YaGames.init(options) - .then(function (ysdk) { - // {{#yagames.service_worker_url}} - var isNativeCache = false; - // {{#yagames.manifest_url}} - isNativeCache = ysdk.yandexApp && ysdk.yandexApp.enabled; - // {{/yagames.manifest_url}} - if ("serviceWorker" in navigator) { - if (isNativeCache) { - // Force unregister all Service Workers - navigator.serviceWorker.getRegistrations().then(function (registrations) { - registrations.forEach(function (registration) { - registration.unregister().then(function (isUnregistered) { - console.info("YaGames: Service Worker unregistration result", isUnregistered); + try { + YaGames.init(options) + .then(function (ysdk) { + // {{#yagames.service_worker_url}} + var isNativeCache = false; + // {{#yagames.manifest_url}} + isNativeCache = ysdk.yandexApp && ysdk.yandexApp.enabled; + // {{/yagames.manifest_url}} + if ("serviceWorker" in navigator) { + if (isNativeCache) { + // Force unregister all Service Workers + navigator.serviceWorker.getRegistrations().then(function (registrations) { + registrations.forEach(function (registration) { + registration.unregister().then(function (isUnregistered) { + console.info("YaGames: Service Worker unregistration result", isUnregistered); + }); }); }); - }); - } else { - // Register Service Worker - navigator.serviceWorker.register("{{yagames.service_worker_url}}").then( - function (registration) { - console.info("YaGames: Service Worker registration successful with scope", registration.scope); - }, - function (err) { - console.info("YaGames: Service Worker registration failed", err); - } - ); + } else { + // Register Service Worker + navigator.serviceWorker.register("{{yagames.service_worker_url}}").then( + function (registration) { + console.info("YaGames: Service Worker registration successful with scope", registration.scope); + }, + function (err) { + console.info("YaGames: Service Worker registration failed", err); + } + ); + } } - } - // {{/yagames.service_worker_url}} + // {{/yagames.service_worker_url}} - // {{#yagames.sdk_init_snippet}} - {{{yagames.sdk_init_snippet}}} - // {{/yagames.sdk_init_snippet}} + // {{#yagames.sdk_init_snippet}} + {{{yagames.sdk_init_snippet}}} + // {{/yagames.sdk_init_snippet}} - send(0, "init", ysdk); - }) - .catch(function (err) { - send(0, "error", err + ""); - }); + send(0, "init", ysdk); + }) + .catch(function (err) { + send(0, "error", err + ""); + }); + } catch (err) { + // In the case that the user specified an incorrect URL and YaGames is undefined. + send(0, "error", "Incorrect SDK. " + err); + } }; s.onerror = function () { send(0, "error", "Error loading SDK. Reload the page.");