AkSwitchValue.cpp 5.9 KB

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