(selector: string)
| 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. |
no test coverage detected