Skip to content

Commit

Permalink
Add sdk url option
Browse files Browse the repository at this point in the history
  • Loading branch information
aglitchman committed Jul 30, 2024
1 parent 752da08 commit f56a444
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 41 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 2 additions & 5 deletions game.project
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`)");

81 changes: 45 additions & 36 deletions yagames/manifests/web/engine_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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.");
Expand Down

0 comments on commit f56a444

Please sign in to comment.