-
Notifications
You must be signed in to change notification settings - Fork 51
/
modframework.js
316 lines (259 loc) · 10.4 KB
/
modframework.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
class FirmwareMod {
constructor(name, description, size) {
this.name = name;
this.description = description;
this.size = size; // Additional flash usage in bytes
this.enabled = false; // Checkbox status, initially disabled
this.hidden = false; // If true, the mod will be hidden until activated in the instructions panel. Use this for risky mods.
this.modSpecificDiv = document.createElement("div"); // Div for mod-specific inputs
// If needed, create input fields here and append them to the modSpecificDiv
}
apply(firmwareData) {
// This method should be overridden in each mod implementation
// It should apply the mod on the firmwareData and return the modified firmwareData
return firmwareData;
}
}
function addModToUI(mod, modDiv) {
// Create a card div
const card = document.createElement("div");
card.classList.add("card", "mb-3", "border-left-primary", "border-left-secondary");
if (mod.hidden) {
card.classList.add("hiddenMod", "d-none", "border-danger", "border-left-danger");
}
// Create a card body div
const cardBody = document.createElement("div");
cardBody.classList.add("card-body");
// Create a row div
const row = document.createElement("div");
row.classList.add("row");
// Create checkbox column
const checkboxCol = document.createElement("div");
checkboxCol.classList.add("col-auto");
// Create checkbox for enabling the mod
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.style.height = "1.45rem";
checkbox.style.width = "1.45rem";
checkbox.checked = mod.enabled;
checkbox.addEventListener("change", function () {
mod.enabled = checkbox.checked;
if (checkbox.checked) {
card.classList.remove("border-left-secondary");
} else {
card.classList.add("border-left-secondary");
}
});
checkboxCol.appendChild(checkbox);
// Create name column
const nameCol = document.createElement("div");
nameCol.classList.add("col-auto", "mr-auto", "pl-1");
if (mod.hidden) {
nameCol.classList.add("text-danger");
}
const nameText = document.createElement("h5");
nameText.textContent = mod.name;
nameCol.appendChild(nameText);
// Create size column
const sizeCol = document.createElement("div");
sizeCol.classList.add("col-auto");
const sizeText = document.createElement("p");
sizeText.textContent = "Flash usage: " + mod.size + " Bytes";
sizeCol.appendChild(sizeText);
// Add columns to the row
row.appendChild(checkboxCol);
row.appendChild(nameCol);
row.appendChild(sizeCol);
// Create description column
const descCol = document.createElement("div");
//descCol.classList.add("col");
const descriptionText = document.createElement("p");
descriptionText.textContent = mod.description;
descCol.appendChild(descriptionText);
// Add the mod-specific div for custom inputs
cardBody.appendChild(row);
cardBody.appendChild(descCol);
cardBody.appendChild(mod.modSpecificDiv);
// Add card body to the card div
card.appendChild(cardBody);
// Add the card to the modDiv
modDiv.appendChild(card);
}
function showHiddenMods() {
const hiddenMods = document.getElementsByClassName("hiddenMod");
for (const mod of hiddenMods) {
mod.classList.remove("d-none");
}
log("Hidden mods shown. Please pay extra attention when using them.");
}
var modClasses = []; // Will be populated in mods.js
var modInstances = [];
function modLoader() {
modClasses.forEach(ModClass => {
const modInstance = new ModClass();
modInstances.push(modInstance); // Add the instance to the array
const modDiv = document.createElement("div");
addModToUI(modInstance, modDiv);
document.getElementById("modsContainer").appendChild(modDiv);
});
log("Patcher ready.");
// for development purposes, add ?hidden to the url to always show hidden mods
if (window.location.href.indexOf("?hidden") > -1) {
showHiddenMods();
}
return modInstances; // Return the array of mod instances
}
function applyMods(firmware) {
for (const modInstance of modInstances) {
if (modInstance.enabled) {
firmware = modInstance.apply(firmware);
}
}
log("Finished applying mods...");
return firmware;
}
function log(message, replace = false) {
const consoleArea = document.getElementById('console');
if (replace) {
// Replace the last line with the new message
const lastLineIndex = consoleArea.value.lastIndexOf('\n');
consoleArea.value = consoleArea.value.substring(0, lastLineIndex) + '\n' + message;
} else {
// Append the new message to the existing content and add a newline
// If the console is empty, dont add a newline
if (consoleArea.value.length === 0) {
consoleArea.value = message;
} else {
consoleArea.value += '\n' + message;
}
}
// Scroll to the bottom to show the latest message
consoleArea.scrollTop = consoleArea.scrollHeight;
}
// Helper functions:
/**
* Converts a hexadecimal string to a Uint8Array.
* The input hex string should have the format "HH" where HH is a two-digit hexadecimal value.
*
* 0x or \x is not allowed
*
* To output a python bytearray in the correct format, use this in python: print(''.join('%02x'%i for i in YOUR_BYTEARRAY))
* @example hexString("0102AAFF") // Outputs Uint8Array of 1, 2, 170, 255
* @param {string} hexString - The hexadecimal string to convert.
* @returns {Uint8Array} The Uint8Array representing the converted data.
*/
function hexString(hexString) {
const byteArray = new Uint8Array(hexString.length / 2);
for (let i = 0; i < byteArray.length; i++) {
const byteValue = parseInt(hexString.substr(i * 2, 2), 16);
byteArray[i] = byteValue;
}
return byteArray;
}
/**
* Converts a Uint8Array to a hexadecimal string, mostly for debugging purposes.
*
* @param {Uint8Array} uint8Array - The Uint8Array to convert.
* @returns {string} The hexadecimal representation of the Uint8Array without separators.
*/
function uint8ArrayToHexString(uint8Array) {
return Array.from(uint8Array)
.map((byte) => byte.toString(16).padStart(2, '0'))
.join('');
}
/**
* Replaces or appends a section in the firmware data with new data at the specified offset.
* To append data to the firmware, use firmwareData.length as the offset.
* @param {Uint8Array} firmwareData - The original firmware Uint8array.
* @param {Uint8Array} newData - The new data to replace the section with.
* @param {number} offset - The offset where the section should be replaced.
* @returns {Uint8Array} - The updated firmware data with the section replaced.
*/
function replaceSection(firmwareData, newData, offset) {
const updatedFirmwareData = new Uint8Array(Math.max(firmwareData.length, offset + newData.length));
updatedFirmwareData.set(firmwareData.subarray(0, offset));
updatedFirmwareData.set(newData, offset);
if (offset + newData.length < firmwareData.length) {
updatedFirmwareData.set(firmwareData.subarray(offset + newData.length), offset + newData.length);
}
return updatedFirmwareData;
}
/**
* Compares two Uint8Arrays to check if they are equal.
* @param {Uint8Array} array1 - The first Uint8Array to compare.
* @param {Uint8Array} array2 - The second Uint8Array to compare.
* @returns {boolean} - True if the Uint8Arrays are equal, false otherwise.
*/
function compareUint8Arrays(array1, array2) {
if (array1.length !== array2.length) {
return false;
}
for (let i = 0; i < array1.length; i++) {
if (array1[i] !== array2[i]) {
return false;
}
}
return true;
}
/**
* Compares a section of a Uint8Array with another Uint8Array.
* @param {Uint8Array} array - The Uint8Array to compare a section from.
* @param {Uint8Array} section - The Uint8Array representing the section to compare.
* @param {number} offset - The offset within the main array to start the comparison.
* @returns {boolean} - True if the section matches the part of the main array starting from the offset, false otherwise.
*/
function compareSection(array, section, offset) {
if (offset < 0 || offset + section.length > array.length) {
throw new Error("Offset is out of bounds.");
}
const slicedArray = array.slice(offset, offset + section.length);
return compareUint8Arrays(slicedArray, section);
}
/**
* Adds an input field to a parent div with a label and default text.
*
* @param {HTMLElement} parentDiv - The parent div to which the input field will be added. Usually this.modSpecificDiv
* @param {string} labelText - The label text (title) for the input field.
* @param {string} defaultText - The default text to pre-fill the input field with.
* @returns {HTMLInputElement} - The created input element, assign it to a constant for later use.
*/
function addInputField(parentDiv, labelText, defaultValue) {
const formGroup = document.createElement("div");
formGroup.classList.add("form-group");
const label = document.createElement("label");
label.textContent = labelText;
formGroup.appendChild(label);
const input = document.createElement("input");
input.classList.add("form-control");
input.type = "text";
input.value = defaultValue; // Set the default value
formGroup.appendChild(input);
parentDiv.appendChild(formGroup);
return input; // Return the input element
}
/**
* Adds a radio input field to a parent div with an id, name, value and label.
*
* @param {HTMLElement} parentDiv - The parent div to which the input field will be added. Usually this.modSpecificDiv
* @param {string} labelText - The label text (title) for the input field.
* @param {string} id - The id is needed to link radio button and label, choose any unique id.
* @param {string} name - The name of the radio button needs to be the same for all radio buttons in a mutually exclusive group.
* @returns {HTMLInputElement} - The created input element, assign it to a constant for later use.
*/
function addRadioButton(parentDiv, labelText, id, name) {
const formCheckDiv = document.createElement("div");
formCheckDiv.classList.add("form-check", "mt-2");
const inputElement = document.createElement("input");
inputElement.classList.add("form-check-input");
inputElement.type = "radio";
inputElement.name = name;
inputElement.id = id;
const labelElement = document.createElement("label");
labelElement.classList.add("form-check-label");
labelElement.htmlFor = id;
labelElement.textContent = labelText;
formCheckDiv.appendChild(inputElement);
formCheckDiv.appendChild(labelElement);
parentDiv.appendChild(formCheckDiv);
return inputElement;
}