// --- retrieve database --- /// <summary>Updates the Get add-ons screen when entered.</summary> private void EnterGetAddOns() { if (!IsBusy()) { labelDownloading.Text = Strings.GetInterfaceString("getaddons_connect"); progressbarDownloading.Style = ProgressBarStyle.Marquee; panelPackages.Enabled = false; CurrentDatabase = null; CurrentDatabaseThread = new Thread(ConnectToServer); CurrentDatabaseThread.IsBackground = true; CurrentDatabaseThread.Start(); } }
/// <summary>Connects to the server and populates the database fields. Intended to be executed from a worker thread.</summary> private void ConnectToServer() { string[] urls = new string[] { "http://trainsimframework.org/common/packages.dat" }; string[] names = new string[] { "trainsimframework.org" }; string directory = System.IO.Path.Combine(Program.FileSystem.SettingsFolder, "Cache"); string file = System.IO.Path.Combine(directory, "packages.dat"); if (System.IO.File.Exists(file)) { try { if ((DateTime.Now - System.IO.File.GetLastWriteTime(file)).TotalMinutes < 10.0) { byte[] bytes = System.IO.File.ReadAllBytes(file); CurrentDatabase = ManagedContent.Database.Load(bytes); this.Invoke(new ThreadStart( () => { labelDownloading.Text = string.Empty; } )); } } catch { } } string error = null; if (CurrentDatabase == null) { for (int i = 0; i < urls.Length; i++) { this.Invoke(new ThreadStart( () => { labelDownloading.Text = Interface.GetInterfaceString("getaddons_connect") + "\n" + names[i]; } )); int size = 0; byte[] bytes; if (Internet.TryDownloadBytesFromUrl(urls[i], out bytes, ref size)) { try { CurrentDatabase = ManagedContent.Database.Load(bytes); this.Invoke(new ThreadStart( () => { labelDownloading.Text = string.Empty; } )); try { System.IO.Directory.CreateDirectory(directory); } catch { } try { System.IO.File.WriteAllBytes(file, bytes); } catch { } break; } catch (Exception ex) { error = ex.Message; } } } } CurrentDatabaseThread = null; this.Invoke(new ThreadStart( () => { progressbarDownloading.Style = ProgressBarStyle.Blocks; if (CurrentDatabase != null) { panelPackages.Enabled = true; ManagedContent.Version[] updates = GetAvailableUpdates(); if (updates != null && updates.Length != 0) { textboxFilter.Text = string.Empty; checkboxFilterRoutes.Checked = true; checkboxFilterTrains.Checked = true; checkboxFilterLibraries.Checked = true; checkboxFilterSharedLibraries.Checked = true; checkboxFilterNoWIPs.Checked = false; checkboxFilterUpdates.Checked = true; timerFilter.Enabled = false; ShowDatabase(true); TreeviewPackagesAfterSelect(null, null); labelDownloading.Text = Interface.GetInterfaceString("getaddons_updates"); if (MessageBox.Show(Interface.GetInterfaceString("getaddons_updates_install"), Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { ButtonPackageInstallClick(updates, null); } else { checkboxFilterLibraries.Checked = false; checkboxFilterSharedLibraries.Checked = false; checkboxFilterUpdates.Checked = false; timerFilter.Enabled = false; ShowDatabase(false); } } else { ShowDatabase(false); } } else if (error != null) { labelDownloading.Text = Interface.GetInterfaceString("getaddons_connect_error") + "\n" + error; } else { labelDownloading.Text = Interface.GetInterfaceString("getaddons_connect_failure"); } } )); }