PackageFile.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // Copyright (c) 2008-2015 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 "../IO/File.h"
  23. #include "../IO/Log.h"
  24. #include "../IO/PackageFile.h"
  25. namespace Urho3D
  26. {
  27. PackageFile::PackageFile(Context* context) :
  28. Object(context),
  29. totalSize_(0),
  30. checksum_(0),
  31. compressed_(false)
  32. {
  33. }
  34. PackageFile::PackageFile(Context* context, const String& fileName, unsigned startOffset) :
  35. Object(context),
  36. totalSize_(0),
  37. checksum_(0),
  38. compressed_(false)
  39. {
  40. Open(fileName, startOffset);
  41. }
  42. PackageFile::~PackageFile()
  43. {
  44. }
  45. bool PackageFile::Open(const String& fileName, unsigned startOffset)
  46. {
  47. #ifdef ANDROID
  48. if (fileName.StartsWith("/apk/"))
  49. {
  50. LOGERROR("Package files within the apk are not supported on Android");
  51. return false;
  52. }
  53. #endif
  54. SharedPtr<File> file(new File(context_, fileName));
  55. if (!file->IsOpen())
  56. return false;
  57. // Check ID, then read the directory
  58. file->Seek(startOffset);
  59. String id = file->ReadFileID();
  60. if (id != "UPAK" && id != "ULZ4")
  61. {
  62. // If start offset has not been explicitly specified, also try to read package size from the end of file
  63. // to know how much we must rewind to find the package start
  64. if (!startOffset)
  65. {
  66. unsigned fileSize = file->GetSize();
  67. file->Seek(fileSize - sizeof(unsigned));
  68. unsigned newStartOffset = fileSize - file->ReadUInt();
  69. if (newStartOffset < fileSize)
  70. {
  71. startOffset = newStartOffset;
  72. file->Seek(startOffset);
  73. id = file->ReadFileID();
  74. }
  75. }
  76. if (id != "UPAK" && id != "ULZ4")
  77. {
  78. LOGERROR(fileName + " is not a valid package file");
  79. return false;
  80. }
  81. }
  82. fileName_ = fileName;
  83. nameHash_ = fileName_;
  84. totalSize_ = file->GetSize();
  85. compressed_ = id == "ULZ4";
  86. unsigned numFiles = file->ReadUInt();
  87. checksum_ = file->ReadUInt();
  88. for (unsigned i = 0; i < numFiles; ++i)
  89. {
  90. String entryName = file->ReadString();
  91. PackageEntry newEntry;
  92. newEntry.offset_ = file->ReadUInt() + startOffset;
  93. newEntry.size_ = file->ReadUInt();
  94. newEntry.checksum_ = file->ReadUInt();
  95. if (!compressed_ && newEntry.offset_ + newEntry.size_ > totalSize_)
  96. LOGERROR("File entry " + entryName + " outside package file");
  97. else
  98. entries_[entryName.ToLower()] = newEntry;
  99. }
  100. return true;
  101. }
  102. bool PackageFile::Exists(const String& fileName) const
  103. {
  104. return entries_.Find(fileName.ToLower()) != entries_.End();
  105. }
  106. const PackageEntry* PackageFile::GetEntry(const String& fileName) const
  107. {
  108. HashMap<String, PackageEntry>::ConstIterator i = entries_.Find(fileName.ToLower());
  109. if (i != entries_.End())
  110. return &i->second_;
  111. else
  112. return 0;
  113. }
  114. }