PackageFile.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #pragma once
  24. #include "HashMap.h"
  25. #include "Object.h"
  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 PackageFile : public Object
  38. {
  39. OBJECT(PackageFile);
  40. public:
  41. /// Construct.
  42. PackageFile(Context* context);
  43. /// Construct and open.
  44. PackageFile(Context* context, const String& fileName);
  45. /// Destruct.
  46. virtual ~PackageFile();
  47. /// Open the package file. Return true if successful.
  48. bool Open(const String& fileName);
  49. /// Check if a file exists within the package file.
  50. bool Exists(const String& fileName) const;
  51. /// Return the file entry corresponding to the name, or null if not found.
  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 checksum of the package file contents.
  64. unsigned GetChecksum() const { return checksum_; }
  65. private:
  66. /// File entries.
  67. HashMap<String, PackageEntry> entries_;
  68. /// File name.
  69. String fileName_;
  70. /// Package file name hash.
  71. StringHash nameHash_;
  72. /// Package file total size.
  73. unsigned totalSize_;
  74. /// Package file checksum.
  75. unsigned checksum_;
  76. };