PackageFile.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // Copyright (c) 2008-2017 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()
  46. {
  47. }
  48. bool PackageFile::Open(const String& fileName, unsigned startOffset)
  49. {
  50. SharedPtr<File> file(new File(context_, fileName));
  51. if (!file->IsOpen())
  52. return false;
  53. // Check ID, then read the directory
  54. file->Seek(startOffset);
  55. String id = file->ReadFileID();
  56. if (id != "UPAK" && id != "ULZ4")
  57. {
  58. // If start offset has not been explicitly specified, also try to read package size from the end of file
  59. // to know how much we must rewind to find the package start
  60. if (!startOffset)
  61. {
  62. unsigned fileSize = file->GetSize();
  63. file->Seek((unsigned)(fileSize - sizeof(unsigned)));
  64. unsigned newStartOffset = fileSize - file->ReadUInt();
  65. if (newStartOffset < fileSize)
  66. {
  67. startOffset = newStartOffset;
  68. file->Seek(startOffset);
  69. id = file->ReadFileID();
  70. }
  71. }
  72. if (id != "UPAK" && id != "ULZ4")
  73. {
  74. URHO3D_LOGERROR(fileName + " is not a valid package file");
  75. return false;
  76. }
  77. }
  78. fileName_ = fileName;
  79. nameHash_ = fileName_;
  80. totalSize_ = file->GetSize();
  81. compressed_ = id == "ULZ4";
  82. unsigned numFiles = file->ReadUInt();
  83. checksum_ = file->ReadUInt();
  84. for (unsigned i = 0; i < numFiles; ++i)
  85. {
  86. String entryName = file->ReadString();
  87. PackageEntry newEntry;
  88. newEntry.offset_ = file->ReadUInt() + startOffset;
  89. totalDataSize_ += (newEntry.size_ = file->ReadUInt());
  90. newEntry.checksum_ = file->ReadUInt();
  91. if (!compressed_ && newEntry.offset_ + newEntry.size_ > totalSize_)
  92. {
  93. URHO3D_LOGERROR("File entry " + entryName + " outside package file");
  94. return false;
  95. }
  96. else
  97. entries_[entryName] = newEntry;
  98. }
  99. return true;
  100. }
  101. bool PackageFile::Exists(const String& fileName) const
  102. {
  103. bool found = entries_.Find(fileName) != entries_.End();
  104. #ifdef _WIN32
  105. // On Windows perform a fallback case-insensitive search
  106. if (!found)
  107. {
  108. for (HashMap<String, PackageEntry>::ConstIterator i = entries_.Begin(); i != entries_.End(); ++i)
  109. {
  110. if (!i->first_.Compare(fileName, false))
  111. {
  112. found = true;
  113. break;
  114. }
  115. }
  116. }
  117. #endif
  118. return found;
  119. }
  120. const PackageEntry* PackageFile::GetEntry(const String& fileName) const
  121. {
  122. HashMap<String, PackageEntry>::ConstIterator i = entries_.Find(fileName);
  123. if (i != entries_.End())
  124. return &i->second_;
  125. #ifdef _WIN32
  126. // On Windows perform a fallback case-insensitive search
  127. else
  128. {
  129. for (HashMap<String, PackageEntry>::ConstIterator j = entries_.Begin(); j != entries_.End(); ++j)
  130. {
  131. if (!j->first_.Compare(fileName, false))
  132. return &j->second_;
  133. }
  134. }
  135. #endif
  136. return nullptr;
  137. }
  138. }