PackageFile.h 2.8 KB

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