PackageFile.cpp 3.0 KB

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