2017-02-08 07:22:19 +01:00
|
|
|
(function(window, document) {
|
|
|
|
"use strict";
|
2017-01-28 05:41:42 +01:00
|
|
|
|
2017-01-29 15:37:14 +01:00
|
|
|
// Each unique kind of patch should have createUI, validatePatch, applyPatch,
|
|
|
|
// updateUI
|
|
|
|
|
2017-02-08 07:22:19 +01:00
|
|
|
var StandardPatch = function(options) {
|
2017-01-29 15:37:14 +01:00
|
|
|
this.name = options.name;
|
|
|
|
this.shortname = options.shortname;
|
|
|
|
this.patches = options.patches;
|
2017-09-04 07:38:54 +02:00
|
|
|
this.tooltip = options.tooltip;
|
2017-01-29 15:37:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
StandardPatch.prototype.createUI = function(parent) {
|
|
|
|
var id = this.shortname;
|
|
|
|
var label = this.name;
|
2017-09-04 07:38:54 +02:00
|
|
|
var patch = $('<div>', {'class' : 'patch'});
|
|
|
|
patch.append('<input type="checkbox" id="' + id + '"><label for="' + id + '">' + label + '</label>');
|
|
|
|
if(this.tooltip) {
|
|
|
|
patch.append('<span class="tooltip">' + this.tooltip + '</div>');
|
|
|
|
}
|
|
|
|
parent.append(patch);
|
2017-01-29 15:37:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
StandardPatch.prototype.updateUI = function(file) {
|
|
|
|
var id = this.shortname;
|
|
|
|
var elem = document.getElementById(id);
|
|
|
|
elem.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) {
|
2017-02-08 07:22:19 +01:00
|
|
|
var id = this.shortname;
|
2017-01-29 15:37:14 +01:00
|
|
|
var enabled = document.getElementById(id).checked;
|
|
|
|
this.replaceAll(file, enabled);
|
|
|
|
return enabled ? this.shortname : "";
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
2017-02-08 07:22:19 +01:00
|
|
|
var UnionPatch = function(options) {
|
2017-01-29 15:37:14 +01:00
|
|
|
this.name = options.name;
|
|
|
|
this.shortname = options.shortname;
|
|
|
|
this.offset = options.offset;
|
|
|
|
this.patches = options.patches;
|
|
|
|
};
|
|
|
|
|
|
|
|
UnionPatch.prototype.createUI = function(parent) {
|
|
|
|
var container = $("<div>", {"class": "patch-union"});
|
|
|
|
container.append('<span class="patch-union-title">' + this.name + ':</span>');
|
|
|
|
for(var i = 0; i < this.patches.length; i++) {
|
|
|
|
var patch = this.patches[i];
|
|
|
|
var id = this.shortname + '-' + patch.shortname;
|
|
|
|
var label = patch.name;
|
|
|
|
container.append('<div class="patch"><input type="radio" id="' + id + '" name="' + this.shortname + '"><label for="' + id + '">' + label + '</label></div>');
|
|
|
|
}
|
|
|
|
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)) {
|
|
|
|
document.getElementById(this.shortname + '-' + this.patches[i].shortname).checked = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Default fallback
|
|
|
|
document.getElementById(this.shortname + '-' + this.patches[0].shortname).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();
|
|
|
|
var name = this.shortname + patch.shortname;
|
|
|
|
replace(file, this.offset, patch.patch);
|
|
|
|
return patch.shortname == "default" ? "" : name;
|
|
|
|
};
|
|
|
|
|
|
|
|
UnionPatch.prototype.getSelected = function() {
|
|
|
|
for(var i = 0; i < this.patches.length; i++) {
|
|
|
|
if(document.getElementById(this.shortname + '-' + this.patches[i].shortname).checked) {
|
|
|
|
return this.patches[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-02-08 07:22:19 +01:00
|
|
|
var DllPatcher = function(fname, args) {
|
|
|
|
this.mods = [];
|
2017-01-29 15:37:14 +01:00
|
|
|
for(var i = 0; i < args.length; i++) {
|
2017-02-08 07:22:19 +01:00
|
|
|
var mod = args[i];
|
2017-01-29 15:37:14 +01:00
|
|
|
if(mod.type) {
|
|
|
|
if(mod.type == "union") {
|
2017-02-08 07:22:19 +01:00
|
|
|
this.mods.push(new UnionPatch(mod));
|
2017-01-29 15:37:14 +01:00
|
|
|
}
|
|
|
|
} else { // standard patch
|
2017-02-08 07:22:19 +01:00
|
|
|
this.mods.push(new StandardPatch(mod));
|
2017-01-29 15:37:14 +01:00
|
|
|
}
|
|
|
|
}
|
2017-02-08 07:22:19 +01:00
|
|
|
this.filename = fname;
|
|
|
|
this.createUI();
|
|
|
|
this.loadPatchUI();
|
2017-01-28 05:41:42 +01:00
|
|
|
};
|
|
|
|
|
2017-02-08 07:22:19 +01:00
|
|
|
DllPatcher.prototype.createUI = function() {
|
|
|
|
var self = this;
|
|
|
|
var container = $("<div>", {"class": "patchContainer"});
|
|
|
|
container.html('<h3>' + this.filename + '.dll</h3>');
|
|
|
|
|
|
|
|
container.on('drag dragstart dragend dragover dragenter dragleave drop', function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
})
|
|
|
|
.on('drop', function(e) {
|
|
|
|
var files = e.originalEvent.dataTransfer.files;
|
|
|
|
if(files && files.length > 0)
|
|
|
|
self.loadFile(files[0]);
|
|
|
|
})
|
|
|
|
.on('dragover dragenter', function() {
|
|
|
|
container.addClass('dragover');
|
|
|
|
})
|
|
|
|
.on('dragleave dragend drop', function() {
|
|
|
|
container.removeClass('dragover');
|
|
|
|
});
|
|
|
|
|
|
|
|
this.fileInput = $("<input>",
|
|
|
|
{"class": "fileInput",
|
|
|
|
"id" : this.filename + '-file',
|
|
|
|
"type" : 'file'});
|
|
|
|
var label = $("<label>", {"class": "fileLabel", "for": this.filename + '-file'});
|
|
|
|
label.html('<strong>Choose a file</strong> or drag and drop.');
|
|
|
|
|
|
|
|
this.fileInput.on('change', function(e) {
|
|
|
|
if(this.files && this.files.length > 0)
|
|
|
|
self.loadFile(this.files[0]);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.successDiv = $("<div>", {"class": "success"});
|
|
|
|
this.errorDiv = $("<div>", {"class": "error"});
|
|
|
|
this.patchDiv = $("<div>", {"class": "patches"});
|
|
|
|
|
2017-03-05 08:23:05 +01:00
|
|
|
var saveButton = $("<button disabled>");
|
|
|
|
saveButton.text('Load DLL First');
|
2017-02-08 07:22:19 +01:00
|
|
|
saveButton.on('click', this.saveDll.bind(this));
|
2017-03-05 08:23:05 +01:00
|
|
|
this.saveButton = saveButton;
|
2017-02-08 07:22:19 +01:00
|
|
|
|
|
|
|
container.append(this.fileInput);
|
|
|
|
container.append(label);
|
|
|
|
container.append(this.successDiv);
|
|
|
|
container.append(this.errorDiv);
|
|
|
|
container.append(this.patchDiv);
|
|
|
|
container.append(saveButton);
|
|
|
|
$('body').append(container);
|
|
|
|
}
|
2017-01-28 05:41:42 +01:00
|
|
|
|
2017-02-08 07:22:19 +01:00
|
|
|
DllPatcher.prototype.loadFile = function(file) {
|
|
|
|
var reader = new FileReader();
|
|
|
|
var self = this;
|
|
|
|
|
2017-01-28 05:41:42 +01:00
|
|
|
reader.onload = function(e) {
|
2017-02-08 07:22:19 +01:00
|
|
|
self.dllFile = new Uint8Array(e.target.result);
|
|
|
|
if(self.validatePatches()) {
|
|
|
|
self.successDiv.removeClass("hidden");
|
|
|
|
self.successDiv.html("DLL loaded successfully!");
|
2017-01-28 05:41:42 +01:00
|
|
|
} else {
|
2017-02-08 07:22:19 +01:00
|
|
|
self.successDiv.addClass("hidden");
|
2017-01-28 05:41:42 +01:00
|
|
|
}
|
2017-03-05 08:23:05 +01:00
|
|
|
// Update save button regardless
|
|
|
|
self.saveButton.prop('disabled', false);
|
|
|
|
self.saveButton.text('Save DLL');
|
2017-02-08 07:22:19 +01:00
|
|
|
self.errorDiv.html(self.errorLog);
|
|
|
|
self.updatePatchUI();
|
2017-01-28 05:41:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
reader.readAsArrayBuffer(file);
|
|
|
|
};
|
|
|
|
|
2017-02-08 07:22:19 +01:00
|
|
|
DllPatcher.prototype.saveDll = function() {
|
|
|
|
if(!this.dllFile || !this.mods || !this.filename)
|
2017-01-28 05:41:42 +01:00
|
|
|
return;
|
2017-02-08 07:22:19 +01:00
|
|
|
var fname = this.filename;
|
2017-01-28 05:41:42 +01:00
|
|
|
|
2017-02-08 07:22:19 +01:00
|
|
|
for(var i = 0; i < this.mods.length; i++) {
|
|
|
|
var enabledStr = this.mods[i].applyPatch(this.dllFile);
|
2017-03-13 05:32:53 +01:00
|
|
|
/* disabled as it can get kinda hectic with many patches
|
2017-01-29 15:37:14 +01:00
|
|
|
if(enabledStr) {
|
|
|
|
fname += '-' + enabledStr;
|
2017-03-13 05:32:53 +01:00
|
|
|
} */
|
|
|
|
}
|
2017-01-28 05:41:42 +01:00
|
|
|
fname += '.dll';
|
|
|
|
|
2017-02-08 07:22:19 +01:00
|
|
|
var blob = new Blob([this.dllFile], {type: "application/octet-stream"});
|
2017-01-28 05:41:42 +01:00
|
|
|
saveAs(blob, fname);
|
|
|
|
}
|
|
|
|
|
2017-02-08 07:22:19 +01:00
|
|
|
DllPatcher.prototype.loadPatchUI = function() {
|
|
|
|
for(var i = 0; i < this.mods.length; i++) {
|
|
|
|
this.mods[i].createUI(this.patchDiv);
|
2017-01-28 05:41:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-08 07:22:19 +01:00
|
|
|
DllPatcher.prototype.updatePatchUI = function() {
|
|
|
|
for(var i = 0; i < this.mods.length; i++) {
|
|
|
|
this.mods[i].updateUI(this.dllFile);
|
2017-01-28 05:41:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-08 07:22:19 +01:00
|
|
|
DllPatcher.prototype.validatePatches = function() {
|
|
|
|
this.errorLog = "";
|
|
|
|
var success = true;
|
|
|
|
for(var i = 0; i < this.mods.length; i++) {
|
|
|
|
var error = this.mods[i].validatePatch(this.dllFile);
|
2017-01-29 15:37:14 +01:00
|
|
|
if(error) {
|
2017-02-08 07:22:19 +01:00
|
|
|
this.errorLog += error + "<br/>";
|
2017-01-28 05:41:42 +01:00
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2017-02-08 07:22:19 +01:00
|
|
|
var bytesMatch = function(buffer, offset, bytes) {
|
2017-01-28 05:41:42 +01:00
|
|
|
for(var i = 0; i < bytes.length; i++) {
|
|
|
|
if(buffer[offset+i] != bytes[i])
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2017-02-08 07:22:19 +01:00
|
|
|
var replace = function(buffer, offset, bytes) {
|
2017-01-28 05:41:42 +01:00
|
|
|
for(var i = 0; i < bytes.length; i++) {
|
|
|
|
buffer[offset+i] = bytes[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-08 07:22:19 +01:00
|
|
|
var whichBytesMatch = function(buffer, offset, bytesArray) {
|
2017-01-28 05:41:42 +01:00
|
|
|
for(var i = 0; i < bytesArray.length; i++) {
|
|
|
|
if(bytesMatch(buffer, offset, bytesArray[i]))
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-02-08 07:22:19 +01:00
|
|
|
window.DllPatcher = DllPatcher;
|
|
|
|
|
|
|
|
})(window, document);
|