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




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