VERSION 5.00
Begin VB.Form Form1 
   Caption         =   "Presetup"
   ClientHeight    =   870
   ClientLeft      =   1020
   ClientTop       =   315
   ClientWidth     =   4680
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   870
   ScaleWidth      =   4680
   ShowInTaskbar   =   0   'False
   StartUpPosition =   2  'CenterScreen
   Begin VB.TextBox Text1 
      Height          =   375
      Left            =   240
      Locked          =   -1  'True
      MultiLine       =   -1  'True
      TabIndex        =   0
      Text            =   "Form65.frx":0000
      Top             =   240
      Width           =   4215
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private Const S_OK = &H0                ' Success
Private Const S_FALSE = &H1             ' The Folder is valid, but does not exist
Private Const E_INVALIDARG = &H80070057 ' Invalid CSIDL Value

Private Const CSIDL_LOCAL_APPDATA = &H1C&
Private Const CSIDL_FLAG_CREATE = &H8000&
Private Const CSIDL_PROGRAMS = &H2&

Private Const SHGFP_TYPE_CURRENT = 0
Private Const SHGFP_TYPE_DEFAULT = 1
Private Const MAX_PATH = 260

Private Declare Function SHGetFolderPath Lib "shfolder" _
    Alias "SHGetFolderPathA" _
    (ByVal hwndOwner As Long, ByVal nFolder As Long, _
    ByVal hToken As Long, ByVal dwFlags As Long, _
    ByVal pszPath As String) As Long
    
Dim sPath As String  ' Global variable holding the "Start Menu\Programs" Path for the local computer
Dim OPDir As Variant ' Makes the String Varaible Containing OP Install Directory a Global Variable


Private Sub Find_OP_Dir()
'  Locates the installation directory of OP from Windows Registry
' stringRegKey = "HKEY_LOCAL_MACHINE\Software\Taldren\Starfleet Command Orion Pirates\Directory"


QueryValue "Software\Taldren\Starfleet Command Orion Pirates", "Directory"

End Sub
'  Function needed to pull information from Windows Registry
Private Sub QueryValue(sKeyName As String, sValueName As String)
       Dim lRetVal As Long         'result of the API functions
       Dim hKey As Long         'handle of opened key
       Dim vValue As Variant      'setting of queried value

       lRetVal = RegOpenKeyEx(HKEY_LOCAL_MACHINE, sKeyName, 0, _
   KEY_QUERY_VALUE, hKey)
       lRetVal = QueryValueEx(hKey, sValueName, vValue)
       OPDir = vValue
       RegCloseKey (hKey)
End Sub

Private Sub Delete_This()
Dim fso
Set fso = CreateObject("Scripting.filesystemobject")
' Note here:  Last variable is true or false, this indicates to the function weather
' or not to delete the folder after it has delelted the file.  In some cases You may want to
' delete the parent folder, to rid of folders installed by this program, but in other cases
' some folders are used by SFCOP itself
' Delete existing Model.siz files, and any old folders from previous instsallations
Call Delete_Folder("Assets\Models\OPPlus")
Call Delete_Folder("Assets\Models\Taldren")
Call Delete_Folder("Assets\Models\OtherMod")
Call Delete_Folder("Assets\Models\OPPlus31")
Call Delete_Folder("Assets\Models\OPPlus32")
'  Deletes old presetup folder and files
Call Delete_Folder("Assets\Presetup")
'  Deletes old lists and markers
Call Delete_File("Assets\Specs\is*.txt")
Call Delete_File("Assets\Specs\*.mrk")
'  Deletes existing folders holding old lists
Call Delete_Folder("Assets\Specs\OtherMod")
Call Delete_Folder("Assets\Specs\OPPlus")
Call Delete_Folder("Assets\Specs\OPPlus31")
Call Delete_Folder("Assets\Specs\OPPlus32")
Call Delete_Folder("Assets\Specs\SW")
Call Delete_Folder("Assets\Specs\SWBeta")
Call Delete_Folder("Assets\Specs\Taldren")
Call Delete_Folder("Assets\Specs\SFCOP Shiplist Changer")
'  Deletes old program files directories
Call Delete_Folder("SFCOP Mod Chooser")
'Call Delete_Folder("Assets\Specs\SFCOP Shiplist Changer")
'Call Delete_Folder("Assets\Specs\Shipwrights Program Files")
' Deletes old string files
Call Delete_Folder("Assets\Strings\OPPlus")
Call Delete_Folder("Assets\Strings\Taldren")
Call Delete_Folder("Assets\Strings\OtherMod")
Call Delete_Folder("Assets\Strings\OPPlus31")
Call Delete_Folder("Assets\Strings\OPPlus32")
' Deletes everything in backup folder
Call Delete_Folder("Assets\Specs\Backup")
' Deletes old start menu shorcuts and folders
Call Delete_FolderNotOP(sPath & "\Shipwrights Shiplist Chooser")
Call Delete_FolderNotOP(sPath & "\SFCOP Shiplist Chooser")
Call Delete_FolderNotOP(sPath & "\SFCOP Mod Chooser")
End Sub

Private Sub Delete_Folder(Dir As String)
On Error Resume Next
Dim fso
Set fso = CreateObject("Scripting.filesystemobject")
' Check to see that the files being deleted exist
Call fso.deletefolder(OPDir & "\" & Dir, True)
End Sub
Private Sub Delete_FolderNotOP(Dir As String)
On Error Resume Next
Dim fso
Set fso = CreateObject("Scripting.filesystemobject")
' Check to see that the files being deleted exist
Call fso.deletefolder(Dir, True)
End Sub
Private Sub Delete_File(Dir As String)
Dim fso
Set fso = CreateObject("Scripting.filesystemobject")
' Check to see that the files being deleted exist
Call fso.deletefile(OPDir & "\" & Dir, True)
End Sub

Private Sub Form_Load()
Call Set_Programs_Folder
' The line below is used to forward the processes to a test installation of the spec
' chooser for testing the presetup program
' OPDir = "C:\SpecChoosertest\"
Call Find_OP_Dir
Call Delete_This
End
End Sub
Private Sub Set_Programs_Folder()
' This function find the "Start Menu\Programs" directory for the local computer and saves it to
' the global variable sPath
Dim RetVal As Long

' Fill our string buffer
sPath = String(MAX_PATH, 0)

RetVal = SHGetFolderPath(0, CSIDL_PROGRAMS Or CSIDL_FLAG_CREATE, 0, SHGFP_TYPE_CURRENT, sPath)

Select Case RetVal
    Case S_OK
        ' We retrieved the folder successfully
        
        ' All C strings are null terminated
        ' So we need to return the string upto the first null character
        sPath = Left(sPath, InStr(1, sPath, Chr(0)) - 1)
    Case S_FALSE
        ' The CSIDL in nFolder is valid, but the folder does not exist.
        ' Use CSIDL_FLAG_CREATE to have it created automatically
        MsgBox "The folder does not exist"
    Case E_INVALIDARG
        ' nFolder is invalid
        MsgBox "An invalid folder ID was specified"
    
End Select
End Sub
