Compare commits

...

10 Commits

Author SHA1 Message Date
paradust7 801f59d5b6 Clarify buttons 2023-08-26 14:30:29 +00:00
paradust7 8a495e165d Add multiplayer to sample index.html 2023-08-24 16:25:16 +00:00
paradust7 cb985f0d51 Pull in merge of minetest & irrlicht upstream 2023-01-26 12:39:32 -08:00
paradust7 39df77c270 Fix Dockerfile 2022-12-27 21:00:27 +00:00
paradust7 79d01a47b6 Use local emsdk so that emsdk version can be pinned 2022-12-27 20:44:27 +00:00
paradust7 e8f754e1c5 Update emsdk patch 2022-11-11 03:04:05 +00:00
paradust7 3c6ebfeb95 Custom minetest.conf method. A few other fixes 2022-11-11 01:03:33 +00:00
paradust7 c9ad7ed687 Build file system the right way, using 'make install' and the minetest_game repo 2022-11-09 00:58:09 +00:00
paradust7 1c5c6db362 Update irrlicht pin 2022-11-09 00:15:11 +00:00
paradust7 fd9628c533 Pull in merge with minetest & irrlichtmt upstream 2022-11-07 12:51:55 -08:00
15 changed files with 319 additions and 77 deletions

2
.gitignore vendored
View File

@ -11,3 +11,5 @@ sources/zstd
sources/webshims
sources/minetest
sources/irrlichtmt
sources/minetest_game
emsdk/

View File

@ -15,20 +15,9 @@ RUN \
COPY . /minetest-wasm
# Install emsdk
RUN \
cd "$HOME" \
&& echo "Building from $(pwd)" \
&& git clone --depth 1 https://github.com/emscripten-core/emsdk.git \
&& cd emsdk \
&& ./emsdk install latest \
&& ./emsdk activate latest
# Build minetest-wasm
RUN \
cd "$HOME"/emsdk \
&& . ./emsdk_env.sh \
&& cd /minetest-wasm \
&& ./apply_patches.sh \
cd /minetest-wasm \
&& ls -la \
&& ./install_emsdk.sh \
&& ./build_all.sh

View File

@ -10,14 +10,6 @@ This has only been tested on Ubuntu 20.04.
* Ubuntu: apt-get install -y build-essential cmake tclsh
Pre-requisites
--------------
The Emscripten SDK (emsdk) must be installed, activated, and in the PATH.
It is assumed to be installed in $HOME/emsdk (edit `common.sh` to change this).
The emsdk directory must be patched exactly once by running:
./apply_patches.sh
Building
---------
@ -31,3 +23,29 @@ If the build completes successfully, the www/ directory will contain the entire
includes an `.htaccess` file which sets headers that are required (by browsers) to load the app.
If your webserver does not recognize `.htaccess` files, you may need to set the headers in
another way.
Network Play
------------
By default, the proxy server is set to `wss://minetest.dustlabs.io/proxy` (see static/launcher.js).
This is necessary for network play, since websites cannot open normal TCP/UDP sockets. This proxy
is located in California. There are regional proxies which may perform better depending on your
location:
North America (Dallas) - wss://na1.dustlabs.io/mtproxy
South America (Sao Paulo) - wss://sa1.dustlabs.io/mtproxy
Europe (Frankfurt) - wss://eu1.dustlabs.io/mtproxy
Asia (Singapore) - wss://ap1.dustlabs.io/mtproxy
Australia (Melbourne) - wss://ap2.dustlabs.io/mtproxy
You could also roll your own own custom proxy server. The client code is here:
https://github.com/paradust7/webshims/blob/main/src/emsocket/proxy.js
Custom Emscripten
-----------------
The Emscripten SDK (emsdk) will be downloaded and installed the first time you build. To provide
your own instead, set $EMSDK before building (e.g. using `emsdk_env.sh`). An external Emscripten
may need to be patched by running this exactly once:
./apply_patches.sh /path/to/emsdk

View File

