Sfoglia il codice sorgente

It looks like the file parsing works.

Eiyeron Fulmincendii 10 anni fa
parent
commit
98d2b81987
4 ha cambiato i file con 34 aggiunte e 11 eliminazioni
  1. 1 1
      Makefile
  2. 4 0
      src/engine/main.cpp
  3. 25 8
      src/piaf/Archive.cpp
  4. 4 2
      src/piaf/Archive.h

+ 1 - 1
Makefile

@@ -14,7 +14,7 @@ CFLAGS = $(CFLAGS_COMMON) -std=gnu11
 
 CPPFLAGS = $(CFLAGS_COMMON) -std=gnu++11
 
-LDFLAGS = $(CFLAGS_COMMON) -Wl,--gc-sections
+LDFLAGS = $(CFLAGS_COMMON) -Wl,--gc-sections -lz
 
 SRCS_C :=
 SRCS_CPP :=

+ 4 - 0
src/engine/main.cpp

@@ -5,8 +5,10 @@
 #include "map/Map.h"
 #include "map/StateMap.h"
 #include "utility/misc.h"
+#include "piaf/Archive.h"
 
 using namespace WalrusRPG;
+using WalrusRPG::PIAF::Archive;
 
 int main(int argc, char *argv[])
 {
@@ -17,6 +19,8 @@ int main(int argc, char *argv[])
     Timing::init();
     Quirks::init();
 
+    // Archive arc("samples/one_file.wrf");
+
     uint16_t dungeonTest[] = {
         21, 21,  1,   1,   1,   1,   21,  22,  21,  22, 21,  22,  21,  21,  1,   22,  21,
         1,  22,  22,  22,  1,   21,  2,   3,   3,   3,  3,   3,   4,   21,  1,   22,  21,

+ 25 - 8
src/piaf/Archive.cpp

@@ -26,6 +26,7 @@ namespace
         {
             char *current_entry_data = &data[index * 24];
             memcpy(entries[index].filename, current_entry_data, 8);
+            entries[index].filename[8] = '\0'; // Makin' sure.
             entries[index].file_type =
                 (FileType) read_big_endian_value<uint32_t>(&current_entry_data[8]);
             entries[index].compression_type =
@@ -39,16 +40,22 @@ namespace
     }
 }
 
-Archive::Archive(char *filepath)
+Archive::Archive(std::string &filepath) : Archive(filepath.c_str())
+{
+}
+
+Archive::Archive(const char *filepath) : file(nullptr), entries(nullptr)
 {
     if (filepath == nullptr)
     {
+        // TODO : throw NPE
+        //fprintf(stderr, "Null filepath\n");
     }
-    // TODO : throw NPE
     file = fopen(filepath, "rb");
     if (file == nullptr)
     {
         // TODO : throw Couldn't open
+        //fprintf(stderr, "Unable to open %s\n", filepath);
     }
     // loading stuff happens NOW
     // checking if the file is long enough to have a header
@@ -58,6 +65,7 @@ Archive::Archive(char *filepath)
     if (filesize < 32)
     {
         // TODO : throw file too small
+        //fprintf(stderr, "File too small\n");
     }
 
     char header_container[32] = {0};
@@ -65,11 +73,14 @@ Archive::Archive(char *filepath)
     if (strncmp(header_container, "WRPGPIAF", 8) != 0)
     {
         // TODO throw bad header
+        //fprintf(stderr, "Bad header magic word\n");
     }
     uint32_t expected_checksum = read_big_endian_value<uint32_t>(&header_container[8]);
-    if (expected_checksum != crc32(0L, (unsigned char *) header_container, 32))
+    uint32_t calculated_checksum = crc32(0L, (unsigned char *) &header_container[16], 16);
+    if (expected_checksum != calculated_checksum)
     {
         // TODO throw bad checksum
+        //fprintf(stderr, "Bad header checksum : %x != %x\n", expected_checksum, calculated_checksum);
     }
 
     // TODO : version checking
@@ -81,24 +92,28 @@ Archive::Archive(char *filepath)
      * }
      */
     nb_files = read_big_endian_value<uint32_t>(&header_container[20]);
+    // printf("nb_files : %u\n", nb_files);
 
     data_size = read_big_endian_value<uint32_t>(&header_container[24]);
-    if (data_size != filesize - 32 - 24 * nb_files)
+    uint64_t calculated_data_size = filesize - 32 - 24 * nb_files;
+    if (data_size != calculated_data_size)
     {
         // T0D0 : throw wrong size exception
+        // fprintf(stderr, "Bad data size : expected %u, got %lld\n", data_size, calculated_data_size);
     }
     if (nb_files != 0)
     {
         uint32_t expected_filetable_checksum =
             read_big_endian_value<uint32_t>(&header_container[12]);
         char *file_entry_data = new char[24 * nb_files];
+        fseek(file, 32, SEEK_SET);
+        fread(file_entry_data, sizeof(char), 24 * nb_files, file);
         if (expected_filetable_checksum !=
             crc32(0L, (unsigned char *) file_entry_data, 24 * nb_files))
         {
             // TODO : checksum exception
+            // fprintf(stderr, "Bad filetable checksum\n");
         }
-        fseek(file, 32, SEEK_SET);
-        fread(file_entry_data, sizeof(char), 24 * nb_files, file);
         entries = new FileEntry[nb_files];
         load_file_table(entries, file_entry_data, nb_files);
         delete[] file_entry_data;
@@ -107,8 +122,10 @@ Archive::Archive(char *filepath)
 
 Archive::~Archive()
 {
-    fclose(file);
-    delete[] entries;
+    if(file != nullptr)
+        fclose(file);
+    if( entries != nullptr)
+        delete[] entries;
 }
 
 FileEntry Archive::get_file_entry(char *filename)

+ 4 - 2
src/piaf/Archive.h

@@ -1,8 +1,9 @@
 #ifndef INCLUDE_ARCHIVE_H
 #define INCLUDE_ARCHIVE_H
 
-#include <fstream>
 #include <cstdint>
+#include <cstdio>
+#include <string>
 
 namespace WalrusRPG
 {
@@ -45,7 +46,8 @@ namespace WalrusRPG
 
           public:
             // RAII stuff
-            Archive(char *filepath);
+            Archive(const char *filepath);
+            Archive(std::string &filepath);
             ~Archive();
 
             FileEntry get_file_entry(char *filename);