ResourceFilesystem.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. #include <AnKi/Resource/ResourceFilesystem.h>
  6. #include <AnKi/Util/Filesystem.h>
  7. #include <AnKi/Core/ConfigSet.h>
  8. #include <AnKi/Util/Tracer.h>
  9. #include <ZLib/contrib/minizip/unzip.h>
  10. namespace anki
  11. {
  12. /// C resource file
  13. class CResourceFile final : public ResourceFile
  14. {
  15. public:
  16. File m_file;
  17. CResourceFile(GenericMemoryPoolAllocator<U8> alloc)
  18. : ResourceFile(alloc)
  19. {
  20. }
  21. ANKI_USE_RESULT Error read(void* buff, PtrSize size) override
  22. {
  23. ANKI_TRACE_SCOPED_EVENT(RSRC_FILE_READ);
  24. return m_file.read(buff, size);
  25. }
  26. ANKI_USE_RESULT Error readAllText(StringAuto& out) override
  27. {
  28. ANKI_TRACE_SCOPED_EVENT(RSRC_FILE_READ);
  29. return m_file.readAllText(out);
  30. }
  31. ANKI_USE_RESULT Error readU32(U32& u) override
  32. {
  33. ANKI_TRACE_SCOPED_EVENT(RSRC_FILE_READ);
  34. return m_file.readU32(u);
  35. }
  36. ANKI_USE_RESULT Error readF32(F32& f) override
  37. {
  38. ANKI_TRACE_SCOPED_EVENT(RSRC_FILE_READ);
  39. return m_file.readF32(f);
  40. }
  41. ANKI_USE_RESULT Error seek(PtrSize offset, FileSeekOrigin origin) override
  42. {
  43. return m_file.seek(offset, origin);
  44. }
  45. PtrSize getSize() const override
  46. {
  47. return m_file.getSize();
  48. }
  49. };
  50. /// ZIP file
  51. class ZipResourceFile final : public ResourceFile
  52. {
  53. public:
  54. unzFile m_archive = nullptr;
  55. PtrSize m_size = 0;
  56. ZipResourceFile(GenericMemoryPoolAllocator<U8> alloc)
  57. : ResourceFile(alloc)
  58. {
  59. }
  60. ~ZipResourceFile()
  61. {
  62. if(m_archive)
  63. {
  64. // It's open
  65. unzClose(m_archive);
  66. m_archive = nullptr;
  67. m_size = 0;
  68. }
  69. }
  70. ANKI_USE_RESULT Error open(const CString& archive, const CString& archivedFname)
  71. {
  72. // Open archive
  73. m_archive = unzOpen(&archive[0]);
  74. if(m_archive == nullptr)
  75. {
  76. ANKI_RESOURCE_LOGE("Failed to open archive");
  77. return Error::FILE_ACCESS;
  78. }
  79. // Locate archived
  80. const int caseSensitive = 1;
  81. if(unzLocateFile(m_archive, &archivedFname[0], caseSensitive) != UNZ_OK)
  82. {
  83. ANKI_RESOURCE_LOGE("Failed to locate file in archive");
  84. return Error::FILE_ACCESS;
  85. }
  86. // Open file
  87. if(unzOpenCurrentFile(m_archive) != UNZ_OK)
  88. {
  89. ANKI_RESOURCE_LOGE("unzOpenCurrentFile() failed");
  90. return Error::FILE_ACCESS;
  91. }
  92. // Get size just in case
  93. unz_file_info zinfo;
  94. zinfo.uncompressed_size = 0;
  95. unzGetCurrentFileInfo(m_archive, &zinfo, nullptr, 0, nullptr, 0, nullptr, 0);
  96. m_size = zinfo.uncompressed_size;
  97. ANKI_ASSERT(m_size != 0);
  98. return Error::NONE;
  99. }
  100. void close()
  101. {
  102. if(m_archive)
  103. {
  104. unzClose(m_archive);
  105. m_archive = nullptr;
  106. m_size = 0;
  107. }
  108. }
  109. ANKI_USE_RESULT Error read(void* buff, PtrSize size) override
  110. {
  111. ANKI_TRACE_SCOPED_EVENT(RSRC_FILE_READ);
  112. I64 readSize = unzReadCurrentFile(m_archive, buff, U32(size));
  113. if(I64(size) != readSize)
  114. {
  115. ANKI_RESOURCE_LOGE("File read failed");
  116. return Error::FILE_ACCESS;
  117. }
  118. return Error::NONE;
  119. }
  120. ANKI_USE_RESULT Error readAllText(StringAuto& out) override
  121. {
  122. ANKI_ASSERT(m_size);
  123. out.create('?', m_size);
  124. return read(&out[0], m_size);
  125. }
  126. ANKI_USE_RESULT Error readU32(U32& u) override
  127. {
  128. // Assume machine and file have same endianness
  129. ANKI_CHECK(read(&u, sizeof(u)));
  130. return Error::NONE;
  131. }
  132. ANKI_USE_RESULT Error readF32(F32& u) override
  133. {
  134. // Assume machine and file have same endianness
  135. ANKI_CHECK(read(&u, sizeof(u)));
  136. return Error::NONE;
  137. }
  138. ANKI_USE_RESULT Error seek(PtrSize offset, FileSeekOrigin origin) override
  139. {
  140. // Rewind if needed
  141. if(origin == FileSeekOrigin::BEGINNING)
  142. {
  143. if(unzCloseCurrentFile(m_archive) || unzOpenCurrentFile(m_archive))
  144. {
  145. ANKI_RESOURCE_LOGE("Rewind failed");
  146. return Error::FUNCTION_FAILED;
  147. }
  148. }
  149. // Move forward by reading dummy data
  150. Array<char, 128> buff;
  151. while(offset != 0)
  152. {
  153. PtrSize toRead = min<PtrSize>(offset, sizeof(buff));
  154. ANKI_CHECK(read(&buff[0], toRead));
  155. offset -= toRead;
  156. }
  157. return Error::NONE;
  158. }
  159. PtrSize getSize() const override
  160. {
  161. ANKI_ASSERT(m_size > 0);
  162. return m_size;
  163. }
  164. };
  165. ResourceFilesystem::~ResourceFilesystem()
  166. {
  167. for(Path& p : m_paths)
  168. {
  169. p.m_files.destroy(m_alloc);
  170. p.m_path.destroy(m_alloc);
  171. }
  172. m_paths.destroy(m_alloc);
  173. m_cacheDir.destroy(m_alloc);
  174. }
  175. Error ResourceFilesystem::init(const ConfigSet& config, const CString& cacheDir)
  176. {
  177. StringListAuto paths(m_alloc);
  178. paths.splitString(config.getString("rsrc_dataPaths"), ':');
  179. // Workaround the fact that : is used in drives in Windows
  180. #if ANKI_OS_WINDOWS
  181. StringListAuto paths2(m_alloc);
  182. StringListAuto::Iterator it = paths.getBegin();
  183. while(it != paths.getEnd())
  184. {
  185. const String& s = *it;
  186. StringListAuto::Iterator it2 = it + 1;
  187. if(s.getLength() == 1 && (s[0] >= 'a' && s[0] <= 'z') || (s[0] >= 'A' && s[0] <= 'Z') && it2 != paths.getEnd())
  188. {
  189. paths2.pushBackSprintf("%s:%s", s.cstr(), it2->cstr());
  190. ++it;
  191. }
  192. else
  193. {
  194. paths2.pushBack(s);
  195. }
  196. ++it;
  197. }
  198. paths.destroy();
  199. paths = std::move(paths2);
  200. #endif
  201. if(paths.getSize() < 1)
  202. {
  203. ANKI_RESOURCE_LOGE("Config option \"rsrc_dataPaths\" is empty");
  204. return Error::USER_DATA;
  205. }
  206. for(auto& path : paths)
  207. {
  208. ANKI_CHECK(addNewPath(path.toCString()));
  209. }
  210. addCachePath(cacheDir);
  211. return Error::NONE;
  212. }
  213. void ResourceFilesystem::addCachePath(const CString& path)
  214. {
  215. Path p;
  216. p.m_path.create(m_alloc, path);
  217. p.m_isCache = true;
  218. m_paths.emplaceBack(m_alloc, std::move(p));
  219. }
  220. Error ResourceFilesystem::addNewPath(const CString& path)
  221. {
  222. U32 fileCount = 0;
  223. static const CString extension(".AnKiZLibip");
  224. auto pos = path.find(extension);
  225. if(pos != CString::NPOS && pos == path.getLength() - extension.getLength())
  226. {
  227. // It's an archive
  228. // Open
  229. unzFile zfile = unzOpen(&path[0]);
  230. if(!zfile)
  231. {
  232. ANKI_RESOURCE_LOGE("Failed to open archive");
  233. return Error::FILE_ACCESS;
  234. }
  235. // List files
  236. if(unzGoToFirstFile(zfile) != UNZ_OK)
  237. {
  238. unzClose(zfile);
  239. ANKI_RESOURCE_LOGE("unzGoToFirstFile() failed. Empty archive?");
  240. return Error::FILE_ACCESS;
  241. }
  242. Path p;
  243. p.m_isArchive = true;
  244. p.m_path.sprintf(m_alloc, "%s", &path[0]);
  245. do
  246. {
  247. Array<char, 1024> filename;
  248. unz_file_info info;
  249. if(unzGetCurrentFileInfo(zfile, &info, &filename[0], filename.getSize(), nullptr, 0, nullptr, 0) != UNZ_OK)
  250. {
  251. unzClose(zfile);
  252. ANKI_RESOURCE_LOGE("unzGetCurrentFileInfo() failed");
  253. return Error::FILE_ACCESS;
  254. }
  255. // If compressed size is zero then it's a dir
  256. if(info.uncompressed_size > 0)
  257. {
  258. p.m_files.pushBackSprintf(m_alloc, "%s", &filename[0]);
  259. ++fileCount;
  260. }
  261. } while(unzGoToNextFile(zfile) == UNZ_OK);
  262. m_paths.emplaceFront(m_alloc, std::move(p));
  263. unzClose(zfile);
  264. }
  265. else
  266. {
  267. // It's simple directory
  268. m_paths.emplaceFront(m_alloc, Path());
  269. Path& p = m_paths.getFront();
  270. p.m_path.sprintf(m_alloc, "%s", &path[0]);
  271. p.m_isArchive = false;
  272. struct UserData
  273. {
  274. ResourceFilesystem* m_sys;
  275. U32* m_fileCount;
  276. } ud{this, &fileCount};
  277. ANKI_CHECK(walkDirectoryTree(path, &ud, [](const CString& fname, void* ud, Bool isDir) -> Error {
  278. if(isDir)
  279. {
  280. return Error::NONE;
  281. }
  282. UserData* udd = static_cast<UserData*>(ud);
  283. ResourceFilesystem* self = udd->m_sys;
  284. Path& p = self->m_paths.getFront();
  285. p.m_files.pushBackSprintf(self->m_alloc, "%s", fname.cstr());
  286. ++(*udd->m_fileCount);
  287. return Error::NONE;
  288. }));
  289. if(p.m_files.getSize() < 1)
  290. {
  291. ANKI_RESOURCE_LOGE("Directory is empty: %s", &path[0]);
  292. return Error::USER_DATA;
  293. }
  294. }
  295. ANKI_RESOURCE_LOGI("Added new data path \"%s\" that contains %u files", &path[0], fileCount);
  296. return Error::NONE;
  297. }
  298. Error ResourceFilesystem::openFile(const ResourceFilename& filename, ResourceFilePtr& filePtr)
  299. {
  300. ResourceFile* rfile = nullptr;
  301. Error err = Error::NONE;
  302. // Search for the fname in reverse order
  303. for(const Path& p : m_paths)
  304. {
  305. // Check if it's cache
  306. if(p.m_isCache)
  307. {
  308. StringAuto newFname(m_alloc);
  309. newFname.sprintf("%s/%s", &p.m_path[0], &filename[0]);
  310. if(fileExists(newFname.toCString()))
  311. {
  312. // In cache
  313. CResourceFile* file = m_alloc.newInstance<CResourceFile>(m_alloc);
  314. rfile = file;
  315. err = file->m_file.open(&newFname[0], FileOpenFlag::READ);
  316. }
  317. }
  318. else
  319. {
  320. // In data path or archive
  321. for(const String& pfname : p.m_files)
  322. {
  323. if(pfname != filename)
  324. {
  325. continue;
  326. }
  327. // Found
  328. if(p.m_isArchive)
  329. {
  330. ZipResourceFile* file = m_alloc.newInstance<ZipResourceFile>(m_alloc);
  331. rfile = file;
  332. err = file->open(p.m_path.toCString(), filename);
  333. }
  334. else
  335. {
  336. StringAuto newFname(m_alloc);
  337. newFname.sprintf("%s/%s", &p.m_path[0], &filename[0]);
  338. CResourceFile* file = m_alloc.newInstance<CResourceFile>(m_alloc);
  339. rfile = file;
  340. err = file->m_file.open(&newFname[0], FileOpenFlag::READ);
  341. #if 0
  342. printf("Opening asset %s\n", &newFname[0]);
  343. #endif
  344. }
  345. }
  346. } // end if cache
  347. if(rfile)
  348. {
  349. break;
  350. }
  351. } // end for all paths
  352. if(err)
  353. {
  354. m_alloc.deleteInstance(rfile);
  355. return err;
  356. }
  357. if(!rfile)
  358. {
  359. ANKI_RESOURCE_LOGE("File not found: %s", &filename[0]);
  360. return Error::USER_DATA;
  361. }
  362. // Done
  363. filePtr.reset(rfile);
  364. return Error::NONE;
  365. }
  366. } // end namespace anki