PackageFile.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // Copyright (c) 2008-2013 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. blockSize_(0),
  33. compressed_(false)
  34. {
  35. }
  36. PackageFile::PackageFile(Context* context, const String& fileName) :
  37. Object(context),
  38. totalSize_(0),
  39. checksum_(0),
  40. blockSize_(0),
  41. compressed_(false)
  42. {
  43. Open(fileName);
  44. }
  45. PackageFile::~PackageFile()
  46. {
  47. }
  48. bool PackageFile::Open(const String& fileName)
  49. {
  50. SharedPtr<File> file(new File(context_, fileName));
  51. if (!file->IsOpen())
  52. return false;
  53. // Check ID, then read the directory
  54. String id = file->ReadFileID();
  55. if (id != "UPAK" && id != "ULZ4")
  56. {
  57. LOGERROR(fileName + " is not a valid package file");
  58. return false;
  59. }
  60. fileName_ = fileName;
  61. nameHash_ = fileName_;
  62. totalSize_ = file->GetSize();
  63. compressed_ = id == "ULZ4";
  64. unsigned numFiles = file->ReadUInt();
  65. checksum_ = file->ReadUInt();
  66. if (compressed_)
  67. blockSize_ = file->ReadUInt();
  68. for (unsigned i = 0; i < numFiles; ++i)
  69. {
  70. String entryName = file->ReadString();
  71. PackageEntry newEntry;
  72. newEntry.offset_ = file->ReadUInt();
  73. newEntry.size_ = file->ReadUInt();
  74. newEntry.checksum_ = file->ReadUInt();
  75. if (!compressed_ && newEntry.offset_ + newEntry.size_ > totalSize_)
  76. LOGERROR("File entry " + entryName + " outside package file");
  77. else
  78. entries_[entryName.ToLower()] = newEntry;
  79. }
  80. return true;
  81. }
  82. bool PackageFile::Exists(const String& fileName) const
  83. {
  84. return entries_.Find(fileName.ToLower()) != entries_.End();
  85. }
  86. const PackageEntry* PackageFile::GetEntry(const String& fileName) const
  87. {
  88. HashMap<String, PackageEntry>::ConstIterator i = entries_.Find(fileName.ToLower());
  89. if (i != entries_.End())
  90. return &i->second_;
  91. else
  92. return 0;
  93. }
  94. }