Attribute VB_Name = "ProgramFunctions"
Public Const OFS_MAXPATHNAME = 128
Private Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Public NewFileDetect As Boolean
'----
Public Taldren_Shiplist_Size As String, Taldren_Ftrlist_Size As String, CMod1_Shiplist_Size As String, CMod1_Ftrlist_Size As String, CMod2_Shiplist_Size As String, CMod2_Ftrlist_Size As String, CMod3_Shiplist_Size As String, CMod3_Ftrlist_Size As String, CMod4_Shiplist_Size As String, CMod4_Ftrlist_Size As String
Public CMod1_Sounds As Boolean, CMod1_Textures As Boolean, CMod1_Cursors As Boolean, CMod1_GFs As Boolean, CMod1_Strings As Boolean
Public CMod2_Sounds As Boolean, CMod2_Textures As Boolean, CMod2_Cursors As Boolean, CMod2_GFs As Boolean, CMod2_Strings As Boolean
Public CMod3_Sounds As Boolean, CMod3_Textures As Boolean, CMod3_Cursors As Boolean, CMod3_GFs As Boolean, CMod3_Strings As Boolean
Public CMod4_Sounds As Boolean, CMod4_Textures As Boolean, CMod4_Cursors As Boolean, CMod4_GFs As Boolean, CMod4_Strings As Boolean
Public stringCurrentSpecs As String, CMod1_Name As String, CMod2_Name As String, CMod3_Name As String, OPDir As Variant, CMod4_Name As String
Public New_CMod_name As String, Taldren_FSound_Size As String, Taldren_FGF_Size As String, Taldren_FCursors_Size As String, Taldren_FTextures_Size As String
Public Taldren_FStrings_Size As String, Taldren_Sounds As Boolean, Taldren_Cursors As Boolean, Taldren_Textures As Boolean, Taldren_GFs As Boolean
Public Current_Loaded_Specs As String
'----
Type OFSTRUCT
    cBytes As Byte
    fFixedDisk As Byte
    nErrCode As Integer
    Reserved1 As Integer
    Reserved2 As Integer
    szPathName(OFS_MAXPATHNAME) As Byte
End Type

Option Explicit

   Public Const REG_SZ As Long = 1
   Public Const REG_DWORD As Long = 4

   Public Const HKEY_CLASSES_ROOT = &H80000000
   Public Const HKEY_CURRENT_USER = &H80000001
   Public Const HKEY_LOCAL_MACHINE = &H80000002
   Public Const HKEY_USERS = &H80000003

   Public Const ERROR_NONE = 0
   Public Const ERROR_BADDB = 1
   Public Const ERROR_BADKEY = 2
   Public Const ERROR_CANTOPEN = 3
   Public Const ERROR_CANTREAD = 4
   Public Const ERROR_CANTWRITE = 5
   Public Const ERROR_OUTOFMEMORY = 6
   Public Const ERROR_ARENA_TRASHED = 7
   Public Const ERROR_ACCESS_DENIED = 8
   Public Const ERROR_INVALID_PARAMETERS = 87
   Public Const ERROR_NO_MORE_ITEMS = 259

   Public Const KEY_QUERY_VALUE = &H1
   Public Const KEY_SET_VALUE = &H2
   Public Const KEY_ALL_ACCESS = &H3F

   Public Const REG_OPTION_NON_VOLATILE = 0

   Declare Function RegCloseKey Lib "advapi32.dll" _
   (ByVal hKey As Long) As Long
   Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias _
   "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, _
   ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions _
   As Long, ByVal samDesired As Long, ByVal lpSecurityAttributes _
   As Long, phkResult As Long, lpdwDisposition As Long) As Long
   Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias _
   "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, _
   ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As _
   Long) As Long
   Declare Function RegQueryValueExString Lib "advapi32.dll" Alias _
   "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As _
   String, ByVal lpReserved As Long, lpType As Long, ByVal lpData _
   As String, lpcbData As Long) As Long
   Declare Function RegQueryValueExLong Lib "advapi32.dll" Alias _
   "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As _
   String, ByVal lpReserved As Long, lpType As Long, lpData As _
   Long, lpcbData As Long) As Long
   Declare Function RegQueryValueExNULL Lib "advapi32.dll" Alias _
   "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As _
   String, ByVal lpReserved As Long, lpType As Long, ByVal lpData _
   As Long, lpcbData As Long) As Long
   Declare Function RegSetValueExString Lib "advapi32.dll" Alias _
   "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, _
   ByVal Reserved As Long, ByVal dwType As Long, ByVal lpValue As _
   String, ByVal cbData As Long) As Long
   Declare Function RegSetValueExLong Lib "advapi32.dll" Alias _
   "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, _
   ByVal Reserved As Long, ByVal dwType As Long, lpValue As Long, _
   ByVal cbData As Long) As Long
   
   

Public Function SetValueEx(ByVal hKey As Long, sValueName As String, _
   lType As Long, vValue As Variant) As Long
       Dim lValue As Long
       Dim sValue As String
       Select Case lType
           Case REG_SZ
               sValue = vValue & Chr$(0)
               SetValueEx = RegSetValueExString(hKey, sValueName, 0&, _
                                              lType, sValue, Len(sValue))
           Case REG_DWORD
               lValue = vValue
               SetValueEx = RegSetValueExLong(hKey, sValueName, 0&, _
   lType, lValue, 4)
           End Select
   End Function

   Function QueryValueEx(ByVal lhKey As Long, ByVal szValueName As _
   String, vValue As Variant) As Long
       Dim cch As Long
       Dim lrc As Long
       Dim lType As Long
       Dim lValue As Long
       Dim sValue As String

       On Error GoTo QueryValueExError

       ' Determine the size and type of data to be read
       lrc = RegQueryValueExNULL(lhKey, szValueName, 0&, lType, 0&, cch)
       If lrc <> ERROR_NONE Then Error 5

       Select Case lType
           ' For strings
           Case REG_SZ:
               sValue = String(cch, 0)

   lrc = RegQueryValueExString(lhKey, szValueName, 0&, lType, _
   sValue, cch)
               If lrc = ERROR_NONE Then
                   vValue = Left$(sValue, cch - 1)
               Else
                   vValue = Empty
               End If
           ' For DWORDS
           Case REG_DWORD:
   lrc = RegQueryValueExLong(lhKey, szValueName, 0&, lType, _
   lValue, cch)
               If lrc = ERROR_NONE Then vValue = lValue
           Case Else
               'all other data types not supported
               lrc = -1
       End Select

