]> git.lizzy.rs Git - dragonblocks_alpha.git/blobdiff - src/client/client_commands.c
Port to OpenBSD
[dragonblocks_alpha.git] / src / client / client_commands.c
index 5581c0f88a972ddb07ead1f970ded21578b580d3..a93f38b34ea7dcb74aa2962c20fc99b7f081b4d6 100644 (file)
@@ -1,10 +1,14 @@
 #include <stdio.h>
 #include <unistd.h>
+#include <dragontype/number.h>
 #include "client/client.h"
 #include "client/client_map.h"
-#include "types.h"
+#include "client/client_player.h"
+#include "day.h"
+#include "perlin.h"
+#include "util.h"
 
-static bool disconnect_handler(__attribute__((unused)) Client *client, bool good)
+static bool disconnect_handler(unused Client *client, bool good)
 {
        if (good)
                client_disconnect(false, NULL);
@@ -38,38 +42,83 @@ static bool block_handler(Client *client, bool good)
        if (! read_v3s32(client->fd, &pos))
                return false;
 
-       MapBlockHeader header;
+       u64 size;
 
-       if (! read_u32(client->fd, &header))
+       if (! read_u64(client->fd, &size))
                return false;
 
-       char data[header];
-       if (! read_full(client->fd, data, header))
+       if (size > sizeof(MapBlockData))        // guard to prevent malicious or malfunctioning packets from allocating huge unnecessary amounts of memory
+               return false;
+
+       char data[size];
+       if (! read_full(client->fd, data, size))
                return false;
 
        MapBlock *block;
 
        if (good)
-               block = map_get_block(client->map, pos, true);
+               block = map_get_block(client_map.map, pos, true);
        else
                block = map_allocate_block(pos);
 
-       if (block->state != MBS_CREATED)
-               map_clear_meta(block);
-
-       bool ret = map_deserialize_block(block, data, header);
+       bool ret = map_deserialize_block(block, data, size);
 
        if (good)
-               client_map_block_changed(block);
+               client_map_block_received(block);
        else
                map_free_block(block);
 
        return ret;
 }
 
+static bool info_handler(Client *client, bool good)
+{
+       u32 simulation_distance;
+       s32 server_seed;
+
+       if (! (read_u32(client->fd, &simulation_distance) && read_s32(client->fd, &server_seed)))
+               return false;
+
+       if (good) {
+               client_map_set_simulation_distance(simulation_distance);
+               seed = server_seed;
+       }
+
+       return true;
+}
+
+static bool setpos_handler(Client *client, bool good)
+{
+       v3f64 pos;
+
+       if (! read_v3f64(client->fd, &pos))
+               return false;
+
+       if (good)
+               client_player_set_position(pos);
+
+       return true;
+}
+
+static bool timeofday_handler(Client *client, bool good)
+{
+       u64 time_of_day;
+
+       if (! read_u64(client->fd, &time_of_day))
+               return false;
+
+       if (good)
+               set_time_of_day(time_of_day);
+
+       return true;
+}
+
 CommandHandler command_handlers[CLIENT_COMMAND_COUNT] = {
        {0},
        {&disconnect_handler, "DISCONNECT", CS_CREATED | CS_AUTH | CS_ACTIVE},
        {&auth_handler, "AUTH", CS_AUTH},
        {&block_handler, "BLOCK", CS_ACTIVE},
+       {&info_handler, "INFO", CS_ACTIVE},
+       {&setpos_handler, "SETPOS", CS_ACTIVE},
+       {&timeofday_handler, "TIMEOFDAY", CS_ACTIVE},
 };