Skip to content

Commit

Permalink
Tweak Target Validation
Browse files Browse the repository at this point in the history
  • Loading branch information
MirisWisdom committed Feb 14, 2021
2 parents db095cf + 7eef25f commit 7e954f1
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 123 deletions.
6 changes: 2 additions & 4 deletions hxe/kernel/src/HXE.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,23 @@
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>x64</PlatformTarget>
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>..\bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32bit>false</Prefer32bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>x64</PlatformTarget>
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32bit>false</Prefer32bit>
</PropertyGroup>
<PropertyGroup>
<ApplicationIcon>Assets\icon.ico</ApplicationIcon>
Expand Down
228 changes: 113 additions & 115 deletions spv3/loader/src/Install.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,120 +121,7 @@ public string Target
if (value == _target) return;
_target = value;
OnPropertyChanged();

/**
* Check validity of the specified target value.
*/
if (string.IsNullOrEmpty(Target) || !Directory.Exists(Target))
{
Status = "Enter a valid path.";
CanInstall = false;
return;
}

if (Target.Contains(GetFolderPath(ProgramFiles))
|| !string.IsNullOrEmpty(GetFolderPath(ProgramFilesX86)) && Target.Contains(GetFolderPath(ProgramFilesX86)) )
{
Status = "The game does not function correctly when install to Program Files. Please choose a difference location.";
CanInstall = false;
return;
}


try
{
var exists = Directory.Exists(Target);
var path = Target;
var rootExists = Directory.Exists(Path.GetPathRoot(Target));

if (!exists && !rootExists)
{
throw new DirectoryNotFoundException(Target);
}
if (!exists && rootExists)
{
while (!Directory.Exists(path))
{
path = Directory.GetParent(path).Name;
if (path == "Debug") return;
}
}

// if Target and Root exist...
_target = Path.GetFullPath(_target);
value = Path.GetFullPath(value);
var test = Path.Combine(path, "io.bin");
WriteAllBytes(test, new byte[8]);
Delete(test);

Status = "Waiting for user to install SPV3.";
CanInstall = true;
}
catch (Exception e)
{
var msg = "Installation not possible at selected path: " + Target + "\n Error: " + e.ToString() + "\n";
var log = (HXE.File)Paths.Exception;
log.AppendAllText(msg);
Status = msg;
CanInstall = false;
return;
}

/**
* Check available disk space. This will NOT work on UNC paths!
*/
try
{

/** First, check the C:\ drive to ensure there's enough free space
* for temporary extraction to %temp% */
if (Directory.Exists(@"C:\"))
{
var systemDrive = new DriveInfo(@"C:\");
if (systemDrive.TotalFreeSpace < 10737418240)
{
Status = @"Not enough disk space (10GB required) on the C:\ drive. " +
"Clear junk files using Disk Cleanup or allocate more space to the volume";
CanInstall = false;
return;
}
}

/**
* Check if the target drive has at least 16GB of free space
*/
var targetDrive = new DriveInfo(Path.GetPathRoot(Target));

if (targetDrive.IsReady && targetDrive.TotalFreeSpace > 17179869184)
{
CanInstall = true;
}
else
{
Status = "Not enough disk space (16GB required) at selected path: " + Target;
CanInstall = false;
return;
}
}
catch (Exception e)
{
var msg = "Failed to get drive space.\n Error: " + e.ToString() + "\n";
var log = (HXE.File)Paths.Exception;
log.AppendAllText(msg);
Status = msg;
CanInstall = false;
}

/**
* Prohibit installations to known problematic folders.
*/
if (Exists(Path.Combine(Target, HXE.Paths.HCE.Executable))
|| Exists(Path.Combine(Target, HXE.Paths.Executable))
|| Exists(Path.Combine(Target, Paths.Executable)))
{
Status = "Selected folder contains existing HCE or SPV3 data. Please choose a different location.";
CanInstall = false;
}
ValidateTarget(value);
}
}

Expand Down Expand Up @@ -278,10 +165,11 @@ public void Initialise()
Main = Visible;
Activation = Collapsed;

ValidateTarget(Target);

/**
* Determine if the current environment fulfills the installation requirements.
*/

if (Registry.GameExists("Custom")
|| Registry.GameExists("Retail")
|| ( Kernel.hxe.Tweaks.Patches & Patcher.EXEP.DISABLE_DRM_AND_KEY_CHECKS) == 1)
Expand Down Expand Up @@ -455,6 +343,116 @@ public void CheckSteamPath(string exe)
Update_SteamStatus();
}

