Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Dependencies

```console
$ npm install
$ node run build
$ npm run build
$ npm run serve
$ <browser> https://localhost:6969/
```
Expand Down
47 changes: 38 additions & 9 deletions build.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// @ts-check
const { spawn } = require('child_process');
const { promisify } = require('util');
const { mkdir, mkdtemp } = require('fs/promises');
const { mkdir, mkdtemp, access } = require('fs/promises');
const fs = require('fs');
const path = require('path');

const BUILD_FOLDER = 'build/';
const SRC_FOLDER = 'src/';
Expand Down Expand Up @@ -33,43 +35,70 @@ function TODO(message) {
}

function buildJs() {
return cmdAsync("./node_modules/.bin/tsc", []);
// Use npx if available, otherwise resolve the path manually
const tscCmd = process.platform === 'win32' ? 'npx.cmd' : 'npx';
return cmdAsync(tscCmd, ['tsc']);
}

function getMathLibFlag() {
return process.platform === 'win32' ? [] : ['-lm'];
}

function getWasmCompiler() {
// Use emcc for wasm on Windows, clang elsewhere
return process.platform === 'win32' ? 'emcc' : 'clang';
}

function getPackerOutput() {
return process.platform === 'win32' ? BUILD_FOLDER + 'packer.exe' : BUILD_FOLDER + 'packer';
}

async function buildClient() {
const packerOutput = getPackerOutput();
await cmdAsync("clang", [
"-Wall", "-Wextra", "-ggdb",
"-I"+SRC_FOLDER,
"-I"+SRC_FOLDER+"cws/",
"-o", BUILD_FOLDER+"packer",
"-o", packerOutput,
SRC_FOLDER+"packer.c",
"-lm",
...getMathLibFlag(),
]);

await cmdAsync(BUILD_FOLDER+"packer", [
await cmdAsync(packerOutput, [
BUILD_FOLDER+"pack.h",
]);

// Check if build/pack.h exists
try {
await access(BUILD_FOLDER + "pack.h", fs.constants.F_OK);
} catch (err) {
throw new Error("build/pack.h was not generated. Please check if the packer step is working correctly.");
}

// Ensure pack.h is generated before starting wasm builds
const wasmCompiler = getWasmCompiler();
await Promise.all([
cmdAsync("clang", [
cmdAsync(wasmCompiler, [
"-Wall", "-Wextra",
"--target=wasm32",
"-I", SRC_FOLDER+"cws/",
"-I", BUILD_FOLDER,
"-c", SRC_FOLDER+"common.c",
"-o", BUILD_FOLDER+"common.wasm.o",
]),
cmdAsync("clang", [
cmdAsync(wasmCompiler, [
"-Wall", "-Wextra",
"--target=wasm32",
"-I", SRC_FOLDER+"cws/",
"-I", BUILD_FOLDER,
"-c", SRC_FOLDER+"client.c",
"-o", BUILD_FOLDER+"client.wasm.o",
]),
cmdAsync("clang", [
cmdAsync(wasmCompiler, [
"-Wall", "-Wextra",
"--target=wasm32",
"-I", SRC_FOLDER+"cws/",
"-I", BUILD_FOLDER,
"-c", SRC_FOLDER+"sort.c",
"-o", BUILD_FOLDER+"sort.wasm.o",
]),
Expand Down Expand Up @@ -155,7 +184,7 @@ async function buildServer() {
BUILD_FOLDER+"common.o",
BUILD_FOLDER+"stats.o",
BUILD_FOLDER+"libcws.a",
"-lm"
...getMathLibFlag(),
]);
}

Expand Down
13 changes: 6 additions & 7 deletions src/client.c3
Original file line number Diff line number Diff line change
Expand Up @@ -352,11 +352,12 @@ fn void ping_server_if_needed() {
if (!platform::is_offline_mode()) {
ping_cooldown -= 1;
if (ping_cooldown == 0) {
platform::send_message(&&PingMessage {
PingMessage ping_msg = {
.byte_length = PingMessage.sizeof,
.kind = PING,
.payload = platform::now_msecs(),
});
};
platform::send_message(&ping_msg);
ping_cooldown = PING_COOLDOWN;
}
}
Expand Down Expand Up @@ -414,8 +415,7 @@ fn bool process_message(Message *message) @extern("process_message") @wasm {
}

extern Player me @extern("me");
def OtherPlayersEntry = Entry(<uint, Player>);
HashMap(<uint, Player>) other_players;
var other_players: HashMap(uint, Player);

fn uint players_count() @extern("players_count") @wasm {
return other_players.len() + 1; // +1 including `me`
Expand All @@ -428,9 +428,8 @@ fn void unregister_all_other_players() @extern("unregister_all_other_players") @
fn void entry() @init(2048) @private {
// NOTE: ideally we need to override os::native_fputc_fn as well
// because io::printn uses it to print newline at the end of the
// message. But since js_write() in server.mts is implemented as a
// single console.log(), that newline is added implicitly anyway.
os::native_fwrite_fn = fn usz!(void* f, char[] buffer) {
// message. But since js_write() in server.mts is implemented as a single console.log(), that newline is added implicitly anyway.
os::native_fwrite_fn = fn(f: void*, buffer: char[]) usz {
client::platform::write(&buffer[0], buffer.len);
return buffer.len;
};
Expand Down