public static bool Attempt(TcpClient client, string ip, int port) { bool closed = false; WaitingForm.BeginWait("Connecting to host...", ev => { while (!client.Connected) { try { client.Connect(ip, port); } catch (SocketException) { if (closed || !ConnectionFailure()) { break; } } } }, () => { closed = true; client.Close(); return(true); }); if (closed || !client.Connected) { return(false); } return(true); }
public static void LoadNews() { string savePath = $"{Helper.directory}temp/news.txt"; string news = null; WaitingForm.BeginWait("Downloading notices...", ev => { try { using (var wc = new WebClient()) { wc.DownloadFile($"https://github.com/CosmoleonGit/RevisionHub/raw/master/News.rtf", savePath); } news = File.ReadAllText(savePath); File.Delete(savePath); } catch (WebException ex) { Helper.Error("Failed to load pack data.", ex.Message); } }); if (news != null) { var newsForm = new NewsForm(news); newsForm.Show(); } }
static void Do(bool quiet = false) { string savePath = $"{Helper.directory}temp/info.txt"; string newVer = null; WaitingForm.BeginWait("Checking for updates...", ev => { try { using (var wc = new WebClient()) { wc.DownloadFile($"https://github.com/CosmoleonGit/RevisionHub/raw/master/info.txt", savePath); } using (var sr = new StreamReader(savePath)) { newVer = sr.ReadLine(); } } catch (WebException ex) { if (!quiet) { Helper.Error("Failed to load pack data.", ex.Message); } } finally { File.Delete(savePath); } }); if (newVer != null) { var ver = Assembly.GetEntryAssembly().GetName().Version; string curVer = Application.ProductVersion.Substring(0, Application.ProductVersion.LastIndexOf('.')); if (curVer != newVer) { if (MsgBox.ShowWait($"There is a new version of Revision Hub (v{newVer}).\n\nWould you like to go to the download page?", "Check for Updates", MsgBox.Options.yesNo, MsgBox.MsgIcon.INFO) == "Yes") { Process.Start("https://github.com/CosmoleonGit/RevisionHub/releases/tag/" + newVer); } } else if (!quiet) { MsgBox.ShowWait("You are running the latest version of Revision Hub.", "Check for Updates", null, MsgBox.MsgIcon.TICK); } } }
public static void ShowPacks() { Directory.CreateDirectory(Helper.directory + "temp"); bool worked = false; WaitingForm.BeginWait("Downloading pack data...", ev => { try { using (var wc = new WebClient()) { wc.DownloadFile($"https://github.com/CosmoleonGit/RevisionHub/raw/master/Packs/_List.txt", savePath); } worked = true; } catch (Exception ex) { Helper.Error("Failed to load pack data.", ex.Message); } }); if (worked) { var packList = new List <Pack>(); var lineQueue = new Queue <string>(); string[] lines = File.ReadAllLines(savePath); File.Delete(savePath); foreach (string line in Helper.SplitByLine(lines, "-")) { lineQueue.Enqueue(line); } while (lineQueue.Count > 0) { string name = lineQueue.Dequeue(); string desc = lineQueue.Dequeue(); packList.Add(new Pack(name, desc)); } var packsForm = new PacksForm(packList.ToArray()); packsForm.Show(); } }
public override bool Setup() { try { server.Start(); } catch (SocketException) { MsgBox.ShowWait("Failed to start server. Is there already a server listening on that port?", "Error", null, MsgBox.MsgIcon.ERROR); return(false); } WaitingForm.BeginWait("Waiting for client to connect...", ev => { while (true) { try { var waiting = Task.Run(async delegate { await Task.Delay(timeout); }); while (!server.Pending() && !waiting.IsCompleted) { } if (server.Pending()) { client = server.AcceptTcpClient(); socket = client.Client; socket.SendBufferSize = bufferSize; socket.ReceiveBufferSize = bufferSize; stream = client.GetStream(); stream.ReadTimeout = timeout; break; } else { throw new TimeoutException(); } } catch (TimeoutException) { if (!ConnectionFailure()) { break; } } catch (ObjectDisposedException) { break; } } }, () => { server.Stop(); return(true); }); if (socket != null) { return(true); } else { return(false); } }
private void DownloadBtn_Click(object sender, EventArgs e) { string packName = PacksList.SelectedItem.ToString(); string savePath = $"{Helper.directory}temp/{packName}.zip"; string moveToPath = $"{Helper.directory}Revision/Downloaded Packs/{packName}"; // Check if a folder with that name already exists. if (OverwritePanel.Visible) { if (MsgBox.ShowWait($"A folder with this name already exists." + $"\n\n" + $"Do you want to overwrite it?", "Overwrite File", MsgBox.Options.yesNo, MsgBox.MsgIcon.EXCL) == "Yes") { Directory.Delete(moveToPath, true); } else { return; } } // Download the pack from GitHub. bool worked = false; WaitingForm.BeginWait("Downloading and extracting pack...", ev => { try { using (var wc = new WebClient()) { wc.DownloadFile($"https://github.com/CosmoleonGit/RevisionProgram/raw/master/Packs/{packName}.zip", savePath); } worked = true; } catch (WebException ex) { Helper.Error("Failed to download pack.", ex.Message); } if (worked) { worked = false; try { Directory.CreateDirectory($"{Helper.directory}Revision/Downloaded Packs"); ZipFile.ExtractToDirectory(savePath, moveToPath); File.Delete(savePath); worked = true; } catch (Exception ex) { Helper.Error("Failed to download pack.", ex.Message); } } }); // If downloading was successful, extract the file. if (worked) { if (MsgBox.ShowWait($"Successfully downloaded {packName}! You will find it in your Revision folder." + $"\n\n" + $"Would you like to go to it now?", "Finished", MsgBox.Options.yesNo, MsgBox.MsgIcon.TICK) == "Yes") { RevisionHub.OpenExplorer(); RevisionHub.explorer.OpenDir($"Downloaded Packs/{packName}"); Close(); } else { PacksList.SelectedIndex = -1; } } }