123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- #ifndef _AK_BANKREADHELPERS_H_
- #define _AK_BANKREADHELPERS_H_
- #include <AK/Tools/Common/AkPlatformFuncs.h>
- #include <type_traits>
- namespace AK
- {
- template<
- typename T,
- typename std::enable_if<std::is_fundamental<T>::value || std::is_enum<T>::value, bool>::type = true
- >
- inline T ReadUnaligned(const AkUInt8* in_pVal)
- {
- #if defined(__GNUC__)
- typedef T __attribute__((aligned(1))) UnalignedT;
- return *reinterpret_cast<const UnalignedT *>(in_pVal);
- #elif defined(_MSC_VER) && !defined(AK_CPU_X86)
- return *reinterpret_cast<const T __unaligned *>(in_pVal);
- #else
- return *reinterpret_cast<const T *>(in_pVal);
- #endif
- }
- template<
- typename T,
- typename std::enable_if<std::is_class<T>::value, bool>::type = true
- >
- inline T ReadUnaligned(const AkUInt8* in_pVal)
- {
- T result;
- AKPLATFORM::AkMemCpy(&result, in_pVal, sizeof(T));
- return result;
- }
- template<
- typename T,
- typename std::enable_if<std::is_fundamental<T>::value || std::is_enum<T>::value, bool>::type = true
- >
- inline void WriteUnaligned(AkUInt8* out_pVal, const T in_val)
- {
- #if defined(__GNUC__)
- typedef T __attribute__((aligned(1))) UnalignedT;
- *reinterpret_cast<UnalignedT *>(out_pVal) = in_val;
- #elif defined(_MSC_VER) && !defined(AK_CPU_X86)
- *reinterpret_cast<T __unaligned *>(out_pVal) = in_val;
- #else
- *reinterpret_cast<T *>(out_pVal) = in_val;
- #endif
- }
- template<
- typename T,
- typename std::enable_if<std::is_class<T>::value, bool>::type = true
- >
- inline void WriteUnaligned(AkUInt8* out_pVal, const T& in_val)
- {
- AKPLATFORM::AkMemCpy(out_pVal, &in_val, sizeof(T));
- }
-
- template< typename T >
- inline T ReadBankData(
- AkUInt8*& in_rptr
- #ifdef _DEBUG
- , AkUInt32& in_rSize
- #endif
- )
- {
- T l_Value = ReadUnaligned<T>(in_rptr);
- in_rptr += sizeof(T);
- #ifdef _DEBUG
- in_rSize -= sizeof(T);
- #endif
- return l_Value;
- }
- template< typename T >
- inline T ReadVariableSizeBankData(
- AkUInt8*& in_rptr
- #ifdef _DEBUG
- , AkUInt32& in_rSize
- #endif
- )
- {
- AkUInt32 l_Value = 0;
- AkUInt8 currentByte = *in_rptr;
- ++in_rptr;
- #ifdef _DEBUG
- --in_rSize;
- #endif
- l_Value = (currentByte & 0x7F);
- while (0x80 & currentByte)
- {
- currentByte = *in_rptr;
- ++in_rptr;
- #ifdef _DEBUG
- --in_rSize;
- #endif
- l_Value = l_Value << 7;
- l_Value |= (currentByte & 0x7F);
- }
- return (T)l_Value;
- }
- inline char * ReadBankStringUtf8(
- AkUInt8*& in_rptr
- #ifdef _DEBUG
- , AkUInt32& in_rSize
- #endif
- , AkUInt32& out_uStringSize)
- {
- char* pString = (char *)in_rptr;
- out_uStringSize = 0;
- while (*in_rptr != 0)
- {
- ++in_rptr;
- #ifdef _DEBUG
- --in_rSize;
- #endif
- ++out_uStringSize;
- }
- ++in_rptr;
- #ifdef _DEBUG
- --in_rSize;
- #endif
- return pString;
- }
- }
- #ifdef _DEBUG
- #define READBANKDATA( _Type, _Ptr, _Size ) \
- AK::ReadBankData<_Type>( _Ptr, _Size )
- #define READVARIABLESIZEBANKDATA( _Type, _Ptr, _Size ) \
- AK::ReadVariableSizeBankData<_Type>( _Ptr, _Size )
- #define READBANKSTRING( _Ptr, _Size, _out_StringSize ) \
- AK::ReadBankStringUtf8( _Ptr, _Size, _out_StringSize )
- #define SKIPBANKDATA( _Type, _Ptr, _Size ) \
- ( _Ptr ) += sizeof( _Type ); \
- ( _Size ) -= sizeof( _Type )
- #define SKIPBANKBYTES( _NumBytes, _Ptr, _Size ) \
- ( _Size ) -= _NumBytes; \
- ( _Ptr ) += _NumBytes
- #define COPYBANKSTRING_CHAR( _Ptr, _Size, _OutPtr, _MaxOutPtrSize ) \
- AKPLATFORM::SafeStrCpy( _OutPtr, ((const char*)_Ptr), (_MaxOutPtrSize) ); \
- SKIPBANKBYTES( (AkUInt32)strlen((const char*)_Ptr) + 1, _Ptr, _Size )
- #define COPYBANKSTRING_OSCHAR( _Ptr, _Size, _OutPtr, _MaxOutPtrSize ) \
- AK_UTF8_TO_OSCHAR( _OutPtr, ((const char*)_Ptr), (_MaxOutPtrSize) ); \
- SKIPBANKBYTES( (AkUInt32)strlen((const char*)_Ptr) + 1, _Ptr, _Size )
- #define COPYBANKSTRING_WCHAR( _Ptr, _Size, OutPtr, _MaxOutPtrSize ) \
- AK_CHAR_TO_UTF16( _OutPtr, ((const char*)_Ptr), (_MaxOutPtrSize) ); \
- SKIPBANKBYTES( (AkUInt32)strlen((const char*)_Ptr) + 1, _Ptr, _Size )
- #else
- #define READBANKDATA( _Type, _Ptr, _Size ) \
- AK::ReadBankData<_Type>( _Ptr )
- #define READVARIABLESIZEBANKDATA( _Type, _Ptr, _Size ) \
- AK::ReadVariableSizeBankData<_Type>( _Ptr )
- #define READBANKSTRING( _Ptr, _Size, _out_StringSize ) \
- AK::ReadBankStringUtf8( _Ptr, _out_StringSize )
- #define SKIPBANKDATA( _Type, _Ptr, _Size ) \
- ( _Ptr ) += sizeof( _Type )
- #define SKIPBANKBYTES( _NumBytes, _Ptr, _Size ) \
- ( _Ptr ) += _NumBytes;
- #define COPYBANKSTRING_CHAR( _Ptr, _Size, _OutPtr, _MaxPtrSize ) \
- AKPLATFORM::SafeStrCpy( _OutPtr, (const char*)_Ptr, _MaxPtrSize ); \
- SKIPBANKBYTES( (AkUInt32)strlen((const char*)_Ptr)+1, _Ptr, _Size )
- #define COPYBANKSTRING_OSCHAR( _Ptr, _Size, _OutPtr, _MaxPtrSize ) \
- AK_UTF8_TO_OSCHAR( _OutPtr, (const char*)_Ptr, _MaxPtrSize ); \
- SKIPBANKBYTES( (AkUInt32)strlen((const char*)_Ptr)+1, _Ptr, _Size )
- #define COPYBANKSTRING_WCHAR( _Ptr, _Size, OutPtr, _MaxPtrSize ) \
- AK_CHAR_TO_UTF16( _OutPtr, (const char*)_Ptr, _MaxPtrSize ); \
- SKIPBANKBYTES( (AkUInt32)strlen((const char*)_Ptr)+1, _Ptr, _Size )
- #endif
- #define GETBANKDATABIT( _Data, _Shift ) \
- (((_Data) >> (_Shift)) & 0x1)
- #ifdef _DEBUG
- #define CHECKBANKDATASIZE( _DATASIZE_, _ERESULT_ ) AKASSERT( _DATASIZE_ == 0 || _ERESULT_ != AK_Success );
- #else
- #define CHECKBANKDATASIZE(_DATASIZE_, _ERESULT_ )
- #endif
- #endif
|