PackageFile.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../IO/File.h"
  24. #include "../IO/Log.h"
  25. #include "../IO/PackageFile.h"
  26. namespace Urho3D
  27. {
  28. PackageFile::PackageFile(Context* context) :
  29. Object(context),
  30. totalSize_(0),
  31. totalDataSize_(0),
  32. checksum_(0),
  33. compressed_(false)
  34. {
  35. }
  36. PackageFile::PackageFile(Context* context, const String& fileName, unsigned startOffset) :
  37. Object(context),
  38. totalSize_(0),
  39. totalDataSize_(0),
  40. checksum_(0),
  41. compressed_(false)
  42. {
  43. Open(fileName, startOffset);
  44. }
  45. PackageFile::~PackageFile() = default;
  46. bool PackageFile::Open(const String& fileName, unsigned startOffset)
  47. {
  48. SharedPtr<File> file(new File(context_, fileName));
  49. if (!file->IsOpen())
  50. return false;
  51. // Check ID, then read the directory
  52. file->Seek(startOffset);
  53. String id = file->ReadFileID();
  54. if (id != "UPAK" && id != "ULZ4")
  55. {
  56. // If start offset has not been explicitly specified, also try to read package size from the end of file
  57. // to know how much we must rewind to find the package start
  58. if (!startOffset)
  59. {
  60. unsigned fileSize = file->GetSize();
  61. file->Seek((unsigned)(fileSize - sizeof(unsigned)));
  62. unsigned newStartOffset = fileSize - file->ReadUInt();
  63. if (newStartOffset < fileSize)
  64. {
  65. startOffset = newStartOffset;
  66. file->Seek(startOffset);
  67. id = file->ReadFileID();
  68. }
  69. }
  70. if (id != "UPAK" && id != "ULZ4")
  71. {
  72. URHO3D_LOGERROR(fileName + " is not a valid package file");
  73. return false;
  74. }
  75. }
  76. fileName_ = fileName;
  77. nameHash_ = fileName_;
  78. totalSize_ = file->GetSize();
  79. compressed_ = id == "ULZ4";
  80. unsigned numFiles = file->ReadUInt();
  81. checksum_ = file->ReadUInt();
  82. for (unsigned i = 0; i < numFiles; ++i)
  83. {
  84. String entryName = file->ReadString();
  85. PackageEntry newEntry{};
  86. newEntry.offset_ = file->ReadUInt() + startOffset;
  87. totalDataSize_ += (newEntry.size_ = file->ReadUInt());
  88. newEntry.checksum_ = file->ReadUInt();
  89. if (!compressed_ && newEntry.offset_ + newEntry.size_ > totalSize_)
  90. {
  91. URHO3D_LOGERROR("File entry " + entryName + " outside package file");
  92. return false;
  93. }
  94. else
  95. entries_[entryName] = newEntry;
  96. }
  97. return true;
  98. }
  99. bool PackageFile::Exists(const String& fileName) const
  100. {
  101. bool found = entries_.Find(fileName) != entries_.End();
  102. #ifdef _WIN32
  103. // On Windows perform a fallback case-insensitive search
  104. if (!found)
  105. {
  106. for (HashMap<String, PackageEntry>::ConstIterator i = entries_.Begin(); i != entries_.End(); ++i)
  107. {
  108. if (!i->first_.Compare(fileName, false))
  109. {
  110. found = true;
  111. break;
  112. }
  113. }
  114. }
  115. #endif
  116. return found;
  117. }
  118. const PackageEntry* PackageFile::GetEntry(const String& fileName) const
  119. {
  120. HashMap<String, PackageEntry>::ConstIterator i = entries_.Find(fileName);
  121. if (i != entries_.End())
  122. return &i->second_;
  123. #ifdef _WIN32
  124. // On Windows perform a fallback case-insensitive search
  125. else
  126. {
  127. for (HashMap<String, PackageEntry>::ConstIterator j = entries_.Begin(); j != entries_.End(); ++j)
  128. {
  129. if (!j->first_.Compare(fileName, false))
  130. return &j->second_;
  131. }
  132. }
  133. #endif
  134. return nullptr;
  135. }
  136. }