PackageFile.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // Copyright (c) 2008-2017 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. #pragma once
  23. #include "../Core/Object.h"
  24. namespace Urho3D
  25. {
  26. /// %File entry within the package file.
  27. struct PackageEntry
  28. {
  29. /// Offset from the beginning.
  30. unsigned offset_;
  31. /// File size.
  32. unsigned size_;
  33. /// File checksum.
  34. unsigned checksum_;
  35. };
  36. /// Stores files of a directory tree sequentially for convenient access.
  37. class URHO3D_API PackageFile : public Object
  38. {
  39. URHO3D_OBJECT(PackageFile, Object);
  40. public:
  41. /// Construct.
  42. PackageFile(Context* context);
  43. /// Construct and open.
  44. PackageFile(Context* context, const String& fileName, unsigned startOffset = 0);
  45. /// Destruct.
  46. virtual ~PackageFile() override;
  47. /// Open the package file. Return true if successful.
  48. bool Open(const String& fileName, unsigned startOffset = 0);
  49. /// Check if a file exists within the package file. This will be case-insensitive on Windows and case-sensitive on other platforms.
  50. bool Exists(const String& fileName) const;
  51. /// Return the file entry corresponding to the name, or null if not found. This will be case-insensitive on Windows and case-sensitive on other platforms.
  52. const PackageEntry* GetEntry(const String& fileName) const;
  53. /// Return all file entries.
  54. const HashMap<String, PackageEntry>& GetEntries() const { return entries_; }
  55. /// Return the package file name.
  56. const String& GetName() const { return fileName_; }
  57. /// Return hash of the package file name.
  58. StringHash GetNameHash() const { return nameHash_; }
  59. /// Return number of files.
  60. unsigned GetNumFiles() const { return entries_.Size(); }
  61. /// Return total size of the package file.
  62. unsigned GetTotalSize() const { return totalSize_; }
  63. /// Return total data size from all the file entries in the package file.
  64. unsigned GetTotalDataSize() const { return totalDataSize_; }
  65. /// Return checksum of the package file contents.
  66. unsigned GetChecksum() const { return checksum_; }
  67. /// Return whether the files are compressed.
  68. bool IsCompressed() const { return compressed_; }
  69. /// Return list of file names in the package.
  70. const Vector<String> GetEntryNames() const { return entries_.Keys(); }
  71. private:
  72. /// File entries.
  73. HashMap<String, PackageEntry> entries_;
  74. /// File name.
  75. String fileName_;
  76. /// Package file name hash.
  77. StringHash nameHash_;
  78. /// Package file total size.
  79. unsigned totalSize_;
  80. /// Total data size in the package using each entry's actual size if it is a compressed package file.
  81. unsigned totalDataSize_;
  82. /// Package file checksum.
  83. unsigned checksum_;
  84. /// Compressed flag.
  85. bool compressed_;
  86. };
  87. }