VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "clsUnitArchive"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
' clsUnitArchive.cls
'   unit archive file (ufo, hpi, ccx).
'   archive can contain any number of units.

Option Explicit
Option Compare Text

Public HPI As clsHPI ' unit archive file. '

Public Units As New Collection ' unit listing. '
Public Weapons As New Collection ' weapon listing. '
Public MenuEntries As New Collection

' Load files for this archive. '
Public Sub LoadFiles()
    Dim Index As Long, i As Long, Flag As Boolean
    Dim Unit As New clsUnit
    Dim MenuEntry As clsMenuEntry
    Dim hTDFFile As Integer
    
    For Index = 1 To HPI.Listing.Count
        If Left(HPI.Listing(Index), 5) = "units" And Right(HPI.Listing(Index), 3) = "fbi" Then
            ' Check to see if a unit of same name has been loaded. '
            Flag = False
            For i = 1 To modUnits.Units.Count
                If modUnits.Units(i).Filename = HPI.Listing(Index) Then
                    Flag = True
                End If
            Next
            ' Load the unit. '
            If Not Flag Then
                Set Unit.HPI = HPI
                Set Unit.Group = Me
                If Unit.Load(HPI.Listing(Index)) Then
                    'Unit.HPIFilename = HPI.Filename
                    Unit.Filename = HPI.Listing(Index)
                    Units.Add Unit
                    modUnits.Units.Add Unit, Unit.Name & CStr(modUnits.Units.Count + 1)
                    Set Unit = New clsUnit
                End If
            End If
        ElseIf Left(HPI.Listing(Index), 7) = "weapons" And Right(HPI.Listing(Index), 3) = "tdf" Then
            ' Check to see if a weapons file of same name has been loaded. '
            Flag = False
            For i = 1 To modUnits.Weapons.Count
                If modUnits.Weapons(i).Filename = HPI.Listing(Index) Then
                    Flag = True
                End If
            Next
            ' Extract all weapons from the file. '
            If Not Flag Then
                LoadWeapons HPI.Listing(Index)
            End If
        ElseIf Left(HPI.Listing(Index), 8) = "download" And Right(HPI.Listing(Index), 3) = "tdf" Then
            'If HPI.Listing(Index) = "download\Corsca.tdf" Then Stop
            HPI.ExtractFile HPI.Listing(Index), "download.tdf"
            hTDFFile = FreeFile
            Open "download.tdf" For Input As hTDFFile
            LoadMenuEntries hTDFFile, Me
            Close hTDFFile
            Kill "download.tdf"
        End If
    Next
End Sub

' Load weapons from a TDF file. '
Public Sub LoadWeapons(Filename As String)
    Dim FileHandle As Integer
    Dim Flag As Boolean
    Dim Buffer As String
    Dim Index As Long, StartIndex As Long, EndIndex As Long
    Dim Weapon As New clsWeapon
    
    On Error GoTo Error
        
    ' Extract the file. '
    If Not HPI.ExtractFile(Filename, "temp.tdf") Then Exit Sub
            
    ' Parse the TDF file. '
    FileHandle = FreeFile
    Open "temp.tdf" For Binary As FileHandle
    Do While Not EOF(FileHandle)
        On Error Resume Next
        Line Input #FileHandle, Buffer
        If Err Then
            Flag = True
        Else
            Flag = False
        End If
        On Error GoTo Error
        Buffer = StripString(Buffer)
        Index = InStr(1, LCase(Buffer), "}")
        If Index <> 0 Or Flag Then ' End of weapon. '
            If (Weapon.Name <> "") Then
                Weapon.Filename = Filename
                Set Weapon.HPI = HPI
                Weapons.Add Weapon
                modUnits.Weapons.Add Weapon, Weapon.Name
                Set Weapon = New clsWeapon
            End If
        End If
        If Left(Buffer, 1) = "[" And Weapon.Name = "" Then
            Index = InStr(1, Buffer, "]")
            If Index > 0 Then
                Weapon.Name = Left(Right(Buffer, Len(Buffer) - 1), Len(Buffer) - (Len(Buffer) - Index) - 2)
                FrmProgress.Status = "Finding weapons... " & Weapon.Name
                FrmProgress.RefreshForm
            End If
        End If
        If Left(Buffer, 2) = "id" Then
            StartIndex = InStr(1, Buffer, "=")
            EndIndex = InStr(1, Buffer, ";")
            Weapon.ID = Val(Mid(Buffer, StartIndex + 1, EndIndex - StartIndex - 1))
        End If
    Loop
    
