WwiseSoundBankInfoCache.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*******************************************************************************
  2. The content of this file includes portions of the proprietary AUDIOKINETIC Wwise
  3. Technology released in source code form as part of the game integration package.
  4. The content of this file may not be used without valid licenses to the
  5. AUDIOKINETIC Wwise Technology.
  6. Note that the use of the game engine is subject to the Unreal(R) Engine End User
  7. License Agreement at https://www.unrealengine.com/en-US/eula/unreal
  8. License Usage
  9. Licensees holding valid licenses to the AUDIOKINETIC Wwise Technology may use
  10. this file in accordance with the end user license agreement provided with the
  11. software or, alternatively, in accordance with the terms contained
  12. in a written agreement between you and Audiokinetic Inc.
  13. Copyright (c) 2023 Audiokinetic Inc.
  14. *******************************************************************************/
  15. #include "WwiseSoundBankInfoCache.h"
  16. #include "AssetManagement/AkAssetDatabase.h"
  17. #include "WwiseDefines.h"
  18. #include "AkUnrealHelper.h"
  19. #include "HAL/FileManager.h"
  20. #if UE_5_0_OR_LATER
  21. #include "HAL/PlatformFileManager.h"
  22. #else
  23. #include "HAL/PlatformFilemanager.h"
  24. #endif
  25. #include "Misc/Paths.h"
  26. #include "AssetManagement/WwiseProjectInfo.h"
  27. namespace WwiseSoundBankInfoCacheHelpers
  28. {
  29. constexpr auto CacheVersion = 1;
  30. constexpr auto SFXLanguageID = 393239870;
  31. }
  32. bool WwiseSoundBankInfoCache::Load(const FString& Path)
  33. {
  34. WwiseProjectInfo wwiseProjectInfo;
  35. wwiseProjectInfo.Parse();
  36. for (auto& platform : wwiseProjectInfo.GetSupportedPlatforms())
  37. {
  38. platformNameToGuidMap.Add(platform.Name, platform.ID);
  39. }
  40. auto& platformFile = FPlatformFileManager::Get().GetPlatformFile();
  41. TUniquePtr<FArchive> ar(IFileManager::Get().CreateFileReader(*Path));
  42. uint32 readVersion = 0;
  43. *ar << readVersion;
  44. if (readVersion != WwiseSoundBankInfoCacheHelpers::CacheVersion)
  45. {
  46. return false;
  47. }
  48. uint32 flagsHash = 0;
  49. *ar << flagsHash;
  50. FGuid currentPlatform;
  51. uint32 currentLanguage = 0;
  52. CacheType currentCacheType = static_cast<CacheType>(0);
  53. SerializeState serializeState = SerializeState::None;
  54. *ar << serializeState;
  55. while (serializeState != SerializeState::EndOfData)
  56. {
  57. switch (serializeState)
  58. {
  59. case SerializeState::None:
  60. case SerializeState::EndOfData:
  61. break;
  62. case SerializeState::Platform:
  63. {
  64. readGuid(*ar, currentPlatform);
  65. break;
  66. }
  67. case SerializeState::Language:
  68. {
  69. *ar << currentLanguage;
  70. break;
  71. }
  72. case SerializeState::BankInfo:
  73. {
  74. FString name;
  75. readString(*ar, name);
  76. auto key = CacheKey(name, currentPlatform, currentLanguage);
  77. BankInfo& info = bankInfoMap.Emplace(key);
  78. *ar << info;
  79. break;
  80. }
  81. case SerializeState::InfoFile:
  82. {
  83. FString name;
  84. readString(*ar, name);
  85. FileInfo info;
  86. *ar << info;
  87. break;
  88. }
  89. case SerializeState::InfoFileType:
  90. {
  91. *ar << currentCacheType;
  92. break;
  93. }
  94. }
  95. *ar << serializeState;
  96. }
  97. return true;
  98. }
  99. void WwiseSoundBankInfoCache::readString(FArchive& Ar, FString& Value)
  100. {
  101. uint32 stringSize = 0;
  102. Ar << stringSize;
  103. TArray<uint8> rawString;
  104. rawString.SetNumUninitialized(stringSize);
  105. Ar.Serialize(rawString.GetData(), stringSize);
  106. FUTF8ToTCHAR utf8(reinterpret_cast<const char*>(rawString.GetData()), stringSize);
  107. Value = FString(utf8.Length(), utf8.Get());
  108. }
  109. void WwiseSoundBankInfoCache::readGuid(FArchive& Ar, FGuid& Id)
  110. {
  111. uint32 data1;
  112. uint16 data2;
  113. uint16 data3;
  114. uint8 data4[8];
  115. Ar << data1;
  116. Ar << data2;
  117. Ar << data3;
  118. Ar.Serialize(data4, sizeof(data4));
  119. Id.A = data1;
  120. Id.B = data3 | (data2 << 16);
  121. Id.C = data4[3] | (data4[2] << 8) | (data4[1] << 16) | (data4[0] << 24);
  122. Id.D = data4[7] | (data4[6] << 8) | (data4[5] << 16) | (data4[4] << 24);
  123. }
  124. void WwiseSoundBankInfoCache::readBool(FArchive& Ar, bool& Value)
  125. {
  126. uint8 temp = 0;
  127. Ar.Serialize(&temp, 1);
  128. Value = temp > 0;
  129. }
  130. bool WwiseSoundBankInfoCache::IsSoundBankUpToUpdate(const FGuid& Id, const FString& Platform, const FString& Language, const uint32 Hash) const
  131. {
  132. FString bankName = AkUnrealHelper::GuidToBankName(Id);
  133. FGuid platformGuid;
  134. if (auto platformIt = platformNameToGuidMap.Find(Platform))
  135. {
  136. platformGuid = *platformIt;
  137. }
  138. uint32 languageId = WwiseSoundBankInfoCacheHelpers::SFXLanguageID;
  139. if (Language.Len() > 0)
  140. {
  141. AK::FNVHash32 hash;
  142. FTCHARToUTF8 utf8(*Language.ToLower());
  143. languageId = hash.Compute(utf8.Get(), utf8.Length());
  144. }
  145. CacheKey key(bankName, platformGuid, languageId);
  146. if (auto* cacheEntry = bankInfoMap.Find(key))
  147. {
  148. return cacheEntry->Hash == Hash;
  149. }
  150. return false;
  151. }
  152. FArchive& operator<<(FArchive& Ar, WwiseSoundBankInfoCache::FileInfo& Value)
  153. {
  154. Ar << Value.Hash;
  155. Ar << Value.Timestamp;
  156. WwiseSoundBankInfoCache::readBool(Ar, Value.Updated);
  157. return Ar;
  158. }
  159. FArchive& operator<<(FArchive& Ar, WwiseSoundBankInfoCache::BankInfo& Value)
  160. {
  161. Ar << static_cast<WwiseSoundBankInfoCache::FileInfo&>(Value);
  162. Ar << Value.Stats;
  163. WwiseSoundBankInfoCache::readBool(Ar, Value.IsTemporary);
  164. return Ar;
  165. }
  166. FArchive& operator<<(FArchive& Ar, WwiseSoundBankInfoCache::MemoryStats& Value)
  167. {
  168. Ar << Value.Timestamp;
  169. Ar << Value.DataSize;
  170. Ar << Value.FileSize;
  171. Ar << Value.DecodedSize;
  172. Ar << Value.SFXPreFetchSize;
  173. Ar << Value.SFXInMemorySize;
  174. Ar << Value.SFXMissingFiles;
  175. Ar << Value.MusicPreFetchSize;
  176. Ar << Value.MusicInMemorySize;
  177. Ar << Value.MusicMissingFiles;
  178. Ar << Value.VoicePreFetchSize;
  179. Ar << Value.VoiceInMemorySize;
  180. Ar << Value.VoiceMissingFiles;
  181. Ar << Value.ReplacedFiles;
  182. return Ar;
  183. }
  184. uint32 GetTypeHash(const WwiseSoundBankInfoCache::CacheKey& Key)
  185. {
  186. AK::FNVHash32 hash;
  187. hash.Compute(*Key.Name, sizeof(TCHAR) * Key.Name.Len());
  188. hash.Compute(&Key.Platform, sizeof(FGuid));
  189. hash.Compute(&Key.Language, sizeof(uint32));
  190. return hash.Get();
  191. }