Skip to content

Commit

Permalink
Added option to create junction links for CustomLevels and UserData
Browse files Browse the repository at this point in the history
  • Loading branch information
bleuthoot-sven committed May 5, 2022
1 parent 53fda09 commit 60b483e
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 7 deletions.
11 changes: 7 additions & 4 deletions LegacyInstaller/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,23 @@
mc:Ignorable="d"
ResizeMode="CanMinimize"
Title="LegacyInstaller" Height="245" Width="410" Background="#FFF0F0F0">
<Grid>
<Grid ToolTip="Create a link">
<Label x:Name="label2" Content="Beat Saber install folder:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,3,0,0" FontSize="10.5"/>
<TextBox x:Name="bsPathTextBox" TextChanged="bsPathTextBox_TextChanged" HorizontalAlignment="Left" Height="20" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="275" Margin="12,25,0,0"/>
<TextBox x:Name="bsPathTextBox" TextChanged="bsPathTextBox_TextChanged" HorizontalAlignment="Left" Height="20" TextWrapping="Wrap" VerticalAlignment="Top" Width="275" Margin="12,25,0,0"/>
<Button x:Name="bsPathBrowseButton" Click="bsPathBrowseButton_Click" Content="Browse..." HorizontalAlignment="Left" Cursor="Hand" VerticalAlignment="Top" Width="89" Margin="294,25,0,0"/>

<Label x:Name="label3" Content="Steam install folder:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,49,0,0" FontSize="10.5"/>
<TextBox x:Name="steamPathTextBox" TextChanged="steamPathTextBox_TextChanged" HorizontalAlignment="Left" Height="20" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="275" Margin="12,70,0,0"/>
<TextBox x:Name="steamPathTextBox" TextChanged="steamPathTextBox_TextChanged" HorizontalAlignment="Left" Height="20" TextWrapping="Wrap" VerticalAlignment="Top" Width="275" Margin="12,70,0,0"/>
<Button x:Name="steamPathBrowseButton" Click="steamPathBrowseButton_Click" Content="Browse..." HorizontalAlignment="Left" Cursor="Hand" VerticalAlignment="Top" Width="89" Margin="294,70,0,0"/>

<Label x:Name="label1" Content="Version:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,103,0,0" FontSize="10.7"/>
<ComboBox x:Name="versionDropdown" SelectionChanged="versionDropdown_SelectedIndexChanged" HorizontalAlignment="Left" Height="20" Text="TextBox" VerticalAlignment="Top" Width="121" Margin="62,106,0,0"/>
<Button x:Name="installButton" Content="Install" Click="installButton_Click" HorizontalAlignment="Left" Height="20" Cursor="Hand" FontSize="11.5" VerticalAlignment="Top" Width="89" Margin="190,106,0,0"/>
<Label x:Name="installStateLabel" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="281,104,0,0" FontSize="10.5"/>

<Label x:Name="downloadInfoLabel" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,140,0,0" FontSize="11" Initialized="downloadInfoLabel_Initialized"/>
<CheckBox x:Name="customLevelsLinkCheckbox" Content="Create link to custom levels" HorizontalAlignment="Left" Margin="10,132,0,0" VerticalAlignment="Top" IsChecked="True" ToolTip="Create a link to your main CustomLevels folder so that the custom songs are syned between installations."/>
<CheckBox x:Name="userDataLinkCheckbox" Content="Create link to user data" HorizontalAlignment="Left" Margin="10,152,0,0" VerticalAlignment="Top" ToolTip="Create a link to your main UserData folder so that your mod settings are syned between installations. Might cause issues very old versions of mods are installed."/>

<Label x:Name="downloadInfoLabel" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,181,0,0" FontSize="11" Initialized="downloadInfoLabel_Initialized"/>
</Grid>
</Window>
56 changes: 53 additions & 3 deletions LegacyInstaller/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ private void RefreshUI(bool idle = true)
downloadInfoLabelObject.Content = labelText;
}
}

