]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/client/client_commands.c
Rework mapblock loading system and add mapgen stage buffer, 64bit floats for player...
[dragonblocks_alpha.git] / src / client / client_commands.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include "client/client.h"
4 #include "client/client_map.h"
5 #include "types.h"
6
7 static bool disconnect_handler(__attribute__((unused)) Client *client, bool good)
8 {
9         if (good)
10                 client_disconnect(false, NULL);
11         return true;
12 }
13
14 static bool auth_handler(Client *client, bool good)
15 {
16         u8 success;
17         if (! read_u8(client->fd, &success))
18                 return false;
19
20         if (! good)
21                 return true;
22
23         if (success) {
24                 printf("Authenticated successfully\n");
25                 client->state = CS_ACTIVE;
26         } else {
27                 printf("Authentication failed, please try again\n");
28                 client->state = CS_CREATED;
29         }
30
31         return true;
32 }
33
34 static bool block_handler(Client *client, bool good)
35 {
36         v3s32 pos;
37
38         if (! read_v3s32(client->fd, &pos))
39                 return false;
40
41         size_t size;
42
43         if (! read_u64(client->fd, &size))
44                 return false;
45
46         if (size > sizeof(MapBlockData))        // guard to prevent malicious or malfunctioning packets from allocating huge unnecessary amounts of memory
47                 return false;
48
49         char data[size];
50         if (! read_full(client->fd, data, size))
51                 return false;
52
53         MapBlock *block;
54
55         if (good)
56                 block = map_get_block(client_map.map, pos, true);
57         else
58                 block = map_allocate_block(pos);
59
60         map_clear_meta(block);
61
62         bool ret = map_deserialize_block(block, data, size);
63
64         if (good)
65                 client_map_block_received(block);
66         else
67                 map_free_block(block);
68
69         return ret;
70 }
71
72 static bool simulation_distance_handler(Client *client, bool good)
73 {
74         u32 simulation_distance;
75
76         if (! read_u32(client->fd, &simulation_distance))
77                 return false;
78
79         if (good)
80                 client_map_set_simulation_distance(simulation_distance);
81
82         return true;
83 }
84
85 CommandHandler command_handlers[CLIENT_COMMAND_COUNT] = {
86         {0},
87         {&disconnect_handler, "DISCONNECT", CS_CREATED | CS_AUTH | CS_ACTIVE},
88         {&auth_handler, "AUTH", CS_AUTH},
89         {&block_handler, "BLOCK", CS_ACTIVE},
90         {&simulation_distance_handler, "SIMULATION_DISTANCE", CS_ACTIVE},
91 };