Skip to content

Commit 75b4f79

Browse files
committed
use b64 to embed wad and compile with optimizations
1 parent 2f300ae commit 75b4f79

File tree

8 files changed

+42
-7
lines changed

8 files changed

+42
-7
lines changed

.github/workflows/publish.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
python3 -m venv .venv
1717
source .venv/bin/activate
1818
pip3 install -r requirements.txt
19-
CFLAGS=-O1 ./build.sh
19+
CFLAGS=-O3 ./build.sh
2020
cp index.html out
2121
rm out/compiled.js
2222

build.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ fi
2121
emmake make -C doomgeneric -f Makefile.pdfjs -j$(nproc --all)
2222

2323
mkdir -p out
24-
cat pre.js doomgeneric/doomgeneric.js > out/compiled.js
25-
#emcc test.c -s WASM=0 -o out/compiled.js -Wno-fastcomp
24+
python3 embed_file.py file_template.js doomgeneric/doom1.wad out/data.js
25+
cat pre.js out/data.js doomgeneric/doomgeneric.js > out/compiled.js
2626
python3 generate.py out/compiled.js out/doom.pdf

doomgeneric/Makefile.pdfjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ else
66
endif
77

88
CC=emcc
9-
CFLAGS+=-sWASM=0 -Wno-fastcomp -static '-sEXPORTED_FUNCTIONS=[_main,_doomjs_tick,_key_to_doomkey]'
10-
LDFLAGS+=--embed-file doom1.wad
9+
CFLAGS+=-sWASM=0 -Wno-fastcomp -static '-sEXPORTED_FUNCTIONS=[_main,_doomjs_tick,_key_to_doomkey]' -sSINGLE_FILE=1
10+
LDFLAGS+=
1111
LIBS+=-lm -lc
1212

1313
# subdirectory for objects

doomgeneric/doomgeneric_pdfjs.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <emscripten.h>
33
#include <stdio.h>
44
#include <stdlib.h>
5+
#include <unistd.h>
56
#include "doomgeneric.h"
67
#include "doomkeys.h"
78

@@ -129,6 +130,12 @@ void doomjs_tick() {
129130
}
130131

131132
int main(int argc, char **argv) {
133+
EM_ASM({
134+
let stream = FS.open("/doom1.wad", "w+");
135+
FS.write(stream, file_data, 0, file_data.length, 0);
136+
FS.close(stream);
137+
});
138+
132139
doomgeneric_Create(argc, argv);
133140

134141
EM_ASM({

embed_file.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import sys
2+
import base64
3+
4+
with open(sys.argv[1]) as f:
5+
js = f.read()
6+
7+
with open(sys.argv[2], "rb") as f:
8+
b64 = base64.b64encode(f.read()).decode()
9+
js = js.replace("__b64_data__", b64, 1)
10+
11+
with open(sys.argv[3], "w") as f:
12+
f.write(js)

file_template.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//https://stackoverflow.com/a/62364519
2+
function b64_to_uint8array(str) {
3+
const abc = [..."ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"]; // base64 alphabet
4+
let result = [];
5+
6+
for(let i=0; i<str.length/4; i++) {
7+
let chunk = [...str.slice(4*i,4*i+4)]
8+
let bin = chunk.map(x=> abc.indexOf(x).toString(2).padStart(6,0)).join('');
9+
let bytes = bin.match(/.{1,8}/g).map(x=> +('0b'+x));
10+
result.push(...bytes.slice(0,3 - (str[4*i+2]=="=") - (str[4*i+3]=="=")));
11+
}
12+
return new Uint8Array(result);
13+
}
14+
15+
var file_data = b64_to_uint8array("__b64_data__");

generate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def create_key_buttons(keys_info):
8989
writer = PdfWriter()
9090
page = create_page(width * scale, height * scale + 220)
9191
page.AA = PdfDict()
92-
page.AA.O = create_script("try {"+js+"} catch (e) {app.alert(e.stack)}");
92+
page.AA.O = create_script("try {"+js+"} catch (e) {app.alert(e.stack || e)}");
9393

9494
fields = []
9595
for i in range(0, height):

pre.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,5 @@ function key_up(key_str) {
6262
function reset_input_box() {
6363
globalThis.getField("key_input").value = "Type here for keyboard controls.";
6464
}
65-
app.setInterval("reset_input_box()", 1000);
65+
app.setInterval("reset_input_box()", 1000);
66+

0 commit comments

Comments
 (0)