Debian Codegraph @

code/web/client.html.in

raw
1 <!DOCTYPE html><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
2 <title>@CLIENT_NAME@ Emscripten demo</title>
3 <style>
4 html, body { margin: 0; padding: 0; width: 100%; height: 100%; overflow: hidden; background: rgb(0, 0, 0); display:flex; align-items: center; justify-content: center; }
5 canvas { max-width: 100%; max-height: 100%; min-width: 100%; min-height: 100%; object-fit: contain; }
6 </style>
7
8 <canvas id=canvas></canvas>
9
10 <script type=module>
11 // These strings are set in the generated HTML file in the build directory.
12 const CLIENT_NAME = '@CLIENT_NAME@';
13 const BASEGAME = '@BASEGAME@';
14 const EMSCRIPTEN_PRELOAD_FILE = '@EMSCRIPTEN_PRELOAD_FILE@' === 'ON';
15
16 // Path or URL containing the client engine .js, .wasm, and possibly .data.
17 let enginePath = './';
18 // Path or URL containing fs_game directories.
19 let dataPath = './';
20 // Path or URL for config file that specifies the files to load for each fs_game.
21 let configFilename = `./${CLIENT_NAME}-config.json`;
22
23 if (window.location.protocol === 'file:') throw new Error(`Unfortunately browser security restrictions prevent loading wasm from a file: URL. This file must be loaded from a web server. The easiest way to do this is probably to use Python\'s built-in web server by running \`python3 -m http.server\` in the top level source directory and then navigate to http://localhost:8000/build/debug-emscripten-wasm32/${CLIENT_NAME}.html`);
24
25 // First set up the command line arguments and the Emscripten filesystem.
26 const urlParams = new URLSearchParams(window.location.search);
27 const com_basegame = urlParams.get('com_basegame') || BASEGAME;
28 const fs_basegame = urlParams.get('fs_basegame') || '';
29 const fs_game = urlParams.get('fs_game') || '';
30 let generatedArguments = `
31 +set sv_pure 0
32 +set net_enabled 0
33 +set r_mode -2
34 +set com_basegame "${com_basegame}"
35 +set fs_basegame "${fs_basegame}"
36 +set fs_game "${fs_game}"
37 `;
38 // Note that unfortunately "+" needs to be encoded as "%2b" in URL query strings or it will be stripped by the browser.
39 const queryArgs = urlParams.get('args');
40 if (queryArgs) generatedArguments += ` ${queryArgs} `;
41
42 const dataURL = new URL(dataPath, location.origin + location.pathname);
43
44 const configPromise = EMSCRIPTEN_PRELOAD_FILE ? Promise.resolve({[BASEGAME]: {files: []}})
45 : fetch(configFilename).then(r => r.ok ? r.json() : { /* empty config */ });
46
47 const ioquake3 = (await import(enginePath + `@[email protected]`)).default;
48 ioquake3({
49 canvas: canvas,
50 arguments: generatedArguments.trim().split(/\s+/),
51 locateFile: (file) => enginePath + file,
52 preRun: [async (module) => {
53 module.addRunDependency('setup-ioq3-filesystem');
54 try {
55 const config = await configPromise;
56 const gamedirs = [com_basegame,fs_basegame,fs_game];
57 for (let g = 0; g < gamedirs.length; g++) {
58 const gamedir = gamedirs[g];
59 if (gamedir === '') {
60 continue;
61 }
62 if (config[gamedir] === null
63 || config[gamedir].files === null) {
64 console.warn(`Game directory '${gamedir}' cannot be used. It must have files listed in ${configFilename}.`);
65 continue;
66 }
67 const files = config[gamedir].files;
68 const fetches = files.map(file => fetch(new URL(file.src, dataURL)));
69 for (let i = 0; i < files.length; i++) {
70 const response = await fetches[i];
71 if (!response.ok) continue;
72 const data = await response.arrayBuffer();
73 let name = files[i].src.match(/[^/]+$/)[0];
74 let dir = files[i].dst;
75 module.FS.mkdirTree(dir);
76 module.FS.writeFile(`${dir}/${name}`, new Uint8Array(data));
77 }
78 }
79 } finally {
80 module.removeRunDependency('setup-ioq3-filesystem');
81 }
82 }],
83 });
84 </script>