16#ifndef AEONGAMES_PROTOBUFHELPERS_H
17#define AEONGAMES_PROTOBUFHELPERS_H
24#pragma warning( push )
25#pragma warning( disable : PROTOBUF_WARNINGS )
27#include <google/protobuf/text_format.h>
28#include <google/protobuf/io/zero_copy_stream.h>
32#include "aeongames/Utilities.hpp"
41 constexpr uint64_t
operator ""_mgk (
const char* literal,
const std::size_t )
noexcept
45 (
static_cast<uint64_t
> ( literal[7] ) << 56 ) |
46 (
static_cast<uint64_t
> ( literal[6] ) << 48 ) |
47 (
static_cast<uint64_t
> ( literal[5] ) << 40 ) |
48 (
static_cast<uint64_t
> ( literal[4] ) << 32 ) |
49 (
static_cast<uint64_t
> ( literal[3] ) << 24 ) |
50 (
static_cast<uint64_t
> ( literal[2] ) << 16 ) |
51 (
static_cast<uint64_t
> ( literal[1] ) << 8 ) |
52 static_cast<uint64_t
> ( literal[0] );
68 mStart{ reinterpret_cast<const uint8_t*> ( aData ) },
70 mEnd{mStart + aSize} {}
78 bool Next (
const void** data,
int* size )
override
81 *size = ( ( mEnd - mCursor ) > std::numeric_limits<int>::max() ) ? std::numeric_limits<int>::max() :
static_cast<int> ( mEnd - mCursor );
83 return ( ( mCursor != mEnd ) || *size );
90 mCursor = ( ( mCursor - count ) > mStart ) ? mCursor - count : mStart;
96 bool Skip (
int count )
override
98 mCursor = ( ( mCursor + count ) < mEnd ) ? mCursor + count : mEnd;
99 return mCursor != mEnd;
106 return mCursor - mStart;
109 const uint8_t* mStart;
110 const uint8_t* mCursor;
125 template<
class T>
void LoadProtoBufObject ( T& t,
const void * aData,
size_t aSize, uint64_t aMagick )
127 if ( !aData || aSize < 8 )
129 throw std::runtime_error (
"Not enough data or null pointer passed to LoadProtoBufObject." );
131 if ( strncmp (
reinterpret_cast<const char *
> ( aData ),
reinterpret_cast<const char *
> ( &aMagick ), 7 ) != 0 )
133 std::ostringstream stream;
134 stream <<
"Provided buffer does not contain " <<
reinterpret_cast<const char*
> ( &aMagick ) <<
" information.";
136 throw std::runtime_error ( stream.str().c_str() );
138 else if (
reinterpret_cast<const uint8_t *
> ( aData ) [7] ==
'\0' )
140 BufferInputStream buffer_input_stream (
reinterpret_cast<const uint8_t*
> ( aData ) + 8, aSize - 8 );
141 if ( !t.ParseFromZeroCopyStream ( &buffer_input_stream ) )
143 std::ostringstream stream;
144 stream <<
"Binary parse failed on " <<
reinterpret_cast<const char*
> ( &aMagick ) <<
"buffer.";
146 throw std::runtime_error ( stream.str().c_str() );
151 BufferInputStream buffer_input_stream (
reinterpret_cast<const uint8_t*
> ( aData ) + 8, aSize - 8 );
152 if ( !google::protobuf::TextFormat::Parse ( &buffer_input_stream, &t ) )
154 std::ostringstream stream;
155 stream <<
"Text parse failed on " <<
reinterpret_cast<const char*
> ( &aMagick ) <<
" buffer.";
157 throw std::runtime_error ( stream.str().c_str() );
190 std::ostringstream stream;
191 stream <<
"File " << aFilename <<
" not found.";
192 throw std::runtime_error ( stream.str().c_str() );
195 file.exceptions ( std::ifstream::failbit | std::ifstream::badbit );
196 file.open ( aFilename, std::ifstream::in | std::ifstream::binary );
197 file.exceptions ( std::ifstream::badbit );
198 std::string text ( ( std::istreambuf_iterator<char> ( file ) ), std::istreambuf_iterator<char>() );
228 template<
class T,
class U, u
int64_t Magick>
231 static std::mutex m{};
233 std::lock_guard<std::mutex> hold ( m );
235 aTarget.LoadFromPBMsg ( buffer );
Defines log severity levels and stream output for the AeonGames engine.
<- This is here just for the literals
DLL bool FileExists(const std::string &aFilePath)
Check whether a file exists at the given path.
void LoadFromProtoBufObject(T &aTarget, const void *aBuffer, size_t aBufferSize)
Loads a Protocol Buffer message from a buffer and populates a target object.
void LoadProtoBufObject(T &t, const void *aData, size_t aSize, uint64_t aMagick)
Loads a Protocol Buffer Object from a meory buffer into the provided reference.