WwiseMetadataLoader.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 "Dom/JsonObject.h"
  17. #include "Wwise/Metadata/WwiseMetadataLoadable.h"
  18. #include "Wwise/Metadata/WwiseMetadataGameParameter.h"
  19. enum class EWwiseRequiredMetadata
  20. {
  21. Optional,
  22. Mandatory
  23. };
  24. struct FWwiseMetadataLoader
  25. {
  26. bool bResult;
  27. const TSharedRef<FJsonObject>& JsonObject;
  28. FWwiseMetadataLoader(const TSharedRef<FJsonObject>& InJsonObject) :
  29. bResult(true),
  30. JsonObject(InJsonObject)
  31. {
  32. }
  33. void Fail(const TCHAR* FieldName);
  34. void LogParsed(const TCHAR* FieldName, const uint32 Id = 0, const FName Name = FName());
  35. bool GetBool(FWwiseMetadataLoadable* Object, const FString& FieldName, EWwiseRequiredMetadata Required = EWwiseRequiredMetadata::Mandatory);
  36. float GetFloat(FWwiseMetadataLoadable* Object, const FString& FieldName, EWwiseRequiredMetadata Required = EWwiseRequiredMetadata::Mandatory);
  37. FGuid GetGuid(FWwiseMetadataLoadable* Object, const FString& FieldName, EWwiseRequiredMetadata Required = EWwiseRequiredMetadata::Mandatory);
  38. FName GetString(FWwiseMetadataLoadable* Object, const FString& FieldName, EWwiseRequiredMetadata Required = EWwiseRequiredMetadata::Mandatory);
  39. uint32 GetUint32(FWwiseMetadataLoadable* Object, const FString& FieldName, EWwiseRequiredMetadata Required = EWwiseRequiredMetadata::Mandatory);
  40. template <typename T>
  41. T GetObject(FWwiseMetadataLoadable* Object, const FString& FieldName);
  42. template <typename T>
  43. T* GetObjectPtr(FWwiseMetadataLoadable* Object, const FString& FieldName);
  44. template<typename T>
  45. TArray<T> GetArray(FWwiseMetadataLoadable* Object, const FString& FieldName);
  46. template<typename T>
  47. void GetPropertyArray(T* Object, const TMap<FName, size_t>& FloatProperties);
  48. };
  49. template<typename T>
  50. T FWwiseMetadataLoader::GetObject(FWwiseMetadataLoadable* Object, const FString& FieldName)
  51. {
  52. check(Object);
  53. Object->AddRequestedValue(TEXT("object"), FieldName);
  54. const TSharedPtr<FJsonObject>* InnerObject;
  55. if (!JsonObject->TryGetObjectField(FieldName, InnerObject))
  56. {
  57. Fail(*FieldName);
  58. return T{};
  59. }
  60. auto SharedRef(InnerObject->ToSharedRef());
  61. FWwiseMetadataLoader ObjectLoader(SharedRef);
  62. T Result(ObjectLoader);
  63. if (ObjectLoader.bResult)
  64. {
  65. Result.CheckRequestedValues(SharedRef);
  66. }
  67. else
  68. {
  69. bResult = false;
  70. LogParsed(*FieldName);
  71. }
  72. return Result;
  73. }
  74. template <typename T>
  75. T* FWwiseMetadataLoader::GetObjectPtr(FWwiseMetadataLoadable* Object, const FString& FieldName)
  76. {
  77. check(Object);
  78. Object->AddRequestedValue(TEXT("optional object"), FieldName);
  79. const TSharedPtr<FJsonObject>* InnerObject;
  80. if (!JsonObject->TryGetObjectField(FieldName, InnerObject))
  81. {
  82. return nullptr;
  83. }
  84. auto SharedRef(InnerObject->ToSharedRef());
  85. FWwiseMetadataLoader ObjectLoader(SharedRef);
  86. T* Result = new T(ObjectLoader);
  87. if (ObjectLoader.bResult)
  88. {
  89. if (Result)
  90. {
  91. Result->CheckRequestedValues(SharedRef);
  92. }
  93. }
  94. else
  95. {
  96. bResult = false;
  97. LogParsed(*FieldName);
  98. delete Result;
  99. return nullptr;
  100. }
  101. return Result;
  102. }
  103. template <typename T>
  104. TArray<T> FWwiseMetadataLoader::GetArray(FWwiseMetadataLoadable* Object, const FString& FieldName)
  105. {
  106. check(Object);
  107. Object->AddRequestedValue(TEXT("array"), FieldName);
  108. const TArray< TSharedPtr<FJsonValue> >* Array;
  109. if (!JsonObject->TryGetArrayField(FieldName, Array))
  110. {
  111. // No data. Not a fail, valid!
  112. Object->IncLoadedSize(sizeof(TArray<T>));
  113. return TArray<T>{};
  114. }
  115. TArray<T> Result;
  116. Result.Empty(Array->Num());
  117. for (auto& InnerObject : *Array)
  118. {
  119. const TSharedPtr<FJsonObject>* InnerJsonObjectPtr;
  120. if (!InnerObject->TryGetObject(InnerJsonObjectPtr))
  121. {
  122. LogParsed(*FieldName);
  123. continue;
  124. }
  125. auto SharedRef(InnerJsonObjectPtr->ToSharedRef());
  126. FWwiseMetadataLoader ArrayLoader(SharedRef);
  127. T ResultObject(ArrayLoader);
  128. if (ArrayLoader.bResult)
  129. {
  130. ResultObject.CheckRequestedValues(SharedRef);
  131. }
  132. else
  133. {
  134. bResult = false;
  135. ArrayLoader.LogParsed(*FieldName);
  136. Result.Empty();
  137. break;
  138. }
  139. Result.Add(MoveTemp(ResultObject));
  140. }
  141. Object->IncLoadedSize(sizeof(TArray<T>));
  142. return Result;
  143. }
  144. template <typename T>
  145. void FWwiseMetadataLoader::GetPropertyArray(T* Object, const TMap<FName, size_t>& FloatProperties)
  146. {
  147. check(Object);
  148. Object->AddRequestedValue(TEXT("propertyarray"), TEXT("Properties"));
  149. Object->IncLoadedSize(FloatProperties.Num() * sizeof(float));
  150. const TArray< TSharedPtr<FJsonValue> >* Array;
  151. if (!JsonObject->TryGetArrayField(TEXT("Properties"), Array))
  152. {
  153. // No data. Not a fail, valid!
  154. return;
  155. }
  156. for (auto& InnerObject : *Array)
  157. {
  158. const TSharedPtr<FJsonObject>* InnerJsonObjectPtr;
  159. if (!InnerObject->TryGetObject(InnerJsonObjectPtr))
  160. {
  161. continue;
  162. }
  163. const auto SharedRef(InnerJsonObjectPtr->ToSharedRef());
  164. FString Name;
  165. if (!SharedRef->TryGetStringField(TEXT("Name"), Name))
  166. {
  167. Fail(TEXT("Property::Name"));
  168. continue;
  169. }
  170. FString Type;
  171. if (!SharedRef->TryGetStringField(TEXT("Type"), Type) || Type != TEXT("Real32"))
  172. {
  173. Fail(TEXT("Property::Type"));
  174. continue;
  175. }
  176. double Value;
  177. if (!SharedRef->TryGetNumberField(TEXT("Value"), Value))
  178. {
  179. Fail(TEXT("Property::Value"));
  180. continue;
  181. }
  182. if (const auto* Property = FloatProperties.Find(FName(Name)))
  183. {
  184. *(float*)((intptr_t)Object + *Property) = Value;
  185. }
  186. else
  187. {
  188. Fail(*Name);
  189. continue;
  190. }
  191. }
  192. }