Skip to content

Commit

Permalink
Major overhaul of multi-instance KSP
Browse files Browse the repository at this point in the history
- Split management of multiple instances into KSPManager
- No need to manually save or load from registry.
- Added GetPreferredInstance() as the best way to find the user's
  preferred instance, if a single preferred instance exists.
- Added PortableDir() to install portable installs.
- Decoupled transactional file transactions from KSP class.
- KSPPathConstants in their own file
- KSPPathUtils in its own file.
- Some tests, but not enough, darn it!

For KSP-CKAN#125
  • Loading branch information
pjf committed Oct 24, 2014
1 parent 9dbc14d commit 41a8dfc
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 37 deletions.
21 changes: 8 additions & 13 deletions ChooseKSPInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public ChooseKSPInstance()

m_BrowseKSPFolder = new FolderBrowserDialog();

if (!KSP.Instances.Any())
if (!KSPManager.Instances.Any())
{
KSP.AddDefaultInstance();
KSPManager.FindAndRegisterDefaultInstance();
}

UpdateInstancesList();
Expand All @@ -37,7 +37,7 @@ private void UpdateInstancesList()
{
KSPInstancesListView.Items.Clear();

foreach (var instance in KSP.Instances)
foreach (var instance in KSPManager.Instances)
{
var item = new ListViewItem() { Text = instance.Key, Tag = instance.Key };

Expand All @@ -53,9 +53,8 @@ private void AddNewButton_Click(object sender, EventArgs e)
{
if (m_BrowseKSPFolder.ShowDialog() == DialogResult.OK)
{
var instance = new KSP();
instance.SetGameDir(m_BrowseKSPFolder.SelectedPath);
KSP.Instances.Add("New instance", instance);
var instance = new KSP(m_BrowseKSPFolder.SelectedPath);
KSPManager.Instances.Add("New instance", instance);
UpdateInstancesList();
}
}
Expand All @@ -66,11 +65,10 @@ private void SelectButton_Click(object sender, EventArgs e)

if (SetAsDefaultCheckbox.Checked)
{
KSP.AutoStartInstance = instance;
KSP.PopulateRegistryWithInstances();
KSPManager.SetAutoStart(instance);
}

KSP.InitializeInstance(instance);
KSPManager.SetCurrentInstance(instance);
Hide();
Main.Instance.Show();
}
Expand All @@ -97,10 +95,7 @@ private void RenameButton_Click(object sender, EventArgs e)
m_RenameInstanceDialog = new RenameInstanceDialog();
if (m_RenameInstanceDialog.ShowRenameInstanceDialog(instance) == DialogResult.OK)
{
var ksp = KSP.Instances[instance];
KSP.Instances.Remove(instance);
KSP.Instances.Add(m_RenameInstanceDialog.GetResult(), ksp);
KSP.PopulateRegistryWithInstances();
KSPManager.RenameInstance(instance, m_RenameInstanceDialog.GetResult());
UpdateInstancesList();
}
}
Expand Down
20 changes: 6 additions & 14 deletions Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,12 @@ public Main()
User.displayMessage = AddStatusMessage;
User.displayError = ErrorDialog;

KSP.LoadInstancesFromRegistry();

controlFactory = new ControlFactory();
m_Instance = this;

InitializeComponent();

if (KSP.AutoStartInstance == "")
if (KSPManager.GetPreferredInstance() != null)
{
Hide();

Expand All @@ -61,17 +59,11 @@ public Main()
Close();
return;
}

KSP.PopulateRegistryWithInstances();
}
else
{
KSP.InitializeInstance(KSP.AutoStartInstance);
}

m_Configuration = Configuration.LoadOrCreateConfiguration
(
Path.Combine(KSP.CurrentInstance.GameDir(), "CKAN/GUIConfig.xml"),
Path.Combine(KSPManager.CurrentInstance.GameDir(), "CKAN/GUIConfig.xml"),
Repo.default_ckan_repo
);

Expand Down Expand Up @@ -125,8 +117,8 @@ private void Main_Load(object sender, EventArgs e)

ApplyToolButton.Enabled = false;

Text = String.Format("CKAN ({0}) - KSP {1}", Meta.Version(), KSP.CurrentInstance.Version());
KSPVersionLabel.Text = String.Format("Kerbal Space Program {0}", KSP.CurrentInstance.Version());
Text = String.Format("CKAN ({0}) - KSP {1}", Meta.Version(), KSPManager.CurrentInstance.Version());
KSPVersionLabel.Text = String.Format("Kerbal Space Program {0}", KSPManager.CurrentInstance.Version());
}

