PackageFile.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 Atomic
  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 ATOMIC_API PackageFile : public Object
  38. {
  39. ATOMIC_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();
  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. // ATOMIC BEGIN
  72. /// Return a file name in the package at the specified index
  73. const String& GetEntryName(unsigned index) const
  74. {
  75. unsigned nn = 0;
  76. for (HashMap<String, PackageEntry>::ConstIterator j = entries_.Begin(); j != entries_.End(); ++j)
  77. {
  78. if (nn == index) return j->first_;
  79. nn++;
  80. }
  81. return String::EMPTY;
  82. }
  83. /// Scan package for specified files.
  84. void Scan(Vector<String>& result, const String& pathName, const String& filter, bool recursive) const;
  85. // ATOMIC END
  86. private:
  87. /// File entries.
  88. HashMap<String, PackageEntry> entries_;
  89. /// File name.
  90. String fileName_;
  91. /// Package file name hash.
  92. StringHash nameHash_;
  93. /// Package file total size.
  94. unsigned totalSize_;
  95. /// Total data size in the package using each entry's actual size if it is a compressed package file.
  96. unsigned totalDataSize_;
  97. /// Package file checksum.
  98. unsigned checksum_;
  99. /// Compressed flag.
  100. bool compressed_;
  101. };
  102. }