diff options
Diffstat (limited to 'libraries/irrlicht-1.8/source/Irrlicht/CPakReader.cpp')
-rw-r--r-- | libraries/irrlicht-1.8/source/Irrlicht/CPakReader.cpp | 392 |
1 files changed, 196 insertions, 196 deletions
diff --git a/libraries/irrlicht-1.8/source/Irrlicht/CPakReader.cpp b/libraries/irrlicht-1.8/source/Irrlicht/CPakReader.cpp index 7b96842..14f42db 100644 --- a/libraries/irrlicht-1.8/source/Irrlicht/CPakReader.cpp +++ b/libraries/irrlicht-1.8/source/Irrlicht/CPakReader.cpp | |||
@@ -1,196 +1,196 @@ | |||
1 | // Copyright (C) 2002-2012 Nikolaus Gebhardt | 1 | // Copyright (C) 2002-2012 Nikolaus Gebhardt |
2 | // This file is part of the "Irrlicht Engine". | 2 | // This file is part of the "Irrlicht Engine". |
3 | // For conditions of distribution and use, see copyright notice in irrlicht.h | 3 | // For conditions of distribution and use, see copyright notice in irrlicht.h |
4 | // Code contributed by skreamz | 4 | // Code contributed by skreamz |
5 | 5 | ||
6 | #include "CPakReader.h" | 6 | #include "CPakReader.h" |
7 | 7 | ||
8 | #ifdef __IRR_COMPILE_WITH_PAK_ARCHIVE_LOADER_ | 8 | #ifdef __IRR_COMPILE_WITH_PAK_ARCHIVE_LOADER_ |
9 | 9 | ||
10 | #include "os.h" | 10 | #include "os.h" |
11 | #include "coreutil.h" | 11 | #include "coreutil.h" |
12 | 12 | ||
13 | namespace irr | 13 | namespace irr |
14 | { | 14 | { |
15 | namespace io | 15 | namespace io |
16 | { | 16 | { |
17 | 17 | ||
18 | namespace | 18 | namespace |
19 | { | 19 | { |
20 | 20 | ||
21 | inline bool isHeaderValid(const SPAKFileHeader& header) | 21 | inline bool isHeaderValid(const SPAKFileHeader& header) |
22 | { | 22 | { |
23 | const c8* tag = header.tag; | 23 | const c8* tag = header.tag; |
24 | return tag[0] == 'P' && | 24 | return tag[0] == 'P' && |
25 | tag[1] == 'A' && | 25 | tag[1] == 'A' && |
26 | tag[2] == 'C' && | 26 | tag[2] == 'C' && |
27 | tag[3] == 'K'; | 27 | tag[3] == 'K'; |
28 | } | 28 | } |
29 | 29 | ||
30 | } // end namespace | 30 | } // end namespace |
31 | 31 | ||
32 | //! Constructor | 32 | //! Constructor |
33 | CArchiveLoaderPAK::CArchiveLoaderPAK( io::IFileSystem* fs) | 33 | CArchiveLoaderPAK::CArchiveLoaderPAK( io::IFileSystem* fs) |
34 | : FileSystem(fs) | 34 | : FileSystem(fs) |
35 | { | 35 | { |
36 | #ifdef _DEBUG | 36 | #ifdef _DEBUG |
37 | setDebugName("CArchiveLoaderPAK"); | 37 | setDebugName("CArchiveLoaderPAK"); |
38 | #endif | 38 | #endif |
39 | } | 39 | } |
40 | 40 | ||
41 | 41 | ||
42 | //! returns true if the file maybe is able to be loaded by this class | 42 | //! returns true if the file maybe is able to be loaded by this class |
43 | bool CArchiveLoaderPAK::isALoadableFileFormat(const io::path& filename) const | 43 | bool CArchiveLoaderPAK::isALoadableFileFormat(const io::path& filename) const |
44 | { | 44 | { |
45 | return core::hasFileExtension(filename, "pak"); | 45 | return core::hasFileExtension(filename, "pak"); |
46 | } | 46 | } |
47 | 47 | ||
48 | //! Check to see if the loader can create archives of this type. | 48 | //! Check to see if the loader can create archives of this type. |
49 | bool CArchiveLoaderPAK::isALoadableFileFormat(E_FILE_ARCHIVE_TYPE fileType) const | 49 | bool CArchiveLoaderPAK::isALoadableFileFormat(E_FILE_ARCHIVE_TYPE fileType) const |
50 | { | 50 | { |
51 | return fileType == EFAT_PAK; | 51 | return fileType == EFAT_PAK; |
52 | } | 52 | } |
53 | 53 | ||
54 | //! Creates an archive from the filename | 54 | //! Creates an archive from the filename |
55 | /** \param file File handle to check. | 55 | /** \param file File handle to check. |
56 | \return Pointer to newly created archive, or 0 upon error. */ | 56 | \return Pointer to newly created archive, or 0 upon error. */ |
57 | IFileArchive* CArchiveLoaderPAK::createArchive(const io::path& filename, bool ignoreCase, bool ignorePaths) const | 57 | IFileArchive* CArchiveLoaderPAK::createArchive(const io::path& filename, bool ignoreCase, bool ignorePaths) const |
58 | { | 58 | { |
59 | IFileArchive *archive = 0; | 59 | IFileArchive *archive = 0; |
60 | io::IReadFile* file = FileSystem->createAndOpenFile(filename); | 60 | io::IReadFile* file = FileSystem->createAndOpenFile(filename); |
61 | 61 | ||
62 | if (file) | 62 | if (file) |
63 | { | 63 | { |
64 | archive = createArchive(file, ignoreCase, ignorePaths); | 64 | archive = createArchive(file, ignoreCase, ignorePaths); |
65 | file->drop (); | 65 | file->drop (); |
66 | } | 66 | } |
67 | 67 | ||
68 | return archive; | 68 | return archive; |
69 | } | 69 | } |
70 | 70 | ||
71 | //! creates/loads an archive from the file. | 71 | //! creates/loads an archive from the file. |
72 | //! \return Pointer to the created archive. Returns 0 if loading failed. | 72 | //! \return Pointer to the created archive. Returns 0 if loading failed. |
73 | IFileArchive* CArchiveLoaderPAK::createArchive(io::IReadFile* file, bool ignoreCase, bool ignorePaths) const | 73 | IFileArchive* CArchiveLoaderPAK::createArchive(io::IReadFile* file, bool ignoreCase, bool ignorePaths) const |
74 | { | 74 | { |
75 | IFileArchive *archive = 0; | 75 | IFileArchive *archive = 0; |
76 | if ( file ) | 76 | if ( file ) |
77 | { | 77 | { |
78 | file->seek ( 0 ); | 78 | file->seek ( 0 ); |
79 | archive = new CPakReader(file, ignoreCase, ignorePaths); | 79 | archive = new CPakReader(file, ignoreCase, ignorePaths); |
80 | } | 80 | } |
81 | return archive; | 81 | return archive; |
82 | } | 82 | } |
83 | 83 | ||
84 | 84 | ||
85 | //! Check if the file might be loaded by this class | 85 | //! Check if the file might be loaded by this class |
86 | /** Check might look into the file. | 86 | /** Check might look into the file. |
87 | \param file File handle to check. | 87 | \param file File handle to check. |
88 | \return True if file seems to be loadable. */ | 88 | \return True if file seems to be loadable. */ |
89 | bool CArchiveLoaderPAK::isALoadableFileFormat(io::IReadFile* file) const | 89 | bool CArchiveLoaderPAK::isALoadableFileFormat(io::IReadFile* file) const |
90 | { | 90 | { |
91 | SPAKFileHeader header; | 91 | SPAKFileHeader header; |
92 | 92 | ||
93 | file->read(&header, sizeof(header)); | 93 | file->read(&header, sizeof(header)); |
94 | 94 | ||
95 | return isHeaderValid(header); | 95 | return isHeaderValid(header); |
96 | } | 96 | } |
97 | 97 | ||
98 | 98 | ||
99 | /*! | 99 | /*! |
100 | PAK Reader | 100 | PAK Reader |
101 | */ | 101 | */ |
102 | CPakReader::CPakReader(IReadFile* file, bool ignoreCase, bool ignorePaths) | 102 | CPakReader::CPakReader(IReadFile* file, bool ignoreCase, bool ignorePaths) |
103 | : CFileList((file ? file->getFileName() : io::path("")), ignoreCase, ignorePaths), File(file) | 103 | : CFileList((file ? file->getFileName() : io::path("")), ignoreCase, ignorePaths), File(file) |
104 | { | 104 | { |
105 | #ifdef _DEBUG | 105 | #ifdef _DEBUG |
106 | setDebugName("CPakReader"); | 106 | setDebugName("CPakReader"); |
107 | #endif | 107 | #endif |
108 | 108 | ||
109 | if (File) | 109 | if (File) |
110 | { | 110 | { |
111 | File->grab(); | 111 | File->grab(); |
112 | scanLocalHeader(); | 112 | scanLocalHeader(); |
113 | sort(); | 113 | sort(); |
114 | } | 114 | } |
115 | } | 115 | } |
116 | 116 | ||
117 | 117 | ||
118 | CPakReader::~CPakReader() | 118 | CPakReader::~CPakReader() |
119 | { | 119 | { |
120 | if (File) | 120 | if (File) |
121 | File->drop(); | 121 | File->drop(); |
122 | } | 122 | } |
123 | 123 | ||
124 | 124 | ||
125 | const IFileList* CPakReader::getFileList() const | 125 | const IFileList* CPakReader::getFileList() const |
126 | { | 126 | { |
127 | return this; | 127 | return this; |
128 | } | 128 | } |
129 | 129 | ||
130 | bool CPakReader::scanLocalHeader() | 130 | bool CPakReader::scanLocalHeader() |
131 | { | 131 | { |
132 | SPAKFileHeader header; | 132 | SPAKFileHeader header; |
133 | 133 | ||
134 | // Read and validate the header | 134 | // Read and validate the header |
135 | File->read(&header, sizeof(header)); | 135 | File->read(&header, sizeof(header)); |
136 | if (!isHeaderValid(header)) | 136 | if (!isHeaderValid(header)) |
137 | return false; | 137 | return false; |
138 | 138 | ||
139 | // Seek to the table of contents | 139 | // Seek to the table of contents |
140 | #ifdef __BIG_ENDIAN__ | 140 | #ifdef __BIG_ENDIAN__ |
141 | header.offset = os::Byteswap::byteswap(header.offset); | 141 | header.offset = os::Byteswap::byteswap(header.offset); |
142 | header.length = os::Byteswap::byteswap(header.length); | 142 | header.length = os::Byteswap::byteswap(header.length); |
143 | #endif | 143 | #endif |
144 | File->seek(header.offset); | 144 | File->seek(header.offset); |
145 | 145 | ||
146 | const int numberOfFiles = header.length / sizeof(SPAKFileEntry); | 146 | const int numberOfFiles = header.length / sizeof(SPAKFileEntry); |
147 | 147 | ||
148 | // Loop through each entry in the table of contents | 148 | // Loop through each entry in the table of contents |
149 | for(int i = 0; i < numberOfFiles; i++) | 149 | for(int i = 0; i < numberOfFiles; i++) |
150 | { | 150 | { |
151 | // read an entry | 151 | // read an entry |
152 | SPAKFileEntry entry; | 152 | SPAKFileEntry entry; |
153 | File->read(&entry, sizeof(entry)); | 153 | File->read(&entry, sizeof(entry)); |
154 | 154 | ||
155 | #ifdef _DEBUG | 155 | #ifdef _DEBUG |
156 | os::Printer::log(entry.name); | 156 | os::Printer::log(entry.name); |
157 | #endif | 157 | #endif |
158 | 158 | ||
159 | #ifdef __BIG_ENDIAN__ | 159 | #ifdef __BIG_ENDIAN__ |
160 | entry.offset = os::Byteswap::byteswap(entry.offset); | 160 | entry.offset = os::Byteswap::byteswap(entry.offset); |
161 | entry.length = os::Byteswap::byteswap(entry.length); | 161 | entry.length = os::Byteswap::byteswap(entry.length); |
162 | #endif | 162 | #endif |
163 | 163 | ||
164 | addItem(io::path(entry.name), entry.offset, entry.length, false ); | 164 | addItem(io::path(entry.name), entry.offset, entry.length, false ); |
165 | } | 165 | } |
166 | return true; | 166 | return true; |
167 | } | 167 | } |
168 | 168 | ||
169 | 169 | ||
170 | //! opens a file by file name | 170 | //! opens a file by file name |
171 | IReadFile* CPakReader::createAndOpenFile(const io::path& filename) | 171 | IReadFile* CPakReader::createAndOpenFile(const io::path& filename) |
172 | { | 172 | { |
173 | s32 index = findFile(filename, false); | 173 | s32 index = findFile(filename, false); |
174 | 174 | ||
175 | if (index != -1) | 175 | if (index != -1) |
176 | return createAndOpenFile(index); | 176 | return createAndOpenFile(index); |
177 | 177 | ||
178 | return 0; | 178 | return 0; |
179 | } | 179 | } |
180 | 180 | ||
181 | 181 | ||
182 | //! opens a file by index | 182 | //! opens a file by index |
183 | IReadFile* CPakReader::createAndOpenFile(u32 index) | 183 | IReadFile* CPakReader::createAndOpenFile(u32 index) |
184 | { | 184 | { |
185 | if (index >= Files.size() ) | 185 | if (index >= Files.size() ) |
186 | return 0; | 186 | return 0; |
187 | 187 | ||
188 | const SFileListEntry &entry = Files[index]; | 188 | const SFileListEntry &entry = Files[index]; |
189 | return createLimitReadFile( entry.FullName, File, entry.Offset, entry.Size ); | 189 | return createLimitReadFile( entry.FullName, File, entry.Offset, entry.Size ); |
190 | } | 190 | } |
191 | 191 | ||
192 | } // end namespace io | 192 | } // end namespace io |
193 | } // end namespace irr | 193 | } // end namespace irr |
194 | 194 | ||
195 | #endif // __IRR_COMPILE_WITH_PAK_ARCHIVE_LOADER_ | 195 | #endif // __IRR_COMPILE_WITH_PAK_ARCHIVE_LOADER_ |
196 | 196 | ||