Skip to content

Commit

Permalink
Move all the message verification functions to common
Browse files Browse the repository at this point in the history
  • Loading branch information
rexim committed Oct 9, 2024
1 parent badce2e commit 48d0f0d
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 103 deletions.
88 changes: 9 additions & 79 deletions client.c3
Original file line number Diff line number Diff line change
Expand Up @@ -452,14 +452,6 @@ fn void kill_all_items(Item[]* items) {
}
}

fn ItemsCollectedBatchMessage*! verify_items_collected_batch_message(Message *message) {
if (message.byte_length < ItemsCollectedBatchMessage.sizeof) return MessageFault.INVALID?;
if ((message.byte_length - ItemsCollectedBatchMessage.sizeof)%int.sizeof != 0) return MessageFault.INVALID?;
ItemsCollectedBatchMessage* items_collected_batch_message = (ItemsCollectedBatchMessage*)message;
if (items_collected_batch_message.kind != MessageKind.ITEM_COLLECTED) return MessageFault.INVALID?;
return items_collected_batch_message;
}

fn bool apply_items_collected_batch_message(ItemsCollectedBatchMessage *message, Item[]* items) {
usz count = (message.byte_length - ItemsCollectedBatchMessage.sizeof)/int.sizeof;

Expand All @@ -482,14 +474,6 @@ fn bool apply_items_collected_batch_message(ItemsCollectedBatchMessage *message,
return true;
}

fn ItemsSpawnedBatchMessage*! verify_items_spawned_batch_message(Message *message) {
if (message.byte_length < ItemsSpawnedBatchMessage.sizeof) return MessageFault.INVALID?;
if ((message.byte_length - ItemsSpawnedBatchMessage.sizeof)%ItemSpawned.sizeof != 0) return MessageFault.INVALID?;
ItemsSpawnedBatchMessage* items_spawned_batch_message = (ItemsSpawnedBatchMessage*)message;
if (items_spawned_batch_message.kind != MessageKind.ITEM_SPAWNED) return MessageFault.INVALID?;
return items_spawned_batch_message;
}

fn bool apply_items_spawned_batch_message(ItemsSpawnedBatchMessage *message, Item[]* items) {
usz count = (message.byte_length - ItemsCollectedBatchMessage.sizeof)/ItemSpawned.sizeof;
for (usz i = 0; i < count; ++i) {
Expand Down Expand Up @@ -564,14 +548,6 @@ fn void update_bombs_on_client_side(SpritePool *sprite_pool, ParticlePool *parti
}
}

fn BombsSpawnedBatchMessage*! verify_bombs_spawned_batch_message(Message *message) {
if (message.byte_length < BombsSpawnedBatchMessage.sizeof) return MessageFault.INVALID?;
if ((message.byte_length - BombsSpawnedBatchMessage.sizeof)%BombSpawned.sizeof != 0) return MessageFault.INVALID?;
BombsSpawnedBatchMessage* bombs_spawned_batch_message = (BombsSpawnedBatchMessage*)message;
if (bombs_spawned_batch_message.kind != MessageKind.BOMB_SPAWNED) return MessageFault.INVALID?;
return bombs_spawned_batch_message;
}

fn bool apply_bombs_spawned_batch_message(BombsSpawnedBatchMessage *message, Bombs *bombs) {
usz count = (message.byte_length - BombsSpawnedBatchMessage.sizeof)/BombSpawned.sizeof;
for (usz i = 0; i < count; ++i) {
Expand All @@ -593,14 +569,6 @@ fn bool apply_bombs_spawned_batch_message(BombsSpawnedBatchMessage *message, Bom
return true;
}

fn BombsExplodedBatchMessage*! verify_bombs_exploded_batch_message(Message *message) {
if (message.byte_length < BombsExplodedBatchMessage.sizeof) return MessageFault.INVALID?;
if ((message.byte_length - BombsExplodedBatchMessage.sizeof)%BombExploded.sizeof != 0) return MessageFault.INVALID?;
BombsExplodedBatchMessage* bombs_exploded_batch_message = (BombsExplodedBatchMessage*)message;
if (bombs_exploded_batch_message.kind != MessageKind.BOMB_EXPLODED) return MessageFault.INVALID?;
return bombs_exploded_batch_message;
}

fn bool apply_bombs_exploded_batch_message(BombsExplodedBatchMessage *message, Bombs *bombs, ParticlePool *particle_pool) {
usz count = (message.byte_length - BombsExplodedBatchMessage.sizeof)/BombExploded.sizeof;
for (usz i = 0; i < count; ++i) {
Expand Down Expand Up @@ -645,13 +613,6 @@ const Control[*] CONTROL_KEYS = {
{83, MOVING_BACKWARD},
};

fn HelloMessage*! verify_hello_message(Message *message) {
if (message.byte_length != HelloMessage.sizeof) return MessageFault.INVALID?;
HelloMessage* hello_message = (HelloMessage*)message;
if (hello_message.kind != HELLO) return MessageFault.INVALID?;
return hello_message;
}

fn bool apply_hello_message_to_me(HelloMessage *hello_message, Item[]* items) {
// TODO: maybe we should reset everything (bombs, etc) on hello message
// So to let the server recreate the world properly
Expand All @@ -665,14 +626,6 @@ fn bool apply_hello_message_to_me(HelloMessage *hello_message, Item[]* items) {
return true;
}

fn PlayersJoinedBatchMessage*! verify_players_joined_batch_message(Message *message) {
if (message.byte_length < PlayersJoinedBatchMessage.sizeof) return MessageFault.INVALID?;
if ((message.byte_length - PlayersJoinedBatchMessage.sizeof)%PlayerStruct.sizeof != 0) return MessageFault.INVALID?;
PlayersJoinedBatchMessage* players_joined_batch_message = (PlayersJoinedBatchMessage*)message;
if (players_joined_batch_message.kind != PLAYER_JOINED) return MessageFault.INVALID?;
return players_joined_batch_message;
}

fn void apply_players_joined_batch_message(PlayersJoinedBatchMessage *message) {
usz count = (message.byte_length - PlayersJoinedBatchMessage.sizeof)/PlayerStruct.sizeof;
for (usz i = 0; i < count; ++i) {
Expand Down Expand Up @@ -703,29 +656,13 @@ fn void apply_players_joined_batch_message(PlayersJoinedBatchMessage *message) {
}
}

fn PlayersLeftBatchMessage*! verify_players_left_batch_message(Message *message) {
if (message.byte_length < PlayersLeftBatchMessage.sizeof) return MessageFault.INVALID?;
if ((message.byte_length - PlayersLeftBatchMessage.sizeof)%uint.sizeof != 0) return MessageFault.INVALID?;
PlayersLeftBatchMessage* players_left_batch_message = (PlayersLeftBatchMessage*)message;
if (players_left_batch_message.kind != PLAYER_LEFT) return MessageFault.INVALID?;
return players_left_batch_message;
}

fn void apply_players_left_batch_message(PlayersLeftBatchMessage *message) {
usz count = (message.byte_length - PlayersLeftBatchMessage.sizeof)/uint.sizeof;
for (usz i = 0; i < count; ++i) {
other_players.remove(message.ids[i]);
}
}

fn PlayersMovingBatchMessage*! verify_players_moving_batch_message(Message *message) {
if (message.byte_length < PlayersMovingBatchMessage.sizeof) return MessageFault.INVALID?;
if ((message.byte_length - PlayersMovingBatchMessage.sizeof)%PlayerStruct.sizeof != 0) return MessageFault.INVALID?;
PlayersMovingBatchMessage* players_moving_batch_message = (PlayersMovingBatchMessage*)message;
if (players_moving_batch_message.kind != PLAYER_MOVING) return MessageFault.INVALID?;
return players_moving_batch_message;
}

fn bool apply_players_moving_batch_message(PlayersMovingBatchMessage *message) {
usz count = (message.byte_length - PlayersMovingBatchMessage.sizeof)/PlayerStruct.sizeof;
for (usz i = 0; i < count; ++i) {
Expand Down Expand Up @@ -887,13 +824,6 @@ fn void ping_server_if_needed() {
}
}

fn PongMessage*! verify_pong_message(Message *message) {
if (message.byte_length != PongMessage.sizeof) return MessageFault.INVALID?;
PongMessage* pong_message = (PongMessage*)message;
if (pong_message.kind != PONG) return MessageFault.INVALID?;
return pong_message;
}

uint ping = 0;
fn void process_pong_message(PongMessage *message) {
ping = platform_now_msecs() - message.timestamp;
Expand All @@ -904,47 +834,47 @@ fn uint ping_msecs() @extern("ping_msecs") @wasm {
}

fn bool process_message(Message *unknown_message) @extern("process_message") @wasm {
if (try message = verify_hello_message(unknown_message)) {
if (try message = common::verify_hello_message(unknown_message)) {
apply_hello_message_to_me(message, &common::items);
return true;
}
if (try message = verify_players_joined_batch_message(unknown_message)) {
if (try message = common::verify_players_joined_batch_message(unknown_message)) {
apply_players_joined_batch_message(message);
return true;
}
if (try message = verify_players_left_batch_message(unknown_message)) {
if (try message = common::verify_players_left_batch_message(unknown_message)) {
apply_players_left_batch_message(message);
return true;
}
if (try message = verify_players_moving_batch_message(unknown_message)) {
if (try message = common::verify_players_moving_batch_message(unknown_message)) {
if (!apply_players_moving_batch_message(message)) {
return false;
}
return true;
}
if (try message = verify_pong_message(unknown_message)) {
if (try message = common::verify_pong_message(unknown_message)) {
process_pong_message(message);
return true;
}
if (try message = verify_items_collected_batch_message(unknown_message)) {
if (try message = common::verify_items_collected_batch_message(unknown_message)) {
if (!apply_items_collected_batch_message(message, &common::items)) {
return false;
}
return true;
}
if (try message = verify_items_spawned_batch_message(unknown_message)) {
if (try message = common::verify_items_spawned_batch_message(unknown_message)) {
if (!apply_items_spawned_batch_message(message, &common::items)) {
return false;
}
return true;
}
if (try message = verify_bombs_spawned_batch_message(unknown_message)) {
if (try message = common::verify_bombs_spawned_batch_message(unknown_message)) {
if (!apply_bombs_spawned_batch_message(message, &common::bombs)) {
return false;
}
return true;
}
if (try message = verify_bombs_exploded_batch_message(unknown_message)) {
if (try message = common::verify_bombs_exploded_batch_message(unknown_message)) {
if (!apply_bombs_exploded_batch_message(message, &common::bombs, &particle_pool)) {
return false;
}
Expand Down
Binary file modified client.wasm
Binary file not shown.
91 changes: 91 additions & 0 deletions common.c3
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,14 @@ struct ItemsSpawnedBatchMessage @packed {
ItemSpawned[*] items;
}

fn ItemsSpawnedBatchMessage*! verify_items_spawned_batch_message(Message *message) {
if (message.byte_length < ItemsSpawnedBatchMessage.sizeof) return MessageFault.INVALID?;
if ((message.byte_length - ItemsSpawnedBatchMessage.sizeof)%ItemSpawned.sizeof != 0) return MessageFault.INVALID?;
ItemsSpawnedBatchMessage* items_spawned_batch_message = (ItemsSpawnedBatchMessage*)message;
if (items_spawned_batch_message.kind != MessageKind.ITEM_SPAWNED) return MessageFault.INVALID?;
return items_spawned_batch_message;
}

fn ItemsSpawnedBatchMessage* reconstruct_state_of_items(Item[] *items) {
usz itemsCount = 0;
foreach (&item: *items) {
Expand Down Expand Up @@ -225,6 +233,14 @@ struct ItemsCollectedBatchMessage @packed {
int[*] ids;
}

fn ItemsCollectedBatchMessage*! verify_items_collected_batch_message(Message *message) {
if (message.byte_length < ItemsCollectedBatchMessage.sizeof) return MessageFault.INVALID?;
if ((message.byte_length - ItemsCollectedBatchMessage.sizeof)%int.sizeof != 0) return MessageFault.INVALID?;
ItemsCollectedBatchMessage* items_collected_batch_message = (ItemsCollectedBatchMessage*)message;
if (items_collected_batch_message.kind != MessageKind.ITEM_COLLECTED) return MessageFault.INVALID?;
return items_collected_batch_message;
}

/// Bombs //////////////////////////////

struct Bomb {
Expand Down Expand Up @@ -307,6 +323,14 @@ struct BombsSpawnedBatchMessage @packed {
BombSpawned[*] bombs;
}

fn BombsSpawnedBatchMessage*! verify_bombs_spawned_batch_message(Message *message) {
if (message.byte_length < BombsSpawnedBatchMessage.sizeof) return MessageFault.INVALID?;
if ((message.byte_length - BombsSpawnedBatchMessage.sizeof)%BombSpawned.sizeof != 0) return MessageFault.INVALID?;
BombsSpawnedBatchMessage* bombs_spawned_batch_message = (BombsSpawnedBatchMessage*)message;
if (bombs_spawned_batch_message.kind != MessageKind.BOMB_SPAWNED) return MessageFault.INVALID?;
return bombs_spawned_batch_message;
}

struct BombExploded @packed {
uint bombIndex;
float x;
Expand All @@ -320,6 +344,14 @@ struct BombsExplodedBatchMessage @packed {
BombExploded[*] bombs;
}

fn BombsExplodedBatchMessage*! verify_bombs_exploded_batch_message(Message *message) {
if (message.byte_length < BombsExplodedBatchMessage.sizeof) return MessageFault.INVALID?;
if ((message.byte_length - BombsExplodedBatchMessage.sizeof)%BombExploded.sizeof != 0) return MessageFault.INVALID?;
BombsExplodedBatchMessage* bombs_exploded_batch_message = (BombsExplodedBatchMessage*)message;
if (bombs_exploded_batch_message.kind != MessageKind.BOMB_EXPLODED) return MessageFault.INVALID?;
return bombs_exploded_batch_message;
}

/// Player //////////////////////////////

enum Moving: char {
Expand Down Expand Up @@ -355,18 +387,42 @@ struct PlayersJoinedBatchMessage @packed {
PlayerStruct[*] players;
}

fn PlayersJoinedBatchMessage*! verify_players_joined_batch_message(Message *message) {
if (message.byte_length < PlayersJoinedBatchMessage.sizeof) return MessageFault.INVALID?;
if ((message.byte_length - PlayersJoinedBatchMessage.sizeof)%PlayerStruct.sizeof != 0) return MessageFault.INVALID?;
PlayersJoinedBatchMessage* players_joined_batch_message = (PlayersJoinedBatchMessage*)message;
if (players_joined_batch_message.kind != PLAYER_JOINED) return MessageFault.INVALID?;
return players_joined_batch_message;
}

struct PlayersLeftBatchMessage @packed {
uint byte_length;
MessageKind kind;
uint[*] ids;
}

fn PlayersLeftBatchMessage*! verify_players_left_batch_message(Message *message) {
if (message.byte_length < PlayersLeftBatchMessage.sizeof) return MessageFault.INVALID?;
if ((message.byte_length - PlayersLeftBatchMessage.sizeof)%uint.sizeof != 0) return MessageFault.INVALID?;
PlayersLeftBatchMessage* players_left_batch_message = (PlayersLeftBatchMessage*)message;
if (players_left_batch_message.kind != PLAYER_LEFT) return MessageFault.INVALID?;
return players_left_batch_message;
}

struct PlayersMovingBatchMessage @packed {
uint byte_length;
MessageKind kind;
PlayerStruct[*] players;
}

fn PlayersMovingBatchMessage*! verify_players_moving_batch_message(Message *message) {
if (message.byte_length < PlayersMovingBatchMessage.sizeof) return MessageFault.INVALID?;
if ((message.byte_length - PlayersMovingBatchMessage.sizeof)%PlayerStruct.sizeof != 0) return MessageFault.INVALID?;
PlayersMovingBatchMessage* players_moving_batch_message = (PlayersMovingBatchMessage*)message;
if (players_moving_batch_message.kind != PLAYER_MOVING) return MessageFault.INVALID?;
return players_moving_batch_message;
}

struct HelloMessage @packed {
uint byte_length;
MessageKind kind;
Expand All @@ -377,30 +433,65 @@ struct HelloMessage @packed {
char hue;
}

fn HelloMessage*! verify_hello_message(Message *message) {
if (message.byte_length != HelloMessage.sizeof) return MessageFault.INVALID?;
HelloMessage* hello_message = (HelloMessage*)message;
if (hello_message.kind != HELLO) return MessageFault.INVALID?;
return hello_message;
}

struct PongMessage @packed {
uint byte_length;
MessageKind kind;
uint timestamp;
}

fn PongMessage*! verify_pong_message(Message *message) {
if (message.byte_length != PongMessage.sizeof) return MessageFault.INVALID?;
PongMessage* pong_message = (PongMessage*)message;
if (pong_message.kind != PONG) return MessageFault.INVALID?;
return pong_message;
}

struct AmmaMovingMessage @packed {
uint byte_length;
MessageKind kind;
Moving direction;
char start;
}

fn AmmaMovingMessage*! verify_amma_moving_message(Message *message) {
if (message.byte_length != AmmaMovingMessage.sizeof) return MessageFault.INVALID?;
AmmaMovingMessage* amma_moving_message = (AmmaMovingMessage*)message;
if (amma_moving_message.kind != AMMA_MOVING) return MessageFault.INVALID?;
return amma_moving_message;
}

struct AmmaThrowingMessage @packed {
uint byte_length;
MessageKind kind;
}

fn AmmaThrowingMessage*! verify_amma_throwing_message(Message *message) {
if (message.byte_length != AmmaThrowingMessage.sizeof) return MessageFault.INVALID?;
AmmaThrowingMessage* amma_throwing_message = (AmmaThrowingMessage*)message;
if (amma_throwing_message.kind != AMMA_THROWING) return MessageFault.INVALID?;
return amma_throwing_message;
}

struct PingMessage {
uint byte_length;
MessageKind kind;
uint timestamp;
}

fn PingMessage*! verify_ping_message(Message *message) {
if (message.byte_length != PingMessage.sizeof) return MessageFault.INVALID?;
PingMessage* ping_message = (PingMessage*)message;
if (ping_message.kind != PING) return MessageFault.INVALID?;
return ping_message;
}

fn void update_player(Player *player, Scene *scene, float delta_time) {
Vector2 control_velocity = {0, 0};
float angular_velocity = 0.0;
Expand Down
Loading

0 comments on commit 48d0f0d

Please sign in to comment.