]> git.lizzy.rs Git - irrlicht.git/blob - source/Irrlicht/Android/CAndroidAssetFileArchive.cpp
53d06f8c60d5182a7a616c7a8489ebcfd55096ea
[irrlicht.git] / source / Irrlicht / Android / CAndroidAssetFileArchive.cpp
1 // Copyright (C) 2002-2011 Nikolaus Gebhardt\r
2 // This file is part of the "Irrlicht Engine".\r
3 // For conditions of distribution and use, see copyright notice in irrlicht.h\r
4 \r
5 #include "IrrCompileConfig.h"\r
6 \r
7 #ifdef   _IRR_COMPILE_ANDROID_ASSET_READER_\r
8 \r
9 #include "CAndroidAssetReader.h"\r
10 \r
11 #include "CReadFile.h"\r
12 #include "coreutil.h"\r
13 #include "CAndroidAssetFileArchive.h"\r
14 #include "CIrrDeviceAndroid.h"\r
15 #include "os.h" // for logging (just keep it in even when not needed right now as it's used all the time)\r
16 \r
17 #include <android_native_app_glue.h>\r
18 #include <android/native_activity.h>\r
19 #include <android/log.h>\r
20 \r
21 namespace irr\r
22 {\r
23 namespace io\r
24 {\r
25 \r
26 CAndroidAssetFileArchive::CAndroidAssetFileArchive(AAssetManager *assetManager, bool ignoreCase, bool ignorePaths)\r
27   : CFileList("/asset", ignoreCase, ignorePaths), AssetManager(assetManager)\r
28 {\r
29 }\r
30 \r
31 \r
32 CAndroidAssetFileArchive::~CAndroidAssetFileArchive()\r
33 {\r
34 }\r
35 \r
36 \r
37 //! get the archive type\r
38 E_FILE_ARCHIVE_TYPE CAndroidAssetFileArchive::getType() const\r
39 {\r
40         return EFAT_ANDROID_ASSET;\r
41 }\r
42 \r
43 const IFileList* CAndroidAssetFileArchive::getFileList() const\r
44 {\r
45     // The assert_manager can not read directory names, so\r
46     // getFileList returns only files in folders which have been added.\r
47     return this;\r
48 }\r
49 \r
50 \r
51 //! opens a file by file name\r
52 IReadFile* CAndroidAssetFileArchive::createAndOpenFile(const io::path& filename)\r
53 {\r
54     CAndroidAssetReader *reader = new CAndroidAssetReader(AssetManager, filename);\r
55 \r
56     if(reader->isOpen())\r
57                 return reader;\r
58 \r
59         reader->drop();\r
60     return NULL;\r
61 }\r
62 \r
63 //! opens a file by index\r
64 IReadFile* CAndroidAssetFileArchive::createAndOpenFile(u32 index)\r
65 {\r
66         const io::path& filename(getFullFileName(index));\r
67         if ( filename.empty() )\r
68                 return 0;\r
69         \r
70     return createAndOpenFile(filename);\r
71 }\r
72 \r
73 void CAndroidAssetFileArchive::addDirectoryToFileList(const io::path &dirname_)\r
74 {\r
75         io::path dirname(dirname_);\r
76         fschar_t lastChar = dirname.lastChar(); \r
77         if ( lastChar == '/' || lastChar == '\\' )\r
78                 dirname.erase(dirname.size()-1);\r
79         \r
80         // os::Printer::log("addDirectoryToFileList:", dirname.c_str(), ELL_DEBUG);\r
81         if  (findFile(dirname, true) >= 0 )\r
82                 return; // was already added\r
83         \r
84         AAssetDir *dir = AAssetManager_openDir(AssetManager, core::stringc(dirname).c_str());\r
85         if(!dir)\r
86                 return;\r
87 \r
88         // add directory itself \r
89         addItem(dirname, 0, 0, /*isDir*/true, getFileCount());\r
90         \r
91         // add all files in folder. \r
92         // Note: AAssetDir_getNextFileName does not return directory names (neither does any other NDK function)\r
93         while(const char *filename = AAssetDir_getNextFileName(dir))\r
94         {\r
95                 core::stringc full_filename= dirname=="" ? filename\r
96                                              : dirname+"/"+filename;\r
97 \r
98                 // We can't get the size without opening the file - so for performance\r
99                 // reasons we set the file size to 0.\r
100                 // TODO: Does this really cost so much performance that it's worth losing this information? Dirs are usually just added once at startup...\r
101                 addItem(full_filename, /*offet*/0, /*size*/0, /*isDir*/false, getFileCount());\r
102                 // os::Printer::log("addItem:", full_filename.c_str(), ELL_DEBUG);\r
103         }\r
104         AAssetDir_close(dir);\r
105 }\r
106 \r
107 } // end namespace io\r
108 } // end namespace irr\r
109 \r
110 #endif //  _IRR_COMPILE_ANDROID_ASSET_READER_\r