Skip to content

Commit f56a444

Browse files
committed
Add sdk url option
1 parent 752da08 commit f56a444

File tree

3 files changed

+49
-41
lines changed

3 files changed

+49
-41
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,14 @@ Yandex dropped the Service Worker description page in their docs, but it still a
193193

194194
```ini
195195
[yagames]
196+
sdk_url = /sdk.js
196197
sdk_init_options = {}
197198
sdk_init_snippet = console.log("Yandex Games SDK is ready!");
198199
service_worker_url = sw.js
199200
manifest_url = yandex-manifest.json
200201
```
201202

203+
* `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`.
202204
* `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 } }`.
203205
* `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.**
204206
* `service_worker_url` - Relative URL to the Service Worker file. Usually it's `sw.js`. Set the URL to enable Service Worker.

game.project

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ high_dpi = 1
1111

1212
[project]
1313
title = yagames
14-
version = 0.13.0
14+
version = 0.14.0
1515
developer = Indiesoft LLC
1616
bundle_resources = example/bundle/
1717
dependencies#0 = https://github.com/subsoap/defos/archive/v2.5.0.zip
@@ -39,8 +39,5 @@ max_characters = 30000
3939
game_binding = /example/input/game.input_bindingc
4040

4141
[yagames]
42-
sdk_init_options = { orientation: { value: "portrait", lock: true } }
43-
sdk_init_snippet = console.log("Yandex Games SDK is ready!");
44-
service_worker_url = sw.js
45-
manifest_url = yandex-manifest.json
42+
sdk_init_snippet = console.log("Yandex Games SDK is ready! (this message is to test the option `yagames.sdk_init_snippet`)");
4643

yagames/manifests/web/engine_template.html

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616

1717
var t = d.getElementsByTagName("script")[0];
1818
var s = d.createElement("script");
19-
s.src = "https://yandex.ru/games/sdk/v2";
19+
var url;
20+
// {{#yagames.sdk_url}}
21+
url = "{{{yagames.sdk_url}}}";
22+
// {{/yagames.sdk_url}}
23+
s.src = url || "/sdk.js";
2024
s.async = true;
2125
t.parentNode.insertBefore(s, t);
2226
s.onload = function () {
@@ -25,46 +29,51 @@
2529
options = {{{yagames.sdk_init_options}}};
2630
// {{/yagames.sdk_init_options}}
2731

28-
YaGames.init(options)
29-
.then(function (ysdk) {
30-
// {{#yagames.service_worker_url}}
31-
var isNativeCache = false;
32-
// {{#yagames.manifest_url}}
33-
isNativeCache = ysdk.yandexApp && ysdk.yandexApp.enabled;
34-
// {{/yagames.manifest_url}}
35-
if ("serviceWorker" in navigator) {
36-
if (isNativeCache) {
37-
// Force unregister all Service Workers
38-
navigator.serviceWorker.getRegistrations().then(function (registrations) {
39-
registrations.forEach(function (registration) {
40-
registration.unregister().then(function (isUnregistered) {
41-
console.info("YaGames: Service Worker unregistration result", isUnregistered);
32+
try {
33+
YaGames.init(options)
34+
.then(function (ysdk) {
35+
// {{#yagames.service_worker_url}}
36+
var isNativeCache = false;
37+
// {{#yagames.manifest_url}}
38+
isNativeCache = ysdk.yandexApp && ysdk.yandexApp.enabled;
39+
// {{/yagames.manifest_url}}
40+
if ("serviceWorker" in navigator) {
41+
if (isNativeCache) {
42+
// Force unregister all Service Workers
43+
navigator.serviceWorker.getRegistrations().then(function (registrations) {
44+
registrations.forEach(function (registration) {
45+
registration.unregister().then(function (isUnregistered) {
46+
console.info("YaGames: Service Worker unregistration result", isUnregistered);
47+
});
4248
});
4349
});
44-
});
45-
} else {
46-
// Register Service Worker
47-
navigator.serviceWorker.register("{{yagames.service_worker_url}}").then(
48-
function (registration) {
49-
console.info("YaGames: Service Worker registration successful with scope", registration.scope);
50-
},
51-
function (err) {
52-
console.info("YaGames: Service Worker registration failed", err);
53-
}
54-
);
50+
} else {
51+
// Register Service Worker
52+
navigator.serviceWorker.register("{{yagames.service_worker_url}}").then(
53+
function (registration) {
54+
console.info("YaGames: Service Worker registration successful with scope", registration.scope);
55+
},
56+
function (err) {
57+
console.info("YaGames: Service Worker registration failed", err);
58+
}
59+
);
60+
}
5561
}
56-
}
57-
// {{/yagames.service_worker_url}}
62+
// {{/yagames.service_worker_url}}
5863

59-
// {{#yagames.sdk_init_snippet}}
60-
{{{yagames.sdk_init_snippet}}}
61-
// {{/yagames.sdk_init_snippet}}
64+
// {{#yagames.sdk_init_snippet}}
65+
{{{yagames.sdk_init_snippet}}}
66+
// {{/yagames.sdk_init_snippet}}
6267

63-
send(0, "init", ysdk);
64-
})
65-
.catch(function (err) {
66-
send(0, "error", err + "");
67-
});
68+
send(0, "init", ysdk);
69+
})
70+
.catch(function (err) {
71+
send(0, "error", err + "");
72+
});
73+
} catch (err) {
74+
// In the case that the user specified an incorrect URL and YaGames is undefined.
75+
send(0, "error", "Incorrect SDK. " + err);
76+
}
6877
};
6978
s.onerror = function () {
7079
send(0, "error", "Error loading SDK. Reload the page.");

0 commit comments

Comments
 (0)