PackageFile.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 "../Precompiled.h"
  23. #include "../IO/File.h"
  24. #include "../IO/Log.h"
  25. #include "../IO/PackageFile.h"
  26. namespace Atomic
  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 (IS_ASSET(fileName))
  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((unsigned)(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] = newEntry;
  100. }
  101. return true;
  102. }
  103. bool PackageFile::Exists(const String& fileName) const
  104. {
  105. bool found = entries_.Find(fileName) != entries_.End();
  106. #ifdef WIN32
  107. // On Windows perform a fallback case-insensitive search
  108. if (!found)
  109. {
  110. for (HashMap<String, PackageEntry>::ConstIterator i = entries_.Begin(); i != entries_.End(); ++i)
  111. {
  112. if (!i->first_.Compare(fileName, false))
  113. {
  114. found = true;
  115. break;
  116. }
  117. }
  118. }
  119. #endif
  120. return found;
  121. }
  122. const PackageEntry* PackageFile::GetEntry(const String& fileName) const
  123. {
  124. HashMap<String, PackageEntry>::ConstIterator i = entries_.Find(fileName);
  125. if (i != entries_.End())
  126. return &i->second_;
  127. #ifdef WIN32
  128. // On Windows perform a fallback case-insensitive search
  129. else
  130. {
  131. for (HashMap<String, PackageEntry>::ConstIterator j = entries_.Begin(); j != entries_.End(); ++j)
  132. {
  133. if (!j->first_.Compare(fileName, false))
  134. return &j->second_;
  135. }
  136. }
  137. #endif
  138. return 0;
  139. }
  140. }