using System; using System.Diagnostics; using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Threading; using Launcher.Code; using MonoTorrent.Client; using MonoTorrent.Common; using PatcherShared; namespace Launcher.XAML { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow { public static readonly RoutedCommand ResetVersionCommand = new RoutedCommand(); #region dependency propeties public static readonly DependencyProperty TotalProgressProperty = DependencyProperty.Register("TotalProgress", typeof (double), typeof (MainWindow)); #endregion public TorrentManager CurrentDownloadManager { get; set; } public double TotalProgress { get { return (double) GetValue(TotalProgressProperty); } private set { SetValue(TotalProgressProperty, value); } } public TaskScheduler uiSchdeuler; public TaskFactory uiFactory; public MainWindow() { InitializeComponent(); ShowWebbrowser(); uiSchdeuler = TaskScheduler.FromCurrentSynchronizationContext(); uiFactory = new TaskFactory(uiSchdeuler); DataProvider.MainWindowInstance = this; DataProvider.LoadTorrents(); Task.Factory.StartNew(StartPatching); InitializeCommands(); #if DEBUG VersionText.Visibility = System.Windows.Visibility.Visible; VersionText.Text = DataProvider.Version.ToString(); #endif } public void InitializeCommands() { ResetVersionCommand.InputGestures.Add(new KeyGesture(Key.R, ModifierKeys.Control)); } public void StartPatching() { uiFactory.StartNew(() => { long patchesSinceStart = DataProvider.Version - DataProvider.StartVersion; long patchesToDoSinceStart = DataProvider.MaxVersion - DataProvider.StartVersion; if (patchesToDoSinceStart != 0) { totalProgress.Maximum = patchesToDoSinceStart; totalProgress.Value = patchesSinceStart; } }); if (BackgroundChecks.NewerVersionAvailable()) { uiFactory.StartNew(ShowPatchPanel); Stream stream; long length = BackgroundChecks.DownloadTorrent(out stream); BackgroundChecks.SaveTorrentToFile(stream, DataProvider.Version, length); TorrentManager manager = BackgroundChecks.GetDownloadForPatch(BackgroundChecks.GetTorrentPath(DataProvider.Version)); TorrentManager equalivant = DataProvider.ClientEngine.Torrents.FirstOrDefault(tm => tm.InfoHash.Equals(manager.InfoHash)); if (equalivant == null) { DataProvider.ClientEngine.Register(manager); this.CurrentDownloadManager = manager; manager.Start(); manager.TrackerManager.Announce(); } else { if (equalivant.State != TorrentState.Downloading || equalivant.State != TorrentState.Seeding) { equalivant.Start(); } equalivant.TrackerManager.Announce(); this.CurrentDownloadManager = equalivant; } CurrentDownloadManager.PieceManager.BlockReceived += (s, args) => SaveSetProgress(CurrentDownloadManager.Progress); SaveSetProgress(CurrentDownloadManager.Progress); CheckTorrentComplete(); } else { uiFactory.StartNew(ShowLoginPanel); } } private void CheckTorrentComplete() { while (!CurrentDownloadManager.Complete) { Thread.Sleep(100); } Debug.WriteLine("Torrent completed"); TorrentComplete(); } private void TorrentComplete() { // TODO: Apply patch Interlocked.Increment(ref DataProvider._v); StartPatching(); } private void ApplyPatch(string patchpath) { PatchFile file = PatchFile.Read(patchpath); if (file.SelfPatch) { } else { } } private void ShowWebbrowser() { WebBrowserOverlay wbo = new WebBrowserOverlay(this.browserPlacement); WebBrowser wb = wbo.WebBrowser; wb.Navigate(new Uri("http://omg-inside.net/notice5.php")); } private void ShowLoginPanel() { PatchPanel.Visibility = Visibility.Collapsed; LoginPanel.Visibility = Visibility.Visible; } private void ShowPatchPanel() { LoginPanel.Visibility = Visibility.Collapsed; PatchPanel.Visibility = Visibility.Visible; } private void StartButtonClick(object sender, RoutedEventArgs e) { try { if (username.Text == "" || password.Password == "") { MessageWindow.Show(Properties.Resources.Message_InvalidData, Properties.Resources.Message_InvalidDataLong); return; } if (!DataProvider.LauncherServiceClient.UserAllowed(username.Text, password.Password)) { MessageWindow.Show(Properties.Resources.Message_IncorrectData, Properties.Resources.Message_IncorrectDataLong); return; } if (!DataProvider.LauncherServiceClient.UserMacAllowed(username.Text, DataProvider.Mac)) { MessageWindow.Show(Properties.Resources.Message_UnknownComputer, Properties.Resources.Message_UnknownComputerLong); return; } DataProvider.FSClient.Username = username.Text; DataProvider.FSClient.Password = password.Password; DataProvider.FSClient.Start(); LoginPanel.Visibility = System.Windows.Visibility.Collapsed; } catch(Exception ex) { #if DEBUG MessageWindow.ShowErrorMessage(ex.ToString()); #else MessageWindow.ShowErrorMessage(Properties.Resources.Message_UnknownError, false); #endif return; } } private void CloseCommandExecuted(object sender, ExecutedRoutedEventArgs e) { Close(); } private void Grid_MouseDown(object sender, MouseButtonEventArgs e) { this.DragMove(); } public void SaveSetProgress(double progress) { uiFactory.StartNew(() => this.downloadProgress.Value = progress); } public void SaveClose() { if (this.Dispatcher.CheckAccess()) { this.Close(); } else { this.Dispatcher.Invoke(new VoidDelegate(SaveClose), DispatcherPriority.Normal); } } private delegate void VoidDelegate(); private void ResetVersion_Executed(object sender, ExecutedRoutedEventArgs e) { DataProvider.Version = 0; DataProvider.ClientEngine.Dispose(); ((App) Application.Current).SaveShutdown(0); } } }