<commit_before>//===----------------------------------------------------------------------===//
//
//                     The Descent map loader
//
// NAME         : Rdl
// PURPOSE      : Providing a decoder and wrapper for the Descent .RDL format.
// COPYRIGHT    : (c) 2011 <NAME>. All Rights Reserved.
// AUTHORS      : <NAME> (<EMAIL>)
// DESCRIPTION  : A decoder for the RDL file format that is used by Parallax
//                Software in the computer game, Descent.
//
//                The file format is as follows:
//
//                - Magic number which is 4 bytes and corresponding to the
//                  string "LVLP"

//                - A series of files which are preceded by a short header,
//                  which describes the name of the file and the numer of bytes.
//
//                 | "DLVLP - 3 bytes
//                 |---------------- Start of the first file
//                 | filename - 13 bytes
//                 | size - 4 bytes
//                 | data - the size of this part is by the size before it.
//                 |---------------- The next header/file comes straight after.
//                 | filename - 13 bytes
//                 | size - 4 bytes
//                 | data - the size of this part is by the size before it.
//
//===----------------------------------------------------------------------===//

#include "rdl.hpp"

#include <string.h>

// The 4-byte MAGIC number at the start of the file format used to identifiy the
// file as being a Descent HOG file.
static uint8_t magicRdl[4] = {'L', 'V', 'L', 'P'};

struct RdlHeader
{
  uint8_t signature[4];
  uint32_t version;
  uint32_t mineDataOffset;
  uint32_t objectsOffset;
  uint32_t fileSize;
};

//uint16_t vertexCount;
//uint16_t cubeCount;
//Vertex verticies[vertexCount];
//Cube cubes[cubeCount];

static_assert(sizeof(RdlHeader) == 20,
              "The RdlHeader structure is incorrectly packed");

RdlReader::RdlReader(const std::vector<uint8_t>& Data)
: myData(Data),
  myHeader(reinterpret_cast<const struct RdlHeader* const>(&Data.front()))
{
  printf("Version: %d\n", myHeader->version);
  printf("Mine data offset: %d\n", myHeader->mineDataOffset);
  printf("Object offset: %d\n", myHeader->objectsOffset);
}


bool RdlReader::IsValid() const
{
  if (myData.size() < sizeof(magicRdl)) return false;
  if (memcmp(magicRdl, &myData.front(), sizeof(magicRdl)) != 0) return false;

  return myHeader->fileSize == myData.size();
}

RdlReader::~RdlReader()
{
}

// TODO: Refactor this out.
inline double fixedToFloating(int32_t value) { return value / 65536.0; }

void RdlReader::DoStuff()
{
  //32-bit fixed point number, in 16:16 format
  size_t index = myHeader->mineDataOffset;
  const uint8_t version = myData[index];
  index = index + 1;

  const uint16_t vertexCount = (myData[index + 1] << 8) +  myData[index + 0];
  const uint16_t cubeCount   = (myData[index + 3] << 8) + myData[index + 2];

  index += 4; // We just read 4 bytes for the two counts.

  printf("\nVertex count: %d\n", vertexCount);
  printf("Cube count: %d\n", cubeCount);

  int32_t buffer[3];
  for (int i = 0; i < vertexCount; index += 4 * 2, ++i )
  {
    memcpy( &buffer, &myData[index], sizeof(buffer) );
    const double x = fixedToFloating(buffer[0]);
    const double y = fixedToFloating(buffer[1]);
    const double z = fixedToFloating(buffer[2]);

    printf("%f %f %f\n", x, y, z);
  }

  exit(0);
}
<commit_msg>Removed some unused code. Fixed the incrementing when iterating over the vertices. Added check to ensure the index is at the right spot after the reading the vertices.<commit_after>//===----------------------------------------------------------------------===//
//
//                     The Descent map loader
//
// NAME         : Rdl
// PURPOSE      : Providing a decoder and wrapper for the Descent .RDL format.
// COPYRIGHT    : (c) 2011 <NAME>. All Rights Reserved.
// AUTHORS      : <NAME> (<EMAIL>)
// DESCRIPTION  : A decoder for the RDL file format that is used by Parallax
//                Software in the computer game, Descent.
//
//                The file format is as follows:
//
//                - Magic number which is 4 bytes and corresponding to the
//                  string "LVLP"

