Progress implementing TylerMapSource.

This commit is contained in:
King_DuckZ 2015-06-06 14:32:36 +02:00
parent ab17e7a4d0
commit d21a567fb7
4 changed files with 204 additions and 4 deletions

View file

@ -7,7 +7,7 @@ set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -std=c++11 -O3 -Wall -We
add_definitions(-DWITH_VECTOR_IOSTREAM)
find_library(Boost 1.21.0 REQUIRED)
find_library(Boost 1.53.0 REQUIRED)
include_directories(SYSTEM
${Boost_INCLUDE_DIRECTORIES}

View file

@ -4,6 +4,7 @@
#include "doorkeeper/primitivetypes.hpp"
#include "doorkeeper/implem/vector.hpp"
#include "doorkeeper/components/basemapsource.hpp"
#include "doorkeeper/implem/maptypes.hpp"
#include <fstream>
#include <cstdint>
#include <memory>
@ -19,6 +20,7 @@
namespace dkh {
template <uint32_t D>
class TylerMapSource : public dk::BaseMapSource<D> {
typedef std::istream::pos_type pos_type;
public:
typedef dk::Vector<D> coords;
enum {
@ -44,6 +46,16 @@ namespace dkh {
virtual void fetch_raw ( char* parOut, const coords& parFrom, const coords& parTo, std::size_t parSize );
std::unique_ptr<std::istream> m_stream;
std::map<std::string, std::string> m_comments;
coords m_map_size;
coords m_tile_size;
pos_type m_stream_start;
std::string m_file_vendor;
dk::MapTypes m_map_type;
uint16_t m_map_version_major;
uint16_t m_map_version_minor;
uint8_t m_layer_count;
};
#if defined(WITH_TYLERMAP_WRITER)
@ -60,6 +72,8 @@ namespace dkh {
class InvalidMapFileException : public std::exception {
};
class InvalidMapDimensionsException : public std::exception {
};
namespace implem {
struct TylerMapHeader {
@ -71,13 +85,27 @@ namespace dkh {
uint8_t map_type;
uint8_t map_info_type;
uint8_t dimensions;
uint32_t dimensions_start;
uint32_t comment_start;
uint32_t layer_start;
} __attribute__((packed, aligned(__alignof__(uint32_t))));
struct TylerMapLayerHeader {
uint32_t layer_length;
uint32_t layer_format;
} __attribute__((packed, aligned(__alignof__(uint32_t))));
typedef std::map<std::string, std::string> CommentMap;
void read_header ( std::istream& parStream, TylerMapHeader& parHeader );
void read_layer_header ( std::istream& parStream, TylerMapLayerHeader& parHeader );
void read_comments ( std::istream& parStream, std::string& parVendor, CommentMap& parComments );
void read_dimensions ( std::istream& parStream, uint32_t parDimensions, std::vector<uint32_t>& parMap, std::vector<uint16_t>& parTile );
void extract_array_from_comments ( std::vector<std::string>& parOut, const std::string& parPrefix, const std::map<std::string, std::string>& parMap );
} //namespace implem
} //namespace dkh
#include "doorkeeper/implem/tylermapsource.inl"
#endif

View file

@ -1,21 +1,103 @@
namespace dkh {
template <uint32_t D>
TylerMapSource<D>::TylerMapSource (const char* parFilename) :
m_stream(new std::ifstream(parFilename))
m_stream(new std::ifstream(parFilename)),
m_stream_start(m_stream->tellg())
{
parse_map_data(*m_stream);
}
template <uint32_t D>
TylerMapSource<D>::TylerMapSource (const std::string& parFilename) :
m_stream(new std::ifstream(parFilename))
m_stream(new std::ifstream(parFilename)),
m_stream_start(m_stream->tellg())
{
parse_map_data(*m_stream);
}
template <uint32_t D>
TylerMapSource<D>::TylerMapSource (std::istream* parData) :
m_stream(parData)
m_stream(parData),
m_stream_start(m_stream->tellg())
{
parse_map_data(*m_stream);
}
template <uint32_t D>
void TylerMapSource<D>::parse_map_data (std::istream& parSrc) {
//Read file header
implem::TylerMapHeader header;
implem::read_header(parSrc, header);
if (header.dimensions != D) {
throw InvalidMapDimensionsException();
}
switch (header.map_type) {
case dk::MapType_IsometricStaggered:
m_map_type = dk::MapType_IsometricStaggered;
break;
case dk::MapType_Isometric:
m_map_type = dk::MapType_Isometric;
break;
case dk::MapType_Orthogonal:
m_map_type = dk::MapType_Orthogonal;
break;
case dk::MapType_Hex:
m_map_type = dk::MapType_Hex;
break;
default:
throw InvalidMapFileException();
}
//Read map and tile dimensions
{
std::vector<uint32_t> map_dims;
std::vector<uint16_t> tile_dims;
const auto read_start = (header.dimensions_start > sizeof(header) ? header.dimensions_start - sizeof(header) : sizeof(header));
parSrc.seekg(read_start, std::ios_base::cur);
implem::read_dimensions(parSrc, D, map_dims, tile_dims);
for (std::size_t z = 0; z < map_dims.size(); ++z) {
m_map_size[z] = map_dims[z];
m_tile_size[z] = tile_dims[z];
}
}
//Read vorbis comments
parSrc.seekg(header.comment_start + m_stream_start, std::ios_base::beg);
implem::read_comments(parSrc, m_file_vendor, m_comments);
}
template <uint32_t D>
const typename TylerMapSource<D>::coords& TylerMapSource<D>::mapSize() const {
return m_map_size;
}
template <uint32_t D>
const typename TylerMapSource<D>::coords& TylerMapSource<D>::tileSize() const {
return m_tile_size;
}
template <uint32_t D>
void TylerMapSource<D>::fetch_raw (char* parOut, const coords& parFrom, const coords& parTo, std::size_t parSize) {
(void)parOut;
(void)parFrom;
(void)parTo;
(void)parSize;
}
template <uint32_t D>
dk::MapTypes TylerMapSource<D>::mapType() const {
return m_map_type;
}
template <uint32_t D>
int TylerMapSource<D>::layersCount() const {
return m_layer_count;
}
template <uint32_t D>
void TylerMapSource<D>::chainedMaps (std::vector<std::string>& parOut) const {
implem::extract_array_from_comments(parOut, "chained_map", m_comments);
}
} //namespace dkh

View file

@ -1,5 +1,10 @@
#include "doorkeeper/helpers/tylermapsource.hpp"
#include <limits>
#include <algorithm>
#include <ciso646>
#include <boost/lexical_cast.hpp>
#include <boost/utility/string_ref.hpp>
#include <iterator>
#if defined(__GNUC__)
# include <endian.h>
@ -37,18 +42,22 @@ namespace dkh {
template<> uint16_t lil_endian_to_machine ( uint16_t parIn ) a_pure;
template<> uint8_t lil_endian_to_machine ( uint8_t parIn ) a_pure;
template<> implem::TylerMapHeader lil_endian_to_machine ( implem::TylerMapHeader parIn ) a_pure;
template<> implem::TylerMapLayerHeader lil_endian_to_machine ( implem::TylerMapLayerHeader parIn ) a_pure;
#if defined(WITH_TYLERMAP_WRITER)
template <typename T> T machine_to_lil_endian ( T parIn ) a_pure;
template<> uint32_t machine_to_lil_endian ( uint32_t parIn ) a_pure;
template<> uint16_t machine_to_lil_endian ( uint16_t parIn ) a_pure;
template<> uint8_t machine_to_lil_endian ( uint8_t parIn ) a_pure;
template<> implem::TylerMapHeader machine_to_lil_endian ( implem::TylerMapHeader parIn ) a_pure;
template<> implem::TylerMapLayerHeader machine_to_lil_endian ( implem::TylerMapLayerHeader parIn ) a_pure;
#endif
template<> uint32_t lil_endian_to_machine (uint32_t parIn) { return le32toh(parIn); }
template<> uint16_t lil_endian_to_machine (uint16_t parIn) { return le16toh(parIn); }
template<> uint8_t lil_endian_to_machine (uint8_t parIn) { return parIn; }
bool isdigit (char parChar) a_pure a_flatten;
template<> implem::TylerMapHeader lil_endian_to_machine (implem::TylerMapHeader parIn) {
parIn.signature = lil_endian_to_machine(parIn.signature);
parIn.file_size = lil_endian_to_machine(parIn.file_size);
@ -58,6 +67,15 @@ namespace dkh {
parIn.map_type = lil_endian_to_machine(parIn.map_type);
parIn.map_info_type = lil_endian_to_machine(parIn.map_info_type);
parIn.dimensions = lil_endian_to_machine(parIn.dimensions);
parIn.dimensions_start = lil_endian_to_machine(parIn.dimensions_start);
parIn.comment_start = lil_endian_to_machine(parIn.comment_start);
parIn.layer_start = lil_endian_to_machine(parIn.layer_start);
return parIn;
}
template<> implem::TylerMapLayerHeader lil_endian_to_machine (implem::TylerMapLayerHeader parIn) {
parIn.layer_length = lil_endian_to_machine(parIn.layer_length);
parIn.layer_format = lil_endian_to_machine(parIn.layer_format);
return parIn;
}
@ -75,6 +93,15 @@ namespace dkh {
parIn.map_type = machine_to_lil_endian(parIn.map_type);
parIn.map_info_type = machine_to_lil_endian(parIn.map_info_type);
parIn.dimensions = machine_to_lil_endian(parIn.dimensions);
parIn.dimensions_start = machine_to_lil_endian(parIn.dimensions_start);
parIn.comment_start = machine_to_lil_endian(parIn.comment_start);
parIn.layer_start = machine_to_lil_endian(parIn.layer_start);
return parIn;
}
template<> implem::TylerMapLayerHeader machine_to_lil_endian (implem::TylerMapLayerHeader parIn) {
parIn.layer_length = machine_to_lil_endian(parIn.layer_length);
parIn.layer_format = machine_to_lil_endian(parIn.layer_format);
return parIn;
}
#endif
@ -113,6 +140,10 @@ namespace dkh {
parStream.write(parString.c_str(), parString.size());
}
#endif
bool isdigit (char parChar) {
return std::isdigit(parChar);
}
} //unnamed namespace
namespace implem {
@ -141,5 +172,64 @@ namespace dkh {
parComments[full_string.substr(0, equal_pos)] = full_string.substr(equal_pos + 1);
}
}
void read_layer_header (std::istream& parStream, TylerMapLayerHeader& parHeader) {
parStream.read(reinterpret_cast<char*>(&parHeader), sizeof(TylerMapLayerHeader));
parHeader = lil_endian_to_machine(parHeader);
}
void read_dimensions (std::istream& parStream, uint32_t parDimensions, std::vector<uint32_t>& parMap, std::vector<uint16_t>& parTile) {
parMap.reserve(parMap.size() + parDimensions);
for (uint32_t z = 0; z < parDimensions; ++z) {
const auto dim = read_int<uint32_t>(parStream);
parMap.push_back(dim);
}
parTile.reserve(parTile.size() + parDimensions);
for (uint32_t z = 0; z < parDimensions; ++z) {
const auto dim = read_int<uint16_t>(parStream);
parTile.push_back(dim);
}
}
void extract_array_from_comments (std::vector<std::string>& parOut, const std::string& parPrefix, const std::map<std::string, std::string>& parMap) {
typedef decltype(std::distance(parPrefix.begin(), parPrefix.end())) diff_type;
using std::size_t;
using boost::lexical_cast;
parOut.clear();
for (const auto& itcurr : parMap) {
const std::string& key = itcurr.first;
if (parPrefix.size() + 3 > key.size())
continue;
if (not std::equal(parPrefix.begin(), parPrefix.end(), key.begin()))
continue;
if (key[parPrefix.size()] != '[')
continue;
auto it_past_index = std::find_if_not(key.begin() + parPrefix.size() + 1, key.end(), &isdigit);
if (it_past_index == key.end())
continue;
if (*it_past_index != ']')
continue;
if (std::distance(key.begin(), it_past_index) + 1 != static_cast<diff_type>(key.size()))
continue;
boost::string_ref index_part(key);
const auto dist = std::distance(key.begin(), it_past_index);
DK_ASSERT(dist > static_cast<diff_type>(parPrefix.size() + 1));
index_part = index_part.substr(parPrefix.size() + 1, dist - parPrefix.size() - 1);
const size_t index = lexical_cast<size_t>(index_part);
if (index >= parMap.size())
continue;
if (parOut.size() <= index)
parOut.resize(index + 1);
parOut[index] = itcurr.second;
}
}
} //namespace implem
} //namespace dkh