Commit Diff


commit - f6333429eba6269d19498eb9eddc6f49be0ed9f9
commit + 26f068bc094128a5aaa18b79538fb7297ee25ac3
blob - 23e98be36a49ff4612a8dbe82b793844532cd9d6
blob + e4b10c147dd047ac6ef5d0853bf3ab3f91f1fb7f
--- README.md
+++ README.md
@@ -15,10 +15,13 @@ For PC x86_64 platforms, grab the [latest build binary
 For microcontrollers, see the section on **compilation** below.
 
 ## Compilation
-Before compiling, you'll need to dump registry data from a vanilla Minecraft server. On Linux, this can be done automatically using the `extract_registries.sh` script. Otherwise, the manual process is as follows: create a folder called `notchian` here, and put a Minecraft server JAR in it. Then, follow [this guide](https://minecraft.wiki/w/Minecraft_Wiki:Projects/wiki.vg_merge/Data_Generators) to dump all of the registries. Finally, run `build_registries.js` with `node`, `bun`, or `deno`.
+Before compiling, you'll need to dump registry data from a vanilla Minecraft server. On Linux, this can be done automatically using the `extract_registries.sh` script. Otherwise, the manual process is as follows: create a folder called `notchian` here, and put a Minecraft server JAR in it. Then, follow [this guide](https://minecraft.wiki/w/Minecraft_Wiki:Projects/wiki.vg_merge/Data_Generators) to dump all of the registries (use the _second_ command with the `--all` flag). Finally, run `build_registries.js` with either [bun](https://bun.sh/), [node](https://nodejs.org/en/download), or [deno](https://docs.deno.com/runtime/getting_started/installation/).
 
-- To target Linux, install `gcc` and run `./build.sh`
-- To target Windows, install [MSYS2](https://www.msys2.org/), and open the "MSYS2 MSYS" shell once installed. From there, install `gcc` (run `pacman -Sy gcc`), navigate to this project's directory and run `./build.sh`. Alternatively, install WSL and use `gcc`. Otherwise, there's no platform-native solution for Windows currently.
+- To compile on Linux, install `gcc` and run `./build.sh`.
+- For compiling on Windows, there are a few options:
+  - To compile a native Windows binary: install [MSYS2](https://www.msys2.org/) and open the "MSYS2 MINGW64" shell. From there, run `pacman -Sy mingw-w64-x86_64-gcc`, navigate to this project's directory, and run `./build.sh`. 
+  - To compile a MSYS2-linked binary: install [MSYS2](https://www.msys2.org/), and open the "MSYS2 MSYS" shell. From there, install `gcc` (run `pacman -Sy gcc`), navigate to this project's directory and run `./build.sh`.
+  - To compile and run a Linux binary from Windows: install WSL, and from there install `gcc` and run `./build.sh` in this project's directory.
 - To target an ESP variant, set up a PlatformIO project (select the ESP-IDF framework, **not Arduino**) and clone this repository on top of it. See **Configuration** below for further steps. For better performance, consider changing the clock speed and enabling compiler optimizations. If you don't know how to do this, there are plenty of resources online.
 
 ## Configuration
blob - 09ed5c7dcd8e403dc753e2794e070d4c0fcce358
blob + 601c1906d4585a3fe6dbe466620164097126ce22
--- build.sh
+++ build.sh
@@ -13,6 +13,15 @@ case "$OSTYPE" in
   *) exe="" ;;
 esac
 
+# mingw64-specific linker options
+windows_linker=""
+unameOut="$(uname -s)"
+case "$unameOut" in
+  MINGW64_NT*)
+    windows_linker="-static -lws2_32 -pthread"
+    ;;
+esac
+
 rm -f "bareiron$exe"
-gcc src/*.c -O3 -Iinclude -o "bareiron$exe"
+gcc src/*.c -O3 -Iinclude -o "bareiron$exe" $windows_linker
 "./bareiron$exe"
blob - ef0638f2b54ea56c7912e5c7622aac38912db3d3
blob + 731e7fad9e48b6855e54acb2603331ca3c42380b
--- src/globals.c
+++ src/globals.c
@@ -1,6 +1,11 @@
 #include <stdio.h>
 #include <stdint.h>
-#include <arpa/inet.h>
+#ifdef _WIN32
+  #include <winsock2.h>
+  #include <ws2tcpip.h>
+#else
+  #include <arpa/inet.h>
+#endif
 #include <unistd.h>
 
 #include "globals.h"
blob - a489810b0140fda7963e4352b88e1989b77ca6cc
blob + dd065d9e1204851ed64c77cb04111a56295c0615
--- src/main.c
+++ src/main.c
@@ -19,9 +19,14 @@
   #include "lwip/netdb.h"
 #else
   #include <sys/types.h>
-  #include <sys/socket.h>
-  #include <netinet/in.h>
-  #include <arpa/inet.h>
+  #ifdef _WIN32
+    #include <winsock2.h>
+    #include <ws2tcpip.h>
+  #else
+    #include <sys/socket.h>
+    #include <netinet/in.h>
+    #include <arpa/inet.h>
+  #endif
   #include <unistd.h>
   #include <time.h>
 #endif
@@ -491,6 +496,13 @@ void handlePacket (int client_fd, int length, int pack
 }
 
 int main () {
+  #ifdef _WIN32 //initialize windows socket
+    WSADATA wsa;
+      if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
+        fprintf(stderr, "WSAStartup failed\n");
+        exit(EXIT_FAILURE);
+      }
+  #endif
 
   // Hash the seeds to ensure they're random enough
   world_seed = splitmix64(world_seed);
@@ -528,8 +540,12 @@ int main () {
     perror("socket failed");
     exit(EXIT_FAILURE);
   }
-
+#ifdef _WIN32
+  if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR,
+      (const char*)&opt, sizeof(opt)) < 0) {
+#else
   if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
+#endif    
     perror("socket options failed");
     exit(EXIT_FAILURE);
   }
@@ -555,8 +571,16 @@ int main () {
 
   // Make the socket non-blocking
   // This is necessary to not starve the idle task during slow connections
+  #ifdef _WIN32
+    u_long mode = 1;  // 1 = non-blocking
+    if (ioctlsocket(server_fd, FIONBIO, &mode) != 0) {
+      fprintf(stderr, "Failed to set non-blocking mode\n");
+      exit(EXIT_FAILURE);
+    }
+  #else
   int flags = fcntl(server_fd, F_GETFL, 0);
   fcntl(server_fd, F_SETFL, flags | O_NONBLOCK);
+  #endif
 
   // Track time of last server tick (in microseconds)
   int64_t last_tick_time = get_program_time();
@@ -577,8 +601,13 @@ int main () {
       // If the accept was successful, make the client non-blocking too
       if (clients[i] != -1) {
         printf("New client, fd: %d\n", clients[i]);
+      #ifdef _WIN32
+        u_long mode = 1;
+        ioctlsocket(clients[i], FIONBIO, &mode);
+      #else
         int flags = fcntl(clients[i], F_GETFL, 0);
         fcntl(clients[i], F_SETFL, flags | O_NONBLOCK);
+      #endif
         client_count ++;
       }
       break;
@@ -600,6 +629,22 @@ int main () {
     int client_fd = clients[client_index];
 
     // Check if at least 2 bytes are available for reading
+    #ifdef _WIN32
+    recv_count = recv(client_fd, recv_buffer, 2, MSG_PEEK);
+    if (recv_count == 0) {
+      disconnectClient(&clients[client_index], 1);
+      continue;
+    }
+    if (recv_count == SOCKET_ERROR) {
+      int err = WSAGetLastError();
+      if (err == WSAEWOULDBLOCK) {
+        continue; // no data yet, keep client alive
+      } else {
+        disconnectClient(&clients[client_index], 1);
+        continue;
+      }
+    }
+    #else
     recv_count = recv(client_fd, &recv_buffer, 2, MSG_PEEK);
     if (recv_count < 2) {
       if (recv_count == 0 || (recv_count < 0 && errno != EAGAIN && errno != EWOULDBLOCK)) {
@@ -607,7 +652,7 @@ int main () {
       }
       continue;
     }
-
+    #endif
     // Handle 0xBEEF and 0xFEED packets for dumping/uploading world data
     #ifdef DEV_ENABLE_BEEF_DUMPS
     // Received BEEF packet, dump world data and disconnect
@@ -674,6 +719,11 @@ int main () {
   }
 
   close(server_fd);
+ 
+  #ifdef _WIN32 //cleanup windows socket
+    WSACleanup();
+  #endif
+
   printf("Server closed.\n");
 
 }
blob - 8fd13a4fc3fee6359f994ce4f14d246d2d7a5791
blob + 5cc1a51e31e791d131addf9f6a7cbdfcd424504a
--- src/packets.c
+++ src/packets.c
@@ -6,7 +6,12 @@
   #include "lwip/netdb.h"
   #include "esp_task_wdt.h"
 #else
-  #include <arpa/inet.h>
+  #ifdef _WIN32
+    #include <winsock2.h>
+    #include <ws2tcpip.h>
+  #else
+    #include <arpa/inet.h>
+  #endif
   #include <unistd.h>
 #endif
 
blob - a680c06915a613fe98831970c443aa7bf9a5e598
blob + 780d6e055a8239a2ee9ca2454ec435e49dac2244
--- src/procedures.c
+++ src/procedures.c
@@ -2,6 +2,9 @@
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
+#ifdef _WIN32
+#include <winsock2.h>
+#endif
 
 #include "globals.h"
 #include "tools.h"
@@ -181,9 +184,14 @@ void disconnectClient (int *client_fd, int cause) {
   client_count --;
   setClientState(*client_fd, STATE_NONE);
   handlePlayerDisconnect(*client_fd);
+  #ifdef _WIN32
+  closesocket(*client_fd);
+  printf("Disconnected client %d, cause: %d, errno: %d\n", *client_fd, cause, WSAGetLastError());
+  #else
   close(*client_fd);
-  *client_fd = -1;
   printf("Disconnected client %d, cause: %d, errno: %d\n\n", *client_fd, cause, errno);
+  #endif
+  *client_fd = -1;
 }
 
 uint8_t serverSlotToClientSlot (int window_id, uint8_t slot) {
blob - 5e2f15dd5bee3adffa38c702806ca07254b55c69
blob + fabb6f097be7f7314556a178a854c0acc6b2df05
--- src/tools.c
+++ src/tools.c
@@ -7,8 +7,13 @@
   #include "lwip/netdb.h"
   #include "esp_timer.h"
 #else
-  #include <sys/socket.h>
-  #include <arpa/inet.h>
+  #ifdef _WIN32
+    #include <winsock2.h>
+    #include <ws2tcpip.h>
+  #else
+    #include <sys/socket.h>
+    #include <arpa/inet.h>
+  #endif
   #include <unistd.h>
   #include <time.h>
   #ifndef CLOCK_MONOTONIC
@@ -95,7 +100,11 @@ ssize_t send_all (int client_fd, const void *buf, ssiz
 
   // Busy-wait (with task yielding) until all data has been sent
   while (sent < len) {
-    ssize_t n = send(client_fd, p + sent, len - sent, MSG_NOSIGNAL);
+    #ifdef _WIN32
+      ssize_t n = send(client_fd, p + sent, len - sent, 0);
+    #else
+      ssize_t n = send(client_fd, p + sent, len - sent, MSG_NOSIGNAL);
+    #endif
     if (n > 0) { // some data was sent, log it
       sent += n;
       last_update_time = get_program_time();
@@ -106,7 +115,12 @@ ssize_t send_all (int client_fd, const void *buf, ssiz
       return -1;
     }
     // not yet ready to transmit, try again
+    #ifdef _WIN32 //handles windows socket timeout
+      int err = WSAGetLastError();
+      if (err == WSAEWOULDBLOCK || err == WSAEINTR) {
+    #else
     if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
+    #endif  
       // handle network timeout
       if (get_program_time() - last_update_time > NETWORK_TIMEOUT_TIME) {
         disconnectClient(&client_fd, -2);
blob - cd030d1232fae8fd7dde115b79e745e2f9def532
blob + 2a51cde42f6130f9dd76594d74690de15c9556e6
--- src/varnum.c
+++ src/varnum.c
@@ -1,5 +1,10 @@
 #include <stdint.h>
-#include <arpa/inet.h>
+#ifdef _WIN32
+  #include <winsock2.h>
+  #include <ws2tcpip.h>
+#else
+  #include <arpa/inet.h>
+#endif
 #include <unistd.h>
 
 #include "varnum.h"