Skip to content

Instantly share code, notes, and snippets.

View 0xSamWitch's full-sized avatar

Sam Witch 0xSamWitch

View GitHub Profile
@0xSamWitch
0xSamWitch / Platformer_2D_single.html
Created February 13, 2022 13:43
Embedding all files inside a single html file from the Platformer 2D example from Godot
This file has been truncated, but you can view the full file.
<!DOCTYPE html>
<html xmlns='http://www.w3.org/1999/xhtml' lang='' xml:lang=''>
<head>
<meta charset='utf-8' />
<meta name='viewport' content='width=device-width, user-scalable=no' />
<title>Platformer 2D</title>
<style type='text/css'>
body {
touch-action: none;
this.preloadedFiles = [];
this.preload = function (pathOrBuffer, destPath, fileSize) {
let buffer = null;
return Promise.reject(new Error('Invalid object for preloading'));
};
Engine.load = function (basePath, size) {
if (loadPromise == null) {
loadPath = basePath;
loadPromise = Promise.resolve({
clone: () => ({
body: wasmBuffer
}),
});
}
};
Engine.load = function (basePath, size) {
...
return loadPromise;
};
const wasmCode = "<copy contents of base64.wasm>";
const wasmBuffer = decode(wasmCode);
const pckCode = "<copy contents of base64.pck>";
const pckBuffer = decode(pckCode);
const fs = require("fs");
// Replace as necessary
const NAME = "Platformer 2D";
let code = fs.readFileSync(`./${NAME}.wasm`);
fs.writeFileSync("base64.wasm", code.toString("base64"));
code = fs.readFileSync(`./${NAME}.pck`);
fs.writeFileSync("base64.pck", code.toString("base64"));
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
// Use a lookup table to find the index.
const lookup = new Uint8Array(256);
for (let i = 0; i < chars.length; i++) {
lookup[chars.charCodeAt(i)] = i;
}
function decode(base64) {
var bufferLength = base64.length * 0.75,
len = base64.length,
i,
const pck = "Platformer 2D.pck";
this.preloadedFiles = [ { path: pck, buffer:pckBuffer, } ];
this.preload = function (pathOrBuffer, destPath, fileSize) {
let buffer = null;
if (typeof pathOrBuffer === 'string') {
if (pathOrBuffer === pck) {
return Promise.resolve();
}
const me = this;
return this.loadPromise(pathOrBuffer, fileSize).then(function (buf) {
this.preloadedFiles = [ { path: pck, buffer:pckBuffer, } ];
if (pathOrBuffer === pck) {
return Promise.resolve();
}