MountPoint.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "OS.h"
  25. #include "File.h"
  26. namespace crown
  27. {
  28. /// Hashed values for supported MountPoint types
  29. const char* const DISK_MOUNT_POINT = "disk";
  30. const char* const ANDROID_MOUNT_POINT = "asset";
  31. const uint32_t DISK_TYPE = 0x7BCBC5EE;
  32. const uint32_t ANDROID_TYPE = 0xAAD5F176;
  33. /// Represent single entity in MountPoint
  34. struct MountPointEntry
  35. {
  36. enum Type
  37. {
  38. DIRECTORY = 0, /// The entry is a directory
  39. FILE, /// The entry is a file
  40. MEMORY, /// The entry is a memory file (i.e. does not exist on the disk)
  41. UNKNOWN /// The entry type is unknown
  42. };
  43. MountPointEntry() : type(UNKNOWN) {}
  44. Type type; /// Type of the entry
  45. char os_path[MAX_PATH_LENGTH]; /// OS-specific path (use only for debug)
  46. char relative_path[MAX_PATH_LENGTH]; /// Relative path of the entry
  47. };
  48. /// Interface which provides a platform-independent way to access files and directories.
  49. /// Each MountPoint are managed by FileSystem.
  50. /// There may be several types of MountPoint:
  51. ///
  52. /// - DiskMountPoint - provides interaction with HDD, DVD, BlueRay...
  53. /// - ZipMountPoint - provides interaction with compressed archives
  54. /// - NetworkMountPoint - provides interaction with network
  55. ///
  56. /// Accessing files:
  57. /// Every file and every directory must be accessed through the Filesystem's MountPoints.
  58. /// Not a single C/C++ std file io call or other similar facilities
  59. /// should be used in any other part of the engine in order to maintain
  60. /// absolute platform independence.
  61. ///
  62. /// MountPoint maintains a root path which acts as base directory for every
  63. /// file operation; access to files outside the root path is not allowed. If
  64. /// you really need it, instantiate another filesystem whith the appropriate
  65. /// root path (e.g.)
  66. ///
  67. /// The MountPoint will take care of the necessary path conversions.
  68. /// The root path must be an absolute path for the underlying operating system.
  69. class MountPoint
  70. {
  71. public:
  72. inline MountPoint(uint32_t type) : m_type(type) {}
  73. /// Opens a file and returns a specific instance
  74. virtual File* open(const char* path, FileOpenMode mode) = 0;
  75. /// Close file
  76. virtual void close(File* file) = 0;
  77. /// Returns whether the @a relative_path exists
  78. virtual bool exists(const char* relative_path) = 0;
  79. virtual const char* os_path(const char* relative_path) = 0;
  80. uint32_t type() const { return m_type; }
  81. protected:
  82. MountPoint* m_next;
  83. uint32_t m_type;
  84. friend class Filesystem;
  85. };
  86. } // namespace crown