PackageFile.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "File.h"
  25. #include "Log.h"
  26. #include "PackageFile.h"
  27. namespace Urho3D
  28. {
  29. OBJECTTYPESTATIC(PackageFile);
  30. PackageFile::PackageFile(Context* context) :
  31. Object(context),
  32. totalSize_(0),
  33. checksum_(0)
  34. {
  35. }
  36. PackageFile::PackageFile(Context* context, const String& fileName) :
  37. Object(context),
  38. totalSize_(0),
  39. checksum_(0)
  40. {
  41. Open(fileName);
  42. }
  43. PackageFile::~PackageFile()
  44. {
  45. }
  46. bool PackageFile::Open(const String& fileName)
  47. {
  48. SharedPtr<File> file(new File(context_, fileName));
  49. if (!file->IsOpen())
  50. return false;
  51. // Check ID, then read the directory
  52. if (file->ReadFileID() != "UPAK")
  53. {
  54. LOGERROR(fileName + " is not a valid package file");
  55. return false;
  56. }
  57. fileName_ = fileName;
  58. nameHash_ = StringHash(fileName_);
  59. totalSize_ = file->GetSize();
  60. unsigned numFiles = file->ReadUInt();
  61. checksum_ = file->ReadUInt();
  62. for (unsigned i = 0; i < numFiles; ++i)
  63. {
  64. String entryName = file->ReadString();
  65. PackageEntry newEntry;
  66. newEntry.offset_ = file->ReadUInt();
  67. newEntry.size_ = file->ReadUInt();
  68. newEntry.checksum_ = file->ReadUInt();
  69. if (newEntry.offset_ + newEntry.size_ > totalSize_)
  70. LOGERROR("File entry " + entryName + " outside package file");
  71. else
  72. entries_[entryName.ToLower()] = newEntry;
  73. }
  74. return true;
  75. }
  76. bool PackageFile::Exists(const String& fileName) const
  77. {
  78. return entries_.Find(fileName.ToLower()) != entries_.End();
  79. }
  80. const PackageEntry* PackageFile::GetEntry(const String& fileName) const
  81. {
  82. HashMap<String, PackageEntry>::ConstIterator i = entries_.Find(fileName.ToLower());
  83. if (i != entries_.End())
  84. return &i->second_;
  85. else
  86. return 0;
  87. }
  88. }