Move code that doesn't need to be templated to another class.
This commit is contained in:
parent
9f9ab65ff3
commit
d815b394ea
3 changed files with 145 additions and 100 deletions
|
@ -18,9 +18,34 @@
|
|||
#endif
|
||||
|
||||
namespace dkh {
|
||||
namespace implem {
|
||||
class TylerMapSourceBase {
|
||||
public:
|
||||
typedef std::istream::pos_type pos_type;
|
||||
|
||||
explicit TylerMapSourceBase ( pos_type parStreamStartPos );
|
||||
~TylerMapSourceBase ( void );
|
||||
|
||||
dk::HashType layerTypeHash ( int parIndex ) const;
|
||||
void chainedMaps ( std::vector<std::string>& parOut ) const;
|
||||
dk::MapTypes mapType ( void ) const;
|
||||
void parseMapHeaders ( std::istream& parSrc, uint32_t parDim, std::vector<uint32_t>& parMapDims, std::vector<uint16_t>& parTileDims );
|
||||
|
||||
private:
|
||||
struct LayerInfo;
|
||||
|
||||
std::vector<LayerInfo> m_layers_info;
|
||||
dk::MapTypes m_map_type;
|
||||
std::map<std::string, std::string> m_comments;
|
||||
std::string m_file_vendor;
|
||||
pos_type m_stream_start;
|
||||
};
|
||||
} //namespace implem
|
||||
|
||||
template <uint32_t D>
|
||||
class TylerMapSource : public dk::BaseMapSource<D> {
|
||||
typedef std::istream::pos_type pos_type;
|
||||
class TylerMapSource : private implem::TylerMapSourceBase, public dk::BaseMapSource<D> {
|
||||
typedef implem::TylerMapSourceBase base_class;
|
||||
typedef base_class::pos_type pos_type;
|
||||
public:
|
||||
typedef dk::Vector<D> coords;
|
||||
enum {
|
||||
|
@ -43,20 +68,13 @@ namespace dkh {
|
|||
virtual dk::HashType layerTypeHash ( int parIndex ) const;
|
||||
|
||||
private:
|
||||
struct LayerInfo;
|
||||
|
||||
void parse_map_data ( std::istream& parSrc );
|
||||
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;
|
||||
std::vector<LayerInfo> m_layers_info;
|
||||
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;
|
||||
|
@ -99,14 +117,9 @@ namespace dkh {
|
|||
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
|
||||
|
||||
|
|
|
@ -1,78 +1,40 @@
|
|||
namespace dkh {
|
||||
template <uint32_t D>
|
||||
struct TylerMapSource<D>::LayerInfo {
|
||||
dk::HashType hash;
|
||||
uint32_t data_start;
|
||||
uint32_t data_length;
|
||||
};
|
||||
|
||||
template <uint32_t D>
|
||||
TylerMapSource<D>::TylerMapSource (const char* parFilename) :
|
||||
m_stream(new std::ifstream(parFilename)),
|
||||
m_stream_start(m_stream->tellg())
|
||||
base_class(m_stream->tellg()),
|
||||
m_stream(new std::ifstream(parFilename))
|
||||
{
|
||||
parse_map_data(*m_stream);
|
||||
}
|
||||
|
||||
template <uint32_t D>
|
||||
TylerMapSource<D>::TylerMapSource (const std::string& parFilename) :
|
||||
m_stream(new std::ifstream(parFilename)),
|
||||
m_stream_start(m_stream->tellg())
|
||||
base_class(m_stream->tellg()),
|
||||
m_stream(new std::ifstream(parFilename))
|
||||
{
|
||||
parse_map_data(*m_stream);
|
||||
}
|
||||
|
||||
template <uint32_t D>
|
||||
TylerMapSource<D>::TylerMapSource (std::istream* parData) :
|
||||
m_stream(parData),
|
||||
m_stream_start(m_stream->tellg())
|
||||
base_class(m_stream->tellg()),
|
||||
m_stream(parData)
|
||||
{
|
||||
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);
|
||||
std::vector<uint32_t> map_dims;
|
||||
std::vector<uint16_t> tile_dims;
|
||||
|
||||
if (header.dimensions != D) {
|
||||
throw InvalidMapDimensionsException();
|
||||
parseMapHeaders(parSrc, D, map_dims, tile_dims);
|
||||
DK_ASSERT(map_dims.size() == D);
|
||||
DK_ASSERT(tile_dims.size() == D);
|
||||
for (std::size_t z = 0; z < D; ++z) {
|
||||
m_map_size[z] = map_dims[z];
|
||||
m_tile_size[z] = tile_dims[z];
|
||||
}
|
||||
|
||||
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>
|
||||
|
@ -95,7 +57,7 @@ namespace dkh {
|
|||
|
||||
template <uint32_t D>
|
||||
dk::MapTypes TylerMapSource<D>::mapType() const {
|
||||
return m_map_type;
|
||||
return base_class::mapType();
|
||||
}
|
||||
|
||||
template <uint32_t D>
|
||||
|
@ -105,13 +67,11 @@ namespace dkh {
|
|||
|
||||
template <uint32_t D>
|
||||
void TylerMapSource<D>::chainedMaps (std::vector<std::string>& parOut) const {
|
||||
implem::extract_array_from_comments(parOut, "chained_map", m_comments);
|
||||
return base_class::chainedMaps(parOut);
|
||||
}
|
||||
|
||||
template <uint32_t D>
|
||||
dk::HashType TylerMapSource<D>::layerTypeHash (int parIndex) const {
|
||||
DK_ASSERT(parIndex >= 0);
|
||||
DK_ASSERT(static_cast<std::size_t>(parIndex) < m_layers_info.size());
|
||||
return m_layers_info[parIndex].hash;
|
||||
return base_class::layerTypeHash(parIndex);
|
||||
}
|
||||
} //namespace dkh
|
||||
|
|
|
@ -35,6 +35,8 @@
|
|||
|
||||
namespace dkh {
|
||||
namespace {
|
||||
typedef std::map<std::string, std::string> CommentMap;
|
||||
|
||||
const uint32_t g_signature = 0x52504B44; //DKPR
|
||||
|
||||
template <typename T> T lil_endian_to_machine ( T parIn ) a_pure;
|
||||
|
@ -148,16 +150,6 @@ namespace dkh {
|
|||
bool isdigit (char parChar) {
|
||||
return std::isdigit(parChar);
|
||||
}
|
||||
} //unnamed namespace
|
||||
|
||||
namespace implem {
|
||||
void read_header (std::istream& parStream, TylerMapHeader& parHeader) {
|
||||
parStream.read(reinterpret_cast<char*>(&parHeader), sizeof(TylerMapHeader));
|
||||
parHeader = lil_endian_to_machine(parHeader);
|
||||
|
||||
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
|
||||
|
@ -177,25 +169,6 @@ namespace dkh {
|
|||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
@ -235,5 +208,104 @@ namespace dkh {
|
|||
parOut[index] = itcurr.second;
|
||||
}
|
||||
}
|
||||
} //unnamed namespace
|
||||
|
||||
namespace implem {
|
||||
struct TylerMapSourceBase::LayerInfo {
|
||||
dk::HashType hash;
|
||||
uint32_t data_start;
|
||||
uint32_t data_length;
|
||||
};
|
||||
|
||||
TylerMapSourceBase::TylerMapSourceBase (pos_type parStreamStartPos) :
|
||||
m_stream_start(parStreamStartPos)
|
||||
{
|
||||
}
|
||||
|
||||
TylerMapSourceBase::~TylerMapSourceBase() {
|
||||
}
|
||||
|
||||
dk::HashType TylerMapSourceBase::layerTypeHash (int parIndex) const {
|
||||
DK_ASSERT(parIndex >= 0);
|
||||
DK_ASSERT(static_cast<std::size_t>(parIndex) < m_layers_info.size());
|
||||
return m_layers_info[parIndex].hash;
|
||||
}
|
||||
|
||||
void TylerMapSourceBase::chainedMaps (std::vector<std::string>& parOut) const {
|
||||
extract_array_from_comments(parOut, "chained_map", m_comments);
|
||||
}
|
||||
|
||||
dk::MapTypes TylerMapSourceBase::mapType() const {
|
||||
return m_map_type;
|
||||
}
|
||||
|
||||
void TylerMapSourceBase::parseMapHeaders (std::istream& parSrc, uint32_t parDim, std::vector<uint32_t>& parMapDims, std::vector<uint16_t>& parTileDims) {
|
||||
//Read file header
|
||||
implem::TylerMapHeader header;
|
||||
implem::read_header(parSrc, header);
|
||||
|
||||
if (header.dimensions != parDim) {
|
||||
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, parDim, map_dims, tile_dims);
|
||||
parMapDims.swap(map_dims);
|
||||
parTileDims.swap(tile_dims);
|
||||
}
|
||||
|
||||
//Read vorbis comments
|
||||
parSrc.seekg(header.comment_start + m_stream_start, std::ios_base::beg);
|
||||
read_comments(parSrc, m_file_vendor, m_comments);
|
||||
}
|
||||
|
||||
void read_header (std::istream& parStream, TylerMapHeader& parHeader) {
|
||||
parStream.read(reinterpret_cast<char*>(&parHeader), sizeof(TylerMapHeader));
|
||||
parHeader = lil_endian_to_machine(parHeader);
|
||||
|
||||
if (g_signature != parHeader.signature)
|
||||
throw InvalidMapFileException();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
} //namespace implem
|
||||
} //namespace dkh
|
||||
|
|
Loading…
Add table
Reference in a new issue