From 74ed8bf083244dbdc5ca162862494698f9776e84 Mon Sep 17 00:00:00 2001 From: Ashnal Date: Mon, 24 Feb 2020 21:38:57 -0500 Subject: [PATCH 1/2] Added choice menu on startup that scrapes hyuu.cc Dependency on HtmlAgility Added game launch to the end Some rudimentary checks to ensure the program can tell if someone enters invalid stuff --- HyuuSeeker/HyuuSeeker.csproj | 1 + HyuuSeeker/Program.cs | 98 ++++++++++++++++++++++++++++++++---- 2 files changed, 90 insertions(+), 9 deletions(-) diff --git a/HyuuSeeker/HyuuSeeker.csproj b/HyuuSeeker/HyuuSeeker.csproj index 8903460..b1cb0e5 100644 --- a/HyuuSeeker/HyuuSeeker.csproj +++ b/HyuuSeeker/HyuuSeeker.csproj @@ -11,6 +11,7 @@ + diff --git a/HyuuSeeker/Program.cs b/HyuuSeeker/Program.cs index 495cd41..70fa783 100644 --- a/HyuuSeeker/Program.cs +++ b/HyuuSeeker/Program.cs @@ -81,6 +81,8 @@ using System.Runtime.InteropServices; using System.Text; using System.Text.RegularExpressions; +using HtmlAgilityPack; +using System.Diagnostics; namespace HyuuSeeker { @@ -225,6 +227,10 @@ public static void ClearCurrentConsoleLine() static void Main(string[] args) { + string hostname = ""; + int intchoice; + int port = 0; + if (!File.Exists("maps.kart")) { Console.WriteLine("Close this program and move it to your SRB2Kart folder. Then try again."); Console.ReadKey(); @@ -258,7 +264,31 @@ static void Main(string[] args) Console.WriteLine("\"like wadseeker but written by chimpanzees\""); Console.WriteLine(""); - Console.WriteLine("Enter a hostname to fetch files for, or leave blank to read your log.txt."); + List servers = new List(); + HtmlWeb web = new HtmlWeb(); + HtmlDocument document = web.Load("http://hyuu.cc"); + HtmlNodeCollection nodes = document.DocumentNode.SelectNodes("//div[@class='hyuuseeker-enabled']/li"); + + Console.WriteLine("Choose a hyuuseeker supported server to play on!\n"); + + int listnumber = 1; + foreach (HtmlNode node in nodes) + { + HtmlNode linknode = node.SelectSingleNode("a"); + HtmlNode descnode = node.SelectSingleNode("div[@class='desc']"); + servers.Add(linknode.Attributes.First().Value); + + + Console.WriteLine(listnumber + " : " + linknode.InnerText); + if (descnode != null) + { + Console.WriteLine("\t" + descnode.InnerText); + } + Console.WriteLine(""); + listnumber++; + } + + Console.WriteLine("Enter a number to select a HyuuSeeker supported server.\nOr, you can type an unsupported hostname directly and HyuuSeeker will try its best.\nYou can also enter nothing and HyuuSeeker will scan your log for previous connections."); string target = Console.ReadLine(); List targets = new List(); @@ -283,16 +313,35 @@ static void Main(string[] args) } } } - } else + } + else { - string hostname = target; - int port = 5029; - string[] parts = hostname.Split(':'); - if (parts.Length == 2) + if (Int32.TryParse(target, out intchoice)) { - hostname = parts[0]; - port = Int32.Parse(parts[1]); + if (servers[intchoice-1] != null) + { + Uri uri = new Uri(servers[intchoice-1]); + hostname = uri.Host; + } + else + { + Console.WriteLine("You entered a number out of the list range, exiting ..."); + return; + } } + else + { + //Support both bare hostnames and ones with uri + if (!target.Contains(Uri.SchemeDelimiter)) { + target = string.Concat(Uri.UriSchemeHttp, Uri.SchemeDelimiter, target); + } + Uri uri = new Uri(target); + hostname = uri.Host; + port = uri.Port; + } + + if (port == 0 || port == -1) {port = 5029;} + UdpClient udpClient = new UdpClient(port); udpClient.Client.ReceiveTimeout = 10000; Console.WriteLine(); @@ -458,7 +507,38 @@ static void Main(string[] args) Console.WriteLine(); Console.WriteLine("Downloaded all missing files. You can now close this window."); } - Console.ReadKey(); + + Console.WriteLine("\nNow launching SRB2Kart and connecting to " + hostname); + Console.WriteLine("Which renderer would you like to use? (Use Software if unsure)"); + Console.WriteLine("\t1 : Software (Default)"); + Console.WriteLine("\t2 : OpenGL"); + Console.WriteLine("\t3 : Cancel Launch"); + + string choice = Console.ReadLine(); + string opengl = ""; + + if (Int32.TryParse(choice, out intchoice)) + { + if (intchoice >= 3 || intchoice <= 0) + { + Console.WriteLine("Cancelling launch ..."); + return; + } + else if (intchoice == 2) + { + opengl = "-opengl"; + } + } + else + { + Console.WriteLine("Numbers allowed only ... exiting ..."); + return; + } + + ProcessStartInfo startinfo = new ProcessStartInfo(); + startinfo.FileName = "srb2kart.exe"; + startinfo.Arguments = "-connect " + hostname + " " + opengl; + Process.Start(startinfo); } } } From 44f3963df246ec656f5a52d462683237b8d88da2 Mon Sep 17 00:00:00 2001 From: Ashnal Date: Mon, 24 Feb 2020 21:45:49 -0500 Subject: [PATCH 2/2] Forgot some error checks and last minute tweaks --- HyuuSeeker/Program.cs | 83 ++++++++++++++++++++++++++++++------------- 1 file changed, 59 insertions(+), 24 deletions(-) diff --git a/HyuuSeeker/Program.cs b/HyuuSeeker/Program.cs index 70fa783..12b4558 100644 --- a/HyuuSeeker/Program.cs +++ b/HyuuSeeker/Program.cs @@ -266,7 +266,19 @@ static void Main(string[] args) List servers = new List(); HtmlWeb web = new HtmlWeb(); - HtmlDocument document = web.Load("http://hyuu.cc"); + HtmlDocument document; + + try + { + document = web.Load("http://hyuu.cc"); + } + catch + { + Console.WriteLine("Couldn't connect to http://hyuu.cc"); + Console.ReadKey(); + return; + } + HtmlNodeCollection nodes = document.DocumentNode.SelectNodes("//div[@class='hyuuseeker-enabled']/li"); Console.WriteLine("Choose a hyuuseeker supported server to play on!\n"); @@ -326,6 +338,7 @@ static void Main(string[] args) else { Console.WriteLine("You entered a number out of the list range, exiting ..."); + Console.ReadKey(); return; } } @@ -354,7 +367,7 @@ static void Main(string[] args) } catch { Console.WriteLine("Couldn't connect."); - Console.ReadLine(); + Console.ReadKey(); return; } //Console.WriteLine("Connected."); @@ -496,7 +509,10 @@ static void Main(string[] args) Console.WriteLine("Downloaded all available files, but some couldn't be retrieved:"); Console.WriteLine(String.Join(", ", failed.ToArray())); Console.WriteLine("You can now close this window."); - } else + Console.ReadKey(); + return; + } + else { Console.WriteLine("__ ______ _ _ _ _____ _____ ____ ____ _ "); Console.WriteLine("\\ \\ / / __ \\| | | ( ) /\\ | __ \\ / ____/ __ \\ / __ \\| | "); @@ -505,40 +521,59 @@ static void Main(string[] args) Console.WriteLine(" | | | |__| | |__| | / ____ \\| | \\ \\ | |___| |__| | |__| | |____ "); Console.WriteLine(" |_| \\____/ \\____/ /_/ \\_\\_| \\_\\ \\_____\\____/ \\____/|______|"); Console.WriteLine(); - Console.WriteLine("Downloaded all missing files. You can now close this window."); } - Console.WriteLine("\nNow launching SRB2Kart and connecting to " + hostname); - Console.WriteLine("Which renderer would you like to use? (Use Software if unsure)"); - Console.WriteLine("\t1 : Software (Default)"); - Console.WriteLine("\t2 : OpenGL"); - Console.WriteLine("\t3 : Cancel Launch"); - - string choice = Console.ReadLine(); - string opengl = ""; - if (Int32.TryParse(choice, out intchoice)) + if (hostname != "") { - if (intchoice >= 3 || intchoice <= 0) + Console.WriteLine("\nNow launching SRB2Kart and connecting to " + hostname); + Console.WriteLine("Which renderer would you like to use? (Use Software if unsure)"); + Console.WriteLine("\t1 : Software (Default)"); + Console.WriteLine("\t2 : OpenGL"); + Console.WriteLine("\t3 : Cancel Launch"); + + string choice = Console.ReadLine(); + string opengl = ""; + + if (Int32.TryParse(choice, out intchoice)) { - Console.WriteLine("Cancelling launch ..."); + if (intchoice >= 3 || intchoice <= 0) + { + Console.WriteLine("Cancelling launch ..."); + Console.ReadKey(); + return; + } + else if (intchoice == 2) + { + opengl = "-opengl"; + } + } + else + { + Console.WriteLine("Numbers allowed only ... exiting ..."); + Console.ReadKey(); return; } - else if (intchoice == 2) + + ProcessStartInfo startinfo = new ProcessStartInfo(); + startinfo.FileName = "srb2kart.exe"; + startinfo.Arguments = "-connect " + hostname + " " + opengl; + try + { + Process.Start(startinfo); + } + catch { - opengl = "-opengl"; + Console.WriteLine("Couldn't start " + startinfo.FileName + " " + startinfo.Arguments); + Console.ReadKey(); } } - else + else { - Console.WriteLine("Numbers allowed only ... exiting ..."); + Console.WriteLine("Downloaded all missing files. You can now close this window."); + Console.ReadKey(); return; } - - ProcessStartInfo startinfo = new ProcessStartInfo(); - startinfo.FileName = "srb2kart.exe"; - startinfo.Arguments = "-connect " + hostname + " " + opengl; - Process.Start(startinfo); } } }