ayunWebEPK/index.html

309 lines
10 KiB
HTML
Raw Normal View History

2022-07-26 10:16:23 -07:00
<!DOCTYPE html>
<html>
<head>
<script async src="https://arc.io/widget.min.js#HEPCFa3K"></script>
2022-07-26 10:16:23 -07:00
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>ayunWebEPK</title>
<script type="text/javascript" src="jszip.min.js"></script>
<script type="text/javascript" src="pako_deflate.min.js"></script>
<script type="text/javascript" src="sha1.min.js"></script>
<link rel="preconnect" href="https://fonts.googleapis.com"/>
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin/>
<link href="https://fonts.googleapis.com/css2?family=Nunito&display=swap" rel="stylesheet"/>
<style>
a:not([href]) {
display: none;
}
*, ::file-selector-button {
font-family: 'Nunito', sans-serif;
}
body {
background-color: #111111;
color: #dddddd;
}
input[disabled] {
opacity: 0.8;
}
2022-08-12 17:21:14 -07:00
input[type=checkbox] {
-webkit-appearance: initial;
-moz-appearance: initial;
appearance: initial;
width: 1.5em;
height: 1.5em;
margin: 0;
}
input[type=checkbox]:checked {
background-color: #eeeeee;
}
input[type=checkbox]:before {
content: ':>';
text-align: center;
color: #dddddd;
margin-left: 0.3em;
}
input[type=checkbox]:checked:before {
color: #343434;
}
input[type=checkbox]:after {
content: 'No';
color: #dddddd;
margin-left: 0.6em;
}
input[type=checkbox]:checked:after {
content: 'Yes';
}
@supports (-webkit-touch-callout: none) or (background: -webkit-named-image(i)) {
input[type=checkbox]:after {
content: 'no';
}
input[type=checkbox]:checked:after {
content: 'yes';
}
}
@supports (-moz-appearance: none) {
input[type=checkbox]:after {
content: 'No.';
}
input[type=checkbox]:checked:after {
content: 'Yes.';
}
}
::file-selector-button, progress, input[type=checkbox] {
2022-07-26 10:16:23 -07:00
background-color: #343434;
color: #eeeeee;
border: 1px solid #eeeeee;
border-radius: 4px;
}
2022-08-12 17:21:14 -07:00
a, input[type=file] {
2022-07-26 10:16:23 -07:00
color: #dddddd;
}
progress {
height: 1em;
2022-08-12 17:21:14 -07:00
}
progress, ::-webkit-file-upload-button {
2022-07-26 10:16:23 -07:00
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
}
progress[value="0"][max="1"] {
display: none;
}
::-moz-progress-bar {
background-color: #eeeeee;
}
::-webkit-progress-value {
background-color: #eeeeee;
}
::-webkit-progress-bar {
background-color: #343434;
border-radius: 4px;
}
</style>
</head>
<body>
<h1>ayunWebEPK</h1>
<p>Compile EPK files in your browser!</p>
2022-08-12 17:21:14 -07:00
Use legacy format: <input type="checkbox"/>
<br/>
2022-07-26 10:16:23 -07:00
Select resources folder: <input type="file" onchange="selectFile(this);" directory webkitdirectory/>
<br/>
2022-07-26 10:25:12 -07:00
<i>~or~</i>
<br/>
Select .ZIP archive of resources: <input type="file" onchange="selectFile(this);" accept=".zip"/>
<br/>
2022-07-26 10:16:23 -07:00
<progress value="0" max="1"></progress>
2022-08-12 17:21:14 -07:00
<a download="my-cool.epk">Download your EPK!</a>
2022-07-26 10:16:23 -07:00
<script>
2022-08-12 17:21:14 -07:00
// https://stackoverflow.com/a/18639903/6917520
const crc32 = (function() {
let table = new Uint32Array(256);
for (let i = 256; i--;) {
let tmp = i;
for (let k = 8; k--;) {
tmp = tmp & 1 ? 3988292384 ^ tmp >>> 1 : tmp >>> 1;
}
table[i] = tmp;
}
return function(data) {
let crc = -1;
for (let i = 0, l = data.length; i < l; i++) {
crc = crc >>> 8 ^ table[crc & 255 ^ data[i]];
}
return (crc ^ -1) >>> 0;
};
})();
2022-07-26 10:16:23 -07:00
const downloadLink = document.querySelector('a');
const progressBar = document.querySelector('progress');
const fileElems = document.querySelectorAll('input[type=file]');
2022-08-12 17:21:14 -07:00
const oldBox = document.querySelector('input[type=checkbox]');
2022-07-26 10:41:18 -07:00
2022-07-26 10:16:23 -07:00
function concatTypedArrays(a, b) {
const c = new (a.constructor)(a.length + b.length);
c.set(a, 0);
c.set(b, a.length);
return c;
}
2022-07-26 10:41:18 -07:00
// https://stackoverflow.com/a/68703218/6917520
function prefix(words) {
// check border cases size 1 array and empty first word)
if (!words[0] || words.length == 1) return words[0] || "";
let i = 0;
// while all words have the same character at position i, increment i
while(words[0][i] && words.every(w => w[i] === words[0][i]))
i++;
// prefix is the substring from the beginning to the last successfully checked i
return words[0].substr(0, i);
}
2022-07-26 10:16:23 -07:00
const textEncoder = new TextEncoder();
2022-08-12 17:21:14 -07:00
function generateLongArray(num) {
return Uint8Array.of((num >>> 56) & 0xFF, (num >>> 48) & 0xFF, (num >>> 40) & 0xFF, (num >>> 32) & 0xFF, (num >>> 24) & 0xFF, (num >>> 16) & 0xFF, (num >>> 8) & 0xFF, num & 0xFF);
}
2022-07-26 10:16:23 -07:00
function generateIntArray(num) {
return Uint8Array.of((num >>> 24) & 0xFF, (num >>> 16) & 0xFF, (num >>> 8) & 0xFF, num & 0xFF);
}
function generateShortArray(num) {
return Uint8Array.of((num >>> 8) & 0xFF, num & 0xFF);
}
function generateUTF(str) {
return concatTypedArrays(generateShortArray(str.length), textEncoder.encode(str));
}
2022-08-12 17:21:14 -07:00
function generateUTFByte(str) {
return concatTypedArrays(Uint8Array.of(str.length), textEncoder.encode(str));
}
2022-07-26 10:16:23 -07:00
let comment = '# eaglercraft package file - generated with ayunWebEPK by ayunami2000';
2022-08-12 17:21:14 -07:00
let commentNew = '\n\n # Eagler EPK v2.0 (c) $$YEAR$$ Calder Young\n # generated with ayunWebEPK by ayunami2000';
2022-07-26 10:16:23 -07:00
let baseEPK = textEncoder.encode('EAGPKG!!');
baseEPK = concatTypedArrays(baseEPK, generateUTF(comment));
2022-08-12 17:21:14 -07:00
let baseEPKNew = textEncoder.encode('EAGPKG$$');
baseEPKNew = concatTypedArrays(baseEPKNew, generateUTFByte('ver2.0'));
baseEPKNew = concatTypedArrays(baseEPKNew, generateUTFByte('my-cool.epk'));
2022-07-26 10:16:23 -07:00
let currentEPK = null;
function selectFile(fileElem) {
downloadLink.removeAttribute('href');
fileElems.forEach(elem => elem.disabled = true);
2022-08-12 17:21:14 -07:00
oldBox.disabled = true;
if (!oldBox.checked) {
currentEPK = textEncoder.encode('HEAD');
currentEPK = concatTypedArrays(currentEPK, generateUTFByte('file-type'));
const fileType = 'epk/resources';
currentEPK = concatTypedArrays(currentEPK, generateIntArray(fileType.length));
currentEPK = concatTypedArrays(currentEPK, textEncoder.encode(fileType));
currentEPK = concatTypedArrays(currentEPK, Uint8Array.of(62));
}
2022-07-26 10:16:23 -07:00
if (fileElem.files.length == 1) {
const zipFile = fileElem.files[0];
2022-07-26 10:41:18 -07:00
const reader = new FileReader();
2022-07-26 10:16:23 -07:00
reader.onload = function(e) {
const zip = new JSZip();
zip.loadAsync(e.target.result).then(function(zip) {
progressBar.max = Object.keys(zip.files).length;
2022-07-26 10:41:18 -07:00
const stripFolder = prefix(Object.keys(zip.files));
2022-07-26 10:16:23 -07:00
let i = 0;
for (let fileName in zip.files) {
if (fileName.endsWith('/')) {
progressBar.value = i++;
if (i == progressBar.max) {
wrapItUp(fileElem);
}
continue;
}
zip.files[fileName].async('uint8array').then(function(data) {
2022-07-26 10:41:18 -07:00
processFile(stripFolder.length == 0 ? fileName : fileName.slice(fileName.indexOf(stripFolder) + stripFolder.length), data);
2022-07-26 10:16:23 -07:00
progressBar.value = i++;
if (i == progressBar.max) {
wrapItUp(fileElem);
}
});
}
});
};
reader.readAsArrayBuffer(zipFile);
} else if (fileElem.files.length > 1) {
progressBar.max = fileElem.files.length;
2022-07-26 10:41:18 -07:00
const stripFolder = prefix(Object.values(fileElem.files).map(file => file.webkitRelativePath));
2022-07-26 10:16:23 -07:00
let i = 0;
for (let file of fileElem.files) {
2022-07-26 10:41:18 -07:00
const fileName = file.webkitRelativePath;
const reader = new FileReader();
2022-07-26 10:16:23 -07:00
reader.onload = function(e) {
2022-07-26 10:41:18 -07:00
processFile(stripFolder.length == 0 ? fileName : fileName.slice(fileName.indexOf(stripFolder) + stripFolder.length), new Uint8Array(e.target.result));
2022-07-26 10:16:23 -07:00
progressBar.value = i++;
if (i == progressBar.max) {
wrapItUp(fileElem);
}
};
reader.readAsArrayBuffer(file);
}
} else {
2022-08-12 17:21:14 -07:00
currentEPK = null;
2022-07-26 10:16:23 -07:00
fileElems.forEach(elem => elem.removeAttribute('disabled'));
2022-08-12 17:21:14 -07:00
oldBox.removeAttribute('disabled');
2022-07-26 10:16:23 -07:00
}
}
function processFile(name, file) {
2022-08-12 17:21:14 -07:00
if (oldBox.checked) {
currentEPK = currentEPK == null ? generateUTF('<file>') : concatTypedArrays(currentEPK, generateUTF('<file>'));
currentEPK = concatTypedArrays(currentEPK, generateUTF(name));
currentEPK = concatTypedArrays(currentEPK, new Uint8Array(sha1.arrayBuffer(file.buffer)));
currentEPK = concatTypedArrays(currentEPK, generateIntArray(file.byteLength));
currentEPK = concatTypedArrays(currentEPK, file);
currentEPK = concatTypedArrays(currentEPK, generateUTF('</file>'));
} else {
currentEPK = concatTypedArrays(currentEPK, textEncoder.encode('FILE'));
currentEPK = concatTypedArrays(currentEPK, generateUTFByte(name));
currentEPK = concatTypedArrays(currentEPK, generateIntArray(file.byteLength + 5));
currentEPK = concatTypedArrays(currentEPK, generateIntArray(crc32(file)));
currentEPK = concatTypedArrays(currentEPK, file);
currentEPK = concatTypedArrays(currentEPK, textEncoder.encode(':>'));
}
2022-07-26 10:16:23 -07:00
}
function wrapItUp(fileElem) {
2022-08-12 17:21:14 -07:00
if (oldBox.checked) {
currentEPK = concatTypedArrays(currentEPK, generateUTF(' end'));
currentEPK = concatTypedArrays(baseEPK, new Uint8Array(pako.deflate(currentEPK, { level: 9 })));
} else {
let currBaseEPK = baseEPKNew;
currBaseEPK = concatTypedArrays(currBaseEPK, generateUTF(commentNew.replace('$$YEAR$$', new Date().getFullYear())));
currBaseEPK = concatTypedArrays(currBaseEPK, generateLongArray(Date.now()));
currBaseEPK = concatTypedArrays(currBaseEPK, generateIntArray(progressBar.max + 1));
currBaseEPK = concatTypedArrays(currBaseEPK, Uint8Array.of(90));
currentEPK = concatTypedArrays(currentEPK, textEncoder.encode('END$'));
currentEPK = concatTypedArrays(currBaseEPK, new Uint8Array(pako.deflate(currentEPK, { level: 9 })));
currentEPK = concatTypedArrays(currentEPK, textEncoder.encode(':::YEE:>'));
}
2022-07-26 10:16:23 -07:00
const blob = new Blob([ currentEPK ], { type: 'application/octet-stream' });
downloadLink.href = window.URL.createObjectURL(blob);
currentEPK = null;
fileElem.value = '';
progressBar.value = 0;
progressBar.max = 1;
fileElems.forEach(elem => elem.removeAttribute('disabled'));
2022-08-12 17:21:14 -07:00
oldBox.removeAttribute('disabled');
2022-07-26 10:16:23 -07:00
}
</script>
</body>
</html>