Skip to content

Commit

Permalink
Merge #4304 Checkboxes to copy more dirs when cloning instances
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Feb 7, 2025
2 parents 793869e + ef61996 commit e7b6e03
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ All notable changes to this project will be documented in this file.
- [CLI] Include `supports` relationship in `ckan show` (#4262 by: HebaruSan)
- [Multiple] Pre-release handling (#4260, #4266, #4294 by: HebaruSan, JonnyOThan)
- [Multiple] French translation updates (#4268 by: HebaruSan)
- [Multiple] Checkboxes to copy more dirs when cloning instances (#4304 by: HebaruSan)

### Bugfixes

Expand Down
27 changes: 26 additions & 1 deletion Core/GameInstanceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ public GameInstance AddInstance(GameInstance instance)
/// <param name="existingInstance">The KSP instance to clone.</param>
/// <param name="newName">The name for the new instance.</param>
/// <param name="newPath">The path where the new instance should be located.</param>
/// <param name="shareStockFolders">True to make junctions or symlinks to stock folders instead of copying</param>
/// <exception cref="InstanceNameTakenKraken">Thrown if the instance name is already in use.</exception>
/// <exception cref="NotKSPDirKraken">Thrown by AddInstance() if created instance is not valid, e.g. if something went wrong with copying.</exception>
/// <exception cref="DirectoryNotFoundKraken">Thrown by CopyDirectory() if directory doesn't exist. Should never be thrown here.</exception>
Expand All @@ -252,6 +253,30 @@ public void CloneInstance(GameInstance existingInstance,
string newName,
string newPath,
bool shareStockFolders = false)
{
CloneInstance(existingInstance, newName, newPath,
existingInstance.game.LeaveEmptyInClones,
shareStockFolders);
}

/// <summary>
/// Clones an existing KSP installation.
/// </summary>
/// <param name="existingInstance">The KSP instance to clone.</param>
/// <param name="newName">The name for the new instance.</param>
/// <param name="newPath">The path where the new instance should be located.</param>
/// <param name="leaveEmpty">Dirs whose contents should not be copied</param>
/// <param name="shareStockFolders">True to make junctions or symlinks to stock folders instead of copying</param>
/// <exception cref="InstanceNameTakenKraken">Thrown if the instance name is already in use.</exception>
/// <exception cref="NotKSPDirKraken">Thrown by AddInstance() if created instance is not valid, e.g. if something went wrong with copying.</exception>
/// <exception cref="DirectoryNotFoundKraken">Thrown by CopyDirectory() if directory doesn't exist. Should never be thrown here.</exception>
/// <exception cref="PathErrorKraken">Thrown by CopyDirectory() if the target folder already exists and is not empty.</exception>
/// <exception cref="IOException">Thrown by CopyDirectory() if something goes wrong during the process.</exception>
public void CloneInstance(GameInstance existingInstance,
string newName,
string newPath,
string[] leaveEmpty,
bool shareStockFolders = false)
{
if (HasInstance(newName))
{
Expand All @@ -267,7 +292,7 @@ public void CloneInstance(GameInstance existingInstance,
Utilities.CopyDirectory(existingInstance.GameDir(), newPath,
shareStockFolders ? existingInstance.game.StockFolders
: Array.Empty<string>(),
existingInstance.game.LeaveEmptyInClones);
leaveEmpty);

// Add the new instance to the config
AddInstance(new GameInstance(existingInstance.game, newPath, newName, User));
Expand Down
43 changes: 40 additions & 3 deletions GUI/Dialogs/CloneGameInstanceDialog.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 23 additions & 5 deletions GUI/Dialogs/CloneGameInstanceDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,21 @@ public CloneGameInstanceDialog(GameInstanceManager manager,

private void comboBoxKnownInstance_SelectedIndexChanged(object? sender, EventArgs? e)
{
textBoxClonePath.Text = comboBoxKnownInstance.SelectedItem is string sel
&& !string.IsNullOrEmpty(sel)
? Platform.FormatPath(manager.Instances[sel].GameDir())
: "";
if (comboBoxKnownInstance.SelectedItem is string sel
&& !string.IsNullOrEmpty(sel))
{
var inst = manager.Instances[sel];
textBoxClonePath.Text = Platform.FormatPath(inst.GameDir());
OptionalPathsListView.Items.Clear();
OptionalPathsListView.Items.AddRange(
inst.game.LeaveEmptyInClones
.OrderBy(path => path,
StringComparer.OrdinalIgnoreCase)
.Select(path => new ListViewItem(path) { Tag = path })
.ToArray());
OptionalPathsLabel.Visible = OptionalPathsListView.Visible =
OptionalPathsListView.Items.Count > 0;
}
}

/// <summary>
Expand Down Expand Up @@ -143,7 +154,14 @@ await Task.Run(() =>
{
if (instanceToClone.Valid)
{
manager.CloneInstance(instanceToClone, newName, newPath, checkBoxShareStock.Checked);
manager.CloneInstance(instanceToClone, newName, newPath,
OptionalPathsListView.Items
.OfType<ListViewItem>()
.Where(lvi => !lvi.Checked)
.Select(lvi => lvi.Tag)
.OfType<string>()
.ToArray(),
checkBoxShareStock.Checked);
}
else
{
Expand Down
2 changes: 2 additions & 0 deletions GUI/Dialogs/CloneGameInstanceDialog.resx
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@
<data name="checkBoxSetAsDefault.Text" xml:space="preserve"><value>Set new instance as default</value></data>
<data name="checkBoxSwitchInstance.Text" xml:space="preserve"><value>Switch to new instance</value></data>
<data name="checkBoxShareStock.Text" xml:space="preserve"><value>Share stock files</value></data>
<data name="OptionalPathsLabel.Text" xml:space="preserve"><value>Optional paths to copy:</value></data>
<data name="PathHeader.Text" xml:space="preserve"><value>Optional paths</value></data>
<data name="buttonOK.Text" xml:space="preserve"><value>Clone</value></data>
<data name="buttonCancel.Text" xml:space="preserve"><value>Cancel</value></data>
<data name="$this.Text" xml:space="preserve"><value>Clone Game Instance</value></data>
Expand Down

0 comments on commit e7b6e03

Please sign in to comment.