forked from Orillusion/orillusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoaderBase.ts
179 lines (162 loc) · 5.88 KB
/
LoaderBase.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { Engine3D } from '../Engine3D';
import { BitmapTexture2D } from '../textures/BitmapTexture2D';
import { StringUtil } from '../util/StringUtil';
import { LoaderFunctions } from './LoaderFunctions';
/**
* @internal
* @group Loader
*/
export class LoaderBase {
public baseUrl: string = '';
public initUrl: string;
private _progress: number = 0;
constructor() {
}
/**
* @private
*/
public async loadBinData(url: string, loaderFunctions?: LoaderFunctions): Promise<any> {
this.baseUrl = StringUtil.getPath(url);
this.initUrl = url;
return new Promise(async (succ, fail) => {
fetch(url, { headers: loaderFunctions?.headers })
.then(async (response) => {
if (response.ok) {
let chunks = await LoaderBase.read(url, response, loaderFunctions);
let buffer = chunks.buffer;
chunks = null;
succ(buffer);
}
else {
throw Error("request rejected with status " + response.status)
}
})
.catch((e) => {
if (loaderFunctions.onError) {
loaderFunctions.onError(e);
}
fail(e);
});
});
}
/**
*
* @private
*/
public async loadAsyncBitmapTexture(url: string, loaderFunctions?: LoaderFunctions) {
this.baseUrl = StringUtil.getPath(url);
this.initUrl = url;
let bitmapTexture = new BitmapTexture2D();
bitmapTexture.url = url;
bitmapTexture.name = StringUtil.getURLName(url);
await bitmapTexture.load(url, loaderFunctions);
Engine3D.res.addTexture(url, bitmapTexture);
return bitmapTexture;
}
/**
*
* @private
*/
public async loadJson(url: string, loaderFunctions?: LoaderFunctions): Promise<object> {
this.baseUrl = StringUtil.getPath(url);
this.initUrl = url;
return new Promise(async (succ, fail) => {
fetch(url, { headers: loaderFunctions?.headers })
.then(async (response) => {
if (response.ok) {
let chunks = await LoaderBase.read(url, response, loaderFunctions);
let utf8decoder = new TextDecoder('utf-8');
const jsonString = utf8decoder.decode(chunks);
chunks = null;
succ(JSON.parse(jsonString));
}
else {
throw Error("request rejected with status" + response.status)
}
})
.catch((e) => {
if (loaderFunctions.onError) {
loaderFunctions.onError(e);
}
fail(e);
});
});
}
/**
* @private
*/
public async loadTxt(url: string, loaderFunctions?: LoaderFunctions): Promise<object> {
this.baseUrl = StringUtil.getPath(url);
return new Promise(async (succ, fail) => {
fetch(url)
.then(async (response) => {
if (response.ok) {
let chunks = await LoaderBase.read(url, response, loaderFunctions);
let utf8decoder = new TextDecoder('utf-8');
const textString = utf8decoder.decode(chunks);
chunks = null;
succ({ data: textString });
}
else {
throw Error("request rejected with status" + response.status)
}
})
.catch((e) => {
if (loaderFunctions.onError) {
loaderFunctions.onError(e);
}
fail(e);
});
});
}
/**
* @private
*/
public static async read(url: string, response, loaderFunctions?: LoaderFunctions): Promise<Uint8Array> {
const reader = response.body.getReader();
const contentLength = +response.headers.get('Content-Length');
let receivedLength = 0;
let chunks = [];
let receivedArr = [];
while (true) {
const { done, value } = await reader.read();
if (done) {
if (contentLength > 0) {
if (loaderFunctions && loaderFunctions.onComplete) {
loaderFunctions.onComplete.call(this, url);
}
}
break;
}
chunks.push(value);
receivedLength += value.length;
if (contentLength > 0) {
if (loaderFunctions && loaderFunctions.onProgress) {
loaderFunctions.onProgress.call(this, receivedLength, contentLength, url);
}
} else {
receivedArr.push(value.length);
}
}
if (receivedArr.length > 0) {
for (let i = 0; i < chunks.length; i++) {
console.log(receivedArr[i]);
if (loaderFunctions && loaderFunctions.onProgress) {
loaderFunctions.onProgress.call(this, receivedArr[i], receivedLength, url);
}
if (receivedArr[i] == receivedLength) {
if (loaderFunctions && loaderFunctions.onComplete) {
loaderFunctions.onComplete.call(this, url);
}
}
}
}
let chunksAll = new Uint8Array(receivedLength);
let position = 0;
for (let chunk of chunks) {
chunksAll.set(chunk, position);
position += chunk.length;
}
return chunksAll;
}
}