]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/client/client.c
Add items
[dragonblocks_alpha.git] / src / client / client.c
1 #define _GNU_SOURCE // don't worry, GNU extensions are only used when available
2 #include <dragonstd/flag.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <pthread.h>
7 #include <unistd.h>
8 #include "client/client.h"
9 #include "client/client_auth.h"
10 #include "client/client_inventory.h"
11 #include "client/client_player.h"
12 #include "client/client_terrain.h"
13 #include "client/debug_menu.h"
14 #include "client/game.h"
15 #include "client/input.h"
16 #include "day.h"
17 #include "interrupt.h"
18 #include "perlin.h"
19 #include "types.h"
20
21 DragonnetPeer *client;
22
23 static Flag finish;
24 static Flag gfx_init;
25
26 static bool on_recv(__attribute__((unused)) DragonnetPeer *peer, DragonnetTypeId type, __attribute__((unused)) void *pkt)
27 {
28         bool allowed = false;
29         pthread_mutex_lock(&client_auth.mtx);
30
31         // this code exists to stop malicious or malfunctioning packets
32         switch (client_auth.state) {
33                 // the server shouldn't send anything during auth preparation, drop it
34                 case AUTH_INIT:
35                         allowed = false;
36                         break;
37
38                 // only the auth packet is allowed before auth is finished
39                 case AUTH_WAIT:
40                         allowed = type == DRAGONNET_TYPE_ToClientAuth;
41                         break;
42
43                 // don't process auth packets when auth is already finished
44                 case AUTH_SUCCESS:
45                         allowed = type != DRAGONNET_TYPE_ToClientAuth;
46                         break;
47         }
48
49         /*
50                 It is important that the auth state does not change to until the packet is
51                         processed.
52
53                 However, the only state change done by other threads is AUTH_INIT -> AUTH_WAIT,
54                         which is not problematic since packets that are received during AUTH_INIT
55                         are not processed, they are always dropped.
56
57                 Therefore the mutex can be unlocked at this point.
58         */
59         pthread_mutex_unlock(&client_auth.mtx);
60         return allowed;
61 }
62
63 static void on_disconnect(__attribute__((unused)) DragonnetPeer *peer)
64 {
65         flag_set(&interrupt);
66         // don't free the connection before all other client components have shut down
67         flag_slp(&finish);
68 }
69
70 static void on_ToClientAuth(__attribute__((unused)) DragonnetPeer *peer, ToClientAuth *pkt)
71 {
72         pthread_mutex_lock(&client_auth.mtx);
73         if (pkt->success) {
74                 client_auth.state = AUTH_SUCCESS;
75                 printf("[access] authenticated successfully\n");
76         } else {
77                 client_auth.state = AUTH_INIT;
78                 printf("[access] authentication failed, please try again\n");
79         }
80         pthread_cond_signal(&client_auth.cv);
81         pthread_mutex_unlock(&client_auth.mtx);
82
83         // yield the connection until the game is fully initialized
84         if (pkt->success)
85                 flag_slp(&gfx_init);
86 }
87
88 static void on_ToClientChunk(__attribute__((unused)) DragonnetPeer *peer, ToClientChunk *pkt)
89 {
90         TerrainChunk *chunk = terrain_get_chunk(client_terrain, pkt->pos, true);
91
92         terrain_deserialize_chunk(chunk, pkt->data);
93         ((TerrainChunkMeta *) chunk->extra)->empty = (pkt->data.siz == 0);
94         client_terrain_chunk_received(chunk);
95 }
96
97 static void on_ToClientInfo(__attribute__((unused)) DragonnetPeer *peer, ToClientInfo *pkt)
98 {
99         client_terrain_set_load_distance(pkt->load_distance);
100         seed = pkt->seed;
101 }
102
103 static void on_ToClientTimeOfDay(__attribute__((unused)) DragonnetPeer *peer, ToClientTimeOfDay *pkt)
104 {
105         set_time_of_day(pkt->time_of_day);
106 }
107
108 static void on_ToClientMovement(__attribute__((unused)) DragonnetPeer *peer, ToClientMovement *pkt)
109 {
110         pthread_rwlock_wrlock(&client_player.lock_movement);
111         client_player.movement = *pkt;
112         pthread_rwlock_unlock(&client_player.lock_movement);
113
114         debug_menu_changed(ENTRY_FLIGHT);
115         debug_menu_changed(ENTRY_COLLISION);
116 }
117
118 int main(int argc, char **argv)
119 {
120 #ifdef __GLIBC__ // check whether bloat is enabled
121         pthread_setname_np(pthread_self(), "main");
122 #endif // __GLIBC__
123
124         if (argc < 2) {
125                 fprintf(stderr, "[error] missing address\n");
126                 return EXIT_FAILURE;
127         }
128
129         if (!(client = dragonnet_connect(argv[1]))) {
130                 fprintf(stderr, "[error] failed to connect to server\n");
131                 return EXIT_FAILURE;
132         }
133
134         printf("[access] connected to %s\n", client->address);
135
136         client->on_disconnect = &on_disconnect;
137         client->on_recv                                                  = (void *) &on_recv;
138         client->on_recv_type[DRAGONNET_TYPE_ToClientAuth               ] = (void *) &on_ToClientAuth;
139         client->on_recv_type[DRAGONNET_TYPE_ToClientChunk              ] = (void *) &on_ToClientChunk;
140         client->on_recv_type[DRAGONNET_TYPE_ToClientInfo               ] = (void *) &on_ToClientInfo;
141         client->on_recv_type[DRAGONNET_TYPE_ToClientTimeOfDay          ] = (void *) &on_ToClientTimeOfDay;
142         client->on_recv_type[DRAGONNET_TYPE_ToClientMovement           ] = (void *) &on_ToClientMovement;
143         client->on_recv_type[DRAGONNET_TYPE_ToClientEntityAdd          ] = (void *) &client_entity_add;
144         client->on_recv_type[DRAGONNET_TYPE_ToClientEntityRemove       ] = (void *) &client_entity_remove;
145         client->on_recv_type[DRAGONNET_TYPE_ToClientEntityUpdatePosRot ] = (void *) &client_entity_update_pos_rot;
146         client->on_recv_type[DRAGONNET_TYPE_ToClientEntityUpdateNametag] = (void *) &client_entity_update_nametag;
147         client->on_recv_type[DRAGONNET_TYPE_ToClientPlayerInventory    ] = (void *) &client_inventory_update_player;
148
149         flag_ini(&finish);
150         flag_ini(&gfx_init);
151
152         interrupt_init();
153         client_terrain_init();
154         client_player_init();
155         client_entity_init();
156         dragonnet_peer_run(client);
157
158         if (!client_auth_init())
159                 return EXIT_FAILURE;
160
161         if (!game(&gfx_init))
162                 return EXIT_FAILURE;
163
164         dragonnet_peer_shutdown(client);
165         client_auth_deinit();
166         client_entity_deinit();
167         client_player_deinit();
168         client_terrain_deinit();
169         interrupt_deinit();
170
171         pthread_t recv_thread = client->recv_thread;
172
173         flag_set(&finish);
174         pthread_join(recv_thread, NULL);
175
176         flag_dst(&finish);
177         flag_dst(&gfx_init);
178
179         return EXIT_SUCCESS;
180 }