@steambrew/client patcher.js

Created Diff never expires
6 removals
Lines
Total
Removed
Words
Total
Removed
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
113 lines
6 additions
Lines
Total
Added
Words
Total
Added
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
113 lines
// TODO: implement storing patches as an option so we can offer unpatchAll selectively
// TODO: implement storing patches as an option so we can offer unpatchAll selectively
// Return this in a replacePatch to call the original method (can still modify args).
// Return this in a replacePatch to call the original method (can still modify args).
export let callOriginal = Symbol('MILLENNIUM_CALL_ORIGINAL');
export let callOriginal = Symbol('MILLENNIUM_CALL_ORIGINAL');
// let patches = new Set<Patch>();
// let patches = new Set<Patch>();
export function beforePatch(object, property, handler, options = {}) {
export function beforePatch(object, property, handler, options = {}) {
const orig = object[property];
const orig = object[property];
object[property] = function (...args) {
object[property] = (function (...args) {
handler.call(this, args);
handler.call(this, args);
const ret = patch.original.call(this, ...args);
const ret = patch.original.call(this, ...args);
if (options.singleShot) {
if (options.singleShot) {
patch.unpatch();
patch.unpatch();
}
}
return ret;
return ret;
};
});
const patch = processPatch(object, property, handler, object[property], orig);
const patch = processPatch(object, property, handler, object[property], orig);
return patch;
return patch;
}
}
export function afterPatch(object, property, handler, options = {}) {
export function afterPatch(object, property, handler, options = {}) {
const orig = object[property];
const orig = object[property];
object[property] = function (...args) {
object[property] = (function (...args) {
let ret = patch.original.call(this, ...args);
let ret = patch.original.call(this, ...args);
ret = handler.call(this, args, ret);
ret = handler.call(this, args, ret);
if (options.singleShot) {
if (options.singleShot) {
patch.unpatch();
patch.unpatch();
}
}
return ret;
return ret;
};
});
const patch = processPatch(object, property, handler, object[property], orig);
const patch = processPatch(object, property, handler, object[property], orig);
return patch;
return patch;
}
}
export function replacePatch(object, property, handler, options = {}) {
export function replacePatch(object, property, handler, options = {}) {
const orig = object[property];
const orig = object[property];
object[property] = function (...args) {
object[property] = (function (...args) {
const ret = handler.call(this, args);
const ret = handler.call(this, args);
// console.debug('[Patcher] replacePatch', patch);
// console.debug('[Patcher] replacePatch', patch);
if (ret == callOriginal)
if (ret == callOriginal)
return patch.original.call(this, ...args);
return patch.original.call(this, ...args);
if (options.singleShot) {
if (options.singleShot) {
patch.unpatch();
patch.unpatch();
}
}
return ret;
return ret;
};
});
const patch = processPatch(object, property, handler, object[property], orig);
const patch = processPatch(object, property, handler, object[property], orig);
return patch;
return patch;
}
}
function processPatch(object, property, handler, patchedFunction, original) {
function processPatch(object, property, handler, patchedFunction, original) {
// Assign all props of original function to new one
// Assign all props of original function to new one
Object.assign(object[property], original);
Object.assign(object[property], original);
// Allow toString webpack filters to continue to work
// Allow toString webpack filters to continue to work
object[property].toString = () => original.toString();
object[property].toString = () => original.toString();
// HACK: for compatibility, remove when all plugins are using new patcher
// HACK: for compatibility, remove when all plugins are using new patcher
Object.defineProperty(object[property], '__millenniumOrig', {
Object.defineProperty(object[property], '__millenniumOrig', {
get: () => patch.original,
get: () => patch.original,
set: (val) => (patch.original = val),
set: (val) => (patch.original = val),
});
});
// Build a Patch object of this patch
// Build a Patch object of this patch
const patch = {
const patch = {
object,
object,
property,
property,
handler,
handler,
patchedFunction,
patchedFunction,
original,
original,
hasUnpatched: false,
hasUnpatched: false,
unpatch: () => unpatch(patch),
unpatch: () => unpatch(patch),
};
};
object[property].__millenniumPatch = patch;
object[property].__millenniumPatch = patch;
return patch;
return patch;
}
}
function unpatch(patch) {
function unpatch(patch) {
const { object, property, handler, patchedFunction, original } = patch;
const { object, property, handler, patchedFunction, original } = patch;
if (patch.hasUnpatched)
if (patch.hasUnpatched)
throw new Error('Function is already unpatched.');
throw new Error('Function is already unpatched.');
let realProp = property;
let realProp = property;
let realObject = object;
let realObject = object;
console.debug('[Patcher] unpatching', {
console.debug('[Patcher] unpatching', {
realObject,
realObject,
realProp,
realProp,
object,
object,
property,
property,
handler,
handler,
patchedFunction,
patchedFunction,
original,
original,
isEqual: realObject[realProp] === patchedFunction,
isEqual: realObject[realProp] === patchedFunction,
});
});
// If another patch has been applied to this function after this one, move down until we find the correct patch
// If another patch has been applied to this function after this one, move down until we find the correct patch
while (realObject[realProp] && realObject[realProp] !== patchedFunction) {
while (realObject[realProp] && realObject[realProp] !== patchedFunction) {
realObject = realObject[realProp].__millenniumPatch;
realObject = realObject[realProp].__millenniumPatch;
realProp = 'original';
realProp = 'original';
console.debug('[Patcher] moved to next', {
console.debug('[Patcher] moved to next', {
realObject,
realObject,
realProp,
realProp,
object,
object,
property,
property,
handler,
handler,
patchedFunction,
patchedFunction,
original,
original,
isEqual: realObject[realProp] === patchedFunction,
isEqual: realObject[realProp] === patchedFunction,
});
});
}
}
realObject[realProp] = realObject[realProp].__millenniumPatch.original;
realObject[realProp] = realObject[realProp].__millenniumPatch.original;
patch.hasUnpatched = true;
patch.hasUnpatched = true;
console.debug('[Patcher] unpatched', {
console.debug('[Patcher] unpatched', {
realObject,
realObject,
realProp,
realProp,
object,
object,
property,
property,
handler,
handler,
patchedFunction,
patchedFunction,
original,
original,
isEqual: realObject[realProp] === patchedFunction,
isEqual: realObject[realProp] === patchedFunction,
});
});
}
}