AkSwitchValue.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 "AkSwitchValue.h"
  16. #include "Wwise/Stats/AkAudio.h"
  17. #include "Wwise/WwiseResourceLoader.h"
  18. #if WITH_EDITORONLY_DATA
  19. #include "Wwise/WwiseProjectDatabase.h"
  20. #include "Wwise/WwiseResourceCooker.h"
  21. #include "AkAudioDevice.h"
  22. #endif
  23. void UAkSwitchValue::LoadGroupValue()
  24. {
  25. SCOPED_AKAUDIO_EVENT_2(TEXT("UAkSwitchValue::LoadGroupValue"));
  26. auto* ResourceLoader = FWwiseResourceLoader::Get();
  27. if (UNLIKELY(!ResourceLoader))
  28. {
  29. return;
  30. }
  31. UnloadGroupValue(false);
  32. #if WITH_EDITORONLY_DATA
  33. if (!IWwiseProjectDatabaseModule::ShouldInitializeProjectDatabase())
  34. {
  35. return;
  36. }
  37. auto* ProjectDatabase = FWwiseProjectDatabase::Get();
  38. if (!ProjectDatabase || !ProjectDatabase->IsProjectDatabaseParsed())
  39. {
  40. UE_LOG(LogAkAudio, VeryVerbose, TEXT("UAkSwitchValue::LoadGroupValue: Not loading '%s' because project database is not parsed."), *GetName())
  41. return;
  42. }
  43. auto* ResourceCooker = FWwiseResourceCooker::GetDefault();
  44. if (UNLIKELY(!ResourceCooker))
  45. {
  46. return;
  47. }
  48. if (UNLIKELY(!ResourceCooker->PrepareCookedData(GroupValueCookedData, GetValidatedInfo(GroupValueInfo), EWwiseGroupType::Switch)))
  49. {
  50. return;
  51. }
  52. #endif
  53. const auto NewlyLoadedGroupValue = ResourceLoader->LoadGroupValue(GroupValueCookedData);
  54. auto PreviouslyLoadedGroupValue = LoadedGroupValue.exchange(NewlyLoadedGroupValue);
  55. if (UNLIKELY(PreviouslyLoadedGroupValue))
  56. {
  57. ResourceLoader->UnloadGroupValue(MoveTemp(PreviouslyLoadedGroupValue));
  58. }
  59. }
  60. void UAkSwitchValue::Serialize(FArchive& Ar)
  61. {
  62. Super::Serialize(Ar);
  63. if (HasAnyFlags(RF_ClassDefaultObject))
  64. {
  65. return;
  66. }
  67. #if !UE_SERVER
  68. #if WITH_EDITORONLY_DATA
  69. if (Ar.IsCooking() && Ar.IsSaving() && !Ar.CookingTarget()->IsServerOnly())
  70. {
  71. FWwiseGroupValueCookedData CookedDataToArchive;
  72. if (auto* ResourceCooker = FWwiseResourceCooker::GetForArchive(Ar))
  73. {
  74. ResourceCooker->PrepareCookedData(CookedDataToArchive, GetValidatedInfo(GroupValueInfo), EWwiseGroupType::Switch);
  75. }
  76. CookedDataToArchive.Serialize(Ar);
  77. }
  78. #else
  79. GroupValueCookedData.Serialize(Ar);
  80. #endif
  81. #endif
  82. }
  83. #if WITH_EDITORONLY_DATA
  84. void UAkSwitchValue::FillInfo()
  85. {
  86. auto* ResourceCooker = FWwiseResourceCooker::GetDefault();
  87. if (UNLIKELY(!ResourceCooker))
  88. {
  89. UE_LOG(LogAkAudio, Error, TEXT("UAkSwitchValue::FillInfo: ResourceCooker not initialized"));
  90. return;
  91. }
  92. auto ProjectDatabase = ResourceCooker->GetProjectDatabase();
  93. if (UNLIKELY(!ProjectDatabase))
  94. {
  95. UE_LOG(LogAkAudio, Error, TEXT("UAkSwitchValue::FillInfo: ProjectDatabase not initialized"));
  96. return;
  97. }
  98. FWwiseGroupValueInfo* AudioTypeInfo = static_cast<FWwiseGroupValueInfo*>(GetInfoMutable());
  99. FWwiseRefSwitch RefSwitch = FWwiseDataStructureScopeLock(*ProjectDatabase).GetSwitch(
  100. GetValidatedInfo(*AudioTypeInfo));
  101. if (RefSwitch.SwitchName().ToString().IsEmpty() || !RefSwitch.SwitchGuid().IsValid() || RefSwitch.SwitchId() == AK_INVALID_UNIQUE_ID)
  102. {
  103. UE_LOG(LogAkAudio, Warning, TEXT("UAkSwitchValue::FillInfo: Valid object not found in Project Database"));
  104. return;
  105. }
  106. FWwiseAnyRef Ref = FWwiseAnyRef::Create(RefSwitch);
  107. AudioTypeInfo->WwiseName = RefSwitch.SwitchName();
  108. AudioTypeInfo->WwiseGuid = RefSwitch.SwitchGuid();
  109. AudioTypeInfo->WwiseShortId = RefSwitch.SwitchId();
  110. AudioTypeInfo->GroupShortId = RefSwitch.SwitchGroupId();
  111. }
  112. void UAkSwitchValue::FillInfo(const FWwiseAnyRef& CurrentWwiseRef)
  113. {
  114. FWwiseGroupValueInfo* AudioTypeInfo = static_cast<FWwiseGroupValueInfo*>(GetInfoMutable());
  115. AudioTypeInfo->WwiseName = CurrentWwiseRef.GetName();
  116. AudioTypeInfo->WwiseGuid = CurrentWwiseRef.GetGuid();
  117. AudioTypeInfo->WwiseShortId = CurrentWwiseRef.GetId();
  118. AudioTypeInfo->GroupShortId = CurrentWwiseRef.GetGroupId();
  119. }
  120. bool UAkSwitchValue::ObjectIsInSoundBanks()
  121. {
  122. auto* ResourceCooker = FWwiseResourceCooker::GetDefault();
  123. if (UNLIKELY(!ResourceCooker))
  124. {
  125. UE_LOG(LogAkAudio, Error, TEXT("UAkSwitchValue::GetWwiseRef: ResourceCooker not initialized"));
  126. return false;
  127. }
  128. auto ProjectDatabase = ResourceCooker->GetProjectDatabase();
  129. if (UNLIKELY(!ProjectDatabase))
  130. {
  131. UE_LOG(LogAkAudio, Error, TEXT("UAkSwitchValue::GetWwiseRef: ProjectDatabase not initialized"));
  132. return false;
  133. }
  134. FWwiseGroupValueInfo* AudioTypeInfo = static_cast<FWwiseGroupValueInfo*>(GetInfoMutable());
  135. FWwiseRefSwitch RefSwitch = FWwiseDataStructureScopeLock(*ProjectDatabase).GetSwitch(
  136. GetValidatedInfo(*AudioTypeInfo));
  137. return RefSwitch.IsValid();
  138. }
  139. FName UAkSwitchValue::GetWwiseGroupName()
  140. {
  141. auto* ResourceCooker = FWwiseResourceCooker::GetDefault();
  142. if (UNLIKELY(!ResourceCooker))
  143. {
  144. UE_LOG(LogAkAudio, Error, TEXT("UAkSwitchValue::GetWwiseRef: ResourceCooker not initialized"));
  145. return {};
  146. }
  147. auto ProjectDatabase = ResourceCooker->GetProjectDatabase();
  148. if (UNLIKELY(!ProjectDatabase))
  149. {
  150. UE_LOG(LogAkAudio, Error, TEXT("UAkSwitchValue::GetWwiseRef: ProjectDatabase not initialized"));
  151. return {};
  152. }
  153. FWwiseGroupValueInfo* AudioTypeInfo = static_cast<FWwiseGroupValueInfo*>(GetInfoMutable());
  154. FWwiseRefSwitch RefSwitch = FWwiseDataStructureScopeLock(*ProjectDatabase).GetSwitch(
  155. GetValidatedInfo(*AudioTypeInfo));
  156. return RefSwitch.SwitchGroupName();
  157. }
  158. #endif