Skip to content

Commit

Permalink
Properly determine the game when cloning instances
Browse files Browse the repository at this point in the history
  • Loading branch information
DasSkelett committed Nov 17, 2021
1 parent 83f54bf commit b90b031
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Cmdline/ConsoleUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public bool RaiseYesNoDialog(string question)
/// The output is index 0 based.
/// To supply a default option, make the first option an integer indicating the index of it.
/// </summary>
/// <returns>The selection dialog</returns>
/// <returns>The selected index or -1 if cancelled</returns>
/// <param name="message">Message</param>
/// <param name="args">Array of available options</param>
public int RaiseSelectionDialog(string message, params object[] args)
Expand Down
74 changes: 47 additions & 27 deletions Core/GameInstanceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public GameInstanceManager(IUser user, IConfiguration configuration = null)
}

/// <summary>
/// Returns the preferred KSP instance, or null if none can be found.
/// Returns the preferred game instance, or null if none can be found.
///
/// This works by checking to see if we're in a KSP dir first, then the
/// config for an autostart instance, then will try to auto-populate
Expand Down Expand Up @@ -170,45 +170,40 @@ public GameInstance FindAndRegisterDefaultInstance()
}

/// <summary>
/// Adds a KSP instance to config.
/// Returns the resulting KSP object.
/// Adds a game instance to config.
/// </summary>
public GameInstance AddInstance(GameInstance ksp_instance)
/// <returns>The resulting GameInstance object</returns>
/// <exception cref="NotKSPDirKraken">Thrown if the instance is not a valid game instance.</exception>
public GameInstance AddInstance(GameInstance instance)
{
if (ksp_instance.Valid)
if (instance.Valid)
{
string name = ksp_instance.Name;
instances.Add(name, ksp_instance);
string name = instance.Name;
instances.Add(name, instance);
Configuration.SetRegistryToInstances(instances);
}
else
{
throw new NotKSPDirKraken(ksp_instance.GameDir());
throw new NotKSPDirKraken(instance.GameDir());
}
return ksp_instance;
return instance;
}

/// <summary>
/// Adds a game instance to config.
/// </summary>
/// <param name="path">The path of the instance</param>
/// <param name="name">The name of the instance</param>
/// <param name="user">IUser object for interaction</param>
/// <returns>The resulting GameInstance object</returns>
/// <exception cref="NotKSPDirKraken">Thrown if the instance is not a valid game instance.</exception>
public GameInstance AddInstance(string path, string name, IUser user)
{
var matchingGames = knownGames
.Where(g => g.GameInFolder(new DirectoryInfo(path)))
.ToList();
switch (matchingGames.Count)
{
case 0:
throw new NotKSPDirKraken(path);

case 1:
return AddInstance(new GameInstance(
matchingGames.First(),
path, name, user
));

default:
// TODO: Prompt user to choose
return null;
var game = DetermineGame(new DirectoryInfo(path));
if (game == null)
throw new NotKSPDirKraken(path);

}
return AddInstance(new GameInstance(game, path, name, user));
}

/// <summary>
Expand Down Expand Up @@ -603,5 +598,30 @@ public static bool IsGameInstanceDir(DirectoryInfo path)
return knownGames.Any(g => g.GameInFolder(path));
}

/// <summary>
/// Tries to determine the game that is installed at the given path
/// </summary>
/// <param name="path">A DirectoryInfo of the path to check</param>
/// <returns>An instance of the matching game, or null if none could be found</returns>
public IGame DetermineGame(DirectoryInfo path)
{
var matchingGames = knownGames.Where(g => g.GameInFolder(path)).ToList();
switch (matchingGames.Count)
{
case 0:
return null;

case 1:
return matchingGames.First();

default:
// Prompt user to choose
int selection = User.RaiseSelectionDialog(
$"Please select the game that is installed at {path}",
matchingGames.Select(g => g.ShortName).ToArray());
return selection >= 0 ? matchingGames[selection] : null;
}
}

}
}
7 changes: 7 additions & 0 deletions Core/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ public interface IUser
bool Headless { get; }

bool RaiseYesNoDialog(string question);

/// <summary>
/// Ask the user to select one of the elements of the array.
/// The output is index 0 based.
/// To supply a default option, make the first option an integer indicating the index of it.
/// </summary>
/// <returns>The index of the item selected from the array or -1 if cancelled</returns>
int RaiseSelectionDialog(string message, params object[] args);
void RaiseError(string message, params object[] args);

Expand Down
13 changes: 9 additions & 4 deletions GUI/Dialogs/CloneFakeGameDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ private void radioButton_CheckedChanged(object sender, EventArgs e)
/// </summary>
private async void buttonOK_Click(object sender, EventArgs e)
{
string existingPath = textBoxClonePath.Text;
string newName = textBoxNewName.Text;
string newPath = textBoxNewPath.Text;

Expand Down Expand Up @@ -144,13 +145,17 @@ private async void buttonOK_Click(object sender, EventArgs e)

try
{
IGame guessedGame = manager.DetermineGame(new DirectoryInfo(existingPath));
await Task.Run(() =>
{
GameInstance sourceInstance = manager.Instances.Values
.FirstOrDefault(i => i.GameDir() == textBoxClonePath.Text);
if (guessedGame == null)
{
throw new NotKSPDirKraken(existingPath);
}

GameInstance instanceToClone = new GameInstance(
sourceInstance.game,
textBoxClonePath.Text,
guessedGame,
existingPath,
"irrelevant",
user
);
Expand Down
2 changes: 1 addition & 1 deletion GUI/Dialogs/SelectionDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public int ShowSelectionDialog (string message, params object[] args)
if (defaultSelection == i)
{
Util.Invoke(OptionsList, () => OptionsList.Items.Add(String.Concat(args[i].ToString(), " -- Default")));

}
else
{
Expand Down

0 comments on commit b90b031

Please sign in to comment.