Start implementing the tyler map loader.

Untented and incomplete.
This commit is contained in:
King_DuckZ 2015-05-29 00:15:06 +02:00
parent 72b400ac0c
commit f1a6cac62b
4 changed files with 179 additions and 0 deletions

View file

@ -0,0 +1,68 @@
#ifndef id3F4AB2FAA29D4A0FA87760E61F0762C0
#define id3F4AB2FAA29D4A0FA87760E61F0762C0
#include "doorkeeper/primitivetypes.hpp"
#include "doorkeeper/implem/vector.hpp"
#include "doorkeeper/components/basemapsource.hpp"
#include <fstream>
#include <cstdint>
#include <memory>
#include <map>
#include <string>
#include <ciso646>
#include <exception>
namespace dkh {
template <typename T, uint32_t D>
class TylerMapSource : public dk::BaseMapSource<T, D> {
public:
typedef dk::Vector<dk::CoordinateScalarType, 2> coords;
typedef T MapTileType;
enum {
MapDimensions = D
};
TylerMapSource ( void ) = delete;
TylerMapSource ( const TylerMapSource& ) = delete;
TylerMapSource ( TylerMapSource&& ) = default;
explicit TylerMapSource ( const char* parFilename );
explicit TylerMapSource ( const std::string& parFilename );
explicit TylerMapSource ( std::istream* parData );
virtual ~TylerMapSource ( void ) noexcept = default;
virtual const coords& mapSize ( void ) const;
virtual const coords& tileSize ( void ) const;
virtual void fetch ( std::vector<MapTileType>& parOut, const coords& parFrom, const coords& parTo );
virtual dk::MapTypes mapType ( void ) const;
virtual int layersCount ( void ) const;
virtual void chainedMaps ( std::vector<std::string>& parOut ) const;
private:
void parse_map_data ( std::istream& parSrc );
std::unique_ptr<std::istream> m_stream;
};
class InvalidMapFileException : public std::exception {
};
namespace implem {
struct TylerMapHeader {
uint32_t signature;
uint32_t file_size;
uint16_t version_major;
uint16_t version_minor;
uint8_t layer_count;
uint8_t map_type;
uint8_t map_info_type;
uint8_t dimensions;
} __attribute__((packed, aligned(__alignof__(uint32_t))));
typedef std::map<std::string, std::string> CommentMap;
void read_header ( std::istream& parStream, TylerMapHeader& parHeader );
void read_comments ( std::istream& parStream, std::string& parVendor, CommentMap& parComments );
} //namespace implem
} //namespace dkh
#endif

View file

@ -0,0 +1,21 @@
namespace dkh {
template <typename T, uint32_t D>
TylerMapSource<T, D>::TylerMapSource (const char* parFilename) :
m_stream(new std::ifstream(parFilename))
{
}
template <typename T, uint32_t D>
TylerMapSource<T, D>::TylerMapSource (const std::string& parFilename) :
m_stream(new std::ifstream(parFilename))
{
}
template <typename T, uint32_t D>
TylerMapSource<T, D>::TylerMapSource (std::istream* parData) :
m_stream(parData)
{
}
} //namespace dkh

View file

@ -7,4 +7,5 @@ include_directories(
add_library(${PROJECT_NAME}
asciimapsource.cpp
tylermapsource.cpp
)

89
src/tylermapsource.cpp Normal file
View file

@ -0,0 +1,89 @@
#include "doorkeeper/helpers/tylermapsource.hpp"
#if defined(__GNUC__)
# include <endian.h>
#else
# error "Unsupported compiler"
#endif
#include <algorithm>
#include <iterator>
//Vorbis comment specification: https://www.xiph.org/vorbis/doc/v-comment.html
//The comment header logically is a list of eight-bit-clean vectors; the number
//of vectors is bounded to 2^32-1 and the length of each vector is limited to
//2^32-1 bytes. The vector length is encoded; the vector contents themselves
//are not null terminated. In addition to the vector list, there is a single
//vector for vendor name (also 8 bit clean, length encoded in 32 bits). For
//example, the 1.0 release of libvorbis set the vendor string to "Xiph.Org
//libVorbis I 20020717".
//
//The comment header is decoded as follows:
//
// 1) [vendor_length] = read an unsigned integer of 32 bits
// 2) [vendor_string] = read a UTF-8 vector as [vendor_length] octets
// 3) [user_comment_list_length] = read an unsigned integer of 32 bits
// 4) iterate [user_comment_list_length] times {
// 5) [length] = read an unsigned integer of 32 bits
// 6) this iteration's user comment = read a UTF-8 vector as [length] octets
// }
namespace dkh {
namespace {
const uint32_t g_signature = 0x52504B44; //DKPR
uint32_t read_uint32 (std::istream& parStream, uint32_t parMax=0xFFFFFFFF) {
uint32_t retval;
#if __BYTE_ORDER == __LITTLE_ENDIAN
parStream.read(reinterpret_cast<char*>(&retval), sizeof(uint32_t));
#else
# error "Unknown endianness, please write the implementation"
#endif
if (retval > parMax)
throw InvalidMapFileException();
return retval;
}
std::string& read_string (std::istream& parStream, std::string& parString, uint32_t parLength) {
std::istream_iterator<char> itstream(parStream);
parString.resize(parLength);
std::copy_n(itstream, parLength, std::insert_iterator<std::string>(parString, parString.begin()));
return parString;
}
} //unnamed namespace
namespace implem {
void read_header (std::istream& parStream, TylerMapHeader& parHeader) {
#if __BYTE_ORDER == __LITTLE_ENDIAN
parStream.read(reinterpret_cast<char*>(&parHeader), sizeof(TylerMapHeader));
#else
# error "Unknown endianness, please write the implementation"
#endif
if (g_signature != parHeader.signature)
throw InvalidMapFileException();
}
void read_comments (std::istream& parStream, std::string& parVendor, CommentMap& parComments) {
//Technically there is no limit on strings other than the uint32
//max value, but wth
const uint32_t max_string_length = 1024 * 2;
const uint32_t max_entries = 200;
{
const uint32_t vendor_length = read_uint32(parStream, max_string_length);
read_string(parStream, parVendor, vendor_length);
}
const uint32_t list_length = read_uint32(parStream, max_entries);
std::string full_string;
for (uint32_t z = 0; z < list_length; ++z) {
const uint32_t string_length = read_uint32(parStream, max_string_length);
read_string(parStream, full_string, string_length);
auto equal_pos = full_string.find('=');
if (std::string::npos == equal_pos)
throw InvalidMapFileException();
parComments[full_string.substr(0, equal_pos)] = full_string.substr(equal_pos + 1);
}
}
} //namespace implem
} //namespace dkh