AkInitBank.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 "AkInitBank.h"
  16. #include "AkSettings.h"
  17. #include "Platforms/AkPlatformInfo.h"
  18. #include "Wwise/WwiseResourceLoader.h"
  19. #include "Wwise/Stats/AkAudio.h"
  20. #if WITH_EDITORONLY_DATA
  21. #include "Wwise/WwiseResourceCooker.h"
  22. #endif
  23. #if WITH_EDITORONLY_DATA
  24. void UAkInitBank::CookAdditionalFilesOverride(const TCHAR* PackageFilename, const ITargetPlatform* TargetPlatform,
  25. TFunctionRef<void(const TCHAR* Filename, void* Data, int64 Size)> WriteAdditionalFile)
  26. {
  27. auto* ResourceCooker = FWwiseResourceCooker::GetForPlatform(TargetPlatform);
  28. if (!ResourceCooker)
  29. {
  30. return;
  31. }
  32. ResourceCooker->SetSandboxRootPath(PackageFilename);
  33. ResourceCooker->CookInitBank(FWwiseObjectInfo::DefaultInitBank, WriteAdditionalFile);
  34. }
  35. void UAkInitBank::BeginCacheForCookedPlatformData(const ITargetPlatform* TargetPlatform)
  36. {
  37. if (auto* AkSettings = GetDefault<UAkSettings>())
  38. {
  39. if (AkSettings->AreSoundBanksGenerated())
  40. {
  41. auto PlatformID = UAkPlatformInfo::GetSharedPlatformInfo(TargetPlatform->IniPlatformName());
  42. FWwiseResourceCooker::CreateForPlatform(TargetPlatform, PlatformID, EWwiseExportDebugNameRule::Name);
  43. }
  44. }
  45. }
  46. #endif
  47. void UAkInitBank::Serialize(FArchive& Ar)
  48. {
  49. bAutoLoad = false;
  50. Super::Serialize(Ar);
  51. if (HasAnyFlags(RF_ClassDefaultObject))
  52. {
  53. return;
  54. }
  55. #if !UE_SERVER
  56. #if WITH_EDITORONLY_DATA
  57. if (Ar.IsCooking() && Ar.IsSaving() && !Ar.CookingTarget()->IsServerOnly())
  58. {
  59. FWwiseInitBankCookedData CookedDataToArchive;
  60. if (auto* ResourceCooker = FWwiseResourceCooker::GetForArchive(Ar))
  61. {
  62. ResourceCooker->PrepareCookedData(CookedDataToArchive, FWwiseObjectInfo::DefaultInitBank);
  63. }
  64. CookedDataToArchive.Serialize(Ar);
  65. }
  66. #else
  67. InitBankCookedData.Serialize(Ar);
  68. #endif
  69. #endif
  70. }
  71. void UAkInitBank::UnloadInitBank(bool bAsync)
  72. {
  73. auto PreviouslyLoadedInitBank = LoadedInitBank.exchange(nullptr);
  74. if (PreviouslyLoadedInitBank)
  75. {
  76. auto* ResourceLoader = FWwiseResourceLoader::Get();
  77. if (UNLIKELY(!ResourceLoader))
  78. {
  79. return;
  80. }
  81. if (bAsync)
  82. {
  83. FWwiseLoadedInitBankPromise Promise;
  84. Promise.EmplaceValue(MoveTemp(PreviouslyLoadedInitBank));
  85. ResourceUnload = ResourceLoader->UnloadInitBankAsync(Promise.GetFuture());
  86. }
  87. else
  88. {
  89. ResourceLoader->UnloadInitBank(MoveTemp(PreviouslyLoadedInitBank));
  90. }
  91. }
  92. }
  93. #if WITH_EDITORONLY_DATA
  94. void UAkInitBank::PrepareCookedData()
  95. {
  96. if (!IWwiseProjectDatabaseModule::ShouldInitializeProjectDatabase())
  97. {
  98. return;
  99. }
  100. auto* ResourceCooker = FWwiseResourceCooker::GetDefault();
  101. if (UNLIKELY(!ResourceCooker))
  102. {
  103. return;
  104. }
  105. if (UNLIKELY(!ResourceCooker->PrepareCookedData(InitBankCookedData, FWwiseObjectInfo::DefaultInitBank)))
  106. {
  107. return;
  108. }
  109. }
  110. #endif
  111. TArray<FWwiseLanguageCookedData> UAkInitBank::GetLanguages()
  112. {
  113. #if WITH_EDITORONLY_DATA
  114. PrepareCookedData();
  115. #endif
  116. return InitBankCookedData.Language;
  117. }
  118. void UAkInitBank::UnloadData(bool bAsync)
  119. {
  120. UnloadInitBank(bAsync);
  121. }
  122. void UAkInitBank::LoadInitBank()
  123. {
  124. SCOPED_AKAUDIO_EVENT_2(TEXT("LoadInitBank"));
  125. auto* ResourceLoader = FWwiseResourceLoader::Get();
  126. if (UNLIKELY(!ResourceLoader))
  127. {
  128. return;
  129. }
  130. UnloadInitBank(false);
  131. #if WITH_EDITORONLY_DATA
  132. PrepareCookedData();
  133. #endif
  134. const auto NewlyLoadedInitBank = ResourceLoader->LoadInitBank(InitBankCookedData);
  135. auto PreviouslyLoadedInitBank = LoadedInitBank.exchange(NewlyLoadedInitBank);
  136. if (UNLIKELY(PreviouslyLoadedInitBank))
  137. {
  138. ResourceLoader->UnloadInitBank(MoveTemp(PreviouslyLoadedInitBank));
  139. }
  140. }
  141. #if WITH_EDITORONLY_DATA
  142. void UAkInitBank::MigrateWwiseObjectInfo()
  143. {
  144. //Do nothing because the DefaultInitBank info is used
  145. }
  146. FWwiseObjectInfo* UAkInitBank::GetInfoMutable()
  147. {
  148. return new FWwiseObjectInfo(FWwiseObjectInfo::DefaultInitBank.WwiseGuid, FWwiseObjectInfo::DefaultInitBank.WwiseShortId, FWwiseObjectInfo::DefaultInitBank.WwiseName);
  149. }
  150. #endif