private async Task WatchDemo() { var site = Registry.Sites.FirstOrDefault(s => s.ID == _openURL.Host); if (site == null) { return; } var prodInfo = await GetDownloadURL(); if (prodInfo == null) { return; } this.Text = "Conduit - downloading demo: " + prodInfo.Name; var url = prodInfo.DownloadLink; TransformURL(ref url); var path = Settings.Options.DemoPath; path = path.Replace("[GROUP]", Sanitize(prodInfo.Group)); path = path.Replace("[YEAR]", prodInfo.ReleaseDate.Year.ToString()); Directory.CreateDirectory(path); var localFileName = await DownloadFile(url, path); if (localFileName == null) { return; } var extractPath = Path.Combine(path, Path.GetFileNameWithoutExtension(localFileName)); var unpacker = FindUnpacker(localFileName); var extractSuccessful = false; if (unpacker == null) { // We couldn't find an unpacker to the file; maybe we can just run it? extractPath = localFileName; } else { extractSuccessful = await UnpackFile(unpacker, localFileName, extractPath); } List <Runners.Runnable> runnables = new List <Runners.Runnable>(); var runners = Registry.Runners.OrderByDescending(s => s.Priority); var lastPriority = runners.First().Priority; foreach (var runner in runners) { if (lastPriority != runner.Priority && runnables.Count > 0) { // If we've reached a different priority level, but we already have something to run, don't bother. break; } runnables.AddRange(runner.GetRunnableFiles(extractPath).Select(s => new Runners.Runnable() { Path = s, Runner = runner })); } if (runnables.Count == 0) { // Nothing found, error if (unpacker != null && !extractSuccessful) { // We found an unpacker but it failed MessageBox.Show($"There's been an error unpacking the following file:\n{localFileName}", "Conduit error: Unpack failed", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (unpacker != null && extractSuccessful) { // We found and successfully used an unpacker, but couldn't find a runner MessageBox.Show($"We couldn't find a way to run the following file:\n{localFileName}", "Conduit error: Run failed", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (unpacker == null) { // We couldn't find an unpacker MessageBox.Show($"We couldn't find a way to unpack the following file:\n{localFileName}", "Conduit error: Unpack failed", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else if (runnables.Count == 1) { var result = runnables[0].Runner.Run(runnables[0].Path); if (!result) { MessageBox.Show($"There was an error starting this file:\n{runnables[0].Path}", "Conduit error: Run failed", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else if (runnables.Count > 1) { SelectRunnableDialog dlg = new SelectRunnableDialog(runnables); DialogResult dlgResult = dlg.ShowDialog(); if (dlgResult == DialogResult.OK) { if (dlg.SelectedRunnable != null) { var result = dlg.SelectedRunnable.Runner.Run(dlg.SelectedRunnable.Path); if (!result) { MessageBox.Show($"There was an error starting this file:\n{dlg.SelectedRunnable.Path}", "Conduit error: Run failed", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } } }
private async Task WatchDemo() { var site = Registry.Sites.FirstOrDefault(s => s.Host == _openURL.Host); if (site == null) { return; } var prodInfo = await GetDownloadURL(); if (prodInfo == null) { return; } this.Text = "Conduit - downloading demo: " + prodInfo.Name; var url = prodInfo.DownloadLink; TransformURL(ref url); var path = Settings.Options.DemoPath; path = path.Replace("[GROUP]", Sanitize(prodInfo.Group)); path = path.Replace("[YEAR]", prodInfo.ReleaseDate.Year.ToString()); Directory.CreateDirectory(path); var localFileName = await DownloadFile(url, path); if (localFileName == null) { return; } var extractPath = Path.Combine(path, Path.GetFileNameWithoutExtension(localFileName)); if (!await UnpackFile(localFileName, extractPath)) { // We couldn't unpack the file; maybe we can just run it? extractPath = localFileName; } List <Runners.Runnable> runnables = new List <Runners.Runnable>(); var runners = Registry.Runners.OrderByDescending(s => s.Priority); var lastPriority = runners.First().Priority; foreach (var runner in runners) { if (lastPriority != runner.Priority && runnables.Count > 0) { // If we've reached a different priority level, but we already have something to run, don't bother. break; } runnables.AddRange(runner.GetRunnableFiles(extractPath).Select(s => new Runners.Runnable() { Path = s, Runner = runner })); } if (runnables.Count == 0) { // Nothing found, error } else if (runnables.Count == 1) { runnables[0].Runner.Run(runnables[0].Path); } else if (runnables.Count > 1) { SelectRunnableDialog dlg = new SelectRunnableDialog(runnables); DialogResult result = dlg.ShowDialog(); if (result == DialogResult.OK) { if (dlg.SelectedRunnable != null) { dlg.SelectedRunnable.Runner.Run(dlg.SelectedRunnable.Path); } } } }