Miren acà les dejo el mod:
- '*****************************************************************
- 'modCompression.bas - v1.0.0
- '
- 'All methods to handle resource files.
- '
- '*****************************************************************
- 'Respective portions copyrighted by contributors listed below.
- '
- 'This library is free software; you can redistribute it and/or
- 'modify it under the terms of the GNU Lesser General Public
- 'License as published by the Free Software Foundation version 2.1 of
- 'the License
- '
- 'This library is distributed in the hope that it will be useful,
- 'but WITHOUT ANY WARRANTY; without even the implied warranty of
- 'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- 'Lesser General Public License for more details.
- '
- 'You should have received a copy of the GNU Lesser General Public
- 'License along with this library; if not, write to the Free Software
- 'Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- '*****************************************************************
- '*****************************************************************
- 'Contributors History
- ' When releasing modifications to this source file please add your
- ' date of release, name, email, and any info to the top of this list.
- ' Follow this template:
- ' XX/XX/200X - Your Name Here (Your Email Here)
- ' - Your Description Here
- ' Sub Release Contributors:
- ' XX/XX/2003 - Sub Contributor Name Here (SC Email Here)
- ' - SC Description Here
- '*****************************************************************
- '
- 'Juan Martín Sotuyo Dodero (juansotuyo@hotmail.com) - 10/13/2004
- ' - First Release
- '*****************************************************************
- Option Explicit
- 'This structure will describe our binary file's
- 'size and number of contained files
- Public Type FILEHEADER
- lngFileSize As Long 'How big is this file? (Used to check integrity)
- intNumFiles As Integer 'How many files are inside?
- End Type
- 'This structure will describe each file contained
- 'in our binary file
- Public Type INFOHEADER
- lngFileStart As Long 'Where does the chunk start?
- lngFileSize As Long 'How big is this chunk of stored data?
- strFileName As String * 32 'What's the name of the file this data came from?
- lngFileSizeUncompressed As Long 'How big is the file compressed
- End Type
- Public Enum resource_file_type
- Graphics
- Midi
- MP3
- Wav
- Scripts
- Patch
- Interface
- Maps
- End Enum
- Private Const GRAPHIC_PATH As String = "\Graficos\"
- Private Const MIDI_PATH As String = "\Midi\"
- Private Const MP3_PATH As String = "\Mp3\"
- Private Const WAV_PATH As String = "\Wav\"
- Private Const MAP_PATH As String = "\Mapas\"
- Private Const INTERFACE_PATH As String = "\Interface\"
- Private Const SCRIPT_PATH As String = "\Init\"
- Private Const PATCH_PATH As String = "\Patches\"
- Private Const OUTPUT_PATH As String = "\Output\"
- Private Declare Function Compress Lib "zlib.dll" Alias "compress" (dest As Any, destLen As Any, src As Any, ByVal srcLen As Long) As Long
- Private Declare Function UnCompress Lib "zlib.dll" Alias "uncompress" (dest As Any, destLen As Any, src As Any, ByVal srcLen As Long) As Long
- 'To get free bytes in drive
- Private Declare Function GetDiskFreeSpace Lib "kernel32" Alias "GetDiskFreeSpaceExA" (ByVal lpRootPathName As String, FreeBytesToCaller As Currency, BytesTotal As Currency, FreeBytesTotal As Currency) As Long
- Public Sub Compress_Data(ByRef data() As Byte)
- '*****************************************************************
- 'Author: Juan Martín Dotuyo Dodero
- 'Last Modify Date: 10/13/2004
- 'Compresses binary data avoiding data loses
- '*****************************************************************
- Dim Dimensions As Long
- Dim DimBuffer As Long
- Dim BufTemp() As Byte
- Dim BufTemp2() As Byte
- Dim loopc As Long
- 'Get size of the uncompressed data
- Dimensions = UBound(data)
- 'Prepare a buffer 1.06 times as big as the original size
- DimBuffer = Dimensions * 1.06
- ReDim BufTemp(DimBuffer)
- 'Compress data using zlib
- Compress BufTemp(0), DimBuffer, data(0), Dimensions
- 'Deallocate memory used by uncompressed data
- Erase data
- 'Get rid of unused bytes in the compressed data buffer
- ReDim Preserve BufTemp(DimBuffer - 1)
- 'Copy the compressed data buffer to the original data array so it will return to caller
- data = BufTemp
- 'Deallocate memory used by the temp buffer
- Erase BufTemp
- 'Encrypt the first byte of the compressed data for extra security
- data(0) = data(0) Xor 189
- End Sub
- Public Sub Decompress_Data(ByRef data() As Byte, ByVal OrigSize As Long)
- '*****************************************************************
- 'Author: Juan Martín Dotuyo Dodero
- 'Last Modify Date: 10/13/2004
- 'Decompresses binary data
- '*****************************************************************
- Dim BufTemp() As Byte
- ReDim BufTemp(OrigSize - 1)
- 'Des-encrypt the first byte of the compressed data
- data(0) = data(0) Xor 189
- UnCompress BufTemp(0), OrigSize, data(0), UBound(data) + 1
- ReDim data(OrigSize - 1)
- data = BufTemp
- Erase BufTemp
- End Sub
- Public Sub Encrypt_File_Header(ByRef FileHead As FILEHEADER)
- '*****************************************************************
- 'Author: Juan Martín Dotuyo Dodero
- 'Last Modify Date: 10/13/2004
- 'Encrypts normal data or turns encrypted data back to normal
- '*****************************************************************
- 'Each different variable is encrypted with a different key for extra security
- With FileHead
- .intNumFiles = .intNumFiles Xor 4725
- .lngFileSize = .lngFileSize Xor 2435437
- End With
- End Sub
- Public Sub Encrypt_Info_Header(ByRef InfoHead As INFOHEADER)
- '*****************************************************************
- 'Author: Juan Martín Dotuyo Dodero
- 'Last Modify Date: 10/13/2004
- 'Encrypts normal data or turns encrypted data back to normal
- '*****************************************************************
- Dim EncryptedFileName As String
- Dim loopc As Long
- For loopc = 1 To Len(InfoHead.strFileName)
- If loopc Mod 2 = 0 Then
- EncryptedFileName = EncryptedFileName & Chr$(Asc(mid$(InfoHead.strFileName, loopc, 1)) Xor 161)
- Else
- EncryptedFileName = EncryptedFileName & Chr$(Asc(mid$(InfoHead.strFileName, loopc, 1)) Xor 47)
- End If
- Next loopc
- 'Each different variable is encrypted with a different key for extra security
- With InfoHead
- .lngFileSize = .lngFileSize Xor 689947
- .lngFileSizeUncompressed = .lngFileSizeUncompressed Xor 445275
- .lngFileStart = .lngFileStart Xor 87540
- .strFileName = EncryptedFileName
- End With
- End Sub
- Public Function Extract_All_Files(ByVal file_type As resource_file_type, ByVal resource_path As String, Optional ByVal UseOutputFolder As Boolean = False) As Boolean
- '*****************************************************************
- 'Author: Juan Martín Dotuyo Dodero
- 'Last Modify Date: 10/13/2004
- 'Extracts all files from a resource file
- '*****************************************************************
- Dim loopc As Long
- Dim SourceFilePath As String
- Dim OutputFilePath As String
- Dim SourceFile As Integer
- Dim SourceData() As Byte
- Dim FileHead As FILEHEADER
- Dim InfoHead() As INFOHEADER
- Dim handle As Integer
- Dim tempbyte As Byte
- 'Set up the error handler
- On Error Resume Next
- On Local Error GoTo ErrHandler
- Select Case file_type
- Case Graphics
- If UseOutputFolder Then
- SourceFilePath = resource_path & OUTPUT_PATH & "Graficos.iao"
- Else
- SourceFilePath = resource_path & "\Graficos.iao"
- End If
- OutputFilePath = resource_path & GRAPHIC_PATH
- Case Midi
- If UseOutputFolder Then
- SourceFilePath = resource_path & OUTPUT_PATH & "Midi.iao"
- Else
- SourceFilePath = resource_path & "\MIDI.iao"
- End If
- OutputFilePath = resource_path & MIDI_PATH
- Case MP3
- If UseOutputFolder Then
- SourceFilePath = resource_path & OUTPUT_PATH & "MP3.iao"
- Else
- SourceFilePath = resource_path & "\MP3.iao"
- End If
- OutputFilePath = resource_path & MP3_PATH
- Case Wav
- If UseOutputFolder Then
- SourceFilePath = resource_path & OUTPUT_PATH & "Sounds.iao"
- Else
- SourceFilePath = resource_path & "\Sounds.iao"
- End If
- OutputFilePath = resource_path & WAV_PATH
- Case Scripts
- If UseOutputFolder Then
- SourceFilePath = resource_path & OUTPUT_PATH & "Init.iao"
- Else
- SourceFilePath = resource_path & "\Init.iao"
- End If
- OutputFilePath = resource_path & SCRIPT_PATH
- Case Interface
- If UseOutputFolder Then
- SourceFilePath = resource_path & OUTPUT_PATH & "Interface.iao"
- Else
- SourceFilePath = resource_path & "\Interface.iao"
- End If
- OutputFilePath = resource_path & INTERFACE_PATH
- Case Maps
- If UseOutputFolder Then
- SourceFilePath = resource_path & OUTPUT_PATH & "Mapas.iao"
- Else
- SourceFilePath = resource_path & "\Mapas.iao"
- End If
- OutputFilePath = resource_path & MAP_PATH
- Case Else
- Exit Function
- End Select
- 'Open the binary file
- SourceFile = FreeFile
- Open SourceFilePath For Binary Access Read Lock Write As SourceFile
- Get SourceFile, 1, tempbyte
- 'Extract the FILEHEADER
- Get SourceFile, , FileHead
- 'Desencrypt FILEHEADER
- Encrypt_File_Header FileHead
- 'Check the file for validity
- If LOF(SourceFile) <> FileHead.lngFileSize Then
- MsgBox "Resource file " & SourceFilePath & " seems to be corrupted.", , "Error"
- Close SourceFile
- Erase InfoHead
- Exit Function
- End If
- 'Size the InfoHead array
- ReDim InfoHead(FileHead.intNumFiles - 1)
- Get SourceFile, , tempbyte
- Get SourceFile, , tempbyte
- 'Extract the INFOHEADER
- Get SourceFile, , InfoHead
- Get SourceFile, , tempbyte
- Get SourceFile, , tempbyte
- Get SourceFile, , tempbyte
- Get SourceFile, , tempbyte
- Get SourceFile, , tempbyte
- Get SourceFile, , tempbyte
- 'Extract all of the files from the binary file
- For loopc = 0 To UBound(InfoHead)
- 'Desencrypt each INFOHEADER before accessing the data
- Encrypt_Info_Header InfoHead(loopc)
- 'Check if there is enough memory
- If InfoHead(loopc).lngFileSizeUncompressed > General_Drive_Get_Free_Bytes(Left(App.Path, 3)) Then
- MsgBox "There is not enough free memory to continue extracting files."
- Exit Function
- End If
- 'Resize the byte data array
- ReDim SourceData(InfoHead(loopc).lngFileSize - 1)
- 'Get the data
- Get SourceFile, InfoHead(loopc).lngFileStart, SourceData
- 'Decompress all data
- 'Decompress_Data SourceData, InfoHead(loopc).lngFileSizeUncompressed
- 'Get a free handler
- handle = FreeFile
- 'Create a new file and put in the data
- Open OutputFilePath & InfoHead(loopc).strFileName For Binary As handle
- Put handle, , SourceData
- Close handle
- Erase SourceData
- DoEvents
- Next loopc
- 'Close the binary file
- Close SourceFile
- Erase InfoHead
- Extract_All_Files = True
- Exit Function
- ErrHandler:
- Close SourceFile
- Erase SourceData
- Erase InfoHead
- 'Display an error message if it didn't work
- MsgBox "Unable to decode binary file. Reason: " & Err.Number & " : " & Err.Description, vbOKOnly, "Error"
- End Function
- Public Function Extract_Patch(ByVal resource_path As String, ByVal file_name As String) As Boolean
- '*****************************************************************
- 'Author: Juan Martín Dotuyo Dodero
- 'Last Modify Date: 10/13/2004
- 'Comrpesses all files to a resource file
- '*****************************************************************
- Dim loopc As Long
- Dim LoopC2 As Long
- Dim LoopC3 As Long
- Dim OutputFile As Integer
- Dim UpdatedFile As Integer
- Dim SourceFilePath As String
- Dim SourceFile As Integer
- Dim SourceData() As Byte
- Dim ResFileHead As FILEHEADER
- Dim ResInfoHead() As INFOHEADER
- Dim UpdatedInfoHead As INFOHEADER
- Dim FileHead As FILEHEADER
- Dim InfoHead() As INFOHEADER
- Dim RequiredSpace As Currency
- Dim FileExtension As String
- Dim DataOffset As Long
- Dim OutputFilePath As String
- 'Done flags
- Dim bmp_done As Boolean
- Dim wav_done As Boolean
- Dim mid_done As Boolean
- Dim mp3_done As Boolean
- Dim exe_done As Boolean
- Dim gui_done As Boolean
- Dim ind_done As Boolean
- Dim dat_done As Boolean
- Dim ini_done As Boolean
- '************************************************************************************************
- 'This is similar to Extract, but has some small differences to make sure what is being updated
- '************************************************************************************************
- 'Set up the error handler
- On Local Error GoTo ErrHandler
- 'Open the binary file
- SourceFile = FreeFile
- SourceFilePath = file_name
- Open SourceFilePath For Binary Access Read Lock Write As SourceFile
- 'Extract the FILEHEADER
- Get SourceFile, 1, FileHead
- 'Desencrypt File Header
- Encrypt_File_Header FileHead
- 'Check the file for validity
- 'If LOF(SourceFile) <> FileHead.lngFileSize Then
- ' MsgBox "Resource file " & SourceFilePath & " seems to be corrupted.", , "Error"
- ' Exit Function
- 'End If
- 'Size the InfoHead array
- ReDim InfoHead(FileHead.intNumFiles - 1)
- 'Extract the INFOHEADER
- Get SourceFile, , InfoHead
- 'Check if there is enough hard drive space to extract all files
- For loopc = 0 To UBound(InfoHead)
- 'Desencrypt each Info Header before accessing the data
- Encrypt_Info_Header InfoHead(loopc)
- RequiredSpace = RequiredSpace + InfoHead(loopc).lngFileSizeUncompressed
- Next loopc
- If RequiredSpace >= General_Drive_Get_Free_Bytes(Left(App.Path, 3)) Then
- Erase InfoHead
- MsgBox "¡No hay espacio suficiente para extraer el archivo!", , "Error"
- Exit Function
- End If
- 'Extract all of the files from the binary file
- For loopc = 0 To UBound(InfoHead())
- 'Check the extension of the file
- Select Case LCase(Right(Trim(InfoHead(loopc).strFileName), 3))
- Case Is = "bmp"
- If bmp_done Then GoTo EndMainLoop
- FileExtension = "bmp"
- OutputFilePath = resource_path & "\Graficos.iao"
- bmp_done = True
- Case Is = "mid"
- If mid_done Then GoTo EndMainLoop
- FileExtension = "mid"
- OutputFilePath = resource_path & "\MIDI.iao"
- mid_done = True
- Case Is = "mp3"
- If mp3_done Then GoTo EndMainLoop
- FileExtension = "mp3"
- OutputFilePath = resource_path & "\MP3.iao"
- mp3_done = True
- Case Is = "wav"
- If wav_done Then GoTo EndMainLoop
- FileExtension = "wav"
- OutputFilePath = resource_path & "\Sounds.iao"
- wav_done = True
- Case Is = "jpg"
- If gui_done Then GoTo EndMainLoop
- FileExtension = "jpg"
- OutputFilePath = resource_path & "\Interface.iao"
- gui_done = True
- Case Is = "ind"
- If ind_done Then GoTo EndMainLoop
- FileExtension = "ind"
- OutputFilePath = resource_path & "\Init.iao"
- ind_done = True
- Case Is = "dat"
- If dat_done Then GoTo EndMainLoop
- FileExtension = "dat"
- OutputFilePath = resource_path & "\Init.iao"
- dat_done = True
- Case Is = "ini"
- If ini_done Then GoTo EndMainLoop
- FileExtension = "ini"
- OutputFilePath = resource_path & "\Init.iao"
- ini_done = True
- End Select
- OutputFile = FreeFile
- Open OutputFilePath For Binary Access Read Lock Write As OutputFile
- 'Get file header
- Get OutputFile, 1, ResFileHead
- 'Desencrypt file header
- Encrypt_File_Header ResFileHead
- 'Resize the Info Header array
- ReDim ResInfoHead(ResFileHead.intNumFiles - 1)
- 'Load the info header
- Get OutputFile, , ResInfoHead
- 'Desencrypt all Info Headers
- For LoopC2 = 0 To UBound(ResInfoHead())
- Encrypt_Info_Header ResInfoHead(LoopC2)
- Next LoopC2
- 'Check how many of the files are new, and how many are replacements
- For LoopC2 = loopc To UBound(InfoHead())
- If LCase$(Right$(Trim$(InfoHead(LoopC2).strFileName), 3)) = FileExtension Then
- 'Look for same name in the resource file
- For LoopC3 = 0 To UBound(ResInfoHead())
- If ResInfoHead(LoopC3).strFileName = InfoHead(LoopC2).strFileName Then
- Exit For
- End If
- Next LoopC3
- 'Update the File Head
- If LoopC3 > UBound(ResInfoHead()) Then
- 'Update number of files and size
- ResFileHead.intNumFiles = ResFileHead.intNumFiles + 1
- ResFileHead.lngFileSize = ResFileHead.lngFileSize + Len(InfoHead(0)) + InfoHead(LoopC2).lngFileSize
- Else
- 'We substract the size of the old file and add the one of the new one
- ResFileHead.lngFileSize = ResFileHead.lngFileSize - ResInfoHead(LoopC3).lngFileSize + InfoHead(LoopC2).lngFileSize
- End If
- End If
- Next LoopC2
- 'Get the offset of the compressed data
- DataOffset = CLng(ResFileHead.intNumFiles) * Len(ResInfoHead(0)) + Len(FileHead) + 1
- 'Encrypt file Header
- Encrypt_File_Header ResFileHead
- 'Now we start saving the updated file
- UpdatedFile = FreeFile
- Open OutputFilePath & "2" For Binary Access Write Lock Read As UpdatedFile
- 'Store the filehead
- Put UpdatedFile, 1, ResFileHead
- 'Start storing the Info Heads
- LoopC2 = loopc
- For LoopC3 = 0 To UBound(ResInfoHead())
- Do While LoopC2 <= UBound(InfoHead())
- If LCase$(ResInfoHead(LoopC3).strFileName) < LCase$(InfoHead(LoopC2).strFileName) Then Exit Do
- If LCase$(Right$(Trim$(InfoHead(LoopC2).strFileName), 3)) = FileExtension Then
- 'Copy the info head data
- UpdatedInfoHead = InfoHead(LoopC2)
- 'Set the file start pos and update the offset
- UpdatedInfoHead.lngFileStart = DataOffset
- DataOffset = DataOffset + UpdatedInfoHead.lngFileSize
- 'Encrypt the info header and save it
- Encrypt_Info_Header UpdatedInfoHead
- Put UpdatedFile, , UpdatedInfoHead
- DoEvents
- End If
- LoopC2 = LoopC2 + 1
- Loop
- 'If the file was replaced in the patch, we skip it
- If LoopC2 Then
- If LCase$(ResInfoHead(LoopC3).strFileName) <= LCase$(InfoHead(LoopC2 - 1).strFileName) Then GoTo EndLoop
- End If
- 'Copy the info head data
- UpdatedInfoHead = ResInfoHead(LoopC3)
- 'Set the file start pos and update the offset
- UpdatedInfoHead.lngFileStart = DataOffset
- DataOffset = DataOffset + UpdatedInfoHead.lngFileSize
- 'Encrypt the info header and save it
- Encrypt_Info_Header UpdatedInfoHead
- Put UpdatedFile, , UpdatedInfoHead
- EndLoop:
- Next LoopC3
- 'If there was any file in the patch that would go in the bottom of the list we put it now
- For LoopC2 = LoopC2 To UBound(InfoHead())
- If LCase$(Right$(Trim$(InfoHead(LoopC2).strFileName), 3)) = FileExtension Then
- 'Copy the info head data
- UpdatedInfoHead = InfoHead(LoopC2)
- 'Set the file start pos and update the offset
- UpdatedInfoHead.lngFileStart = DataOffset
- DataOffset = DataOffset + UpdatedInfoHead.lngFileSize
- 'Encrypt the info header and save it
- Encrypt_Info_Header UpdatedInfoHead
- Put UpdatedFile, , UpdatedInfoHead
- End If
- Next LoopC2
- 'Now we start adding the compressed data
- LoopC2 = loopc
- For LoopC3 = 0 To UBound(ResInfoHead())
- Do While LoopC2 <= UBound(InfoHead())
- If LCase$(ResInfoHead(LoopC3).strFileName) < LCase$(InfoHead(LoopC2).strFileName) Then Exit Do
- If LCase$(Right$(Trim$(InfoHead(LoopC2).strFileName), 3)) = FileExtension Then
- 'Get the compressed data
- ReDim SourceData(InfoHead(LoopC2).lngFileSize - 1)
- Get SourceFile, InfoHead(LoopC2).lngFileStart, SourceData
- Put UpdatedFile, , SourceData
- End If
- LoopC2 = LoopC2 + 1
- Loop
- 'If the file was replaced in the patch, we skip it
- If LoopC2 Then
- If LCase$(ResInfoHead(LoopC3).strFileName) <= LCase$(InfoHead(LoopC2 - 1).strFileName) Then GoTo EndLoop2
- End If
- 'Get the compressed data
- ReDim SourceData(ResInfoHead(LoopC3).lngFileSize - 1)
- Get OutputFile, ResInfoHead(LoopC3).lngFileStart, SourceData
- Put UpdatedFile, , SourceData
- EndLoop2:
- Next LoopC3
- 'If there was any file in the patch that would go in the bottom of the lsit we put it now
- For LoopC2 = LoopC2 To UBound(InfoHead())
- If LCase$(Right$(Trim$(InfoHead(LoopC2).strFileName), 3)) = FileExtension Then
- 'Get the compressed data
- ReDim SourceData(InfoHead(LoopC2).lngFileSize - 1)
- Get SourceFile, InfoHead(LoopC2).lngFileStart, SourceData
- Put UpdatedFile, , SourceData
- End If
- Next LoopC2
- 'We are done updating the file
- Close UpdatedFile
- 'Close and delete the old resource file
- Close OutputFile
- Kill OutputFilePath
- 'Rename the new one
- Name OutputFilePath & "2" As OutputFilePath
- 'Deallocate the memory used by the data array
- Erase SourceData
- EndMainLoop:
- Next loopc
- 'Close the binary file
- Close SourceFile
- Erase InfoHead
- Erase ResInfoHead
- Extract_Patch = True
- Exit Function
- ErrHandler:
- Erase SourceData
- Erase InfoHead
- End Function
- Public Function Compress_Files(ByVal file_type As resource_file_type, ByVal resource_path As String, ByVal dest_path As String) As Boolean
- '*****************************************************************
- 'Author: Juan Martín Dotuyo Dodero
- 'Last Modify Date: 10/13/2004
- 'Comrpesses all files to a resource file
- '*****************************************************************
- Dim SourceFilePath As String
- Dim SourceFileExtension As String
- Dim OutputFilePath As String
- Dim SourceFile As Long
- Dim OutputFile As Long
- Dim SourceFileName As String
- Dim SourceData() As Byte
- Dim FileHead As FILEHEADER
- Dim InfoHead() As INFOHEADER
- Dim FileNames() As String
- Dim lngFileStart As Long
- Dim loopc As Long
- 'Set up the error handler
- On Local Error GoTo ErrHandler
- Select Case file_type
- Case Graphics
- SourceFilePath = resource_path & GRAPHIC_PATH
- SourceFileExtension = ".bmp"
- OutputFilePath = dest_path & "Graficos.iao"
- Case Midi
- SourceFilePath = resource_path & MIDI_PATH
- SourceFileExtension = ".mid"
- OutputFilePath = dest_path & "MIDI.iao"
- Case MP3
- SourceFilePath = resource_path & MP3_PATH
- SourceFileExtension = ".mp3"
- OutputFilePath = dest_path & "MP3.iao"
- Case Wav
- SourceFilePath = resource_path & WAV_PATH
- SourceFileExtension = ".wav"
- OutputFilePath = dest_path & "Sounds.iao"
- Case Scripts
- SourceFilePath = resource_path & SCRIPT_PATH
- SourceFileExtension = ".*"
- OutputFilePath = dest_path & "Init.iao"
- Case Patch
- SourceFilePath = resource_path & PATCH_PATH
- SourceFileExtension = ".*"
- OutputFilePath = dest_path & "Patch.iao"
- Case Interface
- SourceFilePath = resource_path & INTERFACE_PATH
- SourceFileExtension = ".bmp"
- OutputFilePath = dest_path & "Interface.iao"
- Case Maps
- SourceFilePath = resource_path & MAP_PATH
- SourceFileExtension = ".csm"
- OutputFilePath = dest_path & "Mapas.iao"
- End Select
- 'Get first file in the directoy
- SourceFileName = Dir$(SourceFilePath & "*" & SourceFileExtension, vbNormal)
- SourceFile = FreeFile
- 'Get all other files i nthe directory
- While SourceFileName <> ""
- FileHead.intNumFiles = FileHead.intNumFiles + 1
- ReDim Preserve FileNames(FileHead.intNumFiles - 1)
- FileNames(FileHead.intNumFiles - 1) = LCase(SourceFileName)
- 'Search new file
- SourceFileName = Dir$()
- Wend
- 'If we found none, be can't compress a thing, so we exit
- If FileHead.intNumFiles = 0 Then
- MsgBox "There are no files of extension " & SourceFileExtension & " in " & SourceFilePath & ".", , "Error"
- Exit Function
- End If
- 'Sort file names alphabetically (this will make patching much easier).
- General_Quick_Sort FileNames(), 0, UBound(FileNames)
- 'Resize InfoHead array
- ReDim InfoHead(FileHead.intNumFiles - 1)
- 'Destroy file if it previuosly existed
- If Dir(OutputFilePath, vbNormal) <> "" Then
- Kill OutputFilePath
- End If
- 'Open a new file
- OutputFile = FreeFile
- Open OutputFilePath For Binary Access Read Write As OutputFile
- For loopc = 0 To FileHead.intNumFiles - 1
- 'Find a free file number to use and open the file
- SourceFile = FreeFile
- Open SourceFilePath & FileNames(loopc) For Binary Access Read Lock Write As SourceFile
- 'Store file name
- InfoHead(loopc).strFileName = FileNames(loopc)
- 'Find out how large the file is and resize the data array appropriately
- ReDim SourceData(LOF(SourceFile) - 1)
- 'Store the value so we can decompress it later on
- InfoHead(loopc).lngFileSizeUncompressed = LOF(SourceFile)
- 'Get the data from the file
- Get SourceFile, , SourceData
- 'Compress it
- 'Compress_Data SourceData
- 'Save it to a temp file
- Put OutputFile, , SourceData
- 'Set up the file header
- FileHead.lngFileSize = FileHead.lngFileSize + UBound(SourceData) + 1
- 'Set up the info headers
- InfoHead(loopc).lngFileSize = UBound(SourceData) + 1
- Erase SourceData
- 'Close temp file
- Close SourceFile
- DoEvents
- Next loopc
- 'Finish setting the FileHeader data
- FileHead.lngFileSize = FileHead.lngFileSize + CLng(FileHead.intNumFiles) * Len(InfoHead(0)) + Len(FileHead)
- 'Set InfoHead data
- lngFileStart = Len(FileHead) + CLng(FileHead.intNumFiles) * Len(InfoHead(0)) + 1
- For loopc = 0 To FileHead.intNumFiles - 1
- InfoHead(loopc).lngFileStart = lngFileStart
- lngFileStart = lngFileStart + InfoHead(loopc).lngFileSize
- 'Once an InfoHead index is ready, we encrypt it
- Encrypt_Info_Header InfoHead(loopc)
- Next loopc
- 'Encrypt the FileHeader
- Encrypt_File_Header FileHead
- '************ Write Data
- 'Get all data stored so far
- ReDim SourceData(LOF(OutputFile) - 1)
- Seek OutputFile, 1
- Get OutputFile, , SourceData
- Seek OutputFile, 1
- 'Store the data in the file
- Put OutputFile, , FileHead
- Put OutputFile, , InfoHead
- Put OutputFile, , SourceData
- 'Close the file
- Close OutputFile
- Erase InfoHead
- Erase SourceData
- Exit Function
- ErrHandler:
- Erase SourceData
- Erase InfoHead
- 'Display an error message if it didn't work
- MsgBox "Unable to create binary file. Reason: " & Err.Number & " : " & Err.Description, vbOKOnly, "Error"
- End Function
- Public Function Extract_File(ByVal file_type As resource_file_type, ByVal resource_path As String, ByVal file_name As String, ByVal OutputFilePath As String, Optional ByVal UseOutputFolder As Boolean = False) As Boolean
- '*****************************************************************
- 'Author: Juan Martín Dotuyo Dodero
- 'Last Modify Date: 10/13/2004
- 'Extracts all files from a resource file
- '*****************************************************************
- Dim loopc As Long
- Dim SourceFilePath As String
- Dim SourceData() As Byte
- Dim InfoHead As INFOHEADER
- Dim handle As Integer
- Dim tempbyte As Byte
- 'Set up the error handler
- On Local Error GoTo ErrHandler
- Select Case file_type
- Case Graphics
- If UseOutputFolder Then
- SourceFilePath = resource_path & OUTPUT_PATH & "Graficos.iao"
- Else
- SourceFilePath = resource_path & "\Graficos.iao"
- End If
- Case Midi
- If UseOutputFolder Then
- SourceFilePath = resource_path & OUTPUT_PATH & "MIDI.iao"
- Else
- SourceFilePath = resource_path & "\MIDI.iao"
- End If
- Case MP3
- If UseOutputFolder Then
- SourceFilePath = resource_path & OUTPUT_PATH & "MP3.iao"
- Else
- SourceFilePath = resource_path & "\MP3.iao"
- End If
- Case Wav
- If UseOutputFolder Then
- SourceFilePath = resource_path & OUTPUT_PATH & "Sounds.iao"
- Else
- SourceFilePath = resource_path & "\Sounds.iao"
- End If
- Case Scripts
- If UseOutputFolder Then
- SourceFilePath = resource_path & OUTPUT_PATH & "Init.iao"
- Else
- SourceFilePath = resource_path & "\Init.iao"
- End If
- Case Interface
- If UseOutputFolder Then
- SourceFilePath = resource_path & OUTPUT_PATH & "Interface.iao"
- Else
- SourceFilePath = resource_path & "\Interface.iao"
- End If
- Case Maps
- If UseOutputFolder Then
- SourceFilePath = resource_path & OUTPUT_PATH & "Mapas.iao"
- Else
- SourceFilePath = resource_path & "\Mapas.iao"
- End If
- Case Else
- Exit Function
- End Select
- file_name = LCase(file_name)
- 'Find the Info Head of the desired file
- InfoHead = File_Find(SourceFilePath, file_name)
- If InfoHead.strFileName = "" Or InfoHead.lngFileSize = 0 Then Exit Function
- 'Open the binary file
- handle = FreeFile
- Open SourceFilePath For Binary Access Read Lock Write As handle
- 'Make sure there is enough space in the HD
- If InfoHead.lngFileSizeUncompressed > General_Drive_Get_Free_Bytes(Left$(App.Path, 3)) Then
- Close handle
- MsgBox "There is not enough drive space to extract the compressed file.", , "Error"
- Exit Function
- End If
- 'Extract file from the binary file
- 'Resize the byte data array
- ReDim SourceData(InfoHead.lngFileSize - 1)
- 'Get the data
- Get handle, InfoHead.lngFileStart + 9, SourceData
- 'Decompress all data
- Decompress_Data SourceData, InfoHead.lngFileSizeUncompressed
- 'Close the binary file
- Close handle
- 'Get a free handler
- handle = FreeFile
- Open OutputFilePath & InfoHead.strFileName For Binary Access Write As handle
- Put handle, 1, SourceData
- Close handle
- Erase SourceData
- Extract_File = True
- Exit Function
- ErrHandler:
- Close handle
- Erase SourceData
- 'Display an error message if it didn't work
- 'MsgBox "Unable to decode binary file. Reason: " & Err.number & " : " & Err.Description, vbOKOnly, "Error"
- End Function
- Public Function Extract_BMP_Memory(ByVal file_name As String, SourceData() As Byte) As Boolean
- '*****************************************************************
- 'Author: Juan Martín Dotuyo Dodero
- 'Last Modify Date: 10/13/2004
- 'Extracts all files from a resource file
- '*****************************************************************
- Dim loopc As Long
- Dim InfoHead As INFOHEADER
- Dim handle As Integer
- 'Set up the error handler
- On Local Error GoTo ErrHandler
- 'Find the Info Head of the desired file
- InfoHead = File_Find(App.Path & "\Resources\Graficos.iao", file_name)
- If InfoHead.strFileName = "" Or InfoHead.lngFileSize = 0 Then Exit Function
- 'Open the binary file
- handle = FreeFile
- Open App.Path & "\Resources\Graficos.iao" For Binary Access Read Lock Write As handle
- 'Resize the byte data array
- ReDim SourceData(InfoHead.lngFileSize - 1)
- 'Get the data
- Get handle, InfoHead.lngFileStart + 9, SourceData
- 'Decompress all data
- Decompress_Data SourceData, InfoHead.lngFileSizeUncompressed
- 'Close the binary file
- Close handle
- Extract_BMP_Memory = True
- Exit Function
- ErrHandler:
- Close handle
- Erase SourceData
- End Function
- Private Function File_Find(ByVal resource_file_path As String, ByVal file_name As String) As INFOHEADER
- '**************************************************************
- 'Author: Juan Martín Sotuyo Dodero
- 'Last Modify Date: 5/04/2005
- 'Looks for a compressed file in a resource file. Uses binary search ;)
- '**************************************************************
- On Error GoTo ErrHandler
- Dim max As Long 'Max index
- Dim min As Long 'Min index
- Dim mid As Long 'Middle index
- Dim file_handler As Integer
- Dim file_head As FILEHEADER
- Dim info_head As INFOHEADER
- Dim tempbyte As Byte
- 'Fill file name with spaces for compatibility
- If Len(file_name) < Len(info_head.strFileName) Then _
- file_name = file_name & Space$(Len(info_head.strFileName) - Len(file_name))
- 'Open resource file
- file_handler = FreeFile
- Open resource_file_path For Binary Access Read Lock Write As file_handler
- Get file_handler, 1, tempbyte
- 'Get file head
- Get file_handler, , file_head
- Encrypt_File_Header file_head
- Get file_handler, , tempbyte
- Get file_handler, , tempbyte
- min = 1
- max = file_head.intNumFiles
- Do While min <= max
- mid = (min + max) / 2
- 'Get the info header of the appropiate compressed file
- Get file_handler, CLng(Len(file_head) + CLng(Len(info_head)) * CLng((mid - 1)) + 1) + 3, info_head
- Encrypt_Info_Header info_head
- If file_name < info_head.strFileName Then
- If max = mid Then
- max = max - 1
- Else
- max = mid
- End If
- ElseIf file_name > info_head.strFileName Then
- If min = mid Then
- min = min + 1
- Else
- min = mid
- End If
- Else
- 'Copy info head
- File_Find = info_head
- 'Close file and exit
- Close file_handler
- Exit Function
- End If
- Loop
- ErrHandler:
- 'Close file
- Close file_handler
- File_Find.strFileName = ""
- File_Find.lngFileSize = 0
- End Function
- Public Function General_Drive_Get_Free_Bytes(ByVal DriveName As String) As Currency
- '**************************************************************
- 'Author: Juan Martín Sotuyo Dodero
- 'Last Modify Date: 6/07/2004
- '
- '**************************************************************
- Dim RetVal As Long
- Dim FB As Currency
- Dim BT As Currency
- Dim FBT As Currency
- RetVal = GetDiskFreeSpace(Left(DriveName, 2), FB, BT, FBT)
- General_Drive_Get_Free_Bytes = FB * 10000 'convert result to actual size in bytes
- End Function
- Public Sub General_Quick_Sort(ByRef SortArray As Variant, ByVal first As Long, ByVal last As Long)
- '**************************************************************
- 'Author: juan Martín Sotuyo Dodero
- 'Last Modify Date: 3/03/2005
- 'Good old QuickSort algorithm :)
- '**************************************************************
- Dim Low As Long, High As Long
- Dim temp As Variant
- Dim List_Separator As Variant
- Low = first
- High = last
- List_Separator = SortArray((first + last) / 2)
- Do While (Low <= High)
- Do While SortArray(Low) < List_Separator
- Low = Low + 1
- Loop
- Do While SortArray(High) > List_Separator
- High = High - 1
- Loop
- If Low <= High Then
- temp = SortArray(Low)
- SortArray(Low) = SortArray(High)
- SortArray(High) = temp
- Low = Low + 1
- High = High - 1
- End If
- Loop
- If first < High Then General_Quick_Sort SortArray, first, High
- If Low < last Then General_Quick_Sort SortArray, Low, last
- End Sub
- Public Sub Extract_Files(ByVal file_type As resource_file_type, ByVal resource_path As String, Optional ByVal UseOutputFolder As Boolean = False)
- '*****************************************************************
- 'Author: Juan Martín Dotuyo Dodero
- 'Last Modify Date: 10/13/2004
- 'Extracts all files from a resource file
- '*****************************************************************
- Dim loopc As Long
- Dim SourceFilePath As String
- Dim OutputFilePath As String
- Dim SourceFile As Integer
- Dim SourceData() As Byte
- Dim FileHead As FILEHEADER
- Dim InfoHead() As INFOHEADER
- Dim handle As Integer
- On Local Error GoTo ErrHandler
- Select Case file_type
- Case Graphics
- SourceFilePath = resource_path & "\Graficos.iao"
- OutputFilePath = App.Path & GRAPHIC_PATH
- Case Midi
- SourceFilePath = resource_path & "\Midi.iao"
- OutputFilePath = App.Path & MIDI_PATH 'resource_path & MIDI_PATH
- Case MP3
- SourceFilePath = resource_path & "\MP3.iao"
- OutputFilePath = App.Path & MP3_PATH
- Case Wav
- SourceFilePath = resource_path & "\Sounds.iao"
- OutputFilePath = App.Path & WAV_PATH
- Case Scripts
- SourceFilePath = resource_path & "\Init.iao"
- OutputFilePath = App.Path & SCRIPT_PATH
- Case Interface
- SourceFilePath = resource_path & "\Interface.iao"
- OutputFilePath = App.Path & INTERFACE_PATH
- Case Maps
- SourceFilePath = resource_path & "\Map.iao"
- OutputFilePath = App.Path & MAP_PATH
- Case Else
- Exit Sub
- End Select
- 'Open the binary file
- SourceFile = FreeFile
- Open SourceFilePath For Binary Access Read Lock Write As SourceFile
- 'Extract the FILEHEADER
- Get SourceFile, 1, FileHead
- 'Check the file for validity
- If LOF(SourceFile) <> FileHead.lngFileSize Then ' - Pass1.lngFileSize - 1 Then
- MsgBox "Resource file " & SourceFilePath & " seems to be corrupted.", , "Error"
- Close SourceFile
- Erase InfoHead
- Exit Sub
- End If
- 'Size the InfoHead array
- ReDim InfoHead(FileHead.intNumFiles - 1)
- 'Extract the INFOHEADER
- Get SourceFile, , InfoHead
- 'Extract all of the files from the binary file
- For loopc = 0 To UBound(InfoHead)
- 'Check if there is enough memory
- If InfoHead(loopc).lngFileSizeUncompressed > General_Drive_Get_Free_Bytes(Left(App.Path, 3)) Then
- MsgBox "No tienes suficiente espacio en el disco para seguir descomprimiendo archivos."
- Exit Sub
- End If
- 'Resize the byte data array
- ReDim SourceData(InfoHead(loopc).lngFileSize - 1)
- 'Get the data
- Get SourceFile, InfoHead(loopc).lngFileStart, SourceData
- 'Decompress all data
- Decompress_Data SourceData, InfoHead(loopc).lngFileSizeUncompressed
- 'Get a free handler
- handle = FreeFile
- 'Create a new file and put in the data
- Open OutputFilePath & InfoHead(loopc).strFileName For Binary As handle
- Put handle, , SourceData
- Close handle
- Erase SourceData
- DoEvents
- Next loopc
- 'Close the binary file
- Close SourceFile
- Erase InfoHead
- Exit Sub
- ErrHandler:
- Close SourceFile
- Erase SourceData
- Erase InfoHead
- 'Display an error message if it didn't work
- MsgBox "Unable to decode binary file. Reason: " & Err.Number & " : " & Err.Description, vbOKOnly, "Error"
- End Sub
Y el commanddbut
- Private Sub Command1_Click()
- Call Extract_All_Files(Graphics, App.Path & "\Recursos")
- End Sub
Saludos




90![Newbie [3] Newbie [3]](./images/ranks/Rango2.gif)

![Dragon Ancestral [5] Dragon Ancestral [5]](./images/ranks/Rango34.gif)
![Aprendiz [1] Aprendiz [1]](./images/ranks/Rango6.gif)
![Aprendiz [2] Aprendiz [2]](./images/ranks/Rango7.gif)

![Newbie [4] Newbie [4]](./images/ranks/Rango3.gif)