@ -1,7 +1,13 @@
#!/bin/bash -eu
source common.sh
BASE_DIR="$(dirname -- "$(readlink -f -- "$0")")"
if [ $# -ne 1 ]; then
echo "Usage: $0 /path/to/emsdk"
exit 1
fi
EMSDK_ROOT="$1"
cd "$EMSDK_ROOT"
patch -p1 < "$BASE_DIR/emsdk_emcc.patch"

View File

@ -26,8 +26,10 @@ emcc --clear-cache --clear-ports
./build_curl.sh # uses webshims, openssl, zlib
# Minetest
./build_fsroot.sh
./build_minetest.sh
# Virtual file system
./build_fsroot.sh
# Finished product
./build_www.sh

View File

@ -8,43 +8,43 @@
source common.sh
# TODO: Use `make package` for this instead.
MTDIR="minetest-5.5.1-win64"
unpack_source $MTDIR
pushd "$BUILD_DIR"
rm -rf fsroot
mkdir fsroot
cp -a "minetest-install" fsroot/minetest
# Copy root certificates for OpenSSL
mkdir -p fsroot/etc/ssl/certs
# ca-certificates.crt may be a symlink
cat /etc/ssl/certs/ca-certificates.crt > fsroot/etc/ssl/certs/ca-certificates.crt
mv $MTDIR fsroot/minetest
#############################################
pushd fsroot/minetest
# Don't need the Windows exe/dlls
rm -rf bin
rm -rf bin unix
# Emscripten strips empty directories. But bin/ needs to be present so that
# realpath() works on relative paths starting with bin/../
mkdir bin
echo "This is here to ensure bin exists" > bin/readme.txt
# Replace these directories with the ones from the source directory
for I in client builtin clientmods games/devtest textures; do
rm -rf "$I"
cp -r "$MINETEST_REPO/$I" "$I"
done
# Copy the irrlicht shaders
cp -r "$IRRLICHT_REPO/media/Shaders" client/shaders/Irrlicht
rm -rf games/minetest_game
mkdir -p games
cp -a "$SOURCES_DIR"/minetest_game games
cd games/minetest_game
rm -rf ".git" ".github"
popd
#############################################
# Copy root certificates for OpenSSL
pushd fsroot
mkdir -p etc/ssl/certs
# May be a symlink, use cat to copy contents
cat /etc/ssl/certs/ca-certificates.crt > etc/ssl/certs/ca-certificates.crt
popd
# Make fsroot.tar
rm -f fsroot.tar
pushd fsroot

View File

@ -57,8 +57,10 @@ if ! $INCREMENTAL; then
-DEGL_INCLUDE_DIR="$DUMMY_INCLUDE_DIR" \
-DCURL_LIBRARY="$INSTALL_DIR/lib/libcurl.a" \
-DCURL_INCLUDE_DIR="$INSTALL_DIR/include" \
-DCMAKE_INSTALL_PREFIX="$BUILD_DIR/minetest-install" \
-G "Unix Makefiles" \
"$SOURCES_DIR/minetest"
fi
emmake make
rm -rf "$BUILD_DIR/minetest-install"
emmake make install

View File

@ -6,7 +6,7 @@ unpack_source SQLite
pushd "$BUILD_DIR/SQLite"
export BUILD_CC="gcc"
emconfigure ./configure --disable-shared --prefix="$INSTALL_DIR" cross_compiling=yes
emconfigure ./configure --disable-tcl --disable-shared --prefix="$INSTALL_DIR" cross_compiling=yes
emmake make
emmake make install

View File

@ -4,6 +4,28 @@ cd "$BASE_DIR"
# Debug / Release
export BUILD_KIND="${BUILD_KIND:-release}"
# Setup emscripten (if not already)
export EMSDK="${EMSDK:-use_local_install}"
if [ "$EMSDK" == "use_local_install" ]; then
if [ ! -d emsdk ]; then
set +x
echo "-------------------------------------------------------"
echo "Emscripten is not installed. (EMSDK not set)"
echo "Press ENTER to install it into emsdk/. Ctrl-C to abort."
echo "-------------------------------------------------------"
read unused_var
if [ "$unused_var" != "" ]; then
echo "Aborting"
exit 1
fi
set -x
./install_emsdk.sh
fi
pushd emsdk
source ./emsdk_env.sh
popd
fi
case $BUILD_KIND in
debug)
export MINETEST_BUILD_TYPE="Debug"
@ -40,7 +62,7 @@ export CFLAGS="$COMMON_CFLAGS -pthread -sUSE_PTHREADS=1 -fexceptions"
export CXXFLAGS="$COMMON_CFLAGS -pthread -sUSE_PTHREADS=1 -fexceptions"
export LDFLAGS="$COMMON_LDFLAGS -pthread -sUSE_PTHREADS=1 -fexceptions -sEXIT_RUNTIME"
export EMSDK_ROOT="$HOME/emsdk"
export EMSDK_ROOT="$EMSDK"
export EMSDK_SYSLIB="${EMSDK_ROOT}/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten"
export EMSDK_SYSINCLUDE="${EMSDK_ROOT}/upstream/emscripten/cache/sysroot/include"

