Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Bug Unity DataCaching (IndexedDB) not working for .wasm file

Discussion in 'Web' started by Robert-UA, Jan 19, 2024.

  1. Robert-UA

    Robert-UA

    Joined:
    Aug 19, 2021
    Posts:
    11
    Our target platform is TVs that clear the cache for web applications, so a possible way out for us is the use of IndexedDB, for which the Data Caching parameter when compiling WebGL is responsible, but judging by the logs and the page loading debug (Network panel), it works only for the .data file

    I already tried override cacheControl method, but it didn't help either, because it is responsible for files (like .data) requested from loader.js, and file .wasm is requested from .framework.js

    So .wasm can only always be downloaded from server or cached by the browser, but with the TV platform it's the only first option, which really cuts down on load times (~1min for ~10mb build).

    Unity Editor 2021.1.28 (last with asm.js support - we targeting both asm and wasm)

    Is there any option to force .wasm to be "cached" in the IndexedDB like .data?
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,767
    I can't really answer the question but the support of asm.js in this day and age raises my eyebrows, so excuse me for raising the question whether this is absolutely necessary. ;)

    At least explore newer versions - not commit yet. At least 2021.3 LTS but try going even higher. A lot of progress has been made in Web tech on Unity's side, including load times and caching. It's definitely worth investigating, especially if it solves the issue you could use that argue in favor of dropping asm.js support.

    Unless you have a requirement for asm.js in the sense "wouldn't work otherwise on a given hardware" I would straight out drop asm support. Even then, such a requirement is often to satisfy marketing statistics (aka a few percent more potential customers due to device market share) but not the reality (none of these "potential customers" even exist, because these outdated devices, even though still being tracked in statistics, have likely been moved off to secondary uses like a kitchen TV for recipes, kid's room TV set, stored in a cupboard drawer for "hard times", etc).

    If it means a better experience for 95% of your target audience and lose 5% due to incompatibility, that's a good tradeoff. ;)
     
    Robert-UA likes this.
  3. Robert-UA

    Robert-UA

    Joined:
    Aug 19, 2021
    Posts:
    11
    The problem is not with the version of Unity, because the behavior is the same on version 2022.3.17. Of course, if caching works in the browser, then you can simply turn off Data Caching in the settings and configure the appropriate HTTP headers, but when this cache is constantly cleared on TVs, I need "caching" using IndexedDB, which this Unity setting provides. Debugging examples via the Network tab below.

    Here you can see that the size of the .data file is "significant" (1.3MB), because this is the first run and it was being downloaded.
    [UnityCache] 'http://localhost/Build/Build 2022.data.gz' successfully downloaded and stored in the indexedDB cache
    upload_2024-1-20_16-21-26.png

    At the second launch, the size is formal (the data passed validation and was loaded from IndexedDB), but only for the .data file, and I need the same to work for .wasm and .asm.js files (now they download over the network every time).
    [UnityCache] 'http://localhost/Build/Build 2022.data.gz' successfully revalidated and served from the indexedDB cache
    upload_2024-1-20_16-20-34.png

    About asm.js (asm) and WebAssembly (wasm)
    https://webostv.developer.lge.com/develop/specifications/web-api-and-web-engine - WebOS supports WASM from ~2019 (not so old)
    https://developer.samsung.com/smarttv/develop/specifications/web-engine-specifications.html - the same for Tizen

    I build both versions and the appropriate one is delivered to the user
     
  4. Robert-UA

    Robert-UA

    Joined:
    Aug 19, 2021
    Posts:
    11
    I believe that you can add your own caching logic inside the .framework.js file, namely in a specific method integrateWasmJS()

    I have limited knowledge in web and javascript
    If I can achieve the result by manually changing the source .framework.js file of build output, then I can override this method using the .jslib plugin

    It is possible? Any ideas?


    Code (JavaScript):
    1. function integrateWasmJS() {
    2.         var wasmTextFile = "build.wast";
    3.         var wasmBinaryFile = "build.wasm";
    4.         var asmjsCodeFile = "build.temp.asm.js";
    5.         if (!isDataURI(wasmTextFile)) {
    6.             wasmTextFile = locateFile(wasmTextFile)
    7.         }
    8.         if (!isDataURI(wasmBinaryFile)) {
    9.             wasmBinaryFile = locateFile(wasmBinaryFile)
    10.         }
    11.         if (!isDataURI(asmjsCodeFile)) {
    12.             asmjsCodeFile = locateFile(asmjsCodeFile)
    13.         }
    14.         var wasmPageSize = 64 * 1024;
    15.         var info = {
    16.             "global": null,
    17.             "env": null,
    18.             "asm2wasm": asm2wasmImports,
    19.             "parent": Module
    20.         };
    21.         var exports = null;
    22.  
    23.         function mergeMemory(newBuffer) {
    24.             var oldBuffer = Module["buffer"];
    25.             if (newBuffer.byteLength < oldBuffer.byteLength) {
    26.                 err("the new buffer in mergeMemory is smaller than the previous one. in native wasm, we should grow memory here")
    27.             }
    28.             var oldView = new Int8Array(oldBuffer);
    29.             var newView = new Int8Array(newBuffer);
    30.             newView.set(oldView);
    31.             updateGlobalBuffer(newBuffer);
    32.             updateGlobalBufferViews()
    33.         }
    34.  
    35.         function fixImports(imports) {
    36.             return imports
    37.         }
    38.  
    39.         function getBinary() {
    40.             try {
    41.                 if (Module["wasmBinary"]) {
    42.                     return new Uint8Array(Module["wasmBinary"])
    43.                 }
    44.                 if (Module["readBinary"]) {
    45.                     return Module["readBinary"](wasmBinaryFile)
    46.                 } else {
    47.                     throw "both async and sync fetching of the wasm failed"
    48.                 }
    49.             } catch (err) {
    50.                 abort(err)
    51.             }
    52.         }
    53.  
    54.         function getBinaryPromise() {
    55.             if (!Module["wasmBinary"] && (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) && typeof fetch === "function") {
    56.                 return fetch(wasmBinaryFile, {
    57.                     credentials: "same-origin"
    58.                 }).then((function(response) {
    59.                     if (!response["ok"]) {
    60.                         throw "failed to load wasm binary file at '" + wasmBinaryFile + "'"
    61.                     }
    62.                     return response["arrayBuffer"]()
    63.                 })).catch((function() {
    64.                     return getBinary()
    65.                 }))
    66.             }
    67.             return new Promise((function(resolve, reject) {
    68.                 resolve(getBinary())
    69.             }))
    70.         }
    71.         //test start
    72.        
    73.         //test end
    74.  
    75.  
    76.         function doNativeWasm(global, env, providedBuffer) {
    77.             if (typeof WebAssembly !== "object") {
    78.                 err("no native wasm support detected");
    79.                 return false
    80.             }
    81.             if (!(Module["wasmMemory"] instanceof WebAssembly.Memory)) {
    82.                 err("no native wasm Memory in use");
    83.                 return false
    84.             }
    85.             env["memory"] = Module["wasmMemory"];
    86.             info["global"] = {
    87.                 "NaN": NaN,
    88.                 "Infinity": Infinity
    89.             };
    90.             info["global.Math"] = Math;
    91.             info["env"] = env;
    92.  
    93.             function receiveInstance(instance, module) {
    94.                 console.warn("[FFFF] instantiateArrayBuffer" + instance + " | " + module)
    95.                 exports = instance.exports;
    96.                 if (exports.memory) mergeMemory(exports.memory);
    97.                 Module["asm"] = exports;
    98.                 Module["usingWasm"] = true;
    99.                 removeRunDependency("wasm-instantiate")
    100.             }
    101.             addRunDependency("wasm-instantiate");
    102.             if (Module["instantiateWasm"]) {
    103.                 try {
    104.                     return Module["instantiateWasm"](info, receiveInstance)
    105.                 } catch (e) {
    106.                     err("Module.instantiateWasm callback failed with error: " + e);
    107.                     return false
    108.                 }
    109.             }
    110.  
    111.             function receiveInstantiatedSource(output) {
    112.                
    113.                 console.warn("[FFFF] instantiateArrayBuffer" + output)
    114.                 receiveInstance(output["instance"], output["module"])
    115.             }
    116.  
    117.             function instantiateArrayBuffer(receiver) {
    118.                 console.warn("[FFFF] instantiateArrayBuffer" + receiver)
    119.                 getBinaryPromise().then((function(binary) {
    120.                     return WebAssembly.instantiate(binary, info)
    121.                 })).then(receiver).catch((function(reason) {
    122.                     err("failed to asynchronously prepare wasm: " + reason);
    123.                     abort(reason)
    124.                 }))
    125.             }
    126.             if (!Module["wasmBinary"] && typeof WebAssembly.instantiateStreaming === "function" && !isDataURI(wasmBinaryFile) && typeof fetch === "function")
    127.             {
    128.                 console.log("[FFFF-1]" + wasmBinaryFile);
    129.                 WebAssembly.instantiateStreaming(fetch(wasmBinaryFile,
    130.                     {
    131.                         credentials: "same-origin"
    132.                     }),
    133.                     info).then(receiveInstantiatedSource).catch((function(reason)
    134.                     {
    135.                         err("wasm streaming compile failed: " + reason);
    136.                         err("falling back to ArrayBuffer instantiation");
    137.                         instantiateArrayBuffer(receiveInstantiatedSource)
    138.                     }))
    139.             }
    140.             else
    141.             {
    142.                 console.log("[FFFF-2]" + wasmBinaryFile);
    143.                 instantiateArrayBuffer(receiveInstantiatedSource)
    144.             }
    145.             return {}
    146.         }
    147.         Module["asmPreload"] = Module["asm"];
    148.         var asmjsReallocBuffer = Module["reallocBuffer"];
    149.         var wasmReallocBuffer = (function(size) {
    150.             var PAGE_MULTIPLE = Module["usingWasm"] ? WASM_PAGE_SIZE : ASMJS_PAGE_SIZE;
    151.             size = alignUp(size, PAGE_MULTIPLE);
    152.             var old = Module["buffer"];
    153.             var oldSize = old.byteLength;
    154.             if (Module["usingWasm"]) {
    155.                 try {
    156.                     var result = Module["wasmMemory"].grow((size - oldSize) / wasmPageSize);
    157.                     if (result !== (-1 | 0)) {
    158.                         return Module["buffer"] = Module["wasmMemory"].buffer
    159.                     } else {
    160.                         return null
    161.                     }
    162.                 } catch (e) {
    163.                     return null
    164.                 }
    165.             }
    166.         });
    167.         Module["reallocBuffer"] = (function(size) {
    168.             if (finalMethod === "asmjs") {
    169.                 return asmjsReallocBuffer(size)
    170.             } else {
    171.                 return wasmReallocBuffer(size)
    172.             }
    173.         });
    174.         var finalMethod = "";
    175.         Module["asm"] = (function(global, env, providedBuffer) {
    176.             env = fixImports(env);
    177.             if (!env["table"]) {
    178.                 var TABLE_SIZE = Module["wasmTableSize"];
    179.                 if (TABLE_SIZE === undefined) TABLE_SIZE = 1024;
    180.                 var MAX_TABLE_SIZE = Module["wasmMaxTableSize"];
    181.                 if (typeof WebAssembly === "object" && typeof WebAssembly.Table === "function") {
    182.                     if (MAX_TABLE_SIZE !== undefined) {
    183.                         env["table"] = new WebAssembly.Table({
    184.                             "initial": TABLE_SIZE,
    185.                             "maximum": MAX_TABLE_SIZE,
    186.                             "element": "anyfunc"
    187.                         })
    188.                     } else {
    189.                         env["table"] = new WebAssembly.Table({
    190.                             "initial": TABLE_SIZE,
    191.                             element: "anyfunc"
    192.                         })
    193.                     }
    194.                 } else {
    195.                     env["table"] = new Array(TABLE_SIZE)
    196.                 }
    197.                 Module["wasmTable"] = env["table"]
    198.             }
    199.             if (!env["memoryBase"]) {
    200.                 env["memoryBase"] = Module["STATIC_BASE"]
    201.             }
    202.             if (!env["tableBase"]) {
    203.                 env["tableBase"] = 0
    204.             }
    205.             var exports;
    206.             exports = doNativeWasm(global, env, providedBuffer);
    207.             assert(exports, "no binaryen method succeeded.");
    208.             return exports
    209.         });
    210.     }
     
  5. Robert-UA

    Robert-UA

    Joined:
    Aug 19, 2021
    Posts:
    11
    Some update. If you set the Decompression Fallback option together with some compression, then .wasm file is also requested from .loader.js - accordingly, the override cacheControl works and the .wasm file is cached in IndexedDB, just like the .data, but what if I want to do it without Decompression Fallback? Does this one option not cause problems and can it be left? (The headers on the server are configured, so standard browser compression methods will be used if possible and the speed will not decrease?)

    Maybe the answer is hidden in UnityLoader.js? (internal editor file ...\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\UnityLoader\UnityLoader.js)

    Code (JavaScript):
    1. function createUnityInstance(canvas, config, onProgress) {
    2.   onProgress = onProgress || function () {};
    3.  
    4. #if USE_THREADS
    5.   // Polyfill Atomics.wake for old Emscripten fastcomp compiler.
    6.   // TODO: When we update to new Emscripten, this can be removed.
    7.   if (typeof Atomics !== 'undefined' && Atomics.notify && !Atomics.wake) {
    8.     Atomics.wake = Atomics.notify;
    9.   }
    10. #endif
    11.  
    12.   function showBanner(msg, type) {
    13.     // Only ever show one error at most - other banner messages after that should get ignored
    14.     // to avoid noise.
    15.     if (!showBanner.aborted && config.showBanner) {
    16.       if (type == 'error') showBanner.aborted = true;
    17.       return config.showBanner(msg, type);
    18.     }
    19.  
    20.     // Fallback to console logging if visible banners have been suppressed
    21.     // from the main page.
    22.     switch(type) {
    23.       case 'error': console.error(msg); break;
    24.       case 'warning': console.warn(msg); break;
    25.       default: console.log(msg); break;
    26.     }
    27.   }
    28.  
    29.   function errorListener(e) {
    30.     var error = e.reason || e.error;
    31.     var message = error ? error.toString() : (e.message || e.reason || '');
    32.     var stack = (error && error.stack) ? error.stack.toString() : '';
    33.  
    34.     // Do not repeat the error message if it's present in the stack trace.
    35.     if (stack.startsWith(message)) {
    36.       stack = stack.substring(message.length);
    37.     }
    38.  
    39.     message += '\n' + stack.trim();
    40.  
    41.     if (!message || !Module.stackTraceRegExp || !Module.stackTraceRegExp.test(message))
    42.       return;
    43.  
    44.     var filename = e.filename || (error && (error.fileName || error.sourceURL)) || '';
    45.     var lineno = e.lineno || (error && (error.lineNumber || error.line)) || 0;
    46.  
    47. #if SYMBOLS_FILENAME
    48.     demanglingErrorHandler(message, filename, lineno);
    49. #else // SYMBOLS_FILENAME
    50.     errorHandler(message, filename, lineno);
    51. #endif // SYMBOLS_FILENAME
    52.   }
    53.  
    54.   var Module = {
    55.     canvas: canvas,
    56.     webglContextAttributes: {
    57.       preserveDrawingBuffer: false,
    58.     },
    59. #if USE_DATA_CACHING
    60.     cacheControl: function (url) {
    61.       return url == Module.dataUrl ? "must-revalidate" : "no-store";
    62.     },
    63. #endif // USE_DATA_CACHING
    64. #if !USE_WASM
    65.     TOTAL_MEMORY: {{{ TOTAL_MEMORY }}},
    66. #endif // !USE_WASM
    67.     streamingAssetsUrl: "StreamingAssets",
    68.     downloadProgress: {},
    69.     deinitializers: [],
    70.     intervals: {},
    71.     setInterval: function (func, ms) {
    72.       var id = window.setInterval(func, ms);
    73.       this.intervals[id] = true;
    74.       return id;
    75.     },
    76.     clearInterval: function(id) {
    77.       delete this.intervals[id];
    78.       window.clearInterval(id);
    79.     },
    80.     preRun: [],
    81.     postRun: [],
    82.     print: function (message) {
    83.       console.log(message);
    84.     },
    85.     printErr: function (message) {
    86.       console.error(message);
    87.  
    88.       if (typeof message === 'string' && message.indexOf('wasm streaming compile failed') != -1) {
    89.         if (message.toLowerCase().indexOf('mime') != -1) {
    90.           showBanner('HTTP Response Header "Content-Type" configured incorrectly on the server for file ' + Module.codeUrl + ' , should be "application/wasm". Startup time performance will suffer.', 'warning');
    91.         } else {
    92.           showBanner('WebAssembly streaming compilation failed! This can happen for example if "Content-Encoding" HTTP header is incorrectly enabled on the server for file ' + Module.codeUrl + ', but the file is not pre-compressed on disk (or vice versa). Check the Network tab in browser Devtools to debug server header configuration.', 'warning');
    93.         }
    94.       }
    95.     },
    96.     locateFile: function (url) {
    97.       return (
    98. #if USE_WASM && !DECOMPRESSION_FALLBACK
    99.         url == "build.wasm" ? this.codeUrl :
    100. #endif // USE_WASM && !DECOMPRESSION_FALLBACK
    101. #if USE_THREADS
    102. #if DECOMPRESSION_FALLBACK
    103.         url == "pthread-main.js" ? this.frameworkBlobUrl :
    104. #else // DECOMPRESSION_FALLBACK
    105.         url == "pthread-main.js" ? this.frameworkUrl :
    106. #endif // DECOMPRESSION_FALLBACK
    107. #endif // USE_THREADS
    108.         url
    109.       );
    110.     },
    111. #if USE_THREADS
    112.     // The contents of "pthread-main.js" is embedded in the framework, which is used as a worker source.
    113.     // Therefore Module.mainScriptUrlOrBlob is no longer needed and is set to a dummy blob for compatibility reasons.
    114.     mainScriptUrlOrBlob: new Blob([" "], { type: "application/javascript" }),
    115. #endif // USE_THREADS
    116.     disabledCanvasEvents: [
    117.       "contextmenu",
    118.       "dragstart",
    119.     ],
    120.   };
    121.  
    122.   for (var parameter in config)
    123.     Module[parameter] = config[parameter];
    124.  
    125.   Module.streamingAssetsUrl = new URL(Module.streamingAssetsUrl, document.URL).href;
    126.  
    127.   // Operate on a clone of Module.disabledCanvasEvents field so that at Quit time
    128.   // we will ensure we'll remove the events that we created (in case user has
    129.   // modified/cleared Module.disabledCanvasEvents in between)
    130.   var disabledCanvasEvents = Module.disabledCanvasEvents.slice();
    131.  
    132.   function preventDefault(e) {
    133.     e.preventDefault();
    134.   }
    135.  
    136.   disabledCanvasEvents.forEach(function (disabledCanvasEvent) {
    137.     canvas.addEventListener(disabledCanvasEvent, preventDefault);
    138.   });
    139.  
    140.   window.addEventListener("error", errorListener);
    141.   window.addEventListener("unhandledrejection", errorListener);
    142.  
    143.   var unityInstance = {
    144.     Module: Module,
    145.     SetFullscreen: function () {
    146.       if (Module.SetFullscreen)
    147.         return Module.SetFullscreen.apply(Module, arguments);
    148.       Module.print("Failed to set Fullscreen mode: Player not loaded yet.");
    149.     },
    150.     SendMessage: function () {
    151.       if (Module.SendMessage)
    152.         return Module.SendMessage.apply(Module, arguments);
    153.       Module.print("Failed to execute SendMessage: Player not loaded yet.");
    154.     },
    155.     Quit: function () {
    156.       return new Promise(function (resolve, reject) {
    157.         Module.shouldQuit = true;
    158.         Module.onQuit = resolve;
    159.  
    160.         // Clear the event handlers we added above, so that the event handler
    161.         // functions will not hold references to this JS function scope after
    162.         // exit, to allow JS garbage collection to take place.
    163.         disabledCanvasEvents.forEach(function (disabledCanvasEvent) {
    164.           canvas.removeEventListener(disabledCanvasEvent, preventDefault);
    165.         });
    166.         window.removeEventListener("error", errorListener);
    167.         window.removeEventListener("unhandledrejection", errorListener);
    168.       });
    169.     },
    170.   };
    171.  
    172.   Module.SystemInfo = (function () {
    173. #if 0
    174.     // Recognize and parse the following formats of user agents:
    175.  
    176.     // Opera 71 on Windows 10:         Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 OPR/71.0.3770.228
    177.     // Edge 85 on Windows 10:          Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 Edg/85.0.564.70
    178.     // Firefox 81 on Windows 10:       Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:81.0) Gecko/20100101 Firefox/81.0
    179.     // Chrome 85 on Windows 10:        Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36
    180.     // IE 11 on Windows 7:             Mozilla/5.0 CK={} (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
    181.     // IE 10 on Windows 7:             Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)
    182.  
    183.     // Chrome 80 on Android 8.0.0:     Mozilla/5.0 (Linux; Android 8.0.0; VKY-L29) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Mobile Safari/537.36
    184.     // Firefox 68 on Android 8.0.0:    Mozilla/5.0 (Android 8.0.0; Mobile; rv:68.0) Gecko/68.0 Firefox/68.0
    185.  
    186.     // Samsung Browser on Android 9:   Mozilla/5.0 (Linux; Android 9; SAMSUNG SM-G960U) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/10.2 Chrome/71.0.3578.99 Mobile Safari/537.36
    187.     // Safari 13.0.5 on iPhone 13.3.1: Mozilla/5.0 (iPhone; CPU iPhone OS 13_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.5 Mobile/15E148 Safari/604.1
    188.  
    189.     // Safari 12.1 on iPad OS 12.2     Mozilla/5.0 (iPad; CPU OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1 Mobile/15E148 Safari/604.1
    190.  
    191.     // Safari 14 on macOS 11.0:        Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1
    192.     // Safari 14 on macOS 10.15.6:     Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Safari/605.1.15
    193.     // Firefox 80 on macOS 10.15:      Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:80.0) Gecko/20100101 Firefox/80.0
    194.     // Chrome 65 on macOS 10.15.6:     Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36
    195.  
    196.     // Firefox 57 on FreeBSD:          Mozilla/5.0 (X11; FreeBSD amd64; rv:57.0) Gecko/20100101 Firefox/57.0
    197.     // Chrome 43 on OpenBSD:           Mozilla/5.0 (X11; OpenBSD amd64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.125 Safari/537.36
    198. #endif
    199.  
    200.     var browser, browserVersion, os, osVersion, canvas, gpu;
    201.  
    202.     var ua = navigator.userAgent + ' ';
    203.     var browsers = [
    204.       ['Firefox', 'Firefox'],
    205.       ['OPR', 'Opera'],
    206.       ['Edg', 'Edge'],
    207.       ['SamsungBrowser', 'Samsung Browser'],
    208.       ['Trident', 'Internet Explorer'],
    209.       ['MSIE', 'Internet Explorer'],
    210.       ['Chrome', 'Chrome'],
    211.       ['CriOS', 'Chrome on iOS Safari'],
    212.       ['FxiOS', 'Firefox on iOS Safari'],
    213.       ['Safari', 'Safari'],
    214.     ];
    215.  
    216.     function extractRe(re, str, idx) {
    217.       re = RegExp(re, 'i').exec(str);
    218.       return re && re[idx];
    219.     }
    220.     for(var b = 0; b < browsers.length; ++b) {
    221.       browserVersion = extractRe(browsers[b][0] + '[\/ ](.*?)[ \\)]', ua, 1);
    222.       if (browserVersion) {
    223.         browser = browsers[b][1];
    224.         break;
    225.       }
    226.     }
    227.     if (browser == 'Safari') browserVersion = extractRe('Version\/(.*?) ', ua, 1);
    228.     if (browser == 'Internet Explorer') browserVersion = extractRe('rv:(.*?)\\)? ', ua, 1) || browserVersion;
    229.  
    230.     var oses = [
    231.       ['Windows (.*?)[;\)]', 'Windows'],
    232.       ['Android ([0-9_\.]+)', 'Android'],
    233.       ['iPhone OS ([0-9_\.]+)', 'iPhoneOS'],
    234.       ['iPad.*? OS ([0-9_\.]+)', 'iPadOS'],
    235.       ['FreeBSD( )', 'FreeBSD'],
    236.       ['OpenBSD( )', 'OpenBSD'],
    237.       ['Linux|X11()', 'Linux'],
    238.       ['Mac OS X ([0-9_\.]+)', 'macOS'],
    239.       ['bot|google|baidu|bing|msn|teoma|slurp|yandex', 'Search Bot']
    240.     ];
    241.     for(var o = 0; o < oses.length; ++o) {
    242.       osVersion = extractRe(oses[o][0], ua, 1);
    243.       if (osVersion) {
    244.         os = oses[o][1];
    245.         osVersion = osVersion.replace(/_/g, '.');
    246.         break;
    247.       }
    248.     }
    249.     var versionMappings = {
    250.       'NT 5.0': '2000',
    251.       'NT 5.1': 'XP',
    252.       'NT 5.2': 'Server 2003',
    253.       'NT 6.0': 'Vista',
    254.       'NT 6.1': '7',
    255.       'NT 6.2': '8',
    256.       'NT 6.3': '8.1',
    257.       'NT 10.0': '10'
    258.     };
    259.     osVersion = versionMappings[osVersion] || osVersion;
    260.  
    261.     // TODO: Add mobile device identifier, e.g. SM-G960U
    262.  
    263.     canvas = document.createElement("canvas");
    264.     if (canvas) {
    265.       gl = canvas.getContext("webgl2");
    266.       glVersion = gl ? 2 : 0;
    267.       if (!gl) {
    268.         if (gl = canvas && canvas.getContext("webgl")) glVersion = 1;
    269.       }
    270.  
    271.       if (gl) {
    272.         gpu = (gl.getExtension("WEBGL_debug_renderer_info") && gl.getParameter(0x9246 /*debugRendererInfo.UNMASKED_RENDERER_WEBGL*/)) || gl.getParameter(0x1F01 /*gl.RENDERER*/);
    273.       }
    274.     }
    275.  
    276.     var hasThreads = typeof SharedArrayBuffer !== 'undefined';
    277.     var hasWasm = typeof WebAssembly === "object" && typeof WebAssembly.compile === "function";
    278.     return {
    279.       width: screen.width,
    280.       height: screen.height,
    281.       userAgent: ua.trim(),
    282.       browser: browser || 'Unknown browser',
    283.       browserVersion: browserVersion || 'Unknown version',
    284.       mobile: /Mobile|Android|iP(ad|hone)/.test(navigator.appVersion),
    285.       os: os || 'Unknown OS',
    286.       osVersion: osVersion || 'Unknown OS Version',
    287.       gpu: gpu || 'Unknown GPU',
    288.       language: navigator.userLanguage || navigator.language,
    289.       hasWebGL: glVersion,
    290.       hasCursorLock: !!document.body.requestPointerLock,
    291.       hasFullscreen: !!document.body.requestFullscreen || !!document.body.webkitRequestFullscreen, // Safari still uses the webkit prefixed version
    292.       hasThreads: hasThreads,
    293.       hasWasm: hasWasm,
    294.       // This should be updated when we re-enable wasm threads. Previously it checked for WASM thread
    295.       // support with: var wasmMemory = hasWasm && hasThreads && new WebAssembly.Memory({"initial": 1, "maximum": 1, "shared": true});
    296.       // which caused Chrome to have a warning that SharedArrayBuffer requires cross origin isolation.
    297.       hasWasmThreads: false,
    298.     };
    299.   })();
    300.  
    301.   function errorHandler(message, filename, lineno) {
    302.     // Unity needs to rely on Emscripten deferred fullscreen requests, so these will make their way to error handler
    303.     if (message.indexOf('fullscreen error') != -1)
    304.       return;
    305.  
    306.     if (Module.startupErrorHandler) {
    307.       Module.startupErrorHandler(message, filename, lineno);
    308.       return;
    309.     }
    310.     if (Module.errorHandler && Module.errorHandler(message, filename, lineno))
    311.       return;
    312.     console.log("Invoking error handler due to\n" + message);
    313.  
    314.     // Support Firefox window.dump functionality.
    315.     if (typeof dump == "function")
    316.       dump("Invoking error handler due to\n" + message);
    317.  
    318.     if (errorHandler.didShowErrorMessage)
    319.       return;
    320.     var message = "An error occurred running the Unity content on this page. See your browser JavaScript console for more info. The error was:\n" + message;
    321.     if (message.indexOf("DISABLE_EXCEPTION_CATCHING") != -1) {
    322.       message = "An exception has occurred, but exception handling has been disabled in this build. If you are the developer of this content, enable exceptions in your project WebGL player settings to be able to catch the exception or see the stack trace.";
    323.     } else if (message.indexOf("Cannot enlarge memory arrays") != -1) {
    324.       message = "Out of memory. If you are the developer of this content, try allocating more memory to your WebGL build in the WebGL player settings.";
    325.     } else if (message.indexOf("Invalid array buffer length") != -1  || message.indexOf("Invalid typed array length") != -1 || message.indexOf("out of memory") != -1 || message.indexOf("could not allocate memory") != -1) {
    326.       message = "The browser could not allocate enough memory for the WebGL content. If you are the developer of this content, try allocating less memory to your WebGL build in the WebGL player settings.";
    327.     }
    328.     alert(message);
    329.     errorHandler.didShowErrorMessage = true;
    330.   }
    331.  
    332. #if SYMBOLS_FILENAME
    333.   function demangleMessage(message, symbols) {
    334. #if USE_WASM
    335.     var symbolExp = "(wasm-function\\[)(\\d+)(\\])";
    336. #else // USE_WASM
    337.     var symbolExp = "(\\n|\\n    at |\\n    at Array\\.)([a-zA-Z0-9_$]+)(@| \\()";
    338. #endif // USE_WASM
    339.     var symbolRegExp = new RegExp(symbolExp);
    340.     return message.replace(new RegExp(symbolExp, "g"), function (symbol) {
    341.       var match = symbol.match(symbolRegExp);
    342. #if USE_WASM
    343.       return match[1] + (symbols[match[2]] ? symbols[match[2]] + "@" : "") + match[2] + match[3];
    344. #else // USE_WASM
    345.       return match[1] + match[2] + (symbols[match[2]] ? "[" + symbols[match[2]] + "]" : "") + match[3];
    346. #endif // USE_WASM
    347.     });
    348.   }
    349.  
    350.   function demanglingErrorHandler(message, filename, lineno) {
    351.     if (Module.symbols) {
    352.       errorHandler(demangleMessage(message, Module.symbols), filename, lineno);
    353.     } else if (!Module.symbolsUrl) {
    354.       errorHandler(message, filename, lineno);
    355.     } else {
    356.       downloadBinary("symbolsUrl").then(function (data) {
    357.         var json = "";
    358.         for (var i = 0; i < data.length; i++)
    359.           json += String.fromCharCode(data[i]);
    360.         Module.symbols = JSON.parse(json);
    361.         errorHandler(demangleMessage(message, Module.symbols), filename, lineno);
    362.       }).catch(function (error) {
    363.         errorHandler(message, filename, lineno);
    364.       });
    365.     }
    366.   }
    367.  
    368. #endif // SYMBOLS_FILENAME
    369.  
    370.   Module.abortHandler = function (message) {
    371. #if SYMBOLS_FILENAME
    372.     demanglingErrorHandler(message, "", 0);
    373. #else // SYMBOLS_FILENAME
    374.     errorHandler(message, "", 0);
    375. #endif // SYMBOLS_FILENAME
    376.     return true;
    377.   };
    378.  
    379.   Error.stackTraceLimit = Math.max(Error.stackTraceLimit || 0, 50);
    380.  
    381.   function progressUpdate(id, e) {
    382.     if (id == "symbolsUrl")
    383.       return;
    384.     var progress = Module.downloadProgress[id];
    385.     if (!progress)
    386.       progress = Module.downloadProgress[id] = {
    387.         started: false,
    388.         finished: false,
    389.         lengthComputable: false,
    390.         total: 0,
    391.         loaded: 0,
    392.       };
    393.     if (typeof e == "object" && (e.type == "progress" || e.type == "load")) {
    394.       if (!progress.started) {
    395.         progress.started = true;
    396.         progress.lengthComputable = e.lengthComputable;
    397.         progress.total = e.total;
    398.       }
    399.       progress.loaded = e.loaded;
    400.       if (e.type == "load")
    401.         progress.finished = true;
    402.     }
    403.     var loaded = 0, total = 0, started = 0, computable = 0, unfinishedNonComputable = 0;
    404.     for (var id in Module.downloadProgress) {
    405.       var progress = Module.downloadProgress[id];
    406.       if (!progress.started)
    407.         return 0;
    408.       started++;
    409.       if (progress.lengthComputable) {
    410.         loaded += progress.loaded;
    411.         total += progress.total;
    412.         computable++;
    413.       } else if (!progress.finished) {
    414.         unfinishedNonComputable++;
    415.       }
    416.     }
    417.     var totalProgress = started ? (started - unfinishedNonComputable - (total ? computable * (total - loaded) / total : 0)) / started : 0;
    418.     onProgress(0.9 * totalProgress);
    419.   }
    420.  
    421. #if USE_DATA_CACHING
    422.   {{{ read("XMLHttpRequest.js") }}}
    423. #endif // USE_DATA_CACHING
    424.  
    425. #if DECOMPRESSION_FALLBACK
    426.   var decompressors = {
    427. #if DECOMPRESSION_FALLBACK == "Gzip"
    428.     gzip: {
    429.       require: {{{ read("Gzip.js") }}},
    430.       decompress: function (data) {
    431.         if (!this.exports)
    432.           this.exports = this.require("inflate.js");
    433.         try { return this.exports.inflate(data) } catch (e) {};
    434.       },
    435.       hasUnityMarker: function (data) {
    436.         var commentOffset = 10, expectedComment = "UnityWeb Compressed Content (gzip)";
    437.         if (commentOffset > data.length || data[0] != 0x1F || data[1] != 0x8B)
    438.           return false;
    439.         var flags = data[3];
    440.         if (flags & 0x04) {
    441.           if (commentOffset + 2 > data.length)
    442.             return false;
    443.           commentOffset += 2 + data[commentOffset] + (data[commentOffset + 1] << 8);
    444.           if (commentOffset > data.length)
    445.             return false;
    446.         }
    447.         if (flags & 0x08) {
    448.           while (commentOffset < data.length && data[commentOffset])
    449.             commentOffset++;
    450.           if (commentOffset + 1 > data.length)
    451.             return false;
    452.           commentOffset++;
    453.         }
    454.         return (flags & 0x10) && String.fromCharCode.apply(null, data.subarray(commentOffset, commentOffset + expectedComment.length + 1)) == expectedComment + "\0";
    455.       },
    456.     },
    457. #endif // DECOMPRESSION_FALLBACK == "Gzip"
    458. #if DECOMPRESSION_FALLBACK == "Brotli"
    459.     br: {
    460.       require: {{{ read("Brotli.js") }}},
    461.       decompress: function (data) {
    462.         if (!this.exports)
    463.           this.exports = this.require("decompress.js");
    464.         try { return this.exports(data) } catch (e) {};
    465.       },
    466.       hasUnityMarker: function (data) {
    467.         var expectedComment = "UnityWeb Compressed Content (brotli)";
    468.         if (!data.length)
    469.           return false;
    470.         var WBITS_length = (data[0] & 0x01) ? (data[0] & 0x0E) ? 4 : 7 : 1,
    471.             WBITS = data[0] & ((1 << WBITS_length) - 1),
    472.             MSKIPBYTES = 1 + ((Math.log(expectedComment.length - 1) / Math.log(2)) >> 3);
    473.             commentOffset = (WBITS_length + 1 + 2 + 1 + 2 + (MSKIPBYTES << 3) + 7) >> 3;
    474.         if (WBITS == 0x11 || commentOffset > data.length)
    475.           return false;
    476.         var expectedCommentPrefix = WBITS + (((3 << 1) + (MSKIPBYTES << 4) + ((expectedComment.length - 1) << 6)) << WBITS_length);
    477.         for (var i = 0; i < commentOffset; i++, expectedCommentPrefix >>>= 8) {
    478.           if (data[i] != (expectedCommentPrefix & 0xFF))
    479.             return false;
    480.         }
    481.         return String.fromCharCode.apply(null, data.subarray(commentOffset, commentOffset + expectedComment.length)) == expectedComment;
    482.       },
    483.     },
    484. #endif // DECOMPRESSION_FALLBACK == "Brotli"
    485.   };
    486.  
    487.   function decompress(compressed, url, callback) {
    488.     for (var contentEncoding in decompressors) {
    489.       if (decompressors[contentEncoding].hasUnityMarker(compressed)) {
    490.         if (url)
    491.           console.log("You can reduce startup time if you configure your web server to add \"Content-Encoding: " + contentEncoding + "\" response header when serving \"" + url + "\" file.");
    492.         var decompressor = decompressors[contentEncoding];
    493.         if (!decompressor.worker) {
    494.           var workerUrl = URL.createObjectURL(new Blob(["this.require = ", decompressor.require.toString(), "; this.decompress = ", decompressor.decompress.toString(), "; this.onmessage = ", function (e) {
    495.             var data = { id: e.data.id, decompressed: this.decompress(e.data.compressed) };
    496.             postMessage(data, data.decompressed ? [data.decompressed.buffer] : []);
    497.           }.toString(), "; postMessage({ ready: true });"], { type: "application/javascript" }));
    498.           decompressor.worker = new Worker(workerUrl);
    499.           decompressor.worker.onmessage = function (e) {
    500.             if (e.data.ready) {
    501.               URL.revokeObjectURL(workerUrl);
    502.               return;
    503.             }
    504.             this.callbacks[e.data.id](e.data.decompressed);
    505.             delete this.callbacks[e.data.id];
    506.           };
    507.           decompressor.worker.callbacks = {};
    508.           decompressor.worker.nextCallbackId = 0;
    509.         }
    510.         var id = decompressor.worker.nextCallbackId++;
    511.         decompressor.worker.callbacks[id] = callback;
    512.         decompressor.worker.postMessage({id: id, compressed: compressed}, [compressed.buffer]);
    513.         return;
    514.       }
    515.     }
    516.     callback(compressed);
    517.   }
    518. #endif // DECOMPRESSION_FALLBACK
    519.  
    520.   function downloadBinary(urlId) {
    521.     return new Promise(function (resolve, reject) {
    522.       progressUpdate(urlId);
    523. #if USE_DATA_CACHING
    524.       var xhr = Module.companyName && Module.productName ? new Module.XMLHttpRequest({
    525.         companyName: Module.companyName,
    526.         productName: Module.productName,
    527.         cacheControl: Module.cacheControl(Module[urlId]),
    528.       }) : new XMLHttpRequest();
    529. #else // USE_DATA_CACHING
    530.       var xhr = new XMLHttpRequest();
    531. #endif // USE_DATA_CACHING
    532.       xhr.open("GET", Module[urlId]);
    533.       xhr.responseType = "arraybuffer";
    534.       xhr.addEventListener("progress", function (e) {
    535.         progressUpdate(urlId, e);
    536.       });
    537.       xhr.addEventListener("load", function(e) {
    538.         progressUpdate(urlId, e);
    539. #if DECOMPRESSION_FALLBACK
    540.         decompress(new Uint8Array(xhr.response), Module[urlId], resolve);
    541. #else // DECOMPRESSION_FALLBACK
    542.         resolve(new Uint8Array(xhr.response));
    543. #endif // DECOMPRESSION_FALLBACK
    544.       });
    545.  
    546.       xhr.addEventListener("error", function(e) {
    547.         var error = 'Failed to download file ' + Module[urlId]
    548.         if (location.protocol == 'file:') {
    549.           showBanner(error + '. Loading web pages via a file:// URL without a web server is not supported by this browser. Please use a local development web server to host Unity content, or use the Unity Build and Run option.', 'error');
    550.         } else {
    551.           console.error(error);
    552.         }
    553.       });
    554.  
    555.       xhr.send();
    556.     });
    557.   }
    558.  
    559.   function downloadFramework() {
    560. #if DECOMPRESSION_FALLBACK
    561.     return downloadBinary("frameworkUrl").then(function (code) {
    562.       var blobUrl = URL.createObjectURL(new Blob([code], { type: "application/javascript" }));
    563. #if USE_THREADS
    564.       Module.frameworkBlobUrl = blobUrl;
    565. #endif // USE_THREADS
    566. #endif // DECOMPRESSION_FALLBACK
    567.       return new Promise(function (resolve, reject) {
    568.         var script = document.createElement("script");
    569. #if DECOMPRESSION_FALLBACK
    570.         script.src = blobUrl;
    571. #else // DECOMPRESSION_FALLBACK
    572.         script.src = Module.frameworkUrl;
    573. #endif // DECOMPRESSION_FALLBACK
    574.         script.onload = function () {
    575.           // Adding the framework.js script to DOM created a global
    576.           // 'unityFramework' variable that should be considered internal.
    577.           // If not, then we have received a malformed file.
    578.           if (typeof unityFramework === 'undefined' || !unityFramework) {
    579.             var compressions = [['br', 'br'], ['gz', 'gzip']];
    580.             for(var i in compressions) {
    581.               var compression = compressions[i];
    582.               if (Module.frameworkUrl.endsWith('.' + compression[0])) {
    583.                 var error = 'Unable to parse ' + Module.frameworkUrl + '!';
    584.                 if (location.protocol == 'file:') {
    585.                   showBanner(error + ' Loading pre-compressed (brotli or gzip) content via a file:// URL without a web server is not supported by this browser. Please use a local development web server to host compressed Unity content, or use the Unity Build and Run option.', 'error');
    586.                   return;
    587.                 }
    588.                 error += ' This can happen if build compression was enabled but web server hosting the content was misconfigured to not serve the file with HTTP Response Header "Content-Encoding: ' + compression[1] + '" present. Check browser Console and Devtools Network tab to debug.';
    589.                 if (compression[0] == 'br') {
    590.                   if (location.protocol == 'http:') {
    591.                     var migrationHelp = ['localhost', '127.0.0.1'].indexOf(location.hostname) != -1 ? '' : 'Migrate your server to use HTTPS.'
    592.                     if (/Firefox/.test(navigator.userAgent)) error = 'Unable to parse ' + Module.frameworkUrl + '!<br>If using custom web server, verify that web server is sending .br files with HTTP Response Header "Content-Encoding: br". Brotli compression may not be supported in Firefox over HTTP connections. ' + migrationHelp + ' See <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=1670675">https://bugzilla.mozilla.org/show_bug.cgi?id=1670675</a> for more information.';
    593.                     else error = 'Unable to parse ' + Module.frameworkUrl + '!<br>If using custom web server, verify that web server is sending .br files with HTTP Response Header "Content-Encoding: br". Brotli compression may not be supported over HTTP connections. Migrate your server to use HTTPS.';
    594.                   }
    595.                 }
    596.                 showBanner(error, 'error');
    597.                 return;
    598.               }
    599.             };
    600.             showBanner('Unable to parse ' + Module.frameworkUrl + '! The file is corrupt, or compression was misconfigured? (check Content-Encoding HTTP Response Header on web server)', 'error');
    601.           }
    602.  
    603.           // Capture the variable to local scope and clear it from global
    604.           // scope so that JS garbage collection can take place on
    605.           // application quit.
    606.           var fw = unityFramework;
    607.           unityFramework = null;
    608.           // Also ensure this function will not hold any JS scope
    609.           // references to prevent JS garbage collection.
    610.           script.onload = null;
    611. #if DECOMPRESSION_FALLBACK && !USE_THREADS
    612.           URL.revokeObjectURL(blobUrl);
    613. #endif // DECOMPRESSION_FALLBACK && !USE_THREADS
    614.           resolve(fw);
    615.         }
    616.         script.onerror = function(e) {
    617.           showBanner('Unable to load file ' + Module.frameworkUrl + '! Check that the file exists on the remote server. (also check browser Console and Devtools Network tab to debug)', 'error');
    618.         }
    619.         document.body.appendChild(script);
    620.         Module.deinitializers.push(function() {
    621.           document.body.removeChild(script);
    622.         });
    623.       });
    624. #if DECOMPRESSION_FALLBACK
    625.     });
    626. #endif // DECOMPRESSION_FALLBACK
    627.   }
    628.  
    629. #if !USE_WASM
    630.   function downloadAsm() {
    631. #if DECOMPRESSION_FALLBACK
    632.     return downloadBinary("codeUrl").then(function (code) {
    633.       var blobUrl = URL.createObjectURL(new Blob([code], { type: "application/javascript" }));
    634. #endif // DECOMPRESSION_FALLBACK
    635.       return new Promise(function (resolve, reject) {
    636.         var script = document.createElement("script");
    637. #if DECOMPRESSION_FALLBACK
    638.         script.src = blobUrl;
    639. #else // DECOMPRESSION_FALLBACK
    640.         script.src = Module.codeUrl;
    641. #endif // DECOMPRESSION_FALLBACK
    642. #if USE_THREADS
    643.         Module.asmJsUrlOrBlob = script.src;
    644. #endif // USE_THREADS
    645.         script.onload = function () {
    646.           delete script.onload;
    647. #if DECOMPRESSION_FALLBACK && !USE_THREADS
    648.           URL.revokeObjectURL(blobUrl);
    649. #endif // DECOMPRESSION_FALLBACK && !USE_THREADS
    650.           resolve();
    651.         }
    652.         document.body.appendChild(script);
    653.         Module.deinitializers.push(function() {
    654.           document.body.removeChild(script);
    655.         });
    656.       });
    657. #if DECOMPRESSION_FALLBACK
    658.     });
    659. #endif // DECOMPRESSION_FALLBACK
    660.   }
    661.  
    662. #endif // !USE_WASM
    663.   function loadBuild() {
    664. #if USE_WASM
    665. #if DECOMPRESSION_FALLBACK
    666.     Promise.all([
    667.       downloadFramework(),
    668.       downloadBinary("codeUrl"),
    669.     ]).then(function (results) {
    670.       Module.wasmBinary = results[1];
    671.       results[0](Module);
    672.     });
    673.  
    674. #else // DECOMPRESSION_FALLBACK
    675.     downloadFramework().then(function (unityFramework) {
    676.       unityFramework(Module);
    677.     });
    678.  
    679. #endif // DECOMPRESSION_FALLBACK
    680. #else // USE_WASM
    681.     Promise.all([
    682.       downloadFramework(),
    683.       downloadAsm(),
    684.     ]).then(function (results) {
    685.       results[0](Module);
    686.     });
    687.  
    688. #endif // USE_WASM
    689. #if MEMORY_FILENAME
    690.     Module.memoryInitializerRequest = {
    691.       addEventListener: function (type, listener) {
    692.         if (type == "load")
    693.           Module.memoryInitializerRequest.useRequest = listener;
    694.       },
    695.     };
    696.     downloadBinary("memoryUrl").then(function (data) {
    697.       Module.memoryInitializerRequest.status = 200;
    698.       Module.memoryInitializerRequest.response = data;
    699.       if (Module.memoryInitializerRequest.useRequest)
    700.         Module.memoryInitializerRequest.useRequest();
    701.     });
    702.  
    703. #endif // MEMORY_FILENAME
    704.     var dataPromise = downloadBinary("dataUrl");
    705.     Module.preRun.push(function () {
    706.       Module.addRunDependency("dataUrl");
    707.       dataPromise.then(function (data) {
    708.         var view = new DataView(data.buffer, data.byteOffset, data.byteLength);
    709.         var pos = 0;
    710.         var prefix = "UnityWebData1.0\0";
    711.         if (!String.fromCharCode.apply(null, data.subarray(pos, pos + prefix.length)) == prefix)
    712.           throw "unknown data format";
    713.         pos += prefix.length;
    714.         var headerSize = view.getUint32(pos, true); pos += 4;
    715.         while (pos < headerSize) {
    716.           var offset = view.getUint32(pos, true); pos += 4;
    717.           var size = view.getUint32(pos, true); pos += 4;
    718.           var pathLength = view.getUint32(pos, true); pos += 4;
    719.           var path = String.fromCharCode.apply(null, data.subarray(pos, pos + pathLength)); pos += pathLength;
    720.           for (var folder = 0, folderNext = path.indexOf("/", folder) + 1 ; folderNext > 0; folder = folderNext, folderNext = path.indexOf("/", folder) + 1)
    721.             Module.FS_createPath(path.substring(0, folder), path.substring(folder, folderNext - 1), true, true);
    722.           Module.FS_createDataFile(path, null, data.subarray(offset, offset + size), true, true, true);
    723.         }
    724.         Module.removeRunDependency("dataUrl");
    725.       });
    726.     });
    727.   }
    728.  
    729.   return new Promise(function (resolve, reject) {
    730.     if (!Module.SystemInfo.hasWebGL) {
    731.       reject("Your browser does not support WebGL.");
    732.   #if !USE_WEBGL_1_0
    733.     } else if (Module.SystemInfo.hasWebGL == 1) {
    734.       reject("Your browser does not support graphics API \"WebGL 2\" which is required for this content.");
    735.   #endif // !USE_WEBGL_1_0
    736.   #if USE_WASM
    737.     } else if (!Module.SystemInfo.hasWasm) {
    738.       reject("Your browser does not support WebAssembly.");
    739.   #endif // USE_WASM
    740.   #if USE_THREADS
    741.     } else if (!Module.SystemInfo.hasThreads) {
    742.       reject("Your browser does not support multithreading.");
    743.   #endif // USE_THREADS
    744.     } else {
    745.   #if USE_WEBGL_2_0
    746.       if (Module.SystemInfo.hasWebGL == 1)
    747.         Module.print("Warning: Your browser does not support \"WebGL 2\" Graphics API, switching to \"WebGL 1\"");
    748.   #endif // USE_WEBGL_2_0
    749.       Module.startupErrorHandler = reject;
    750.       onProgress(0);
    751.       Module.postRun.push(function () {
    752.         onProgress(1);
    753.         delete Module.startupErrorHandler;
    754.         resolve(unityInstance);
    755.       });
    756.       loadBuild();
    757.     }
    758.   });
    759. }
    760.  
     
  6. Robert-UA

    Robert-UA

    Joined:
    Aug 19, 2021
    Posts:
    11
  7. Robert-UA

    Robert-UA

    Joined:
    Aug 19, 2021
    Posts:
    11
    My solution (for now) is to rewrite some UnityLoader.js logic, namely remove the #if DECOMPRESSION_FALLBACK checks in the methods corresponding to loading the relevant files.

    As it was already described above, when the Decompression Fallback parameter was turned on, caching in IndexedDB worked. That's why I turned it off and removed checks on it where I "don't need it". Tested and did not notice that my changes created other problems.

    Still wondering if it is possible to cache not the .wasm and .asm.js files, but rather their "compiled output", if there is such a thing and it is possible to implement.

    Code (JavaScript):
    1.   // function downloadFramework() {
    2.   //   #if DECOMPRESSION_FALLBACK
    3.   //       return downloadBinary("frameworkUrl").then(function (code) {
    4.   //         var blobUrl = URL.createObjectURL(new Blob([code], { type: "application/javascript" }));
    5.   //   #if USE_THREADS
    6.   //         Module.frameworkBlobUrl = blobUrl;
    7.   //   #endif // USE_THREADS
    8.   //   #endif // DECOMPRESSION_FALLBACK
    9.   //         return new Promise(function (resolve, reject) {
    10.   //           var script = document.createElement("script");
    11.   //   #if DECOMPRESSION_FALLBACK
    12.   //           script.src = blobUrl;
    13.   //   #else // DECOMPRESSION_FALLBACK
    14.   //           script.src = Module.frameworkUrl;
    15.   //   #endif // DECOMPRESSION_FALLBACK
    16.   //           script.onload = function () {
    17.   //             // Adding the framework.js script to DOM created a global
    18.   //             // 'unityFramework' variable that should be considered internal.
    19.   //             // If not, then we have received a malformed file.
    20.   //             if (typeof unityFramework === 'undefined' || !unityFramework) {
    21.   //               var compressions = [['br', 'br'], ['gz', 'gzip']];
    22.   //               for(var i in compressions) {
    23.   //                 var compression = compressions[i];
    24.   //                 if (Module.frameworkUrl.endsWith('.' + compression[0])) {
    25.   //                   var error = 'Unable to parse ' + Module.frameworkUrl + '!';
    26.   //                   if (location.protocol == 'file:') {
    27.   //                     showBanner(error + ' Loading pre-compressed (brotli or gzip) content via a file:// URL without a web server is not supported by this browser. Please use a local development web server to host compressed Unity content, or use the Unity Build and Run option.', 'error');
    28.   //                     return;
    29.   //                   }
    30.   //                   error += ' This can happen if build compression was enabled but web server hosting the content was misconfigured to not serve the file with HTTP Response Header "Content-Encoding: ' + compression[1] + '" present. Check browser Console and Devtools Network tab to debug.';
    31.   //                   if (compression[0] == 'br') {
    32.   //                     if (location.protocol == 'http:') {
    33.   //                       var migrationHelp = ['localhost', '127.0.0.1'].indexOf(location.hostname) != -1 ? '' : 'Migrate your server to use HTTPS.'
    34.   //                       if (/Firefox/.test(navigator.userAgent)) error = 'Unable to parse ' + Module.frameworkUrl + '!<br>If using custom web server, verify that web server is sending .br files with HTTP Response Header "Content-Encoding: br". Brotli compression may not be supported in Firefox over HTTP connections. ' + migrationHelp + ' See <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=1670675">https://bugzilla.mozilla.org/show_bug.cgi?id=1670675</a> for more information.';
    35.   //                       else error = 'Unable to parse ' + Module.frameworkUrl + '!<br>If using custom web server, verify that web server is sending .br files with HTTP Response Header "Content-Encoding: br". Brotli compression may not be supported over HTTP connections. Migrate your server to use HTTPS.';
    36.   //                     }
    37.   //                   }
    38.   //                   showBanner(error, 'error');
    39.   //                   return;
    40.   //                 }
    41.   //               };
    42.   //               showBanner('Unable to parse ' + Module.frameworkUrl + '! The file is corrupt, or compression was misconfigured? (check Content-Encoding HTTP Response Header on web server)', 'error');
    43.   //             }
    44.    
    45.   //             // Capture the variable to local scope and clear it from global
    46.   //             // scope so that JS garbage collection can take place on
    47.   //             // application quit.
    48.   //             var fw = unityFramework;
    49.   //             unityFramework = null;
    50.   //             // Also ensure this function will not hold any JS scope
    51.   //             // references to prevent JS garbage collection.
    52.   //             script.onload = null;
    53.   //   #if DECOMPRESSION_FALLBACK && !USE_THREADS
    54.   //             URL.revokeObjectURL(blobUrl);
    55.   //   #endif // DECOMPRESSION_FALLBACK && !USE_THREADS
    56.   //             resolve(fw);
    57.   //           }
    58.   //           script.onerror = function(e) {
    59.   //             showBanner('Unable to load file ' + Module.frameworkUrl + '! Check that the file exists on the remote server. (also check browser Console and Devtools Network tab to debug)', 'error');
    60.   //           }
    61.   //           document.body.appendChild(script);
    62.   //           Module.deinitializers.push(function() {
    63.   //             document.body.removeChild(script);
    64.   //           });
    65.   //         });
    66.   //   #if DECOMPRESSION_FALLBACK
    67.   //       });
    68.   //   #endif // DECOMPRESSION_FALLBACK
    69.   //     }
    70.  
    71.   function downloadFramework() {
    72.     return downloadBinary("frameworkUrl").then(function (code) {
    73.       var blobUrl = URL.createObjectURL(new Blob([code], { type: "application/javascript" }));
    74. #if USE_THREADS
    75.       Module.frameworkBlobUrl = blobUrl;
    76. #endif // USE_THREADS
    77.       return new Promise(function (resolve, reject) {
    78.         var script = document.createElement("script");
    79.         script.src = blobUrl;
    80.         script.onload = function () {
    81.           // Adding the framework.js script to DOM created a global
    82.           // 'unityFramework' variable that should be considered internal.
    83.           // If not, then we have received a malformed file.
    84.           if (typeof unityFramework === 'undefined' || !unityFramework) {
    85.             var compressions = [['br', 'br'], ['gz', 'gzip']];
    86.             for(var i in compressions) {
    87.               var compression = compressions[i];
    88.               if (Module.frameworkUrl.endsWith('.' + compression[0])) {
    89.                 var error = 'Unable to parse ' + Module.frameworkUrl + '!';
    90.                 if (location.protocol == 'file:') {
    91.                   showBanner(error + ' Loading pre-compressed (brotli or gzip) content via a file:// URL without a web server is not supported by this browser. Please use a local development web server to host compressed Unity content, or use the Unity Build and Run option.', 'error');
    92.                   return;
    93.                 }
    94.                 error += ' This can happen if build compression was enabled but web server hosting the content was misconfigured to not serve the file with HTTP Response Header "Content-Encoding: ' + compression[1] + '" present. Check browser Console and Devtools Network tab to debug.';
    95.                 if (compression[0] == 'br') {
    96.                   if (location.protocol == 'http:') {
    97.                     var migrationHelp = ['localhost', '127.0.0.1'].indexOf(location.hostname) != -1 ? '' : 'Migrate your server to use HTTPS.'
    98.                     if (/Firefox/.test(navigator.userAgent)) error = 'Unable to parse ' + Module.frameworkUrl + '!<br>If using custom web server, verify that web server is sending .br files with HTTP Response Header "Content-Encoding: br". Brotli compression may not be supported in Firefox over HTTP connections. ' + migrationHelp + ' See <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=1670675">https://bugzilla.mozilla.org/show_bug.cgi?id=1670675</a> for more information.';
    99.                     else error = 'Unable to parse ' + Module.frameworkUrl + '!<br>If using custom web server, verify that web server is sending .br files with HTTP Response Header "Content-Encoding: br". Brotli compression may not be supported over HTTP connections. Migrate your server to use HTTPS.';
    100.                   }
    101.                 }
    102.                 showBanner(error, 'error');
    103.                 return;
    104.               }
    105.             };
    106.             showBanner('Unable to parse ' + Module.frameworkUrl + '! The file is corrupt, or compression was misconfigured? (check Content-Encoding HTTP Response Header on web server)', 'error');
    107.           }
    108.  
    109.           // Capture the variable to local scope and clear it from global
    110.           // scope so that JS garbage collection can take place on
    111.           // application quit.
    112.           var fw = unityFramework;
    113.           unityFramework = null;
    114.           // Also ensure this function will not hold any JS scope
    115.           // references to prevent JS garbage collection.
    116.           script.onload = null;
    117. #if !USE_THREADS
    118.           URL.revokeObjectURL(blobUrl);
    119. #endif // !USE_THREADS
    120.           resolve(fw);
    121.         }
    122.         script.onerror = function(e) {
    123.           showBanner('Unable to load file ' + Module.frameworkUrl + '! Check that the file exists on the remote server. (also check browser Console and Devtools Network tab to debug)', 'error');
    124.         }
    125.         document.body.appendChild(script);
    126.         Module.deinitializers.push(function() {
    127.           document.body.removeChild(script);
    128.         });
    129.       });
    130.     });
    131.   }
    132.  
    133. // #if !USE_WASM
    134. //   function downloadAsm() {
    135. // #if DECOMPRESSION_FALLBACK
    136. //     return downloadBinary("codeUrl").then(function (code) {
    137. //       var blobUrl = URL.createObjectURL(new Blob([code], { type: "application/javascript" }));
    138. // #endif // DECOMPRESSION_FALLBACK
    139. //       return new Promise(function (resolve, reject) {
    140. //         var script = document.createElement("script");
    141. // #if DECOMPRESSION_FALLBACK
    142. //         script.src = blobUrl;
    143. // #else // DECOMPRESSION_FALLBACK
    144. //         script.src = Module.codeUrl;
    145. // #endif // DECOMPRESSION_FALLBACK
    146. // #if USE_THREADS
    147. //         Module.asmJsUrlOrBlob = script.src;
    148. // #endif // USE_THREADS
    149. //         script.onload = function () {
    150. //           delete script.onload;
    151. // #if DECOMPRESSION_FALLBACK && !USE_THREADS
    152. //           URL.revokeObjectURL(blobUrl);
    153. // #endif // DECOMPRESSION_FALLBACK && !USE_THREADS
    154. //           resolve();
    155. //         }
    156. //         document.body.appendChild(script);
    157. //         Module.deinitializers.push(function() {
    158. //           document.body.removeChild(script);
    159. //         });
    160. //       });
    161. // #if DECOMPRESSION_FALLBACK
    162. //     });
    163. // #endif // DECOMPRESSION_FALLBACK
    164. //   }
    165. // #endif // !USE_WASM
    166.  
    167.  
    168. #if !USE_WASM
    169.   function downloadAsm() {
    170.     return downloadBinary("codeUrl").then(function (code) {
    171.       var blobUrl = URL.createObjectURL(new Blob([code], { type: "application/javascript" }));
    172.       return new Promise(function (resolve, reject) {
    173.         var script = document.createElement("script");
    174.         script.src = blobUrl;
    175. #if USE_THREADS
    176.         Module.asmJsUrlOrBlob = script.src;
    177. #endif // USE_THREADS
    178.         script.onload = function () {
    179.           delete script.onload;
    180. #if !USE_THREADS
    181.           URL.revokeObjectURL(blobUrl);
    182. #endif // !USE_THREADS // DECOMPRESSION_FALLBACK && !USE_THREADS
    183.           resolve();
    184.         }
    185.         document.body.appendChild(script);
    186.         Module.deinitializers.push(function() {
    187.           document.body.removeChild(script);
    188.         });
    189.       });
    190.     });
    191.   }
    192. #endif // !USE_WASM
    193.  
    194.  
    195.   function loadBuild() {
    196.     console.log("loadBuild()");
    197. #if USE_WASM
    198. // #if DECOMPRESSION_FALLBACK
    199. //     Promise.all([
    200. //       downloadFramework(),
    201. //       downloadBinary("codeUrl"),
    202. //     ]).then(function (results) {
    203. //       Module.wasmBinary = results[1];
    204. //       results[0](Module);
    205. //     });
    206.  
    207. // #else // DECOMPRESSION_FALLBACK
    208. //     downloadFramework().then(function (unityFramework) {
    209. //       unityFramework(Module);
    210. //     });
    211.  
    212. // #endif // DECOMPRESSION_FALLBACK
    213.     Promise.all([
    214.       downloadFramework(),
    215.       downloadBinary("codeUrl"),
    216.     ]).then(function (results) {
    217.       Module.wasmBinary = results[1];
    218.       results[0](Module);
    219.     });
    220. #else // USE_WASM
    221.     Promise.all([
    222.       downloadFramework(),
    223.       downloadAsm(),
    224.     ]).then(function (results) {
    225.       results[0](Module);
    226.     });
    227. #endif // USE_WASM
     
  8. unity_2D833335567EB140E14B

    unity_2D833335567EB140E14B

    Joined:
    Jul 28, 2023
    Posts:
    2
    We have been looking into the lack of caching for .wasm files as well. Is this last post of yours a functional solution? or are you just following the thread you are on.
     
    Robert-UA likes this.
  9. Robert-UA

    Robert-UA

    Joined:
    Aug 19, 2021
    Posts:
    11
    Not as much as I hoped, because the first run does not use instantiateStreaming (so it turns out that I actually enabled Decompression Fallback without enabling it)

    Therefore, now the hope is to disable this parameter and process (rewrite the logic) precisely at the level of the integrateWasmJS() method

    This is all relevant only when you cannot configure headers on the server or native caching is not available due to the specifics of the platform (as in my case - clearing the cache at each individual launch of the web application on the TV).

    So if your goal is not as specific as mine - you just need to configure the headers for the files, depending on where you host the files, it can be different, in my case I use https://www.netlify.com/ and add rules for headers using the netlify.toml file with the content below.

    Cache-Control = "public, max-age=31536000" - for browser caching (that is what your want)

    Please note that the last rules cannot be universal for the option when you use Decompression Fallback - because the extensions (.unityweb) for gzip and br are the same - so change the Content-Encoding for them on the server according to your settings.

    If it works, you'll see "cached from disk" next to your files in the Network panel of dev browser tools (F12).

    Code (CSharp):
    1.  
    2. [[headers]]
    3.   for = "/*.mem"
    4.   [headers.values]
    5.     Content-Type = "application/octet-stream"
    6.     Cache-Control = "public, max-age=31536000"
    7.  
    8. [[headers]]
    9.   for = "/*.mem.br"
    10.   [headers.values]
    11.     Content-Encoding = "br"
    12.     Content-Type = "application/octet-stream"
    13.     Cache-Control = "public, max-age=31536000"
    14.  
    15. [[headers]]
    16.   for = "/*.mem.gz"
    17.   [headers.values]
    18.     Content-Encoding = "gzip"
    19.     Content-Type = "application/octet-stream"
    20.     Cache-Control = "public, max-age=31536000"
    21.  
    22. [[headers]]
    23.   for = "/*.data"
    24.   [headers.values]
    25.     Content-Type = "application/octet-stream"
    26.     Cache-Control = "public, max-age=31536000"
    27.  
    28. [[headers]]
    29.   for = "/*.data.br"
    30.   [headers.values]
    31.     Content-Encoding = "br"
    32.     Content-Type = "application/octet-stream"
    33.     Cache-Control = "public, max-age=31536000"
    34.  
    35. [[headers]]
    36.   for = "/*.data.gz"
    37.   [headers.values]
    38.     Content-Encoding = "gzip"
    39.     Content-Type = "application/octet-stream"
    40.     Cache-Control = "public, max-age=31536000"
    41.  
    42. [[headers]]
    43.   for = "/*.symbols.json.br"
    44.   [headers.values]
    45.     Content-Encoding = "br"
    46.     Content-Type = "application/octet-stream"
    47.  
    48. [[headers]]
    49.   for = "/*.symbols.json.gz"
    50.   [headers.values]
    51.     Content-Encoding = "gzip"
    52.     Content-Type = "application/octet-stream"
    53.  
    54. [[headers]]
    55.   for = "/*.js.br"
    56.   [headers.values]
    57.     Content-Encoding = "br"
    58.     Content-Type = "application/javascript"
    59.  
    60. [[headers]]
    61.   for = "/*.js.gz"
    62.   [headers.values]
    63.     Content-Encoding = "gzip"
    64.     Content-Type = "application/javascript"
    65.  
    66. [[headers]]
    67.   for = "/*.framework.js.br"
    68.   [headers.values]
    69.     Content-Encoding = "br"
    70.     Content-Type = "application/javascript"
    71.  
    72. [[headers]]
    73.   for = "/*.framework.js.gz"
    74.   [headers.values]
    75.     Content-Encoding = "gzip"
    76.     Content-Type = "application/javascript"
    77.  
    78. [[headers]]
    79.   for = "/*.asm.js"
    80.   [headers.values]
    81.     Content-Type = "application/javascript"
    82.     Cache-Control = "public, max-age=31536000"
    83.  
    84. [[headers]]
    85.   for = "/*.asm.js.gz"
    86.   [headers.values]
    87.     Content-Encoding = "gzip"
    88.     Content-Type = "application/javascript"
    89.     Cache-Control = "public, max-age=31536000"
    90.  
    91. [[headers]]
    92.   for = "/*.asm.js.br"
    93.   [headers.values]
    94.     Content-Encoding = "br"
    95.     Content-Type = "application/javascript"
    96.     Cache-Control = "public, max-age=31536000"
    97.  
    98. [[headers]]
    99.   for = "/*.wasm"
    100.   [headers.values]
    101.     Content-Type = "application/wasm"
    102.     Cache-Control = "public, max-age=31536000"
    103.  
    104. [[headers]]
    105.   for = "/*.wasm.br"
    106.   [headers.values]
    107.     Content-Encoding = "br"
    108.     Content-Type = "application/wasm"
    109.     Cache-Control = "public, max-age=31536000"
    110.  
    111. [[headers]]
    112.   for = "/*.wasm.gz"
    113.   [headers.values]
    114.     Content-Encoding = "gzip"
    115.     Content-Type = "application/wasm"
    116.     Cache-Control = "public, max-age=31536000"
    117.  
    118.  
    119.  
    120. [[headers]]
    121.   for = "/*.wasm.unityweb"
    122.   [headers.values]
    123.     Content-Encoding = "gzip"
    124.     Content-Type = "application/wasm"
    125.     Cache-Control = "public, max-age=31536000"
    126.  
    127. [[headers]]
    128.   for = "/*.data.unityweb"
    129.   [headers.values]
    130.     Content-Encoding = "gzip"
    131.     Content-Type = "application/octet-stream"
    132.     Cache-Control = "public, max-age=31536000"
    133.  
    134. [[headers]]
    135.   for = "/*.js.unityweb"
    136.   [headers.values]
    137.     Content-Encoding = "gzip"
    138.     Content-Type = "application/javascript"
    139.     Cache-Control = "public, max-age=31536000"
    140.  
    141. [[headers]]
    142.   for = "/*.mem.unityweb"
    143.   [headers.values]
    144.     Content-Encoding = "gzip"
    145.     Content-Type = "application/octet-stream"
    146.     Cache-Control = "public, max-age=31536000"
    147.  
    148. [[headers]]
    149.   for = "/*.symbols.json.unityweb"
    150.   [headers.values]
    151.     Content-Encoding = "gzip"
    152.     Content-Type = "application/octet-stream"
     
    So_What likes this.