Error:
    Close FileHandle
    Kill "temp.tdf"
End Sub

' Save the unit/weapon list to a local file. '
Public Sub SaveList(Filename As String)
    Dim File As Integer
    Dim Index As Long, i As Long
    Dim Buffer As Long
    
    On Error GoTo Error
    File = FreeFile
    Open Filename For Binary As File
    
    ' Write header. '
    WriteString File, GetFilename(HPI.Filename)
    Index = Units.Count
    Put File, , Index
    Index = Weapons.Count
    Put File, , Index
    Index = MenuEntries.Count
    Put File, , Index
    
    ' Write units. '
    For Index = 1 To Units.Count
        WriteString File, Units(Index).Filename
        WriteString File, Units(Index).Name
        WriteString File, Units(Index).Callname
        WriteString File, Units(Index).Description
        WriteString File, Units(Index).Class
        WriteString File, Units(Index).ID
        WriteString File, Units(Index).Side
        
        WriteString File, Units(Index).MaxDamage
        WriteString File, Units(Index).BuildTime
        WriteString File, Units(Index).CostEnergy
        WriteString File, Units(Index).CostMetal
        WriteString File, Units(Index).SightDistance
        WriteString File, Units(Index).RadarDistance
        WriteString File, Units(Index).SonarDistance
        
        Buffer = Units(Index).Weapons.Count
        Put File, , Buffer
        For i = 1 To Units(Index).Weapons.Count
            WriteString File, Units(Index).Weapons(i).Name
        Next
    Next
    
    ' Write weapons. '
    For Index = 1 To Weapons.Count
        WriteString File, Weapons(Index).Filename
        WriteString File, Weapons(Index).Name
        WriteString File, CStr(Weapons(Index).ID)
    Next
    ' Write build-menu data. '
    'If Weapons.Count > 0 Then
    For Index = 1 To MenuEntries.Count
        WriteString File, MenuEntries(Index).BuilderUnit
        WriteString File, MenuEntries(Index).Button
        WriteString File, MenuEntries(Index).Menu
        WriteString File, MenuEntries(Index).Unit
    Next
    
    Close File
    Exit Sub
    
Error:
    On Error Resume Next
    Close File
    Kill Filename
    MsgBox Err.Description
End Sub

