GameResourcesExporter.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 "EditorDefs.h"
  9. #include "GameResourcesExporter.h"
  10. // Qt
  11. #include <QFileDialog>
  12. // Editor
  13. #include "UsedResources.h"
  14. #include "GameEngine.h"
  15. #include "WaitProgress.h"
  16. //////////////////////////////////////////////////////////////////////////
  17. // Static data.
  18. //////////////////////////////////////////////////////////////////////////
  19. CGameResourcesExporter::Files CGameResourcesExporter::m_files;
  20. //////////////////////////////////////////////////////////////////////////
  21. void CGameResourcesExporter::ChooseDirectoryAndSave()
  22. {
  23. ChooseDirectory();
  24. if (!m_path.isEmpty())
  25. {
  26. Save(m_path);
  27. }
  28. }
  29. //////////////////////////////////////////////////////////////////////////
  30. void CGameResourcesExporter::ChooseDirectory()
  31. {
  32. m_path = QFileDialog::getExistingDirectory(nullptr, QObject::tr("Choose Target (root/PrimaryCD) Folder"));
  33. }
  34. //////////////////////////////////////////////////////////////////////////
  35. void CGameResourcesExporter::GatherAllLoadedResources()
  36. {
  37. m_files.clear();
  38. m_files.reserve(100000); // count from GetResourceList, GetFilesFromObjects ... is unknown
  39. auto pResList = gEnv->pCryPak->GetResourceList(AZ::IO::IArchive::RFOM_Level);
  40. {
  41. for (const char* filename = pResList->GetFirst(); filename; filename = pResList->GetNext())
  42. {
  43. m_files.push_back(filename);
  44. }
  45. }
  46. }
  47. //////////////////////////////////////////////////////////////////////////
  48. void CGameResourcesExporter::SetUsedResources(CUsedResources& resources)
  49. {
  50. for (CUsedResources::TResourceFiles::const_iterator it = resources.files.begin(); it != resources.files.end(); it++)
  51. {
  52. m_files.push_back(*it);
  53. }
  54. }
  55. //////////////////////////////////////////////////////////////////////////
  56. void CGameResourcesExporter::Save(const QString& outputDirectory)
  57. {
  58. CMemoryBlock data;
  59. int numFiles = m_files.size();
  60. CLogFile::WriteLine("===========================================================================");
  61. CLogFile::FormatLine("Exporting Level %s resources, %d files", GetIEditor()->GetGameEngine()->GetLevelName().toUtf8().data(), numFiles);
  62. CLogFile::WriteLine("===========================================================================");
  63. // Needed files.
  64. CWaitProgress wait("Exporting Resources");
  65. for (int i = 0; i < numFiles; i++)
  66. {
  67. QString srcFilename = m_files[i];
  68. if (!wait.Step((i * 100) / numFiles))
  69. {
  70. break;
  71. }
  72. wait.SetText(srcFilename.toUtf8().data());
  73. CLogFile::WriteLine(srcFilename.toUtf8().data());
  74. CCryFile file;
  75. if (file.Open(srcFilename.toUtf8().data(), "rb"))
  76. {
  77. // Save this file in target folder.
  78. QString trgFilename = Path::Make(outputDirectory, srcFilename);
  79. int fsize = static_cast<int>(file.GetLength());
  80. if (fsize > data.GetSize())
  81. {
  82. data.Allocate(fsize + 16);
  83. }
  84. // Read data.
  85. file.ReadRaw(data.GetBuffer(), fsize);
  86. // Save this data to target file.
  87. QString trgFileDir = Path::GetPath(trgFilename);
  88. CFileUtil::CreateDirectory(trgFileDir.toUtf8().data());
  89. // Create a file.
  90. FILE* trgFile = nullptr;
  91. azfopen(&trgFile, trgFilename.toUtf8().data(), "wb");
  92. if (trgFile)
  93. {
  94. // Save data to new file.
  95. fwrite(data.GetBuffer(), fsize, 1, trgFile);
  96. fclose(trgFile);
  97. }
  98. }
  99. }
  100. CLogFile::WriteLine("===========================================================================");
  101. m_files.clear();
  102. }