(function(window, document) { "use strict"; // form labels often need unique IDs - this can be used to generate some window.DllPatcher_uniqueid = 0; var createID = function() { window.DllPatcher_uniqueid++; return "dllpatch_" + window.DllPatcher_uniqueid; } // Each unique kind of patch should have createUI, validatePatch, applyPatch, // updateUI var StandardPatch = function(options) { this.name = options.name; this.patches = options.patches; this.tooltip = options.tooltip; }; StandardPatch.prototype.createUI = function(parent) { var id = createID(); var label = this.name; var patch = $('
', {'class' : 'patch'}); this.checkbox = $('')[0]; patch.append(this.checkbox); patch.append(''); if(this.tooltip) { patch.append('
' + this.tooltip + '
'); } parent.append(patch); }; StandardPatch.prototype.updateUI = function(file) { this.checkbox.checked = this.checkPatchBytes(file) === "on"; }; StandardPatch.prototype.validatePatch = function(file) { var status = this.checkPatchBytes(file); if(status === "on") { console.log('"' + this.name + '"', "is enabled!"); } else if(status === "off") { console.log('"' + this.name + '"', "is disabled!"); } else { return '"' + this.name + '" is neither on nor off! Have you got the right dll?'; } }; StandardPatch.prototype.applyPatch = function(file) { this.replaceAll(file, this.checkbox.checked); }; StandardPatch.prototype.replaceAll = function(file, featureOn) { for(var i = 0; i < this.patches.length; i++) { replace(file, this.patches[i].offset, featureOn? this.patches[i].on : this.patches[i].off); } } StandardPatch.prototype.checkPatchBytes = function(file) { var patchStatus = ""; for(var i = 0; i < this.patches.length; i++) { var patch = this.patches[i]; if(bytesMatch(file, patch.offset, patch.off)) { if(patchStatus === "") { patchStatus = "off"; } else if(patchStatus != "off"){ return "on/off mismatch within patch"; } } else if(bytesMatch(file, patch.offset, patch.on)) { if(patchStatus === "") { patchStatus = "on"; } else if(patchStatus != "on"){ return "on/off mismatch within patch"; } } else { return "patch neither on nor off"; } } return patchStatus; } // Each unique kind of patch should have createUI, validatePatch, applyPatch, // updateUI // The DEFAULT state is always the 1st element in the patches array var UnionPatch = function(options) { this.name = options.name; this.offset = options.offset; this.patches = options.patches; }; UnionPatch.prototype.createUI = function(parent) { this.radios = []; var radio_id = createID(); var container = $("
", {"class": "patch-union"}); container.append('' + this.name + ':'); for(var i = 0; i < this.patches.length; i++) { var patch = this.patches[i]; var id = createID(); var label = patch.name; var patchDiv = $('
', {'class' : 'patch'}); var radio = $('')[0]; this.radios.push(radio); patchDiv.append(radio); patchDiv.append(''); if(patch.tooltip) { patchDiv.append('
' + patch.tooltip + '
'); } container.append(patchDiv); } parent.append(container); }; UnionPatch.prototype.updateUI = function(file) { for(var i = 0; i < this.patches.length; i++) { if(bytesMatch(file, this.offset, this.patches[i].patch)) { this.radios[i].checked = true; return; } } // Default fallback this.radios[0].checked = true; }; UnionPatch.prototype.validatePatch = function(file) { for(var i = 0; i < this.patches.length; i++) { if(bytesMatch(file, this.offset, this.patches[i].patch)) { console.log(this.name, "has", this.patches[i].name, "enabled"); return; } } return '"' + this.name + '" doesn\'t have a valid patch! Have you got the right dll?'; }; UnionPatch.prototype.applyPatch = function(file) { var patch = this.getSelected(); replace(file, this.offset, patch.patch); }; UnionPatch.prototype.getSelected = function() { for(var i = 0; i < this.patches.length; i++) { if(this.radios[i].checked) { return this.patches[i]; } } return null; } var DllPatcherContainer = function (patchers) { this.patchers = patchers; this.createUI(); }; DllPatcherContainer.prototype.getSupportedDLLs = function () { var dlls = []; for (var i = 0; i < this.patchers.length; i++) { var name = this.patchers[i].filename + ".dll"; if (dlls.indexOf(name) === -1) { dlls.push(name); } } return dlls; }; DllPatcherContainer.prototype.getSupportedVersions = function () { var descriptions = []; for (var i = 0; i < this.patchers.length; i++) { if ($.type(this.patchers[i].description) === "string") { descriptions.push(this.patchers[i].description); } } return descriptions; }; DllPatcherContainer.prototype.createUI = function () { var self = this; var container = $("
", {"class": "patchContainer"}); var header = this.getSupportedDLLs().join(", "); container.html("

" + header + "

"); var supportedDlls = $("
    "); var versions = this.getSupportedVersions(); for (var i = 0; i < versions.length; i++) { $("
  • ").text(versions[i]).appendTo(supportedDlls); } $("html").on("dragover dragenter", function () { container.addClass("dragover"); return true; }) .on("dragleave dragend drop", function () { container.removeClass("dragover"); return true; }) .on("dragover dragenter dragleave dragend drop", function (e) { e.preventDefault(); }); container.on("drop", function (e) { var files = e.originalEvent.dataTransfer.files; if (files && files.length > 0) self.loadFile(files[0]); }); this.fileInput = $("", { "class": "fileInput", "id": "any-file", "type": "file", }); var label = $("