PackageFile.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 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. OBJECTTYPESTATIC(PackageFile);
  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_ = StringHash(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. }