AkStateValue.cpp 5.7 KB

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