private void routeWorkerThread_completed(object sender, RunWorkerCompletedEventArgs e) { if (e.Error != null) { TryLoadImage(pictureboxRouteImage, "route_error.png"); textboxRouteDescription.Text = e.Error.Message; textboxRouteEncodingPreview.Text = ""; pictureboxRouteMap.Image = null; pictureboxRouteGradient.Image = null; Result.RouteFile = null; checkboxTrainDefault.Text = Interface.GetInterfaceString("start_train_usedefault"); return; } try { pictureboxRouteMap.Image = Illustrations.CreateRouteMap(pictureboxRouteMap.Width, pictureboxRouteMap.Height, false); pictureboxRouteGradient.Image = Illustrations.CreateRouteGradientProfile(pictureboxRouteGradient.Width, pictureboxRouteGradient.Height, false); // image if (Game.RouteImage.Length != 0) { try { pictureboxRouteImage.Image = Image.FromFile(Game.RouteImage); } catch { TryLoadImage(pictureboxRouteImage, "route_error.png"); } } else { string[] f = new string[] { ".png", ".bmp", ".gif", ".tiff", ".tif", ".jpeg", ".jpg" }; int i; for (i = 0; i < f.Length; i++) { string g = OpenBveApi.Path.CombineFile(System.IO.Path.GetDirectoryName(Result.RouteFile), System.IO.Path.GetFileNameWithoutExtension(Result.RouteFile) + f[i]); if (System.IO.File.Exists(g)) { try { pictureboxRouteImage.Image = Image.FromFile(g); } catch { pictureboxRouteImage.Image = null; } break; } } if (i == f.Length) { TryLoadImage(pictureboxRouteImage, "route_unknown.png"); } } // description string Description = Interface.ConvertNewlinesToCrLf(Game.RouteComment); if (Description.Length != 0) { textboxRouteDescription.Text = Description; } else { textboxRouteDescription.Text = System.IO.Path.GetFileNameWithoutExtension(Result.RouteFile); } textboxRouteEncodingPreview.Text = Interface.ConvertNewlinesToCrLf(Description); if (Game.TrainName != null) { checkboxTrainDefault.Text = Interface.GetInterfaceString("start_train_usedefault") + @" (" + Game.TrainName + @")"; } else { checkboxTrainDefault.Text = Interface.GetInterfaceString("start_train_usedefault"); } } catch (Exception ex) { TryLoadImage(pictureboxRouteImage, "route_error.png"); textboxRouteDescription.Text = ex.Message; textboxRouteEncodingPreview.Text = ""; pictureboxRouteMap.Image = null; pictureboxRouteGradient.Image = null; Result.RouteFile = null; checkboxTrainDefault.Text = Interface.GetInterfaceString("start_train_usedefault"); } if (checkboxTrainDefault.Checked) { ShowDefaultTrain(); } this.Cursor = Cursors.Default; //Deliberately select the tab when the process is complete //This hopefully fixes another instance of the 'grey tabs' bug tabcontrolRouteDetails.SelectedTab = tabpageRouteDescription; buttonStart.Enabled = Result.RouteFile != null & Result.TrainFolder != null; }
// show train private void ShowTrain(bool UserSelectedEncoding) { if (!UserSelectedEncoding) { comboboxTrainEncoding.Tag = new object(); comboboxTrainEncoding.SelectedIndex = 0; comboboxTrainEncoding.Items[0] = "(UTF-8)"; comboboxTrainEncoding.Tag = null; Result.TrainEncoding = System.Text.Encoding.UTF8; switch (Interface.GetEncodingFromFile(Result.TrainFolder, "train.txt")) { case Interface.Encoding.Utf8: comboboxTrainEncoding.SelectedIndex = 0; comboboxTrainEncoding.Items[0] = "(UTF-8)"; Result.TrainEncoding = System.Text.Encoding.UTF8; break; case Interface.Encoding.Utf16Le: comboboxTrainEncoding.SelectedIndex = 0; comboboxTrainEncoding.Items[0] = "(UTF-16 little endian)"; Result.TrainEncoding = System.Text.Encoding.Unicode; break; case Interface.Encoding.Utf16Be: comboboxTrainEncoding.SelectedIndex = 0; comboboxTrainEncoding.Items[0] = "(UTF-16 big endian)"; Result.TrainEncoding = System.Text.Encoding.BigEndianUnicode; break; case Interface.Encoding.Utf32Le: comboboxTrainEncoding.SelectedIndex = 0; comboboxTrainEncoding.Items[0] = "(UTF-32 little endian)"; Result.TrainEncoding = System.Text.Encoding.UTF32; break; case Interface.Encoding.Utf32Be: comboboxTrainEncoding.SelectedIndex = 0; comboboxTrainEncoding.Items[0] = "(UTF-32 big endian)"; Result.TrainEncoding = System.Text.Encoding.GetEncoding(12001); break; } int i; for (i = 0; i < Interface.CurrentOptions.TrainEncodings.Length; i++) { if (Interface.CurrentOptions.TrainEncodings[i].Value == Result.TrainFolder) { int j; for (j = 1; j < EncodingCodepages.Length; j++) { if (EncodingCodepages[j] == Interface.CurrentOptions.TrainEncodings[i].Codepage) { comboboxTrainEncoding.SelectedIndex = j; Result.TrainEncoding = System.Text.Encoding.GetEncoding(EncodingCodepages[j]); break; } } if (j == EncodingCodepages.Length) { comboboxTrainEncoding.SelectedIndex = 0; Result.TrainEncoding = System.Text.Encoding.UTF8; } break; } } panelTrainEncoding.Enabled = true; comboboxTrainEncoding.Tag = null; } { // train image string File = OpenBveApi.Path.CombineFile(Result.TrainFolder, "train.png"); if (!System.IO.File.Exists(File)) { File = OpenBveApi.Path.CombineFile(Result.TrainFolder, "train.bmp"); } if (System.IO.File.Exists(File)) { try { pictureboxTrainImage.Image = Image.FromFile(File); } catch { pictureboxTrainImage.Image = null; TryLoadImage(pictureboxTrainImage, "train_error.png"); } } else { TryLoadImage(pictureboxTrainImage, "train_unknown.png"); } } { // train description string File = OpenBveApi.Path.CombineFile(Result.TrainFolder, "train.txt"); if (System.IO.File.Exists(File)) { try { string trainText = System.IO.File.ReadAllText(File, Result.TrainEncoding); trainText = Interface.ConvertNewlinesToCrLf(trainText); textboxTrainDescription.Text = trainText; textboxTrainEncodingPreview.Text = trainText; } catch { textboxTrainDescription.Text = System.IO.Path.GetFileName(Result.TrainFolder); textboxTrainEncodingPreview.Text = ""; } } else { textboxTrainDescription.Text = System.IO.Path.GetFileName(Result.TrainFolder); textboxTrainEncodingPreview.Text = ""; } } groupboxTrainDetails.Visible = true; labelTrainEncoding.Enabled = true; labelTrainEncodingPreview.Enabled = true; textboxTrainEncodingPreview.Enabled = true; buttonStart.Enabled = Result.RouteFile != null & Result.TrainFolder != null; }
// show default train private void ShowDefaultTrain() { if (string.IsNullOrEmpty(Result.RouteFile)) { return; } if (string.IsNullOrEmpty(Game.TrainName)) { return; } string Folder; try { Folder = System.IO.Path.GetDirectoryName(Result.RouteFile); if (Game.TrainName[0] == '$') { Folder = OpenBveApi.Path.CombineDirectory(Folder, Game.TrainName); if (System.IO.Directory.Exists(Folder)) { string File = OpenBveApi.Path.CombineFile(Folder, "train.dat"); if (System.IO.File.Exists(File)) { Result.TrainFolder = Folder; ShowTrain(false); return; } } } } catch { Folder = null; } try { while (true) { string TrainFolder = OpenBveApi.Path.CombineDirectory(Folder, "Train"); if (System.IO.Directory.Exists(TrainFolder)) { try { Folder = OpenBveApi.Path.CombineDirectory(TrainFolder, Game.TrainName); } catch { Folder = null; } if (Folder != null && System.IO.Directory.Exists(Folder)) { string File = OpenBveApi.Path.CombineFile(Folder, "train.dat"); if (System.IO.File.Exists(File)) { // train found Result.TrainFolder = Folder; ShowTrain(false); return; } } break; } if (Folder == null) { continue; } System.IO.DirectoryInfo Info = System.IO.Directory.GetParent(Folder); if (Info != null) { Folder = Info.FullName; } else { break; } } } catch { } // train not found Result.TrainFolder = null; TryLoadImage(pictureboxTrainImage, "train_error.png"); textboxTrainDescription.Text = Interface.ConvertNewlinesToCrLf(Interface.GetInterfaceString("start_train_notfound") + Game.TrainName); comboboxTrainEncoding.Tag = new object(); comboboxTrainEncoding.SelectedIndex = 0; comboboxTrainEncoding.Tag = null; labelTrainEncoding.Enabled = false; panelTrainEncoding.Enabled = false; labelTrainEncodingPreview.Enabled = false; textboxTrainEncodingPreview.Enabled = false; textboxTrainEncodingPreview.Text = ""; groupboxTrainDetails.Visible = true; }