2
0

ResourceFilesystem.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  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
  19. {
  20. public:
  21. ResourceFile(GenericMemoryPoolAllocator<U8> alloc)
  22. : m_alloc(alloc)
  23. {
  24. }
  25. ResourceFile(const ResourceFile&) = delete; // Non-copyable
  26. virtual ~ResourceFile()
  27. {
  28. }
  29. ResourceFile& operator=(const ResourceFile&) = delete; // Non-copyable
  30. /// Read data from the file
  31. virtual ANKI_USE_RESULT Error read(void* buff, PtrSize size) = 0;
  32. /// Read all the contents of a text file. If the file is not rewined it will probably fail
  33. virtual ANKI_USE_RESULT Error readAllText(StringAuto& out) = 0;
  34. /// Read 32bit unsigned integer. Set the endianness if the file's endianness is different from the machine's
  35. virtual ANKI_USE_RESULT Error readU32(U32& u) = 0;
  36. /// Read 32bit float. Set the endianness if the file's endianness is different from the machine's
  37. virtual ANKI_USE_RESULT Error readF32(F32& f) = 0;
  38. /// Set the position indicator to a new position
  39. /// @param offset Number of bytes to offset from origin
  40. /// @param origin Position used as reference for the offset
  41. virtual ANKI_USE_RESULT Error seek(PtrSize offset, FileSeekOrigin origin) = 0;
  42. /// Get the size of the file.
  43. virtual PtrSize getSize() const = 0;
  44. Atomic<I32>& getRefcount()
  45. {
  46. return m_refcount;
  47. }
  48. GenericMemoryPoolAllocator<U8> getAllocator() const
  49. {
  50. return m_alloc;
  51. }
  52. private:
  53. GenericMemoryPoolAllocator<U8> m_alloc;
  54. Atomic<I32> m_refcount = {0};
  55. };
  56. /// Resource file smart pointer.
  57. using ResourceFilePtr = IntrusivePtr<ResourceFile>;
  58. /// Resource filesystem.
  59. class ResourceFilesystem
  60. {
  61. public:
  62. ResourceFilesystem(GenericMemoryPoolAllocator<U8> alloc)
  63. : m_alloc(alloc)
  64. {
  65. }
  66. ResourceFilesystem(const ResourceFilesystem&) = delete; // Non-copyable
  67. ~ResourceFilesystem();
  68. ResourceFilesystem& operator=(const ResourceFilesystem&) = delete; // Non-copyable
  69. ANKI_USE_RESULT Error init(const ConfigSet& config, const CString& cacheDir);
  70. /// Search the path list to find the file. Then open the file for reading. It's thread-safe.
  71. ANKI_USE_RESULT Error openFile(const ResourceFilename& filename, ResourceFilePtr& file);
  72. /// Iterate all the filenames from all paths provided.
  73. template<typename TFunc>
  74. ANKI_USE_RESULT Error iterateAllFilenames(TFunc func) const
  75. {
  76. for(const Path& path : m_paths)
  77. {
  78. for(const String& fname : path.m_files)
  79. {
  80. ANKI_CHECK(func(fname.toCString()));
  81. }
  82. }
  83. return Error::NONE;
  84. }
  85. #if !ANKI_TESTS
  86. private:
  87. #endif
  88. class Path
  89. {
  90. public:
  91. StringList m_files; ///< Files inside the directory.
  92. String m_path; ///< A directory or an archive.
  93. Bool m_isArchive = false;
  94. Bool m_isCache = false;
  95. Bool m_isSpecial = false;
  96. Path() = default;
  97. Path(const Path&) = delete; // Non-copyable
  98. Path(Path&& b)
  99. {
  100. *this = std::move(b);
  101. }
  102. Path& operator=(const Path&) = delete; // Non-copyable
  103. Path& operator=(Path&& b)
  104. {
  105. m_files = std::move(b.m_files);
  106. m_path = std::move(b.m_path);
  107. m_isArchive = b.m_isArchive;
  108. m_isCache = b.m_isCache;
  109. m_isSpecial = b.m_isSpecial;
  110. return *this;
  111. }
  112. };
  113. GenericMemoryPoolAllocator<U8> m_alloc;
  114. List<Path> m_paths;
  115. String m_cacheDir;
  116. /// Add a filesystem path or an archive. The path is read-only.
  117. ANKI_USE_RESULT Error addNewPath(const CString& path, const StringListAuto& excludedStrings, Bool special = false);
  118. void addCachePath(const CString& path);
  119. };
  120. /// @}
  121. } // end namespace anki