WwiseProjectInfo.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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/WwiseProjectInfo.h"
  16. #include "AkAudioDevice.h"
  17. #include "AkSettings.h"
  18. #include "Misc/Paths.h"
  19. #include "XmlFile.h"
  20. #include "Internationalization/Regex.h"
  21. #include "Misc/FileHelper.h"
  22. void WwiseProjectInfo::Parse()
  23. {
  24. SupportedPlatforms.Empty();
  25. SupportedLanguages.Empty();
  26. DefaultLanguage.Empty();
  27. CacheDirectory.Empty();
  28. bProjectInfoParsed = false;
  29. FString ProjectPath = GetProjectPath();
  30. if (ProjectPath.Len() > 0)
  31. {
  32. FText errorMessage;
  33. int32 errorLineNumber;
  34. FString ProjectFileString;
  35. if (!FFileHelper::LoadFileToString(ProjectFileString, *ProjectPath))
  36. {
  37. // UE_LOG(LogAkAudio, Error, TEXT("Could not read the Wwise project file '<%s>'."), *ProjectPath);
  38. return;
  39. }
  40. ParseCacheDirectory(*ProjectFileString);
  41. SanitizeProjectFileString(ProjectFileString);
  42. bProjectInfoParsed = FFastXml::ParseXmlFile(this, nullptr, ProjectFileString.GetCharArray().GetData(), nullptr, false, false, errorMessage, errorLineNumber);
  43. if (DefaultLanguage.Len() == 0 || DefaultLanguage == TEXT(""))
  44. {
  45. DefaultLanguage = TEXT("English(US)");
  46. }
  47. }
  48. }
  49. void WwiseProjectInfo::ParseCacheDirectory(const FString ProjectFileString)
  50. {
  51. FString PatternString(TEXT("<MiscSettingEntry Name=\"Cache\">([\\s\\S]*?)</MiscSettingEntry>"));
  52. FRegexPattern Pattern(PatternString);
  53. FRegexMatcher Matcher(Pattern, ProjectFileString);
  54. if (Matcher.FindNext())
  55. {
  56. CacheDirectory = Matcher.GetCaptureGroup(1);
  57. CacheDirectory.RemoveFromStart(TEXT("<![CDATA["));
  58. CacheDirectory.RemoveFromEnd(TEXT("]]>"));
  59. }
  60. else if (CacheDirectory.IsEmpty())
  61. {
  62. CacheDirectory = TEXT(".cache");
  63. UE_LOG(LogAkAudio, Log, TEXT("Could not parse the Cache directory from the Wwise project file. Using default value : .cache"));
  64. }
  65. if (FPaths::IsRelative(CacheDirectory))
  66. {
  67. CacheDirectory = FPaths::ConvertRelativePathToFull(FPaths::GetPath(GetProjectPath()), CacheDirectory);
  68. }
  69. }
  70. void WwiseProjectInfo::SanitizeProjectFileString(FString& InOutProjectFileString)
  71. {
  72. FString PatternString(TEXT("<\\!\\[CDATA[\\s\\S]*?>"));
  73. FRegexPattern Pattern(PatternString);
  74. FRegexMatcher Matcher(Pattern, InOutProjectFileString);
  75. while (Matcher.FindNext())
  76. {
  77. InOutProjectFileString = InOutProjectFileString.Replace(*Matcher.GetCaptureGroup(0), TEXT(""));
  78. }
  79. }
  80. bool WwiseProjectInfo::ProcessAttribute(const TCHAR* AttributeName, const TCHAR* AttributeValue)
  81. {
  82. if (bInsidePlatformElement)
  83. {
  84. if (FCString::Strcmp(AttributeName, TEXT("Name")) == 0)
  85. {
  86. CurrentPlatformInfo.Name = AttributeValue;
  87. }
  88. else if (FCString::Strcmp(AttributeName, TEXT("ID")) == 0)
  89. {
  90. FGuid::ParseExact(AttributeValue, EGuidFormats::DigitsWithHyphensInBraces, CurrentPlatformInfo.ID);
  91. }
  92. }
  93. if (bInsideLanguageElement)
  94. {
  95. if (FCString::Strcmp(AttributeName, TEXT("Name")) == 0)
  96. {
  97. if (FCString::Strcmp(AttributeValue, TEXT("External")) == 0
  98. || FCString::Strcmp(AttributeValue, TEXT("Mixed")) == 0
  99. || FCString::Strcmp(AttributeValue, TEXT("SFX")) == 0)
  100. {
  101. bInsideLanguageElement = false;
  102. }
  103. else
  104. {
  105. if (CurrentLanguageInfo.Name.IsEmpty())
  106. {
  107. CurrentLanguageInfo.Name = AttributeValue;
  108. AK::FNVHash32 hash;
  109. FTCHARToUTF8 utf8(*CurrentLanguageInfo.Name.ToLower());
  110. CurrentLanguageInfo.ShortID = hash.Compute(utf8.Get(), utf8.Length());
  111. }
  112. }
  113. }
  114. else if (FCString::Strcmp(AttributeName, TEXT("ID")) == 0)
  115. {
  116. FGuid::ParseExact(AttributeValue, EGuidFormats::DigitsWithHyphensInBraces, CurrentLanguageInfo.ID);
  117. }
  118. }
  119. if (bInsidePropertyElement
  120. && FCString::Strcmp(AttributeName, TEXT("Name")) == 0
  121. && FCString::Strcmp(AttributeValue, TEXT("DefaultLanguage")) == 0
  122. )
  123. {
  124. bInsideDefaultLanguage = true;
  125. }
  126. if (bInsideDefaultLanguage && FCString::Strcmp(AttributeName, TEXT("Value")) == 0)
  127. {
  128. DefaultLanguage = AttributeValue;
  129. bInsideDefaultLanguage = false;
  130. }
  131. return true;
  132. }
  133. bool WwiseProjectInfo::ProcessClose(const TCHAR* Element)
  134. {
  135. if (bInsidePlatformElement && FCString::Strcmp(Element, TEXT("Platform")) == 0)
  136. {
  137. SupportedPlatforms.Add(CurrentPlatformInfo);
  138. bInsidePlatformElement = false;
  139. }
  140. if (bInsideLanguageElement && FCString::Strcmp(Element, TEXT("Language")) == 0)
  141. {
  142. SupportedLanguages.Add(CurrentLanguageInfo);
  143. bInsideLanguageElement = false;
  144. }
  145. if (bInsidePropertyElement && FCString::Strcmp(Element, TEXT("Property")) == 0)
  146. {
  147. bInsidePropertyElement = false;
  148. }
  149. return true;
  150. }
  151. bool WwiseProjectInfo::ProcessComment(const TCHAR* Comment)
  152. {
  153. return true;
  154. }
  155. bool WwiseProjectInfo::ProcessElement(const TCHAR* ElementName, const TCHAR* ElementData, int32 XmlFileLineNumber)
  156. {
  157. if (FCString::Strcmp(ElementName, TEXT("Platform")) == 0)
  158. {
  159. bInsidePlatformElement = true;
  160. // Clear CurrentPlatformInfo
  161. new (&CurrentPlatformInfo) FWwisePlatformInfo();
  162. }
  163. else if (FCString::Strcmp(ElementName, TEXT("Language")) == 0)
  164. {
  165. bInsideLanguageElement = true;
  166. // Clear CurrentLanguageInfo
  167. new (&CurrentLanguageInfo) FWwiseLanguageInfo();
  168. }
  169. else if (FCString::Strcmp(ElementName, TEXT("Property")) == 0)
  170. {
  171. bInsidePropertyElement = true;
  172. }
  173. if (bInsideDefaultLanguage && FCString::Strcmp(ElementName, TEXT("Value")) == 0)
  174. {
  175. // Only use this as a backup
  176. if (DefaultLanguage.IsEmpty())
  177. {
  178. DefaultLanguage = ElementData;
  179. }
  180. bInsideDefaultLanguage = false;
  181. }
  182. return true;
  183. }
  184. bool WwiseProjectInfo::ProcessXmlDeclaration(const TCHAR* ElementData, int32 XmlFileLineNumber)
  185. {
  186. return true;
  187. }
  188. FString WwiseProjectInfo::GetProjectPath() const
  189. {
  190. FString ProjectPath;
  191. if (const UAkSettings* Settings = GetDefault<UAkSettings>())
  192. {
  193. ProjectPath = Settings->WwiseProjectPath.FilePath;
  194. if (FPaths::IsRelative(ProjectPath))
  195. {
  196. ProjectPath = FPaths::ConvertRelativePathToFull(FPaths::ProjectDir(), ProjectPath);
  197. }
  198. }
  199. return ProjectPath;
  200. }