AkComponentCallbackManager.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #pragma once
  16. #include "AkAudioDevice.h"
  17. #include "WwiseUnrealDefines.h"
  18. class IAkUserEventCallbackPackage
  19. {
  20. public:
  21. /** Copy of the user callback flags, for use in our own callback */
  22. uint32 uUserFlags;
  23. uint32 KeyHash;
  24. bool HasExternalSources = false;
  25. IAkUserEventCallbackPackage()
  26. : uUserFlags(0)
  27. {}
  28. IAkUserEventCallbackPackage(uint32 in_Flags, uint32 in_Hash, bool in_HasExternalSources)
  29. : uUserFlags(in_Flags)
  30. , KeyHash(in_Hash)
  31. , HasExternalSources(in_HasExternalSources)
  32. {}
  33. virtual ~IAkUserEventCallbackPackage() {}
  34. virtual void HandleAction(AkCallbackType in_eType, AkCallbackInfo* in_pCallbackInfo) = 0;
  35. virtual void CancelCallback() {};
  36. };
  37. class FAkFunctionPtrEventCallbackPackage : public IAkUserEventCallbackPackage
  38. {
  39. public:
  40. FAkFunctionPtrEventCallbackPackage(AkCallbackFunc CbFunc, void* Cookie, uint32 Flags, uint32 in_Hash, bool in_HasExternalSources)
  41. : IAkUserEventCallbackPackage(Flags, in_Hash, in_HasExternalSources)
  42. , pfnUserCallback(CbFunc)
  43. , pUserCookie(Cookie)
  44. {}
  45. virtual void HandleAction(AkCallbackType in_eType, AkCallbackInfo* in_pCallbackInfo) override;
  46. virtual void CancelCallback() override;
  47. private:
  48. /** Copy of the user callback, for use in our own callback */
  49. AkCallbackFunc pfnUserCallback;
  50. /** Copy of the user cookie, for use in our own callback */
  51. void* pUserCookie;
  52. };
  53. class FAkBlueprintDelegateEventCallbackPackage : public IAkUserEventCallbackPackage
  54. {
  55. public:
  56. FAkBlueprintDelegateEventCallbackPackage(FOnAkPostEventCallback PostEventCallback, uint32 Flags, uint32 in_Hash, bool in_HasExternalSources)
  57. : IAkUserEventCallbackPackage(Flags, in_Hash, in_HasExternalSources)
  58. , BlueprintCallback(PostEventCallback)
  59. {}
  60. virtual void HandleAction(AkCallbackType in_eType, AkCallbackInfo* in_pCallbackInfo) override;
  61. virtual void CancelCallback() override;
  62. private:
  63. FOnAkPostEventCallback BlueprintCallback;
  64. };
  65. class FAkLatentActionEventCallbackPackage : public IAkUserEventCallbackPackage
  66. {
  67. public:
  68. FAkLatentActionEventCallbackPackage(FWaitEndOfEventAction* LatentAction, uint32 in_Hash, bool in_HasExternalSources)
  69. : IAkUserEventCallbackPackage(AK_EndOfEvent, in_Hash, in_HasExternalSources)
  70. , EndOfEventLatentAction(LatentAction)
  71. {
  72. LatentActionValidityToken = MakeShared<FPendingLatentActionValidityToken, ESPMode::ThreadSafe>();
  73. EndOfEventLatentAction->ValidityToken = LatentActionValidityToken;
  74. }
  75. virtual void HandleAction(AkCallbackType in_eType, AkCallbackInfo* in_pCallbackInfo) override;
  76. private:
  77. TSharedPtr<FPendingLatentActionValidityToken, ESPMode::ThreadSafe> LatentActionValidityToken;
  78. FWaitEndOfEventAction* EndOfEventLatentAction;
  79. };
  80. class FAkComponentCallbackManager
  81. {
  82. public:
  83. static FAkComponentCallbackManager* GetInstance();
  84. static FAkComponentCallbackManager* Instance;
  85. /** Our own event callback */
  86. static void AkComponentCallback(AkCallbackType in_eType, AkCallbackInfo* in_pCallbackInfo);
  87. FAkComponentCallbackManager();
  88. ~FAkComponentCallbackManager();
  89. IAkUserEventCallbackPackage* CreateCallbackPackage(AkCallbackFunc in_cbFunc, void* in_Cookie, uint32 in_Flags, AkGameObjectID in_gameObjID, bool HasExternalSources);
  90. IAkUserEventCallbackPackage* CreateCallbackPackage(FOnAkPostEventCallback BlueprintCallback, uint32 in_Flags, AkGameObjectID in_gameObjID, bool HasExternalSources);
  91. IAkUserEventCallbackPackage* CreateCallbackPackage(FWaitEndOfEventAction* LatentAction, AkGameObjectID in_gameObjID, bool HasExternalSources);
  92. void RemoveCallbackPackage(IAkUserEventCallbackPackage* in_Package, AkGameObjectID in_gameObjID);
  93. void CancelEventCallback(void* in_Cookie);
  94. void CancelEventCallback(const FOnAkPostEventCallback& in_Delegate);
  95. void RegisterGameObject(AkGameObjectID in_gameObjID);
  96. void UnregisterGameObject(AkGameObjectID in_gameObjID);
  97. bool HasActiveEvents(AkGameObjectID in_gameObjID);
  98. private:
  99. typedef TSet<IAkUserEventCallbackPackage*> PackageSet;
  100. void RemovePackageFromSet(PackageSet* in_pPackageSet, IAkUserEventCallbackPackage* in_pPackage, AkGameObjectID in_gameObjID);
  101. FCriticalSection CriticalSection;
  102. typedef WwiseUnrealHelper::AkGameObjectIdKeyFuncs<PackageSet, false> PackageSetGameObjectIDKeyFuncs;
  103. TMap<AkGameObjectID, PackageSet, FDefaultSetAllocator, PackageSetGameObjectIDKeyFuncs> GameObjectToPackagesMap;
  104. // Used for quick lookup in cancel
  105. uint32 inline GetKeyHash(void* Key);
  106. uint32 inline GetKeyHash(const FOnAkPostEventCallback& Key);
  107. void CancelKeyHash(uint32 HashToCancel);
  108. TMultiMap<uint32, IAkUserEventCallbackPackage*> UserCookieHashToPackageMap;
  109. };