2018-02-18 09:26:35 +08:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
using System.IO ;
using System.Windows.Forms ;
using CodeWalker.Properties ;
2019-10-28 14:15:56 +08:00
using Microsoft.Win32 ;
2018-02-18 09:26:35 +08:00
namespace CodeWalker
{
public static class GTAFolder
{
public static string CurrentGTAFolder { get ; private set ; } = Settings . Default . GTAFolder ;
public static bool ValidateGTAFolder ( string folder , out string failReason )
{
failReason = "" ;
if ( string . IsNullOrWhiteSpace ( folder ) )
{
failReason = "No folder specified" ;
return false ;
}
if ( ! Directory . Exists ( folder ) )
{
failReason = $"Folder \" { folder } \ " does not exist" ;
return false ;
}
if ( ! File . Exists ( folder + @"\gta5.exe" ) )
{
failReason = $"GTA5.exe not found in folder \" { folder } \ "" ;
return false ;
}
return true ;
}
public static bool ValidateGTAFolder ( string folder ) = > ValidateGTAFolder ( folder , out string reason ) ;
public static bool IsCurrentGTAFolderValid ( ) = > ValidateGTAFolder ( CurrentGTAFolder ) ;
public static bool UpdateGTAFolder ( bool UseCurrentIfValid = false )
{
if ( UseCurrentIfValid & & IsCurrentGTAFolderValid ( ) )
{
return true ;
}
2018-02-21 17:12:17 +08:00
string origFolder = CurrentGTAFolder ;
2018-02-18 09:26:35 +08:00
string folder = CurrentGTAFolder ;
SelectFolderForm f = new SelectFolderForm ( ) ;
2019-10-28 14:15:56 +08:00
2019-10-28 14:39:25 +08:00
string autoFolder = AutoDetectFolder ( out string source ) ;
if ( autoFolder ! = null & & MessageBox . Show ( $"Auto-detected game folder \" { autoFolder } \ " from {source}.\n\nContinue with auto-detected folder?" , "Auto-detected game folder" , MessageBoxButtons . YesNo , MessageBoxIcon . Question , MessageBoxDefaultButton . Button1 ) = = DialogResult . Yes )
2019-10-28 14:15:56 +08:00
{
f . SelectedFolder = autoFolder ;
}
2018-02-18 09:26:35 +08:00
f . ShowDialog ( ) ;
if ( f . Result = = DialogResult . OK & & Directory . Exists ( f . SelectedFolder ) )
{
folder = f . SelectedFolder ;
}
string failReason ;
if ( ValidateGTAFolder ( folder , out failReason ) )
{
SetGTAFolder ( folder ) ;
2018-02-21 17:12:17 +08:00
if ( folder ! = origFolder )
{
MessageBox . Show ( $"Successfully changed GTA Folder to \" { folder } \ "" , "Set GTA Folder" , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
}
2018-02-18 09:26:35 +08:00
return true ;
} else
{
2018-02-21 16:34:49 +08:00
var tryAgain = MessageBox . Show ( $"Folder \" { folder } \ " is not a valid GTA folder:\n\n{failReason}\n\nDo you want to try choosing a different folder?" , "Unable to set GTA Folder" , MessageBoxButtons . RetryCancel , MessageBoxIcon . Question , MessageBoxDefaultButton . Button1 ) ;
2018-02-18 09:26:35 +08:00
if ( tryAgain = = DialogResult . Retry )
{
return UpdateGTAFolder ( false ) ;
} else
{
return false ;
}
}
}
public static bool SetGTAFolder ( string folder )
{
if ( ValidateGTAFolder ( folder ) )
{
CurrentGTAFolder = folder ;
Settings . Default . GTAFolder = folder ;
2018-02-24 21:59:00 +08:00
Settings . Default . Save ( ) ;
2018-02-18 09:26:35 +08:00
return true ;
}
return false ;
}
2018-02-18 17:20:39 +08:00
public static string GetCurrentGTAFolderWithTrailingSlash ( ) = > CurrentGTAFolder . EndsWith ( @"\" ) ? CurrentGTAFolder : CurrentGTAFolder + @"\" ;
2019-10-28 14:39:25 +08:00
public static bool AutoDetectFolder ( out Dictionary < string , string > matches )
2019-10-28 14:15:56 +08:00
{
2019-10-28 14:39:25 +08:00
matches = new Dictionary < string , string > ( ) ;
if ( ValidateGTAFolder ( CurrentGTAFolder ) )
{
matches . Add ( "Current CodeWalker Folder" , CurrentGTAFolder ) ;
}
2019-10-28 14:15:56 +08:00
2019-10-28 14:28:15 +08:00
RegistryKey baseKey32 = RegistryKey . OpenBaseKey ( RegistryHive . LocalMachine , RegistryView . Registry32 ) ;
string steamPathValue = baseKey32 . OpenSubKey ( @"Software\Rockstar Games\GTAV" ) ? . GetValue ( "InstallFolderSteam" ) as string ;
string retailPathValue = baseKey32 . OpenSubKey ( @"Software\Rockstar Games\Grand Theft Auto V" ) ? . GetValue ( "InstallFolder" ) as string ;
string oivPathValue = Registry . CurrentUser . OpenSubKey ( @"Software\NewTechnologyStudio\OpenIV.exe\BrowseForFolder" ) ? . GetValue ( "game_path_Five_pc" ) as string ;
2019-10-28 14:15:56 +08:00
2019-10-28 14:28:15 +08:00
if ( steamPathValue ? . EndsWith ( "\\GTAV" ) = = true )
2019-10-28 14:15:56 +08:00
{
2019-10-28 14:28:15 +08:00
steamPathValue = steamPathValue . Substring ( 0 , steamPathValue . LastIndexOf ( "\\GTAV" ) ) ;
}
2019-10-28 14:15:56 +08:00
2019-10-28 14:28:15 +08:00
if ( ValidateGTAFolder ( steamPathValue ) )
{
2019-10-28 14:39:25 +08:00
matches . Add ( "Steam" , steamPathValue ) ;
2019-10-28 14:15:56 +08:00
}
2019-10-28 14:28:15 +08:00
if ( ValidateGTAFolder ( retailPathValue ) )
2019-10-28 14:15:56 +08:00
{
2019-10-28 14:39:25 +08:00
matches . Add ( "Retail" , retailPathValue ) ;
2019-10-28 14:15:56 +08:00
}
2019-10-28 14:28:15 +08:00
if ( ValidateGTAFolder ( oivPathValue ) )
{
2019-10-28 14:39:25 +08:00
matches . Add ( "OpenIV" , oivPathValue ) ;
2019-10-28 14:28:15 +08:00
}
2019-10-28 14:39:25 +08:00
return matches . Count > 0 ;
2019-10-28 14:15:56 +08:00
}
2019-10-28 14:39:25 +08:00
public static string AutoDetectFolder ( out string source )
2019-10-28 14:15:56 +08:00
{
2019-10-28 14:39:25 +08:00
source = null ;
if ( AutoDetectFolder ( out Dictionary < string , string > matches ) )
2019-10-28 14:15:56 +08:00
{
2019-10-28 14:39:25 +08:00
var match = matches . First ( ) ;
source = match . Key ;
return match . Value ;
2019-10-28 14:15:56 +08:00
}
return null ;
}
2019-10-28 14:39:25 +08:00
public static string AutoDetectFolder ( ) = > AutoDetectFolder ( out string _ ) ;
2018-02-18 09:26:35 +08:00
}
}