-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch.js
More file actions
161 lines (158 loc) · 9.1 KB
/
patch.js
File metadata and controls
161 lines (158 loc) · 9.1 KB
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
(function() {
let lastLoggedUrl = null;
function decodeUrlIfNeeded(url) {
if (typeof url !== 'string' || !url.includes('%')) return url;
try {
const decodedUrl = decodeURIComponent(url);
if (decodedUrl !== url && url !== lastLoggedUrl) lastLoggedUrl = url;
return decodedUrl;
} catch (e) { return url; }
}
function patchXMLHttpRequest() {
if (window.XMLHttpRequest && !window.XMLHttpRequest._patchedByUrlV5) {
const orig = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function(m, u) {
const args = Array.prototype.slice.call(arguments, 2);
return orig.apply(this, [m, decodeUrlIfNeeded(u)].concat(args));
};
window.XMLHttpRequest._patchedByUrlV5 = true;
return true;
}
return window.XMLHttpRequest && window.XMLHttpRequest._patchedByUrlV5;
}
function patchImageSrc() {
if (window.Image && !window.Image.prototype._patchedByUrlV5) {
const desc = Object.getOwnPropertyDescriptor(Image.prototype, 'src');
if (!desc || !(desc.set || desc.configurable)) return false;
Object.defineProperty(Image.prototype, 'src', {
configurable: true,
enumerable: desc.enumerable !== undefined ? desc.enumerable : true,
get: desc.get || function() { return this.getAttribute('src') || ''; },
set: function(u) {
const url = decodeUrlIfNeeded(u);
if (desc.set) { try { desc.set.call(this, url); } catch (e) { try { this.setAttribute('src', url); } catch (e2) {} } }
else { try { this.setAttribute('src', url); } catch (e) {} }
}
});
window.Image.prototype._patchedByUrlV5 = true;
return true;
}
return window.Image && window.Image.prototype._patchedByUrlV5;
}
function patchBitmapDrawTextAlign() {
if (!window.Bitmap || !Bitmap.prototype.drawText) return false;
if (Bitmap.prototype._patchedByCanvasTextAlignFix) return true;
const originalDrawText = Bitmap.prototype.drawText;
Bitmap.prototype.drawText = function(text, x, y, maxWidth, lineHeight, align) {
if (align !== 'left' && align !== 'center' && align !== 'right') align = 'left';
return originalDrawText.call(this, text, x, y, maxWidth, lineHeight, align);
};
Bitmap.prototype._patchedByCanvasTextAlignFix = true;
return true;
}
let webAudioPatched = false;
const patchWebAudioMethods = function() {
if (webAudioPatched || typeof WebAudio === 'undefined' || !WebAudio.prototype || WebAudio.prototype._patchedByAudioCacheV2_1) return webAudioPatched;
if (!WebAudio._originalInitialize) WebAudio._originalInitialize = WebAudio.prototype.initialize;
if (!WebAudio._original_load) WebAudio._original_load = WebAudio.prototype._load;
if (!WebAudio._original_onXhrLoad) WebAudio._original_onXhrLoad = WebAudio.prototype._onXhrLoad;
if (!WebAudio._original_onError) WebAudio._original_onError = WebAudio.prototype._onError || function() { this._hasError = true; };
if (!WebAudio._originalClear) WebAudio._originalClear = WebAudio.prototype.clear;
if (!WebAudio._original_readLoopComments) WebAudio._original_readLoopComments = WebAudio.prototype._readLoopComments;
if (typeof WebAudio._audioBuffers === 'undefined') WebAudio._audioBuffers = {};
if (typeof WebAudio._loadingUrls === 'undefined') WebAudio._loadingUrls = {};
WebAudio.prototype._readLoopCommentsFromCache = function(c) {
this._loopStart = c.loopStart || 0;
this._loopLength = c.loopLength || this._totalTime || 0;
this._sampleRate = c.sampleRate || 0;
};
WebAudio.prototype.initialize = function(url) {
WebAudio._originalClear.call(this);
this._url = url;
if (!WebAudio._initialized) WebAudio.initialize();
if (!WebAudio._context) { this._hasError = true; this._loadingState = 'error'; setTimeout(this._callLoadListeners.bind(this), 0); return; }
const c = WebAudio._audioBuffers[url];
if (c && c.buffer) {
this._buffer = c.buffer; this._totalTime = this._buffer.duration;
this._readLoopCommentsFromCache(c); this._loadingState = 'loaded';
setTimeout(this._onLoad.bind(this), 0); return;
}
const loadingPromise = WebAudio._loadingUrls[url];
if (loadingPromise) {
this._isLoading = true; this._loadingState = 'pending';
loadingPromise.then((d) => {
if (d && d.buffer) {
this._buffer = d.buffer; this._totalTime = this._buffer.duration;
this._readLoopCommentsFromCache(d); this._loadingState = 'loaded'; this._onLoad();
} else { this._hasError = true; this._loadingState = 'error'; WebAudio._original_onError.call(this); }
this._isLoading = false;
}).catch(() => {
this._hasError = true; this._loadingState = 'error'; WebAudio._original_onError.call(this);
this._isLoading = false;
});
return;
}
this._loadingState = 'loading';
WebAudio._loadingUrls[url] = new Promise((resolve, reject) => { this._loadResolver = resolve; this._loadRejecter = reject; });
if (!WebAudio._standAlone) this._loader = ResourceHandler.createLoader(url, this._load.bind(this, url), () => {
this._hasError = true;
if (WebAudio._loadingUrls[this._url]) { WebAudio._loadingUrls[this._url].reject(new Error(`ResourceHandler failed: ${this._url}`)); delete WebAudio._loadingUrls[this._url]; }
this._loadingState = 'error'; WebAudio._original_onError.call(this);
});
this._load(url);
};
WebAudio.prototype._load = function(url) {
if (WebAudio._context) {
const xhr = new XMLHttpRequest();
let requestUrl = url;
if (Decrypter.hasEncryptedAudio) requestUrl = Decrypter.extToEncryptExt(url);
xhr.open('GET', requestUrl); xhr.responseType = 'arraybuffer';
xhr.onload = () => { if (xhr.status < 400) this._onXhrLoad(xhr); else this._onError(); };
xhr.onerror = this._loader || this._onError.bind(this);
xhr.send();
} else { this._onError(); }
};
WebAudio.prototype._onXhrLoad = function(xhr) {
let array = xhr.response;
if (Decrypter.hasEncryptedAudio) array = Decrypter.decryptArrayBuffer(array);
WebAudio._original_readLoopComments.call(this, new Uint8Array(array));
const loopStart = this._loopStart, loopLength = this._loopLength, sampleRate = this._sampleRate;
WebAudio._context.decodeAudioData(array, (buffer) => {
this._buffer = buffer; this._totalTime = buffer.duration;
const cacheData = { buffer, loopStart, loopLength, sampleRate };
WebAudio._audioBuffers[this._url] = cacheData;
if (this._loadResolver) this._loadResolver(cacheData);
if (WebAudio._loadingUrls[this._url]) delete WebAudio._loadingUrls[this._url];
if (loopLength > 0 && sampleRate > 0) {
this._loopStart = loopStart / sampleRate; this._loopLength = loopLength / sampleRate;
} else { this._loopStart = 0; this._loopLength = this._totalTime; }
this._loadingState = 'loaded'; this._onLoad();
}, () => {
if (this._loadRejecter) this._loadRejecter(new Error(`decodeAudioData failed for ${this._url}`));
if (WebAudio._loadingUrls[this._url]) delete WebAudio._loadingUrls[this._url];
if (this._loader) this._loader(); else this._onError();
});
};
WebAudio.prototype._onError = function() {
this._hasError = true; this._loadingState = 'error';
if (this._loadRejecter) this._loadRejecter(new Error(`Failed to load audio: ${this._url}`));
if (WebAudio._loadingUrls[this._url]) delete WebAudio._loadingUrls[this._url];
WebAudio._original_onError.call(this);
this._callLoadListeners();
};
WebAudio.prototype._patchedByAudioCacheV2_1 = true;
webAudioPatched = true;
return true;
};
let patchRAFId = null;
const attemptPatches = () => {
const xhrPatched = patchXMLHttpRequest();
const imgPatched = patchImageSrc();
const audioPatched = patchWebAudioMethods();
const drawTextPatched = patchBitmapDrawTextAlign();
if (xhrPatched && imgPatched && audioPatched && drawTextPatched) {
if (patchRAFId) { cancelAnimationFrame(patchRAFId); patchRAFId = null; }
} else patchRAFId = requestAnimationFrame(attemptPatches);
};
patchRAFId = requestAnimationFrame(attemptPatches);
})();