22#include "aeongames/AeonEngine.hpp"
23#include "aeongames/CRC.hpp"
25#ifndef AEONGAMES_DECODER_H
26#define AEONGAMES_DECODER_H
39 static bool Decode ( T& aOutput, uint32_t aId )
52 std::vector<uint8_t> buffer ( buffer_size );
54 return Decode ( aOutput, buffer.data(), buffer.size() );
60 static bool Decode ( T& aOutput,
const std::string& aPath )
62 return Decode ( aOutput,
crc32i ( aPath.data(), aPath.size() ) );
70 static bool Decode ( T& aOutput,
const void* aBuffer,
size_t aBufferSize )
72 auto iterator = std::lower_bound ( Decoders.begin(), Decoders.end(), aBuffer,
73 [] (
const std::tuple<std::string, std::function < bool ( T&,
size_t,
const void* ) >> & aTuple,
const void* aBuffer )
75 int difference = std::get<0> ( aTuple ).compare ( 0, std::get<0> ( aTuple ).size(),
static_cast<const char*
> ( aBuffer ), std::get<0> ( aTuple ).size() );
76 return difference < 0;
78 if ( iterator == Decoders.end() || std::get<0> ( *iterator ).compare ( 0, std::get<0> ( *iterator ).size(),
static_cast<const char *
> ( aBuffer ), std::get<0> ( *iterator ).size() ) != 0 )
82 return std::get<1> ( *iterator ) ( aOutput, aBufferSize, aBuffer );
88 static bool RegisterDecoder (
const std::string& aMagick,
const std::function <
bool ( T&,
size_t,
const void* ) > & aDecoder )
90 auto iterator = std::lower_bound ( Decoders.begin(), Decoders.end(), aMagick,
91 [] (
const std::tuple<std::string, std::function < bool ( T&,
size_t,
const void* ) >> & aTuple,
const std::string & aMagick )
93 return std::get<0> ( aTuple ) < aMagick;
95 if ( iterator != Decoders.end() && std::get<0> ( *iterator ) == aMagick )
99 Decoders.insert ( iterator, std::tuple<std::string, std::function <
bool ( T&,
size_t,
const void* ) >> ( aMagick, aDecoder ) );
107 auto iterator = std::lower_bound ( Decoders.begin(), Decoders.end(), aMagick,
108 [] (
const std::tuple<std::string, std::function < bool ( T&,
size_t,
const void* ) >>& aTuple,
const std::string & aMagick )
110 return std::get<0> ( aTuple ) < aMagick;
112 if ( iterator == Decoders.end() )
116 Decoders.erase ( std::remove_if ( Decoders.begin(), Decoders.end(),
117 [&aMagick] (
const std::tuple<std::string, std::function < bool ( T&,
size_t,
const void* ) >> & aTuple )
119 return std::get<0> ( aTuple ) == aMagick;
120 } ), Decoders.end() );
125 static std::vector<std::tuple<std::string, std::function < bool ( T&,
size_t,
const void* ) >>> Decoders;
129 std::vector < std::tuple<std::string, std::function < bool ( T&,
size_t,
const void* ) >>> Decoder<T>::Decoders{};
Template class that dispatches decoding of binary data to registered format-specific decoders.
static bool Decode(T &aOutput, uint32_t aId)
Decodes a resource identified by its CRC32 id.
static bool RegisterDecoder(const std::string &aMagick, const std::function< bool(T &, size_t, const void *) > &aDecoder)
Registers a decoder function for the given magic byte sequence.
static bool Decode(T &aOutput, const std::string &aPath)
Decodes a resource identified by its path string.
static bool Decode(T &aOutput, const void *aBuffer, size_t aBufferSize)
Decodes data from a raw memory buffer by matching its magic bytes to a registered decoder.
static bool UnregisterDecoder(const std::string &aMagick)
Unregisters the decoder for the given magic byte sequence.
<- This is here just for the literals
DLL size_t GetResourceSize(uint32_t crc)
uint32_t crc32i(const char *message, size_t size, uint32_t previous_crc)
Compute the CRC32 of a given message, continuing from a previous CRC value.
DLL void LoadResource(uint32_t crc, void *buffer, size_t buffer_size)