Aeon Engine c550894
AeonGames Open Source Game Engine
Loading...
Searching...
No Matches
Decoder.h
1/*
2Copyright (C) 2018,2025,2026 Rodrigo Jose Hernandez Cordoba
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16#include <vector>
17#include <tuple>
18#include <string>
19#include <functional>
20#include <algorithm>
21#include <fstream>
22#include "aeongames/AeonEngine.hpp"
23#include "aeongames/CRC.hpp"
24#include <utility>
25#ifndef AEONGAMES_DECODER_H
26#define AEONGAMES_DECODER_H
27namespace AeonGames
28{
31 template<class T>
32 class Decoder
33 {
34 public:
39 static bool Decode ( T& aOutput, uint32_t aId )
40 {
41 //--------------------------------------------------------
42 // File loading code
43 //std::ifstream file;
44 //file.exceptions ( std::ifstream::failbit | std::ifstream::badbit );
45 //file.open ( aFileName, std::ios::binary );
46 //std::vector<uint8_t> buffer ( (
47 // std::istreambuf_iterator<char> ( file ) ),
48 // ( std::istreambuf_iterator<char>() ) );
49 //file.close();
50 //--------------------------------------------------------
51 size_t buffer_size = GetResourceSize ( aId );
52 std::vector<uint8_t> buffer ( buffer_size );
53 LoadResource ( aId, buffer.data(), buffer.size() );
54 return Decode ( aOutput, buffer.data(), buffer.size() );
55 }
56
60 static bool Decode ( T& aOutput, const std::string& aPath )
61 {
62 return Decode ( aOutput, crc32i ( aPath.data(), aPath.size() ) );
63 }
64
70 static bool Decode ( T& aOutput, const void* aBuffer, size_t aBufferSize )
71 {
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 )
74 {
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;
77 } );
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 )
79 {
80 return false;
81 }
82 return std::get<1> ( *iterator ) ( aOutput, aBufferSize, aBuffer );
83 }
84
88 static bool RegisterDecoder ( const std::string& aMagick, const std::function < bool ( T&, size_t, const void* ) > & aDecoder )
89 {
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 )
92 {
93 return std::get<0> ( aTuple ) < aMagick;
94 } );
95 if ( iterator != Decoders.end() && std::get<0> ( *iterator ) == aMagick )
96 {
97 return false;
98 }
99 Decoders.insert ( iterator, std::tuple<std::string, std::function < bool ( T&, size_t, const void* ) >> ( aMagick, aDecoder ) );
100 return true;
101 }
102
105 static bool UnregisterDecoder ( const std::string& aMagick )
106 {
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 )
109 {
110 return std::get<0> ( aTuple ) < aMagick;
111 } );
112 if ( iterator == Decoders.end() )
113 {
114 return false;
115 }
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 )
118 {
119 return std::get<0> ( aTuple ) == aMagick;
120 } ), Decoders.end() );
121 return true;
122 }
123 private:
125 static std::vector<std::tuple<std::string, std::function < bool ( T&, size_t, const void* ) >>> Decoders;
126 };
127
128 template<class T>
129 std::vector < std::tuple<std::string, std::function < bool ( T&, size_t, const void* ) >>> Decoder<T>::Decoders{};
131}
132#endif
Template class that dispatches decoding of binary data to registered format-specific decoders.
Definition Decoder.h:33
static bool Decode(T &aOutput, uint32_t aId)
Decodes a resource identified by its CRC32 id.
Definition Decoder.h:39
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.
Definition Decoder.h:88
static bool Decode(T &aOutput, const std::string &aPath)
Decodes a resource identified by its path string.
Definition Decoder.h:60
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.
Definition Decoder.h:70
static bool UnregisterDecoder(const std::string &aMagick)
Unregisters the decoder for the given magic byte sequence.
Definition Decoder.h:105
<- This is here just for the literals
Definition AABB.hpp:31
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.
Definition CRC.cpp:27
DLL void LoadResource(uint32_t crc, void *buffer, size_t buffer_size)