PackageFile.cpp 2.9 KB

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