AkUnrealEditorHelper.cpp 5.9 KB

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