AkEffectShareSet.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 "AkEffectShareSet.h"
  16. #include "Wwise/WwiseResourceLoader.h"
  17. #include "Wwise/Stats/AkAudio.h"
  18. #if WITH_EDITORONLY_DATA
  19. #include "AkAudioDevice.h"
  20. #include "Wwise/WwiseProjectDatabase.h"
  21. #include "Wwise/WwiseResourceCooker.h"
  22. #endif
  23. void UAkEffectShareSet::Serialize(FArchive& Ar)
  24. {
  25. Super::Serialize(Ar);
  26. if (HasAnyFlags(RF_ClassDefaultObject))
  27. {
  28. return;
  29. }
  30. #if !UE_SERVER
  31. #if WITH_EDITORONLY_DATA
  32. if (Ar.IsCooking() && Ar.IsSaving() && !Ar.CookingTarget()->IsServerOnly())
  33. {
  34. FWwiseLocalizedShareSetCookedData CookedDataToArchive;
  35. if (auto* ResourceCooker = FWwiseResourceCooker::GetForArchive(Ar))
  36. {
  37. ResourceCooker->PrepareCookedData(CookedDataToArchive, GetValidatedInfo(ShareSetInfo));
  38. }
  39. CookedDataToArchive.Serialize(Ar);
  40. }
  41. #else
  42. ShareSetCookedData.Serialize(Ar);
  43. #endif
  44. #endif
  45. }
  46. void UAkEffectShareSet::LoadEffectShareSet()
  47. {
  48. SCOPED_AKAUDIO_EVENT_2(TEXT("LoadEffectShareSet"));
  49. auto* ResourceLoader = FWwiseResourceLoader::Get();
  50. if (UNLIKELY(!ResourceLoader))
  51. {
  52. return;
  53. }
  54. UnloadEffectShareSet(false);
  55. #if WITH_EDITORONLY_DATA
  56. if (!IWwiseProjectDatabaseModule::ShouldInitializeProjectDatabase())
  57. {
  58. return;
  59. }
  60. auto* ProjectDatabase = FWwiseProjectDatabase::Get();
  61. if (!ProjectDatabase || !ProjectDatabase->IsProjectDatabaseParsed())
  62. {
  63. UE_LOG(LogAkAudio, VeryVerbose, TEXT("UAkEffectShareSet::LoadEffectShareSet: Not loading '%s' because project database is not parsed."), *GetName())
  64. return;
  65. }
  66. auto* ResourceCooker = FWwiseResourceCooker::GetDefault();
  67. if (UNLIKELY(!ResourceCooker))
  68. {
  69. return;
  70. }
  71. if (UNLIKELY(!ResourceCooker->PrepareCookedData(ShareSetCookedData, GetValidatedInfo(ShareSetInfo))))
  72. {
  73. return;
  74. }
  75. #endif
  76. const auto NewlyLoadedShareSet = ResourceLoader->LoadShareSet(ShareSetCookedData);
  77. auto PreviouslyLoadedShareSet = LoadedShareSet.exchange(NewlyLoadedShareSet);
  78. if (UNLIKELY(PreviouslyLoadedShareSet))
  79. {
  80. ResourceLoader->UnloadShareSet(MoveTemp(PreviouslyLoadedShareSet));
  81. }
  82. }
  83. void UAkEffectShareSet::UnloadEffectShareSet(bool bAsync)
  84. {
  85. auto PreviouslyLoadedShareSet = LoadedShareSet.exchange(nullptr);
  86. if (PreviouslyLoadedShareSet)
  87. {
  88. auto* ResourceLoader = FWwiseResourceLoader::Get();
  89. if (UNLIKELY(!ResourceLoader))
  90. {
  91. return;
  92. }
  93. if (bAsync)
  94. {
  95. FWwiseLoadedShareSetPromise Promise;
  96. Promise.EmplaceValue(MoveTemp(PreviouslyLoadedShareSet));
  97. ResourceUnload = ResourceLoader->UnloadShareSetAsync(Promise.GetFuture());
  98. }
  99. else
  100. {
  101. ResourceLoader->UnloadShareSet(MoveTemp(PreviouslyLoadedShareSet));
  102. }
  103. }
  104. }
  105. #if WITH_EDITORONLY_DATA
  106. bool UAkEffectShareSet::ObjectIsInSoundBanks()
  107. {
  108. auto* ResourceCooker = FWwiseResourceCooker::GetDefault();
  109. if (UNLIKELY(!ResourceCooker))
  110. {
  111. UE_LOG(LogAkAudio, Error, TEXT("UAkEffectShareSet::GetWwiseRef: ResourceCooker not initialized"));
  112. return false;
  113. }
  114. auto ProjectDatabase = ResourceCooker->GetProjectDatabase();
  115. if (UNLIKELY(!ProjectDatabase))
  116. {
  117. UE_LOG(LogAkAudio, Error, TEXT("UAkEffectShareSet::GetWwiseRef: ProjectDatabase not initialized"));
  118. return false;
  119. }
  120. FWwiseObjectInfo* AudioTypeInfo = &ShareSetInfo;
  121. const FWwiseRefPluginShareSet AudioTypeRef = FWwiseDataStructureScopeLock(*ProjectDatabase).GetPluginShareSet(
  122. GetValidatedInfo(ShareSetInfo));
  123. return AudioTypeRef.IsValid();
  124. }
  125. void UAkEffectShareSet::CookAdditionalFilesOverride(const TCHAR* PackageFilename, const ITargetPlatform* TargetPlatform,
  126. TFunctionRef<void(const TCHAR* Filename, void* Data, int64 Size)> WriteAdditionalFile)
  127. {
  128. if (HasAnyFlags(RF_ClassDefaultObject))
  129. {
  130. return;
  131. }
  132. FWwiseResourceCooker* ResourceCooker = FWwiseResourceCooker::GetForPlatform(TargetPlatform);
  133. if (!ResourceCooker)
  134. {
  135. return;
  136. }
  137. ResourceCooker->SetSandboxRootPath(PackageFilename);
  138. ResourceCooker->CookShareSet(GetValidatedInfo(ShareSetInfo), WriteAdditionalFile);
  139. }
  140. void UAkEffectShareSet::FillInfo()
  141. {
  142. auto* ResourceCooker = FWwiseResourceCooker::GetDefault();
  143. if (UNLIKELY(!ResourceCooker))
  144. {
  145. UE_LOG(LogAkAudio, Error, TEXT("UAkEffectShareSet::FillInfo: ResourceCooker not initialized"));
  146. return;
  147. }
  148. auto ProjectDatabase = ResourceCooker->GetProjectDatabase();
  149. if (UNLIKELY(!ProjectDatabase))
  150. {
  151. UE_LOG(LogAkAudio, Error, TEXT("UAkEffectShareSet::FillInfo: ProjectDatabase not initialized"));
  152. return;
  153. }
  154. FWwiseObjectInfo* AudioTypeInfo = &ShareSetInfo;
  155. const FWwiseRefPluginShareSet AudioTypeRef = FWwiseDataStructureScopeLock(*ProjectDatabase).GetPluginShareSet(
  156. GetValidatedInfo(ShareSetInfo));
  157. if (AudioTypeRef.PluginShareSetName().ToString().IsEmpty() || !AudioTypeRef.PluginShareSetGuid().IsValid() || AudioTypeRef.PluginShareSetId() == AK_INVALID_UNIQUE_ID)
  158. {
  159. UE_LOG(LogAkAudio, Warning, TEXT("UAkEffectShareSet::FillInfo: Valid object not found in Project Database"));
  160. return;
  161. }
  162. AudioTypeInfo->WwiseName = AudioTypeRef.PluginShareSetName();
  163. AudioTypeInfo->WwiseGuid = AudioTypeRef.PluginShareSetGuid();
  164. AudioTypeInfo->WwiseShortId = AudioTypeRef.PluginShareSetId();
  165. }
  166. #endif