' Load the unit/weapon list from a local file. '
Public Function LoadList(Filename As String) As Boolean
    Dim File As Integer
    Dim Index As Long, i As Long
    Dim Buffer As String
    Dim UnitCount As Long, WeaponCount As Long, Count As Long
    Dim BuildMenuCount As Long
    Dim Unit As New clsUnit
    Dim Weapon As New clsWeapon
    Dim MenuEntry As clsMenuEntry
    
    On Error GoTo Error
    LoadList = False
    File = FreeFile
    Open Filename For Binary As File
    
    ' Read header. '
    Buffer = ReadString(File)
    If Buffer = "" Then GoTo Error
    If Dir(TALocation & Buffer) = "" Then
        If Dir(TALocation & EnableFilename(Buffer)) = "" Then
            Close File
            Exit Function
        Else
            Buffer = EnableFilename(Buffer)
        End If
    End If
    
    Set HPI = New clsHPI
    HPI.Filename = TALocation & Buffer
    
    Get File, , UnitCount
    Get File, , WeaponCount
    Get File, , BuildMenuCount
    
    ' Read units. '
    For Index = 1 To UnitCount
        Set Unit = New clsUnit
        Set Unit.HPI = HPI
        Set Unit.Group = Me
        
        Buffer = ReadString(File)
        Unit.Filename = Buffer
        Buffer = ReadString(File)
        Unit.Name = Buffer
        Buffer = ReadString(File)
        Unit.Callname = Buffer
        Buffer = ReadString(File)
        Unit.Description = Buffer
        Buffer = ReadString(File)
        Unit.Class = Buffer
        Buffer = ReadString(File)
        Unit.ID = Buffer
        Buffer = ReadString(File)
        Unit.Side = Buffer
        
        Buffer = ReadString(File)
        Unit.MaxDamage = Buffer
        Buffer = ReadString(File)
        Unit.BuildTime = Buffer
        Buffer = ReadString(File)
        Unit.CostEnergy = Buffer
        Buffer = ReadString(File)
        Unit.CostMetal = Buffer
        Buffer = ReadString(File)
        Unit.SightDistance = Buffer
        Buffer = ReadString(File)
        Unit.RadarDistance = Buffer
        Buffer = ReadString(File)
        Unit.SonarDistance = Buffer
        
        Get File, , Count
        For i = 1 To Count
            Buffer = ReadString(File)
            Unit.WeaponNames.Add Buffer
        Next
        
        Units.Add Unit
        modUnits.Units.Add Unit, Unit.Name & CStr(modUnits.Units.Count + 1)
    Next
    
    ' Read weapons. '
    For Index = 1 To WeaponCount
        Set Weapon = New clsWeapon
        Set Weapon.HPI = HPI
        
        Buffer = ReadString(File)
        Weapon.Filename = Buffer
        Buffer = ReadString(File)
        Weapon.Name = Buffer
        Buffer = ReadString(File)
        Weapon.ID = Val(Buffer)
    
        Weapons.Add Weapon
        modUnits.Weapons.Add Weapon, Weapon.Name
    Next
    
    'Read build menu entries
    For Index = 1 To BuildMenuCount
        Set MenuEntry = New clsMenuEntry
        
        Buffer = ReadString(File)
        MenuEntry.BuilderUnit = Buffer
        Buffer = ReadString(File)
        MenuEntry.Button = Buffer
        Buffer = ReadString(File)
        MenuEntry.Menu = Buffer
        Buffer = ReadString(File)
        MenuEntry.Unit = Buffer
    
        Me.MenuEntries.Add MenuEntry, MenuEntry.GenerateKey
        modUnits.MenuEntries.Add MenuEntry, MenuEntry.GenerateKey
    Next
    
    Close File
    LoadList = True
    Exit Function
    
Error:
    On Error Resume Next
    Close File
    Kill Filename
End Function

Public Sub LoadMenuEntries(h As Integer, UnitArchive As clsUnitArchive)
    On Error Resume Next
    Dim l As String, MenuEntry As clsMenuEntry
    Do While Not EOF(h)
        Line Input #h, l
        RemovePreBlanks l
        If l = "{" Then
            Set MenuEntry = New clsMenuEntry
            'read the data
            Line Input #h, l
            RemovePreBlanks l
            MenuEntry.BuilderUnit = Mid$(l, 10, Len(l) - 10)
            Line Input #h, l
            RemovePreBlanks l
            MenuEntry.Menu = Mid$(l, 6, 1)
            Line Input #h, l
            RemovePreBlanks l
            MenuEntry.Button = Mid$(l, 8, 1)
            Line Input #h, l
            RemovePreBlanks l
            MenuEntry.Unit = Mid$(l, 10, Len(l) - 10)
            'add the entry to the list
            l = MenuEntry.GenerateKey
            modUnits.MenuEntries.Add MenuEntry, l
            UnitArchive.MenuEntries.Add MenuEntry, l
        End If
    Loop
End Sub

Public Sub RemovePreBlanks(ByRef l As String)
    If InStr(l, Chr$(9)) = 1 Then _
        l = Right$(l, Len(l) - 1)
    l = Trim$(l)
End Sub