QueryValueExExit:
       QueryValueEx = lrc
       Exit Function

QueryValueExError:
       Resume QueryValueExExit
   End Function
            

   
Function FileExists(FileName As String) As Integer
    Dim RetCode As Integer
    Dim OpenFileStructure As OFSTRUCT

    Const OF_EXIST = &H4000
    Const FILE_NOT_FOUND = 2

    RetCode = OpenFile(FileName$, OpenFileStructure, OF_EXIST)
    If OpenFileStructure.nErrCode = FILE_NOT_FOUND Then
        FileExists = False
    Else
        FileExists = True
    End If
End Function
'------------------------- WAS IN FORM -------------------------------------------------------------
'||||||||||||||||||||||||| WAS IN FORM |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'------------------------- WAS IN FORM -------------------------------------------------------------

Public Function Conversion(Target_Mod As String, ModName As String, Sounds As Boolean, Cursors As Boolean, Textures As Boolean, GFs As Boolean, Strings As Boolean)
Dim fso
Set fso = CreateObject("Scripting.filesystemobject")
ProgressBarForm.Visible = True
Call SetAttributeFolder(OPDir & "\Assets\Models")
ProgressBarForm.ProgressBar1.Value = 5.3
Call fso.CopyFile(OPDir & "\SFCOP Mod Chooser\Bin\" & Target_Mod & "\Models\model.siz", OPDir & "\Assets\Models\model.siz")
ProgressBarForm.ProgressBar1.Value = 10.6
'--- Strings
Call SetAttributeFolder(OPDir & "\Assets\Strings")
Call fso.CopyFolder(OPDir & "\SFCOP Mod Chooser\Bin\" & Target_Mod & "\Strings", OPDir & "\Assets\")
ProgressBarForm.ProgressBar1.Value = 15.7
'--- Specs
ProgressBarForm.ProgressBar1.Value = 21
Call SetAttributeFolder(OPDir & "\assets\Specs")
Call fso.CopyFolder(OPDir & "\SFCOP Mod Chooser\Bin\" & Target_Mod & "\Specs", OPDir & "\Assets\")
ProgressBarForm.ProgressBar1.Value = 26
Call fso.CopyFile(OPDir & "\SFCOP Mod Chooser\Bin\" & Target_Mod & "\Specs\shiplist.txt", OPDir & "\MetaAssets\shiplist.txt")
ProgressBarForm.ProgressBar1.Value = 31
Call fso.CopyFile(OPDir & "\SFCOP Mod Chooser\Bin\" & Target_Mod & "\Specs\ftrlist.txt", OPDir & "\MetaAssets\ftrlist.txt")

' Sounds
If Sounds = True Or GetSetting(Current_Loaded_Specs, "Sounds", False) = True Then
    ProgressBarForm.ProgressBar1.Value = 36
    Call SetAttributeFolder(OPDir & "\assets\Sound")
    If FileExists(OPDir & "\SFCOP Mod Chooser\Bin\" & Target_Mod & "\Sound\sounds.txt") Then
        Call fso.CopyFile(OPDir & "\SFCOP Mod Chooser\Bin\" & Target_Mod & "\Sound\sounds.txt", OPDir & "\Assets\Sound\sounds.txt")
        ProgressBarForm.ProgressBar1.Value = 42
        Call fso.CopyFile(OPDir & "\SFCOP Mod Chooser\Bin\" & Target_Mod & "\Sound\SFCSounds.zip", OPDir & "\Assets\Sound\SFCSounds.zip")
        ProgressBarForm.ProgressBar1.Value = 47
        Call fso.CopyFile(OPDir & "\SFCOP Mod Chooser\Bin\" & Target_Mod & "\Sound\SFCVoice.zip", OPDir & "\Assets\Sound\SFCVoice.zip")
    Else
        Call fso.CopyFile(OPDir & "\SFCOP Mod Chooser\Bin\Taldren\Sound\sounds.txt", OPDir & "\Assets\Sound\sounds.txt")
        ProgressBarForm.ProgressBar1.Value = 42
        Call fso.CopyFile(OPDir & "\SFCOP Mod Chooser\Bin\Taldren\Sound\SFCSounds.zip", OPDir & "\Assets\Sound\SFCSounds.zip")
        ProgressBarForm.ProgressBar1.Value = 47
        Call fso.CopyFile(OPDir & "\SFCOP Mod Chooser\Bin\Taldren\Sound\SFCVoice.zip", OPDir & "\Assets\Sound\SFCVoice.zip")
    End If
End If

If Cursors = True Or GetSetting(Current_Loaded_Specs, "Cursors", False) = True Then
    Call SetAttributeFolder(OPDir & "\assets\Mouse")
    If FileExists(OPDir & "\SFCOP Mod Chooser\Bin\" & Target_Mod & "\Mouse\cursors.txt") Then
        ProgressBarForm.ProgressBar1.Value = 56
        Call fso.CopyFolder(OPDir & "\SFCOP Mod Chooser\Bin\" & Target_Mod & "\Mouse", OPDir & "\Assets\Mouse")
    Else
        ProgressBarForm.ProgressBar1.Value = 56
        Call fso.CopyFolder(OPDir & "\SFCOP Mod Chooser\Bin\Taldren\Mouse", OPDir & "\Assets\Mouse")
    End If
End If

If Textures = True Or GetSetting(Current_Loaded_Specs, "Textures", False) = True Then
    ProgressBarForm.ProgressBar1.Value = 63
    Call SetAttributeFolder(OPDir & "\Assets\Textures")
    If FileExists(OPDir & "\SFCOP Mod Chooser\Bin\" & Target_Mod & "\Textures\debris1.bmp") Then
        ProgressBarForm.ProgressBar1.Value = 68
        Call fso.CopyFile(OPDir & "\SFCOP Mod Chooser\Bin\" & Target_Mod & "\Textures\*.bmp", OPDir & "\Assets\Textures", True)
    Else
        ProgressBarForm.ProgressBar1.Value = 68
        Call fso.CopyFile(OPDir & "\SFCOP Mod Chooser\Bin\Taldren\Textures\*.bmp", OPDir & "\Assets\Textures", True)
    End If
End If

If GFs = True Or GetSetting(Current_Loaded_Specs, "GFs", False) = True Then
    ProgressBarForm.ProgressBar1.Value = 73
    Call SetAttributeFolder(OPDir & "\MetaAssets\ServerProfiles\SinglePlayer")
    If FileExists(OPDir & "\SFCOP Mod Chooser\Bin\" & Target_Mod & "\GFs\assets.gf") Then
        ProgressBarForm.ProgressBar1.Value = 78
        Call fso.CopyFolder(OPDir & "\SFCOP Mod Chooser\Bin\" & Target_Mod & "\GFs", OPDir & "\MetaAssets\ServerProfiles\SinglePlayer", True)
    Else
        ProgressBarForm.ProgressBar1.Value = 78
        Call fso.CopyFolder(OPDir & "\SFCOP Mod Chooser\Bin\Taldren\GFs", OPDir & "\MetaAssets\ServerProfiles\SinglePlayer", True)
    End If
End If

If Strings = True Or GetSetting(Current_Loaded_Specs, "Strings", False) = True Then
    ProgressBarForm.ProgressBar1.Value = 84
    Call SetAttributeFolder(OPDir & "\Assets\Strings")
    If FileExists(OPDir & "\SFCOP Mod Chooser\Bin\" & Target_Mod & "\Strings\strings.txt") Then
        ProgressBarForm.ProgressBar1.Value = 92
        Call fso.CopyFolder(OPDir & "\SFCOP Mod Chooser\Bin\" & Target_Mod & "\Strings", OPDir & "\Assets\", True)
    Else
        ProgressBarForm.ProgressBar1.Value = 92
        Call fso.CopyFolder(OPDir & "\SFCOP Mod Chooser\Bin\Taldren\Strings", OPDir & "\Assets\", True)
    End If
End If

' Makes new Marker
Call WritePrivateProfileString("CurrentSpecs", "CurrentSpecs", Target_Mod, OPDir & "\SFCOP Mod Chooser\filedata.ini")
ProgressBarForm.ProgressBar1.Value = 100
ProgressBarForm.Visible = False
Call Reset
Call FinishedMsg(ModName)
End Function
Function CompairShiplists(SpecTypeCompair As String, SpecCompair As String, FileLenCompair As String, SpecTypeName As String)

If FileLenCompair = Not FileLen(OPDir & "\SFCOP Mod Chooser\Bin\" & SpecTypeCompair & "\Specs\" & SpecCompair & ".txt") Then
    Call Error(SpecTypeName)
End If

End Function
Function CompairCurrent()
Dim ShiplistFileLen As String, FtrlistFileLen As String
If Current_Loaded_Specs = "Taldren" Then
    ShiplistFileLen = Taldren_Shiplist_Size
    FtrlistFileLen = Taldren_Ftrlist_Size
ElseIf Current_Loaded_Specs = "CMod1" Then
    ShiplistFileLen = CMod1_Shiplist_Size
    FtrlistFileLen = CMod1_Ftrlist_Size
ElseIf Current_Loaded_Specs = "CMod2" Then
    ShiplistFileLen = CMod2_Shiplist_Size
    FtrlistFileLen = CMod2_Ftrlist_Size
ElseIf Current_Loaded_Specs = "CMod3" Then
    ShiplistFileLen = CMod3_Shiplist_Size
    FtrlistFileLen = CMod3_Ftrlist_Size
ElseIf Current_Loaded_Specs = "CMod4" Then
    ShiplistFileLen = CMod4_Shiplist_Size
    FtrlistFileLen = CMod4_Ftrlist_Size
Else
     Call Conversion("Taldren", "Taldren's 2552", False, False, False, False, False)
End If


If FileExists(OPDir & "\assets\specs\shiplist.txt") Then
        ' If it does, this checks for legitamacy by file size date, and if OK, end this function here.
    If ShiplistFileLen = FileLen(OPDir & "\assets\specs\shiplist.txt") Then
        Exit Function
    Else
        Call Identify_Lists
    End If
Else
    Call Conversion("Taldren", "Taldren's 2552", False, False, False, False, False)
End If

If FileExists(OPDir & "\assets\specs\ftrlist.txt") Then
        ' If it does, this checks for legitamacy by file size date, and if OK, end this function here.
    If FtrlistFileLen = FileLen(OPDir & "\assets\specs\ftrlist.txt") Then
        Exit Function
    Else
        Call Identify_Lists
    End If
Else
    Call Conversion("Taldren", "Taldren's 2552", False, False, False, False, False)
End If

End Function
Function FinishedMsg(LoadedSpecs As String)
'Shoes after Ship Specs are loaded, then closes program
MsgBox "You have successfully loaded the following Mod: " & LoadedSpecs, vbOKOnly
End Function
Function Error(Offender As String)

MsgBox "The " & Offender & " Mod has been corrupted.  To repair, please reinstall the " & Offender & " Mod.", vbOKOnly

End Function

Function Load_INI()
Current_Loaded_Specs = GetSetting("CurrentSpecs", "CurrentSpecs", "Error")
'--
Taldren_Shiplist_Size = GetSetting("Shiplist_Files_Sizes", "Taldren_Shiplist_Size", 0)
Taldren_Ftrlist_Size = GetSetting("Ftrlist_Files_Sizes", "Taldren_Ftrlist_Size", 0)
CMod1_Shiplist_Size = GetSetting("Shiplist_Files_Sizes", "CMod1_Shiplist_Size", 0)
CMod1_Ftrlist_Size = GetSetting("Ftrlist_Files_Sizes", "CMod1_Ftrlist_Size", 0)
CMod2_Shiplist_Size = GetSetting("Shiplist_Files_Sizes", "CMod2_Shiplist_Size", 0)
CMod2_Ftrlist_Size = GetSetting("Ftrlist_Files_Sizes", "CMod2_Ftrlist_Size", 0)
CMod3_Shiplist_Size = GetSetting("Shiplist_Files_Sizes", "CMod3_Shiplist_Size", 0)
CMod3_Ftrlist_Size = GetSetting("Ftrlist_Files_Sizes", "CMod3_Ftrlist_Size", 0)
CMod4_Shiplist_Size = GetSetting("Shiplist_Files_Sizes", "CMod4_Shiplist_Size", 0)
CMod4_Ftrlist_Size = GetSetting("Ftrlist_Files_Sizes", "CMod4_Ftrlist_Size", 0)
'---
CMod1_Name = GetSetting("Names", "CMod1_Name", "Custom Mod #1")
CMod2_Name = GetSetting("Names", "CMod2_Name", "Custom Mod #2")
CMod3_Name = GetSetting("Names", "CMod3_Name", "Custom Mod #3")
CMod4_Name = GetSetting("Names", "CMod4_Name", "Custom Mod #4")
'---
CMod1_Sounds = GetSetting("CMod1", "Sounds", False)
CMod1_Textures = GetSetting("CMod1", "Textures", False)
CMod1_Cursors = GetSetting("CMod1", "Cursors", False)
CMod1_GFs = GetSetting("CMod1", "GFs", False)
CMod1_Strings = GetSetting("CMod1", "Strings", False)
'-
CMod2_Sounds = GetSetting("CMod2", "Sounds", False)
CMod2_Textures = GetSetting("CMod2", "Textures", False)
CMod2_Cursors = GetSetting("CMod2", "Cursors", False)
CMod2_GFs = GetSetting("CMod2", "GFs", False)
CMod2_Strings = GetSetting("CMod2", "Strings", False)
'-
CMod3_Sounds = GetSetting("CMod3", "Sounds", False)
CMod3_Textures = GetSetting("CMod3", "Textures", False)
CMod3_Cursors = GetSetting("CMod3", "Cursors", False)
CMod3_GFs = GetSetting("CMod3", "GFs", False)
CMod3_Strings = GetSetting("CMod3", "Strings", False)
'-
CMod4_Sounds = GetSetting("CMod4", "Sounds", False)
CMod4_Textures = GetSetting("CMod4", "Textures", False)
CMod4_Cursors = GetSetting("CMod4", "Cursors", False)
CMod4_GFs = GetSetting("CMod4", "GFs", False)
CMod4_Strings = GetSetting("CMod4", "Strings", False)
'--
Taldren_FSound_Size = GetSetting("Taldren", "SoundsFolder", 423418336)
Taldren_FTextures_Size = GetSetting("Taldren", "TexturesFolder", 7645482)
Taldren_FGF_Size = GetSetting("Taldren", "GFFolder", 42248)
Taldren_FCursors_Size = GetSetting("Taldren", "CursorsFolder", 27273)
Taldren_FStrings_Size = GetSetting("Taldren", "StringsFolder", 964444)

End Function
Function Find_OP_Dir()
'  Locates the installation directory of OP from Windows Registry
OPDir = QueryValue("Software\Taldren\Starfleet Command Orion Pirates", "Directory")
End Function
'  Function needed to pull information from Windows Registry SAVES RESULTS TO OPDIR
Function QueryValue(sKeyName As String, sValueName As String) As Variant
       Dim lRetVal As Long         'result of the API functions
       Dim hKey As Long         'handle of opened key
       Dim vValue As String 'setting of queried value is vValue

       lRetVal = RegOpenKeyEx(HKEY_LOCAL_MACHINE, sKeyName, 0, _
   KEY_QUERY_VALUE, hKey)
       lRetVal = QueryValueEx(hKey, sValueName, vValue)
       RegCloseKey (hKey)
       QueryValue = vValue
End Function
' Function pulls data from filedata.ini file
Function GetSetting(WhatSection As String, WhatKey As String, DefaultValue As String) As String
Dim Results As String
Dim Length As Long
Dim WhatINI As String
Dim returnCode As Long

Length = 255
WhatINI = OPDir & "\SFCOP Mod Chooser\filedata.ini"
Results = String(Length, " ")
returnCode = GetPrivateProfileString(WhatSection, WhatKey, DefaultValue, Results, Length, WhatINI)
GetSetting = Left(Results, returnCode)

End Function
Function Identify_Lists()
' Ship LIST CHECKS --------------------
If Taldren_Shiplist_Size = FileLen(OPDir & "\assets\specs\shiplist.txt") Then
    Call Conversion("Taldren", "Taldren's 2552", False, False, False, False, False)
    Exit Function
End If

If CMod1_Shiplist_Size = FileLen(OPDir & "\assets\specs\shiplist.txt") Then
    Call Conversion("CMod1", CMod1_Name, CMod1_Sounds, CMod1_Cursors, CMod1_Textures, CMod1_GFs, CMod1_Strings)
    Exit Function
End If

If CMod2_Shiplist_Size = FileLen(OPDir & "\assets\specs\shiplist.txt") Then
    Call Conversion("CMod2", CMod2_Name, CMod2_Sounds, CMod2_Cursors, CMod2_Textures, CMod2_GFs, CMod2_Strings)
    Exit Function
End If

If CMod3_Shiplist_Size = FileLen(OPDir & "\assets\specs\shiplist.txt") Then
    Call Conversion("CMod3", CMod3_Name, CMod3_Sounds, CMod3_Cursors, CMod3_Textures, CMod3_GFs, CMod3_Strings)
    Exit Function
End If

If CMod4_Shiplist_Size = FileLen(OPDir & "\assets\specs\shiplist.txt") Then
    Call Conversion("CMod4", CMod4_Name, CMod4_Sounds, CMod4_Cursors, CMod4_Textures, CMod4_GFs, CMod4_Strings)
    Exit Function
End If

NewFileDetect = True

End Function

Function Load_File_Sizes_to_INI()
Dim CM1SS As String, CM2SS As String, CM3SS As String, CM4SS As String
Dim CM1FS As String, CM2FS As String, CM3FS As String, CM4FS As String
' Load Files Sizes For the files
' ----------- Shiplist Files
If FileExists(OPDir & "\SFCOP Mod Chooser\Bin\CMod1\Specs\Shiplist.txt") Then
    CM1SS = FileLen(OPDir & "\SFCOP Mod Chooser\Bin\CMod1\Specs\Shiplist.txt")
Else
    CM1SS = 0
End If
If FileExists(OPDir & "\SFCOP Mod Chooser\Bin\CMod2\Specs\Shiplist.txt") Then
    CM2SS = FileLen(OPDir & "\SFCOP Mod Chooser\Bin\CMod2\Specs\Shiplist.txt")
Else
    CM2SS = 0
End If
If FileExists(OPDir & "\SFCOP Mod Chooser\Bin\CMod3\Specs\Shiplist.txt") Then
    CM3SS = FileLen(OPDir & "\SFCOP Mod Chooser\Bin\Cmod3\Specs\Shiplist.txt")
Else
    CM3SS = 0
End If
If FileExists(OPDir & "\SFCOP Mod Chooser\Bin\CMod4\Specs\Shiplist.txt") Then
    CM4SS = FileLen(OPDir & "\SFCOP Mod Chooser\Bin\Cmod4\Specs\Shiplist.txt")
Else
    CM4SS = 0
End If

' ----------- Ftrlist Files
If FileExists(OPDir & "\SFCOP Mod Chooser\Bin\CMod1\Specs\Ftrlist.txt") Then
    CM1FS = FileLen(OPDir & "\SFCOP Mod Chooser\Bin\CMod1\Specs\Ftrlist.txt")
Else
    CM1FS = 0
End If
If FileExists(OPDir & "\SFCOP Mod Chooser\Bin\CMod2\Specs\Ftrlist.txt") Then
    CM2FS = FileLen(OPDir & "\SFCOP Mod Chooser\Bin\CMod2\Specs\Ftrlist.txt")
Else
    CM2FS = 0
End If
If FileExists(OPDir & "\SFCOP Mod Chooser\Bin\CMod3\Specs\Ftrlist.txt") Then
    CM3FS = FileLen(OPDir & "\SFCOP Mod Chooser\Bin\Cmod3\Specs\Ftrlist.txt")
Else
    CM3FS = 0
End If
If FileExists(OPDir & "\SFCOP Mod Chooser\Bin\CMod4\Specs\Ftrlist.txt") Then
    CM4FS = FileLen(OPDir & "\SFCOP Mod Chooser\Bin\Cmod4\Specs\Ftrlist.txt")
Else
    CM4FS = 0
End If
' Shiplist
Call WritePrivateProfileString("Shiplist_Files_Sizes", "CMod1_Shiplist_Size", CM1SS, OPDir & "\SFCOP Mod Chooser\filedata.ini")
Call WritePrivateProfileString("Shiplist_Files_Sizes", "CMod2_Shiplist_Size", CM2SS, OPDir & "\SFCOP Mod Chooser\filedata.ini")
Call WritePrivateProfileString("Shiplist_Files_Sizes", "CMod3_Shiplist_Size", CM3SS, OPDir & "\SFCOP Mod Chooser\filedata.ini")
Call WritePrivateProfileString("Shiplist_Files_Sizes", "CMod4_Shiplist_Size", CM4SS, OPDir & "\SFCOP Mod Chooser\filedata.ini")

' Fighterlist
Call WritePrivateProfileString("Ftrlist_Files_Sizes", "CMod1_Ftrlist_Size", CM1FS, OPDir & "\SFCOP Mod Chooser\filedata.ini")
Call WritePrivateProfileString("Ftrlist_Files_Sizes", "CMod2_Ftrlist_Size", CM2FS, OPDir & "\SFCOP Mod Chooser\filedata.ini")
Call WritePrivateProfileString("Ftrlist_Files_Sizes", "CMod3_Ftrlist_Size", CM3FS, OPDir & "\SFCOP Mod Chooser\filedata.ini")
Call WritePrivateProfileString("Ftrlist_Files_Sizes", "CMod4_Ftrlist_Size", CM4FS, OPDir & "\SFCOP Mod Chooser\filedata.ini")
End Function

Function SpecsVerification()
' Judges by file size in bytes if ship specs files are legitimate.  If not, program should attemp to
' restore origioanls from a back folder.  Program should also check BackUp folder first to see that it has not been
' tampered with.  If the BackUp folder does not contain legitimate backup files(Proven by creation
' date), then a message appears requesting reinstall.  No swapping should occur.

Call CompairShiplists("Taldren", "shiplist", Taldren_Shiplist_Size, "Taldren's 2552")
Call CompairShiplists("Taldren", "ftrlist", Taldren_Ftrlist_Size, "Taldren's 2552")
If CMod1_Shiplist_Size > 0 Then Call CompairShiplists("CMod1", "shiplist", CMod1_Shiplist_Size, CMod1_Name)
If CMod1_Ftrlist_Size > 0 Then Call CompairShiplists("CMod1", "ftrlist", CMod1_Ftrlist_Size, CMod1_Name)
If CMod2_Shiplist_Size > 0 Then Call CompairShiplists("CMod2", "shiplist", CMod2_Shiplist_Size, CMod2_Name)
If CMod2_Ftrlist_Size > 0 Then Call CompairShiplists("CMod2", "ftrlist", CMod2_Ftrlist_Size, CMod2_Name)
If CMod3_Shiplist_Size > 0 Then Call CompairShiplists("CMod3", "shiplist", CMod3_Shiplist_Size, CMod3_Name)
If CMod3_Ftrlist_Size > 0 Then Call CompairShiplists("CMod3", "ftrlist", CMod3_Ftrlist_Size, CMod3_Name)
If CMod4_Shiplist_Size > 0 Then Call CompairShiplists("CMod4", "shiplist", CMod4_Shiplist_Size, CMod4_Name)
If CMod4_Ftrlist_Size > 0 Then Call CompairShiplists("CMod4", "ftrlist", CMod4_Ftrlist_Size, CMod4_Name)
Call CompairCurrent

End Function
Function InstallNewMod(Destination As String)
Dim fso
Set fso = CreateObject("Scripting.filesystemobject")
ProgressBarForm.Visible = True
ProgressBarForm.ProgressBar1.Value = 7.7
'-- Shiplist and FTRlist
If FileExists(OPDir & "\SFCOP Mod Chooser\Bin\" & Destination & "\Specs\shiplist.txt") Then
    Call fso.DeleteFile(OPDir & "\SFCOP Mod Chooser\Bin\" & Destination & "\Specs\shiplist.txt", True)
End If
ProgressBarForm.ProgressBar1.Value = 15
If FileExists(OPDir & "\SFCOP Mod Chooser\Bin\" & Destination & "\Specs\ftrlist.txt") Then
    Call fso.DeleteFile(OPDir & "\SFCOP Mod Chooser\Bin\" & Destination & "\Specs\ftrlist.txt", True)
End If
ProgressBarForm.ProgressBar1.Value = 23
Call fso.CopyFile(OPDir & "\Assets\Specs\shiplist.txt", OPDir & "\SFCOP Mod Chooser\Bin\" & Destination & "\Specs\shiplist.txt", True)
ProgressBarForm.ProgressBar1.Value = 30
Call fso.CopyFile(OPDir & "\Assets\Specs\ftrlist.txt", OPDir & "\SFCOP Mod Chooser\Bin\" & Destination & "\Specs\ftrlist.txt", True)
ProgressBarForm.ProgressBar1.Value = 38
'-- Model.siz                                                  SFCOP Mod Chooser\Bin\CMod1\Models
If FileExists(OPDir & "\SFCOP Mod Chooser\Bin\" & Destination & "\Models\model.siz") Then
    Call fso.DeleteFile(OPDir & "\SFCOP Mod Chooser\Bin\" & Destination & "\Models\model.siz", True)
End If
ProgressBarForm.ProgressBar1.Value = 46
Call fso.CopyFile(OPDir & "\Assets\Models\model.siz", OPDir & "\SFCOP Mod Chooser\Bin\" & Destination & "\Models\model.siz", True)
ProgressBarForm.ProgressBar1.Value = 54
'-- Textures
If ShowFolderSize(OPDir & "\Assets\Textures") = Taldren_FTextures_Size Then
    Call WritePrivateProfileString(Destination, "Textures", "False", OPDir & "\SFCOP Mod Chooser\filedata.ini")
    ProgressBarForm.ProgressBar1.Value = 61
Else
    Call WritePrivateProfileString(Destination, "Textures", "True", OPDir & "\SFCOP Mod Chooser\filedata.ini")
    ProgressBarForm.ProgressBar1.Value = 61
    Call SetAttributeFolder(OPDir & "\SFCOP Mod Chooser\Bin\" & Destination & "\Textures")
    ProgressBarForm.ProgressBar1.Value = 63
    Call fso.CopyFolder(OPDir & "\Assets\Textures", OPDir & "\SFCOP Mod Chooser\Bin\" & Destination & "\", True)
    ProgressBarForm.ProgressBar1.Value = 66
End If
'-- Sounds
If ShowFolderSize(OPDir & "\Assets\Sound") = Taldren_FSound_Size Then
    Call WritePrivateProfileString(Destination, "Sounds", "False", OPDir & "\SFCOP Mod Chooser\filedata.ini")
    ProgressBarForm.ProgressBar1.Value = 69
Else
    Call WritePrivateProfileString(Destination, "Sounds", "True", OPDir & "\SFCOP Mod Chooser\filedata.ini")
    ProgressBarForm.ProgressBar1.Value = 69
    Call SetAttributeFile(OPDir & "\SFCOP Mod Chooser\Bin\" & Destination & "\Sound\SFCSounds.zip")
    ProgressBarForm.ProgressBar1.Value = 70
    Call SetAttributeFile(OPDir & "\SFCOP Mod Chooser\Bin\" & Destination & "\Sound\SFCVoice.zip")
    ProgressBarForm.ProgressBar1.Value = 72
    Call SetAttributeFile(OPDir & "\SFCOP Mod Chooser\Bin\" & Destination & "\Sound\Sounds.txt")
    ProgressBarForm.ProgressBar1.Value = 74
    Call fso.CopyFile(OPDir & "\Assets\Sound\SFCSounds.zip", OPDir & "\SFCOP Mod Chooser\Bin\" & Destination & "\Sound\SFCSounds.zip", True)
    ProgressBarForm.ProgressBar1.Value = 75
    Call fso.CopyFile(OPDir & "\Assets\Sound\SFCVoice.zip", OPDir & "\SFCOP Mod Chooser\Bin\" & Destination & "\Sound\SFCVoice.zip", True)
    ProgressBarForm.ProgressBar1.Value = 76
    Call fso.CopyFile(OPDir & "\Assets\Sound\sounds.txt", OPDir & "\SFCOP Mod Chooser\Bin\" & Destination & "\Sound\Sounds.txt", True)
    ProgressBarForm.ProgressBar1.Value = 77
End If
'-- GF's
If ShowFolderSize(OPDir & "\MetaAssets\ServerProfiles\SinglePlayer") = Taldren_FGF_Size Then
    ProgressBarForm.ProgressBar1.Value = 78
    Call WritePrivateProfileString(Destination, "GFs", "False", OPDir & "\SFCOP Mod Chooser\filedata.ini")
Else
    Call WritePrivateProfileString(Destination, "GFs", "True", OPDir & "\SFCOP Mod Chooser\filedata.ini")
    ProgressBarForm.ProgressBar1.Value = 78
    Call SetAttributeFolder(OPDir & "\SFCOP Mod Chooser\Bin\" & Destination & "\GFs")
    ProgressBarForm.ProgressBar1.Value = 80
    Call fso.CopyFolder(OPDir & "\MetaAssets\ServerProfiles\SinglePlayer", OPDir & "\SFCOP Mod Chooser\Bin\" & Destination & "\GFs", True)
    ProgressBarForm.ProgressBar1.Value = 82
End If
'-- Cursors
If ShowFolderSize(OPDir & "\Assets\Mouse") = Taldren_FCursors_Size Then
    ProgressBarForm.ProgressBar1.Value = 85
    Call WritePrivateProfileString(Destination, "Cursors", "False", OPDir & "\SFCOP Mod Chooser\filedata.ini")
Else
    Call WritePrivateProfileString(Destination, "Cursors", "True", OPDir & "\SFCOP Mod Chooser\filedata.ini")
    ProgressBarForm.ProgressBar1.Value = 85
    Call SetAttributeFolder(OPDir & "\SFCOP Mod Chooser\Bin\" & Destination & "\Mouse")
    ProgressBarForm.ProgressBar1.Value = 88
    Call fso.CopyFolder(OPDir & "\Assets\Mouse", OPDir & "\SFCOP Mod Chooser\Bin\" & Destination & "\", True)
    ProgressBarForm.ProgressBar1.Value = 90
End If
 '-- Strings
If ShowFolderSize(OPDir & "\Assets\Strings") = Taldren_FStrings_Size Then
    Call WritePrivateProfileString(Destination, "Strings", "False", OPDir & "\SFCOP Mod Chooser\filedata.ini")
    ProgressBarForm.ProgressBar1.Value = 92
Else
    Call WritePrivateProfileString(Destination, "Strings", "True", OPDir & "\SFCOP Mod Chooser\filedata.ini")
    ProgressBarForm.ProgressBar1.Value = 92
    Call SetAttributeFolder(OPDir & "\SFCOP Mod Chooser\Bin\" & Destination & "\Strings")
    ProgressBarForm.ProgressBar1.Value = 93
    Call fso.CopyFolder(OPDir & "\Assets\Strings", OPDir & "\SFCOP Mod Chooser\Bin\" & Destination & "\", True)
    ProgressBarForm.ProgressBar1.Value = 94
End If
Call WritePrivateProfileString("Names", Destination & "_name", New_CMod_name, OPDir & "\SFCOP Mod Chooser\filedata.ini")
ProgressBarForm.ProgressBar1.Value = 95
Call WritePrivateProfileString("CurrentSpecs", "CurrentSpecs", Destination, OPDir & "\SFCOP Mod Chooser\filedata.ini")
ProgressBarForm.ProgressBar1.Value = 100
ProgressBarForm.ProgressBar1.Visible = False
Call FinishedMsg(New_CMod_name)
ProgressBarForm.Visible = True
Call Reset
End Function


Sub Main()
OPDir = QueryValue("Software\Taldren\Starfleet Command Orion Pirates", "Directory")
'  These commands/functions are run when form opens, in this case, when program starts
' Calls to find what Directory from the Windows Registry OP is installed to.
' Scan for Mod files and input sizes to INI
Call Load_File_Sizes_to_INI
' Get Variable Data from 'filedata.ini'
Call Load_INI

Call NewInstall

Call Load_INI
'  Program should test all file for legitimacy at startup
Call SpecsVerification

Call Load_INI

If NewFileDetect = True Then
    NewModForm.Show
Else
    MainWindow.Show
End If

End Sub

Public Function ShowFolderSize(FileSpec As String) As Long

    Dim FS, f, s
    Set FS = CreateObject("Scripting.FileSystemObject")
        Set f = FS.GetFolder(FileSpec)
    ShowFolderSize = f.Size
End Function
Public Function SetAttributeFile(FileSpec As String)

    Dim FS, f, r
    Set FS = CreateObject("Scripting.FileSystemObject")
    If FileExists(FileSpec) Then
        Set f = FS.GetFile(FileSpec)
        f.Attributes = f.Attributes - 1
    End If
End Function
Public Function SetAttributeFolder(FileSpec As String)
    Dim s
    FileSpec = FileSpec & "\"
    s = Dir$(FileSpec & "\*.*")
    
    Do Until Len(s) = 0
    s = FileSpec & s
    SetAttr s, vbNormal
    s = Dir$
  'This call to dir with no arguments will get the next file in the directory.
    Loop

End Function

Public Function NewInstall()
Dim NewModStatus As Boolean, Taldren_FTextures_Size As String, Taldren_FCursors_Size As String, Taldren_FGFs_Size As String, Taldren_FSounds_Size As String, Taldren_FStrings_Size As String, Taldren_Shiplist_Size As String, Taldren_Ftrlist_Size As String
Dim fso
Set fso = CreateObject("Scripting.filesystemobject")
NewModStatus = GetSetting("NewInstall", "Status", "p")
If NewModStatus = True Then
    ProgressBarForm.Show
    Taldren_FTextures_Size = ShowFolderSize(OPDir & "\Assets\Textures")
    ProgressBarForm.ProgressBar1.Value = 4
    Call WritePrivateProfileString("Taldren", "TexturesFolder", Taldren_FTextures_Size, OPDir & "\SFCOP Mod Chooser\filedata.ini")
    ProgressBarForm.ProgressBar1.Value = 8
    Taldren_FCursors_Size = ShowFolderSize(OPDir & "\Assets\Mouse")
    ProgressBarForm.ProgressBar1.Value = 12
    Call WritePrivateProfileString("Taldren", "CursorsFolder", Taldren_FCursors_Size, OPDir & "\SFCOP Mod Chooser\filedata.ini")
    ProgressBarForm.ProgressBar1.Value = 16
    Taldren_FGFs_Size = ShowFolderSize(OPDir & "\MetaAssets\ServerProfiles\SinglePlayer")
    ProgressBarForm.ProgressBar1.Value = 20
    Call WritePrivateProfileString("Taldren", "GFFolder", Taldren_FGFs_Size, OPDir & "\SFCOP Mod Chooser\filedata.ini")
    ProgressBarForm.ProgressBar1.Value = 24
    Taldren_FSounds_Size = ShowFolderSize(OPDir & "\Assets\Sound")
    ProgressBarForm.ProgressBar1.Value = 28
    Call WritePrivateProfileString("Taldren", "SoundsFolder", Taldren_FSounds_Size, OPDir & "\SFCOP Mod Chooser\filedata.ini")
    ProgressBarForm.ProgressBar1.Value = 32
    Taldren_FStrings_Size = ShowFolderSize(OPDir & "\Assets\Strings")
    ProgressBarForm.ProgressBar1.Value = 36
    Call WritePrivateProfileString("Taldren", "StringsFolder", Taldren_FStrings_Size, OPDir & "\SFCOP Mod Chooser\filedata.ini")
    ProgressBarForm.ProgressBar1.Value = 40
    Taldren_Shiplist_Size = FileLen(OPDir & "\Assets\specs\shiplist.txt")
    ProgressBarForm.ProgressBar1.Value = 44
    Call WritePrivateProfileString("Shiplist_Files_Sizes", "Taldren_Shiplist_Size", Taldren_Shiplist_Size, OPDir & "\SFCOP Mod Chooser\filedata.ini")
    ProgressBarForm.ProgressBar1.Value = 48
    Taldren_Ftrlist_Size = FileLen(OPDir & "\Assets\specs\ftrlist.txt")
    ProgressBarForm.ProgressBar1.Value = 52
    Call WritePrivateProfileString("ftrlist_Files_Sizes", "Taldren_ftrlist_Size", Taldren_Ftrlist_Size, OPDir & "\SFCOP Mod Chooser\filedata.ini")
    ProgressBarForm.ProgressBar1.Value = 60
    
    Call fso.CopyFile(OPDir & "\Assets\Specs\shiplist.txt", OPDir & "\SFCOP Mod Chooser\Bin\Taldren\Specs\Shiplist.txt", True)
    ProgressBarForm.ProgressBar1.Value = 64
    Call fso.CopyFile(OPDir & "\Assets\Specs\ftrlist.txt", OPDir & "\SFCOP Mod Chooser\Bin\Taldren\Specs\ftrlist.txt", True)
    ProgressBarForm.ProgressBar1.Value = 68
    Call fso.CopyFile(OPDir & "\Assets\Models\model.siz", OPDir & "\SFCOP Mod Chooser\Bin\Taldren\Models\model.siz", True)
    ProgressBarForm.ProgressBar1.Value = 72
    Call fso.CopyFolder(OPDir & "\Assets\Textures", OPDir & "\SFCOP Mod Chooser\Bin\Taldren\", True)
    ProgressBarForm.ProgressBar1.Value = 76
    Call fso.CopyFolder(OPDir & "\Assets\Mouse", OPDir & "\SFCOP Mod Chooser\Bin\Taldren\", True)
    ProgressBarForm.ProgressBar1.Value = 80
    Call fso.CopyFolder(OPDir & "\Assets\Strings", OPDir & "\SFCOP Mod Chooser\Bin\Taldren\", True)
    ProgressBarForm.ProgressBar1.Value = 82
    Call fso.CopyFolder(OPDir & "\MetaAssets\ServerProfiles\SinglePlayer", OPDir & "\SFCOP Mod Chooser\Bin\Taldren\GFs", True)
    ProgressBarForm.ProgressBar1.Value = 85
    Call fso.CopyFile(OPDir & "\Assets\Sound\SFCSounds.zip", OPDir & "\SFCOP Mod Chooser\Bin\Taldren\Sound\SFCSounds.zip", True)
    ProgressBarForm.ProgressBar1.Value = 89
    Call fso.CopyFile(OPDir & "\Assets\Sound\SFCVoice.zip", OPDir & "\SFCOP Mod Chooser\Bin\Taldren\Sound\SFCVoice.zip", True)
    ProgressBarForm.ProgressBar1.Value = 92
    Call fso.CopyFile(OPDir & "\Assets\Sound\sounds.txt", OPDir & "\SFCOP Mod Chooser\Bin\Taldren\Sound\Sounds.txt", True)
    ProgressBarForm.ProgressBar1.Value = 96
    Call WritePrivateProfileString("NewInstall", "Status", "False", OPDir & "\SFCOP Mod Chooser\filedata.ini")
    ProgressBarForm.ProgressBar1.Value = 100
    ProgressBarForm.Visible = False
End If

End Function

Public Function Reset()
Unload MainWindow
Call Load_INI
Load MainWindow
MainWindow.Show
End Function
