PackageFile.cpp 6.0 KB

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