@muhammedaksam/opentui-doom 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,36 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Muhammed Mustafa AKŞAM
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
23
+ ---
24
+
25
+ ## Third-Party Licenses
26
+
27
+ ### DOOM Source Code
28
+ The original DOOM source code is released under the GPL-2.0 license by id Software.
29
+ https://github.com/id-Software/DOOM
30
+
31
+ ### doomgeneric
32
+ The portable DOOM library is released under the GPL-2.0 license.
33
+ https://github.com/ozkl/doomgeneric
34
+
35
+ Note: The WASM build of DOOM includes GPL-2.0 licensed code. When distributing
36
+ the compiled WASM module, you must comply with the GPL-2.0 license terms.
package/README.md ADDED
@@ -0,0 +1,137 @@
1
+ # DOOM for OpenTUI
2
+
3
+ 🎮 Play DOOM in your terminal using [OpenTUI](https://github.com/sst/opentui)'s framebuffer rendering!
4
+
5
+ ## ✨ Features
6
+
7
+ - **Full DOOM gameplay** in your terminal
8
+ - **High-resolution rendering** using half-block characters (▀) for 2x vertical resolution
9
+ - **Keyboard input support** with WASD and arrow keys
10
+ - **WebAssembly powered** - DOOM compiled to WASM via Emscripten
11
+
12
+ ## 📋 Requirements
13
+
14
+ - **Bun** - JavaScript runtime
15
+ - **Emscripten SDK** - For compiling DOOM to WebAssembly
16
+ - **DOOM WAD file** - Game data (shareware `doom1.wad` is freely available)
17
+
18
+ ## 🚀 Quick Start
19
+
20
+ ### 1. Clone the Repository
21
+
22
+ ```bash
23
+ git clone https://github.com/muhammedaksam/opentui-doom.git
24
+ cd opentui-doom
25
+ bun install
26
+ ```
27
+
28
+ ### 2. Install Emscripten (if not already installed)
29
+
30
+ ```bash
31
+ git clone https://github.com/emscripten-core/emsdk.git ~/emsdk
32
+ cd ~/emsdk
33
+ ./emsdk install latest
34
+ ./emsdk activate latest
35
+ source ./emsdk_env.sh
36
+ ```
37
+
38
+ ### 3. Build DOOM WASM Module
39
+
40
+ ```bash
41
+ bun run build:doom
42
+ ```
43
+
44
+ This clones [doomgeneric](https://github.com/ozkl/doomgeneric) and compiles it to WebAssembly.
45
+
46
+ ### 4. Get a WAD File
47
+
48
+ Download the shareware DOOM WAD:
49
+ - [doom1.wad from ibiblio](https://distro.ibiblio.org/slitaz/sources/packages/d/doom-wad/)
50
+ - Or use your own `DOOM.WAD` / `DOOM2.WAD`
51
+
52
+ Place the WAD file in the project root.
53
+
54
+ ### 5. Run DOOM
55
+
56
+ ```bash
57
+ bun run dev -- --wad ./doom1.wad
58
+ ```
59
+
60
+ ## 🎮 Controls
61
+
62
+ | Action | Keys |
63
+ |--------|------|
64
+ | Move Forward/Back | W / S or ↑ / ↓ |
65
+ | Turn Left/Right | ← / → |
66
+ | Strafe | A / D |
67
+ | Fire | Ctrl |
68
+ | Use/Open | Space |
69
+ | Run | Shift |
70
+ | Weapons | 1-7 |
71
+ | Menu | Escape |
72
+ | Map | Tab |
73
+ | Quit | Ctrl+C |
74
+
75
+ ## 🖥️ Recommended Terminal Configuration
76
+
77
+ For the best experience, we recommend:
78
+ - **Alacritty** terminal emulator
79
+ - **Font size: 5** (for maximum resolution)
80
+ - Maximize your terminal window
81
+
82
+ ## ⚠️ Known Limitations
83
+
84
+ - **Multi-key input**: Terminals only send key repeat events for one key at a time. Holding W to move forward will stop when you press arrow keys to turn. This is a terminal limitation, not a bug.
85
+ - **No Kitty keyboard protocol**: While OpenTUI supports the Kitty keyboard protocol for proper key release events, it didn't work as expected in my testing. Currently using timeout-based key release as a workaround.
86
+
87
+ ## 🔧 How It Works
88
+
89
+ ```
90
+ ┌─────────────────┐ ┌──────────────────┐ ┌────────────────┐
91
+ │ doomgeneric │───▶│ OpenTUI │───▶│ Terminal │
92
+ │ (WASM) │ │ FrameBuffer │ │ Display │
93
+ └─────────────────┘ └──────────────────┘ └────────────────┘
94
+ ▲ │
95
+ │ │
96
+ └──────────────────────┘
97
+ Key Events
98
+ ```
99
+
100
+ 1. **DOOM** runs as a WebAssembly module (compiled from C via Emscripten)
101
+ 2. Each frame, DOOM renders to a 1280x800 framebuffer
102
+ 3. **OpenTUI** reads the framebuffer and converts it to terminal cells using half-block characters
103
+ 4. Terminal keyboard input is mapped back to DOOM key codes
104
+
105
+ ## 📁 Project Structure
106
+
107
+ ```
108
+ opentui-doom/
109
+ ├── src/
110
+ │ ├── index.ts # Main entry point
111
+ │ ├── doom-engine.ts # WASM module wrapper
112
+ │ └── doom-input.ts # Keyboard input mapping
113
+ ├── doom/
114
+ │ ├── doomgeneric_opentui.c # Platform implementation
115
+ │ ├── doomgeneric/ # doomgeneric source (cloned during build)
116
+ │ └── build/ # Compiled WASM output
117
+ ├── scripts/
118
+ │ └── build-doom.sh # Build script
119
+ ├── package.json
120
+ └── README.md
121
+ ```
122
+
123
+ ## 📝 License
124
+
125
+ - This project code: MIT
126
+ - DOOM source code: GPL-2.0 (from id Software)
127
+ - doomgeneric: GPL-2.0 (by ozkl)
128
+
129
+ ## 🙏 Credits
130
+
131
+ - [id Software](https://github.com/id-Software/DOOM) for the original DOOM source release
132
+ - [doomgeneric](https://github.com/ozkl/doomgeneric) for the portable DOOM implementation
133
+ - [OpenTUI](https://github.com/sst/opentui) for the terminal rendering framework
134
+
135
+ ## 🤝 Contributing
136
+
137
+ Contributions are welcome! Feel free to open issues or submit pull requests.
@@ -0,0 +1,2 @@
1
+ async function createDoomModule(moduleArg={}){var moduleRtn;var Module=moduleArg;var ENVIRONMENT_IS_WORKER=false;var ENVIRONMENT_IS_NODE=true;var arguments_=[];var thisProgram="./this.program";var quit_=(status,toThrow)=>{throw toThrow};var _scriptName;if(typeof __filename!="undefined"){_scriptName=__filename}else{}var scriptDirectory="";function locateFile(path){if(Module["locateFile"]){return Module["locateFile"](path,scriptDirectory)}return scriptDirectory+path}var readAsync,readBinary;if(ENVIRONMENT_IS_NODE){var fs=require("fs");scriptDirectory=__dirname+"/";readBinary=filename=>{filename=isFileURI(filename)?new URL(filename):filename;var ret=fs.readFileSync(filename);return ret};readAsync=async(filename,binary=true)=>{filename=isFileURI(filename)?new URL(filename):filename;var ret=fs.readFileSync(filename,binary?undefined:"utf8");return ret};if(process.argv.length>1){thisProgram=process.argv[1].replace(/\\/g,"/")}arguments_=process.argv.slice(2);quit_=(status,toThrow)=>{process.exitCode=status;throw toThrow}}else{}var out=console.log.bind(console);var err=console.error.bind(console);var wasmBinary;var ABORT=false;var EXITSTATUS;var isFileURI=filename=>filename.startsWith("file://");var readyPromiseResolve,readyPromiseReject;var HEAP8,HEAPU8,HEAP16,HEAPU16,HEAP32,HEAPU32,HEAPF32,HEAPF64;var HEAP64,HEAPU64;var runtimeInitialized=false;function updateMemoryViews(){var b=wasmMemory.buffer;HEAP8=new Int8Array(b);HEAP16=new Int16Array(b);HEAPU8=new Uint8Array(b);HEAPU16=new Uint16Array(b);HEAP32=new Int32Array(b);HEAPU32=new Uint32Array(b);HEAPF32=new Float32Array(b);HEAPF64=new Float64Array(b);HEAP64=new BigInt64Array(b);HEAPU64=new BigUint64Array(b)}function preRun(){if(Module["preRun"]){if(typeof Module["preRun"]=="function")Module["preRun"]=[Module["preRun"]];while(Module["preRun"].length){addOnPreRun(Module["preRun"].shift())}}callRuntimeCallbacks(onPreRuns)}function initRuntime(){runtimeInitialized=true;if(!Module["noFSInit"]&&!FS.initialized)FS.init();TTY.init();wasmExports["__wasm_call_ctors"]();FS.ignorePermissions=false}function postRun(){if(Module["postRun"]){if(typeof Module["postRun"]=="function")Module["postRun"]=[Module["postRun"]];while(Module["postRun"].length){addOnPostRun(Module["postRun"].shift())}}callRuntimeCallbacks(onPostRuns)}function abort(what){Module["onAbort"]?.(what);what="Aborted("+what+")";err(what);ABORT=true;what+=". Build with -sASSERTIONS for more info.";var e=new WebAssembly.RuntimeError(what);readyPromiseReject?.(e);throw e}var wasmBinaryFile;function findWasmBinary(){return locateFile("doom.wasm")}function getBinarySync(file){if(file==wasmBinaryFile&&wasmBinary){return new Uint8Array(wasmBinary)}if(readBinary){return readBinary(file)}throw"both async and sync fetching of the wasm failed"}async function getWasmBinary(binaryFile){if(!wasmBinary){try{var response=await readAsync(binaryFile);return new Uint8Array(response)}catch{}}return getBinarySync(binaryFile)}async function instantiateArrayBuffer(binaryFile,imports){try{var binary=await getWasmBinary(binaryFile);var instance=await WebAssembly.instantiate(binary,imports);return instance}catch(reason){err(`failed to asynchronously prepare wasm: ${reason}`);abort(reason)}}async function instantiateAsync(binary,binaryFile,imports){if(!binary&&!ENVIRONMENT_IS_NODE){try{var response=fetch(binaryFile,{credentials:"same-origin"});var instantiationResult=await WebAssembly.instantiateStreaming(response,imports);return instantiationResult}catch(reason){err(`wasm streaming compile failed: ${reason}`);err("falling back to ArrayBuffer instantiation")}}return instantiateArrayBuffer(binaryFile,imports)}function getWasmImports(){var imports={env:wasmImports,wasi_snapshot_preview1:wasmImports};return imports}async function createWasm(){function receiveInstance(instance,module){wasmExports=instance.exports;assignWasmExports(wasmExports);updateMemoryViews();return wasmExports}function receiveInstantiationResult(result){return receiveInstance(result["instance"])}var info=getWasmImports();if(Module["instantiateWasm"]){return new Promise((resolve,reject)=>{Module["instantiateWasm"](info,(inst,mod)=>{resolve(receiveInstance(inst,mod))})})}wasmBinaryFile??=findWasmBinary();var result=await instantiateAsync(wasmBinary,wasmBinaryFile,info);var exports=receiveInstantiationResult(result);return exports}class ExitStatus{name="ExitStatus";constructor(status){this.message=`Program terminated with exit(${status})`;this.status=status}}var callRuntimeCallbacks=callbacks=>{while(callbacks.length>0){callbacks.shift()(Module)}};var onPostRuns=[];var addOnPostRun=cb=>onPostRuns.push(cb);var onPreRuns=[];var addOnPreRun=cb=>onPreRuns.push(cb);function getValue(ptr,type="i8"){if(type.endsWith("*"))type="*";switch(type){case"i1":return HEAP8[ptr];case"i8":return HEAP8[ptr];case"i16":return HEAP16[ptr>>1];case"i32":return HEAP32[ptr>>2];case"i64":return HEAP64[ptr>>3];case"float":return HEAPF32[ptr>>2];case"double":return HEAPF64[ptr>>3];case"*":return HEAPU32[ptr>>2];default:abort(`invalid type for getValue: ${type}`)}}var noExitRuntime=true;function setValue(ptr,value,type="i8"){if(type.endsWith("*"))type="*";switch(type){case"i1":HEAP8[ptr]=value;break;case"i8":HEAP8[ptr]=value;break;case"i16":HEAP16[ptr>>1]=value;break;case"i32":HEAP32[ptr>>2]=value;break;case"i64":HEAP64[ptr>>3]=BigInt(value);break;case"float":HEAPF32[ptr>>2]=value;break;case"double":HEAPF64[ptr>>3]=value;break;case"*":HEAPU32[ptr>>2]=value;break;default:abort(`invalid type for setValue: ${type}`)}}var stackRestore=val=>__emscripten_stack_restore(val);var stackSave=()=>_emscripten_stack_get_current();var syscallGetVarargI=()=>{var ret=HEAP32[+SYSCALLS.varargs>>2];SYSCALLS.varargs+=4;return ret};var syscallGetVarargP=syscallGetVarargI;var PATH={isAbs:path=>path.charAt(0)==="/",splitPath:filename=>{var splitPathRe=/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/;return splitPathRe.exec(filename).slice(1)},normalizeArray:(parts,allowAboveRoot)=>{var up=0;for(var i=parts.length-1;i>=0;i--){var last=parts[i];if(last==="."){parts.splice(i,1)}else if(last===".."){parts.splice(i,1);up++}else if(up){parts.splice(i,1);up--}}if(allowAboveRoot){for(;up;up--){parts.unshift("..")}}return parts},normalize:path=>{var isAbsolute=PATH.isAbs(path),trailingSlash=path.slice(-1)==="/";path=PATH.normalizeArray(path.split("/").filter(p=>!!p),!isAbsolute).join("/");if(!path&&!isAbsolute){path="."}if(path&&trailingSlash){path+="/"}return(isAbsolute?"/":"")+path},dirname:path=>{var result=PATH.splitPath(path),root=result[0],dir=result[1];if(!root&&!dir){return"."}if(dir){dir=dir.slice(0,-1)}return root+dir},basename:path=>path&&path.match(/([^\/]+|\/)\/*$/)[1],join:(...paths)=>PATH.normalize(paths.join("/")),join2:(l,r)=>PATH.normalize(l+"/"+r)};var initRandomFill=()=>{if(ENVIRONMENT_IS_NODE){var nodeCrypto=require("crypto");return view=>nodeCrypto.randomFillSync(view)}return view=>crypto.getRandomValues(view)};var randomFill=view=>{(randomFill=initRandomFill())(view)};var PATH_FS={resolve:(...args)=>{var resolvedPath="",resolvedAbsolute=false;for(var i=args.length-1;i>=-1&&!resolvedAbsolute;i--){var path=i>=0?args[i]:FS.cwd();if(typeof path!="string"){throw new TypeError("Arguments to path.resolve must be strings")}else if(!path){return""}resolvedPath=path+"/"+resolvedPath;resolvedAbsolute=PATH.isAbs(path)}resolvedPath=PATH.normalizeArray(resolvedPath.split("/").filter(p=>!!p),!resolvedAbsolute).join("/");return(resolvedAbsolute?"/":"")+resolvedPath||"."},relative:(from,to)=>{from=PATH_FS.resolve(from).slice(1);to=PATH_FS.resolve(to).slice(1);function trim(arr){var start=0;for(;start<arr.length;start++){if(arr[start]!=="")break}var end=arr.length-1;for(;end>=0;end--){if(arr[end]!=="")break}if(start>end)return[];return arr.slice(start,end-start+1)}var fromParts=trim(from.split("/"));var toParts=trim(to.split("/"));var length=Math.min(fromParts.length,toParts.length);var samePartsLength=length;for(var i=0;i<length;i++){if(fromParts[i]!==toParts[i]){samePartsLength=i;break}}var outputParts=[];for(var i=samePartsLength;i<fromParts.length;i++){outputParts.push("..")}outputParts=outputParts.concat(toParts.slice(samePartsLength));return outputParts.join("/")}};var UTF8Decoder=globalThis.TextDecoder&&new TextDecoder;var findStringEnd=(heapOrArray,idx,maxBytesToRead,ignoreNul)=>{var maxIdx=idx+maxBytesToRead;if(ignoreNul)return maxIdx;while(heapOrArray[idx]&&!(idx>=maxIdx))++idx;return idx};var UTF8ArrayToString=(heapOrArray,idx=0,maxBytesToRead,ignoreNul)=>{var endPtr=findStringEnd(heapOrArray,idx,maxBytesToRead,ignoreNul);if(endPtr-idx>16&&heapOrArray.buffer&&UTF8Decoder){return UTF8Decoder.decode(heapOrArray.subarray(idx,endPtr))}var str="";while(idx<endPtr){var u0=heapOrArray[idx++];if(!(u0&128)){str+=String.fromCharCode(u0);continue}var u1=heapOrArray[idx++]&63;if((u0&224)==192){str+=String.fromCharCode((u0&31)<<6|u1);continue}var u2=heapOrArray[idx++]&63;if((u0&240)==224){u0=(u0&15)<<12|u1<<6|u2}else{u0=(u0&7)<<18|u1<<12|u2<<6|heapOrArray[idx++]&63}if(u0<65536){str+=String.fromCharCode(u0)}else{var ch=u0-65536;str+=String.fromCharCode(55296|ch>>10,56320|ch&1023)}}return str};var FS_stdin_getChar_buffer=[];var lengthBytesUTF8=str=>{var len=0;for(var i=0;i<str.length;++i){var c=str.charCodeAt(i);if(c<=127){len++}else if(c<=2047){len+=2}else if(c>=55296&&c<=57343){len+=4;++i}else{len+=3}}return len};var stringToUTF8Array=(str,heap,outIdx,maxBytesToWrite)=>{if(!(maxBytesToWrite>0))return 0;var startIdx=outIdx;var endIdx=outIdx+maxBytesToWrite-1;for(var i=0;i<str.length;++i){var u=str.codePointAt(i);if(u<=127){if(outIdx>=endIdx)break;heap[outIdx++]=u}else if(u<=2047){if(outIdx+1>=endIdx)break;heap[outIdx++]=192|u>>6;heap[outIdx++]=128|u&63}else if(u<=65535){if(outIdx+2>=endIdx)break;heap[outIdx++]=224|u>>12;heap[outIdx++]=128|u>>6&63;heap[outIdx++]=128|u&63}else{if(outIdx+3>=endIdx)break;heap[outIdx++]=240|u>>18;heap[outIdx++]=128|u>>12&63;heap[outIdx++]=128|u>>6&63;heap[outIdx++]=128|u&63;i++}}heap[outIdx]=0;return outIdx-startIdx};var intArrayFromString=(stringy,dontAddNull,length)=>{var len=length>0?length:lengthBytesUTF8(stringy)+1;var u8array=new Array(len);var numBytesWritten=stringToUTF8Array(stringy,u8array,0,u8array.length);if(dontAddNull)u8array.length=numBytesWritten;return u8array};var FS_stdin_getChar=()=>{if(!FS_stdin_getChar_buffer.length){var result=null;if(ENVIRONMENT_IS_NODE){var BUFSIZE=256;var buf=Buffer.alloc(BUFSIZE);var bytesRead=0;var fd=process.stdin.fd;try{bytesRead=fs.readSync(fd,buf,0,BUFSIZE)}catch(e){if(e.toString().includes("EOF"))bytesRead=0;else throw e}if(bytesRead>0){result=buf.slice(0,bytesRead).toString("utf-8")}}else{}if(!result){return null}FS_stdin_getChar_buffer=intArrayFromString(result,true)}return FS_stdin_getChar_buffer.shift()};var TTY={ttys:[],init(){},shutdown(){},register(dev,ops){TTY.ttys[dev]={input:[],output:[],ops};FS.registerDevice(dev,TTY.stream_ops)},stream_ops:{open(stream){var tty=TTY.ttys[stream.node.rdev];if(!tty){throw new FS.ErrnoError(43)}stream.tty=tty;stream.seekable=false},close(stream){stream.tty.ops.fsync(stream.tty)},fsync(stream){stream.tty.ops.fsync(stream.tty)},read(stream,buffer,offset,length,pos){if(!stream.tty||!stream.tty.ops.get_char){throw new FS.ErrnoError(60)}var bytesRead=0;for(var i=0;i<length;i++){var result;try{result=stream.tty.ops.get_char(stream.tty)}catch(e){throw new FS.ErrnoError(29)}if(result===undefined&&bytesRead===0){throw new FS.ErrnoError(6)}if(result===null||result===undefined)break;bytesRead++;buffer[offset+i]=result}if(bytesRead){stream.node.atime=Date.now()}return bytesRead},write(stream,buffer,offset,length,pos){if(!stream.tty||!stream.tty.ops.put_char){throw new FS.ErrnoError(60)}try{for(var i=0;i<length;i++){stream.tty.ops.put_char(stream.tty,buffer[offset+i])}}catch(e){throw new FS.ErrnoError(29)}if(length){stream.node.mtime=stream.node.ctime=Date.now()}return i}},default_tty_ops:{get_char(tty){return FS_stdin_getChar()},put_char(tty,val){if(val===null||val===10){out(UTF8ArrayToString(tty.output));tty.output=[]}else{if(val!=0)tty.output.push(val)}},fsync(tty){if(tty.output?.length>0){out(UTF8ArrayToString(tty.output));tty.output=[]}},ioctl_tcgets(tty){return{c_iflag:25856,c_oflag:5,c_cflag:191,c_lflag:35387,c_cc:[3,28,127,21,4,0,1,0,17,19,26,0,18,15,23,22,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}},ioctl_tcsets(tty,optional_actions,data){return 0},ioctl_tiocgwinsz(tty){return[24,80]}},default_tty1_ops:{put_char(tty,val){if(val===null||val===10){err(UTF8ArrayToString(tty.output));tty.output=[]}else{if(val!=0)tty.output.push(val)}},fsync(tty){if(tty.output?.length>0){err(UTF8ArrayToString(tty.output));tty.output=[]}}}};var mmapAlloc=size=>{abort()};var MEMFS={ops_table:null,mount(mount){return MEMFS.createNode(null,"/",16895,0)},createNode(parent,name,mode,dev){if(FS.isBlkdev(mode)||FS.isFIFO(mode)){throw new FS.ErrnoError(63)}MEMFS.ops_table||={dir:{node:{getattr:MEMFS.node_ops.getattr,setattr:MEMFS.node_ops.setattr,lookup:MEMFS.node_ops.lookup,mknod:MEMFS.node_ops.mknod,rename:MEMFS.node_ops.rename,unlink:MEMFS.node_ops.unlink,rmdir:MEMFS.node_ops.rmdir,readdir:MEMFS.node_ops.readdir,symlink:MEMFS.node_ops.symlink},stream:{llseek:MEMFS.stream_ops.llseek}},file:{node:{getattr:MEMFS.node_ops.getattr,setattr:MEMFS.node_ops.setattr},stream:{llseek:MEMFS.stream_ops.llseek,read:MEMFS.stream_ops.read,write:MEMFS.stream_ops.write,mmap:MEMFS.stream_ops.mmap,msync:MEMFS.stream_ops.msync}},link:{node:{getattr:MEMFS.node_ops.getattr,setattr:MEMFS.node_ops.setattr,readlink:MEMFS.node_ops.readlink},stream:{}},chrdev:{node:{getattr:MEMFS.node_ops.getattr,setattr:MEMFS.node_ops.setattr},stream:FS.chrdev_stream_ops}};var node=FS.createNode(parent,name,mode,dev);if(FS.isDir(node.mode)){node.node_ops=MEMFS.ops_table.dir.node;node.stream_ops=MEMFS.ops_table.dir.stream;node.contents={}}else if(FS.isFile(node.mode)){node.node_ops=MEMFS.ops_table.file.node;node.stream_ops=MEMFS.ops_table.file.stream;node.usedBytes=0;node.contents=null}else if(FS.isLink(node.mode)){node.node_ops=MEMFS.ops_table.link.node;node.stream_ops=MEMFS.ops_table.link.stream}else if(FS.isChrdev(node.mode)){node.node_ops=MEMFS.ops_table.chrdev.node;node.stream_ops=MEMFS.ops_table.chrdev.stream}node.atime=node.mtime=node.ctime=Date.now();if(parent){parent.contents[name]=node;parent.atime=parent.mtime=parent.ctime=node.atime}return node},getFileDataAsTypedArray(node){if(!node.contents)return new Uint8Array(0);if(node.contents.subarray)return node.contents.subarray(0,node.usedBytes);return new Uint8Array(node.contents)},expandFileStorage(node,newCapacity){var prevCapacity=node.contents?node.contents.length:0;if(prevCapacity>=newCapacity)return;var CAPACITY_DOUBLING_MAX=1024*1024;newCapacity=Math.max(newCapacity,prevCapacity*(prevCapacity<CAPACITY_DOUBLING_MAX?2:1.125)>>>0);if(prevCapacity!=0)newCapacity=Math.max(newCapacity,256);var oldContents=node.contents;node.contents=new Uint8Array(newCapacity);if(node.usedBytes>0)node.contents.set(oldContents.subarray(0,node.usedBytes),0)},resizeFileStorage(node,newSize){if(node.usedBytes==newSize)return;if(newSize==0){node.contents=null;node.usedBytes=0}else{var oldContents=node.contents;node.contents=new Uint8Array(newSize);if(oldContents){node.contents.set(oldContents.subarray(0,Math.min(newSize,node.usedBytes)))}node.usedBytes=newSize}},node_ops:{getattr(node){var attr={};attr.dev=FS.isChrdev(node.mode)?node.id:1;attr.ino=node.id;attr.mode=node.mode;attr.nlink=1;attr.uid=0;attr.gid=0;attr.rdev=node.rdev;if(FS.isDir(node.mode)){attr.size=4096}else if(FS.isFile(node.mode)){attr.size=node.usedBytes}else if(FS.isLink(node.mode)){attr.size=node.link.length}else{attr.size=0}attr.atime=new Date(node.atime);attr.mtime=new Date(node.mtime);attr.ctime=new Date(node.ctime);attr.blksize=4096;attr.blocks=Math.ceil(attr.size/attr.blksize);return attr},setattr(node,attr){for(const key of["mode","atime","mtime","ctime"]){if(attr[key]!=null){node[key]=attr[key]}}if(attr.size!==undefined){MEMFS.resizeFileStorage(node,attr.size)}},lookup(parent,name){if(!MEMFS.doesNotExistError){MEMFS.doesNotExistError=new FS.ErrnoError(44);MEMFS.doesNotExistError.stack="<generic error, no stack>"}throw MEMFS.doesNotExistError},mknod(parent,name,mode,dev){return MEMFS.createNode(parent,name,mode,dev)},rename(old_node,new_dir,new_name){var new_node;try{new_node=FS.lookupNode(new_dir,new_name)}catch(e){}if(new_node){if(FS.isDir(old_node.mode)){for(var i in new_node.contents){throw new FS.ErrnoError(55)}}FS.hashRemoveNode(new_node)}delete old_node.parent.contents[old_node.name];new_dir.contents[new_name]=old_node;old_node.name=new_name;new_dir.ctime=new_dir.mtime=old_node.parent.ctime=old_node.parent.mtime=Date.now()},unlink(parent,name){delete parent.contents[name];parent.ctime=parent.mtime=Date.now()},rmdir(parent,name){var node=FS.lookupNode(parent,name);for(var i in node.contents){throw new FS.ErrnoError(55)}delete parent.contents[name];parent.ctime=parent.mtime=Date.now()},readdir(node){return[".","..",...Object.keys(node.contents)]},symlink(parent,newname,oldpath){var node=MEMFS.createNode(parent,newname,511|40960,0);node.link=oldpath;return node},readlink(node){if(!FS.isLink(node.mode)){throw new FS.ErrnoError(28)}return node.link}},stream_ops:{read(stream,buffer,offset,length,position){var contents=stream.node.contents;if(position>=stream.node.usedBytes)return 0;var size=Math.min(stream.node.usedBytes-position,length);if(size>8&&contents.subarray){buffer.set(contents.subarray(position,position+size),offset)}else{for(var i=0;i<size;i++)buffer[offset+i]=contents[position+i]}return size},write(stream,buffer,offset,length,position,canOwn){if(buffer.buffer===HEAP8.buffer){canOwn=false}if(!length)return 0;var node=stream.node;node.mtime=node.ctime=Date.now();if(buffer.subarray&&(!node.contents||node.contents.subarray)){if(canOwn){node.contents=buffer.subarray(offset,offset+length);node.usedBytes=length;return length}else if(node.usedBytes===0&&position===0){node.contents=buffer.slice(offset,offset+length);node.usedBytes=length;return length}else if(position+length<=node.usedBytes){node.contents.set(buffer.subarray(offset,offset+length),position);return length}}MEMFS.expandFileStorage(node,position+length);if(node.contents.subarray&&buffer.subarray){node.contents.set(buffer.subarray(offset,offset+length),position)}else{for(var i=0;i<length;i++){node.contents[position+i]=buffer[offset+i]}}node.usedBytes=Math.max(node.usedBytes,position+length);return length},llseek(stream,offset,whence){var position=offset;if(whence===1){position+=stream.position}else if(whence===2){if(FS.isFile(stream.node.mode)){position+=stream.node.usedBytes}}if(position<0){throw new FS.ErrnoError(28)}return position},mmap(stream,length,position,prot,flags){if(!FS.isFile(stream.node.mode)){throw new FS.ErrnoError(43)}var ptr;var allocated;var contents=stream.node.contents;if(!(flags&2)&&contents&&contents.buffer===HEAP8.buffer){allocated=false;ptr=contents.byteOffset}else{allocated=true;ptr=mmapAlloc(length);if(!ptr){throw new FS.ErrnoError(48)}if(contents){if(position>0||position+length<contents.length){if(contents.subarray){contents=contents.subarray(position,position+length)}else{contents=Array.prototype.slice.call(contents,position,position+length)}}HEAP8.set(contents,ptr)}}return{ptr,allocated}},msync(stream,buffer,offset,length,mmapFlags){MEMFS.stream_ops.write(stream,buffer,0,length,offset,false);return 0}}};var FS_modeStringToFlags=str=>{var flagModes={r:0,"r+":2,w:512|64|1,"w+":512|64|2,a:1024|64|1,"a+":1024|64|2};var flags=flagModes[str];if(typeof flags=="undefined"){throw new Error(`Unknown file open mode: ${str}`)}return flags};var FS_getMode=(canRead,canWrite)=>{var mode=0;if(canRead)mode|=292|73;if(canWrite)mode|=146;return mode};var asyncLoad=async url=>{var arrayBuffer=await readAsync(url);return new Uint8Array(arrayBuffer)};var FS_createDataFile=(...args)=>FS.createDataFile(...args);var getUniqueRunDependency=id=>id;var runDependencies=0;var dependenciesFulfilled=null;var removeRunDependency=id=>{runDependencies--;Module["monitorRunDependencies"]?.(runDependencies);if(runDependencies==0){if(dependenciesFulfilled){var callback=dependenciesFulfilled;dependenciesFulfilled=null;callback()}}};var addRunDependency=id=>{runDependencies++;Module["monitorRunDependencies"]?.(runDependencies)};var preloadPlugins=[];var FS_handledByPreloadPlugin=async(byteArray,fullname)=>{if(typeof Browser!="undefined")Browser.init();for(var plugin of preloadPlugins){if(plugin["canHandle"](fullname)){return plugin["handle"](byteArray,fullname)}}return byteArray};var FS_preloadFile=async(parent,name,url,canRead,canWrite,dontCreateFile,canOwn,preFinish)=>{var fullname=name?PATH_FS.resolve(PATH.join2(parent,name)):parent;var dep=getUniqueRunDependency(`cp ${fullname}`);addRunDependency(dep);try{var byteArray=url;if(typeof url=="string"){byteArray=await asyncLoad(url)}byteArray=await FS_handledByPreloadPlugin(byteArray,fullname);preFinish?.();if(!dontCreateFile){FS_createDataFile(parent,name,byteArray,canRead,canWrite,canOwn)}}finally{removeRunDependency(dep)}};var FS_createPreloadedFile=(parent,name,url,canRead,canWrite,onload,onerror,dontCreateFile,canOwn,preFinish)=>{FS_preloadFile(parent,name,url,canRead,canWrite,dontCreateFile,canOwn,preFinish).then(onload).catch(onerror)};var FS={root:null,mounts:[],devices:{},streams:[],nextInode:1,nameTable:null,currentPath:"/",initialized:false,ignorePermissions:true,filesystems:null,syncFSRequests:0,readFiles:{},ErrnoError:class{name="ErrnoError";constructor(errno){this.errno=errno}},FSStream:class{shared={};get object(){return this.node}set object(val){this.node=val}get isRead(){return(this.flags&2097155)!==1}get isWrite(){return(this.flags&2097155)!==0}get isAppend(){return this.flags&1024}get flags(){return this.shared.flags}set flags(val){this.shared.flags=val}get position(){return this.shared.position}set position(val){this.shared.position=val}},FSNode:class{node_ops={};stream_ops={};readMode=292|73;writeMode=146;mounted=null;constructor(parent,name,mode,rdev){if(!parent){parent=this}this.parent=parent;this.mount=parent.mount;this.id=FS.nextInode++;this.name=name;this.mode=mode;this.rdev=rdev;this.atime=this.mtime=this.ctime=Date.now()}get read(){return(this.mode&this.readMode)===this.readMode}set read(val){val?this.mode|=this.readMode:this.mode&=~this.readMode}get write(){return(this.mode&this.writeMode)===this.writeMode}set write(val){val?this.mode|=this.writeMode:this.mode&=~this.writeMode}get isFolder(){return FS.isDir(this.mode)}get isDevice(){return FS.isChrdev(this.mode)}},lookupPath(path,opts={}){if(!path){throw new FS.ErrnoError(44)}opts.follow_mount??=true;if(!PATH.isAbs(path)){path=FS.cwd()+"/"+path}linkloop:for(var nlinks=0;nlinks<40;nlinks++){var parts=path.split("/").filter(p=>!!p);var current=FS.root;var current_path="/";for(var i=0;i<parts.length;i++){var islast=i===parts.length-1;if(islast&&opts.parent){break}if(parts[i]==="."){continue}if(parts[i]===".."){current_path=PATH.dirname(current_path);if(FS.isRoot(current)){path=current_path+"/"+parts.slice(i+1).join("/");nlinks--;continue linkloop}else{current=current.parent}continue}current_path=PATH.join2(current_path,parts[i]);try{current=FS.lookupNode(current,parts[i])}catch(e){if(e?.errno===44&&islast&&opts.noent_okay){return{path:current_path}}throw e}if(FS.isMountpoint(current)&&(!islast||opts.follow_mount)){current=current.mounted.root}if(FS.isLink(current.mode)&&(!islast||opts.follow)){if(!current.node_ops.readlink){throw new FS.ErrnoError(52)}var link=current.node_ops.readlink(current);if(!PATH.isAbs(link)){link=PATH.dirname(current_path)+"/"+link}path=link+"/"+parts.slice(i+1).join("/");continue linkloop}}return{path:current_path,node:current}}throw new FS.ErrnoError(32)},getPath(node){var path;while(true){if(FS.isRoot(node)){var mount=node.mount.mountpoint;if(!path)return mount;return mount[mount.length-1]!=="/"?`${mount}/${path}`:mount+path}path=path?`${node.name}/${path}`:node.name;node=node.parent}},hashName(parentid,name){var hash=0;for(var i=0;i<name.length;i++){hash=(hash<<5)-hash+name.charCodeAt(i)|0}return(parentid+hash>>>0)%FS.nameTable.length},hashAddNode(node){var hash=FS.hashName(node.parent.id,node.name);node.name_next=FS.nameTable[hash];FS.nameTable[hash]=node},hashRemoveNode(node){var hash=FS.hashName(node.parent.id,node.name);if(FS.nameTable[hash]===node){FS.nameTable[hash]=node.name_next}else{var current=FS.nameTable[hash];while(current){if(current.name_next===node){current.name_next=node.name_next;break}current=current.name_next}}},lookupNode(parent,name){var errCode=FS.mayLookup(parent);if(errCode){throw new FS.ErrnoError(errCode)}var hash=FS.hashName(parent.id,name);for(var node=FS.nameTable[hash];node;node=node.name_next){var nodeName=node.name;if(node.parent.id===parent.id&&nodeName===name){return node}}return FS.lookup(parent,name)},createNode(parent,name,mode,rdev){var node=new FS.FSNode(parent,name,mode,rdev);FS.hashAddNode(node);return node},destroyNode(node){FS.hashRemoveNode(node)},isRoot(node){return node===node.parent},isMountpoint(node){return!!node.mounted},isFile(mode){return(mode&61440)===32768},isDir(mode){return(mode&61440)===16384},isLink(mode){return(mode&61440)===40960},isChrdev(mode){return(mode&61440)===8192},isBlkdev(mode){return(mode&61440)===24576},isFIFO(mode){return(mode&61440)===4096},isSocket(mode){return(mode&49152)===49152},flagsToPermissionString(flag){var perms=["r","w","rw"][flag&3];if(flag&512){perms+="w"}return perms},nodePermissions(node,perms){if(FS.ignorePermissions){return 0}if(perms.includes("r")&&!(node.mode&292)){return 2}else if(perms.includes("w")&&!(node.mode&146)){return 2}else if(perms.includes("x")&&!(node.mode&73)){return 2}return 0},mayLookup(dir){if(!FS.isDir(dir.mode))return 54;var errCode=FS.nodePermissions(dir,"x");if(errCode)return errCode;if(!dir.node_ops.lookup)return 2;return 0},mayCreate(dir,name){if(!FS.isDir(dir.mode)){return 54}try{var node=FS.lookupNode(dir,name);return 20}catch(e){}return FS.nodePermissions(dir,"wx")},mayDelete(dir,name,isdir){var node;try{node=FS.lookupNode(dir,name)}catch(e){return e.errno}var errCode=FS.nodePermissions(dir,"wx");if(errCode){return errCode}if(isdir){if(!FS.isDir(node.mode)){return 54}if(FS.isRoot(node)||FS.getPath(node)===FS.cwd()){return 10}}else{if(FS.isDir(node.mode)){return 31}}return 0},mayOpen(node,flags){if(!node){return 44}if(FS.isLink(node.mode)){return 32}else if(FS.isDir(node.mode)){if(FS.flagsToPermissionString(flags)!=="r"||flags&(512|64)){return 31}}return FS.nodePermissions(node,FS.flagsToPermissionString(flags))},checkOpExists(op,err){if(!op){throw new FS.ErrnoError(err)}return op},MAX_OPEN_FDS:4096,nextfd(){for(var fd=0;fd<=FS.MAX_OPEN_FDS;fd++){if(!FS.streams[fd]){return fd}}throw new FS.ErrnoError(33)},getStreamChecked(fd){var stream=FS.getStream(fd);if(!stream){throw new FS.ErrnoError(8)}return stream},getStream:fd=>FS.streams[fd],createStream(stream,fd=-1){stream=Object.assign(new FS.FSStream,stream);if(fd==-1){fd=FS.nextfd()}stream.fd=fd;FS.streams[fd]=stream;return stream},closeStream(fd){FS.streams[fd]=null},dupStream(origStream,fd=-1){var stream=FS.createStream(origStream,fd);stream.stream_ops?.dup?.(stream);return stream},doSetAttr(stream,node,attr){var setattr=stream?.stream_ops.setattr;var arg=setattr?stream:node;setattr??=node.node_ops.setattr;FS.checkOpExists(setattr,63);setattr(arg,attr)},chrdev_stream_ops:{open(stream){var device=FS.getDevice(stream.node.rdev);stream.stream_ops=device.stream_ops;stream.stream_ops.open?.(stream)},llseek(){throw new FS.ErrnoError(70)}},major:dev=>dev>>8,minor:dev=>dev&255,makedev:(ma,mi)=>ma<<8|mi,registerDevice(dev,ops){FS.devices[dev]={stream_ops:ops}},getDevice:dev=>FS.devices[dev],getMounts(mount){var mounts=[];var check=[mount];while(check.length){var m=check.pop();mounts.push(m);check.push(...m.mounts)}return mounts},syncfs(populate,callback){if(typeof populate=="function"){callback=populate;populate=false}FS.syncFSRequests++;if(FS.syncFSRequests>1){err(`warning: ${FS.syncFSRequests} FS.syncfs operations in flight at once, probably just doing extra work`)}var mounts=FS.getMounts(FS.root.mount);var completed=0;function doCallback(errCode){FS.syncFSRequests--;return callback(errCode)}function done(errCode){if(errCode){if(!done.errored){done.errored=true;return doCallback(errCode)}return}if(++completed>=mounts.length){doCallback(null)}}for(var mount of mounts){if(mount.type.syncfs){mount.type.syncfs(mount,populate,done)}else{done(null)}}},mount(type,opts,mountpoint){var root=mountpoint==="/";var pseudo=!mountpoint;var node;if(root&&FS.root){throw new FS.ErrnoError(10)}else if(!root&&!pseudo){var lookup=FS.lookupPath(mountpoint,{follow_mount:false});mountpoint=lookup.path;node=lookup.node;if(FS.isMountpoint(node)){throw new FS.ErrnoError(10)}if(!FS.isDir(node.mode)){throw new FS.ErrnoError(54)}}var mount={type,opts,mountpoint,mounts:[]};var mountRoot=type.mount(mount);mountRoot.mount=mount;mount.root=mountRoot;if(root){FS.root=mountRoot}else if(node){node.mounted=mount;if(node.mount){node.mount.mounts.push(mount)}}return mountRoot},unmount(mountpoint){var lookup=FS.lookupPath(mountpoint,{follow_mount:false});if(!FS.isMountpoint(lookup.node)){throw new FS.ErrnoError(28)}var node=lookup.node;var mount=node.mounted;var mounts=FS.getMounts(mount);for(var[hash,current]of Object.entries(FS.nameTable)){while(current){var next=current.name_next;if(mounts.includes(current.mount)){FS.destroyNode(current)}current=next}}node.mounted=null;var idx=node.mount.mounts.indexOf(mount);node.mount.mounts.splice(idx,1)},lookup(parent,name){return parent.node_ops.lookup(parent,name)},mknod(path,mode,dev){var lookup=FS.lookupPath(path,{parent:true});var parent=lookup.node;var name=PATH.basename(path);if(!name){throw new FS.ErrnoError(28)}if(name==="."||name===".."){throw new FS.ErrnoError(20)}var errCode=FS.mayCreate(parent,name);if(errCode){throw new FS.ErrnoError(errCode)}if(!parent.node_ops.mknod){throw new FS.ErrnoError(63)}return parent.node_ops.mknod(parent,name,mode,dev)},statfs(path){return FS.statfsNode(FS.lookupPath(path,{follow:true}).node)},statfsStream(stream){return FS.statfsNode(stream.node)},statfsNode(node){var rtn={bsize:4096,frsize:4096,blocks:1e6,bfree:5e5,bavail:5e5,files:FS.nextInode,ffree:FS.nextInode-1,fsid:42,flags:2,namelen:255};if(node.node_ops.statfs){Object.assign(rtn,node.node_ops.statfs(node.mount.opts.root))}return rtn},create(path,mode=438){mode&=4095;mode|=32768;return FS.mknod(path,mode,0)},mkdir(path,mode=511){mode&=511|512;mode|=16384;return FS.mknod(path,mode,0)},mkdirTree(path,mode){var dirs=path.split("/");var d="";for(var dir of dirs){if(!dir)continue;if(d||PATH.isAbs(path))d+="/";d+=dir;try{FS.mkdir(d,mode)}catch(e){if(e.errno!=20)throw e}}},mkdev(path,mode,dev){if(typeof dev=="undefined"){dev=mode;mode=438}mode|=8192;return FS.mknod(path,mode,dev)},symlink(oldpath,newpath){if(!PATH_FS.resolve(oldpath)){throw new FS.ErrnoError(44)}var lookup=FS.lookupPath(newpath,{parent:true});var parent=lookup.node;if(!parent){throw new FS.ErrnoError(44)}var newname=PATH.basename(newpath);var errCode=FS.mayCreate(parent,newname);if(errCode){throw new FS.ErrnoError(errCode)}if(!parent.node_ops.symlink){throw new FS.ErrnoError(63)}return parent.node_ops.symlink(parent,newname,oldpath)},rename(old_path,new_path){var old_dirname=PATH.dirname(old_path);var new_dirname=PATH.dirname(new_path);var old_name=PATH.basename(old_path);var new_name=PATH.basename(new_path);var lookup,old_dir,new_dir;lookup=FS.lookupPath(old_path,{parent:true});old_dir=lookup.node;lookup=FS.lookupPath(new_path,{parent:true});new_dir=lookup.node;if(!old_dir||!new_dir)throw new FS.ErrnoError(44);if(old_dir.mount!==new_dir.mount){throw new FS.ErrnoError(75)}var old_node=FS.lookupNode(old_dir,old_name);var relative=PATH_FS.relative(old_path,new_dirname);if(relative.charAt(0)!=="."){throw new FS.ErrnoError(28)}relative=PATH_FS.relative(new_path,old_dirname);if(relative.charAt(0)!=="."){throw new FS.ErrnoError(55)}var new_node;try{new_node=FS.lookupNode(new_dir,new_name)}catch(e){}if(old_node===new_node){return}var isdir=FS.isDir(old_node.mode);var errCode=FS.mayDelete(old_dir,old_name,isdir);if(errCode){throw new FS.ErrnoError(errCode)}errCode=new_node?FS.mayDelete(new_dir,new_name,isdir):FS.mayCreate(new_dir,new_name);if(errCode){throw new FS.ErrnoError(errCode)}if(!old_dir.node_ops.rename){throw new FS.ErrnoError(63)}if(FS.isMountpoint(old_node)||new_node&&FS.isMountpoint(new_node)){throw new FS.ErrnoError(10)}if(new_dir!==old_dir){errCode=FS.nodePermissions(old_dir,"w");if(errCode){throw new FS.ErrnoError(errCode)}}FS.hashRemoveNode(old_node);try{old_dir.node_ops.rename(old_node,new_dir,new_name);old_node.parent=new_dir}catch(e){throw e}finally{FS.hashAddNode(old_node)}},rmdir(path){var lookup=FS.lookupPath(path,{parent:true});var parent=lookup.node;var name=PATH.basename(path);var node=FS.lookupNode(parent,name);var errCode=FS.mayDelete(parent,name,true);if(errCode){throw new FS.ErrnoError(errCode)}if(!parent.node_ops.rmdir){throw new FS.ErrnoError(63)}if(FS.isMountpoint(node)){throw new FS.ErrnoError(10)}parent.node_ops.rmdir(parent,name);FS.destroyNode(node)},readdir(path){var lookup=FS.lookupPath(path,{follow:true});var node=lookup.node;var readdir=FS.checkOpExists(node.node_ops.readdir,54);return readdir(node)},unlink(path){var lookup=FS.lookupPath(path,{parent:true});var parent=lookup.node;if(!parent){throw new FS.ErrnoError(44)}var name=PATH.basename(path);var node=FS.lookupNode(parent,name);var errCode=FS.mayDelete(parent,name,false);if(errCode){throw new FS.ErrnoError(errCode)}if(!parent.node_ops.unlink){throw new FS.ErrnoError(63)}if(FS.isMountpoint(node)){throw new FS.ErrnoError(10)}parent.node_ops.unlink(parent,name);FS.destroyNode(node)},readlink(path){var lookup=FS.lookupPath(path);var link=lookup.node;if(!link){throw new FS.ErrnoError(44)}if(!link.node_ops.readlink){throw new FS.ErrnoError(28)}return link.node_ops.readlink(link)},stat(path,dontFollow){var lookup=FS.lookupPath(path,{follow:!dontFollow});var node=lookup.node;var getattr=FS.checkOpExists(node.node_ops.getattr,63);return getattr(node)},fstat(fd){var stream=FS.getStreamChecked(fd);var node=stream.node;var getattr=stream.stream_ops.getattr;var arg=getattr?stream:node;getattr??=node.node_ops.getattr;FS.checkOpExists(getattr,63);return getattr(arg)},lstat(path){return FS.stat(path,true)},doChmod(stream,node,mode,dontFollow){FS.doSetAttr(stream,node,{mode:mode&4095|node.mode&~4095,ctime:Date.now(),dontFollow})},chmod(path,mode,dontFollow){var node;if(typeof path=="string"){var lookup=FS.lookupPath(path,{follow:!dontFollow});node=lookup.node}else{node=path}FS.doChmod(null,node,mode,dontFollow)},lchmod(path,mode){FS.chmod(path,mode,true)},fchmod(fd,mode){var stream=FS.getStreamChecked(fd);FS.doChmod(stream,stream.node,mode,false)},doChown(stream,node,dontFollow){FS.doSetAttr(stream,node,{timestamp:Date.now(),dontFollow})},chown(path,uid,gid,dontFollow){var node;if(typeof path=="string"){var lookup=FS.lookupPath(path,{follow:!dontFollow});node=lookup.node}else{node=path}FS.doChown(null,node,dontFollow)},lchown(path,uid,gid){FS.chown(path,uid,gid,true)},fchown(fd,uid,gid){var stream=FS.getStreamChecked(fd);FS.doChown(stream,stream.node,false)},doTruncate(stream,node,len){if(FS.isDir(node.mode)){throw new FS.ErrnoError(31)}if(!FS.isFile(node.mode)){throw new FS.ErrnoError(28)}var errCode=FS.nodePermissions(node,"w");if(errCode){throw new FS.ErrnoError(errCode)}FS.doSetAttr(stream,node,{size:len,timestamp:Date.now()})},truncate(path,len){if(len<0){throw new FS.ErrnoError(28)}var node;if(typeof path=="string"){var lookup=FS.lookupPath(path,{follow:true});node=lookup.node}else{node=path}FS.doTruncate(null,node,len)},ftruncate(fd,len){var stream=FS.getStreamChecked(fd);if(len<0||(stream.flags&2097155)===0){throw new FS.ErrnoError(28)}FS.doTruncate(stream,stream.node,len)},utime(path,atime,mtime){var lookup=FS.lookupPath(path,{follow:true});var node=lookup.node;var setattr=FS.checkOpExists(node.node_ops.setattr,63);setattr(node,{atime,mtime})},open(path,flags,mode=438){if(path===""){throw new FS.ErrnoError(44)}flags=typeof flags=="string"?FS_modeStringToFlags(flags):flags;if(flags&64){mode=mode&4095|32768}else{mode=0}var node;var isDirPath;if(typeof path=="object"){node=path}else{isDirPath=path.endsWith("/");var lookup=FS.lookupPath(path,{follow:!(flags&131072),noent_okay:true});node=lookup.node;path=lookup.path}var created=false;if(flags&64){if(node){if(flags&128){throw new FS.ErrnoError(20)}}else if(isDirPath){throw new FS.ErrnoError(31)}else{node=FS.mknod(path,mode|511,0);created=true}}if(!node){throw new FS.ErrnoError(44)}if(FS.isChrdev(node.mode)){flags&=~512}if(flags&65536&&!FS.isDir(node.mode)){throw new FS.ErrnoError(54)}if(!created){var errCode=FS.mayOpen(node,flags);if(errCode){throw new FS.ErrnoError(errCode)}}if(flags&512&&!created){FS.truncate(node,0)}flags&=~(128|512|131072);var stream=FS.createStream({node,path:FS.getPath(node),flags,seekable:true,position:0,stream_ops:node.stream_ops,ungotten:[],error:false});if(stream.stream_ops.open){stream.stream_ops.open(stream)}if(created){FS.chmod(node,mode&511)}if(Module["logReadFiles"]&&!(flags&1)){if(!(path in FS.readFiles)){FS.readFiles[path]=1}}return stream},close(stream){if(FS.isClosed(stream)){throw new FS.ErrnoError(8)}if(stream.getdents)stream.getdents=null;try{if(stream.stream_ops.close){stream.stream_ops.close(stream)}}catch(e){throw e}finally{FS.closeStream(stream.fd)}stream.fd=null},isClosed(stream){return stream.fd===null},llseek(stream,offset,whence){if(FS.isClosed(stream)){throw new FS.ErrnoError(8)}if(!stream.seekable||!stream.stream_ops.llseek){throw new FS.ErrnoError(70)}if(whence!=0&&whence!=1&&whence!=2){throw new FS.ErrnoError(28)}stream.position=stream.stream_ops.llseek(stream,offset,whence);stream.ungotten=[];return stream.position},read(stream,buffer,offset,length,position){if(length<0||position<0){throw new FS.ErrnoError(28)}if(FS.isClosed(stream)){throw new FS.ErrnoError(8)}if((stream.flags&2097155)===1){throw new FS.ErrnoError(8)}if(FS.isDir(stream.node.mode)){throw new FS.ErrnoError(31)}if(!stream.stream_ops.read){throw new FS.ErrnoError(28)}var seeking=typeof position!="undefined";if(!seeking){position=stream.position}else if(!stream.seekable){throw new FS.ErrnoError(70)}var bytesRead=stream.stream_ops.read(stream,buffer,offset,length,position);if(!seeking)stream.position+=bytesRead;return bytesRead},write(stream,buffer,offset,length,position,canOwn){if(length<0||position<0){throw new FS.ErrnoError(28)}if(FS.isClosed(stream)){throw new FS.ErrnoError(8)}if((stream.flags&2097155)===0){throw new FS.ErrnoError(8)}if(FS.isDir(stream.node.mode)){throw new FS.ErrnoError(31)}if(!stream.stream_ops.write){throw new FS.ErrnoError(28)}if(stream.seekable&&stream.flags&1024){FS.llseek(stream,0,2)}var seeking=typeof position!="undefined";if(!seeking){position=stream.position}else if(!stream.seekable){throw new FS.ErrnoError(70)}var bytesWritten=stream.stream_ops.write(stream,buffer,offset,length,position,canOwn);if(!seeking)stream.position+=bytesWritten;return bytesWritten},mmap(stream,length,position,prot,flags){if((prot&2)!==0&&(flags&2)===0&&(stream.flags&2097155)!==2){throw new FS.ErrnoError(2)}if((stream.flags&2097155)===1){throw new FS.ErrnoError(2)}if(!stream.stream_ops.mmap){throw new FS.ErrnoError(43)}if(!length){throw new FS.ErrnoError(28)}return stream.stream_ops.mmap(stream,length,position,prot,flags)},msync(stream,buffer,offset,length,mmapFlags){if(!stream.stream_ops.msync){return 0}return stream.stream_ops.msync(stream,buffer,offset,length,mmapFlags)},ioctl(stream,cmd,arg){if(!stream.stream_ops.ioctl){throw new FS.ErrnoError(59)}return stream.stream_ops.ioctl(stream,cmd,arg)},readFile(path,opts={}){opts.flags=opts.flags||0;opts.encoding=opts.encoding||"binary";if(opts.encoding!=="utf8"&&opts.encoding!=="binary"){abort(`Invalid encoding type "${opts.encoding}"`)}var stream=FS.open(path,opts.flags);var stat=FS.stat(path);var length=stat.size;var buf=new Uint8Array(length);FS.read(stream,buf,0,length,0);if(opts.encoding==="utf8"){buf=UTF8ArrayToString(buf)}FS.close(stream);return buf},writeFile(path,data,opts={}){opts.flags=opts.flags||577;var stream=FS.open(path,opts.flags,opts.mode);if(typeof data=="string"){data=new Uint8Array(intArrayFromString(data,true))}if(ArrayBuffer.isView(data)){FS.write(stream,data,0,data.byteLength,undefined,opts.canOwn)}else{abort("Unsupported data type")}FS.close(stream)},cwd:()=>FS.currentPath,chdir(path){var lookup=FS.lookupPath(path,{follow:true});if(lookup.node===null){throw new FS.ErrnoError(44)}if(!FS.isDir(lookup.node.mode)){throw new FS.ErrnoError(54)}var errCode=FS.nodePermissions(lookup.node,"x");if(errCode){throw new FS.ErrnoError(errCode)}FS.currentPath=lookup.path},createDefaultDirectories(){FS.mkdir("/tmp");FS.mkdir("/home");FS.mkdir("/home/web_user")},createDefaultDevices(){FS.mkdir("/dev");FS.registerDevice(FS.makedev(1,3),{read:()=>0,write:(stream,buffer,offset,length,pos)=>length,llseek:()=>0});FS.mkdev("/dev/null",FS.makedev(1,3));TTY.register(FS.makedev(5,0),TTY.default_tty_ops);TTY.register(FS.makedev(6,0),TTY.default_tty1_ops);FS.mkdev("/dev/tty",FS.makedev(5,0));FS.mkdev("/dev/tty1",FS.makedev(6,0));var randomBuffer=new Uint8Array(1024),randomLeft=0;var randomByte=()=>{if(randomLeft===0){randomFill(randomBuffer);randomLeft=randomBuffer.byteLength}return randomBuffer[--randomLeft]};FS.createDevice("/dev","random",randomByte);FS.createDevice("/dev","urandom",randomByte);FS.mkdir("/dev/shm");FS.mkdir("/dev/shm/tmp")},createSpecialDirectories(){FS.mkdir("/proc");var proc_self=FS.mkdir("/proc/self");FS.mkdir("/proc/self/fd");FS.mount({mount(){var node=FS.createNode(proc_self,"fd",16895,73);node.stream_ops={llseek:MEMFS.stream_ops.llseek};node.node_ops={lookup(parent,name){var fd=+name;var stream=FS.getStreamChecked(fd);var ret={parent:null,mount:{mountpoint:"fake"},node_ops:{readlink:()=>stream.path},id:fd+1};ret.parent=ret;return ret},readdir(){return Array.from(FS.streams.entries()).filter(([k,v])=>v).map(([k,v])=>k.toString())}};return node}},{},"/proc/self/fd")},createStandardStreams(input,output,error){if(input){FS.createDevice("/dev","stdin",input)}else{FS.symlink("/dev/tty","/dev/stdin")}if(output){FS.createDevice("/dev","stdout",null,output)}else{FS.symlink("/dev/tty","/dev/stdout")}if(error){FS.createDevice("/dev","stderr",null,error)}else{FS.symlink("/dev/tty1","/dev/stderr")}var stdin=FS.open("/dev/stdin",0);var stdout=FS.open("/dev/stdout",1);var stderr=FS.open("/dev/stderr",1)},staticInit(){FS.nameTable=new Array(4096);FS.mount(MEMFS,{},"/");FS.createDefaultDirectories();FS.createDefaultDevices();FS.createSpecialDirectories();FS.filesystems={MEMFS}},init(input,output,error){FS.initialized=true;input??=Module["stdin"];output??=Module["stdout"];error??=Module["stderr"];FS.createStandardStreams(input,output,error)},quit(){FS.initialized=false;for(var stream of FS.streams){if(stream){FS.close(stream)}}},findObject(path,dontResolveLastLink){var ret=FS.analyzePath(path,dontResolveLastLink);if(!ret.exists){return null}return ret.object},analyzePath(path,dontResolveLastLink){try{var lookup=FS.lookupPath(path,{follow:!dontResolveLastLink});path=lookup.path}catch(e){}var ret={isRoot:false,exists:false,error:0,name:null,path:null,object:null,parentExists:false,parentPath:null,parentObject:null};try{var lookup=FS.lookupPath(path,{parent:true});ret.parentExists=true;ret.parentPath=lookup.path;ret.parentObject=lookup.node;ret.name=PATH.basename(path);lookup=FS.lookupPath(path,{follow:!dontResolveLastLink});ret.exists=true;ret.path=lookup.path;ret.object=lookup.node;ret.name=lookup.node.name;ret.isRoot=lookup.path==="/"}catch(e){ret.error=e.errno}return ret},createPath(parent,path,canRead,canWrite){parent=typeof parent=="string"?parent:FS.getPath(parent);var parts=path.split("/").reverse();while(parts.length){var part=parts.pop();if(!part)continue;var current=PATH.join2(parent,part);try{FS.mkdir(current)}catch(e){if(e.errno!=20)throw e}parent=current}return current},createFile(parent,name,properties,canRead,canWrite){var path=PATH.join2(typeof parent=="string"?parent:FS.getPath(parent),name);var mode=FS_getMode(canRead,canWrite);return FS.create(path,mode)},createDataFile(parent,name,data,canRead,canWrite,canOwn){var path=name;if(parent){parent=typeof parent=="string"?parent:FS.getPath(parent);path=name?PATH.join2(parent,name):parent}var mode=FS_getMode(canRead,canWrite);var node=FS.create(path,mode);if(data){if(typeof data=="string"){var arr=new Array(data.length);for(var i=0,len=data.length;i<len;++i)arr[i]=data.charCodeAt(i);data=arr}FS.chmod(node,mode|146);var stream=FS.open(node,577);FS.write(stream,data,0,data.length,0,canOwn);FS.close(stream);FS.chmod(node,mode)}},createDevice(parent,name,input,output){var path=PATH.join2(typeof parent=="string"?parent:FS.getPath(parent),name);var mode=FS_getMode(!!input,!!output);FS.createDevice.major??=64;var dev=FS.makedev(FS.createDevice.major++,0);FS.registerDevice(dev,{open(stream){stream.seekable=false},close(stream){if(output?.buffer?.length){output(10)}},read(stream,buffer,offset,length,pos){var bytesRead=0;for(var i=0;i<length;i++){var result;try{result=input()}catch(e){throw new FS.ErrnoError(29)}if(result===undefined&&bytesRead===0){throw new FS.ErrnoError(6)}if(result===null||result===undefined)break;bytesRead++;buffer[offset+i]=result}if(bytesRead){stream.node.atime=Date.now()}return bytesRead},write(stream,buffer,offset,length,pos){for(var i=0;i<length;i++){try{output(buffer[offset+i])}catch(e){throw new FS.ErrnoError(29)}}if(length){stream.node.mtime=stream.node.ctime=Date.now()}return i}});return FS.mkdev(path,mode,dev)},forceLoadFile(obj){if(obj.isDevice||obj.isFolder||obj.link||obj.contents)return true;if(globalThis.XMLHttpRequest){abort("Lazy loading should have been performed (contents set) in createLazyFile, but it was not. Lazy loading only works in web workers. Use --embed-file or --preload-file in emcc on the main thread.")}else{try{obj.contents=readBinary(obj.url)}catch(e){throw new FS.ErrnoError(29)}}},createLazyFile(parent,name,url,canRead,canWrite){class LazyUint8Array{lengthKnown=false;chunks=[];get(idx){if(idx>this.length-1||idx<0){return undefined}var chunkOffset=idx%this.chunkSize;var chunkNum=idx/this.chunkSize|0;return this.getter(chunkNum)[chunkOffset]}setDataGetter(getter){this.getter=getter}cacheLength(){var xhr=new XMLHttpRequest;xhr.open("HEAD",url,false);xhr.send(null);if(!(xhr.status>=200&&xhr.status<300||xhr.status===304))abort("Couldn't load "+url+". Status: "+xhr.status);var datalength=Number(xhr.getResponseHeader("Content-length"));var header;var hasByteServing=(header=xhr.getResponseHeader("Accept-Ranges"))&&header==="bytes";var usesGzip=(header=xhr.getResponseHeader("Content-Encoding"))&&header==="gzip";var chunkSize=1024*1024;if(!hasByteServing)chunkSize=datalength;var doXHR=(from,to)=>{if(from>to)abort("invalid range ("+from+", "+to+") or no bytes requested!");if(to>datalength-1)abort("only "+datalength+" bytes available! programmer error!");var xhr=new XMLHttpRequest;xhr.open("GET",url,false);if(datalength!==chunkSize)xhr.setRequestHeader("Range","bytes="+from+"-"+to);xhr.responseType="arraybuffer";if(xhr.overrideMimeType){xhr.overrideMimeType("text/plain; charset=x-user-defined")}xhr.send(null);if(!(xhr.status>=200&&xhr.status<300||xhr.status===304))abort("Couldn't load "+url+". Status: "+xhr.status);if(xhr.response!==undefined){return new Uint8Array(xhr.response||[])}return intArrayFromString(xhr.responseText||"",true)};var lazyArray=this;lazyArray.setDataGetter(chunkNum=>{var start=chunkNum*chunkSize;var end=(chunkNum+1)*chunkSize-1;end=Math.min(end,datalength-1);if(typeof lazyArray.chunks[chunkNum]=="undefined"){lazyArray.chunks[chunkNum]=doXHR(start,end)}if(typeof lazyArray.chunks[chunkNum]=="undefined")abort("doXHR failed!");return lazyArray.chunks[chunkNum]});if(usesGzip||!datalength){chunkSize=datalength=1;datalength=this.getter(0).length;chunkSize=datalength;out("LazyFiles on gzip forces download of the whole file when length is accessed")}this._length=datalength;this._chunkSize=chunkSize;this.lengthKnown=true}get length(){if(!this.lengthKnown){this.cacheLength()}return this._length}get chunkSize(){if(!this.lengthKnown){this.cacheLength()}return this._chunkSize}}if(globalThis.XMLHttpRequest){if(!ENVIRONMENT_IS_WORKER)abort("Cannot do synchronous binary XHRs outside webworkers in modern browsers. Use --embed-file or --preload-file in emcc");var lazyArray=new LazyUint8Array;var properties={isDevice:false,contents:lazyArray}}else{var properties={isDevice:false,url}}var node=FS.createFile(parent,name,properties,canRead,canWrite);if(properties.contents){node.contents=properties.contents}else if(properties.url){node.contents=null;node.url=properties.url}Object.defineProperties(node,{usedBytes:{get:function(){return this.contents.length}}});var stream_ops={};for(const[key,fn]of Object.entries(node.stream_ops)){stream_ops[key]=(...args)=>{FS.forceLoadFile(node);return fn(...args)}}function writeChunks(stream,buffer,offset,length,position){var contents=stream.node.contents;if(position>=contents.length)return 0;var size=Math.min(contents.length-position,length);if(contents.slice){for(var i=0;i<size;i++){buffer[offset+i]=contents[position+i]}}else{for(var i=0;i<size;i++){buffer[offset+i]=contents.get(position+i)}}return size}stream_ops.read=(stream,buffer,offset,length,position)=>{FS.forceLoadFile(node);return writeChunks(stream,buffer,offset,length,position)};stream_ops.mmap=(stream,length,position,prot,flags)=>{FS.forceLoadFile(node);var ptr=mmapAlloc(length);if(!ptr){throw new FS.ErrnoError(48)}writeChunks(stream,HEAP8,ptr,length,position);return{ptr,allocated:true}};node.stream_ops=stream_ops;return node}};var UTF8ToString=(ptr,maxBytesToRead,ignoreNul)=>ptr?UTF8ArrayToString(HEAPU8,ptr,maxBytesToRead,ignoreNul):"";var SYSCALLS={DEFAULT_POLLMASK:5,calculateAt(dirfd,path,allowEmpty){if(PATH.isAbs(path)){return path}var dir;if(dirfd===-100){dir=FS.cwd()}else{var dirstream=SYSCALLS.getStreamFromFD(dirfd);dir=dirstream.path}if(path.length==0){if(!allowEmpty){throw new FS.ErrnoError(44)}return dir}return dir+"/"+path},writeStat(buf,stat){HEAPU32[buf>>2]=stat.dev;HEAPU32[buf+4>>2]=stat.mode;HEAPU32[buf+8>>2]=stat.nlink;HEAPU32[buf+12>>2]=stat.uid;HEAPU32[buf+16>>2]=stat.gid;HEAPU32[buf+20>>2]=stat.rdev;HEAP64[buf+24>>3]=BigInt(stat.size);HEAP32[buf+32>>2]=4096;HEAP32[buf+36>>2]=stat.blocks;var atime=stat.atime.getTime();var mtime=stat.mtime.getTime();var ctime=stat.ctime.getTime();HEAP64[buf+40>>3]=BigInt(Math.floor(atime/1e3));HEAPU32[buf+48>>2]=atime%1e3*1e3*1e3;HEAP64[buf+56>>3]=BigInt(Math.floor(mtime/1e3));HEAPU32[buf+64>>2]=mtime%1e3*1e3*1e3;HEAP64[buf+72>>3]=BigInt(Math.floor(ctime/1e3));HEAPU32[buf+80>>2]=ctime%1e3*1e3*1e3;HEAP64[buf+88>>3]=BigInt(stat.ino);return 0},writeStatFs(buf,stats){HEAPU32[buf+4>>2]=stats.bsize;HEAPU32[buf+60>>2]=stats.bsize;HEAP64[buf+8>>3]=BigInt(stats.blocks);HEAP64[buf+16>>3]=BigInt(stats.bfree);HEAP64[buf+24>>3]=BigInt(stats.bavail);HEAP64[buf+32>>3]=BigInt(stats.files);HEAP64[buf+40>>3]=BigInt(stats.ffree);HEAPU32[buf+48>>2]=stats.fsid;HEAPU32[buf+64>>2]=stats.flags;HEAPU32[buf+56>>2]=stats.namelen},doMsync(addr,stream,len,flags,offset){if(!FS.isFile(stream.node.mode)){throw new FS.ErrnoError(43)}if(flags&2){return 0}var buffer=HEAPU8.slice(addr,addr+len);FS.msync(stream,buffer,offset,len,flags)},getStreamFromFD(fd){var stream=FS.getStreamChecked(fd);return stream},varargs:undefined,getStr(ptr){var ret=UTF8ToString(ptr);return ret}};function ___syscall_fcntl64(fd,cmd,varargs){SYSCALLS.varargs=varargs;try{var stream=SYSCALLS.getStreamFromFD(fd);switch(cmd){case 0:{var arg=syscallGetVarargI();if(arg<0){return-28}while(FS.streams[arg]){arg++}var newStream;newStream=FS.dupStream(stream,arg);return newStream.fd}case 1:case 2:return 0;case 3:return stream.flags;case 4:{var arg=syscallGetVarargI();stream.flags|=arg;return 0}case 12:{var arg=syscallGetVarargP();var offset=0;HEAP16[arg+offset>>1]=2;return 0}case 13:case 14:return 0}return-28}catch(e){if(typeof FS=="undefined"||!(e.name==="ErrnoError"))throw e;return-e.errno}}function ___syscall_ioctl(fd,op,varargs){SYSCALLS.varargs=varargs;try{var stream=SYSCALLS.getStreamFromFD(fd);switch(op){case 21509:{if(!stream.tty)return-59;return 0}case 21505:{if(!stream.tty)return-59;if(stream.tty.ops.ioctl_tcgets){var termios=stream.tty.ops.ioctl_tcgets(stream);var argp=syscallGetVarargP();HEAP32[argp>>2]=termios.c_iflag||0;HEAP32[argp+4>>2]=termios.c_oflag||0;HEAP32[argp+8>>2]=termios.c_cflag||0;HEAP32[argp+12>>2]=termios.c_lflag||0;for(var i=0;i<32;i++){HEAP8[argp+i+17]=termios.c_cc[i]||0}return 0}return 0}case 21510:case 21511:case 21512:{if(!stream.tty)return-59;return 0}case 21506:case 21507:case 21508:{if(!stream.tty)return-59;if(stream.tty.ops.ioctl_tcsets){var argp=syscallGetVarargP();var c_iflag=HEAP32[argp>>2];var c_oflag=HEAP32[argp+4>>2];var c_cflag=HEAP32[argp+8>>2];var c_lflag=HEAP32[argp+12>>2];var c_cc=[];for(var i=0;i<32;i++){c_cc.push(HEAP8[argp+i+17])}return stream.tty.ops.ioctl_tcsets(stream.tty,op,{c_iflag,c_oflag,c_cflag,c_lflag,c_cc})}return 0}case 21519:{if(!stream.tty)return-59;var argp=syscallGetVarargP();HEAP32[argp>>2]=0;return 0}case 21520:{if(!stream.tty)return-59;return-28}case 21537:case 21531:{var argp=syscallGetVarargP();return FS.ioctl(stream,op,argp)}case 21523:{if(!stream.tty)return-59;if(stream.tty.ops.ioctl_tiocgwinsz){var winsize=stream.tty.ops.ioctl_tiocgwinsz(stream.tty);var argp=syscallGetVarargP();HEAP16[argp>>1]=winsize[0];HEAP16[argp+2>>1]=winsize[1]}return 0}case 21524:{if(!stream.tty)return-59;return 0}case 21515:{if(!stream.tty)return-59;return 0}default:return-28}}catch(e){if(typeof FS=="undefined"||!(e.name==="ErrnoError"))throw e;return-e.errno}}function ___syscall_mkdirat(dirfd,path,mode){try{path=SYSCALLS.getStr(path);path=SYSCALLS.calculateAt(dirfd,path);FS.mkdir(path,mode,0);return 0}catch(e){if(typeof FS=="undefined"||!(e.name==="ErrnoError"))throw e;return-e.errno}}function ___syscall_openat(dirfd,path,flags,varargs){SYSCALLS.varargs=varargs;try{path=SYSCALLS.getStr(path);path=SYSCALLS.calculateAt(dirfd,path);var mode=varargs?syscallGetVarargI():0;return FS.open(path,flags,mode).fd}catch(e){if(typeof FS=="undefined"||!(e.name==="ErrnoError"))throw e;return-e.errno}}function ___syscall_renameat(olddirfd,oldpath,newdirfd,newpath){try{oldpath=SYSCALLS.getStr(oldpath);newpath=SYSCALLS.getStr(newpath);oldpath=SYSCALLS.calculateAt(olddirfd,oldpath);newpath=SYSCALLS.calculateAt(newdirfd,newpath);FS.rename(oldpath,newpath);return 0}catch(e){if(typeof FS=="undefined"||!(e.name==="ErrnoError"))throw e;return-e.errno}}function ___syscall_rmdir(path){try{path=SYSCALLS.getStr(path);FS.rmdir(path);return 0}catch(e){if(typeof FS=="undefined"||!(e.name==="ErrnoError"))throw e;return-e.errno}}function ___syscall_unlinkat(dirfd,path,flags){try{path=SYSCALLS.getStr(path);path=SYSCALLS.calculateAt(dirfd,path);if(!flags){FS.unlink(path)}else if(flags===512){FS.rmdir(path)}else{return-28}return 0}catch(e){if(typeof FS=="undefined"||!(e.name==="ErrnoError"))throw e;return-e.errno}}var __emscripten_system=command=>{if(ENVIRONMENT_IS_NODE){if(!command)return 1;var cmdstr=UTF8ToString(command);if(!cmdstr.length)return 0;var cp=require("child_process");var ret=cp.spawnSync(cmdstr,[],{shell:true,stdio:"inherit"});var _W_EXITCODE=(ret,sig)=>ret<<8|sig;if(ret.status===null){var signalToNumber=sig=>{switch(sig){case"SIGHUP":return 1;case"SIGQUIT":return 3;case"SIGFPE":return 8;case"SIGKILL":return 9;case"SIGALRM":return 14;case"SIGTERM":return 15;default:return 2}};return _W_EXITCODE(0,signalToNumber(ret.signal))}return _W_EXITCODE(ret.status,0)}if(!command)return 0;return-52};var _emscripten_get_now=()=>performance.now();var getHeapMax=()=>2147483648;var alignMemory=(size,alignment)=>Math.ceil(size/alignment)*alignment;var growMemory=size=>{var oldHeapSize=wasmMemory.buffer.byteLength;var pages=(size-oldHeapSize+65535)/65536|0;try{wasmMemory.grow(pages);updateMemoryViews();return 1}catch(e){}};var _emscripten_resize_heap=requestedSize=>{var oldSize=HEAPU8.length;requestedSize>>>=0;var maxHeapSize=getHeapMax();if(requestedSize>maxHeapSize){return false}for(var cutDown=1;cutDown<=4;cutDown*=2){var overGrownHeapSize=oldSize*(1+.2/cutDown);overGrownHeapSize=Math.min(overGrownHeapSize,requestedSize+100663296);var newSize=Math.min(maxHeapSize,alignMemory(Math.max(requestedSize,overGrownHeapSize),65536));var replacement=growMemory(newSize);if(replacement){return true}}return false};var runtimeKeepaliveCounter=0;var keepRuntimeAlive=()=>noExitRuntime||runtimeKeepaliveCounter>0;var _proc_exit=code=>{EXITSTATUS=code;if(!keepRuntimeAlive()){Module["onExit"]?.(code);ABORT=true}quit_(code,new ExitStatus(code))};var exitJS=(status,implicit)=>{EXITSTATUS=status;_proc_exit(status)};var _exit=exitJS;function _fd_close(fd){try{var stream=SYSCALLS.getStreamFromFD(fd);FS.close(stream);return 0}catch(e){if(typeof FS=="undefined"||!(e.name==="ErrnoError"))throw e;return e.errno}}var doReadv=(stream,iov,iovcnt,offset)=>{var ret=0;for(var i=0;i<iovcnt;i++){var ptr=HEAPU32[iov>>2];var len=HEAPU32[iov+4>>2];iov+=8;var curr=FS.read(stream,HEAP8,ptr,len,offset);if(curr<0)return-1;ret+=curr;if(curr<len)break;if(typeof offset!="undefined"){offset+=curr}}return ret};function _fd_read(fd,iov,iovcnt,pnum){try{var stream=SYSCALLS.getStreamFromFD(fd);var num=doReadv(stream,iov,iovcnt);HEAPU32[pnum>>2]=num;return 0}catch(e){if(typeof FS=="undefined"||!(e.name==="ErrnoError"))throw e;return e.errno}}var INT53_MAX=9007199254740992;var INT53_MIN=-9007199254740992;var bigintToI53Checked=num=>num<INT53_MIN||num>INT53_MAX?NaN:Number(num);function _fd_seek(fd,offset,whence,newOffset){offset=bigintToI53Checked(offset);try{if(isNaN(offset))return 61;var stream=SYSCALLS.getStreamFromFD(fd);FS.llseek(stream,offset,whence);HEAP64[newOffset>>3]=BigInt(stream.position);if(stream.getdents&&offset===0&&whence===0)stream.getdents=null;return 0}catch(e){if(typeof FS=="undefined"||!(e.name==="ErrnoError"))throw e;return e.errno}}var doWritev=(stream,iov,iovcnt,offset)=>{var ret=0;for(var i=0;i<iovcnt;i++){var ptr=HEAPU32[iov>>2];var len=HEAPU32[iov+4>>2];iov+=8;var curr=FS.write(stream,HEAP8,ptr,len,offset);if(curr<0)return-1;ret+=curr;if(curr<len){break}if(typeof offset!="undefined"){offset+=curr}}return ret};function _fd_write(fd,iov,iovcnt,pnum){try{var stream=SYSCALLS.getStreamFromFD(fd);var num=doWritev(stream,iov,iovcnt);HEAPU32[pnum>>2]=num;return 0}catch(e){if(typeof FS=="undefined"||!(e.name==="ErrnoError"))throw e;return e.errno}}var getCFunc=ident=>{var func=Module["_"+ident];return func};var writeArrayToMemory=(array,buffer)=>{HEAP8.set(array,buffer)};var stringToUTF8=(str,outPtr,maxBytesToWrite)=>stringToUTF8Array(str,HEAPU8,outPtr,maxBytesToWrite);var stackAlloc=sz=>__emscripten_stack_alloc(sz);var stringToUTF8OnStack=str=>{var size=lengthBytesUTF8(str)+1;var ret=stackAlloc(size);stringToUTF8(str,ret,size);return ret};var ccall=(ident,returnType,argTypes,args,opts)=>{var toC={string:str=>{var ret=0;if(str!==null&&str!==undefined&&str!==0){ret=stringToUTF8OnStack(str)}return ret},array:arr=>{var ret=stackAlloc(arr.length);writeArrayToMemory(arr,ret);return ret}};function convertReturnValue(ret){if(returnType==="string"){return UTF8ToString(ret)}if(returnType==="boolean")return Boolean(ret);return ret}var func=getCFunc(ident);var cArgs=[];var stack=0;if(args){for(var i=0;i<args.length;i++){var converter=toC[argTypes[i]];if(converter){if(stack===0)stack=stackSave();cArgs[i]=converter(args[i])}else{cArgs[i]=args[i]}}}var ret=func(...cArgs);function onDone(ret){if(stack!==0)stackRestore(stack);return convertReturnValue(ret)}ret=onDone(ret);return ret};var cwrap=(ident,returnType,argTypes,opts)=>{var numericArgs=!argTypes||argTypes.every(type=>type==="number"||type==="boolean");var numericRet=returnType!=="string";if(numericRet&&numericArgs&&!opts){return getCFunc(ident)}return(...args)=>ccall(ident,returnType,argTypes,args,opts)};var FS_createPath=(...args)=>FS.createPath(...args);var FS_unlink=(...args)=>FS.unlink(...args);var FS_createLazyFile=(...args)=>FS.createLazyFile(...args);var FS_createDevice=(...args)=>FS.createDevice(...args);FS.createPreloadedFile=FS_createPreloadedFile;FS.preloadFile=FS_preloadFile;FS.staticInit();{if(Module["noExitRuntime"])noExitRuntime=Module["noExitRuntime"];if(Module["preloadPlugins"])preloadPlugins=Module["preloadPlugins"];if(Module["print"])out=Module["print"];if(Module["printErr"])err=Module["printErr"];if(Module["wasmBinary"])wasmBinary=Module["wasmBinary"];if(Module["arguments"])arguments_=Module["arguments"];if(Module["thisProgram"])thisProgram=Module["thisProgram"];if(Module["preInit"]){if(typeof Module["preInit"]=="function")Module["preInit"]=[Module["preInit"]];while(Module["preInit"].length>0){Module["preInit"].shift()()}}}Module["addRunDependency"]=addRunDependency;Module["removeRunDependency"]=removeRunDependency;Module["ccall"]=ccall;Module["cwrap"]=cwrap;Module["setValue"]=setValue;Module["getValue"]=getValue;Module["FS_preloadFile"]=FS_preloadFile;Module["FS_unlink"]=FS_unlink;Module["FS_createPath"]=FS_createPath;Module["FS_createDevice"]=FS_createDevice;Module["FS_createDataFile"]=FS_createDataFile;Module["FS_createLazyFile"]=FS_createLazyFile;var _free,_malloc,_doomgeneric_Tick,_doomgeneric_Create,_DG_GetFrameBuffer,_DG_PushKeyEvent,__emscripten_stack_restore,__emscripten_stack_alloc,_emscripten_stack_get_current,memory,__indirect_function_table,wasmMemory;function assignWasmExports(wasmExports){_free=Module["_free"]=wasmExports["free"];_malloc=Module["_malloc"]=wasmExports["malloc"];_doomgeneric_Tick=Module["_doomgeneric_Tick"]=wasmExports["doomgeneric_Tick"];_doomgeneric_Create=Module["_doomgeneric_Create"]=wasmExports["doomgeneric_Create"];_DG_GetFrameBuffer=Module["_DG_GetFrameBuffer"]=wasmExports["DG_GetFrameBuffer"];_DG_PushKeyEvent=Module["_DG_PushKeyEvent"]=wasmExports["DG_PushKeyEvent"];__emscripten_stack_restore=wasmExports["_emscripten_stack_restore"];__emscripten_stack_alloc=wasmExports["_emscripten_stack_alloc"];_emscripten_stack_get_current=wasmExports["emscripten_stack_get_current"];memory=wasmMemory=wasmExports["memory"];__indirect_function_table=wasmExports["__indirect_function_table"]}var wasmImports={__syscall_fcntl64:___syscall_fcntl64,__syscall_ioctl:___syscall_ioctl,__syscall_mkdirat:___syscall_mkdirat,__syscall_openat:___syscall_openat,__syscall_renameat:___syscall_renameat,__syscall_rmdir:___syscall_rmdir,__syscall_unlinkat:___syscall_unlinkat,_emscripten_system:__emscripten_system,emscripten_get_now:_emscripten_get_now,emscripten_resize_heap:_emscripten_resize_heap,exit:_exit,fd_close:_fd_close,fd_read:_fd_read,fd_seek:_fd_seek,fd_write:_fd_write};function run(){if(runDependencies>0){dependenciesFulfilled=run;return}preRun();if(runDependencies>0){dependenciesFulfilled=run;return}function doRun(){Module["calledRun"]=true;if(ABORT)return;initRuntime();readyPromiseResolve?.(Module);Module["onRuntimeInitialized"]?.();postRun()}if(Module["setStatus"]){Module["setStatus"]("Running...");setTimeout(()=>{setTimeout(()=>Module["setStatus"](""),1);doRun()},1)}else{doRun()}}var wasmExports;wasmExports=await (createWasm());run();if(runtimeInitialized){moduleRtn=Module}else{moduleRtn=new Promise((resolve,reject)=>{readyPromiseResolve=resolve;readyPromiseReject=reject})}
2
+ ;return moduleRtn}if(typeof exports==="object"&&typeof module==="object"){module.exports=createDoomModule;module.exports.default=createDoomModule}else if(typeof define==="function"&&define["amd"])define([],()=>createDoomModule);
Binary file
@@ -0,0 +1,81 @@
1
+ /**
2
+ * OpenTUI platform implementation for doomgeneric
3
+ *
4
+ * This file implements the 5 required functions for doomgeneric:
5
+ * - DG_Init: Initialize the rendering system
6
+ * - DG_DrawFrame: Called when a frame is ready to be displayed
7
+ * - DG_SleepMs: Sleep for a number of milliseconds
8
+ * - DG_GetTicksMs: Get current time in milliseconds
9
+ * - DG_GetKey: Get keyboard input
10
+ */
11
+
12
+ #include "doomgeneric.h"
13
+ #include "doomkeys.h"
14
+ #include <emscripten.h>
15
+ #include <stdint.h>
16
+ #include <string.h>
17
+
18
+ // NOTE: vanilla_keyboard_mapping is defined in i_input.c
19
+
20
+ // Key event queue
21
+ #define KEY_QUEUE_SIZE 256
22
+ static struct {
23
+ int pressed;
24
+ unsigned char key;
25
+ } key_queue[KEY_QUEUE_SIZE];
26
+ static int key_queue_read = 0;
27
+ static int key_queue_write = 0;
28
+
29
+ // Frame buffer pointer (exposed to JS)
30
+ static uint32_t *frame_buffer = NULL;
31
+
32
+ // Get the framebuffer pointer for JS to read
33
+ EMSCRIPTEN_KEEPALIVE
34
+ uint32_t *DG_GetFrameBuffer(void) { return DG_ScreenBuffer; }
35
+
36
+ // Push a key event from JavaScript
37
+ EMSCRIPTEN_KEEPALIVE
38
+ void DG_PushKeyEvent(int pressed, unsigned char key) {
39
+ int next_write = (key_queue_write + 1) % KEY_QUEUE_SIZE;
40
+ if (next_write != key_queue_read) {
41
+ key_queue[key_queue_write].pressed = pressed;
42
+ key_queue[key_queue_write].key = key;
43
+ key_queue_write = next_write;
44
+ }
45
+ }
46
+
47
+ // NOTE: I_InitInput and I_GetEvent are defined in i_input.c
48
+ // i_input.c's I_GetEvent calls DG_GetKey to read our key queue
49
+ // Do NOT define stubs here as they would override the real implementations!
50
+
51
+ void DG_Init(void) {
52
+ // Initialization done in JavaScript
53
+ }
54
+
55
+ void DG_DrawFrame(void) {
56
+ // The frame is drawn to DG_ScreenBuffer
57
+ // JavaScript will read it via DG_GetFrameBuffer
58
+ // Signal to JS that a frame is ready (handled by tick loop)
59
+ }
60
+
61
+ void DG_SleepMs(uint32_t ms) {
62
+ // No-op in WASM - JavaScript handles timing via game loop
63
+ // Don't use emscripten_sleep as it requires ASYNCIFY
64
+ (void)ms; // Suppress unused warning
65
+ }
66
+
67
+ uint32_t DG_GetTicksMs(void) { return (uint32_t)emscripten_get_now(); }
68
+
69
+ int DG_GetKey(int *pressed, unsigned char *key) {
70
+ if (key_queue_read != key_queue_write) {
71
+ *pressed = key_queue[key_queue_read].pressed;
72
+ *key = key_queue[key_queue_read].key;
73
+ key_queue_read = (key_queue_read + 1) % KEY_QUEUE_SIZE;
74
+ return 1;
75
+ }
76
+ return 0;
77
+ }
78
+
79
+ void DG_SetWindowTitle(const char *title) {
80
+ // Could emit to JS if needed
81
+ }
package/package.json ADDED
@@ -0,0 +1,59 @@
1
+ {
2
+ "name": "@muhammedaksam/opentui-doom",
3
+ "version": "0.1.0",
4
+ "description": "Play DOOM in your terminal using OpenTUI's framebuffer rendering and doomgeneric WASM",
5
+ "module": "src/index.ts",
6
+ "type": "module",
7
+ "bin": {
8
+ "opentui-doom": "./src/index.ts"
9
+ },
10
+ "files": [
11
+ "src",
12
+ "doom/build",
13
+ "doom/doomgeneric_opentui.c",
14
+ "scripts"
15
+ ],
16
+ "author": "Muhammed Mustafa AKŞAM <info@muhammedaksam.com.tr> (https://github.com/muhammedaksam)",
17
+ "license": "MIT",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/muhammedaksam/opentui-doom.git"
21
+ },
22
+ "bugs": {
23
+ "url": "https://github.com/muhammedaksam/opentui-doom/issues"
24
+ },
25
+ "homepage": "https://github.com/muhammedaksam/opentui-doom#readme",
26
+ "keywords": [
27
+ "doom",
28
+ "terminal",
29
+ "tui",
30
+ "opentui",
31
+ "wasm",
32
+ "webassembly",
33
+ "game",
34
+ "retro",
35
+ "cli",
36
+ "console",
37
+ "framebuffer",
38
+ "doomgeneric"
39
+ ],
40
+ "scripts": {
41
+ "dev": "bun run --watch src/index.ts",
42
+ "build:doom": "bash ./scripts/build-doom.sh",
43
+ "start": "bun run src/index.ts",
44
+ "prepublishOnly": "bun run build:doom"
45
+ },
46
+ "devDependencies": {
47
+ "@types/bun": "latest"
48
+ },
49
+ "peerDependencies": {
50
+ "typescript": "^5"
51
+ },
52
+ "dependencies": {
53
+ "@opentui/core": "^0.1.57"
54
+ },
55
+ "engines": {
56
+ "node": ">=18",
57
+ "bun": ">=1.0"
58
+ }
59
+ }
@@ -0,0 +1,145 @@
1
+ #!/bin/bash
2
+ # Build DOOM for OpenTUI using doomgeneric and Emscripten
3
+
4
+ set -e
5
+
6
+ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
7
+ PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
8
+ DOOM_DIR="$PROJECT_ROOT/doom"
9
+ BUILD_DIR="$PROJECT_ROOT/doom/build"
10
+
11
+ echo "=== DOOM OpenTUI Build Script ==="
12
+
13
+ # Check for emcc
14
+ if ! command -v emcc &> /dev/null; then
15
+ echo "Error: Emscripten (emcc) not found!"
16
+ echo "Please install Emscripten SDK from https://emscripten.org/docs/getting_started/downloads.html"
17
+ echo ""
18
+ echo "Quick install:"
19
+ echo " git clone https://github.com/emscripten-core/emsdk.git"
20
+ echo " cd emsdk"
21
+ echo " ./emsdk install latest"
22
+ echo " ./emsdk activate latest"
23
+ echo " source ./emsdk_env.sh"
24
+ exit 1
25
+ fi
26
+
27
+ # Clone doomgeneric if not present
28
+ if [ ! -d "$DOOM_DIR/doomgeneric" ]; then
29
+ echo "Cloning doomgeneric..."
30
+ cd "$DOOM_DIR"
31
+ git clone https://github.com/ozkl/doomgeneric.git
32
+ fi
33
+
34
+ # Create build directory
35
+ mkdir -p "$BUILD_DIR"
36
+
37
+ # Copy our platform file
38
+ cp "$DOOM_DIR/doomgeneric_opentui.c" "$DOOM_DIR/doomgeneric/doomgeneric/"
39
+
40
+ echo "Compiling DOOM to WebAssembly..."
41
+ cd "$DOOM_DIR/doomgeneric/doomgeneric"
42
+
43
+ # Compile with Emscripten
44
+ emcc -O2 \
45
+ -s WASM=1 \
46
+ -s EXPORTED_FUNCTIONS="['_doomgeneric_Create','_doomgeneric_Tick','_DG_GetFrameBuffer','_DG_PushKeyEvent','_malloc','_free']" \
47
+ -s EXPORTED_RUNTIME_METHODS="['ccall','cwrap','getValue','setValue']" \
48
+ -s ALLOW_MEMORY_GROWTH=1 \
49
+ -s INITIAL_MEMORY=33554432 \
50
+ -s MODULARIZE=1 \
51
+ -s EXPORT_NAME="createDoomModule" \
52
+ -s ENVIRONMENT='node' \
53
+ -s FILESYSTEM=1 \
54
+ -s FORCE_FILESYSTEM=1 \
55
+ -s EXIT_RUNTIME=0 \
56
+ -s NO_EXIT_RUNTIME=1 \
57
+ -DDOOMGENERIC_RESX=1280 \
58
+ -DDOOMGENERIC_RESY=800 \
59
+ -I. \
60
+ am_map.c \
61
+ d_event.c \
62
+ d_items.c \
63
+ d_iwad.c \
64
+ d_loop.c \
65
+ d_main.c \
66
+ d_mode.c \
67
+ d_net.c \
68
+ doomdef.c \
69
+ doomgeneric.c \
70
+ doomgeneric_opentui.c \
71
+ doomstat.c \
72
+ dstrings.c \
73
+ f_finale.c \
74
+ f_wipe.c \
75
+ g_game.c \
76
+ hu_lib.c \
77
+ hu_stuff.c \
78
+ i_cdmus.c \
79
+ i_input.c \
80
+ i_endoom.c \
81
+ i_joystick.c \
82
+ i_scale.c \
83
+ i_sound.c \
84
+ i_system.c \
85
+ i_timer.c \
86
+ i_video.c \
87
+ icon.c \
88
+ info.c \
89
+ m_argv.c \
90
+ m_bbox.c \
91
+ m_cheat.c \
92
+ m_config.c \
93
+ m_controls.c \
94
+ m_fixed.c \
95
+ m_menu.c \
96
+ m_misc.c \
97
+ m_random.c \
98
+ memio.c \
99
+ p_ceilng.c \
100
+ p_doors.c \
101
+ p_enemy.c \
102
+ p_floor.c \
103
+ p_inter.c \
104
+ p_lights.c \
105
+ p_map.c \
106
+ p_maputl.c \
107
+ p_mobj.c \
108
+ p_plats.c \
109
+ p_pspr.c \
110
+ p_saveg.c \
111
+ p_setup.c \
112
+ p_sight.c \
113
+ p_spec.c \
114
+ p_switch.c \
115
+ p_telept.c \
116
+ p_tick.c \
117
+ p_user.c \
118
+ r_bsp.c \
119
+ r_data.c \
120
+ r_draw.c \
121
+ r_main.c \
122
+ r_plane.c \
123
+ r_segs.c \
124
+ r_sky.c \
125
+ r_things.c \
126
+ s_sound.c \
127
+ sha1.c \
128
+ sounds.c \
129
+ st_lib.c \
130
+ st_stuff.c \
131
+ statdump.c \
132
+ tables.c \
133
+ v_video.c \
134
+ w_checksum.c \
135
+ w_file.c \
136
+ w_file_stdc.c \
137
+ w_main.c \
138
+ w_wad.c \
139
+ wi_stuff.c \
140
+ z_zone.c \
141
+ dummy.c \
142
+ -o "$BUILD_DIR/doom.js"
143
+
144
+ echo "Build complete!"
145
+ echo "Output: $BUILD_DIR/doom.js and $BUILD_DIR/doom.wasm"
@@ -0,0 +1,192 @@
1
+ /**
2
+ * DOOM Engine - WebAssembly wrapper for doomgeneric
3
+ *
4
+ * Handles loading and running the DOOM WASM module,
5
+ * providing a TypeScript interface for the game.
6
+ */
7
+
8
+ import { readFile } from "fs/promises";
9
+ import { join, resolve } from "path";
10
+
11
+ // DOOM screen dimensions
12
+ export const DOOM_WIDTH = 1280;
13
+ export const DOOM_HEIGHT = 800;
14
+
15
+ export interface DoomModule {
16
+ _doomgeneric_Create: (argc: number, argv: number) => void;
17
+ _doomgeneric_Tick: () => void;
18
+ _DG_GetFrameBuffer: () => number;
19
+ _DG_PushKeyEvent: (pressed: number, key: number) => void;
20
+ _malloc: (size: number) => number;
21
+ _free: (ptr: number) => void;
22
+ HEAPU8: Uint8Array;
23
+ HEAPU32: Uint32Array;
24
+ FS_createDataFile: (
25
+ parent: string,
26
+ name: string,
27
+ data: number[],
28
+ canRead: boolean,
29
+ canWrite: boolean,
30
+ canOwn?: boolean
31
+ ) => void;
32
+ FS_createPath: (
33
+ parent: string,
34
+ path: string,
35
+ canRead: boolean,
36
+ canWrite: boolean
37
+ ) => string;
38
+ ccall: (name: string, returnType: string | null, argTypes: string[], args: any[]) => any;
39
+ cwrap: (name: string, returnType: string | null, argTypes: string[]) => (...args: any[]) => any;
40
+ setValue: (ptr: number, value: number, type: string) => void;
41
+ getValue: (ptr: number, type: string) => number;
42
+ }
43
+
44
+ export class DoomEngine {
45
+ private module: DoomModule | null = null;
46
+ private frameBufferPtr: number = 0;
47
+ private initialized: boolean = false;
48
+ private wadPath: string;
49
+
50
+ constructor(wadPath: string) {
51
+ // Resolve to absolute path
52
+ this.wadPath = resolve(wadPath);
53
+ }
54
+
55
+ async init(): Promise<void> {
56
+ // Load the WASM module
57
+ const buildDir = join(import.meta.dir, "..", "doom", "build");
58
+ const doomJsPath = join(buildDir, "doom.js");
59
+
60
+ // Read WAD file first
61
+ const wadData = await readFile(this.wadPath);
62
+ const wadArray = Array.from(new Uint8Array(wadData));
63
+
64
+ // Dynamic import of the compiled DOOM module
65
+ const createDoomModule = require(doomJsPath);
66
+
67
+ // Create module with proper callbacks
68
+ const moduleConfig: any = {
69
+ locateFile: (path: string) => {
70
+ if (path.endsWith('.wasm')) {
71
+ return join(buildDir, path);
72
+ }
73
+ return path;
74
+ },
75
+ print: (text: string) => console.log('[DOOM]', text),
76
+ printErr: (text: string) => console.error('[DOOM]', text),
77
+
78
+ // preRun receives Module as first argument
79
+ preRun: [
80
+ function (module: DoomModule) {
81
+ // Create /doom directory
82
+ module.FS_createPath("/", "doom", true, true);
83
+ // Write WAD file to virtual filesystem
84
+ module.FS_createDataFile("/doom", "doom1.wad", wadArray, true, false);
85
+ }
86
+ ],
87
+ };
88
+
89
+ this.module = await createDoomModule(moduleConfig);
90
+
91
+ if (!this.module) {
92
+ throw new Error("Failed to initialize DOOM module");
93
+ }
94
+
95
+ // Initialize DOOM
96
+ this.initDoom();
97
+
98
+ // Get framebuffer pointer
99
+ this.frameBufferPtr = this.module._DG_GetFrameBuffer();
100
+ this.initialized = true;
101
+ }
102
+
103
+ private initDoom(): void {
104
+ if (!this.module) return;
105
+
106
+ const module = this.module;
107
+
108
+ const args = [
109
+ "doom",
110
+ "-iwad",
111
+ "/doom/doom1.wad"
112
+ ];
113
+
114
+ // Allocate memory for argv using ccall for strings
115
+ const argPtrs: number[] = [];
116
+ for (const arg of args) {
117
+ // Allocate space for string + null terminator
118
+ const ptr = module._malloc(arg.length + 1);
119
+ // Use setValue to write each character
120
+ for (let i = 0; i < arg.length; i++) {
121
+ module.setValue(ptr + i, arg.charCodeAt(i), 'i8');
122
+ }
123
+ // Null terminate
124
+ module.setValue(ptr + arg.length, 0, 'i8');
125
+ argPtrs.push(ptr);
126
+ }
127
+
128
+ // Create argv array
129
+ const argvPtr = module._malloc(argPtrs.length * 4);
130
+ for (let i = 0; i < argPtrs.length; i++) {
131
+ const ptr = argPtrs[i];
132
+ if (ptr !== undefined) {
133
+ module.setValue(argvPtr + i * 4, ptr, 'i32');
134
+ }
135
+ }
136
+
137
+ // Call doomgeneric_Create
138
+ module._doomgeneric_Create(args.length, argvPtr);
139
+
140
+ // Free argv (DOOM copies the strings)
141
+ for (const ptr of argPtrs) {
142
+ module._free(ptr);
143
+ }
144
+ module._free(argvPtr);
145
+ }
146
+
147
+ /**
148
+ * Run one game tick - called each frame
149
+ */
150
+ tick(): void {
151
+ if (!this.module || !this.initialized) return;
152
+ this.module._doomgeneric_Tick();
153
+ }
154
+
155
+ /**
156
+ * Get the current frame as RGBA pixel data
157
+ * DOOM uses ARGB format, so we need to convert
158
+ */
159
+ getFrameBuffer(): Uint8Array {
160
+ if (!this.module || !this.initialized) {
161
+ return new Uint8Array(DOOM_WIDTH * DOOM_HEIGHT * 4);
162
+ }
163
+
164
+ const pixels = DOOM_WIDTH * DOOM_HEIGHT;
165
+ const buffer = new Uint8Array(pixels * 4);
166
+ const module = this.module;
167
+
168
+ // Read ARGB data from DOOM's framebuffer using getValue
169
+ for (let i = 0; i < pixels; i++) {
170
+ const argb = module.getValue(this.frameBufferPtr + i * 4, 'i32');
171
+ const offset = i * 4;
172
+ buffer[offset + 0] = (argb >> 16) & 0xFF; // R
173
+ buffer[offset + 1] = (argb >> 8) & 0xFF; // G
174
+ buffer[offset + 2] = argb & 0xFF; // B
175
+ buffer[offset + 3] = 255; // A (always opaque)
176
+ }
177
+
178
+ return buffer;
179
+ }
180
+
181
+ /**
182
+ * Push a key event to DOOM
183
+ */
184
+ pushKey(pressed: boolean, key: number): void {
185
+ if (!this.module || !this.initialized) return;
186
+ this.module._DG_PushKeyEvent(pressed ? 1 : 0, key);
187
+ }
188
+
189
+ isInitialized(): boolean {
190
+ return this.initialized;
191
+ }
192
+ }
@@ -0,0 +1,180 @@
1
+ /**
2
+ * DOOM Input Handler
3
+ *
4
+ * Maps OpenTUI keyboard events to DOOM key codes
5
+ */
6
+
7
+ import type { KeyEvent } from "@opentui/core";
8
+ import type { DoomEngine } from "./doom-engine";
9
+
10
+ // DOOM key codes (from doomkeys.h)
11
+ export const DoomKeys = {
12
+ KEY_RIGHTARROW: 0xae,
13
+ KEY_LEFTARROW: 0xac,
14
+ KEY_UPARROW: 0xad,
15
+ KEY_DOWNARROW: 0xaf,
16
+ KEY_STRAFE_L: 0xa0,
17
+ KEY_STRAFE_R: 0xa1,
18
+ KEY_USE: 0xa2,
19
+ KEY_FIRE: 0xa3,
20
+ KEY_ESCAPE: 27,
21
+ KEY_ENTER: 13,
22
+ KEY_TAB: 9,
23
+ KEY_F1: 0x80 + 0x3b,
24
+ KEY_F2: 0x80 + 0x3c,
25
+ KEY_F3: 0x80 + 0x3d,
26
+ KEY_F4: 0x80 + 0x3e,
27
+ KEY_F5: 0x80 + 0x3f,
28
+ KEY_F6: 0x80 + 0x40,
29
+ KEY_F7: 0x80 + 0x41,
30
+ KEY_F8: 0x80 + 0x42,
31
+ KEY_F9: 0x80 + 0x43,
32
+ KEY_F10: 0x80 + 0x44,
33
+ KEY_F11: 0x80 + 0x57,
34
+ KEY_F12: 0x80 + 0x58,
35
+ KEY_BACKSPACE: 127,
36
+ KEY_PAUSE: 0xff,
37
+ KEY_EQUALS: 0x3d,
38
+ KEY_MINUS: 0x2d,
39
+ KEY_RSHIFT: 0x80 + 0x36,
40
+ KEY_RCTRL: 0x80 + 0x1d,
41
+ KEY_RALT: 0x80 + 0x38,
42
+ KEY_LALT: 0x80 + 0x38,
43
+ KEY_CAPSLOCK: 0x80 + 0x3a,
44
+ KEY_NUMLOCK: 0x80 + 0x45,
45
+ KEY_SCRLCK: 0x80 + 0x46,
46
+ KEY_PRTSCR: 0x80 + 0x59,
47
+ KEY_HOME: 0x80 + 0x47,
48
+ KEY_END: 0x80 + 0x4f,
49
+ KEY_PGUP: 0x80 + 0x49,
50
+ KEY_PGDN: 0x80 + 0x51,
51
+ KEY_INS: 0x80 + 0x52,
52
+ KEY_DEL: 0x80 + 0x53,
53
+ } as const;
54
+
55
+ // Key state tracking for press/release
56
+ const keyStates = new Map<string, boolean>();
57
+
58
+ /**
59
+ * Map an OpenTUI key event to a DOOM key code
60
+ */
61
+ function mapKeyToDoom(key: KeyEvent): number | null {
62
+ const name = key.name?.toLowerCase() ?? "";
63
+
64
+ // Arrow keys
65
+ if (name === "up" || key.sequence === "\x1b[A") return DoomKeys.KEY_UPARROW;
66
+ if (name === "down" || key.sequence === "\x1b[B") return DoomKeys.KEY_DOWNARROW;
67
+ if (name === "left" || key.sequence === "\x1b[D") return DoomKeys.KEY_LEFTARROW;
68
+ if (name === "right" || key.sequence === "\x1b[C") return DoomKeys.KEY_RIGHTARROW;
69
+
70
+ // WASD movement (alternative to arrows)
71
+ if (name === "w") return DoomKeys.KEY_UPARROW;
72
+ if (name === "s") return DoomKeys.KEY_DOWNARROW;
73
+ if (name === "a") return DoomKeys.KEY_STRAFE_L;
74
+ if (name === "d") return DoomKeys.KEY_STRAFE_R;
75
+
76
+ // Action keys
77
+ if (name === "space") return " ".charCodeAt(0); // Use
78
+ if (name === "return" || name === "enter") return DoomKeys.KEY_ENTER;
79
+ if (name === "escape") return DoomKeys.KEY_ESCAPE;
80
+ if (name === "tab") return DoomKeys.KEY_TAB;
81
+ if (name === "backspace") return DoomKeys.KEY_BACKSPACE;
82
+
83
+ // Fire (Ctrl)
84
+ if (key.ctrl) return DoomKeys.KEY_FIRE;
85
+
86
+ // Alt for strafe
87
+ if (key.meta || key.name === "alt") return DoomKeys.KEY_LALT;
88
+
89
+ // Shift for run
90
+ if (key.shift) return DoomKeys.KEY_RSHIFT;
91
+
92
+ // Function keys
93
+ if (name === "f1") return DoomKeys.KEY_F1;
94
+ if (name === "f2") return DoomKeys.KEY_F2;
95
+ if (name === "f3") return DoomKeys.KEY_F3;
96
+ if (name === "f4") return DoomKeys.KEY_F4;
97
+ if (name === "f5") return DoomKeys.KEY_F5;
98
+ if (name === "f6") return DoomKeys.KEY_F6;
99
+ if (name === "f7") return DoomKeys.KEY_F7;
100
+ if (name === "f8") return DoomKeys.KEY_F8;
101
+ if (name === "f9") return DoomKeys.KEY_F9;
102
+ if (name === "f10") return DoomKeys.KEY_F10;
103
+ if (name === "f11") return DoomKeys.KEY_F11;
104
+ if (name === "f12") return DoomKeys.KEY_F12;
105
+
106
+ // Weapon selection (1-9, 0)
107
+ if (name >= "0" && name <= "9") return name.charCodeAt(0);
108
+
109
+ // Plus/minus for gamma/zoom
110
+ if (name === "+" || name === "=") return DoomKeys.KEY_EQUALS;
111
+ if (name === "-") return DoomKeys.KEY_MINUS;
112
+
113
+ // Y/N for prompts
114
+ if (name === "y") return "y".charCodeAt(0);
115
+ if (name === "n") return "n".charCodeAt(0);
116
+
117
+ // Other letter keys (for cheats, etc)
118
+ if (name.length === 1 && name >= "a" && name <= "z") {
119
+ return name.charCodeAt(0);
120
+ }
121
+
122
+ return null;
123
+ }
124
+
125
+ /**
126
+ * Create an input handler that forwards OpenTUI key events to DOOM
127
+ */
128
+ // Track release timers for each key
129
+ const keyTimers = new Map<string, ReturnType<typeof setTimeout>>();
130
+
131
+ export function createDoomInputHandler(engine: DoomEngine) {
132
+ return (key: KeyEvent) => {
133
+ const doomKey = mapKeyToDoom(key);
134
+
135
+ if (doomKey === null) return;
136
+
137
+ const keyId = key.name || key.sequence || "";
138
+ const wasPressed = keyStates.get(keyId) ?? false;
139
+
140
+ // Clear any existing release timer for this key
141
+ const existingTimer = keyTimers.get(keyId);
142
+ if (existingTimer) {
143
+ clearTimeout(existingTimer);
144
+ keyTimers.delete(keyId);
145
+ }
146
+
147
+ // Key press - only send if not already pressed
148
+ if (!wasPressed) {
149
+ keyStates.set(keyId, true);
150
+ engine.pushKey(true, doomKey);
151
+ }
152
+
153
+ // Schedule key release after 300ms of no input
154
+ const timer = setTimeout(() => {
155
+ if (keyStates.get(keyId)) {
156
+ keyStates.set(keyId, false);
157
+ engine.pushKey(false, doomKey);
158
+ keyTimers.delete(keyId);
159
+ }
160
+ }, 300);
161
+ keyTimers.set(keyId, timer);
162
+ };
163
+ }
164
+
165
+ /**
166
+ * Get help text for controls
167
+ */
168
+ export function getControlsHelp(): string {
169
+ return [
170
+ "Controls:",
171
+ " Movement: Arrow Keys or WASD",
172
+ " Fire: Ctrl",
173
+ " Use/Open: Space",
174
+ " Run: Shift",
175
+ " Strafe: A/D or Alt+Arrows",
176
+ " Weapons: 1-7",
177
+ " Menu: Escape",
178
+ " Map: Tab",
179
+ ].join("\n");
180
+ }
package/src/index.ts ADDED
@@ -0,0 +1,205 @@
1
+ #!/usr/bin/env bun
2
+ /**
3
+ * DOOM for OpenTUI
4
+ *
5
+ * Plays DOOM in your terminal using OpenTUI's framebuffer rendering.
6
+ *
7
+ * Usage: bun run dev -- --wad /path/to/doom1.wad
8
+ */
9
+
10
+ import {
11
+ createCliRenderer,
12
+ FrameBufferRenderable,
13
+ TextRenderable,
14
+ BoxRenderable,
15
+ RGBA,
16
+ TextAttributes,
17
+ } from "@opentui/core";
18
+ import { DoomEngine, DOOM_WIDTH, DOOM_HEIGHT } from "./doom-engine";
19
+ import { createDoomInputHandler, getControlsHelp } from "./doom-input";
20
+ import { parseArgs } from "util";
21
+
22
+ // Parse command line arguments
23
+ const { values } = parseArgs({
24
+ args: Bun.argv.slice(2),
25
+ options: {
26
+ wad: {
27
+ type: "string",
28
+ short: "w",
29
+ default: "doom1.wad",
30
+ },
31
+ help: {
32
+ type: "boolean",
33
+ short: "h",
34
+ default: false,
35
+ },
36
+ },
37
+ });
38
+
39
+ if (values.help) {
40
+ console.log(`
41
+ DOOM for OpenTUI
42
+
43
+ Usage: bun run dev -- --wad <path-to-wad-file>
44
+
45
+ Options:
46
+ -w, --wad Path to DOOM WAD file (default: doom1.wad)
47
+ -h, --help Show this help message
48
+
49
+ ${getControlsHelp()}
50
+ `);
51
+ process.exit(0);
52
+ }
53
+
54
+ // Initialize renderer
55
+ const renderer = await createCliRenderer({
56
+ exitOnCtrlC: true,
57
+ targetFps: 35, // DOOM's native framerate
58
+ });
59
+
60
+ renderer.start();
61
+
62
+ // Create UI container
63
+ const container = new BoxRenderable(renderer, {
64
+ id: "doom-container",
65
+ flexGrow: 1,
66
+ justifyContent: "center",
67
+ alignItems: "center",
68
+ });
69
+ renderer.root.add(container);
70
+
71
+ // Loading text
72
+ const loadingText = new TextRenderable(renderer, {
73
+ id: "loading",
74
+ content: "Loading DOOM...",
75
+ fg: RGBA.fromInts(255, 255, 100),
76
+ attributes: TextAttributes.BOLD,
77
+ });
78
+ container.add(loadingText);
79
+
80
+ // Try to initialize DOOM engine
81
+ let doomEngine: DoomEngine | null = null;
82
+ let framebufferRenderable: FrameBufferRenderable | null = null;
83
+
84
+ async function initDoom() {
85
+ try {
86
+ loadingText.content = `Loading DOOM from: ${values.wad}`;
87
+
88
+ doomEngine = new DoomEngine(values.wad!);
89
+ await doomEngine.init();
90
+
91
+ // Remove loading text
92
+ container.remove("loading");
93
+
94
+ // Create framebuffer for DOOM rendering
95
+ framebufferRenderable = new FrameBufferRenderable(renderer, {
96
+ id: "doom-screen",
97
+ width: renderer.terminalWidth,
98
+ height: renderer.terminalHeight,
99
+ position: "absolute",
100
+ left: 0,
101
+ top: 0,
102
+ zIndex: 0,
103
+ });
104
+ renderer.root.add(framebufferRenderable);
105
+
106
+ // Add controls overlay
107
+ const controlsText = new TextRenderable(renderer, {
108
+ id: "controls",
109
+ content: "DOOM | Ctrl+C to exit | Arrow/WASD=Move Space=Use Ctrl=Fire",
110
+ position: "absolute",
111
+ left: 1,
112
+ top: 0,
113
+ fg: RGBA.fromInts(200, 200, 200),
114
+ attributes: TextAttributes.DIM,
115
+ zIndex: 100,
116
+ });
117
+ renderer.root.add(controlsText);
118
+
119
+ // Set up input handler
120
+ const inputHandler = createDoomInputHandler(doomEngine);
121
+ renderer.keyInput.on("keypress", inputHandler);
122
+
123
+ // Start game loop
124
+ renderer.setFrameCallback(gameLoop);
125
+
126
+ } catch (error) {
127
+ loadingText.content = `Error: ${error}`;
128
+ loadingText.fg = RGBA.fromInts(255, 100, 100);
129
+ console.error("Failed to initialize DOOM:", error);
130
+
131
+ // Show troubleshooting info
132
+ const helpText = new TextRenderable(renderer, {
133
+ id: "help",
134
+ content: [
135
+ "",
136
+ "Troubleshooting:",
137
+ "1. Make sure you have built the DOOM WASM module:",
138
+ " ./scripts/build-doom.sh",
139
+ "",
140
+ "2. Make sure you have a valid WAD file:",
141
+ " bun run dev -- --wad /path/to/doom1.wad",
142
+ "",
143
+ "3. Download shareware WAD from:",
144
+ " https://distro.ibiblio.org/slitaz/sources/packages/d/doom-wad/",
145
+ ].join("\n"),
146
+ position: "absolute",
147
+ left: 2,
148
+ top: 3,
149
+ fg: RGBA.fromInts(180, 180, 180),
150
+ });
151
+ container.add(helpText);
152
+ }
153
+ }
154
+
155
+ async function gameLoop(deltaMs: number) {
156
+ if (!doomEngine || !framebufferRenderable) return;
157
+
158
+ // Run DOOM tick
159
+ doomEngine.tick();
160
+
161
+ // Get framebuffer from DOOM
162
+ const pixels = doomEngine.getFrameBuffer();
163
+ const fb = framebufferRenderable.frameBuffer;
164
+
165
+ // With half-block rendering, each terminal cell represents 2 vertical pixels
166
+ // So we need to scale DOOM to (fb.width * 1) x (fb.height * 2) source pixels
167
+ const scaleX = DOOM_WIDTH / fb.width;
168
+ const scaleY = DOOM_HEIGHT / (fb.height * 2); // *2 because each cell = 2 vertical pixels
169
+
170
+ // Render to OpenTUI framebuffer using half-block characters
171
+ // The upper half-block character (▀) uses foreground for top pixel, background for bottom
172
+ for (let y = 0; y < fb.height; y++) {
173
+ const srcY1 = Math.floor(y * 2 * scaleY); // Top pixel row
174
+ const srcY2 = Math.floor((y * 2 + 1) * scaleY); // Bottom pixel row
175
+
176
+ for (let x = 0; x < fb.width; x++) {
177
+ const srcX = Math.floor(x * scaleX);
178
+
179
+ // Top pixel (foreground)
180
+ const srcIdx1 = (srcY1 * DOOM_WIDTH + srcX) * 4;
181
+ const r1 = pixels[srcIdx1] ?? 0;
182
+ const g1 = pixels[srcIdx1 + 1] ?? 0;
183
+ const b1 = pixels[srcIdx1 + 2] ?? 0;
184
+
185
+ // Bottom pixel (background)
186
+ const srcIdx2 = (srcY2 * DOOM_WIDTH + srcX) * 4;
187
+ const r2 = pixels[srcIdx2] ?? 0;
188
+ const g2 = pixels[srcIdx2 + 1] ?? 0;
189
+ const b2 = pixels[srcIdx2 + 2] ?? 0;
190
+
191
+ // Use upper half-block: ▀ (foreground = top, background = bottom)
192
+ fb.setCell(x, y, "▀", RGBA.fromInts(r1, g1, b1), RGBA.fromInts(r2, g2, b2));
193
+ }
194
+ }
195
+ }
196
+
197
+ // Handle resize
198
+ renderer.on("resize", (width, height) => {
199
+ if (framebufferRenderable) {
200
+ framebufferRenderable.frameBuffer.resize(width, height);
201
+ }
202
+ });
203
+
204
+ // Initialize
205
+ initDoom();