Pārlūkot izejas kodu

Try and fail fixing the protocol

Client crashes
Wolf Clement 3 gadi atpakaļ
vecāks
revīzija
2e4c3f93a7
6 mainītis faili ar 98 papildinājumiem un 28 dzēšanām
  1. 1 1
      soulmate.sublime-project
  2. 56 0
      src/customknight.c
  3. 28 0
      src/customknight.h
  4. 8 0
      src/player.c
  5. 2 0
      src/player.h
  6. 3 27
      src/protocol.c

+ 1 - 1
soulmate.sublime-project

@@ -3,7 +3,7 @@
     {
       "path": ".",
       "file_exclude_patterns": ["soulmate"],
-      "folder_exclude_patterns": ["assets", "lib", "obj", "skins"]
+      "folder_exclude_patterns": ["assets", "extern", "lib", "obj", "skins"],
     }
 	],
   "settings": {

+ 56 - 0
src/customknight.c

@@ -0,0 +1,56 @@
+#include "customknight.h"
+
+#include <bearssl_hash.h>
+#include <stdlib.h>
+
+#include "player.h"
+#include "protocol.h"
+
+PacketReadResult handle_texture_receive(Player* player, char* data, int length,
+                                        int pos) {
+  (void)player;
+
+  int texture_length;
+  READ_INT(texture_length);
+  if (texture_length > 20000000) {
+    // Over 20mb really ? That's going to be a 'no from me'.
+    return PLAYER_DESERVES_KICK;
+  }
+
+  uv_fs_t open_req = {0};
+  open_req.data = malloc(texture_length);
+  READ_BYTES(open_req.data, texture_length);
+
+  /*  br_sha1_context ctx = {0};
+    br_sha1_init(&ctx);
+    br_sha1_update(&ctx, open_req->data, texture_length);
+
+    unsigned char sha1[HASH_LENGTH];
+    br_sha1_out(&ctx, sha1);
+
+    char sha1_str[HASH_LENGTH * 2 + 7] = "skins/";
+    for (int i = 0; i < HASH_LENGTH; i++) {
+      sprintf(sha1_str + 6 + i * 2, "%02X", buf[i])
+    }
+    sha1_str[HASH_LENGTH * 2 + 6] = '\0';
+
+      int fd = uv_fs_open(uv_default_loop(), &open_req, sha1_str, O_CREAT,
+       O_WRONLY, save_texture);*/
+
+  /*  FILE* outfile = fopen(sha1_str, "w");
+    fwrite(open_req->data, 1, texture_length, outfile);
+    fclose(outfile);*/
+
+  return DONE_READING;
+}
+
+PacketReadResult handle_texture_request(Player* player, char* data, int length,
+                                        int pos) {
+  unsigned char sha1[HASH_LENGTH];
+  READ_BYTES(sha1, HASH_LENGTH);
+
+  // TODO: send texture
+  (void)player;
+
+  return DONE_READING;
+}

+ 28 - 0
src/customknight.h

@@ -0,0 +1,28 @@
+#pragma once
+
+// Forward declarations
+typedef enum read_packet_retval PacketReadResult;
+typedef struct player_s Player;
+
+#define HASH_LENGTH 20
+
+typedef enum texture_type_e {
+  BALDUR_TEXTURE,
+  FLUKE_TEXTURE,
+  GRIMM_TEXTURE,
+  HATCHLING_TEXTURE,
+  KNIGHT_TEXTURE,
+  SHIELD_TEXTURE,
+  SPRINT_TEXTURE,
+  UNN_TEXTURE,
+  VOID_TEXTURE,
+  VS_TEXTURE,
+  WEAVER_TEXTURE,
+  WRAITHS_TEXTURE,
+  NB_TEXTURES
+} TextureType;
+
+PacketReadResult handle_texture_receive(Player* player, char* data, int length,
+                                        int pos);
+PacketReadResult handle_texture_request(Player* player, char* data, int length,
+                                        int pos);

+ 8 - 0
src/player.c

@@ -3,6 +3,7 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "customknight.h"
 #include "server.h"
 #include "tcp.h"
 
@@ -18,6 +19,13 @@ void make_player_info_packet(Player* player, char packet[MAX_PACKET_LENGTH],
   for (int i = 0; i < NB_CHARMS; i++) {
     WRITE_BOOL(player->charms[i]);
   }
+
+  // TODO: send actual hashes
+  unsigned char fake_hash[HASH_LENGTH] = {0};
+  for (int i = 0; i < NB_TEXTURES; i++) {
+    WRITE_BYTES(fake_hash, HASH_LENGTH);
+  }
+
   *length = pos;
 }
 

+ 2 - 0
src/player.h

@@ -1,6 +1,7 @@
 #pragma once
 #include <uv.h>
 
+#include "customknight.h"
 #include "misc.h"
 #include "protocol.h"
 
@@ -22,6 +23,7 @@ typedef struct player_s {
   int max_health;
   int blue_health;
   bool charms[NB_CHARMS];
+  unsigned char texture_hashes[HASH_LENGTH * NB_TEXTURES];
 } Player;
 
 void make_player_info_packet(Player* player, char packet[MAX_PACKET_LENGTH],

+ 3 - 27
src/protocol.c

@@ -1,10 +1,10 @@
 #include "protocol.h"
 
-#include <bearssl_hash.h>
 #include <stdlib.h>
 #include <string.h>
 #include <time.h>
 
+#include "customknight.h"
 #include "player.h"
 #include "server.h"
 #include "tcp.h"
@@ -63,37 +63,13 @@ PacketReadResult read_tcp_packet(Player* player) {
     //
     // -> When the player sends a texture
     case C_PACKET_TEXTURE_FRAGMENT: {
-      int texture_length;
-      READ_INT(texture_length);
-      if (texture_length > 20000000) {
-        // Over 20mb really ? That's going to be a 'no from me'.
-        return PLAYER_DESERVES_KICK;
-      }
-
-      unsigned char texture[texture_length];
-      READ_BYTES(texture, texture_length);
-
-      br_sha1_context ctx = {0};
-      br_sha1_init(&ctx);
-      br_sha1_update(&ctx, texture, texture_length);
-
-      unsigned char sha1[20];
-      br_sha1_out(&ctx, sha1);
-
-      // TODO: store texture
-
-      return DONE_READING;
+      return handle_texture_receive(player, data, length, pos);
     }
 
     //
     // -> When the player requests a texture
     case S_PACKET_TEXTURE_REQUEST: {
-      unsigned char sha1[20];
-      READ_BYTES(sha1, 20);
-
-      // TODO: send texture
-
-      return DONE_READING;
+      return handle_texture_request(player, data, length, pos);
     }
 
     //