AkAssetDatabase.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 "AssetManagement/AkAssetDatabase.h"
  16. #include "AkAudioType.h"
  17. #include "AkAcousticTexture.h"
  18. #include "AkAudioEvent.h"
  19. #include "AkAuxBus.h"
  20. #include "AkRtpc.h"
  21. #include "AkStateValue.h"
  22. #include "AkSwitchValue.h"
  23. #include "AkTrigger.h"
  24. #include "WwiseUnrealHelper.h"
  25. #include "AkAudioDevice.h"
  26. #include "AkSettingsPerUser.h"
  27. #include "IAudiokineticTools.h"
  28. #include "AssetRegistry/AssetRegistryModule.h"
  29. #include "AssetToolsModule.h"
  30. #include "Async/Async.h"
  31. #include "Misc/FeedbackContext.h"
  32. #include "ObjectTools.h"
  33. #include "Logging/LogMacros.h"
  34. #define LOCTEXT_NAMESPACE "AkAudio"
  35. AkAssetDatabase& AkAssetDatabase::Get()
  36. {
  37. static AkAssetDatabase instance;
  38. return instance;
  39. }
  40. AkAssetDatabase::AkAssetDatabase()
  41. {
  42. AssetRegistryModule = &FModuleManager::LoadModuleChecked<FAssetRegistryModule>("AssetRegistry");
  43. AssetToolsModule = &FModuleManager::LoadModuleChecked<FAssetToolsModule>("AssetTools");
  44. }
  45. bool AkAssetDatabase::FindAllAssets(TArray<FAssetData>& OutData)
  46. {
  47. #if UE_5_1_OR_LATER
  48. AssetRegistryModule->Get().GetAssetsByClass(UAkAudioType::StaticClass()->GetClassPathName(), OutData, true);
  49. #else
  50. AssetRegistryModule->Get().GetAssetsByClass(UAkAudioType::StaticClass()->GetFName(), OutData, true);
  51. #endif
  52. return OutData.Num() > 0;
  53. }
  54. bool AkAssetDatabase::FindAssets(const FGuid& AkGuid, TArray<FAssetData>& OutData)
  55. {
  56. TMultiMap<FName, FString> Search;
  57. Search.Add(GET_MEMBER_NAME_CHECKED(FWwiseObjectInfo, WwiseGuid), AkGuid.ToString(EGuidFormats::Digits));
  58. AssetRegistryModule->Get().GetAssetsByTagValues(Search, OutData);
  59. return OutData.Num() > 0;
  60. }
  61. bool AkAssetDatabase::FindAssets(const FString& AssetName, TArray<FAssetData>& OutData)
  62. {
  63. TMultiMap<FName, FString> Search;
  64. Search.Add(GET_MEMBER_NAME_CHECKED(FAssetData, AssetName), AssetName);
  65. AssetRegistryModule->Get().GetAssetsByTagValues(Search, OutData);
  66. return OutData.Num() > 0;
  67. }
  68. FAssetData AkAssetDatabase::FindAssetByObjectPath(const FSoftObjectPath& AssetPath)
  69. {
  70. #if UE_5_1_OR_LATER
  71. return AssetRegistryModule->Get().GetAssetByObjectPath(AssetPath);
  72. #else
  73. return AssetRegistryModule->Get().GetAssetByObjectPath(AssetPath.GetAssetPathName());
  74. #endif
  75. }
  76. bool AkAssetDatabase::FindFirstAsset(const FGuid& AkGuid, FAssetData& OutAsset)
  77. {
  78. TArray<FAssetData> Assets;
  79. if (FindAssets(AkGuid, Assets))
  80. {
  81. OutAsset = Assets[0];
  82. return true;
  83. }
  84. return false;
  85. }
  86. bool AkAssetDatabase::FindFirstAsset(const FString& AssetName, FAssetData& OutAsset)
  87. {
  88. TArray<FAssetData> Assets;
  89. if (FindAssets(AssetName, Assets))
  90. {
  91. OutAsset = Assets[0];
  92. return true;
  93. }
  94. return false;
  95. }
  96. bool AkAssetDatabase::FindAssetsByGuidAndClass(const FGuid& AkGuid, const UClass* StaticClass, TArray<FAssetData>& OutWwiseAssets)
  97. {
  98. TMultiMap<FName, FString> Search;
  99. FARFilter Filter;
  100. #if UE_5_1_OR_LATER
  101. Filter.ClassPaths.Add(StaticClass->GetClassPathName());
  102. #else
  103. Filter.ClassNames.Add(StaticClass->GetFName());
  104. #endif
  105. Filter.bRecursiveClasses = true;
  106. Filter.TagsAndValues.AddUnique(GET_MEMBER_NAME_CHECKED(FWwiseObjectInfo, WwiseGuid), AkGuid.ToString(EGuidFormats::Digits));
  107. AssetRegistryModule->Get().GetAssets(Filter, OutWwiseAssets);
  108. return OutWwiseAssets.Num() > 0;
  109. }
  110. bool AkAssetDatabase::RenameAsset(const FGuid& Id, const FString& AssetName,
  111. const FString& RelativePath)
  112. {
  113. check(IsInGameThread());
  114. auto parentPath = RelativePath;
  115. TArray<FAssetData> AssetData;
  116. if (!FindAssets(Id, AssetData))
  117. {
  118. UE_LOG(LogAudiokineticTools, Verbose, TEXT("Can't find Wwise asset to rename with ID %s"), *Id.ToString());
  119. return false;
  120. }
  121. TArray<FAssetRenameData> AssetsToRename;
  122. for (FAssetData Asset : AssetData)
  123. {
  124. if (Asset.AssetName.ToString() != AssetName || parentPath != Asset.PackagePath.ToString())
  125. {
  126. if (parentPath.IsEmpty())
  127. {
  128. parentPath = Asset.PackagePath.ToString();
  129. }
  130. FAssetRenameData NewAssetRenameData(Asset.GetAsset(), parentPath, AssetName);
  131. AssetsToRename.Add(NewAssetRenameData);
  132. UE_LOG(LogAudiokineticTools, Verbose, TEXT("Renaming Wwise asset %s"), *AssetName);
  133. }
  134. }
  135. if (!AssetToolsModule->Get().RenameAssets(AssetsToRename))
  136. {
  137. UE_LOG(LogAudiokineticTools, Error, TEXT("Failed to rename Wwise Assets"));
  138. return false;
  139. }
  140. return true;
  141. }
  142. void AkAssetDatabase::DeleteAsset(const FGuid& Id)
  143. {
  144. check(IsInGameThread());
  145. TArray<FAssetData> AssetsToDelete;
  146. if (FindAssets(Id, AssetsToDelete))
  147. {
  148. ObjectTools::DeleteAssets(AssetsToDelete, true);
  149. }
  150. }
  151. void AkAssetDatabase::DeleteAssets(const TSet<FGuid>& AssetsId)
  152. {
  153. for (auto& ID : AssetsId)
  154. {
  155. DeleteAsset(ID);
  156. }
  157. }
  158. void AkAssetDatabase::FixUpRedirectors(const FString& AssetPackagePath)
  159. {
  160. TArray<UObjectRedirector*> redirectorsToFix;
  161. TArray<FAssetData> foundRedirectorsData;
  162. #if UE_5_1_OR_LATER
  163. AssetRegistryModule->Get().GetAssetsByClass(UObjectRedirector::StaticClass()->GetClassPathName(), foundRedirectorsData);
  164. #else
  165. AssetRegistryModule->Get().GetAssetsByClass(UObjectRedirector::StaticClass()->GetFName(), foundRedirectorsData);
  166. #endif
  167. if (foundRedirectorsData.Num() > 0)
  168. {
  169. for (auto& entry : foundRedirectorsData)
  170. {
  171. if (auto redirector = Cast<UObjectRedirector>(entry.GetAsset()))
  172. {
  173. if (redirector->DestinationObject)
  174. {
  175. auto pathName = redirector->DestinationObject->GetPathName();
  176. if (pathName.StartsWith(AssetPackagePath))
  177. {
  178. redirectorsToFix.Add(redirector);
  179. }
  180. }
  181. }
  182. }
  183. }
  184. if (redirectorsToFix.Num() > 0)
  185. {
  186. AssetToolsModule->Get().FixupReferencers(redirectorsToFix);
  187. }
  188. }
  189. bool AkAssetDatabase::IsAkAudioType(const FAssetData& AssetData)
  190. {
  191. #if UE_5_1_OR_LATER
  192. static const TArray<FTopLevelAssetPath> AkAudioClassPaths = {
  193. UAkAcousticTexture::StaticClass()->GetClassPathName(),
  194. UAkAudioEvent::StaticClass()->GetClassPathName(),
  195. UAkAuxBus::StaticClass()->GetClassPathName(),
  196. UAkRtpc::StaticClass()->GetClassPathName(),
  197. UAkStateValue::StaticClass()->GetClassPathName(),
  198. UAkSwitchValue::StaticClass()->GetClassPathName(),
  199. UAkTrigger::StaticClass()->GetClassPathName()
  200. };
  201. if (AkAudioClassPaths.Contains(AssetData.AssetClassPath))
  202. return true;
  203. #else
  204. static const TArray<FName> AkAudioClassNames = {
  205. UAkAcousticTexture::StaticClass()->GetFName(),
  206. UAkAudioEvent::StaticClass()->GetFName(),
  207. UAkAuxBus::StaticClass()->GetFName(),
  208. UAkRtpc::StaticClass()->GetFName(),
  209. UAkStateValue::StaticClass()->GetFName(),
  210. UAkSwitchValue::StaticClass()->GetFName(),
  211. UAkTrigger::StaticClass()->GetFName()
  212. };
  213. if (AkAudioClassNames.Contains(AssetData.AssetClass))
  214. return true;
  215. #endif
  216. return false;
  217. }
  218. bool AkAssetDatabase::CheckIfLoadingAssets()
  219. {
  220. return AssetRegistryModule->Get().IsLoadingAssets();
  221. }
  222. #undef LOCTEXT_NAMESPACE