PackageFile.cpp 4.0 KB

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