From a6c46e3ae5ee101335b0a5529dbb4e9d0cc94dac Mon Sep 17 00:00:00 2001 From: Nathan Chowning Date: Sat, 11 Sep 2021 21:37:14 +0000 Subject: [PATCH] Added Number patch type for easily manipulating timing offsets --- js/dllpatcher.js | 86 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 85 insertions(+), 1 deletion(-) diff --git a/js/dllpatcher.js b/js/dllpatcher.js index 0952581..725cad7 100644 --- a/js/dllpatcher.js +++ b/js/dllpatcher.js @@ -112,6 +112,7 @@ class UnionPatch { this.name = options.name; this.offset = options.offset; this.patches = options.patches; + this.tooltip = options.tooltip; } createUI(parent) { @@ -119,7 +120,12 @@ class UnionPatch { var radio_id = createID(); var container = $("
", {"class": "patch-union"}); - container.append('' + this.name + ':'); + container.append('' + this.name + ':'); + if(this.tooltip) { + container.append('
' + this.tooltip + '
'); + } + container.append('
'); + for(var i = 0; i < this.patches.length; i++) { var patch = this.patches[i]; var id = createID(); @@ -174,6 +180,81 @@ class UnionPatch { } } +// Each unique kind of patch should have createUI, validatePatch, applyPatch, +// updateUI +class NumberPatch { + constructor(options) { + this.name = options.name; + this.tooltip = options.tooltip; + + this.offset = options.offset; + this.size = options.size; + this.min = options.min; + this.max = options.max; + } + + createUI(parent) { + var id = createID(); + var label = this.name; + var patch = $('
', {'class': 'patch'}); + + patch.append(''); + + var minmax = ' '; + if (this.min !== null) { + minmax += 'min="' + this.min + '" '; + } + if (this.max) { + minmax += 'max="' + this.max + '" '; + } + + this.number = $('')[0]; + patch.append(this.number); + + + if (this.tooltip) { + patch.append('
' + this.tooltip + '
'); + } + parent.append(patch) + + } + + updateUI(file) { + // This converts bytes from the file to big endian by shifting each + // byte `i` bytes to the left then doing a bitwise OR to add the less + // significant bytes that were gathered at earlier iterations of loop + var val = 0; + for (var i = 0; i < this.size; i++) { + val = (file[this.offset + i] << (8 * i)) | val; + } + + this.number.value = val; + } + + validatePatch(file) { + return; + } + + applyPatch(file) { + // Convert user inputted number to little endian + const view = new DataView(new ArrayBuffer(this.size * 2)); + view.setInt32(1, this.number.value, true); + + for (var i = 0; i < this.size; i++) { + var val = view.getInt32(1); + + // Shift off less significant bytes + val = val >> ((this.size - 1 - i) * 8); + + // Mask off more significant bytes + val = val & 0xFF; + + // Write this byte + file[this.offset + i] = val; + } + } +} + var loadPatch = function(_this, self, patcher) { patcher.loadPatchUI(); patcher.updatePatchUI(); @@ -363,6 +444,9 @@ class Patcher { if(mod.type === "union") { this.mods.push(new UnionPatch(mod)); } + if(mod.type === "number") { + this.mods.push(new NumberPatch(mod)); + } } else { // standard patch this.mods.push(new StandardPatch(mod)); }