-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_diff.js
65 lines (59 loc) · 3.09 KB
/
utils_diff.js
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
/** d2rmp, a patcher script toolset for D2R mods.
* Copyright (C) 2024 myocytebd
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
'use strict';
const microdiff = require('microdiff').default;
function toHex(ivalue) {
let sign = Math.sign(ivalue), ustr = Math.abs(ivalue).toString(16);
return `${sign < 0 ? '-' : ''}0x${ustr.padStart((ustr.length + 1) & -2, '0')}`;
}
/** @param {Uint8Array} buf0 @param {Uint8Array} buf1 @param { desc?: string, maxDiffCount?: number, diffOnSizeMismatch?: boolean } */
function diffBufferToConsole(buf0, buf1, { desc, maxDiffCount, diffOnSizeMismatch } = { desc: 'unnamed-buffer', maxDiffCount: 10, diffOnSizeMismatch: false }) {
if (Buffer.compare(buf0, buf1) === 0) return 0;
if (buf0.length !== buf1.length) {
console.warn(`diffBuffer-${desc}: size mismatch: ${buf0.length} vs ${buf1.length}`);
if (!diffOnSizeMismatch) return 1E6;
}
let diffCount = 0, headerPrinted = false;
const ensureHeaderPrint = () => {
if (headerPrinted) return;
headerPrinted = true;
if (buf0.length === buf1.length) console.info(`diffBuffer-${desc}: same size: ${buf0.length}`);
};
for (let i = 0, len = Math.min(buf0.length, buf1.length); i < len; i++) {
const shortPrintSlice = (buf, pos) => Buffer.from(buf.slice(pos, pos + Math.min(80, len))).toString('hex');
if (buf0[i] !== buf1[i]) {
ensureHeaderPrint();
console.warn(`diffBuffer-${desc}: first byte diff at: ${i}/${toHex(i)}, ${buf0[i]} vs ${buf1[i]}`);
console.warn(`diffBuffer-${desc}: src[${toHex(i)}]: ${shortPrintSlice(buf0, i)}}`);
console.warn(`diffBuffer-${desc}: dst[${toHex(i)}]: ${shortPrintSlice(buf1, i)}}`);
if (++diffCount >= maxDiffCount) break;
}
}
if (diffCount > 0) {
if (diffCount === maxDiffCount) {
console.warn(`diffBuffer-${desc}: total >=${diffCount} diffs (capped)`);
} else {
console.warn(`diffBuffer-${desc}: total ${diffCount} diffs`);
}
}
return diffCount;
}
/** @param { desc?: string, maxDiffCount?: number } */
function diffObjectToConsole(obj0, obj1, { desc, maxDiffCount } = { desc: 'unnamed-object', maxDiffCount: 20 }) {
let diffArray = microdiff(obj0, obj1);
if (diffArray.length > 0) {
console.warn(`diffObject-${desc}: found ${diffArray.length} diffs`);
console.warn(diffArray.slice(0, maxDiffCount));
}
return diffArray.length;
}
module.exports = {
diffBufferToConsole, diffObjectToConsole,
};