private void importMd(MapDescriptor mapDescriptor) { OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "Map Descriptor File (.md)|*.md"; openFileDialog1.Title = "Which Map to import?"; if (openFileDialog1.ShowDialog(this) == DialogResult.OK && !string.IsNullOrWhiteSpace(openFileDialog1.FileName)) { using (var cancelTokenSource = new CancellationTokenSource()) using (var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(exitTokenSource.Token, cancelTokenSource.Token)) { CancellationToken ct = linkedTokenSource.Token; ProgressBar progressBar = new ProgressBar(verboseToolStripMenuItem.Checked); progressBar.callback = (b) => { try { cancelTokenSource?.Cancel(); } catch (ObjectDisposedException) { } }; progressBar.Show(this); var progress = new Progress <ProgressInfo>(progressInfo => { progressBar.update(progressInfo); Debug.WriteLine(progressInfo.line); }); try { var importedMapDescriptor = PatchProcess.ImportMd(openFileDialog1.FileName, progress, ct); if (mapDescriptor != null) { mapDescriptor.setFromImport(importedMapDescriptor); } else { BindingSource bs = dataGridView1.DataSource as BindingSource; bs.Add(importedMapDescriptor); } } catch (Exception e) { progressBar.appendText(e.Message); progressBar.appendText(Environment.NewLine + Environment.NewLine + e.ToString()); progressBar.EnableButton(); Debug.WriteLine(e.ToString()); } } } }
public static void Load(string fileName, List <MapDescriptor> mapDescriptors, IProgress <ProgressInfo> progress, CancellationToken ct) { var dir = Directory.GetParent(fileName).FullName; string[] lines = File.ReadAllLines(fileName); var p = 0; // how many ids are there? var maxId = 0; foreach (var line in lines) { string[] columns = line.Split(new[] { ',' }, 6); var i = int.Parse(columns[0].Trim()); if (i > maxId) { maxId = i; } } var tempMapDescriptors = new List <MapDescriptor>(); // add as many new map descriptors for (int i = 0; i < maxId + 1; i++) { var md = new MapDescriptor(); tempMapDescriptors.Add(md); if (i < mapDescriptors.Count) { md.set(mapDescriptors[i]); } } foreach (var line in lines) { string[] columns = line.Split(new[] { ',' }, 6); var i = int.Parse(columns[0].Trim()); var mapSet = sbyte.Parse(columns[1].Trim()); var zone = sbyte.Parse(columns[2].Trim()); var order = sbyte.Parse(columns[3].Trim()); var isPracticeBoard = bool.Parse(columns[4].Trim()); var mapDescriptorFilePath = columns[5].Trim(); tempMapDescriptors[i].MapSet = mapSet; tempMapDescriptors[i].Zone = zone; tempMapDescriptors[i].Order = order; tempMapDescriptors[i].IsPracticeBoard = isPracticeBoard; if (!string.IsNullOrEmpty(mapDescriptorFilePath)) { mapDescriptorFilePath = Path.Combine(dir, mapDescriptorFilePath); var importMd = PatchProcess.ImportMd(mapDescriptorFilePath, ProgressInfo.makeNoProgress(progress), ct); tempMapDescriptors[i].setFromImport(importMd); } progress?.Report(100 * p / lines.Count()); p++; } while (mapDescriptors.Count > tempMapDescriptors.Count) { mapDescriptors.RemoveAt(mapDescriptors.Count - 1); } for (int i = mapDescriptors.Count; i < tempMapDescriptors.Count; i++) { var md = new MapDescriptor(); // only add new maps, if there is a map descriptor file path available if (!string.IsNullOrEmpty(tempMapDescriptors[i].MapDescriptorFilePath)) { mapDescriptors.Add(md); } else { progress?.Report("Warning: Could not load the configuration after map " + i + " because the md files are not set."); break; } } for (int i = 0; i < mapDescriptors.Count; i++) { if (!tempMapDescriptors[i].Equals(mapDescriptors[i])) { tempMapDescriptors[i].Dirty = true; } mapDescriptors[i].setFromImport(tempMapDescriptors[i]); mapDescriptors[i].MapSet = tempMapDescriptors[i].MapSet; mapDescriptors[i].Zone = tempMapDescriptors[i].Zone; mapDescriptors[i].Order = tempMapDescriptors[i].Order; mapDescriptors[i].IsPracticeBoard = tempMapDescriptors[i].IsPracticeBoard; } progress?.Report(100); progress?.Report("Loaded configuration from " + fileName); }