WwiseSoundBankInfoCache.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 "WwiseUnrealDefines.h"
  19. #include "WwiseUnrealHelper.h"
  20. #include "HAL/FileManager.h"
  21. #if UE_5_0_OR_LATER
  22. #include "HAL/PlatformFileManager.h"
  23. #else
  24. #include "HAL/PlatformFilemanager.h"
  25. #endif
  26. #include "Misc/Paths.h"
  27. #include "AssetManagement/WwiseProjectInfo.h"
  28. namespace WwiseSoundBankInfoCacheHelpers
  29. {
  30. constexpr auto CacheVersion = 1;
  31. constexpr auto SFXLanguageID = 393239870;
  32. }
  33. bool WwiseSoundBankInfoCache::Load(const FString& Path)
  34. {
  35. WwiseProjectInfo wwiseProjectInfo;
  36. wwiseProjectInfo.Parse();
  37. for (auto& platform : wwiseProjectInfo.GetSupportedPlatforms())
  38. {
  39. platformNameToGuidMap.Add(platform.Name, platform.ID);
  40. }
  41. auto& platformFile = FPlatformFileManager::Get().GetPlatformFile();
  42. TUniquePtr<FArchive> ar(IFileManager::Get().CreateFileReader(*Path));
  43. uint32 readVersion = 0;
  44. *ar << readVersion;
  45. if (readVersion != WwiseSoundBankInfoCacheHelpers::CacheVersion)
  46. {
  47. return false;
  48. }
  49. uint32 flagsHash = 0;
  50. *ar << flagsHash;
  51. FGuid currentPlatform;
  52. uint32 currentLanguage = 0;
  53. CacheType currentCacheType = static_cast<CacheType>(0);
  54. SerializeState serializeState = SerializeState::None;
  55. *ar << serializeState;
  56. while (serializeState != SerializeState::EndOfData)
  57. {
  58. switch (serializeState)
  59. {
  60. case SerializeState::None:
  61. case SerializeState::EndOfData:
  62. break;
  63. case SerializeState::Platform:
  64. {
  65. readGuid(*ar, currentPlatform);
  66. break;
  67. }
  68. case SerializeState::Language:
  69. {
  70. *ar << currentLanguage;
  71. break;
  72. }
  73. case SerializeState::BankInfo:
  74. {
  75. FString name;
  76. readString(*ar, name);
  77. auto key = CacheKey(name, currentPlatform, currentLanguage);
  78. BankInfo& info = bankInfoMap.Emplace(key);
  79. *ar << info;
  80. break;
  81. }
  82. case SerializeState::InfoFile:
  83. {
  84. FString name;
  85. readString(*ar, name);
  86. FileInfo info;
  87. *ar << info;
  88. break;
  89. }
  90. case SerializeState::InfoFileType:
  91. {
  92. *ar << currentCacheType;
  93. break;
  94. }
  95. }
  96. *ar << serializeState;
  97. }
  98. return true;
  99. }
  100. void WwiseSoundBankInfoCache::readString(FArchive& Ar, FString& Value)
  101. {
  102. uint32 stringSize = 0;
  103. Ar << stringSize;
  104. TArray<uint8> rawString;
  105. rawString.SetNumUninitialized(stringSize);
  106. Ar.Serialize(rawString.GetData(), stringSize);
  107. FUTF8ToTCHAR utf8(reinterpret_cast<const char*>(rawString.GetData()), stringSize);
  108. Value = FString(utf8.Length(), utf8.Get());
  109. }
  110. void WwiseSoundBankInfoCache::readGuid(FArchive& Ar, FGuid& Id)
  111. {
  112. uint32 data1;
  113. uint16 data2;
  114. uint16 data3;
  115. uint8 data4[8];
  116. Ar << data1;
  117. Ar << data2;
  118. Ar << data3;
  119. Ar.Serialize(data4, sizeof(data4));
  120. Id.A = data1;
  121. Id.B = data3 | (data2 << 16);
  122. Id.C = data4[3] | (data4[2] << 8) | (data4[1] << 16) | (data4[0] << 24);
  123. Id.D = data4[7] | (data4[6] << 8) | (data4[5] << 16) | (data4[4] << 24);
  124. }
  125. void WwiseSoundBankInfoCache::readBool(FArchive& Ar, bool& Value)
  126. {
  127. uint8 temp = 0;
  128. Ar.Serialize(&temp, 1);
  129. Value = temp > 0;
  130. }
  131. bool WwiseSoundBankInfoCache::IsSoundBankUpToUpdate(const FGuid& Id, const FString& Platform, const FString& Language, const uint32 Hash) const
  132. {
  133. FString bankName = WwiseUnrealHelper::GuidToBankName(Id);
  134. FGuid platformGuid;
  135. if (auto platformIt = platformNameToGuidMap.Find(Platform))
  136. {
  137. platformGuid = *platformIt;
  138. }
  139. uint32 languageId = WwiseSoundBankInfoCacheHelpers::SFXLanguageID;
  140. if (Language.Len() > 0)
  141. {
  142. AK::FNVHash32 hash;
  143. FTCHARToUTF8 utf8(*Language.ToLower());
  144. languageId = hash.Compute(utf8.Get(), utf8.Length());
  145. }
  146. CacheKey key(bankName, platformGuid, languageId);
  147. if (auto* cacheEntry = bankInfoMap.Find(key))
  148. {
  149. return cacheEntry->Hash == Hash;
  150. }
  151. return false;
  152. }
  153. FArchive& operator<<(FArchive& Ar, WwiseSoundBankInfoCache::FileInfo& Value)
  154. {
  155. Ar << Value.Hash;
  156. Ar << Value.Timestamp;
  157. WwiseSoundBankInfoCache::readBool(Ar, Value.Updated);
  158. return Ar;
  159. }
  160. FArchive& operator<<(FArchive& Ar, WwiseSoundBankInfoCache::BankInfo& Value)
  161. {
  162. Ar << static_cast<WwiseSoundBankInfoCache::FileInfo&>(Value);
  163. Ar << Value.Stats;
  164. WwiseSoundBankInfoCache::readBool(Ar, Value.IsTemporary);
  165. return Ar;
  166. }
  167. FArchive& operator<<(FArchive& Ar, WwiseSoundBankInfoCache::MemoryStats& Value)
  168. {
  169. Ar << Value.Timestamp;
  170. Ar << Value.DataSize;
  171. Ar << Value.FileSize;
  172. Ar << Value.DecodedSize;
  173. Ar << Value.SFXPreFetchSize;
  174. Ar << Value.SFXInMemorySize;
  175. Ar << Value.SFXMissingFiles;
  176. Ar << Value.MusicPreFetchSize;
  177. Ar << Value.MusicInMemorySize;
  178. Ar << Value.MusicMissingFiles;
  179. Ar << Value.VoicePreFetchSize;
  180. Ar << Value.VoiceInMemorySize;
  181. Ar << Value.VoiceMissingFiles;
  182. Ar << Value.ReplacedFiles;
  183. return Ar;
  184. }
  185. uint32 GetTypeHash(const WwiseSoundBankInfoCache::CacheKey& Key)
  186. {
  187. AK::FNVHash32 hash;
  188. hash.Compute(*Key.Name, sizeof(TCHAR) * Key.Name.Len());
  189. hash.Compute(&Key.Platform, sizeof(FGuid));
  190. hash.Compute(&Key.Language, sizeof(uint32));
  191. return hash.Get();
  192. }