From f1a6cac62bbb5a77c7d05f2f388733751652145e Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Fri, 29 May 2015 00:15:06 +0200 Subject: [PATCH] Start implementing the tyler map loader. Untented and incomplete. --- include/doorkeeper/helpers/tylermapsource.hpp | 68 ++++++++++++++ include/doorkeeper/implem/tylermapsource.inl | 21 +++++ src/CMakeLists.txt | 1 + src/tylermapsource.cpp | 89 +++++++++++++++++++ 4 files changed, 179 insertions(+) create mode 100644 include/doorkeeper/helpers/tylermapsource.hpp create mode 100644 include/doorkeeper/implem/tylermapsource.inl create mode 100644 src/tylermapsource.cpp diff --git a/include/doorkeeper/helpers/tylermapsource.hpp b/include/doorkeeper/helpers/tylermapsource.hpp new file mode 100644 index 0000000..612f171 --- /dev/null +++ b/include/doorkeeper/helpers/tylermapsource.hpp @@ -0,0 +1,68 @@ +#ifndef id3F4AB2FAA29D4A0FA87760E61F0762C0 +#define id3F4AB2FAA29D4A0FA87760E61F0762C0 + +#include "doorkeeper/primitivetypes.hpp" +#include "doorkeeper/implem/vector.hpp" +#include "doorkeeper/components/basemapsource.hpp" +#include +#include +#include +#include +#include +#include +#include + +namespace dkh { + template + class TylerMapSource : public dk::BaseMapSource { + public: + typedef dk::Vector 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& parOut, const coords& parFrom, const coords& parTo ); + virtual dk::MapTypes mapType ( void ) const; + virtual int layersCount ( void ) const; + virtual void chainedMaps ( std::vector& parOut ) const; + + private: + void parse_map_data ( std::istream& parSrc ); + + std::unique_ptr 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 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 diff --git a/include/doorkeeper/implem/tylermapsource.inl b/include/doorkeeper/implem/tylermapsource.inl new file mode 100644 index 0000000..3fb23c1 --- /dev/null +++ b/include/doorkeeper/implem/tylermapsource.inl @@ -0,0 +1,21 @@ +namespace dkh { + template + TylerMapSource::TylerMapSource (const char* parFilename) : + m_stream(new std::ifstream(parFilename)) + { + } + + template + TylerMapSource::TylerMapSource (const std::string& parFilename) : + m_stream(new std::ifstream(parFilename)) + { + } + + template + TylerMapSource::TylerMapSource (std::istream* parData) : + m_stream(parData) + { + } + + +} //namespace dkh diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6c651a1..1e95479 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -7,4 +7,5 @@ include_directories( add_library(${PROJECT_NAME} asciimapsource.cpp + tylermapsource.cpp ) diff --git a/src/tylermapsource.cpp b/src/tylermapsource.cpp new file mode 100644 index 0000000..ce157e4 --- /dev/null +++ b/src/tylermapsource.cpp @@ -0,0 +1,89 @@ +#include "doorkeeper/helpers/tylermapsource.hpp" + +#if defined(__GNUC__) +# include +#else +# error "Unsupported compiler" +#endif +#include +#include + +//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(&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 itstream(parStream); + parString.resize(parLength); + std::copy_n(itstream, parLength, std::insert_iterator(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(&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