/// <summary> /// Loads a vault file /// </summary> /// <param name="vaultName">Name of the vault</param> public LoadVaultResult LoadVault(string vaultName) { var result = new LoadVaultResult(); // Get the filename result.Filename = TQData.GetVaultFile(vaultName); // Check the cache try { result.Vault = this.userContext.Vaults[result.Filename]; } catch (KeyNotFoundException) { // We need to load the vault. if (!File.Exists(result.Filename)) { // the file does not exist so create a new vault. result.Vault = this.CreateVault(vaultName, result.Filename); result.VaultLoaded = true; } else { result.Vault = new PlayerCollection(vaultName, result.Filename); result.Vault.IsVault = true; try { PlayerCollectionProvider.LoadFile(result.Vault); result.VaultLoaded = true; } catch (ArgumentException argumentException) { result.ArgumentException = argumentException; } } // Add the vault to the cache, but only if we create it or it successfully loads. if (result.VaultLoaded) { this.userContext.Vaults.Add(result.Filename, result.Vault); } } return(result); }
/// <summary> /// Background worker has finished /// </summary> /// <param name="sender">sender object</param> /// <param name="e">RunWorkerCompletedEventArgs data</param> private void BackgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { // First, handle the case where an exception was thrown. if (e.Error != null) { if (MessageBox.Show( string.Concat(e.Error.Message, Resources.Form1BadLanguage), Resources.Form1ErrorLoadingResources, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, RightToLeftOptions) == DialogResult.Yes) { Application.Restart(); } else { Application.Exit(); } } else if (e.Cancelled && !this.resourcesLoaded) { Application.Exit(); } else if (e.Result.Equals(true)) { this.loadingComplete = true; this.Enabled = true; this.LoadTransferStash(); this.LoadRelicVaultStash(); // Load last character here if selected if (Config.Settings.Default.LoadLastCharacter) { int ind = this.characterComboBox.FindStringExact(Config.Settings.Default.LastCharacterName); if (ind != -1) { this.characterComboBox.SelectedIndex = ind; } } string currentVault = VaultService.MAINVAULT; // See if we should load the last loaded vault if (Config.Settings.Default.LoadLastVault) { currentVault = Config.Settings.Default.LastVaultName; // Make sure there is something in the config file to load else load the Main Vault // We do not want to create new here. if (string.IsNullOrEmpty(currentVault) || !File.Exists(TQData.GetVaultFile(currentVault))) { currentVault = VaultService.MAINVAULT; } } this.vaultListComboBox.SelectedItem = currentVault; // Finally load Vault this.LoadVault(currentVault, false); this.splashScreen.UpdateText(); this.splashScreen.ShowMainForm = true; CommandLineArgs args = new CommandLineArgs(); // Allows skipping of title screen with setting if (args.IsAutomatic || Config.Settings.Default.SkipTitle == true) { string player = args.Player; int index = this.characterComboBox.FindStringExact(player); if (index != -1) { this.characterComboBox.SelectedIndex = index; } this.splashScreen.CloseForm(); } } else { // If for some reason the loading failed, but there was no error raised. MessageBox.Show( Resources.Form1ErrorLoadingResources, Resources.Form1ErrorLoadingResources, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, RightToLeftOptions); Application.Exit(); } }
/// <summary> /// Method for the maintain vault files dialog /// </summary> private void MaintainVaultFilesDialog() { try { this.SaveAllModifiedFiles(); VaultMaintenanceDialog dlg = new VaultMaintenanceDialog(); dlg.Scale(new SizeF(UIService.UI.Scale, UIService.UI.Scale)); if (dlg.ShowDialog() == DialogResult.OK) { string newName = dlg.Target; string oldName = dlg.Source; bool handled = false; // Create a new vault? if (dlg.Action == VaultMaintenanceDialog.VaultMaintenance.New && newName != null) { // Add the name to the list this.vaultListComboBox.Items.Add(newName); // Select it this.vaultListComboBox.SelectedItem = newName; // Load it this.LoadVault(newName, false); handled = true; } else if (dlg.Action == VaultMaintenanceDialog.VaultMaintenance.Copy && newName != null && oldName != null) { string oldFilename = TQData.GetVaultFile(oldName); string newFilename = TQData.GetVaultFile(newName); // Make sure we save all modifications first. this.SaveAllModifiedFiles(); // Make sure the vault file to copy exists and the new name does not. if (File.Exists(oldFilename) && !File.Exists(newFilename)) { File.Copy(oldFilename, newFilename); // Add the new name to the list this.vaultListComboBox.Items.Add(newName); // Select the new name this.vaultListComboBox.SelectedItem = newName; // Load the new file. this.LoadVault(newName, false); handled = true; } } else if (dlg.Action == VaultMaintenanceDialog.VaultMaintenance.Delete && oldName != null) { string filename = TQData.GetVaultFile(oldName); // Make sure we save all modifications first. this.SaveAllModifiedFiles(); // Make sure the vault file to delete exists. if (File.Exists(filename)) { File.Delete(filename); } // Remove the file from the cache. userContext.Vaults.Remove(filename); // Remove the deleted file from the list. this.vaultListComboBox.Items.Remove(oldName); // Select the Main Vault since we know it's still there. this.vaultListComboBox.SelectedIndex = 1; handled = true; } else if (dlg.Action == VaultMaintenanceDialog.VaultMaintenance.Rename && newName != null && oldName != null) { string oldFilename = TQData.GetVaultFile(oldName); string newFilename = TQData.GetVaultFile(newName); // Make sure we save all modifications first. this.SaveAllModifiedFiles(); // Make sure the vault file to rename exists and the new name does not. if (File.Exists(oldFilename) && !File.Exists(newFilename)) { File.Move(oldFilename, newFilename); // Remove the old vault from the cache. userContext.Vaults.Remove(oldFilename); // Get rid of the old name from the list this.vaultListComboBox.Items.Remove(oldName); // If we renamed something to main vault we need to remove it, // since the list always contains Main Vault. if (newName == VaultService.MAINVAULT) { userContext.Vaults.Remove(newFilename); this.vaultListComboBox.Items.Remove(newName); } // Add the new name to the list this.vaultListComboBox.Items.Add(newName); // Select the new name this.vaultListComboBox.SelectedItem = newName; // Load the new file. this.LoadVault(newName, false); handled = true; } } if ((newName == null && oldName == null) || !handled) { // put the vault back to what it was if (this.vaultPanel.Player != null) { this.vaultListComboBox.SelectedItem = this.vaultPanel.Player.PlayerName; } } } else { // put the vault back to what it was if (this.vaultPanel.Player != null) { this.vaultListComboBox.SelectedItem = this.vaultPanel.Player.PlayerName; } } } catch (IOException exception) { Log.ErrorException(exception); MessageBox.Show(Log.FormatException(exception), Resources.GlobalError, MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, RightToLeftOptions); } }
/// <summary> /// Gets a list of all available vault files and populates the drop down list. /// </summary> /// <param name="loadVault">Indicates whether the list will also load the last vault selected.</param> private void GetVaultList(bool loadVault) { if (this.vaultService is null) { this.vaultService = new VaultService(userContext); } string[] vaults = TQData.GetVaultList(); // Added by VillageIdiot // See if the Vault path was set during GetVaultList and update the key accordingly if (TQData.VaultFolderChanged) { this.vaultService.UpdateVaultPath(TQData.TQVaultSaveFolder); } string currentVault; // There was something already selected so we will save it. if (this.vaultListComboBox.Items.Count > 0) { currentVault = this.vaultListComboBox.SelectedItem.ToString(); } else { currentVault = VaultService.MAINVAULT; } // Added by VillageIdiot // Clear the list before creating since this function can be called multiple times. this.vaultListComboBox.Items.Clear(); this.vaultListComboBox.Items.Add(Resources.MainFormMaintainVault); // Add Main Vault first if (this.secondaryVaultListComboBox.SelectedItem == null || this.secondaryVaultListComboBox.SelectedItem.ToString() != VaultService.MAINVAULT) { this.vaultListComboBox.Items.Add(VaultService.MAINVAULT); } if ((vaults?.Length ?? 0) > 0) { // now add everything EXCEPT for main vault foreach (string vault in vaults) { if (!vault.Equals(VaultService.MAINVAULT)) { // we already added main vault if (this.secondaryVaultListComboBox.SelectedItem != null && vault.Equals(this.secondaryVaultListComboBox.SelectedItem.ToString()) && this.showSecondaryVault) { break; } this.vaultListComboBox.Items.Add(vault); } } } // See if we should load the last loaded vault if (Config.Settings.Default.LoadLastVault) { currentVault = Config.Settings.Default.LastVaultName; // Make sure there is something in the config file to load else load the Main Vault // We do not want to create new here. if (string.IsNullOrEmpty(currentVault) || !File.Exists(TQData.GetVaultFile(currentVault))) { currentVault = VaultService.MAINVAULT; } } if (loadVault) { this.vaultListComboBox.SelectedItem = currentVault; // Finally load Vault this.LoadVault(currentVault, false); } }
/// <summary> /// Loads a vault file /// </summary> /// <param name="vaultName">Name of the vault</param> /// <param name="secondaryVault">flag indicating whether this selection is for the secondary panel</param> private void LoadVault(string vaultName, bool secondaryVault) { PlayerCollection vault = null; if (secondaryVault && vaultName == Resources.MainFormSelectVault) { if (this.secondaryVaultPanel.Player != null) { this.secondaryVaultPanel.Player = null; } } else { // Get the filename string filename = TQData.GetVaultFile(vaultName); // Check the cache try { vault = this.vaults[filename]; } catch (KeyNotFoundException) { // We need to load the vault. bool vaultLoaded = false; if (!File.Exists(filename)) { // the file does not exist so create a new vault. vault = CreateVault(vaultName, filename); vaultLoaded = true; } else { vault = new PlayerCollection(vaultName, filename); vault.IsVault = true; try { PlayerCollectionProvider.LoadFile(vault); vaultLoaded = true; } catch (ArgumentException argumentException) { string msg = string.Format(CultureInfo.CurrentUICulture, "{0}\n{1}\n{2}", Resources.MainFormPlayerReadError, filename, argumentException.Message); MessageBox.Show(msg, Resources.GlobalError, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, RightToLeftOptions); vaultLoaded = false; } } // Add the vault to the cache, but only if we create it or it successfully loads. if (vaultLoaded) { this.vaults.Add(filename, vault); } } } // Now assign the vault to the vaultpanel if (secondaryVault) { this.secondaryVaultPanel.Player = vault; } else { this.vaultPanel.Player = vault; } }