MCPcopy Create free account
hub / github.com/Monogatari/Monogatari / setup

Method setup

src/monogatari.ts:1913–2093  ·  view source on GitHub ↗
(selector: string)

Source from the content-addressed store, hash-verified

1911 }
1912
1913 static setup (selector: string): Promise<void | undefined> {
1914 const components = this.components();
1915
1916 // Set the initial settings if they don't exist or load them from the
1917 // Storage if they do.
1918 const loadPreferencesPromise = new Promise<void>((resolve, _reject) => {
1919 this.Storage.get('Settings').then ((local_settings) => {
1920 this.global ('_first_run', false);
1921 this._preferences = merge (this._preferences, local_settings as Partial<PlayerPreferences>);
1922 resolve();
1923 }).catch ((e) => {
1924 console.warn ('There was no settings saved. This may be the first time this game was opened, we\'ll create them now.', e);
1925 this.global ('_first_run', true);
1926 if (this.setting ('MultiLanguage') !== true || this.setting ('LanguageSelectionScreen') !== true) {
1927 this.Storage.set ('Settings', this._preferences).then(() => resolve()).catch(() => resolve());
1928 } else {
1929 resolve();
1930 }
1931 });
1932 });
1933
1934 return loadPreferencesPromise.then(() => {
1935 // Define all the components that were registered to this point
1936 for (const component of components) {
1937 try {
1938 component.engine = this.asEngine();
1939 Registry.register(component.tag, component);
1940 } catch (e) {
1941 FancyError.show ('engine:component:already_registered', {
1942 tag: component.tag,
1943 component: component,
1944 unregisterCode: `<pre><code class='language-javascript'>monogatari.unregisterComponent ('${component.tag}')</code></pre>`
1945 });
1946 }
1947 }
1948
1949 // Register service worker. The service worker will save all requests into
1950 // the cache so the game loads more quickly and we can play offline. The
1951 // service worker will only be used if it was allowed by the settings and
1952 // if we are not running in a local platform such as electron or cordova
1953 // where the assets are expected to be available locally and thus don't
1954 // require being cached.
1955 if (this.setting ('ServiceWorkers')) {
1956 if (!Platform.desktopApp && !Platform.cordova && Platform.serviceWorkers) {
1957 if (window.location.protocol === 'file:') {
1958 console.warn ('Service Workers are not available when opening the index.html file directly in your browser. Service Workers are available only when serving your files through a server, once you upload your game this warning will go away. You can also try using a simple server like this one for development: https://chrome.google.com/webstore/detail/web-server-for-chrome/ofhbbkphhbklhfoeikjpcbhemlocgigb/.');
1959 } else {
1960 navigator.serviceWorker.register('./service-worker.js').then ((registration) => {
1961
1962 // Check if an update to the service worker was found
1963 registration.onupdatefound = () => {
1964 const worker = registration.installing;
1965
1966 if (worker) {
1967 worker.onstatechange = () => {
1968 // Once the updated service worker has been installed,
1969 // show a notice to the players so that they reload the
1970 // page and get the latest content.

Callers 1

initMethod · 0.95

Calls 15

componentsMethod · 0.95
globalMethod · 0.95
settingMethod · 0.95
asEngineMethod · 0.95
elementMethod · 0.95
stringMethod · 0.95
storageMethod · 0.95
registerListenerMethod · 0.95
showScreenMethod · 0.95
onStartMethod · 0.95
labelMethod · 0.95
stateMethod · 0.95

Tested by

no test coverage detected