AkBankReadHelpers.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. static_assert(std::is_trivially_copyable<T>::value, "Unaligned operations require being trivially copiable");
  48. T result;
  49. AKPLATFORM::AkMemCpy(&result, in_pVal, sizeof(T));
  50. return result;
  51. }
  52. template<
  53. typename T,
  54. typename std::enable_if<std::is_fundamental<T>::value || std::is_enum<T>::value, bool>::type = true
  55. >
  56. inline void WriteUnaligned(AkUInt8* out_pVal, const T in_val)
  57. {
  58. #if defined(__GNUC__)
  59. #if defined(__has_warning)
  60. #if __has_warning("-Walign-mismatch")
  61. #define __IGNORE_RECENT_CLANG_WARNINGS
  62. #pragma clang diagnostic push
  63. #pragma clang diagnostic ignored "-Walign-mismatch"
  64. #endif
  65. #endif
  66. typedef T __attribute__((aligned(1))) UnalignedT;
  67. *reinterpret_cast<UnalignedT *>(out_pVal) = in_val;
  68. #ifdef __IGNORE_RECENT_CLANG_WARNINGS
  69. #undef __IGNORE_RECENT_CLANG_WARNINGS
  70. #pragma clang diagnostic pop
  71. #endif
  72. #elif defined(_MSC_VER) && !defined(AK_CPU_X86)
  73. *reinterpret_cast<T __unaligned *>(out_pVal) = in_val; // __unaligned not supported on 32-bit x86
  74. #else
  75. *reinterpret_cast<T *>(out_pVal) = in_val;
  76. #endif
  77. }
  78. template<
  79. typename T,
  80. typename std::enable_if<std::is_class<T>::value, bool>::type = true
  81. >
  82. inline void WriteUnaligned(AkUInt8* out_pVal, const T& in_val)
  83. {
  84. static_assert(std::is_trivially_copyable<T>::value, "Unaligned operations require being trivially copiable");
  85. AKPLATFORM::AkMemCpy(out_pVal, &in_val, sizeof(T));
  86. }
  87. /// Read data from bank and advance pointer.
  88. template< typename T >
  89. inline T ReadBankData(
  90. AkUInt8*& in_rptr
  91. #ifdef _DEBUG
  92. , AkUInt32& in_rSize
  93. #endif
  94. )
  95. {
  96. T l_Value = ReadUnaligned<T>(in_rptr);
  97. in_rptr += sizeof(T);
  98. #ifdef _DEBUG
  99. in_rSize -= sizeof(T);
  100. #endif
  101. return l_Value;
  102. }
  103. template< typename T >
  104. inline T ReadVariableSizeBankData(
  105. AkUInt8*& in_rptr
  106. #ifdef _DEBUG
  107. , AkUInt32& in_rSize
  108. #endif
  109. )
  110. {
  111. AkUInt32 l_Value = 0;
  112. AkUInt8 currentByte = *in_rptr;
  113. ++in_rptr;
  114. #ifdef _DEBUG
  115. --in_rSize;
  116. #endif
  117. l_Value = (currentByte & 0x7F);
  118. while (0x80 & currentByte)
  119. {
  120. currentByte = *in_rptr;
  121. ++in_rptr;
  122. #ifdef _DEBUG
  123. --in_rSize;
  124. #endif
  125. l_Value = l_Value << 7;
  126. l_Value |= (currentByte & 0x7F);
  127. }
  128. return (T)l_Value;
  129. }
  130. inline char * ReadBankStringUtf8(
  131. AkUInt8*& in_rptr
  132. #ifdef _DEBUG
  133. , AkUInt32& in_rSize
  134. #endif
  135. , AkUInt32& out_uStringSize)
  136. {
  137. char* pString = (char *)in_rptr;
  138. out_uStringSize = 0;
  139. while (*in_rptr != 0)
  140. {
  141. ++in_rptr;
  142. #ifdef _DEBUG
  143. --in_rSize;
  144. #endif
  145. ++out_uStringSize;
  146. }
  147. ++in_rptr;
  148. #ifdef _DEBUG
  149. --in_rSize;
  150. #endif
  151. return pString;
  152. }
  153. }
  154. #ifdef _DEBUG
  155. /// Read and return bank data of a given type, incrementing running pointer and decrementing block size for debug tracking purposes
  156. #define READBANKDATA( _Type, _Ptr, _Size ) \
  157. AK::ReadBankData<_Type>( _Ptr, _Size )
  158. #define READVARIABLESIZEBANKDATA( _Type, _Ptr, _Size ) \
  159. AK::ReadVariableSizeBankData<_Type>( _Ptr, _Size )
  160. /// Read and return a null-terminated UTF-8 string, written with the DataWriter of \ref AK::Wwise::Plugin::CustomData::GetPluginData or
  161. /// \ref AK::Wwise::Plugin::AudioPlugin::GetBankParameters, and stored in bank, and its size.
  162. /// \ref wwiseplugin_bank
  163. #define READBANKSTRING( _Ptr, _Size, _out_StringSize ) \
  164. AK::ReadBankStringUtf8( _Ptr, _Size, _out_StringSize )
  165. /// Skip over some bank data of a given type, incrementing running pointer and decrementing block size for debug tracking purposes
  166. #define SKIPBANKDATA( _Type, _Ptr, _Size ) \
  167. ( _Ptr ) += sizeof( _Type ); \
  168. ( _Size ) -= sizeof( _Type )
  169. /// Skip over some bank data by a given size in bytes, incrementing running pointer and decrementing block size for debug tracking purposes
  170. #define SKIPBANKBYTES( _NumBytes, _Ptr, _Size ) \
  171. ( _Size ) -= _NumBytes; \
  172. ( _Ptr ) += _NumBytes
  173. /// Read and copy to a null-terminated UTF-8 string conversion from string stored in bank.
  174. #define COPYBANKSTRING_CHAR( _Ptr, _Size, _OutPtr, _MaxOutPtrSize ) \
  175. AKPLATFORM::SafeStrCpy( _OutPtr, ((const char*)_Ptr), (_MaxOutPtrSize) ); \
  176. SKIPBANKBYTES( (AkUInt32)strlen((const char*)_Ptr) + 1, _Ptr, _Size )
  177. /// Read and copy to a null-terminated OSChar string conversion from string stored in bank.
  178. #define COPYBANKSTRING_OSCHAR( _Ptr, _Size, _OutPtr, _MaxOutPtrSize ) \
  179. AK_UTF8_TO_OSCHAR( _OutPtr, ((const char*)_Ptr), (_MaxOutPtrSize) ); \
  180. SKIPBANKBYTES( (AkUInt32)strlen((const char*)_Ptr) + 1, _Ptr, _Size )
  181. /// Read and copy to a null-terminated wchar_t string conversion from string stored in bank.
  182. #define COPYBANKSTRING_WCHAR( _Ptr, _Size, OutPtr, _MaxOutPtrSize ) \
  183. AK_CHAR_TO_UTF16( _OutPtr, ((const char*)_Ptr), (_MaxOutPtrSize) ); \
  184. SKIPBANKBYTES( (AkUInt32)strlen((const char*)_Ptr) + 1, _Ptr, _Size )
  185. #else
  186. /// Read and return bank data of a given type, incrementing running pointer and decrementing block size for debug tracking purposes
  187. #define READBANKDATA( _Type, _Ptr, _Size ) \
  188. AK::ReadBankData<_Type>( _Ptr )
  189. #define READVARIABLESIZEBANKDATA( _Type, _Ptr, _Size ) \
  190. AK::ReadVariableSizeBankData<_Type>( _Ptr )
  191. #define READBANKSTRING( _Ptr, _Size, _out_StringSize ) \
  192. AK::ReadBankStringUtf8( _Ptr, _out_StringSize )
  193. /// Skip over some bank data of a given type, incrementing running pointer and decrementing block size for debug tracking purposes
  194. #define SKIPBANKDATA( _Type, _Ptr, _Size ) \
  195. ( _Ptr ) += sizeof( _Type )
  196. /// Skip over some bank data by a given size in bytes, incrementing running pointer and decrementing block size for debug tracking purposes
  197. #define SKIPBANKBYTES( _NumBytes, _Ptr, _Size ) \
  198. ( _Ptr ) += _NumBytes;
  199. /// Read and copy to a null-terminated UTF-8 string conversion from string stored in bank.
  200. #define COPYBANKSTRING_CHAR( _Ptr, _Size, _OutPtr, _MaxPtrSize ) \
  201. AKPLATFORM::SafeStrCpy( _OutPtr, (const char*)_Ptr, _MaxPtrSize ); \
  202. SKIPBANKBYTES( (AkUInt32)strlen((const char*)_Ptr)+1, _Ptr, _Size )
  203. /// Read and copy to a null-terminated OSChar string conversion from string stored in bank.
  204. #define COPYBANKSTRING_OSCHAR( _Ptr, _Size, _OutPtr, _MaxPtrSize ) \
  205. AK_UTF8_TO_OSCHAR( _OutPtr, (const char*)_Ptr, _MaxPtrSize ); \
  206. SKIPBANKBYTES( (AkUInt32)strlen((const char*)_Ptr)+1, _Ptr, _Size )
  207. /// Read and copy to a null-terminated wchar_t string conversion from string stored in bank.
  208. #define COPYBANKSTRING_WCHAR( _Ptr, _Size, OutPtr, _MaxPtrSize ) \
  209. AK_CHAR_TO_UTF16( _OutPtr, (const char*)_Ptr, _MaxPtrSize ); \
  210. SKIPBANKBYTES( (AkUInt32)strlen((const char*)_Ptr)+1, _Ptr, _Size )
  211. #endif
  212. #define GETBANKDATABIT( _Data, _Shift ) \
  213. (((_Data) >> (_Shift)) & 0x1)
  214. /// Helper macro to determine whether the full content of a block of memory was properly parsed
  215. #ifdef _DEBUG
  216. #define CHECKBANKDATASIZE( _DATASIZE_, _ERESULT_ ) AKASSERT( _DATASIZE_ == 0 || _ERESULT_ != AK_Success );
  217. #else
  218. #define CHECKBANKDATASIZE(_DATASIZE_, _ERESULT_ )
  219. #endif
  220. #endif //_AK_BANKREADHELPERS_H_