PackageFile.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // Copyright (c) 2008-2016 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/FileSystem.h"
  24. #include "../IO/File.h"
  25. #include "../IO/Log.h"
  26. #include "../IO/PackageFile.h"
  27. namespace Atomic
  28. {
  29. PackageFile::PackageFile(Context* context) :
  30. Object(context),
  31. totalSize_(0),
  32. totalDataSize_(0),
  33. checksum_(0),
  34. compressed_(false)
  35. {
  36. }
  37. PackageFile::PackageFile(Context* context, const String& fileName, unsigned startOffset) :
  38. Object(context),
  39. totalSize_(0),
  40. totalDataSize_(0),
  41. checksum_(0),
  42. compressed_(false)
  43. {
  44. Open(fileName, startOffset);
  45. }
  46. PackageFile::~PackageFile()
  47. {
  48. }
  49. bool PackageFile::Open(const String& fileName, unsigned startOffset)
  50. {
  51. SharedPtr<File> file(new File(context_, fileName));
  52. if (!file->IsOpen())
  53. return false;
  54. // Check ID, then read the directory
  55. file->Seek(startOffset);
  56. String id = file->ReadFileID();
  57. if (id != "UPAK" && id != "ULZ4")
  58. {
  59. // If start offset has not been explicitly specified, also try to read package size from the end of file
  60. // to know how much we must rewind to find the package start
  61. if (!startOffset)
  62. {
  63. unsigned fileSize = file->GetSize();
  64. file->Seek((unsigned)(fileSize - sizeof(unsigned)));
  65. unsigned newStartOffset = fileSize - file->ReadUInt();
  66. if (newStartOffset < fileSize)
  67. {
  68. startOffset = newStartOffset;
  69. file->Seek(startOffset);
  70. id = file->ReadFileID();
  71. }
  72. }
  73. if (id != "UPAK" && id != "ULZ4")
  74. {
  75. ATOMIC_LOGERROR(fileName + " is not a valid package file");
  76. return false;
  77. }
  78. }
  79. fileName_ = fileName;
  80. nameHash_ = fileName_;
  81. totalSize_ = file->GetSize();
  82. compressed_ = id == "ULZ4";
  83. unsigned numFiles = file->ReadUInt();
  84. checksum_ = file->ReadUInt();
  85. for (unsigned i = 0; i < numFiles; ++i)
  86. {
  87. String entryName = file->ReadString();
  88. PackageEntry newEntry;
  89. newEntry.offset_ = file->ReadUInt() + startOffset;
  90. totalDataSize_ += (newEntry.size_ = file->ReadUInt());
  91. newEntry.checksum_ = file->ReadUInt();
  92. if (!compressed_ && newEntry.offset_ + newEntry.size_ > totalSize_)
  93. {
  94. ATOMIC_LOGERROR("File entry " + entryName + " outside package file");
  95. return false;
  96. }
  97. else
  98. entries_[entryName] = newEntry;
  99. }
  100. return true;
  101. }
  102. bool PackageFile::Exists(const String& fileName) const
  103. {
  104. bool found = entries_.Find(fileName) != entries_.End();
  105. #ifdef _WIN32
  106. // On Windows perform a fallback case-insensitive search
  107. if (!found)
  108. {
  109. for (HashMap<String, PackageEntry>::ConstIterator i = entries_.Begin(); i != entries_.End(); ++i)
  110. {
  111. if (!i->first_.Compare(fileName, false))
  112. {
  113. found = true;
  114. break;
  115. }
  116. }
  117. }
  118. #endif
  119. return found;
  120. }
  121. const PackageEntry* PackageFile::GetEntry(const String& fileName) const
  122. {
  123. HashMap<String, PackageEntry>::ConstIterator i = entries_.Find(fileName);
  124. if (i != entries_.End())
  125. return &i->second_;
  126. #ifdef _WIN32
  127. // On Windows perform a fallback case-insensitive search
  128. else
  129. {
  130. for (HashMap<String, PackageEntry>::ConstIterator j = entries_.Begin(); j != entries_.End(); ++j)
  131. {
  132. if (!j->first_.Compare(fileName, false))
  133. return &j->second_;
  134. }
  135. }
  136. #endif
  137. return 0;
  138. }
  139. // ATOMIC BEGIN
  140. void PackageFile::Scan(Vector<String>& result, const String& pathName, const String& filter, bool recursive) const
  141. {
  142. result.Clear();
  143. String sanitizedPath = GetSanitizedPath(pathName);
  144. String filterExtension = filter.Substring(filter.FindLast('.'));
  145. if (filterExtension.Contains('*'))
  146. filterExtension.Clear();
  147. const StringVector& entryNames = GetEntryNames();
  148. for (StringVector::ConstIterator i = entryNames.Begin(); i != entryNames.End(); ++i)
  149. {
  150. String entryName = GetSanitizedPath(*i);
  151. if ((filterExtension.Empty() || entryName.EndsWith(filterExtension)) &&
  152. entryName.StartsWith(sanitizedPath))
  153. {
  154. String fileName = entryName.Substring(sanitizedPath.Length());
  155. if (fileName.StartsWith("\\") || fileName.StartsWith("/"))
  156. fileName = fileName.Substring(1, fileName.Length() - 1);
  157. if (!recursive && (fileName.Contains("\\") || fileName.Contains("/")))
  158. continue;
  159. result.Push(fileName);
  160. }
  161. }
  162. }
  163. // ATOMIC END
  164. }