GenerateBundlesModal.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <source/ui/GenerateBundlesModal.h>
  9. #include <source/ui/ui_GenerateBundlesModal.h>
  10. #include <source/ui/AssetListTabWidget.h>
  11. #include <source/ui/NewFileDialog.h>
  12. #include <source/utils/utils.h>
  13. #include <AzCore/IO/FileIO.h>
  14. #include <AzFramework/StringFunc/StringFunc.h>
  15. #include <AzToolsFramework/AssetBundle/AssetBundleAPI.h>
  16. #include <QMessageBox>
  17. const char CustomBundleSettingsText[] = "Custom";
  18. namespace AssetBundler
  19. {
  20. GenerateBundlesModal::GenerateBundlesModal(
  21. QWidget* parent,
  22. const AZStd::string& assetListFileAbsolutePath,
  23. const AZStd::string& defaultBundleDirectory,
  24. const AZStd::string& defaultBundleSettingsDirectory,
  25. AssetListTabWidget* assetListTabWidget)
  26. : QDialog(parent)
  27. , m_assetListFileAbsolutePath(assetListFileAbsolutePath)
  28. , m_defaultBundleDirectory(defaultBundleDirectory)
  29. , m_defaultBundleSettingsDirectory(defaultBundleSettingsDirectory)
  30. , m_assetListTabWidget(assetListTabWidget)
  31. {
  32. m_ui.reset(new Ui::GenerateBundlesModal);
  33. m_ui->setupUi(this);
  34. m_platformName = AzToolsFramework::GetPlatformIdentifier(m_assetListFileAbsolutePath);
  35. // Selected Asset List
  36. m_ui->selectedAssetListPathLabel->setText(QString(m_assetListFileAbsolutePath.c_str()));
  37. // Platform
  38. m_ui->platformNameLabel->setText(QString(m_platformName.c_str()));
  39. // Bundle Output
  40. m_ui->outputBundlePathLineEdit->setReadOnly(true);
  41. connect(m_ui->outputBundlePathBrowseButton,
  42. &QPushButton::clicked,
  43. this,
  44. &GenerateBundlesModal::OnOutputBundleLocationBrowseButtonPressed);
  45. // Bundle Settings files
  46. m_ui->bundleSettingsFileLineEdit->setReadOnly(true);
  47. m_ui->bundleSettingsFileLineEdit->setText(tr(CustomBundleSettingsText));
  48. connect(m_ui->bundleSettingsFileBrowseButton,
  49. &QPushButton::clicked,
  50. this,
  51. &GenerateBundlesModal::OnBundleSettingsBrowseButtonPressed);
  52. connect(m_ui->bundleSettingsFileSaveButton,
  53. &QPushButton::clicked,
  54. this,
  55. &GenerateBundlesModal::OnBundleSettingsSaveButtonPressed);
  56. // Max Bundle Size
  57. m_ui->maxBundleSizeSpinBox->setRange(1, AzToolsFramework::MaxBundleSizeInMB);
  58. m_ui->maxBundleSizeSpinBox->setValue(AzToolsFramework::MaxBundleSizeInMB);
  59. m_ui->maxBundleSizeSpinBox->setButtonSymbols(QAbstractSpinBox::ButtonSymbols::NoButtons);
  60. m_ui->maxBundleSizeSpinBox->setSuffix(" MB");
  61. connect(m_ui->maxBundleSizeSpinBox,
  62. QOverload<int>::of(&QSpinBox::valueChanged),
  63. this,
  64. &GenerateBundlesModal::OnMaxBundleSizeChanged);
  65. // Bundle Version
  66. m_ui->bundleVersionSpinBox->setRange(1, AzFramework::AssetBundleManifest::CurrentBundleVersion);
  67. m_ui->bundleVersionSpinBox->setValue(AzFramework::AssetBundleManifest::CurrentBundleVersion);
  68. m_ui->bundleVersionSpinBox->setButtonSymbols(QAbstractSpinBox::ButtonSymbols::NoButtons);
  69. connect(m_ui->bundleVersionSpinBox,
  70. QOverload<int>::of(&QSpinBox::valueChanged),
  71. this,
  72. &GenerateBundlesModal::OnBundleVersionChanged);
  73. // Cancel and Generate Bundles buttons
  74. m_ui->generateBundlesButton->setEnabled(false);
  75. connect(m_ui->cancelButton, &QPushButton::clicked, this, &QDialog::reject);
  76. connect(m_ui->generateBundlesButton, &QPushButton::clicked, this, &GenerateBundlesModal::OnGenerateBundlesButtonPressed);
  77. // In-memory Bundle Settings
  78. m_bundleSettings.m_assetFileInfoListPath = m_assetListFileAbsolutePath;
  79. m_bundleSettings.m_platform = m_platformName;
  80. m_bundleSettings.m_maxBundleSizeInMB = static_cast<AZ::u64>(m_ui->maxBundleSizeSpinBox->value());
  81. m_bundleSettings.m_bundleVersion = m_ui->bundleVersionSpinBox->value();
  82. }
  83. GenerateBundlesModal::~GenerateBundlesModal()
  84. {
  85. m_assetListTabWidget = nullptr;
  86. }
  87. void GenerateBundlesModal::OnOutputBundleLocationBrowseButtonPressed()
  88. {
  89. AZStd::string outputBundleAbsolutePath = NewFileDialog::OSNewFileDialog(
  90. this,
  91. AzToolsFramework::AssetBundleSettings::GetBundleFileExtension(),
  92. "Bundle",
  93. m_defaultBundleDirectory);
  94. if (outputBundleAbsolutePath.empty())
  95. {
  96. // User canceled out of the dialog
  97. return;
  98. }
  99. AzToolsFramework::RemovePlatformIdentifier(outputBundleAbsolutePath);
  100. AddPlatformIdentifier(outputBundleAbsolutePath, m_platformName);
  101. m_bundleSettings.m_bundleFilePath = outputBundleAbsolutePath;
  102. m_ui->outputBundlePathLineEdit->setText(outputBundleAbsolutePath.c_str());
  103. m_ui->generateBundlesButton->setEnabled(true);
  104. }
  105. void GenerateBundlesModal::OnBundleSettingsBrowseButtonPressed()
  106. {
  107. AZStd::string bundleSettingsAbsolutePath = NewFileDialog::OSNewFileDialog(
  108. this,
  109. AzToolsFramework::AssetBundleSettings::GetBundleSettingsFileExtension(),
  110. "Bundle Settings",
  111. m_defaultBundleSettingsDirectory);
  112. if (bundleSettingsAbsolutePath.empty())
  113. {
  114. // User canceled out of the dialog
  115. return;
  116. }
  117. // Read in the values from the Bundle Settings file
  118. bool loadResult = LoadBundleSettingsValues(bundleSettingsAbsolutePath);
  119. if (!loadResult)
  120. {
  121. return;
  122. }
  123. // Update the display name for our settings
  124. UpdateBundleSettingsDisplayName(bundleSettingsAbsolutePath);
  125. }
  126. bool GenerateBundlesModal::LoadBundleSettingsValues(const AZStd::string& absoluteBundleSettingsFilePath)
  127. {
  128. using namespace AzToolsFramework;
  129. auto outcome = AssetBundleSettings::Load(absoluteBundleSettingsFilePath);
  130. if (!outcome.IsSuccess())
  131. {
  132. AZ_Error("AssetBundler", false, outcome.GetError().c_str());
  133. return false;
  134. }
  135. // We don't want to overwrite all of the in-memory bundle settings, so we will just update a few
  136. AssetBundleSettings loadedSettings = outcome.TakeValue();
  137. m_bundleSettings.m_maxBundleSizeInMB = loadedSettings.m_maxBundleSizeInMB;
  138. m_ui->maxBundleSizeSpinBox->setValue(static_cast<int>(m_bundleSettings.m_maxBundleSizeInMB));
  139. m_bundleSettings.m_bundleVersion = loadedSettings.m_bundleVersion;
  140. m_ui->bundleVersionSpinBox->setValue(m_bundleSettings.m_bundleVersion);
  141. return true;
  142. }
  143. void GenerateBundlesModal::UpdateBundleSettingsDisplayName(const AZStd::string& absoluteBundleSettingsFilePath)
  144. {
  145. if (absoluteBundleSettingsFilePath.empty())
  146. {
  147. m_ui->bundleSettingsFileLineEdit->setText(tr(CustomBundleSettingsText));
  148. return;
  149. }
  150. AZStd::string bundleSettingsFileName = absoluteBundleSettingsFilePath;
  151. AzToolsFramework::RemovePlatformIdentifier(bundleSettingsFileName);
  152. AzFramework::StringFunc::Path::GetFileName(bundleSettingsFileName.c_str(), bundleSettingsFileName);
  153. m_ui->bundleSettingsFileLineEdit->setText(bundleSettingsFileName.c_str());
  154. }
  155. void GenerateBundlesModal::OnBundleSettingsSaveButtonPressed()
  156. {
  157. using namespace AzToolsFramework;
  158. // Ask the user where they want to save the Bundle Settings file
  159. AZStd::string bundleSettingsAbsolutePath = NewFileDialog::OSNewFileDialog(
  160. this,
  161. AssetBundleSettings::GetBundleSettingsFileExtension(),
  162. "Bundle Settings",
  163. m_defaultBundleSettingsDirectory);
  164. if (bundleSettingsAbsolutePath.empty())
  165. {
  166. // User canceled out of the operation
  167. return;
  168. }
  169. AzToolsFramework::RemovePlatformIdentifier(bundleSettingsAbsolutePath);
  170. AddPlatformIdentifier(bundleSettingsAbsolutePath, m_platformName);
  171. if (AZ::IO::FileIOBase::GetInstance()->Exists(bundleSettingsAbsolutePath.c_str()))
  172. {
  173. QString messageBoxText = QString(tr(
  174. "Bundle Settings ( %1 ) already exists on-disk. Saving the current settings will override the existing settings. \n\nDo you wish to continue?")).arg(bundleSettingsAbsolutePath.c_str());
  175. QMessageBox::StandardButton confirmDeleteFileResult =
  176. QMessageBox::question(this, QString(tr("Replace Existing Settings")), messageBoxText);
  177. if (confirmDeleteFileResult != QMessageBox::StandardButton::Yes)
  178. {
  179. // User canceled out of the operation
  180. return;
  181. }
  182. }
  183. bool saveResult = AssetBundleSettings::Save(m_bundleSettings, bundleSettingsAbsolutePath);
  184. if (!saveResult)
  185. {
  186. // Error has already been thrown
  187. return;
  188. }
  189. m_assetListTabWidget->AddScanPathToAssetBundlerSettings(AssetBundlingFileType::BundleSettingsFileType, bundleSettingsAbsolutePath);
  190. UpdateBundleSettingsDisplayName(bundleSettingsAbsolutePath);
  191. }
  192. void GenerateBundlesModal::OnMaxBundleSizeChanged()
  193. {
  194. m_bundleSettings.m_maxBundleSizeInMB = static_cast<AZ::u64>(m_ui->maxBundleSizeSpinBox->value());
  195. UpdateBundleSettingsDisplayName("");
  196. }
  197. void GenerateBundlesModal::OnBundleVersionChanged()
  198. {
  199. m_bundleSettings.m_bundleVersion = m_ui->bundleVersionSpinBox->value();
  200. UpdateBundleSettingsDisplayName("");
  201. }
  202. void GenerateBundlesModal::OnGenerateBundlesButtonPressed()
  203. {
  204. using namespace AzToolsFramework;
  205. if (AZ::IO::FileIOBase::GetInstance()->Exists(m_bundleSettings.m_bundleFilePath.c_str()))
  206. {
  207. QString messageBoxText = QString(tr(
  208. "Asset Bundle ( %1 ) already exists on-disk. Generating a new Bundle will override the existing Bundle. \n\nDo you wish to permanently delete the existing Bundle?")).arg(m_bundleSettings.m_bundleFilePath.c_str());
  209. QMessageBox::StandardButton confirmDeleteFileResult =
  210. QMessageBox::question(this, QString(tr("Replace Existing Bundle")), messageBoxText);
  211. if (confirmDeleteFileResult != QMessageBox::StandardButton::Yes)
  212. {
  213. // User canceled out of the operation
  214. return;
  215. }
  216. }
  217. bool result = false;
  218. AssetBundleCommandsBus::BroadcastResult(result, &AssetBundleCommandsBus::Events::CreateAssetBundle, m_bundleSettings);
  219. // This operation will take long enough that the OS will throw up its own wait cursor,
  220. // but there is a slight delay where the UI is unresponsive but the cursor hasn't changed.
  221. // We are changing the cursor manually to avoid that.
  222. setCursor(Qt::WaitCursor);
  223. emit QDialog::accept();
  224. if (result)
  225. {
  226. m_assetListTabWidget->AddScanPathToAssetBundlerSettings(
  227. AssetBundlingFileType::BundleFileType,
  228. m_bundleSettings.m_bundleFilePath);
  229. // The watched files list was updated after the files were created, so we need to force-reload them
  230. m_assetListTabWidget->GetGUIApplicationManager()->UpdateFiles(
  231. AssetBundlingFileType::BundleFileType,
  232. { m_bundleSettings.m_bundleFilePath });
  233. }
  234. AZStd::vector<AZStd::string> generatedFilePaths = { m_bundleSettings.m_bundleFilePath };
  235. NewFileDialog::FileGenerationResultMessageBox(this, generatedFilePaths, !result);
  236. unsetCursor();
  237. }
  238. } // namespace AssetBundler
  239. #include <source/ui/moc_GenerateBundlesModal.cpp>