AkUnrealEditorHelper.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 "AkUnrealEditorHelper.h"
  16. #include "WwiseUnrealHelper.h"
  17. #if WITH_EDITOR
  18. #include "Interfaces/IPluginManager.h"
  19. #include "HAL/FileManager.h"
  20. #include "Misc/App.h"
  21. #include "Misc/MessageDialog.h"
  22. #include "Misc/Paths.h"
  23. #include "ISourceControlModule.h"
  24. #include "SSettingsEditorCheckoutNotice.h"
  25. #include "AkSettings.h"
  26. #include "WwiseUnrealDefines.h"
  27. #include "Wwise/Stats/AkAudio.h"
  28. #if UE_5_0_OR_LATER
  29. #include "HAL/PlatformFileManager.h"
  30. #else
  31. #include "HAL/PlatformFilemanager.h"
  32. #endif
  33. #define LOCTEXT_NAMESPACE "AkAudio"
  34. namespace AkUnrealEditorHelper
  35. {
  36. const TCHAR* LocalizedFolderName = TEXT("Localized");
  37. void SanitizePath(FString& Path, const FString& PreviousPath, const FText& DialogMessage)
  38. {
  39. WwiseUnrealHelper::TrimPath(Path);
  40. FText FailReason;
  41. if (!FPaths::ValidatePath(Path, &FailReason))
  42. {
  43. if (FApp::CanEverRender())
  44. {
  45. FMessageDialog::Open(EAppMsgType::Ok, FailReason);
  46. }
  47. else
  48. {
  49. UE_LOG(LogAkAudio, Error, TEXT("%s"), *FailReason.ToString());
  50. }
  51. Path = PreviousPath;
  52. return;
  53. }
  54. const FString AbsolutePath = IFileManager::Get().ConvertToAbsolutePathForExternalAppForRead(*Path);
  55. if (!FPaths::DirectoryExists(AbsolutePath))
  56. {
  57. if (FApp::CanEverRender())
  58. {
  59. FMessageDialog::Open(EAppMsgType::Ok, DialogMessage);
  60. }
  61. else
  62. {
  63. UE_LOG(LogAkAudio, Error, TEXT("%s"), *DialogMessage.ToString());
  64. }
  65. Path = PreviousPath;
  66. return;
  67. }
  68. }
  69. bool SanitizeFolderPathAndMakeRelativeToContentDir(FString& Path, const FString& PreviousPath, const FText& DialogMessage)
  70. {
  71. WwiseUnrealHelper::TrimPath(Path);
  72. FString TempPath = IFileManager::Get().ConvertToAbsolutePathForExternalAppForWrite(*Path);
  73. FText FailReason;
  74. if (!FPaths::ValidatePath(TempPath, &FailReason))
  75. {
  76. if (FApp::CanEverRender())
  77. {
  78. FMessageDialog::Open(EAppMsgType::Ok, FailReason);
  79. }
  80. else
  81. {
  82. UE_LOG(LogAkAudio, Error, TEXT("%s"), *FailReason.ToString());
  83. }
  84. Path = PreviousPath;
  85. return false;
  86. }
  87. auto ContentDirectory = WwiseUnrealHelper::GetContentDirectory();
  88. if (!FPaths::FileExists(TempPath))
  89. {
  90. // Path might be a valid one (relative to game) entered manually. Check that.
  91. TempPath = FPaths::ConvertRelativePathToFull(ContentDirectory, Path);
  92. if (!FPaths::DirectoryExists(TempPath))
  93. {
  94. if (FApp::CanEverRender())
  95. {
  96. if (EAppReturnType::Ok == FMessageDialog::Open(EAppMsgType::Ok, DialogMessage))
  97. {
  98. Path = PreviousPath;
  99. return false;
  100. }
  101. }
  102. else
  103. {
  104. // Allow setting not yet existing paths when running in headless mode (e.g. migration)
  105. UE_LOG(LogAkAudio, Warning, TEXT("Path '%s' does not exist."), *Path);
  106. }
  107. }
  108. }
  109. // Make the path relative to the game dir
  110. FPaths::MakePathRelativeTo(TempPath, *ContentDirectory);
  111. Path = TempPath;
  112. if (Path != PreviousPath)
  113. {
  114. return true;
  115. }
  116. return false;
  117. }
  118. bool SaveConfigFile(UObject* ConfigObject)
  119. {
  120. const FString ConfigFilename = ConfigObject->GetDefaultConfigFilename();
  121. if(ISourceControlModule::Get().IsEnabled())
  122. {
  123. if (!SettingsHelpers::IsCheckedOut(ConfigFilename, true))
  124. {
  125. if (!SettingsHelpers::CheckOutOrAddFile(ConfigFilename, true))
  126. {
  127. return false;
  128. }
  129. }
  130. }
  131. #if UE_5_0_OR_LATER
  132. return ConfigObject->TryUpdateDefaultConfigFile();
  133. #else
  134. ConfigObject->UpdateDefaultConfigFile();
  135. return true;
  136. #endif
  137. }
  138. FString GetLegacySoundBankDirectory()
  139. {
  140. if (const UAkSettings* AkSettings = GetDefault<UAkSettings>())
  141. {
  142. return FPaths::Combine(WwiseUnrealHelper::GetContentDirectory(), AkSettings->WwiseSoundDataFolder.Path);
  143. }
  144. else
  145. {
  146. return FPaths::Combine(WwiseUnrealHelper::GetContentDirectory(), UAkSettings::DefaultSoundDataFolder);
  147. }
  148. }
  149. FString GetContentDirectory()
  150. {
  151. return FPaths::ConvertRelativePathToFull(FPaths::ProjectContentDir());
  152. }
  153. void DeleteLegacySoundBanks()
  154. {
  155. const TArray<FString> ExtensionsToDelete = { "bnk", "wem", "json", "txt", "xml" };
  156. bool SuccessfulDelete = true;
  157. for (auto& Extension : ExtensionsToDelete)
  158. {
  159. TArray<FString> FoundFiles;
  160. FPlatformFileManager::Get().GetPlatformFile().FindFilesRecursively(FoundFiles, *WwiseUnrealHelper::GetSoundBankDirectory(), *Extension);
  161. FPlatformFileManager::Get().GetPlatformFile().FindFilesRecursively(FoundFiles, *GetLegacySoundBankDirectory(), *Extension);
  162. TSet<FString> FoundFilesSet(FoundFiles);
  163. for (auto& File : FoundFilesSet)
  164. {
  165. SuccessfulDelete |= FPlatformFileManager::Get().GetPlatformFile().DeleteFile(*File);
  166. }
  167. }
  168. if (!SuccessfulDelete)
  169. {
  170. if (!FApp::CanEverRender())
  171. {
  172. UE_LOG(LogAkAudio, Warning, TEXT("Unable to delete legacy SoundBank files. Please ensure to manually delete them after migration is complete."));
  173. }
  174. else
  175. {
  176. FMessageDialog::Open(EAppMsgType::Ok, LOCTEXT("CannotDeleteOldBanks", "Unable to delete legacy SoundBank files. Please ensure to manually delete them after migration is complete."));
  177. }
  178. }
  179. }
  180. }
  181. #undef LOCTEXT_NAMESPACE
  182. #endif // WITH_EDITOR