View File

@ -1,12 +1,12 @@
diff -urN emsdk-pure/upstream/emscripten/emcc.py emsdk/upstream/emscripten/emcc.py
--- emsdk-pure/upstream/emscripten/emcc.py 2022-08-02 14:52:59.442877733 +0000
+++ emsdk/upstream/emscripten/emcc.py 2022-08-02 14:52:18.131057940 +0000
@@ -624,6 +624,8 @@
cmd = config.JS_ENGINE
diff -urN emsdk/upstream/emscripten/emcc.py emsdk-fixed/upstream/emscripten/emcc.py
--- emsdk/upstream/emscripten/emcc.py 2022-11-07 20:04:28.000000000 +0000
+++ emsdk-fixed/upstream/emscripten/emcc.py 2022-11-11 02:48:00.766940527 +0000
@@ -632,6 +632,8 @@
cmd = config.JS_ENGINES[0]
if settings.WASM_BIGINT:
cmd.append('--experimental-wasm-bigint')
cmd += shared.node_bigint_flags()
+ if settings.USE_PTHREADS:
+ cmd.append('--experimental-wasm-threads')
cmd = shared.shlex_join(cmd)
if not os.path.isabs(config.JS_ENGINE[0]):
if not os.path.isabs(cmd[0]):
# TODO: use whereis etc. And how about non-*NIX?

View File

@ -16,7 +16,6 @@ getsource "https://downloads.xiph.org/releases/vorbis/libvorbis-1.3.7.tar.gz" 0e
getsource "https://www.sqlite.org/src/tarball/698edb77/SQLite-698edb77.tar.gz" b1568dc5d17788b9dd9575ecd224b3f7985b51764bc2f34f4808d851332840ef
getsource "https://www.openssl.org/source/openssl-1.1.1n.tar.gz" 40dceb51a4f6a5275bde0e6bf20ef4b91bfc32ed57c0552e2e8e15463372b17a
getsource "https://curl.se/download/curl-7.82.0.tar.bz2" 46d9a0400a33408fd992770b04a44a7434b3036f2e8089ac28b57573d59d371f
getsource "https://github.com/minetest/minetest/releases/download/5.5.1/minetest-5.5.1-win64.zip" 9be48fb4126312b0d8fd048d1ab06d59b3109f6fe037fcd5ab65b99507e68a5f
getsource "https://www.libarchive.org/downloads/libarchive-3.6.1.tar.xz" 5a411aceb978f43e626f0c2d1812ddd8807b645ed892453acabd532376c148e6
# These are never checked into the repo, since they are separate git repos.
@ -27,13 +26,17 @@ getrepo libpng "https://git.code.sf.net/p/libpng/code" a37d4836519517bdce6cb9d95
getrepo freetype "https://gitlab.freedesktop.org/freetype/freetype.git" a8e4563c3418ed74d39019a6c5e2122d12c8f56f
getrepo zstd "https://github.com/facebook/zstd.git" e47e674cd09583ff0503f0f6defd6d23d8b718d3
# Minetest Game
getrepo minetest_game "https://github.com/minetest/minetest_game.git" 99a719309559b632af65756f380496aa269d4b4a
# These repos are part of the fork
getrepo webshims "https://github.com/paradust7/webshims.git" 91c3fe85d2cb7f85cc8e19d3f53dc8f252a69ff7
getrepo minetest "https://github.com/paradust7/minetest.git" 08c43ff4602810269a204bcae1cdd72005b82f22
getrepo irrlichtmt "https://github.com/paradust7/irrlicht.git" d1dae521fb75d2140e3db20a4562f907cdb77971
getrepo minetest "https://github.com/paradust7/minetest.git" 20368d3a8d797f06c1a33aeb660f9ca4f5798595
getrepo irrlichtmt "https://github.com/paradust7/irrlicht.git" 5a79e1ae2bb0346e261152a21261af0531c2e4bf
# Make irrlichtmt symlink
pushd "$SOURCES_DIR"/minetest/lib
rm -f irrlichtmt
ln -s ../../irrlichtmt irrlichtmt
if [ ! -e irrlichtmt ]; then
ln -s ../../irrlichtmt irrlichtmt
fi
popd

