PakFile.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 "PakFile.h"
  10. // AzFramework
  11. #include <AzFramework/Archive/INestedArchive.h>
  12. #include <AzFramework/Archive/IArchive.h>
  13. // Editor
  14. #include "Util/CryMemFile.h"
  15. //////////////////////////////////////////////////////////////////////////
  16. CPakFile::CPakFile()
  17. : m_pArchive(nullptr)
  18. , m_pCryPak(nullptr)
  19. {
  20. }
  21. //////////////////////////////////////////////////////////////////////////
  22. CPakFile::CPakFile(AZ::IO::IArchive* pCryPak)
  23. : m_pArchive(nullptr)
  24. , m_pCryPak(pCryPak)
  25. {
  26. }
  27. //////////////////////////////////////////////////////////////////////////
  28. CPakFile::~CPakFile()
  29. {
  30. Close();
  31. }
  32. //////////////////////////////////////////////////////////////////////////
  33. CPakFile::CPakFile(const char* filename)
  34. {
  35. m_pArchive = nullptr;
  36. Open(filename);
  37. }
  38. //////////////////////////////////////////////////////////////////////////
  39. void CPakFile::Close()
  40. {
  41. m_pArchive = nullptr;
  42. }
  43. //////////////////////////////////////////////////////////////////////////
  44. bool CPakFile::Open(const char* filename, bool bAbsolutePath)
  45. {
  46. if (m_pArchive)
  47. {
  48. Close();
  49. }
  50. auto pCryPak = m_pCryPak ? m_pCryPak : GetIEditor()->GetSystem()->GetIPak();
  51. if (pCryPak == nullptr)
  52. {
  53. return false;
  54. }
  55. if (bAbsolutePath)
  56. {
  57. m_pArchive = pCryPak->OpenArchive(filename, {}, AZ::IO::INestedArchive::FLAGS_ABSOLUTE_PATHS);
  58. }
  59. else
  60. {
  61. m_pArchive = pCryPak->OpenArchive(filename);
  62. }
  63. if (m_pArchive)
  64. {
  65. return true;
  66. }
  67. return false;
  68. }
  69. //////////////////////////////////////////////////////////////////////////
  70. bool CPakFile::OpenForRead(const char* filename)
  71. {
  72. if (m_pArchive)
  73. {
  74. Close();
  75. }
  76. auto pCryPak = m_pCryPak ? m_pCryPak : GetIEditor()->GetSystem()->GetIPak();
  77. if (pCryPak == nullptr)
  78. {
  79. return false;
  80. }
  81. m_pArchive = pCryPak->OpenArchive(filename, {}, AZ::IO::INestedArchive::FLAGS_OPTIMIZED_READ_ONLY | AZ::IO::INestedArchive::FLAGS_ABSOLUTE_PATHS);
  82. if (m_pArchive)
  83. {
  84. return true;
  85. }
  86. return false;
  87. }
  88. //////////////////////////////////////////////////////////////////////////
  89. bool CPakFile::UpdateFile(const char* filename, CCryMemFile& file, bool bCompress)
  90. {
  91. if (m_pArchive)
  92. {
  93. int nSize = static_cast<int>(file.GetLength());
  94. UpdateFile(filename, file.GetMemPtr(), nSize, bCompress);
  95. file.Close();
  96. return true;
  97. }
  98. return false;
  99. }
  100. //////////////////////////////////////////////////////////////////////////
  101. bool CPakFile::UpdateFile(const char* filename, CMemoryBlock& mem, bool bCompress, int nCompressLevel)
  102. {
  103. if (m_pArchive)
  104. {
  105. return UpdateFile(filename, mem.GetBuffer(), mem.GetSize(), bCompress, nCompressLevel);
  106. }
  107. return false;
  108. }
  109. /////////////////////////////////////////////////////////////////////////
  110. bool CPakFile::UpdateFile(const char* filename, void* pBuffer, int nSize, bool bCompress, int nCompressLevel)
  111. {
  112. if (m_pArchive)
  113. {
  114. if (bCompress)
  115. {
  116. return 0 == m_pArchive->UpdateFile(filename, pBuffer, nSize, AZ::IO::INestedArchive::METHOD_DEFLATE, nCompressLevel);
  117. }
  118. else
  119. {
  120. return 0 == m_pArchive->UpdateFile(filename, pBuffer, nSize);
  121. }
  122. }
  123. return false;
  124. }
  125. //////////////////////////////////////////////////////////////////////////
  126. bool CPakFile::RemoveFile(const char* filename)
  127. {
  128. if (m_pArchive)
  129. {
  130. return m_pArchive->RemoveFile(filename);
  131. }
  132. return false;
  133. }
  134. //////////////////////////////////////////////////////////////////////////
  135. bool CPakFile::RemoveDir(const char* directory)
  136. {
  137. if (m_pArchive)
  138. {
  139. return m_pArchive->RemoveDir(directory);
  140. }
  141. return false;
  142. }