Skip to content

Commit 2ba6f85

Browse files
authored
Improve Binary Serialization in the Code Generator (#58)
* Improve Binary Serialization in the Code Generator I'm a big fan of the Code Generator tool, but I noticed the all-in-one pages serialize the game as a JSON array, which can get kind of chunky. This PR swaps the JSON for a base64 string, bringing a `53.35mb` .html down to `21.85mb`. This should help with storage and load-times. Zipping the .html afterwards brings the whole thing down to within 10% of the original game size. bytes <-> base64 conversions adopted from examples on this page: https://developer.mozilla.org/en-US/docs/Web/API/Window/btoa * Fix silly gaps
1 parent a3f1277 commit 2ba6f85

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

public/js/main.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,11 @@ function editorMain() {
156156
fileData += (spaces + k + ' = "' + data[k] + '";\n');
157157
}
158158
}
159-
fileData += ' </scr' + 'ipt>\n <script src="' + data['EJS_pathtodata'] + 'loader.js"></scr' + 'ipt>\n </body>\n</html>';
159+
fileData += ' </script>\n <script src="' + data['EJS_pathtodata'] + 'loader.js"></script>\n </body>\n</html>';
160160
} else if (document.getElementById('offlinePack').checked) {
161-
data['EJS_gameUrl'] = 'new Blob([Uint8Array.from(window.gameData)])';
162-
var b = JSON.stringify(Array.from(new Uint8Array(await (new Blob([file])).arrayBuffer())));
163-
var a = spaces + 'window.gameData = ' + b + ';\n';
161+
data['EJS_gameUrl'] = 'new Blob([Uint8Array.from(atob(window.gameData), (m) => m.codePointAt(0))])';
162+
var b = bytesToBase64(new Uint8Array(await (new Blob([file])).arrayBuffer()));
163+
var a = spaces + 'window.gameData = `' + b + '`;\n';
164164
fileData += a;
165165
for (var k in data) {
166166
if (data[k] === true || data[k] === false || k === 'EJS_gameUrl') {
@@ -169,20 +169,20 @@ function editorMain() {
169169
fileData += (spaces + k + ' = "' + data[k] + '";\n');
170170
}
171171
}
172-
fileData += ' </scr' + 'ipt>\n <script src="' + data['EJS_pathtodata'] + 'loader.js"></scr' + 'ipt>\n </body>\n</html>';
172+
fileData += ' </script>\n <script src="' + data['EJS_pathtodata'] + 'loader.js"></script>\n </body>\n</html>';
173173
zipOut = false;
174174
} else {
175-
data['EJS_gameUrl'] = 'new Blob([Uint8Array.from(window.gameData)])';
176-
var b = JSON.stringify(Array.from(new Uint8Array(await (new Blob([file])).arrayBuffer())));
177-
zip.file('gameData.js', 'window.gameData = ' + b + '\n');
175+
data['EJS_gameUrl'] = 'new Blob([Uint8Array.from(atob(window.gameData), (m) => m.codePointAt(0))])';
176+
var b = bytesToBase64(new Uint8Array(await (new Blob([file])).arrayBuffer()));
177+
zip.file('gameData.js', 'window.gameData = `' + b + '`\n');
178178
for (var k in data) {
179179
if (data[k] === true || data[k] === false || k === 'EJS_gameUrl') {
180180
fileData += (spaces + k + ' = ' + data[k] + ';\n');
181181
} else {
182182
fileData += (spaces + k + ' = \'' + data[k] + '\';\n');
183183
}
184184
}
185-
fileData += ' </scr' + 'ipt>\n <script src=\'' + data['EJS_pathtodata'] + 'loader.js\'></scr' + 'ipt>\n </body>\n</html>';
185+
fileData += ' </script>\n <script src=\'' + data['EJS_pathtodata'] + 'loader.js\'></script>\n </body>\n</html>';
186186
}
187187
if (zipOut === false) {
188188
document.getElementById('select2').style = 'display:none;';
@@ -265,3 +265,10 @@ function copy(textareaId) {
265265
textarea.select();
266266
document.execCommand('copy');
267267
}
268+
269+
function bytesToBase64(bytes) {
270+
const binString = Array.from(bytes, (byte) =>
271+
String.fromCodePoint(byte),
272+
).join("");
273+
return btoa(binString);
274+
}

0 commit comments

Comments
 (0)