Webclient: Get 'download size for texture packs' to work in firefox for some sites at least

This commit is contained in:
UnknownShadow200 2020-12-16 00:06:04 +11:00
parent adb314458e
commit 408cb1726e
3 changed files with 14 additions and 26 deletions

View File

@ -65,24 +65,3 @@ and change to
```
eventHandler.target.addEventListener(eventHandler.eventTypeString, jsEventHandler, { useCapture: eventHandler.useCapture, passive: false });
```
#### Texture pack confirm dialog always shows *Download size: Determining*..
Unfortunately emscripten doesn't store content-length for HEAD http requests. This can be fixed.
First you need to find at what offset emscripten stores content-length. Look for something like:
```
xhr.onprogress = function(e) {
...
Fetch.setu64(fetch + 32, e.total);
```
then look for
```
xhr.onload = function(e) {
...
```
and finally change that to
```
xhr.onload = function(e) {
Fetch.setu64(fetch + 32, e.total);
...
```

View File

@ -124,7 +124,7 @@ NOTE: You have to change entry->d_type == DT_DIR to Directory_Exists(&path) (TOD
#### Web
```emcc *.c -s FETCH=1 -s ALLOW_MEMORY_GROWTH=1 --preload-file texpacks/default.zip```
```emcc *.c -s ALLOW_MEMORY_GROWTH=1 --preload-file texpacks/default.zip```
The generated javascript file has some issues. [See here for how to fix](doc/compile-fixes.md#webclient-patches)

View File

@ -354,17 +354,26 @@ static void Http_DownloadAsync(struct HttpRequest* req) {
var xhr = new XMLHttpRequest();
xhr.open(reqMethod, url);
xhr.responseType = 'arraybuffer';
var getContentLength = function(e) {
if (e.total) return e.total;
try {
var len = xhr.getResponseHeader('Content-Length');
return parseInt(len, 10);
} catch (ex) { return 0; }
};
xhr.onload = function(e) {
var src = new Uint8Array(xhr.response);
var len = src.byteLength;
var data = _malloc(len);
HEAPU8.set(src, data);
onFinished(data, len || e.total, xhr.status);
onFinished(data, len || getContentLength(e), xhr.status);
};
xhr.onerror = function(e) { onFinished(0, 0, xhr.status); };
xhr.ontimeout = function(e) { onFinished(0, 0, xhr.status); };
xhr.onprogress = function(e) { onProgress(e.loaded, e.total); };
xhr.onerror = function(e) { onFinished(0, 0, xhr.status); };
xhr.ontimeout = function(e) { onFinished(0, 0, xhr.status); };
xhr.onprogress = function(e) { onProgress(e.loaded, e.total); };
try { xhr.send(); } catch (e) { onFinished(0, 0, 0); }
}, urlStr, req->requestType, OnFinishedAsync, OnUpdateProgress);