private void RefreshToolButton_Click(object sender, EventArgs e)
Expand Down Expand Up @@ -404,11 +396,11 @@ private void launchKSPToolStripMenuItem_Click(object sender, EventArgs e)
{
if (Util.IsLinux)
{
Process.Start(Path.Combine(KSP.CurrentInstance.GameDir(), "KSP.x86"), m_Configuration.CommandLineArguments);
Process.Start(Path.Combine(KSPManager.CurrentInstance.GameDir(), "KSP.x86"), m_Configuration.CommandLineArguments);
}
else
{
Process.Start(Path.Combine(KSP.CurrentInstance.GameDir(), "KSP.exe"), m_Configuration.CommandLineArguments);
Process.Start(Path.Combine(KSPManager.CurrentInstance.GameDir(), "KSP.exe"), m_Configuration.CommandLineArguments);
}
}

Expand Down
4 changes: 2 additions & 2 deletions MainInstall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private void InstallMods(object sender, DoWorkEventArgs e) // this probably need
// the mod is not already in the install list
if (
RegistryManager.Instance()
.registry.LatestAvailable(mod.name.ToString(), KSP.CurrentInstance.Version()) != null &&
.registry.LatestAvailable(mod.name.ToString(), KSPManager.CurrentInstance.Version()) != null &&
!RegistryManager.Instance().registry.IsInstalled(mod.name.ToString()) &&
!toInstall.Contains(mod.name.ToString()))
{
Expand Down Expand Up @@ -113,7 +113,7 @@ private void InstallMods(object sender, DoWorkEventArgs e) // this probably need
{
if (
RegistryManager.Instance()
.registry.LatestAvailable(mod.name.ToString(), KSP.CurrentInstance.Version()) != null &&
.registry.LatestAvailable(mod.name.ToString(), KSPManager.CurrentInstance.Version()) != null &&
!RegistryManager.Instance().registry.IsInstalled(mod.name.ToString()) &&
!toInstall.Contains(mod.name.ToString()))
{
Expand Down
4 changes: 2 additions & 2 deletions MainModInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ private TreeNode UpdateModDependencyGraphRecursively(TreeNode parentNode, CkanMo
try
{
dependencyModule = registry.LatestAvailable
(dependency.name.ToString(), KSP.CurrentInstance.Version());
(dependency.name.ToString(), KSPManager.CurrentInstance.Version());
UpdateModDependencyGraphRecursively(node, dependencyModule, relationship, depth + 1);
}
catch (ModuleNotFoundKraken)
{
List<CkanModule> dependencyModules = registry.LatestAvailableWithProvides
(dependency.name.ToString(), KSP.CurrentInstance.Version());
(dependency.name.ToString(), KSPManager.CurrentInstance.Version());

if (dependencyModules == null)
{
Expand Down
2 changes: 1 addition & 1 deletion MainRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private void PostUpdateRepo(object sender, RunWorkerCompletedEventArgs e)
UpdateModFilterList();

m_WaitDialog.SetDescription("Scanning for manually installed mods");
KSP.CurrentInstance.ScanGameData();
KSPManager.CurrentInstance.ScanGameData();

m_WaitDialog.HideWaitDialog();
AddStatusMessage("Repository successfully updated");
Expand Down
9 changes: 4 additions & 5 deletions SettingsDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ public SettingsDialog()
private void SettingsDialog_Load(object sender, EventArgs e)
{
CKANRepositoryTextBox.Text = Main.Instance.m_Configuration.Repository;
KSPInstallPathLabel.Text = KSP.CurrentInstance.GameDir();
KSPInstallPathLabel.Text = KSPManager.CurrentInstance.GameDir();

UpdateCacheInfo();
}

private void UpdateCacheInfo()
{
long cacheSize = 0;
var cachePath = Path.Combine(KSP.CurrentInstance.CkanDir(), "downloads");
var cachePath = Path.Combine(KSPManager.CurrentInstance.CkanDir(), "downloads");

var cacheDirectory = new DirectoryInfo(cachePath);
int count = 0;
Expand Down Expand Up @@ -60,7 +60,7 @@ private void CKANRepositoryDefaultButton_Click(object sender, EventArgs e)

private void ClearCKANCacheButton_Click(object sender, EventArgs e)
{
var cachePath = Path.Combine(KSP.CurrentInstance.CkanDir(), "downloads");
var cachePath = Path.Combine(KSPManager.CurrentInstance.CkanDir(), "downloads");
foreach (var file in Directory.GetFiles(cachePath))
{
try
Expand All @@ -77,8 +77,7 @@ private void ClearCKANCacheButton_Click(object sender, EventArgs e)

private void ResetAutoStartChoice_Click(object sender, EventArgs e)
{
KSP.AutoStartInstance = "";
KSP.PopulateRegistryWithInstances();
KSPManager.ClearAutoStart();

Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);
Application.Exit();
Expand Down

0 comments on commit 41a8dfc

Please sign in to comment.