ResourceFilesystem.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright (C) 2009-2016, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <anki/resource/Common.h>
  7. #include <anki/util/String.h>
  8. #include <anki/util/StringList.h>
  9. #include <anki/util/File.h>
  10. #include <anki/util/Ptr.h>
  11. namespace anki
  12. {
  13. // Forward
  14. class ConfigSet;
  15. /// @addtogroup resource
  16. /// @{
  17. /// Resource filesystem file. An interface that abstracts the resource file.
  18. class ResourceFile : public NonCopyable
  19. {
  20. public:
  21. using SeekOrigin = File::SeekOrigin;
  22. ResourceFile(GenericMemoryPoolAllocator<U8> alloc)
  23. : m_alloc(alloc)
  24. {
  25. }
  26. virtual ~ResourceFile()
  27. {
  28. }
  29. /// Read data from the file
  30. virtual ANKI_USE_RESULT Error read(void* buff, PtrSize size) = 0;
  31. /// Read all the contents of a text file
  32. /// If the file is not rewined it will probably fail
  33. virtual ANKI_USE_RESULT Error readAllText(
  34. GenericMemoryPoolAllocator<U8> alloc, String& out) = 0;
  35. /// Read 32bit unsigned integer. Set the endianness if the file's
  36. /// endianness is different from the machine's
  37. virtual ANKI_USE_RESULT Error readU32(U32& u) = 0;
  38. /// Read 32bit float. Set the endianness if the file's endianness is
  39. /// different from the machine's
  40. virtual ANKI_USE_RESULT Error readF32(F32& f) = 0;
  41. /// Set the position indicator to a new position
  42. /// @param offset Number of bytes to offset from origin
  43. /// @param origin Position used as reference for the offset
  44. virtual ANKI_USE_RESULT Error seek(PtrSize offset, SeekOrigin origin) = 0;
  45. /// Get the size of the file.
  46. virtual PtrSize getSize() const = 0;
  47. Atomic<I32>& getRefcount()
  48. {
  49. return m_refcount;
  50. }
  51. GenericMemoryPoolAllocator<U8> getAllocator() const
  52. {
  53. return m_alloc;
  54. }
  55. private:
  56. GenericMemoryPoolAllocator<U8> m_alloc;
  57. Atomic<I32> m_refcount = {0};
  58. };
  59. /// Resource file smart pointer.
  60. using ResourceFilePtr = IntrusivePtr<ResourceFile>;
  61. /// Resource filesystem.
  62. class ResourceFilesystem : public NonCopyable
  63. {
  64. public:
  65. ResourceFilesystem(GenericMemoryPoolAllocator<U8> alloc)
  66. : m_alloc(alloc)
  67. {
  68. }
  69. ~ResourceFilesystem();
  70. ANKI_USE_RESULT Error init(
  71. const ConfigSet& config, const CString& cacheDir);
  72. /// Search the path list to find the file. Then open the file for reading.
  73. ANKI_USE_RESULT Error openFile(
  74. const ResourceFilename& filename, ResourceFilePtr& file);
  75. private:
  76. class Path : public NonCopyable
  77. {
  78. public:
  79. StringList m_files; ///< Files inside the directory.
  80. String m_path; ///< A directory or an archive.
  81. Bool8 m_isArchive = false;
  82. Bool8 m_isCache = false;
  83. Path() = default;
  84. Path(Path&& b)
  85. : m_files(std::move(b.m_files))
  86. , m_path(std::move(b.m_path))
  87. , m_isArchive(std::move(b.m_isArchive))
  88. , m_isCache(std::move(b.m_isCache))
  89. {
  90. }
  91. Path& operator=(Path&& b)
  92. {
  93. m_files = std::move(b.m_files);
  94. m_path = std::move(b.m_path);
  95. m_isArchive = std::move(b.m_isArchive);
  96. m_isCache = std::move(b.m_isCache);
  97. return *this;
  98. }
  99. };
  100. GenericMemoryPoolAllocator<U8> m_alloc;
  101. List<Path> m_paths;
  102. String m_cacheDir;
  103. /// Add a filesystem path or an archive. The path is read-only.
  104. ANKI_USE_RESULT Error addNewPath(const CString& path);
  105. void addCachePath(const CString& path);
  106. };
  107. /// @}
  108. } // end namespace anki