]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/client/client_commands.c
cf13c0fde9b261154113fafa6929d5a59e88432a
[dragonblocks_alpha.git] / src / client / client_commands.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <dragontype/number.h>
4 #include "client/client.h"
5 #include "client/client_map.h"
6 #include "client/client_player.h"
7 #include "day.h"
8 #include "perlin.h"
9 #include "util.h"
10
11 static bool disconnect_handler(unused Client *client, bool good)
12 {
13         if (good)
14                 client_disconnect(false, NULL);
15         return true;
16 }
17
18 static bool auth_handler(Client *client, bool good)
19 {
20         u8 success;
21         if (! read_u8(client->fd, &success))
22                 return false;
23
24         if (! good)
25                 return true;
26
27         if (success) {
28                 printf("Authenticated successfully\n");
29                 client->state = CS_ACTIVE;
30         } else {
31                 printf("Authentication failed, please try again\n");
32                 client->state = CS_CREATED;
33         }
34
35         return true;
36 }
37
38 static bool block_handler(Client *client, bool good)
39 {
40         v3s32 pos;
41
42         if (! read_v3s32(client->fd, &pos))
43                 return false;
44
45         size_t size;
46
47         if (! read_u64(client->fd, &size))
48                 return false;
49
50         if (size > sizeof(MapBlockData))        // guard to prevent malicious or malfunctioning packets from allocating huge unnecessary amounts of memory
51                 return false;
52
53         char data[size];
54         if (! read_full(client->fd, data, size))
55                 return false;
56
57         MapBlock *block;
58
59         if (good)
60                 block = map_get_block(client_map.map, pos, true);
61         else
62                 block = map_allocate_block(pos);
63
64         bool ret = map_deserialize_block(block, data, size);
65
66         if (good)
67                 client_map_block_received(block);
68         else
69                 map_free_block(block);
70
71         return ret;
72 }
73
74 static bool info_handler(Client *client, bool good)
75 {
76         u32 simulation_distance;
77         s32 server_seed;
78
79         if (! (read_u32(client->fd, &simulation_distance) && read_s32(client->fd, &server_seed)))
80                 return false;
81
82         if (good) {
83                 client_map_set_simulation_distance(simulation_distance);
84                 seed = server_seed;
85         }
86
87         return true;
88 }
89
90 static bool setpos_handler(Client *client, bool good)
91 {
92         v3f64 pos;
93
94         if (! read_v3f64(client->fd, &pos))
95                 return false;
96
97         if (good)
98                 client_player_set_position(pos);
99
100         return true;
101 }
102
103 static bool timeofday_handler(Client *client, bool good)
104 {
105         u64 time_of_day;
106
107         if (! read_u64(client->fd, &time_of_day))
108                 return false;
109
110         if (good)
111                 set_time_of_day(time_of_day);
112
113         return true;
114 }
115
116 CommandHandler command_handlers[CLIENT_COMMAND_COUNT] = {
117         {0},
118         {&disconnect_handler, "DISCONNECT", CS_CREATED | CS_AUTH | CS_ACTIVE},
119         {&auth_handler, "AUTH", CS_AUTH},
120         {&block_handler, "BLOCK", CS_ACTIVE},
121         {&info_handler, "INFO", CS_ACTIVE},
122         {&setpos_handler, "SETPOS", CS_ACTIVE},
123         {&timeofday_handler, "TIMEOFDAY", CS_ACTIVE},
124 };