pages: cleanup
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
/* Config: Cust */
|
||||
const CUST_REV = 3;
|
||||
var buffer: ArrayBuffer;
|
||||
|
||||
enum CustPlatform {
|
||||
Undefined = 0,
|
||||
@@ -10,50 +9,39 @@ enum CustPlatform {
|
||||
};
|
||||
|
||||
class CustEntry {
|
||||
id: string;
|
||||
name: string;
|
||||
platform: CustPlatform;
|
||||
size: number;
|
||||
desc: string;
|
||||
defval: number;
|
||||
min: number;
|
||||
max: number;
|
||||
step: number; // also as quotient
|
||||
zeroable: boolean;
|
||||
value?: number;
|
||||
offset?: number;
|
||||
|
||||
constructor(id: string, name: string, platform: CustPlatform, size: number, desc: string, defval: number, minmax: [number, number] = [0, 1_000_000], step: number = 1, zeroable: boolean = true) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.platform = platform;
|
||||
this.size = size;
|
||||
this.desc = desc;
|
||||
this.defval = defval;
|
||||
constructor(
|
||||
public id: string,
|
||||
public name: string,
|
||||
public platform: CustPlatform,
|
||||
public size: number,
|
||||
public desc: string[],
|
||||
public defval: number,
|
||||
minmax: [number, number] = [0, 1_000_000],
|
||||
public step: number = 1,
|
||||
public zeroable: boolean = true) {
|
||||
this.min = minmax[0];
|
||||
this.max = minmax[1];
|
||||
this.step = step;
|
||||
this.zeroable = zeroable;
|
||||
};
|
||||
|
||||
validate(): boolean {
|
||||
let tip = new ErrorToolTip(this.id);
|
||||
tip.clear();
|
||||
let tip = new ErrorToolTip(this.id).clear();
|
||||
if (Number.isNaN(this.value) || this.value === undefined) {
|
||||
tip.setMsg(`Invalid value: Not a number`);
|
||||
tip.show();
|
||||
tip.setMsg(`Invalid value: Not a number`).show();
|
||||
return false;
|
||||
}
|
||||
if (this.zeroable && this.value == 0)
|
||||
return true;
|
||||
if (this.value < this.min || this.value > this.max) {
|
||||
tip.setMsg(`Expected range: [${this.min}, ${this.max}], got ${this.value}.`);
|
||||
tip.show();
|
||||
tip.setMsg(`Expected range: [${this.min}, ${this.max}], got ${this.value}.`).show();
|
||||
return false;
|
||||
}
|
||||
if (this.value % this.step != 0) {
|
||||
tip.setMsg(`${this.value} % ${this.step} ≠ 0`);
|
||||
tip.show();
|
||||
tip.setMsg(`${this.value} % ${this.step} ≠ 0`).show();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -64,7 +52,7 @@ class CustEntry {
|
||||
}
|
||||
|
||||
updateValueFromElement() {
|
||||
this.value = Number(this.getInputElement()!.value);
|
||||
this.value = Number(this.getInputElement()?.value);
|
||||
}
|
||||
|
||||
isAvailableFor(platform: CustPlatform): boolean {
|
||||
@@ -92,20 +80,23 @@ class CustEntry {
|
||||
|
||||
// Description in blockquote style
|
||||
let desc = document.createElement("blockquote");
|
||||
desc.innerHTML = this.desc;
|
||||
desc.innerHTML = "<ul>" + this.desc.map(i => `<li>${i}</li>`).join('') + "</ul>";
|
||||
desc.setAttribute("for", this.id);
|
||||
grid.appendChild(desc);
|
||||
|
||||
document.getElementById("form")!.appendChild(grid);
|
||||
document.getElementById("config-list-basic")!.appendChild(grid);
|
||||
|
||||
let tooltip = new ErrorToolTip(this.id);
|
||||
tooltip.addChangeListener();
|
||||
new ErrorToolTip(this.id).addChangeListener();
|
||||
}
|
||||
input.value = String(this.value);
|
||||
}
|
||||
|
||||
setElementValue() {
|
||||
this.getInputElement()!.value = String(this.value!);
|
||||
}
|
||||
|
||||
setElementDefaultValue() {
|
||||
(document.getElementById(this.id) as HTMLInputElement).value = String(this.defval);
|
||||
this.getInputElement()!.value = String(this.defval);
|
||||
}
|
||||
|
||||
removeElement() {
|
||||
@@ -136,9 +127,9 @@ var CustTable: Array<CustEntry> = [
|
||||
"DRAM Timing",
|
||||
CustPlatform.Mariko,
|
||||
2,
|
||||
"<li><b>0</b>: AUTO_ADJ_MARIKO_SAFE: Auto adjust timings for LPDDR4 ≤3733 Mbps specs, 8Gb density. (Default)</li>\
|
||||
<li><b>1</b>: AUTO_ADJ_MARIKO_4266: Auto adjust timings for LPDDR4X 4266 Mbps specs, 8Gb density.</li>\
|
||||
<li><b>2</b>: NO_ADJ_ALL: No timing adjustment for both Erista and Mariko. Might achieve better performance on Mariko but lower maximum frequency is expected.",
|
||||
["<b>0</b>: AUTO_ADJ_MARIKO_SAFE: Auto adjust timings for LPDDR4 ≤3733 Mbps specs, 8Gb density. (Default)",
|
||||
"<b>1</b>: AUTO_ADJ_MARIKO_4266: Auto adjust timings for LPDDR4X 4266 Mbps specs, 8Gb density.",
|
||||
"<b>2</b>: NO_ADJ_ALL: No timing adjustment for both Erista and Mariko. Might achieve better performance on Mariko but lower maximum frequency is expected."],
|
||||
0,
|
||||
[0, 2],
|
||||
1
|
||||
@@ -148,8 +139,8 @@ var CustTable: Array<CustEntry> = [
|
||||
"Mariko CPU Max Clock in kHz",
|
||||
CustPlatform.Mariko,
|
||||
4,
|
||||
"<li>System default: 1785000</li>\
|
||||
<li>2397000 might be unreachable for some SoCs.</li>",
|
||||
["System default: 1785000",
|
||||
"2397000 might be unreachable for some SoCs."],
|
||||
2397_000,
|
||||
[1785_000, 3000_000],
|
||||
1,
|
||||
@@ -159,9 +150,9 @@ var CustTable: Array<CustEntry> = [
|
||||
"Mariko CPU Boost Clock in kHz",
|
||||
CustPlatform.Mariko,
|
||||
4,
|
||||
"<li>System default: 1785000</li>\
|
||||
<li>Boost clock will be applied when applications request higher CPU frequency for quicker loading.</li>\
|
||||
<li>This will be set regardless of whether sys-clk is enabled.</li>",
|
||||
["System default: 1785000",
|
||||
"Boost clock will be applied when applications request higher CPU frequency for quicker loading.",
|
||||
"This will be set regardless of whether sys-clk is enabled."],
|
||||
1785_000,
|
||||
[1020_000, 3000_000],
|
||||
1,
|
||||
@@ -172,8 +163,8 @@ var CustTable: Array<CustEntry> = [
|
||||
"Mariko CPU Max Voltage in mV",
|
||||
CustPlatform.Mariko,
|
||||
4,
|
||||
"<li>System default: 1120</li>\
|
||||
<li>Acceptable range: 1100 ≤ x ≤ 1300</li>",
|
||||
["System default: 1120",
|
||||
"Acceptable range: 1100 ≤ x ≤ 1300"],
|
||||
1235,
|
||||
[1100, 1300],
|
||||
5
|
||||
@@ -183,9 +174,9 @@ var CustTable: Array<CustEntry> = [
|
||||
"Mariko GPU Max Clock in kHz",
|
||||
CustPlatform.Mariko,
|
||||
4,
|
||||
"<li>System default: 921600</li>\
|
||||
<li>Tegra X1+ official maximum: 1267200</li>\
|
||||
<li>1305600 might be unreachable for some SoCs.</li>",
|
||||
["System default: 921600",
|
||||
"Tegra X1+ official maximum: 1267200",
|
||||
"1305600 might be unreachable for some SoCs."],
|
||||
1305_600,
|
||||
[768_000, 1536_000],
|
||||
100,
|
||||
@@ -195,8 +186,8 @@ var CustTable: Array<CustEntry> = [
|
||||
"Mariko RAM Max Clock in kHz",
|
||||
CustPlatform.Mariko,
|
||||
4,
|
||||
"<li>Values should be ≥ 1600000, and divided evenly by 3200.</li>\
|
||||
<li><b>WARNING:</b> RAM overclock could be UNSTABLE if timing parameters are not suitable for your DRAM</li>",
|
||||
["Values should be ≥ 1600000, and divided evenly by 3200.",
|
||||
"<b>WARNING:</b> RAM overclock could be UNSTABLE if timing parameters are not suitable for your DRAM."],
|
||||
1996_800,
|
||||
[1600_000, 2400_000],
|
||||
3200,
|
||||
@@ -206,11 +197,11 @@ var CustTable: Array<CustEntry> = [
|
||||
"EMC Vddq (Mariko Only) Voltage in uV",
|
||||
CustPlatform.Mariko,
|
||||
4,
|
||||
"<li>Acceptable range: 550000 ≤ x ≤ 650000</li>\
|
||||
<li>Value should be divided evenly by 5000</li>\
|
||||
<li>Default: 600000</li>\
|
||||
<li>Not enabled by default.</li>\
|
||||
<li>This will not work without sys-clk-OC.</li>",
|
||||
["Acceptable range: 550000 ≤ x ≤ 650000",
|
||||
"Value should be divided evenly by 5000",
|
||||
"Default: 600000",
|
||||
"Not enabled by default.",
|
||||
"This will not work without sys-clk-OC."],
|
||||
0,
|
||||
[550_000, 650_000],
|
||||
5000,
|
||||
@@ -220,7 +211,7 @@ var CustTable: Array<CustEntry> = [
|
||||
"Erista CPU Max Voltage in mV",
|
||||
CustPlatform.Erista,
|
||||
4,
|
||||
"<li>Acceptable range: 1100 ≤ x ≤ 1300</li>",
|
||||
["Acceptable range: 1100 ≤ x ≤ 1300"],
|
||||
1235,
|
||||
[1100, 1300],
|
||||
1,
|
||||
@@ -230,8 +221,8 @@ var CustTable: Array<CustEntry> = [
|
||||
"Erista RAM Max Clock in kHz",
|
||||
CustPlatform.Erista,
|
||||
4,
|
||||
"<li>Values should be ≥ 1600000, and divided evenly by 3200.</li>\
|
||||
<li><b>WARNING:</b> RAM overclock could be UNSTABLE if timing parameters are not suitable for your DRAM</li>",
|
||||
["Values should be ≥ 1600000, and divided evenly by 3200.",
|
||||
"<b>WARNING:</b> RAM overclock could be UNSTABLE if timing parameters are not suitable for your DRAM"],
|
||||
1862_400,
|
||||
[1600_000, 2400_000],
|
||||
3200,
|
||||
@@ -241,314 +232,363 @@ var CustTable: Array<CustEntry> = [
|
||||
"EMC Vddq (Erista Only) & RAM Vdd2 Voltage in uV",
|
||||
CustPlatform.All,
|
||||
4,
|
||||
"<li>Acceptable range: 1100000 ≤ x ≤ 1250000, and it should be divided evenly by 12500.</li>\
|
||||
<li>Erista Default (HOS): 1125000 (bootloader: 1100000)</li>\
|
||||
<li>Mariko Default: 1100000 (It will not work without sys-clk-OC)</li>\
|
||||
<li>Not enabled by default</li>",
|
||||
["Acceptable range: 1100000 ≤ x ≤ 1250000, and it should be divided evenly by 12500.",
|
||||
"Erista Default (HOS): 1125000 (bootloader: 1100000)",
|
||||
"Mariko Default: 1100000 (It will not work without sys-clk-OC)",
|
||||
"Not enabled by default"],
|
||||
0,
|
||||
[1100_000, 1250_000],
|
||||
12500,
|
||||
),
|
||||
];
|
||||
|
||||
function FindMagicOffset(buffer) {
|
||||
let view = new DataView(buffer);
|
||||
for (let i = 0; i < view.byteLength; i += 4) {
|
||||
if (view.getUint32(i, true) == 0x54535543) { // "CUST"
|
||||
return i;
|
||||
}
|
||||
}
|
||||
throw new Error("Invalid loader.kip file");
|
||||
}
|
||||
|
||||
class ErrorToolTip {
|
||||
id: string;
|
||||
message: string | null;
|
||||
element: HTMLElement | null;
|
||||
|
||||
constructor(id: string, msg?: string) {
|
||||
constructor(public id: string, public msg?: string) {
|
||||
this.id = id;
|
||||
this.element = document.getElementById(id);
|
||||
if (msg) { this.setMsg(msg); }
|
||||
};
|
||||
|
||||
setMsg(msg: string) {
|
||||
this.message = msg;
|
||||
setMsg(msg: string): ErrorToolTip {
|
||||
this.msg = msg;
|
||||
return this;
|
||||
}
|
||||
|
||||
show() {
|
||||
if (this.element) {
|
||||
this.element.setAttribute("aria-invalid", "true");
|
||||
if (this.message) {
|
||||
this.element.setAttribute("title", this.message);
|
||||
this.element.parentElement!.setAttribute("data-tooltip", this.message);
|
||||
this.element.parentElement!.setAttribute("data-placement", "top");
|
||||
}
|
||||
show(): ErrorToolTip {
|
||||
this.element?.setAttribute("aria-invalid", "true");
|
||||
if (this.msg) {
|
||||
this.element?.setAttribute("title", this.msg);
|
||||
this.element?.parentElement?.setAttribute("data-tooltip", this.msg);
|
||||
this.element?.parentElement?.setAttribute("data-placement", "top");
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
||||
clear() {
|
||||
if (this.element) {
|
||||
this.element.removeAttribute("aria-invalid");
|
||||
this.element.removeAttribute("title");
|
||||
this.element.parentElement!.removeAttribute("data-tooltip");
|
||||
this.element.parentElement!.removeAttribute("data-placement");
|
||||
}
|
||||
clear(): ErrorToolTip {
|
||||
this.element?.removeAttribute("aria-invalid");
|
||||
this.element?.removeAttribute("title");
|
||||
this.element?.parentElement?.removeAttribute("data-tooltip");
|
||||
this.element?.parentElement?.removeAttribute("data-placement");
|
||||
return this;
|
||||
}
|
||||
|
||||
addChangeListener() {
|
||||
if (this.element) {
|
||||
this.element.addEventListener('change', (_evt) => {
|
||||
let obj = CustTable.filter((obj) => { return obj.id === this.id; })[0];
|
||||
obj.value = Number((this.element as HTMLInputElement).value);
|
||||
obj.validate();
|
||||
});
|
||||
}
|
||||
this.element?.addEventListener('change', (_evt) => {
|
||||
let obj = CustTable.filter((obj) => { return obj.id === this.id; })[0];
|
||||
obj.value = Number((this.element as HTMLInputElement).value);
|
||||
obj.validate();
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
function SaveCust(buffer) {
|
||||
let view = new DataView(buffer);
|
||||
let storage = {};
|
||||
class CustStorage {
|
||||
storage: { [key: string]: (number | undefined) } = {};
|
||||
readonly key = "last_saved";
|
||||
|
||||
CustTable.forEach(i => {
|
||||
i.updateValueFromElement();
|
||||
if (!i.validate()) {
|
||||
document.getElementById(i.id)!.focus();
|
||||
throw new Error(`Invalid ${i.name}`);
|
||||
}
|
||||
if (!i.offset) {
|
||||
document.getElementById(i.id)!.focus();
|
||||
throw new Error(`Failed to get offset for ${i.name}`);;
|
||||
}
|
||||
switch (i.size) {
|
||||
case 2:
|
||||
view.setUint16(i.offset, i.value!, true);
|
||||
break;
|
||||
case 4:
|
||||
view.setUint32(i.offset, i.value!, true);
|
||||
break;
|
||||
default:
|
||||
document.getElementById(i.id)!.focus();
|
||||
throw new Error(`Unknown size at ${i.name}`);
|
||||
}
|
||||
storage[i.id] = i.value;
|
||||
});
|
||||
|
||||
storage["custRev"] = CUST_REV;
|
||||
localStorage.setItem("last_saved", JSON.stringify(storage));
|
||||
let a = document.createElement("a");
|
||||
a.href = window.URL.createObjectURL(new Blob([buffer], { type: "application/octet-stream" }));
|
||||
a.download = "loader.kip";
|
||||
a.click();
|
||||
}
|
||||
|
||||
function LastSaved() {
|
||||
let storage = localStorage.getItem("last_saved");
|
||||
if (!storage) {
|
||||
return false;
|
||||
}
|
||||
let sObj = JSON.parse(storage);
|
||||
if (!sObj["custRev"] || sObj["custRev"] != CUST_REV) {
|
||||
localStorage.removeItem("last_saved");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function CustNavTabsInit() {
|
||||
const custNavTabs = Array.from(document.querySelectorAll(`nav[role="tab-control"] > button`)) as HTMLElement[];
|
||||
custNavTabs.forEach(i => {
|
||||
i.removeAttribute("disabled");
|
||||
let platform = Number(i.getAttribute("data-filter")!) as CustPlatform;
|
||||
i.addEventListener('click', (_evt) => {
|
||||
const unfocusedClasses = ["outline"];
|
||||
i.classList.remove(...unfocusedClasses);
|
||||
custNavTabs.filter(j => j != i).forEach(k => k.classList.add(...unfocusedClasses));
|
||||
|
||||
CustTable.forEach(e => {
|
||||
if (e.isAvailableFor(platform)) {
|
||||
e.showElement();
|
||||
} else {
|
||||
e.hideElement();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function UpdateHTMLForm() {
|
||||
CustTable.forEach(i => i.createElement());
|
||||
|
||||
let default_btn = document.getElementById("load_default")!;
|
||||
default_btn.removeAttribute("disabled");
|
||||
default_btn.addEventListener('click', () => {
|
||||
CustTable.forEach(i => i.setElementDefaultValue());
|
||||
});
|
||||
|
||||
let last_btn = document.getElementById("load_saved")!;
|
||||
if (LastSaved()) {
|
||||
last_btn.style.removeProperty("display");
|
||||
last_btn.removeAttribute("disabled");
|
||||
last_btn.addEventListener('click', () => {
|
||||
// Load last saved from localStorage
|
||||
let dict = JSON.parse(localStorage.getItem("last_saved")!);
|
||||
delete dict["custRev"];
|
||||
dict.forEach((key: string) => (document.getElementById(key) as HTMLInputElement).value = key);
|
||||
});
|
||||
} else {
|
||||
last_btn.style.setProperty("display", "none");
|
||||
}
|
||||
|
||||
let save_btn = document.getElementById("save")!;
|
||||
save_btn.removeAttribute("disabled");
|
||||
save_btn.addEventListener('click', () => {
|
||||
try {
|
||||
SaveCust(buffer);
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
alert(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function ParseCust(magicOffset: number, buffer: ArrayBuffer) {
|
||||
let view = new DataView(buffer);
|
||||
let offset = magicOffset + 4;
|
||||
let rev = view.getUint16(offset, true);
|
||||
offset += 2;
|
||||
if (rev != CUST_REV) {
|
||||
throw new Error(`Unsupported custRev, expected: ${CUST_REV}, got ${rev}`);
|
||||
}
|
||||
document.getElementById("cust_rev")!.innerHTML = `Cust V${CUST_REV} is loaded.`;
|
||||
|
||||
CustTable.forEach(i => {
|
||||
i.offset = offset;
|
||||
switch (i.size) {
|
||||
case 2:
|
||||
i.value = view.getUint16(offset, true);
|
||||
break;
|
||||
case 4:
|
||||
i.value = view.getUint32(offset, true);
|
||||
break;
|
||||
default:
|
||||
document.getElementById(i.id)!.focus();
|
||||
throw new Error("Unknown size at " + i);
|
||||
}
|
||||
offset += i.size;
|
||||
i.validate();
|
||||
});
|
||||
}
|
||||
|
||||
const fileInput = document.getElementById("file") as HTMLInputElement;
|
||||
fileInput.addEventListener('change', (event) => {
|
||||
let reader = new FileReader();
|
||||
reader.readAsArrayBuffer((event.target as HTMLInputElement).files![0]);
|
||||
reader.onloadend = (progEvent) => {
|
||||
if (progEvent.target!.readyState === FileReader.DONE) {
|
||||
buffer = (progEvent.target!.result!) as ArrayBuffer;
|
||||
try {
|
||||
let offset = FindMagicOffset(buffer);
|
||||
CustTable.forEach(i => i.removeElement());
|
||||
ParseCust(offset, buffer);
|
||||
CustNavTabsInit();
|
||||
UpdateHTMLForm();
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
alert(e);
|
||||
fileInput.value = "";
|
||||
updateFromTable() {
|
||||
CustTable.forEach(i => {
|
||||
i.updateValueFromElement();
|
||||
if (!i.validate()) {
|
||||
i.getInputElement()?.focus();
|
||||
throw new Error(`Invalid ${i.name}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
/* GitHub Release fetch */
|
||||
type ReleaseInfo = {
|
||||
OCSuiteVer: string;
|
||||
LoaderKipUrl: string;
|
||||
LoaderKipTime: string;
|
||||
SdOutZipUrl: string;
|
||||
SdOutZipTime: string;
|
||||
AMSVer: string;
|
||||
AMSUrl: string;
|
||||
};
|
||||
|
||||
async function fetchRelease(): Promise<ReleaseInfo | void> {
|
||||
try {
|
||||
const responseFromSuite = await fetch('https://api.github.com/repos/KazushiMe/Switch-OC-Suite/releases/latest', {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
},
|
||||
});
|
||||
if (!responseFromSuite.ok) {
|
||||
throw new Error(`Failed to fetch latest release info from GitHub: ${responseFromSuite.status}`);
|
||||
|
||||
this.storage = {};
|
||||
let kv = Object.fromEntries(CustTable.map((i) => [i.id, i.value]));
|
||||
Object.keys(kv)
|
||||
.forEach(k => this.storage[k] = kv[k]);
|
||||
}
|
||||
|
||||
setTable() {
|
||||
let keys = Object.keys(this.storage);
|
||||
keys.forEach(k => CustTable.filter(i => i.id == k)[0].value = this.storage[k]);
|
||||
|
||||
// Set default for missing values
|
||||
CustTable.filter(i => !keys.includes(i.id))
|
||||
.forEach(i => i.value = i.defval);
|
||||
|
||||
CustTable.forEach(i => {
|
||||
if (!i.validate()) {
|
||||
i.getInputElement()?.focus();
|
||||
throw new Error(`Invalid ${i.name}`);
|
||||
}
|
||||
i.setElementValue();
|
||||
});
|
||||
}
|
||||
|
||||
save() {
|
||||
localStorage.setItem(this.key, JSON.stringify(this.storage));
|
||||
}
|
||||
|
||||
load(): { [key: string]: (number | undefined) } | null {
|
||||
let s = localStorage.getItem(this.key);
|
||||
if (!s) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const resultFromSuite = await responseFromSuite.json();
|
||||
const latestVerFromSuite = resultFromSuite.tag_name;
|
||||
const amsVer = latestVerFromSuite.split(".").slice(0, 3).join(".");
|
||||
|
||||
const loaderKip = resultFromSuite.assets.filter((obj) => {
|
||||
return obj.name.endsWith("loader.kip");
|
||||
})[0];
|
||||
const sdOut = resultFromSuite.assets.filter((obj) => {
|
||||
return obj.name.endsWith(".zip");
|
||||
})[0];
|
||||
const amsReleaseUrl = `https://github.com/Atmosphere-NX/Atmosphere/releases/tags/${amsVer}`;
|
||||
|
||||
let info: ReleaseInfo = {
|
||||
OCSuiteVer: latestVerFromSuite,
|
||||
LoaderKipUrl: loaderKip.browser_download_url,
|
||||
LoaderKipTime: loaderKip.updated_at,
|
||||
SdOutZipUrl: sdOut.browser_download_url,
|
||||
SdOutZipTime: sdOut.updated_at,
|
||||
AMSVer: amsVer,
|
||||
AMSUrl: amsReleaseUrl
|
||||
};
|
||||
return info;
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
alert(e);
|
||||
let dict = JSON.parse(s);
|
||||
let keys = CustTable.map(i => i.id);
|
||||
let ignoredKeys: string[] = Object.keys(dict).filter(k => !keys.includes(k));
|
||||
if (ignoredKeys.length) {
|
||||
console.log(`Ignored: ${ignoredKeys}`);
|
||||
}
|
||||
Object.keys(dict)
|
||||
.filter(k => keys.includes(k))
|
||||
.forEach(k => this.storage[k] = dict[k]);
|
||||
return this.storage;
|
||||
}
|
||||
}
|
||||
|
||||
const isElementVisible = (element: HTMLElement): boolean => {
|
||||
let rect = element.getBoundingClientRect();
|
||||
return (
|
||||
rect.top > 0 &&
|
||||
rect.left > 0 &&
|
||||
rect.bottom - rect.height < (window.innerHeight || document.documentElement.clientHeight) &&
|
||||
rect.right - rect.width < (window.innerWidth || document.documentElement.clientWidth)
|
||||
);
|
||||
};
|
||||
class Cust {
|
||||
buffer: ArrayBuffer;
|
||||
view: DataView;
|
||||
beginOffset: number;
|
||||
storage: CustStorage = new CustStorage();
|
||||
readonly magic = 0x54535543; // "CUST"
|
||||
readonly magicLen = 4;
|
||||
|
||||
async function updateDownloadUrls() {
|
||||
// Wait until download buttons are visible
|
||||
while (!isElementVisible(document.getElementById("download_btn_grid")!)) {
|
||||
await new Promise(r => setTimeout(r, 1000));
|
||||
mapper : {[size: number]: { get: any, set: any }} = {
|
||||
2: {
|
||||
get: (offset: number) => this.view.getUint16(offset, true),
|
||||
set: (offset: number, value: number) => this.view.setUint16(offset, value, true),
|
||||
},
|
||||
4: {
|
||||
get: (offset: number) => this.view.getUint32(offset, true),
|
||||
set: (offset: number, value: number) => this.view.setUint32(offset, value, true) },
|
||||
};
|
||||
|
||||
const updateHref = (id: string, name: string, url: string) => {
|
||||
findMagicOffset() {
|
||||
this.view = new DataView(this.buffer);
|
||||
for (let offset = 0; offset < this.view.byteLength; offset += this.magicLen) {
|
||||
if (this.mapper[this.magicLen].get(offset) == this.magic) {
|
||||
this.beginOffset = offset;
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw new Error("Invalid loader.kip file");
|
||||
}
|
||||
|
||||
save() {
|
||||
this.storage.updateFromTable();
|
||||
CustTable.forEach(i => {
|
||||
if (!i.offset) {
|
||||
i.getInputElement()?.focus();
|
||||
throw new Error(`Failed to get offset for ${i.name}`);
|
||||
}
|
||||
let mapper = this.mapper[i.size];
|
||||
if (!mapper) {
|
||||
i.getInputElement()?.focus();
|
||||
throw new Error(`Unknown size at ${i.name}`);
|
||||
}
|
||||
mapper.set(i.offset, i.value!);
|
||||
});
|
||||
this.storage.save();
|
||||
|
||||
let a = document.createElement("a");
|
||||
a.href = window.URL.createObjectURL(new Blob([this.buffer], { type: "application/octet-stream" }));
|
||||
a.download = "loader.kip";
|
||||
a.click();
|
||||
|
||||
this.toggleLoadLastSavedBtn(true);
|
||||
}
|
||||
|
||||
removeHTMLForm() {
|
||||
CustTable.forEach(i => i.removeElement());
|
||||
}
|
||||
|
||||
toggleLoadLastSavedBtn(enable: boolean) {
|
||||
let last_btn = document.getElementById("load_saved")!;
|
||||
if (enable) {
|
||||
last_btn.addEventListener('click', () => {
|
||||
if (this.storage.load()) {
|
||||
this.storage.setTable();
|
||||
}
|
||||
});
|
||||
last_btn.style.removeProperty("display");
|
||||
last_btn.removeAttribute("disabled");
|
||||
} else {
|
||||
last_btn.style.setProperty("display", "none");
|
||||
}
|
||||
}
|
||||
|
||||
createHTMLForm() {
|
||||
CustTable.forEach(i => i.createElement());
|
||||
|
||||
let default_btn = document.getElementById("load_default")!;
|
||||
default_btn.removeAttribute("disabled");
|
||||
default_btn.addEventListener('click', () => {
|
||||
CustTable.forEach(i => i.setElementDefaultValue());
|
||||
});
|
||||
|
||||
this.toggleLoadLastSavedBtn(this.storage.load() !== null);
|
||||
|
||||
let save_btn = document.getElementById("save")!;
|
||||
save_btn.removeAttribute("disabled");
|
||||
save_btn.addEventListener('click', () => {
|
||||
try {
|
||||
this.save();
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
alert(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
initCustTabs() {
|
||||
const custTabs = Array.from(document.querySelectorAll(`nav[role="tablist"] > button`)) as HTMLElement[];
|
||||
custTabs.forEach(tab => {
|
||||
tab.removeAttribute("disabled");
|
||||
let platform = Number(tab.getAttribute("data-platform")!) as CustPlatform;
|
||||
tab.addEventListener('click', (_evt) => {
|
||||
// Set other tabs to unfocused state
|
||||
const unfocusedClasses = ["outline"];
|
||||
tab.classList.remove(...unfocusedClasses);
|
||||
let otherTabs = custTabs.filter(j => j != tab);
|
||||
otherTabs.forEach(k => k.classList.add(...unfocusedClasses));
|
||||
|
||||
CustTable.forEach(e => {
|
||||
e.isAvailableFor(platform) ? e.showElement() : e.hideElement();
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
parse() {
|
||||
let offset = this.beginOffset + this.magicLen;
|
||||
let revLen = 2;
|
||||
let rev = this.mapper[revLen].get(offset);
|
||||
if (rev != CUST_REV) {
|
||||
throw new Error(`Unsupported custRev, expected: ${CUST_REV}, got ${rev}`);
|
||||
}
|
||||
offset += revLen;
|
||||
document.getElementById("cust_rev")!.innerHTML = `Cust v${CUST_REV} is loaded.`;
|
||||
|
||||
CustTable.forEach(i => {
|
||||
i.offset = offset;
|
||||
let mapper = this.mapper[i.size];
|
||||
if (!mapper) {
|
||||
i.getInputElement()?.focus();
|
||||
throw new Error(`Unknown size at ${i}`);
|
||||
}
|
||||
i.value = mapper.get(offset);
|
||||
offset += i.size;
|
||||
i.validate();
|
||||
});
|
||||
}
|
||||
|
||||
load(buffer: ArrayBuffer) {
|
||||
try {
|
||||
this.buffer = buffer;
|
||||
this.findMagicOffset();
|
||||
this.removeHTMLForm();
|
||||
this.parse();
|
||||
this.initCustTabs();
|
||||
this.createHTMLForm();
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
alert(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* GitHub Release fetch */
|
||||
class ReleaseAsset {
|
||||
downloadUrl: string;
|
||||
updatedAt: string;
|
||||
|
||||
constructor (obj: { browser_download_url: string; updated_at: string; }) {
|
||||
this.downloadUrl = obj.browser_download_url;
|
||||
this.updatedAt = obj.updated_at;
|
||||
};
|
||||
};
|
||||
|
||||
class ReleaseInfo {
|
||||
ocVer: string;
|
||||
amsVer: string;
|
||||
loaderKipAsset: ReleaseAsset;
|
||||
sdOutZipAsset: ReleaseAsset;
|
||||
amsUrl: string;
|
||||
|
||||
readonly ocLatestApi = "https://api.github.com/repos/KazushiMe/Switch-OC-Suite/releases/latest";
|
||||
|
||||
async load() {
|
||||
try {
|
||||
this.parseOcResponse(await this.responseFromApi(this.ocLatestApi).catch());
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
alert(e);
|
||||
}
|
||||
};
|
||||
|
||||
async responseFromApi(apiUrl: string) : Promise<any> {
|
||||
const response = await fetch(apiUrl, { method: 'GET', headers: { Accept: 'application/json' } } );
|
||||
if (response.ok) {
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
throw new Error(`Failed to connect to "${apiUrl}": ${response.status}`);
|
||||
};
|
||||
|
||||
parseOcResponse(response) {
|
||||
this.ocVer = response.tag_name;
|
||||
this.amsVer = this.ocVer.split(".").slice(0, 3).join(".");
|
||||
this.loaderKipAsset = new ReleaseAsset(response.assets.filter( a => a.name.endsWith("loader.kip") )[0]);
|
||||
this.sdOutZipAsset = new ReleaseAsset(response.assets.filter( a => a.name.endsWith(".zip") )[0]);
|
||||
this.amsUrl = `https://github.com/Atmosphere-NX/Atmosphere/releases/tags/${this.amsVer}`;
|
||||
};
|
||||
};
|
||||
|
||||
class DownloadSection {
|
||||
readonly element: HTMLElement = document.getElementById("download_btn_grid")!;
|
||||
|
||||
async load() {
|
||||
while(!this.isVisible()) {
|
||||
await new Promise(r => setTimeout(r, 1000));
|
||||
}
|
||||
|
||||
const info = new ReleaseInfo()
|
||||
await info.load();
|
||||
this.update("loader_kip_btn", `loader.kip <b>${info.ocVer}</b><br>${info.loaderKipAsset.updatedAt}`, info.loaderKipAsset.downloadUrl);
|
||||
this.update("sdout_zip_btn", `SdOut.zip <b>${info.ocVer}</b><br>${info.sdOutZipAsset.updatedAt}`, info.sdOutZipAsset.downloadUrl);
|
||||
this.update("ams_btn", `Atmosphere-NX <b>${info.amsVer}</b>`, info.amsUrl);
|
||||
}
|
||||
|
||||
isVisible(): boolean {
|
||||
let rect = this.element.getBoundingClientRect();
|
||||
return (
|
||||
rect.top > 0 &&
|
||||
rect.left > 0 &&
|
||||
rect.bottom - rect.height < (window.innerHeight || document.documentElement.clientHeight) &&
|
||||
rect.right - rect.width < (window.innerWidth || document.documentElement.clientWidth)
|
||||
);
|
||||
}
|
||||
|
||||
update(id: string, name: string, url: string) {
|
||||
let element = document.getElementById(id)!;
|
||||
element.innerHTML = name;
|
||||
element.removeAttribute("aria-busy");
|
||||
element.setAttribute("href", url);
|
||||
};
|
||||
|
||||
let info = await fetchRelease();
|
||||
if (info) {
|
||||
const loaderKipName = `loader.kip <b>${info.OCSuiteVer}</b><br>${info.LoaderKipTime}`;
|
||||
updateHref("loader_kip_btn", loaderKipName, info.LoaderKipUrl);
|
||||
|
||||
const sdOutName = `SdOut.zip <b>${info.OCSuiteVer}</b><br>${info.SdOutZipTime}`;
|
||||
updateHref("sdout_zip_btn", sdOutName, info.SdOutZipUrl);
|
||||
|
||||
const amsName = `Atmosphere-NX <b>${info.AMSVer}</b>`;
|
||||
updateHref("ams_btn", amsName, info.AMSUrl);
|
||||
}
|
||||
}
|
||||
|
||||
const fileInput = document.getElementById("file") as HTMLInputElement;
|
||||
fileInput.addEventListener('change', (event) => {
|
||||
var cust: Cust = new Cust();
|
||||
// User canceled or non files selected
|
||||
if (!event.target || !(event.target as HTMLInputElement).files) {
|
||||
return;
|
||||
}
|
||||
let reader = new FileReader();
|
||||
reader.readAsArrayBuffer((event.target as HTMLInputElement).files![0]);
|
||||
reader.onloadend = (progEvent) => {
|
||||
if (progEvent.target!.readyState == FileReader.DONE) {
|
||||
cust.load(progEvent.target!.result! as ArrayBuffer);
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
addEventListener('DOMContentLoaded', async (_evt) => {
|
||||
await updateDownloadUrls();
|
||||
});
|
||||
await new DownloadSection().load();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user