BAG File Extraction

Mentalist

School Cricketer
Joined
Aug 15, 2005
Location
Birmingham, UK
Online Cricket Games Owned
Hi guys i know this is possible with BAGGI Studio, but i can't get hold of Sam2002 to answer this question i have. Hopefully someone else knows the answer :hpraise.

When you read the file list out of the bag file along with the offsets they arn't the exact offsets of where the file starts. How do work this out? Because it must be possible, obviously because BAGGI Studio does it, and so does the BAG Extractor. I need to know this so i can make a load of updates to my team editor.

Please help guys ;)
 
yea i may do tht barmyarmy, hopefully he'll see this. If he doesn't then i'll PM him. cheers for the reply though mate
 
Since you are only going to edit TeamAndPlayerInfo.bag, I think you should store the teams offsets in some array rather than reading it everytime from the header.
If you look at the TeamAndPlayerInfo.bag file you will notice that the first team starts at 6144 and each team is of 3328 bytes long (actually 3296 bytes and remaining 32 are blank spaces).
 
Cheers for the reply prakash, but im not just going to be reading stuff from the TeamAndPlayerInfo.bag. I would like to know how you work the padding out!
 
The first 4 bytes of the bag file will be zero the next 4 bytes(long data type) tells you how many files are packed inside the bag file. Use a long vairable to read it. For TeamAndPlayerInfo.bag its 80, so there will be 80 files in that bag file.

Now next 4 bytes(long) tells you number of characters of the first file name, so for TeamAndPlayerInfo.bag the first file has 22 characters and the second file has 21 characters and so on. Since there are 80 files in that bag file so this list will go on till 320(80*4) bytes.

After the above list the file size of each file are stored, again its of long data type. So the next 320(80*4) bytes will hold the filesizes.
After the filesize list the actual file name are stored in the bag, its of 64 bytes in length. Use a constant length string variable to read it
Code:
dim strFileName as string * 64
Now the next 320(80*4) bytes contains the starting offset of every file. It can be tricky, as it sometimes start at different locations(not sure why though). And also its complicated to explain, so the best thing would be to post my code here.
PHP:
Open strFileName For Binary As #1
Dim lngOffset&, lngTotalFiles&, lngFileNameLength&, i&, lngFileNameOffset&, lngFileOffset&, lngFileSize&, lngTemp&, lngFirstOffset&, strName As String * 64
Get #1, 5, lngTotalFiles
lngOffset = (lngTotalFiles * 8) + (lngTotalFiles * 64) + 8
Get #1, lngOffset + 1, lngTemp
lngFirstOffset = lngTemp
lngTemp = lngOffset + lngTemp + (lngTotalFiles * 4)
If lngTemp Mod 128 <> 0 Then
    lngOffset = Int(lngTemp / 128) + 1
    lngOffset = lngOffset * 128
Else
    lngOffset = lngTemp
End If
For i = 1 To lngTotalFiles
    Get #1, (i * 4) + 5, lngFileNameLength
    Get #1, (i + lngTotalFiles) * 4 + 5, lngFileSize
    lngFileNameOffset = ((lngTotalFiles * 8) + 9 + ((i - 1) * 64))
    Get #1, lngFileNameOffset, strName
    lngTemp = ((lngTotalFiles - i + 1) * 64) + lngFileNameOffset + ((i - 1) * 4)
    Get #1, lngTemp, lngFileOffset
    lstFileList.AddItem Left$(strName, lngFileNameLength)
    lstFileOffset.AddItem lngFileOffset + lngOffset - lngFirstOffset
    lstFileSize.AddItem lngFileSize
Next i
Close #1
As you can see I am using 3 list boxes (lstFileList, lstFileOffset & lstFileSize) to store the file informations. I hope I haven't confused you too much ;)
Note: The above code will only work on un compressed bag files.
 
That is ace mate, i knew you would come up trumps. That thing has been getting to me for ages!

Here's my code, if you would ever like to use it for anything anybody:

Declarations:
Code:
    Dim lBAGType As Long
    Dim lBAGNumFiles As Long
    Dim lBAGFileNameLength() As Long
    Dim lBAGFileLength() As Long
    Dim sBAGFileName() As String * 64
    Dim lBAGFileOffset() As Long

Main Code:
Code:
    Dim nFileNum, iPadding, i As Integer
    
    nFileNum = FreeFile
    
    Open sFileName For Binary Access _
       Read Lock Read Write As #nFileNum

    Get #nFileNum, , lBAGType
    Get #nFileNum, , lBAGNumFiles
            
    ReDim lBAGFileNameLength(1 To lBAGNumFiles)
    ReDim lBAGFileLength(1 To lBAGNumFiles)
    ReDim sBAGFileName(1 To lBAGNumFiles)
    ReDim lBAGFileOffset(1 To lBAGNumFiles)

    Get #nFileNum, , lBAGFileNameLength
    Get #nFileNum, , lBAGFileLength
    Get #nFileNum, , sBAGFileName
    Get #nFileNum, , lBAGFileOffset
            
    iPadding = (Len(lBAGFileNameLength(1)) + Len(lBAGFileLength(1)) + Len(sBAGFileName(1))) * lBAGNumFiles + 8 + lBAGFileOffset(1) + (lBAGNumFiles * 4)
            
    If iPadding Mod 128 <> 0 Then
            iPadding = (Int(iPadding / 128) + 1) * 128
    End If
            
    iPadding = iPadding - lBAGFileOffset(1)
            
    For i = 1 To lBAGNumFiles
            lBAGFileOffset(i) = lBAGFileOffset(i) + iPadding
    Next

    Close #nFileNum
 
Last edited:
Good stuff mate :)
Just to let you know that if you use
PHP:
Dim nFileNum, iPadding, i As Integer
then the nFileNum as iPadding will be declared as a varient data type. Because this is VB not VB.net ;). So I would rather use
PHP:
Dim nFileNum%, iPadding%, i%
or
Dim nFileNum as Integer, iPadding as Integer, i As Integer
 
haha, cheers mate. I got into doin that from C and C++. Don't spose you have any idea about the encrypted files do you. I'm trying to make a module that we can all use to extract files from the bag in vb. Once it's finished i shall post it.
 
Last edited:

Users who are viewing this thread

Top