public void ValidateTarget(string path)
{
/**
* Check validity of the specified target value.
*/
if (string.IsNullOrEmpty(path)
|| !Directory.Exists(Path.GetPathRoot(path)))
{
Status = "Enter a valid path.";
CanInstall = false;
return;
}

/**
* Check if path contains Program Files or Program Files (x86)
*/
if (path.Contains(GetFolderPath(ProgramFiles))
|| !string.IsNullOrEmpty(GetFolderPath(ProgramFilesX86))
&& path.Contains(GetFolderPath(ProgramFilesX86)))
{
Status = "The game does not function correctly when install to Program Files. Please choose a difference location.";
CanInstall = false;
return;
}

try
{
var exists = Directory.Exists(path);
var root = Path.GetPathRoot(path);
var rootExists = Directory.Exists(root);

if (!exists && rootExists)
{
while (!Directory.Exists(path))
{
path = Directory.GetParent(path).FullName;
if (path == CurrentDirectory)
{
Status = "Enter a valid path.";
CanInstall = false;
return;
}
}
}

// if Target and Root exist...
path = Path.GetFullPath(path);
var test = Path.Combine(path, "io.bin");
WriteAllBytes(test, new byte[8]);
Delete(test);

Status = "Waiting for user to install SPV3.";
CanInstall = true;
}
catch (Exception e)
{
var msg = "Installation not possible at selected path: " + path + "\n Error: " + e.ToString() + "\n";
var log = (HXE.File) Paths.Exception;
log.AppendAllText(msg);
Status = msg;
CanInstall = false;
return;
}

/**
* Check available disk space. This will NOT work on UNC paths!
*/
try
{
/** First, check the user's temp folder's drive to ensure there's enough free space
* for temporary extraction to %temp%
*/
{
var tmpath = Path.GetPathRoot(Path.GetTempPath());
var systemDrive = new DriveInfo(tmpath);
if (systemDrive.TotalFreeSpace < 11811160064)
{
Status = $"Not enough disk space (11GB required) on the {tmpath} drive. " +
"Clear junk files using Disk Cleanup or allocate more space to the volume";
CanInstall = false;
return;
}
}

/**
* Check if the target drive has at least 16GB of free space
*/
var targetDrive = new DriveInfo(Path.GetPathRoot(path));

if (targetDrive.IsReady && targetDrive.TotalFreeSpace > 17179869184)
{
CanInstall = true;
}
else
{
Status = "Not enough disk space (16GB required) at selected path: " + path;
CanInstall = false;
return;
}
}
catch (Exception e)
{
var msg = "Failed to get drive space.\n Error: " + e.ToString() + "\n";
var log = (HXE.File)Paths.Exception;
log.AppendAllText(msg);
Status = msg;
CanInstall = false;
}
}

public void ViewActivation() // Debug widget
{
Main = Collapsed;
Expand Down
6 changes: 2 additions & 4 deletions spv3/loader/src/SPV3.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,23 @@
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>x64</PlatformTarget>
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>..\bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32bit>false</Prefer32bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>x64</PlatformTarget>
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32bit>false</Prefer32bit>
</PropertyGroup>
<PropertyGroup>
<ApplicationIcon>Assets\icon.ico</ApplicationIcon>
Expand Down

0 comments on commit 7e954f1

Please sign in to comment.