improve compatibility with espidf

This commit is contained in:
p2r3
2025-08-14 05:08:08 +03:00
parent c40928412b
commit 1d76a0d96e
4 changed files with 63 additions and 21 deletions

24
main.c
View File

@@ -7,8 +7,14 @@
#define CLOCK_REALTIME 0
#endif
#include <arpa/inet.h>
#include <unistd.h>
#ifdef ESP_PLATFORM
#include "lwip/sockets.h"
#include "lwip/netdb.h"
#include "esp_task_wdt.h"
#else
#include <arpa/inet.h>
#include <unistd.h>
#endif
#include "src/globals.h"
#include "src/tools.h"
@@ -30,8 +36,8 @@ void handlePacket (int client_fd, int length, int packet_id) {
return;
} else if (state == STATE_LOGIN) {
if (cs_loginStart(client_fd)) break;
if (reservePlayerData(client_fd, recv_buffer + 17)) break;
if (sc_loginSuccess(client_fd, recv_buffer, recv_buffer + 17)) break;
if (reservePlayerData(client_fd, (char *)(recv_buffer + 17))) break;
if (sc_loginSuccess(client_fd, (char *)recv_buffer, (char *)(recv_buffer + 17))) break;
return;
} else if (state == STATE_CONFIGURATION) {
if (cs_clientInformation(client_fd)) break;
@@ -216,6 +222,10 @@ void handlePacket (int client_fd, int length, int packet_id) {
int main () {
#ifdef ESP_PLATFORM
esp_task_wdt_add(NULL);
#endif
for (int i = 0; i < sizeof(block_changes) / sizeof(BlockChange); i ++) {
block_changes[i].block = 0xFF;
}
@@ -300,6 +310,7 @@ int main () {
handlePacket(client_fd, length - sizeVarInt(packet_id), packet_id);
if (recv_count == -1) break;
wdt_reset();
}
setClientState(client_fd, STATE_NONE);
@@ -308,11 +319,16 @@ int main () {
close(client_fd);
printf("Connection closed.\n");
wdt_reset();
}
close(server_fd);
printf("Server closed.\n");
#ifdef ESP_PLATFORM
vTaskDelete(NULL);
#else
return 0;
#endif
}

View File

@@ -4,6 +4,14 @@
#include <stdint.h>
#include <unistd.h>
#ifdef ESP_PLATFORM
#define wdt_reset(); \
esp_task_wdt_reset(); \
vTaskDelay(1);
#else
#define wdt_reset();
#endif
#define true 1
#define false 0

View File

@@ -2,8 +2,14 @@
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#ifdef ESP_PLATFORM
#include "lwip/sockets.h"
#include "lwip/netdb.h"
#include "esp_task_wdt.h"
#else
#include <arpa/inet.h>
#include <unistd.h>
#endif
#include "globals.h"
#include "tools.h"
@@ -16,7 +22,7 @@
int cs_handshake (int client_fd) {
printf("Received Handshake:\n");
printf(" Protocol version: %d\n", readVarInt(client_fd));
printf(" Protocol version: %d\n", (int)readVarInt(client_fd));
readString(client_fd);
if (recv_count == -1) return 1;
printf(" Server address: %s\n", recv_buffer);
@@ -75,23 +81,23 @@ int cs_clientInformation (int client_fd) {
printf(" Chat mode: %d\n", tmp);
tmp = readByte(client_fd);
if (recv_count == -1) return 1;
if (tmp) printf(" Chat colors: on\n", tmp);
else printf(" Chat colors: off\n", tmp);
if (tmp) printf(" Chat colors: on\n");
else printf(" Chat colors: off\n");
tmp = readByte(client_fd);
if (recv_count == -1) return 1;
printf(" Skin parts: %d\n", tmp);
tmp = readVarInt(client_fd);
if (recv_count == -1) return 1;
if (tmp) printf(" Main hand: right\n", tmp);
else printf(" Main hand: left\n", tmp);
if (tmp) printf(" Main hand: right\n");
else printf(" Main hand: left\n");
tmp = readByte(client_fd);
if (recv_count == -1) return 1;
if (tmp) printf(" Text filtering: on\n", tmp);
else printf(" Text filtering: off\n", tmp);
if (tmp) printf(" Text filtering: on\n");
else printf(" Text filtering: off\n");
tmp = readByte(client_fd);
if (recv_count == -1) return 1;
if (tmp) printf(" Allow listing: on\n", tmp);
else printf(" Allow listing: off\n", tmp);
if (tmp) printf(" Allow listing: on\n");
else printf(" Allow listing: off\n");
tmp = readVarInt(client_fd);
if (recv_count == -1) return 1;
printf(" Particles: %d\n\n", tmp);
@@ -118,7 +124,7 @@ int cs_pluginMessage (int client_fd) {
readString(client_fd);
if (recv_count == -1) return 1;
printf(" Channel: \"%s\"\n", recv_buffer);
if (strcmp(recv_buffer, "minecraft:brand") == 0) {
if (strcmp((char *)recv_buffer, "minecraft:brand") == 0) {
readString(client_fd);
if (recv_count == -1) return 1;
printf(" Brand: \"%s\"\n", recv_buffer);
@@ -228,6 +234,7 @@ int sc_setDefaultSpawnPosition (int client_fd, long x, long y, long z) {
writeUint64(client_fd, ((x & 0x3FFFFFF) << 38) | ((z & 0x3FFFFFF) << 12) | (y & 0xFFF));
writeFloat(client_fd, 0);
return 0;
}
// S->C Player Abilities (clientbound)
@@ -240,6 +247,7 @@ int sc_playerAbilities (int client_fd, uint8_t flags) {
writeFloat(client_fd, 0.05f);
writeFloat(client_fd, 0.1f);
return 0;
}
// S->C Update Time
@@ -252,6 +260,7 @@ int sc_updateTime (int client_fd, uint64_t ticks) {
writeUint64(client_fd, ticks);
writeByte(client_fd, true);
return 0;
}
// S->C Game Event 13 (Start waiting for level chunks)
@@ -311,6 +320,8 @@ int sc_chunkDataAndUpdateLight (int client_fd, int _x, int _z) {
// biome data
writeByte(client_fd, 0); // bits per entry
writeByte(client_fd, W_plains); // palette
// reset watchdog and yield
wdt_reset();
}
writeVarInt(client_fd, 0); // omit block entities
@@ -336,6 +347,7 @@ int sc_keepAlive (int client_fd) {
writeUint64(client_fd, 0);
return 0;
}
// S->C Set Container Slot
@@ -386,7 +398,7 @@ int cs_playerAction (int client_fd) {
// block was mined in survival
uint8_t block = getBlockAt(x, y, z);
uint16_t tmp, item = getMiningResult(client_fd, block);
uint16_t item = getMiningResult(client_fd, block);
makeBlockChange(x, y, z, 0);
@@ -411,6 +423,7 @@ int sc_openScreen (int client_fd, uint8_t window, const char *title, uint16_t le
writeUint16(client_fd, length); // string length
send(client_fd, title, length, 0);
return 0;
}
// C->S Use Item On

View File

@@ -1,8 +1,14 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#ifdef ESP_PLATFORM
#include "lwip/sockets.h"
#include "lwip/netdb.h"
#else
#include <arpa/inet.h>
#include <unistd.h>
#endif
#include "globals.h"
#include "registries.h"
@@ -292,7 +298,6 @@ uint16_t getMiningResult (int client_fd, uint8_t block) {
case B_oak_leaves:
uint32_t r = fast_rand();
printf("fast_rand: %u, in distribution: %.3f\n", r, (float)r / (4294967295.0f));
if (r < 21474836) return I_apple; // 0.5%
if (r < 85899345) return I_stick; // 2%
if (r < 214748364) return I_oak_sapling; // 5%