PackageFile.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  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. #pragma once
  24. #include "Object.h"
  25. namespace Urho3D
  26. {
  27. /// %File entry within the package file.
  28. struct PackageEntry
  29. {
  30. /// Offset from the beginning.
  31. unsigned offset_;
  32. /// File size.
  33. unsigned size_;
  34. /// File checksum.
  35. unsigned checksum_;
  36. };
  37. /// Stores files of a directory tree sequentially for convenient access.
  38. class PackageFile : public Object
  39. {
  40. OBJECT(PackageFile);
  41. public:
  42. /// Construct.
  43. PackageFile(Context* context);
  44. /// Construct and open.
  45. PackageFile(Context* context, const String& fileName);
  46. /// Destruct.
  47. virtual ~PackageFile();
  48. /// Open the package file. Return true if successful.
  49. bool Open(const String& fileName);
  50. /// Check if a file exists within the package file.
  51. bool Exists(const String& fileName) const;
  52. /// Return the file entry corresponding to the name, or null if not found.
  53. const PackageEntry* GetEntry(const String& fileName) const;
  54. /// Return all file entries.
  55. const HashMap<String, PackageEntry>& GetEntries() const { return entries_; }
  56. /// Return the package file name.
  57. const String& GetName() const { return fileName_; }
  58. /// Return hash of the package file name.
  59. StringHash GetNameHash() const { return nameHash_; }
  60. /// Return number of files.
  61. unsigned GetNumFiles() const { return entries_.Size(); }
  62. /// Return total size of the package file.
  63. unsigned GetTotalSize() const { return totalSize_; }
  64. /// Return checksum of the package file contents.
  65. unsigned GetChecksum() const { return checksum_; }
  66. private:
  67. /// File entries.
  68. HashMap<String, PackageEntry> entries_;
  69. /// File name.
  70. String fileName_;
  71. /// Package file name hash.
  72. StringHash nameHash_;
  73. /// Package file total size.
  74. unsigned totalSize_;
  75. /// Package file checksum.
  76. unsigned checksum_;
  77. };
  78. }