AkBankReadHelpers.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*******************************************************************************
  2. The content of this file includes portions of the AUDIOKINETIC Wwise Technology
  3. released in source code form as part of the SDK installer package.
  4. Commercial License Usage
  5. Licensees holding valid commercial licenses to the AUDIOKINETIC Wwise Technology
  6. may use this file in accordance with the end user license agreement provided
  7. with the software or, alternatively, in accordance with the terms contained in a
  8. written agreement between you and Audiokinetic Inc.
  9. Apache License Usage
  10. Alternatively, this file may be used under the Apache License, Version 2.0 (the
  11. "Apache License"); you may not use this file except in compliance with the
  12. Apache License. You may obtain a copy of the Apache License at
  13. http://www.apache.org/licenses/LICENSE-2.0.
  14. Unless required by applicable law or agreed to in writing, software distributed
  15. under the Apache License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
  16. OR CONDITIONS OF ANY KIND, either express or implied. See the Apache License for
  17. the specific language governing permissions and limitations under the License.
  18. Copyright (c) 2023 Audiokinetic Inc.
  19. *******************************************************************************/
  20. #ifndef _AK_BANKREADHELPERS_H_
  21. #define _AK_BANKREADHELPERS_H_
  22. #include <AK/Tools/Common/AkPlatformFuncs.h>
  23. #include <type_traits>
  24. namespace AK
  25. {
  26. template<
  27. typename T,
  28. typename std::enable_if<std::is_fundamental<T>::value || std::is_enum<T>::value, bool>::type = true
  29. >
  30. inline T ReadUnaligned(const AkUInt8* in_pVal)
  31. {
  32. #if defined(__GNUC__)
  33. typedef T __attribute__((aligned(1))) UnalignedT;
  34. return *reinterpret_cast<const UnalignedT *>(in_pVal);
  35. #elif defined(_MSC_VER) && !defined(AK_CPU_X86)
  36. return *reinterpret_cast<const T __unaligned *>(in_pVal); // __unaligned not supported on 32-bit x86
  37. #else
  38. return *reinterpret_cast<const T *>(in_pVal);
  39. #endif
  40. }
  41. template<
  42. typename T,
  43. typename std::enable_if<std::is_class<T>::value, bool>::type = true
  44. >
  45. inline T ReadUnaligned(const AkUInt8* in_pVal)
  46. {
  47. T result;
  48. AKPLATFORM::AkMemCpy(&result, in_pVal, sizeof(T));
  49. return result;
  50. }
  51. template<
  52. typename T,
  53. typename std::enable_if<std::is_fundamental<T>::value || std::is_enum<T>::value, bool>::type = true
  54. >
  55. inline void WriteUnaligned(AkUInt8* out_pVal, const T in_val)
  56. {
  57. #if defined(__GNUC__)
  58. typedef T __attribute__((aligned(1))) UnalignedT;
  59. *reinterpret_cast<UnalignedT *>(out_pVal) = in_val;
  60. #elif defined(_MSC_VER) && !defined(AK_CPU_X86)
  61. *reinterpret_cast<T __unaligned *>(out_pVal) = in_val; // __unaligned not supported on 32-bit x86
  62. #else
  63. *reinterpret_cast<T *>(out_pVal) = in_val;
  64. #endif
  65. }
  66. template<
  67. typename T,
  68. typename std::enable_if<std::is_class<T>::value, bool>::type = true
  69. >
  70. inline void WriteUnaligned(AkUInt8* out_pVal, const T& in_val)
  71. {
  72. AKPLATFORM::AkMemCpy(out_pVal, &in_val, sizeof(T));
  73. }
  74. /// Read data from bank and advance pointer.
  75. template< typename T >
  76. inline T ReadBankData(
  77. AkUInt8*& in_rptr
  78. #ifdef _DEBUG
  79. , AkUInt32& in_rSize
  80. #endif
  81. )
  82. {
  83. T l_Value = ReadUnaligned<T>(in_rptr);
  84. in_rptr += sizeof(T);
  85. #ifdef _DEBUG
  86. in_rSize -= sizeof(T);
  87. #endif
  88. return l_Value;
  89. }
  90. template< typename T >
  91. inline T ReadVariableSizeBankData(
  92. AkUInt8*& in_rptr
  93. #ifdef _DEBUG
  94. , AkUInt32& in_rSize
  95. #endif
  96. )
  97. {
  98. AkUInt32 l_Value = 0;
  99. AkUInt8 currentByte = *in_rptr;
  100. ++in_rptr;
  101. #ifdef _DEBUG
  102. --in_rSize;
  103. #endif
  104. l_Value = (currentByte & 0x7F);
  105. while (0x80 & currentByte)
  106. {
  107. currentByte = *in_rptr;
  108. ++in_rptr;
  109. #ifdef _DEBUG
  110. --in_rSize;
  111. #endif
  112. l_Value = l_Value << 7;
  113. l_Value |= (currentByte & 0x7F);
  114. }
  115. return (T)l_Value;
  116. }
  117. inline char * ReadBankStringUtf8(
  118. AkUInt8*& in_rptr
  119. #ifdef _DEBUG
  120. , AkUInt32& in_rSize
  121. #endif
  122. , AkUInt32& out_uStringSize)
  123. {
  124. char* pString = (char *)in_rptr;
  125. out_uStringSize = 0;
  126. while (*in_rptr != 0)
  127. {
  128. ++in_rptr;
  129. #ifdef _DEBUG
  130. --in_rSize;
  131. #endif
  132. ++out_uStringSize;
  133. }
  134. ++in_rptr;
  135. #ifdef _DEBUG
  136. --in_rSize;
  137. #endif
  138. return pString;
  139. }
  140. }
  141. #ifdef _DEBUG
  142. /// Read and return bank data of a given type, incrementing running pointer and decrementing block size for debug tracking purposes
  143. #define READBANKDATA( _Type, _Ptr, _Size ) \
  144. AK::ReadBankData<_Type>( _Ptr, _Size )
  145. #define READVARIABLESIZEBANKDATA( _Type, _Ptr, _Size ) \
  146. AK::ReadVariableSizeBankData<_Type>( _Ptr, _Size )
  147. /// Read and return a null-terminated UTF-8 string, written with the DataWriter of \ref AK::Wwise::Plugin::CustomData::GetPluginData or
  148. /// \ref AK::Wwise::Plugin::AudioPlugin::GetBankParameters, and stored in bank, and its size.
  149. /// \ref wwiseplugin_bank
  150. #define READBANKSTRING( _Ptr, _Size, _out_StringSize ) \
  151. AK::ReadBankStringUtf8( _Ptr, _Size, _out_StringSize )
  152. /// Skip over some bank data of a given type, incrementing running pointer and decrementing block size for debug tracking purposes
  153. #define SKIPBANKDATA( _Type, _Ptr, _Size ) \
  154. ( _Ptr ) += sizeof( _Type ); \
  155. ( _Size ) -= sizeof( _Type )
  156. /// Skip over some bank data by a given size in bytes, incrementing running pointer and decrementing block size for debug tracking purposes
  157. #define SKIPBANKBYTES( _NumBytes, _Ptr, _Size ) \
  158. ( _Size ) -= _NumBytes; \
  159. ( _Ptr ) += _NumBytes
  160. /// Read and copy to a null-terminated UTF-8 string conversion from string stored in bank.
  161. #define COPYBANKSTRING_CHAR( _Ptr, _Size, _OutPtr, _MaxOutPtrSize ) \
  162. AKPLATFORM::SafeStrCpy( _OutPtr, ((const char*)_Ptr), (_MaxOutPtrSize) ); \
  163. SKIPBANKBYTES( (AkUInt32)strlen((const char*)_Ptr) + 1, _Ptr, _Size )
  164. /// Read and copy to a null-terminated OSChar string conversion from string stored in bank.
  165. #define COPYBANKSTRING_OSCHAR( _Ptr, _Size, _OutPtr, _MaxOutPtrSize ) \
  166. AK_UTF8_TO_OSCHAR( _OutPtr, ((const char*)_Ptr), (_MaxOutPtrSize) ); \
  167. SKIPBANKBYTES( (AkUInt32)strlen((const char*)_Ptr) + 1, _Ptr, _Size )
  168. /// Read and copy to a null-terminated wchar_t string conversion from string stored in bank.
  169. #define COPYBANKSTRING_WCHAR( _Ptr, _Size, OutPtr, _MaxOutPtrSize ) \
  170. AK_CHAR_TO_UTF16( _OutPtr, ((const char*)_Ptr), (_MaxOutPtrSize) ); \
  171. SKIPBANKBYTES( (AkUInt32)strlen((const char*)_Ptr) + 1, _Ptr, _Size )
  172. #else
  173. /// Read and return bank data of a given type, incrementing running pointer and decrementing block size for debug tracking purposes
  174. #define READBANKDATA( _Type, _Ptr, _Size ) \
  175. AK::ReadBankData<_Type>( _Ptr )
  176. #define READVARIABLESIZEBANKDATA( _Type, _Ptr, _Size ) \
  177. AK::ReadVariableSizeBankData<_Type>( _Ptr )
  178. #define READBANKSTRING( _Ptr, _Size, _out_StringSize ) \
  179. AK::ReadBankStringUtf8( _Ptr, _out_StringSize )
  180. /// Skip over some bank data of a given type, incrementing running pointer and decrementing block size for debug tracking purposes
  181. #define SKIPBANKDATA( _Type, _Ptr, _Size ) \
  182. ( _Ptr ) += sizeof( _Type )
  183. /// Skip over some bank data by a given size in bytes, incrementing running pointer and decrementing block size for debug tracking purposes
  184. #define SKIPBANKBYTES( _NumBytes, _Ptr, _Size ) \
  185. ( _Ptr ) += _NumBytes;
  186. /// Read and copy to a null-terminated UTF-8 string conversion from string stored in bank.
  187. #define COPYBANKSTRING_CHAR( _Ptr, _Size, _OutPtr, _MaxPtrSize ) \
  188. AKPLATFORM::SafeStrCpy( _OutPtr, (const char*)_Ptr, _MaxPtrSize ); \
  189. SKIPBANKBYTES( (AkUInt32)strlen((const char*)_Ptr)+1, _Ptr, _Size )
  190. /// Read and copy to a null-terminated OSChar string conversion from string stored in bank.
  191. #define COPYBANKSTRING_OSCHAR( _Ptr, _Size, _OutPtr, _MaxPtrSize ) \
  192. AK_UTF8_TO_OSCHAR( _OutPtr, (const char*)_Ptr, _MaxPtrSize ); \
  193. SKIPBANKBYTES( (AkUInt32)strlen((const char*)_Ptr)+1, _Ptr, _Size )
  194. /// Read and copy to a null-terminated wchar_t string conversion from string stored in bank.
  195. #define COPYBANKSTRING_WCHAR( _Ptr, _Size, OutPtr, _MaxPtrSize ) \
  196. AK_CHAR_TO_UTF16( _OutPtr, (const char*)_Ptr, _MaxPtrSize ); \
  197. SKIPBANKBYTES( (AkUInt32)strlen((const char*)_Ptr)+1, _Ptr, _Size )
  198. #endif
  199. #define GETBANKDATABIT( _Data, _Shift ) \
  200. (((_Data) >> (_Shift)) & 0x1)
  201. /// Helper macro to determine whether the full content of a block of memory was properly parsed
  202. #ifdef _DEBUG
  203. #define CHECKBANKDATASIZE( _DATASIZE_, _ERESULT_ ) AKASSERT( _DATASIZE_ == 0 || _ERESULT_ != AK_Success );
  204. #else
  205. #define CHECKBANKDATASIZE(_DATASIZE_, _ERESULT_ )
  206. #endif
  207. #endif //_AK_BANKREADHELPERS_H_