16
install_emsdk.sh Executable file
View File

@ -0,0 +1,16 @@
#!/bin/bash -eux
BASE_DIR="$(dirname -- "$(readlink -f -- "$0")")"
cd "$BASE_DIR"
rm -rf emsdk
git clone https://github.com/emscripten-core/emsdk.git
pushd emsdk
./emsdk install 3.1.25
./emsdk activate 3.1.25
popd
./apply_patches.sh emsdk

Binary file not shown.

View File

@ -5,17 +5,60 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Minetest for the Web</title>
</head>
<body>
<div id="temporary_console" class="console"></div>
<body style="background-color: black">
<div id="temporary_console" class="console" style="color: white"></div>
<div id="launch_form" class="console" style="color: white; display: none;">
<hr>
<b>Select Network Proxy:</b>
<select id="select_proxy">
<!-- filled by javascript -->
</select>
<hr>
<button id="launch" style="width: 300px; height: 50px; font-size: 20pt;">Launch Minetest</button>
<br/>
<hr>
<b style="font-size: 20pt;">Run Server In Browser</b>
<br/>
<b>Player Name:</b>
<input style="border: 2px solid black; width: 400px; height: 2em" type="text" id="multiplayer_username" required="" pattern="[A-Za-z0-9_-]{1,20}" maxlength="19" title="Alphanumeric, 19 characters max">
<br/>
<b>Game:</b>
<select id="multiplayer_game">
<option value="minetest_game">minetest_game</option>
</select>
<br/>
<span>After launch, copy the URL and use it to open clients.</span>
<br/>
<span>Closing this page shuts down the server and ends the game.</span>
<br/>
<button id="launch_server" style="width: 300px; height: 50px; font-size: 20pt;">Launch Server</button>
</div>
<div id="join_form" class="console" style="color: white; display: none;">
<b>Joining game:</b>
<br/>
<b>Player Name:</b>
<input style="border: 2px solid black; width: 400px; height: 2em" type="text" id="join_username" required="" pattern="[A-Za-z0-9_-]{1,20}" maxlength="19" title="Alphanumeric, 19 characters max">
<br/>
<button id="join_launch">Join Game</button>
</div>
<script type="text/javascript" src="%__RELEASE_UUID__%/launcher.js"></script>
<script type="text/javascript">
document.body.style.backgroundColor = 'black';
// Reduce memory and network usage
const memorySavingConfig = `
viewing_range = 85
max_block_send_distance = 5
max_block_generate_distance = 5
`;
const launchForm = document.getElementById('launch_form');
const joinForm = document.getElementById('join_form');
const tcon = document.getElementById('temporary_console');
tcon.style.color = 'white';
function tcon_print(prefix, line) {
if (prefix) {
for (child of tcon.childNodes) {
if (child.innerText.startsWith(prefix)) {
if (child.nodeName == 'PRE' && child.innerText.startsWith(prefix)) {
child.innerText = prefix + line;
return;
}
@ -25,6 +68,142 @@
p.innerText = prefix + line;
tcon.appendChild(p);
}
function wipePage() {
launchForm.remove();
joinForm.remove();
tcon.remove();
}
function enableLaunchForm() {
if (join_code) {
joinForm.style.display = 'block';
} else {
launchForm.style.display = 'block';
}
const select_proxy = document.getElementById('select_proxy');
const proxies = [
[ "wss://na1.dustlabs.io/mtproxy", "North America" ],
[ "wss://sa1.dustlabs.io/mtproxy", "South America" ],
[ "wss://eu1.dustlabs.io/mtproxy", "Europe" ],
[ "wss://ap1.dustlabs.io/mtproxy", "Asia" ],
[ "wss://ap2.dustlabs.io/mtproxy", "Australia" ],
];
proxies.forEach((entry, index) => {
const opt = document.createElement('option');
opt.value = index;
opt.innerText = entry[1];
select_proxy.appendChild(opt);
});
const launch = document.getElementById('launch');
launch.addEventListener('click', () => {
const proxyIndex = document.getElementById('select_proxy').value;
const proxy = proxies[proxyIndex][0];
wipePage();
mtl.setProxy(proxy);
mtl.launch(args);
});
const launchServer = document.getElementById('launch_server');
launchServer.addEventListener('click', async () => {
const proxyIndex = document.getElementById('select_proxy').value;
const username = document.getElementById('multiplayer_username').value;
const game = document.getElementById('multiplayer_game').value;
if (username.length == 0) {
alert("Empty username");
return;
}
wipePage();
const proxy = proxies[proxyIndex][0];
mtl.setProxy(proxy);
mtl.addPack(game);
const [cmd, serverCode, clientCode] = await queryProxy(`MAKEVPN ${game}`, proxy);
if (cmd != 'NEWVPN') {
alert("Invalid response from proxy");
return;
}
const args = new MinetestArgs();
args.extra.push('--withserver');
args.gameid = game;
args.name = username;
args.address = '127.0.0.1';
args.port = 30000;
args.go = true;
history.pushState({}, "", `?proxy=${proxyIndex}&game=${game}&join=${clientCode}`);
mtl.setVPN(serverCode, clientCode);
mtl.setMinetestConf(memorySavingConfig);
mtl.launch(args);
});
const joinLaunch = document.getElementById('join_launch');
joinLaunch.addEventListener('click', async () => {
const username = document.getElementById('join_username').value;
if (username.length == 0) {
alert("Empty username");
return;
}
wipePage();
mtl.setProxy(proxies[join_proxy][0]);
mtl.addPack(join_game);
const args = new MinetestArgs();
args.gameid = join_game;
args.name = username;
args.address = '172.16.0.1';
args.port = 30000;
args.go = true;
mtl.setVPN(null, join_code);
mtl.setMinetestConf(memorySavingConfig);
mtl.launch(args);
});
}
function queryProxy(cmd, proxy) {
return new Promise((resolve, reject) => {
let finished = false;
const ws = new WebSocket(proxy);
ws.addEventListener('open', (event) => {
ws.send(cmd);
});
ws.addEventListener('error', (event) => {
alert('Error initiating proxy connection');
finished = true;
reject(new Error('Received error'));
});
ws.addEventListener('close', (event) => {
if (!finished) {
alert('Proxy connection closed unexpectedly');
finished = true;
reject(new Error('Received close'));
}
});
ws.addEventListener('message', (event) => {
if (typeof event.data !== 'string') {
alert('Invalid message received from proxy');
finished = true;
reject(new Error('Invalid message'));
return;
}
finished = true;
ws.close();
resolve(event.data.split(' '));
});
});
}
let join_code = null;
let join_proxy = null;
let join_game = null;
const peek_params = new URLSearchParams(window.location.search);
if (peek_params.has("join")) {
join_code = peek_params.get("join");
join_game = peek_params.get("game");
join_proxy = peek_params.get("proxy");
}
const args = MinetestArgs.fromQueryString(window.location.search);
tcon_print("", "Minetest Arguments: " + JSON.stringify(args.toArray(), null, ' '));
tcon_print("", "Data Packs: " + JSON.stringify(args.packs, null, ' '));
@ -41,16 +220,7 @@
};
mtl.onready = () => {
tcon_print("", "READY!");
const launchButton = document.createElement('button');
launchButton.style.width = '300px';
launchButton.style.height = '120px';
launchButton.style.fontSize = '20pt';
launchButton.innerText = 'Click to Launch';
launchButton.addEventListener('click', () => {
tcon.remove();
mtl.launch(args);
});
tcon.appendChild(launchButton);
enableLaunchForm();
};
mtl.onerror = (err) => {
tcon_print("", "**************** FATAL ERROR ******************");

View File

@ -240,6 +240,7 @@ var emloop_unpause;
var emloop_init_sound;
var emloop_invoke_main;
var emloop_install_pack;
var emloop_set_minetest_conf;
var irrlicht_want_pointerlock;
var irrlicht_force_pointerlock;
var irrlicht_resize;
@ -254,6 +255,7 @@ function emloop_ready() {
emloop_init_sound = cwrap("emloop_init_sound", null, []);
emloop_invoke_main = cwrap("emloop_invoke_main", null, ["number", "number"]);
emloop_install_pack = cwrap("emloop_install_pack", null, ["number", "number", "number"]);
emloop_set_minetest_conf = cwrap("emloop_set_minetest_conf", null, ["number"]);
irrlicht_want_pointerlock = cwrap("irrlicht_want_pointerlock", "number");
irrlicht_force_pointerlock = cwrap("irrlicht_force_pointerlock", null);
irrlicht_resize = cwrap("irrlicht_resize", null, ["number", "number"]);
@ -274,9 +276,9 @@ function makeArgv(args) {
const argv = _malloc((args.length + 1) * 4);
let i;
for (i = 0; i < args.length; i++) {
HEAPU32[(argv >> 2) + i] = allocateUTF8(args[i]);
HEAPU32[(argv >>> 2) + i] = allocateUTF8(args[i]);
}
HEAPU32[(argv >> 2) + i] = 0; // argv[argc] == NULL
HEAPU32[(argv >>> 2) + i] = 0; // argv[argc] == NULL
return [i, argv];
}
@ -500,7 +502,7 @@ class MinetestArgs {
if (this.password) args.push('--password', this.password);
if (this.gameid) args.push('--gameid', this.gameid);
if (this.address) args.push('--address', this.address);
if (this.port) args.push('--port', this.port);
if (this.port) args.push('--port', this.port.toString());
args.push(...this.extra);
return args;
}
@ -513,7 +515,7 @@ class MinetestArgs {
if (this.password) params.append('password', this.password);
if (this.gameid) params.append('gameid', this.gameid);
if (this.address) params.append('address', this.address);
if (this.port) params.append('port', this.port);
if (this.port) params.append('port', this.port.toString());
const extra_packs = [];
this.packs.forEach(v => {
if (v != 'base' && v != 'minetest_game' && v != 'devtest' && v != this.gameid) {
@ -538,7 +540,7 @@ class MinetestArgs {
if (params.has('password')) r.password = params.get('password');
if (params.has('gameid')) r.gameid = params.get('gameid');
if (params.has('address')) r.address = params.get('address');
if (params.has('port')) r.port = params.get('port');
if (params.has('port')) r.port = parseInt(params.get('port'));
if (r.gameid && r.gameid != 'minetest_game' && r.gameid != 'devtest' && r.gameid != 'base') {
r.packs.push(r.gameid);
}
@ -574,6 +576,7 @@ class MinetestLauncher {
this.proxyUrl = "wss://minetest.dustlabs.io/proxy";
this.packsDir = DEFAULT_PACKS_DIR;
this.packsDirIsCors = false;
this.minetestConf = null;
mtScheduler.addCondition("wasmReady", loadWasm);
mtScheduler.addCondition("launch_called");
@ -611,6 +614,10 @@ class MinetestLauncher {
this.vpn = serverCode ? serverCode : clientCode;
}
setMinetestConf(contents) {
this.minetestConf = contents;
}
// Returns pack status:
// 0 - pack has not been added
// 1 - pack is downloading
@ -725,6 +732,11 @@ class MinetestLauncher {
this.addPacks(this.args.packs);
activateBody();
fixGeometry();
if (this.minetestConf) {
const confBuf = allocateUTF8(this.minetestConf)
emloop_set_minetest_conf(confBuf);
_free(confBuf);
}
emloop_init_sound();
// Setup emsocket
// TODO: emsocket should export the helpers for this