function installDipWallhäçk(win, cfg, maskFn, swapFn) {
const INDEXED_METHODS = [
'drawElements',
'drawRangeElements',
'drawElementsInstanced',
'drawElementsInstancedBaseVertex'
];
function isTargetIndexedDraw(gl, fnName, args) {
if (!cfg || !cfg.wh) return false;
if (args[0] !== gl.TRIANGLES) return false;
const count = (fnName === 'drawRangeElements') ? args[3] : args[1];
const minIndices = (typeof cfg.whMinIndices === 'number') ? cfg.whMinIndices : 240;
const maxIndices = (typeof cfg.whMaxIndices === 'number') ? cfg.whMaxIndices : 60000;
return typeof count === 'number' && count >= minIndices && count <= maxIndices;
}
[win.WebGLRenderingContext, win.WebGL2RenderingContext].forEach(function (C) {
if (!C || !C.prototype) return;
const P = C.prototype;
INDEXED_METHODS.forEach(function (name) {
const orig = P[name];
if (typeof orig !== 'function') return;
const wrapped = function () {
if (!isTargetIndexedDraw(this, name, arguments)) {
return orig.apply(this, arguments);
}
let prevDepthTest = null;
let prevDepthFunc = null;
try {
prevDepthTest = this.isEnabled(this.DEPTH_TEST);
prevDepthFunc = this.getParameter(this.DEPTH_FUNC);
this.enable(this.DEPTH_TEST);
this.depthFunc(this.ALWAYS);
} catch (e) {}
try {
return orig.apply(this, arguments);
} finally {
try {
if (prevDepthFunc !== null) this.depthFunc(prevDepthFunc);
else this.depthFunc(this.LESS);
if (prevDepthTest === false) this.disable(this.DEPTH_TEST);
} catch (e) {}
}
};
const fn = (typeof maskFn === 'function') ? maskFn(wrapped, name) : wrapped;
if (typeof swapFn === 'function') {
swapFn(P, name, fn);
} else {
P[name] = fn;
}
});
});
}