AkStateValue.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 "AkStateValue.h"
  16. #include "Wwise/WwiseResourceLoader.h"
  17. #include "Wwise/Stats/AkAudio.h"
  18. #if WITH_EDITORONLY_DATA
  19. #include "Wwise/WwiseProjectDatabase.h"
  20. #include "Wwise/WwiseResourceCooker.h"
  21. #include "AkAudioDevice.h"
  22. #endif
  23. void UAkStateValue::LoadGroupValue()
  24. {
  25. SCOPED_AKAUDIO_EVENT_2(TEXT("UAkStateValue::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("UAkStateValue::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::State)))
  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 UAkStateValue::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::State);
  75. }
  76. CookedDataToArchive.Serialize(Ar);
  77. }
  78. #else
  79. GroupValueCookedData.Serialize(Ar);
  80. #endif
  81. #endif
  82. }
  83. #if WITH_EDITORONLY_DATA
  84. void UAkStateValue::FillInfo()
  85. {
  86. auto* ResourceCooker = FWwiseResourceCooker::GetDefault();
  87. if (UNLIKELY(!ResourceCooker))
  88. {
  89. UE_LOG(LogAkAudio, Error, TEXT("UAkStateValue::FillInfo: ResourceCooker not initialized"));
  90. return;
  91. }
  92. auto ProjectDatabase = ResourceCooker->GetProjectDatabase();
  93. if (UNLIKELY(!ProjectDatabase))
  94. {
  95. UE_LOG(LogAkAudio, Error, TEXT("UAkStateValue::FillInfo: ProjectDatabase not initialized"));
  96. return;
  97. }
  98. FWwiseGroupValueInfo* AudioTypeInfo = static_cast<FWwiseGroupValueInfo*>(GetInfoMutable());
  99. FWwiseRefState RefState = FWwiseDataStructureScopeLock(*ProjectDatabase).GetState(
  100. GetValidatedInfo(*AudioTypeInfo));
  101. if (RefState.StateName().ToString().IsEmpty() || !RefState.StateGuid().IsValid() || RefState.StateId() == AK_INVALID_UNIQUE_ID)
  102. {
  103. UE_LOG(LogAkAudio, Warning, TEXT("UAkStateValue::FillInfo: Valid object not found in Project Database"));
  104. return;
  105. }
  106. AudioTypeInfo->WwiseName = RefState.StateName();
  107. AudioTypeInfo->WwiseGuid = RefState.StateGuid();
  108. AudioTypeInfo->WwiseShortId = RefState.StateId();
  109. AudioTypeInfo->GroupShortId = RefState.StateGroupId();
  110. }
  111. void UAkStateValue::FillInfo(const FWwiseAnyRef& CurrentWwiseRef)
  112. {
  113. FWwiseGroupValueInfo* AudioTypeInfo = static_cast<FWwiseGroupValueInfo*>(GetInfoMutable());
  114. AudioTypeInfo->WwiseName = CurrentWwiseRef.GetName();
  115. AudioTypeInfo->WwiseGuid = CurrentWwiseRef.GetGuid();
  116. AudioTypeInfo->WwiseShortId = CurrentWwiseRef.GetId();
  117. AudioTypeInfo->GroupShortId = CurrentWwiseRef.GetGroupId();
  118. }
  119. FName UAkStateValue::GetWwiseGroupName()
  120. {
  121. auto* ResourceCooker = FWwiseResourceCooker::GetDefault();
  122. if (UNLIKELY(!ResourceCooker))
  123. {
  124. UE_LOG(LogAkAudio, Error, TEXT("UAkStateValue::FillInfo: ResourceCooker not initialized"));
  125. return {};
  126. }
  127. auto ProjectDatabase = ResourceCooker->GetProjectDatabase();
  128. if (UNLIKELY(!ProjectDatabase))
  129. {
  130. UE_LOG(LogAkAudio, Error, TEXT("UAkStateValue::FillInfo: ProjectDatabase not initialized"));
  131. return {};
  132. }
  133. FWwiseGroupValueInfo* AudioTypeInfo = static_cast<FWwiseGroupValueInfo*>(GetInfoMutable());
  134. FWwiseRefState RefState = FWwiseDataStructureScopeLock(*ProjectDatabase).GetState(
  135. GetValidatedInfo(*AudioTypeInfo));
  136. return RefState.StateGroupName();
  137. }
  138. bool UAkStateValue::ObjectIsInSoundBanks()
  139. {
  140. auto* ResourceCooker = FWwiseResourceCooker::GetDefault();
  141. if (UNLIKELY(!ResourceCooker))
  142. {
  143. UE_LOG(LogAkAudio, Error, TEXT("UAkStateValue::GetWwiseRef: ResourceCooker not initialized"));
  144. return false;
  145. }
  146. auto ProjectDatabase = ResourceCooker->GetProjectDatabase();
  147. if (UNLIKELY(!ProjectDatabase))
  148. {
  149. UE_LOG(LogAkAudio, Error, TEXT("UAkStateValue::GetWwiseRef: ProjectDatabase not initialized"));
  150. return false;
  151. }
  152. FWwiseGroupValueInfo* AudioTypeInfo = static_cast<FWwiseGroupValueInfo*>(GetInfoMutable());
  153. FWwiseRefState RefState = FWwiseDataStructureScopeLock(*ProjectDatabase).GetState(
  154. GetValidatedInfo(*AudioTypeInfo));
  155. return RefState.IsValid();
  156. }
  157. #endif