//                - A series of files which are preceded by a short header,
//                  which describes the name of the file and the numer of bytes.
//
//                 | "DLVLP - 3 bytes
//                 |---------------- Start of the first file
//                 | filename - 13 bytes
//                 | size - 4 bytes
//                 | data - the size of this part is by the size before it.
//                 |---------------- The next header/file comes straight after.
//                 | filename - 13 bytes
//                 | size - 4 bytes
//                 | data - the size of this part is by the size before it.
//
//===----------------------------------------------------------------------===//

#include "rdl.hpp"

#include <assert.h>
#include <string.h>

// The 4-byte MAGIC number at the start of the file format used to identifiy the
// file as being a Descent HOG file.
static uint8_t magicRdl[4] = {'L', 'V', 'L', 'P'};

struct RdlHeader
{
  uint8_t signature[4];
  uint32_t version;
  uint32_t mineDataOffset;
  uint32_t objectsOffset;
  uint32_t fileSize;
};

//struct RdlMineData
//{
//  uint16_t vertexCount;
//  uint16_t cubeCount;
//  Vertex verticies[vertexCount]; // Vertex = { int32_t, int32_t, int32_t};
//  Cube cubes[cubeCount];
//};

static_assert(sizeof(RdlHeader) == 20,
              "The RdlHeader structure is incorrectly packed");

RdlReader::RdlReader(const std::vector<uint8_t>& Data)
: myData(Data),
  myHeader(reinterpret_cast<const struct RdlHeader* const>(&Data.front()))
{
  printf("Version: %d\n", myHeader->version);
  printf("Mine data offset: %d\n", myHeader->mineDataOffset);
  printf("Object offset: %d\n", myHeader->objectsOffset);
}


bool RdlReader::IsValid() const
{
  if (myData.size() < sizeof(magicRdl)) return false;
  if (memcmp(magicRdl, &myData.front(), sizeof(magicRdl)) != 0) return false;

  return myHeader->fileSize == myData.size();
}

RdlReader::~RdlReader()
{
}

// TODO: Refactor this out.
inline double fixedToFloating(int32_t value) { return value / 65536.0; }

void RdlReader::DoStuff()
{
  //32-bit fixed point number, in 16:16 format
  size_t index = myHeader->mineDataOffset;
  const uint8_t version = myData[index];
  index = index + 1;

  const uint16_t vertexCount = (myData[index + 1] << 8) +  myData[index + 0];
  const uint16_t cubeCount   = (myData[index + 3] << 8) + myData[index + 2];

  index += 4; // We just read 4 bytes for the two counts.

  printf("Vertex count: %d\n", vertexCount);
  printf("Cube count: %d\n", cubeCount);

  int32_t buffer[3];
  for (int i = 0; i < vertexCount; index += sizeof(buffer), ++i )
  {
    memcpy( &buffer, &myData[index], sizeof(buffer) );
    const double x = fixedToFloating(buffer[0]);
    const double y = fixedToFloating(buffer[1]);
    const double z = fixedToFloating(buffer[2]);

    printf("%f %f %f\n", x, y, z);
  }

  // A check to ensure index is current positioned at the start of the cubes.
  assert( index == myHeader->mineDataOffset + 1 + 4 + 12 *  vertexCount );

  exit(0);
}

{'fraction_non_alphanumeric': 0.17397279098529614, 'fraction_numerical': 0.02061288992716779, 'mean_word_length': 2.107600341588386, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5224219', 'n_tokens_mistral': 2363, 'n_tokens_neox': 2196, 'n_words': 731}