private async Task RefreshInternal()
{
if (_steamProcess == null || BSInstallDir == null)
Expand Down Expand Up @@ -207,8 +208,6 @@ private void steamPathTextBox_TextChanged(object sender, TextChangedEventArgs e)
_steamProcess = null;
}



private void versionDropdown_SelectedIndexChanged(object sender, SelectionChangedEventArgs e)
{
SelectedVersion = (Version)versionDropdown.SelectedItem;
Expand All @@ -223,7 +222,26 @@ private void installButton_Click(object sender, RoutedEventArgs e)
else
{
_steamProcess.Shortcuts.DeleteSteamShortcut($"Beat Saber {SelectedVersion.BSVersion}");
Directory.Delete(SelectedVersionInstallDir, true);

try
{
// First delete junction links
string customLevelsJunctionPath = Path.Combine(SelectedVersionInstallDir, "Beat Saber_Data", "CustomLevels");
if (Directory.Exists(customLevelsJunctionPath) && Utilities.IsJunctionLink(customLevelsJunctionPath))
Directory.Delete(customLevelsJunctionPath);

string userDataJunctionLink = Path.Combine(SelectedVersionInstallDir, "UserData");
if (Directory.Exists(userDataJunctionLink) && Utilities.IsJunctionLink(userDataJunctionLink))
Directory.Delete(userDataJunctionLink);

// Delete folder
Directory.Delete(SelectedVersionInstallDir, true);
}
catch
{
string alertMessageContent = $"Uninstall failed, please check if the files have been deleted\n${SelectedVersionInstallDir}";
_ = MessageBox.Show(alertMessageContent, "Uninstall failed", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}

_ = RestartSteam();
Expand Down Expand Up @@ -263,6 +281,33 @@ private async Task InstallVersion(Version version)

// Enable UI
this.Dispatcher.Invoke((Action)delegate { RefreshUI(true); });

// Create Junction Links for custom levels
if (customLevelsLinkCheckbox.IsChecked.GetValueOrDefault())
{
string relativeDir = Path.Combine("Beat Saber_Data", "CustomLevels");
string sourceDir = Path.Combine(BSInstallDir, relativeDir);
string targetDir = Path.Combine(SelectedVersionInstallDir, relativeDir);

// Remove existing target dir folder
if (Directory.Exists(targetDir))
Directory.Delete(targetDir, true);

Utilities.CreateJunctionLink(targetDir, sourceDir);
}

// Create Junction Links for user data
if (userDataLinkCheckbox.IsChecked.GetValueOrDefault())
{
string relativeDir = Path.Combine("UserData");
string sourceDir = Path.Combine(BSInstallDir, relativeDir);
string targetDir = Path.Combine(SelectedVersionInstallDir, relativeDir);

if (Directory.Exists(targetDir))
Directory.Delete(targetDir, true);

Utilities.CreateJunctionLink(targetDir, sourceDir);
}
}

private void FileSystemChanged(object sender, FileSystemEventArgs e)
Expand Down Expand Up @@ -291,5 +336,10 @@ private void downloadInfoLabel_Initialized(object sender, EventArgs e)
downloadInfoLabelObject = (Label)sender;
RefreshUI();
}

private void CheckBox_Checked(object sender, RoutedEventArgs e)
{

}
}
}
30 changes: 30 additions & 0 deletions LegacyInstaller/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,35 @@ public static UInt64 GetSteamAppId(string appName, string path, string exe)

return gameId;
}

public static void CreateJunctionLink(string destinationPath, string sourcePath)
{
using (System.Diagnostics.Process process = new System.Diagnostics.Process())
{
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = $" /C mklink /J \"{destinationPath}\" \"{sourcePath}\"";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += Process_OutputDataReceived;

process.Start();

Console.WriteLine(process.StandardOutput.ReadToEnd());

process.WaitForExit();
}
}

private static void Process_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
{
Console.WriteLine(e.Data);
}

public static bool IsJunctionLink(string path)
{
FileInfo pathInfo = new FileInfo(path);
return pathInfo.Attributes.HasFlag(FileAttributes.ReparsePoint);
}
}
}

0 comments on commit 60b483e

Please sign in to comment.