Current News Archived News Search News Discussion Forum Old Forum Install Programs More Downloads... Troubleshooting Source Code Format Specs. Misc. Information Non-SF Stuff Links Small banner for links to this site: |
e6d38e6514ac959fd4967afa043bdcecb1b84ac7
1 Attribute VB_Name = "SFmpqapi"\r
2 Option Explicit\r
3 \r
4 ' ShadowFlare MPQ API Library. (c) ShadowFlare Software 2002-2008\r
5 ' License information for this code is in license.txt\r
6 \r
7 ' All functions below are actual functions that are part of this\r
8 ' library and do not need any additional dll files. It does not\r
9 ' even require Storm to be able to decompress or compress files.\r
10 \r
11 ' This library emulates the interface of Lmpqapi and Storm MPQ\r
12 ' functions, so it may be used as a replacement for them in\r
13 ' MPQ extractors/archivers without even needing to recompile\r
14 ' the program that uses Lmpqapi or Storm. It has a few features\r
15 ' not included in Lmpqapi and Storm, such as extra flags for some\r
16 ' functions, setting the locale ID of existing files, and adding\r
17 ' files without having to write them somewhere else first. Also,\r
18 ' MPQ handles used by functions prefixed with "SFile" and "Mpq"\r
19 ' can be used interchangably; all functions use the same type\r
20 ' of MPQ handles. You cannot, however, use handles from this\r
21 ' library with storm or lmpqapi or vice-versa. Doing so will\r
22 ' most likely result in a crash.\r
23 \r
24 ' Revision History:\r
25 ' 06/12/2002 1.07 (ShadowFlare)\r
26 ' - No longer requires Storm.dll to compress or decompress\r
27 ' Warcraft III files\r
28 ' - Added SFileListFiles for getting names and information\r
29 ' about all of the files in an archive\r
30 ' - Fixed a bug with renaming and deleting files\r
31 ' - Fixed a bug with adding wave compressed files with\r
32 ' low compression setting\r
33 ' - Added a check in MpqOpenArchiveForUpdate for proper\r
34 ' dwMaximumFilesInArchive values (should be a number that\r
35 ' is a power of 2). If it is not a proper value, it will\r
36 ' be rounded up to the next higher power of 2\r
37 \r
38 ' 05/09/2002 1.06 (ShadowFlare)\r
39 ' - Compresses files without Storm.dll!\r
40 ' - If Warcraft III is installed, this library will be able to\r
41 ' find Storm.dll on its own. (Storm.dll is needed to\r
42 ' decompress Warcraft III files)\r
43 ' - Fixed a bug where an embedded archive and the file that\r
44 ' contains it would be corrupted if the archive was modified\r
45 ' - Able to open all .w3m maps now\r
46 \r
47 ' 29/06/2002 1.05 (ShadowFlare)\r
48 ' - Supports decompressing files from Warcraft III MPQ archives\r
49 ' if using Storm.dll from Warcraft III\r
50 ' - Added MpqAddFileToArchiveEx and MpqAddFileFromBufferEx for\r
51 ' using extra compression types\r
52 \r
53 ' 29/05/2002 1.04 (ShadowFlare)\r
54 ' - Files can be compressed now!\r
55 ' - Fixed a bug in SFileReadFile when reading data not aligned\r
56 ' to the block size\r
57 ' - Optimized some of SFileReadFile's code. It can read files\r
58 ' faster now\r
59 ' - SFile functions may now be used to access files not in mpq\r
60 ' archives as you can with the real storm functions\r
61 ' - MpqCompactArchive will no longer corrupt files with the\r
62 ' MODCRYPTKEY flag as long as the file is either compressed,\r
63 ' listed in "(listfile)", is "(listfile)", or is located in\r
64 ' the same place in the compacted archive; so it is safe\r
65 ' enough to use it on almost any archive\r
66 ' - Added MpqAddWaveFromBuffer\r
67 ' - Better handling of archives with no files\r
68 ' - Fixed compression with COMPRESS2 flag\r
69 \r
70 ' 15/05/2002 1.03 (ShadowFlare)\r
71 ' - Supports adding files with the compression attribute (does\r
72 ' not actually compress files). Now archives created with\r
73 ' this dll can have files added to them through lmpqapi\r
74 ' without causing staredit to crash\r
75 ' - SFileGetBasePath and SFileSetBasePath work more like their\r
76 ' Storm equivalents now\r
77 ' - Implemented MpqCompactArchive, but it is not finished yet.\r
78 ' In its current state, I would recommend against using it\r
79 ' on archives that contain files with the MODCRYPTKEY flag,\r
80 ' since it will corrupt any files with that flag\r
81 ' - Added SFMpqGetVersionString2 which may be used in Visual\r
82 ' Basic to get the version string\r
83 \r
84 ' 07/05/2002 1.02 (ShadowFlare)\r
85 ' - SFileReadFile no longer passes the lpOverlapped parameter it\r
86 ' receives to ReadFile. This is what was causing the function\r
87 ' to fail when used in Visual Basic\r
88 ' - Added support for more Storm MPQ functions\r
89 ' - GetLastError may now be used to get information about why a\r
90 ' function failed\r
91 \r
92 ' 01/05/2002 1.01 (ShadowFlare)\r
93 ' - Added ordinals for Storm MPQ functions\r
94 ' - Fixed MPQ searching functionality of SFileOpenFileEx\r
95 ' - Added a check for whether a valid handle is given when\r
96 ' SFileCloseArchive is called\r
97 ' - Fixed functionality of SFileSetArchivePriority when multiple\r
98 ' files are open\r
99 ' - File renaming works for all filenames now\r
100 ' - SFileReadFile no longer reallocates the buffer for each block\r
101 ' that is decompressed. This should make SFileReadFile at least\r
102 ' a little faster\r
103 \r
104 ' 30/04/2002 1.00 (ShadowFlare)\r
105 ' - First version.\r
106 ' - Compression not yet supported\r
107 ' - Does not use SetLastError yet, so GetLastError will not return any\r
108 ' errors that have to do with this library\r
109 ' - MpqCompactArchive not implemented\r
110 \r
111 ' Any comments or suggestions are accepted at blakflare@hotmail.com (ShadowFlare)\r
112 \r
113 Type SFMPQVERSION\r
114 Major As Integer\r
115 Minor As Integer\r
116 Revision As Integer\r
117 Subrevision As Integer\r
118 End Type\r
119 \r
120 ' MpqInitialize does nothing. It is only provided for\r
121 ' compatibility with MPQ archivers that use lmpqapi.\r
122 Declare Function MpqInitialize Lib "SFmpq.dll" () As Boolean\r
123 \r
124 Declare Function MpqGetVersionString Lib "SFmpq.dll" () As String\r
125 Declare Function MpqGetVersion Lib "SFmpq.dll" () As Single\r
126 \r
127 Declare Sub SFMpqDestroy Lib "SFmpq.dll" () ' This no longer needs to be called. It is only provided for compatibility with older versions\r
128 \r
129 ' SFMpqGetVersionString2's return value is the required length of the buffer plus\r
130 ' the terminating null, so use SFMpqGetVersionString2(ByVal 0&, 0) to get the length.\r
131 Declare Function SFMpqGetVersionString Lib "SFmpq.dll" () As String\r
132 Declare Function SFMpqGetVersionString2 Lib "SFmpq.dll" (ByVal lpBuffer As String, ByVal dwBufferLength As Long) As Long\r
133 Declare Function SFMpqGetVersion Lib "SFmpq.dll" () As SFMPQVERSION\r
134 \r
135 ' General error codes\r
136 Public Const MPQ_ERROR_MPQ_INVALID As Long = &H85200065\r
137 Public Const MPQ_ERROR_FILE_NOT_FOUND As Long = &H85200066\r
138 Public Const MPQ_ERROR_DISK_FULL As Long = &H85200068 'Physical write file to MPQ failed. Not sure of exact meaning\r
139 Public Const MPQ_ERROR_HASH_TABLE_FULL As Long = &H85200069\r
140 Public Const MPQ_ERROR_ALREADY_EXISTS As Long = &H8520006A\r
141 Public Const MPQ_ERROR_BAD_OPEN_MODE As Long = &H8520006C 'When MOAU_READ_ONLY is used without MOAU_OPEN_EXISTING\r
142 \r
143 Public Const MPQ_ERROR_COMPACT_ERROR As Long = &H85300001\r
144 \r
145 ' MpqOpenArchiveForUpdate flags\r
146 Public Const MOAU_CREATE_NEW As Long = &H0\r
147 Public Const MOAU_CREATE_ALWAYS As Long = &H8 'Was wrongly named MOAU_CREATE_NEW\r
148 Public Const MOAU_OPEN_EXISTING As Long = &H4\r
149 Public Const MOAU_OPEN_ALWAYS As Long = &H20\r
150 Public Const MOAU_READ_ONLY As Long = &H10 'Must be used with MOAU_OPEN_EXISTING\r
151 Public Const MOAU_MAINTAIN_LISTFILE As Long = &H1\r
152 \r
153 ' MpqAddFileToArchive flags\r
154 Public Const MAFA_EXISTS As Long = &H80000000 'Will be added if not present\r
155 Public Const MAFA_UNKNOWN40000000 As Long = &H40000000\r
156 Public Const MAFA_MODCRYPTKEY As Long = &H20000\r
157 Public Const MAFA_ENCRYPT As Long = &H10000\r
158 Public Const MAFA_COMPRESS As Long = &H200\r
159 Public Const MAFA_COMPRESS2 As Long = &H100\r
160 Public Const MAFA_REPLACE_EXISTING As Long = &H1\r
161 \r
162 ' MpqAddFileToArchiveEx compression flags\r
163 Public Const MAFA_COMPRESS_STANDARD As Long = &H8 'Standard PKWare DCL compression\r
164 Public Const MAFA_COMPRESS_DEFLATE As Long = &H2 'ZLib's deflate compression\r
165 Public Const MAFA_COMPRESS_WAVE As Long = &H81 'Standard wave compression\r
166 Public Const MAFA_COMPRESS_WAVE2 As Long = &H41 'Unused wave compression\r
167 \r
168 ' Flags for individual compression types used for wave compression\r
169 Public Const MAFA_COMPRESS_WAVECOMP1 As Long = &H80 'Main compressor for standard wave compression\r
170 Public Const MAFA_COMPRESS_WAVECOMP2 As Long = &H40 'Main compressor for unused wave compression\r
171 Public Const MAFA_COMPRESS_WAVECOMP3 As Long = &H1 'Secondary compressor for wave compression\r
172 \r
173 ' ZLib deflate compression level constants (used with MpqAddFileToArchiveEx and MpqAddFileFromBufferEx)\r
174 Public Const Z_NO_COMPRESSION As Long = 0\r
175 Public Const Z_BEST_SPEED As Long = 1\r
176 Public Const Z_BEST_COMPRESSION As Long = 9\r
177 Public Const Z_DEFAULT_COMPRESSION As Long = (-1)\r
178 \r
179 ' MpqAddWAVToArchive quality flags\r
180 Public Const MAWA_QUALITY_HIGH As Long = 1\r
181 Public Const MAWA_QUALITY_MEDIUM As Long = 0\r
182 Public Const MAWA_QUALITY_LOW As Long = 2\r
183 \r
184 ' SFileGetFileInfo flags\r
185 Public Const SFILE_INFO_BLOCK_SIZE As Long = &H1 'Block size in MPQ\r
186 Public Const SFILE_INFO_HASH_TABLE_SIZE As Long = &H2 'Hash table size in MPQ\r
187 Public Const SFILE_INFO_NUM_FILES As Long = &H3 'Number of files in MPQ\r
188 Public Const SFILE_INFO_TYPE As Long = &H4 'Is Long a file or an MPQ?\r
189 Public Const SFILE_INFO_SIZE As Long = &H5 'Size of MPQ or uncompressed file\r
190 Public Const SFILE_INFO_COMPRESSED_SIZE As Long = &H6 'Size of compressed file\r
191 Public Const SFILE_INFO_FLAGS As Long = &H7 'File flags (compressed, etc.), file attributes if a file not in an archive\r
192 Public Const SFILE_INFO_PARENT As Long = &H8 'Handle of MPQ that file is in\r
193 Public Const SFILE_INFO_POSITION As Long = &H9 'Position of file pointer in files\r
194 Public Const SFILE_INFO_LOCALEID As Long = &HA 'Locale ID of file in MPQ\r
195 Public Const SFILE_INFO_PRIORITY As Long = &HB 'Priority of open MPQ\r
196 Public Const SFILE_INFO_HASH_INDEX As Long = &HC 'Hash index of file in MPQ\r
197 \r
198 ' SFileListFiles flags\r
199 Public Const SFILE_LIST_MEMORY_LIST As Long = &H1 ' Specifies that lpFilelists is a file list from memory, rather than being a list of file lists\r
200 Public Const SFILE_LIST_ONLY_KNOWN As Long = &H2 ' Only list files that the function finds a name for\r
201 Public Const SFILE_LIST_ONLY_UNKNOWN As Long = &H4 ' Only list files that the function does not find a name for\r
202 \r
203 Public Const SFILE_TYPE_MPQ As Long = &H1\r
204 Public Const SFILE_TYPE_FILE As Long = &H2\r
205 \r
206 Public Const INVALID_HANDLE_VALUE As Long = -1\r
207 \r
208 Public Const FILE_BEGIN As Long = 0\r
209 Public Const FILE_CURRENT As Long = 1\r
210 Public Const FILE_END As Long = 2\r
211 \r
212 Public Const SFILE_OPEN_HARD_DISK_FILE As Long = &H0 'Open archive without regard to the drive type it resides on\r
213 Public Const SFILE_OPEN_CD_ROM_FILE As Long = &H1 'Open the archive only if it is on a CD-ROM\r
214 Public Const SFILE_OPEN_ALLOW_WRITE As Long = &H8000 'Open file with write access\r
215 \r
216 Public Const SFILE_SEARCH_CURRENT_ONLY As Long = &H0 'Used with SFileOpenFileEx; only the archive with the handle specified will be searched for the file\r
217 Public Const SFILE_SEARCH_ALL_OPEN As Long = &H1 'SFileOpenFileEx will look through all open archives for the file\r
218 \r
219 Type FILELISTENTRY\r
220 dwFileExists As Long ' Nonzero if this entry is used\r
221 lcLocale As Long ' Locale ID of file\r
222 dwCompressedSize As Long ' Compressed size of file\r
223 dwFullSize As Long ' Uncompressed size of file\r
224 dwFlags As Long ' Flags for file\r
225 szFileName(259) As Byte\r
226 End Type\r
227 \r
228 ' Storm functions implemented by this library\r
229 Declare Function SFileOpenArchive Lib "SFmpq.dll" (ByVal lpFileName As String, ByVal dwPriority As Long, ByVal dwFlags As Long, ByRef hMPQ As Long) As Boolean\r
230 Declare Function SFileCloseArchive Lib "SFmpq.dll" (ByVal hMPQ As Long) As Boolean\r
231 Declare Function SFileGetArchiveName Lib "SFmpq.dll" (ByVal hMPQ As Long, ByVal lpBuffer As String, ByVal dwBufferLength As Long) As Boolean\r
232 Declare Function SFileOpenFile Lib "SFmpq.dll" (ByVal lpFileName As String, ByRef hFile As Long) As Boolean\r
233 Declare Function SFileOpenFileEx Lib "SFmpq.dll" (ByVal hMPQ As Long, ByVal lpFileName As String, ByVal dwSearchScope As Long, ByRef hFile As Long) As Boolean\r
234 Declare Function SFileCloseFile Lib "SFmpq.dll" (ByVal hFile As Long) As Boolean\r
235 Declare Function SFileGetFileSize Lib "SFmpq.dll" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long\r
236 Declare Function SFileGetFileArchive Lib "SFmpq.dll" (ByVal hFile As Long, ByRef hMPQ As Long) As Boolean\r
237 Declare Function SFileGetFileName Lib "SFmpq.dll" (ByVal hMPQ As Long, ByVal lpBuffer As String, ByVal dwBufferLength As Long) As Boolean\r
238 Declare Function SFileSetFilePointer Lib "SFmpq.dll" (ByVal hFile As Long, ByVal lDistanceToMove As Long, lplDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long\r
239 Declare Function SFileReadFile Lib "SFmpq.dll" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Any) As Boolean\r
240 Declare Function SFileSetLocale Lib "SFmpq.dll" (ByVal nNewLocale As Long) As Long\r
241 Declare Function SFileGetBasePath Lib "SFmpq.dll" (ByVal lpBuffer As String, ByVal dwBufferLength As Long) As Boolean\r
242 Declare Function SFileSetBasePath Lib "SFmpq.dll" (ByVal lpNewBasePath As String) As Boolean\r
243 \r
244 ' Extra storm-related functions\r
245 Declare Function SFileGetFileInfo Lib "SFmpq.dll" (ByVal hFile As Long, ByVal dwInfoType As Long) As Long\r
246 Declare Function SFileSetArchivePriority Lib "SFmpq.dll" (ByVal hMPQ As Long, ByVal dwPriority As Long) As Boolean\r
247 Declare Function SFileFindMpqHeader Lib "SFmpq.dll" (ByVal hFile As Long) As Long\r
248 Declare Function SFileListFiles Lib "SFmpq.dll" (ByVal hMPQ As Long, ByVal lpFileLists As String, ByRef lpListBuffer As FILELISTENTRY, ByVal dwFlags As Long) As Boolean\r
249 \r
250 ' Archive editing functions implemented by this library\r
251 Declare Function MpqOpenArchiveForUpdate Lib "SFmpq.dll" (ByVal lpFileName As String, ByVal dwFlags As Long, ByVal dwMaximumFilesInArchive As Long) As Long\r
252 Declare Function MpqCloseUpdatedArchive Lib "SFmpq.dll" (ByVal hMPQ As Long, ByVal dwUnknown2 As Long) As Long\r
253 Declare Function MpqAddFileToArchive Lib "SFmpq.dll" (ByVal hMPQ As Long, ByVal lpSourceFileName As String, ByVal lpDestFileName As String, ByVal dwFlags As Long) As Boolean\r
254 Declare Function MpqAddWaveToArchive Lib "SFmpq.dll" (ByVal hMPQ As Long, ByVal lpSourceFileName As String, ByVal lpDestFileName As String, ByVal dwFlags As Long, ByVal dwQuality As Long) As Boolean\r
255 Declare Function MpqRenameFile Lib "SFmpq.dll" (ByVal hMPQ As Long, ByVal lpcOldFileName As String, ByVal lpcNewFileName As String) As Boolean\r
256 Declare Function MpqDeleteFile Lib "SFmpq.dll" (ByVal hMPQ As Long, ByVal lpFileName As String) As Boolean\r
257 Declare Function MpqCompactArchive Lib "SFmpq.dll" (ByVal hMPQ As Long) As Boolean\r
258 \r
259 ' Extra archive editing functions\r
260 Declare Function MpqAddFileToArchiveEx Lib "SFmpq.dll" (ByVal hMPQ As Long, ByVal lpSourceFileName As String, ByVal lpDestFileName As String, ByVal dwFlags As Long, ByVal dwCompressionType As Long, ByVal dwCompressLevel As Long) As Boolean\r
261 Declare Function MpqAddFileFromBufferEx Lib "SFmpq.dll" (ByVal hMPQ As Long, lpBuffer As Any, ByVal dwLength As Long, ByVal lpFileName As String, ByVal dwFlags As Long, ByVal dwCompressionType As Long, ByVal dwCompressLevel As Long) As Boolean\r
262 Declare Function MpqAddFileFromBuffer Lib "SFmpq.dll" (ByVal hMPQ As Long, lpBuffer As Any, ByVal dwLength As Long, ByVal lpFileName As String, ByVal dwFlags As Long) As Boolean\r
263 Declare Function MpqAddWaveFromBuffer Lib "SFmpq.dll" (ByVal hMPQ As Long, lpBuffer As Any, ByVal dwLength As Long, ByVal lpFileName As String, ByVal dwFlags As Long, ByVal dwQuality As Long) As Boolean\r
264 Declare Function MpqSetFileLocale Lib "SFmpq.dll" (ByVal hMPQ As Long, ByVal lpFileName As String, ByVal nOldLocale As Long, ByVal nNewLocale As Long) As Boolean\r
265 \r
266 ' These functions do nothing. They are only provided for\r
267 ' compatibility with MPQ extractors that use storm.\r
268 Declare Function SFileDestroy Lib "SFmpq.dll" () As Boolean\r
269 Declare Sub StormDestroy Lib "SFmpq.dll" ()\r
270 \r
271 ' Returns 0 if the dll version is equal to the version your program was compiled\r
272 ' with, 1 if the dll is newer, -1 if the dll is older.\r
273 Function SFMpqCompareVersion() As Long\r
274 Dim ExeVersion As SFMPQVERSION, DllVersion As SFMPQVERSION\r
275 With ExeVersion\r
276 .Major = 1\r
277 .Minor = 0\r
278 .Revision = 7\r
279 .Subrevision = 4\r
280 End With\r
281 DllVersion = SFMpqGetVersion()\r
282 If DllVersion.Major > ExeVersion.Major Then\r
283 SFMpqCompareVersion = 1\r
284 Exit Function\r
285 ElseIf DllVersion.Major < ExeVersion.Major Then\r
286 SFMpqCompareVersion = -1\r
287 Exit Function\r
288 End If\r
289 If DllVersion.Minor > ExeVersion.Minor Then\r
290 SFMpqCompareVersion = 1\r
291 Exit Function\r
292 ElseIf DllVersion.Minor < ExeVersion.Minor Then\r
293 SFMpqCompareVersion = -1\r
294 Exit Function\r
295 End If\r
296 If DllVersion.Revision > ExeVersion.Revision Then\r
297 SFMpqCompareVersion = 1\r
298 Exit Function\r
299 ElseIf DllVersion.Revision < ExeVersion.Revision Then\r
300 SFMpqCompareVersion = -1\r
301 Exit Function\r
302 End If\r
303 If DllVersion.Subrevision > ExeVersion.Subrevision Then\r
304 SFMpqCompareVersion = 1\r
305 Exit Function\r
306 ElseIf DllVersion.Subrevision < ExeVersion.Subrevision Then\r
307 SFMpqCompareVersion = -1\r
308 Exit Function\r
309 End If\r
310 SFMpqCompareVersion = 0\r
311 End Function\r
312 \r
|