From d82e85a90edc9fa33244bb8be523d12fef886bf9 Mon Sep 17 00:00:00 2001 From: Yoshi Askharoun Date: Sun, 2 Jan 2022 21:30:16 -0600 Subject: [PATCH] Fixed Webservices Mod availability tester --- ZuneModCore/Mods/WebservicesMod.cs | 104 +++++++++++++++++---------- ZuneModdingHelper.sln | 6 +- ZuneModdingHelper/MainWindow.xaml.cs | 5 ++ 3 files changed, 73 insertions(+), 42 deletions(-) diff --git a/ZuneModCore/Mods/WebservicesMod.cs b/ZuneModCore/Mods/WebservicesMod.cs index 4430d4b..4513915 100644 --- a/ZuneModCore/Mods/WebservicesMod.cs +++ b/ZuneModCore/Mods/WebservicesMod.cs @@ -3,6 +3,7 @@ using OwlCore.AbstractUI.Models; using System; using System.Collections.Generic; using System.IO; +using System.Net; using System.Threading.Tasks; using static ZuneModCore.Mods.FeaturesOverrideMod; @@ -16,6 +17,9 @@ namespace ZuneModCore.Mods private const string WEBSERVICE_ICON_SUCCESS = "\uE73E"; private const string WEBSERVICE_ICON_UNREACHABLE = "\uF384"; private const string WEBSERVICE_ICON_UNAVAILABLE = "\uE894"; + private const string WEBSERVICE_SUBTITLE_TESTING = "Testing..."; + private const string WEBSERVICE_SUBTITLE_SUCCESS = "OK"; + private const string WEBSERVICE_SUBTITLE_UNREACHABLE = "Unreachable"; public override string Id => nameof(WebservicesMod); @@ -33,7 +37,7 @@ namespace ZuneModCore.Mods Title = string.Empty, Items = new List { - new AbstractTextBox("hostBox", "zunes.me", "zune.net") + new AbstractTextBox("hostBox", string.Empty, "zune.net") { Title = "Host", TooltipText = "The host where the replacement servers are located. Must be the same length as \"zune.net\"." @@ -76,46 +80,9 @@ namespace ZuneModCore.Mods public override Task Init() { - Threading.SetPrimarySynchronizationContext(System.Threading.SynchronizationContext.Current!); - AbstractTextBox newHostBox = (AbstractTextBox)OptionsUI!.Items[0]; - System.Net.NetworkInformation.Ping ping = new(); - async void OnHostChanged(object? sender, string newHost) - { - ping.SendAsyncCancel(); - AbstractDataList list = (AbstractDataList)OptionsUI!.Items[1]; - // Reset all statuses - foreach (AbstractUIMetadata metadata in list.Items) - metadata.IconCode = WEBSERVICE_ICON_TESTING; - - // Update all statuses - foreach (AbstractUIMetadata metadata in list.Items) - { - string url = (metadata.Id.Split('_')[1] + '.' + newHost).Replace("www.", string.Empty); - try - { - var pingResult = await ping.SendPingAsync(url); - if (pingResult.Status == System.Net.NetworkInformation.IPStatus.Success) - { - metadata.IconCode = WEBSERVICE_ICON_SUCCESS; - metadata.ImagePath = "http://free.pagepeeker.com/v2/thumbs.php?size=m&url=" + url; - } - else - { - metadata.IconCode = WEBSERVICE_ICON_UNAVAILABLE; - metadata.Subtitle = pingResult.Status.ToString(); - } - } - catch (System.Net.NetworkInformation.PingException ex) - { - metadata.IconCode = WEBSERVICE_ICON_UNREACHABLE; - if (ex.InnerException?.Message != null) - metadata.Subtitle = ex.InnerException.Message; - } - } - } - OnHostChanged(newHostBox, newHostBox.Value); newHostBox.ValueChanged += OnHostChanged; + newHostBox.Value = "zunes.me"; return Task.CompletedTask; } @@ -255,5 +222,64 @@ namespace ZuneModCore.Mods return ex.Message; } } + + async void OnHostChanged(object? sender, string newHost) + { + AbstractDataList list = (AbstractDataList)OptionsUI!.Items[1]; + + // Reset all statuses + foreach (AbstractUIMetadata metadata in list.Items) + { + metadata.IconCode = WEBSERVICE_ICON_TESTING; + metadata.Subtitle = WEBSERVICE_SUBTITLE_TESTING; + } + + // Update all statuses + foreach (AbstractUIMetadata metadata in list.Items) + { + string url = "http://" + (metadata.Id.Split('_')[1] + '.' + newHost).Replace("www.", string.Empty); + string? pingResult = await Ping(url); + + if (pingResult == null) + { + metadata.IconCode = WEBSERVICE_ICON_SUCCESS; + metadata.Subtitle = WEBSERVICE_SUBTITLE_SUCCESS; + } + else if (pingResult != string.Empty) + { + metadata.IconCode = WEBSERVICE_ICON_UNAVAILABLE; + metadata.Subtitle = pingResult; + } + else + { + metadata.IconCode = WEBSERVICE_ICON_UNREACHABLE; + metadata.Subtitle = WEBSERVICE_SUBTITLE_UNREACHABLE; + } + } + } + + private async Task Ping(string url) + { + try + { + HttpWebRequest request = WebRequest.CreateHttp(url); + request.Timeout = 3000; + request.AllowAutoRedirect = true; + request.Method = "GET"; + + using var response = await request.GetResponseAsync(); + return null; + } + catch (WebException webEx) + { + if (webEx.InnerException?.InnerException != null) + return webEx.InnerException.InnerException.Message; + return webEx?.Message ?? string.Empty; + } + catch (Exception ex) + { + return ex?.Message ?? string.Empty; + } + } } } diff --git a/ZuneModdingHelper.sln b/ZuneModdingHelper.sln index 926a14d..f3a7bd6 100644 --- a/ZuneModdingHelper.sln +++ b/ZuneModdingHelper.sln @@ -1,13 +1,13 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.31205.134 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31919.166 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ZuneModdingHelper", "ZuneModdingHelper\ZuneModdingHelper.csproj", "{A4AE840D-BC01-4E60-B106-289218D9479F}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ZuneModCore", "ZuneModCore\ZuneModCore.csproj", "{4B1587B5-0DE2-4E94-87FC-239D5E7FC4E7}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OwlCore.Wpf", "OwlCore.Wpf\OwlCore.Wpf.csproj", "{BD87C032-C255-409E-8402-2DE16BFCE801}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OwlCore.Wpf", "OwlCore.Wpf\OwlCore.Wpf.csproj", "{BD87C032-C255-409E-8402-2DE16BFCE801}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/ZuneModdingHelper/MainWindow.xaml.cs b/ZuneModdingHelper/MainWindow.xaml.cs index c13ff80..ddfaea3 100644 --- a/ZuneModdingHelper/MainWindow.xaml.cs +++ b/ZuneModdingHelper/MainWindow.xaml.cs @@ -34,11 +34,15 @@ namespace ZuneModdingHelper public MainWindow() { InitializeComponent(); + OwlCore.Threading.SetPrimarySynchronizationContext(System.Threading.SynchronizationContext.Current!); + OwlCore.Threading.SetPrimaryThreadInvokeHandler(RunOnUI); ThemeManager.Current.ThemeSyncMode = ThemeSyncMode.SyncWithAppMode; ThemeManager.Current.SyncTheme(); } + private async System.Threading.Tasks.Task RunOnUI(Action action) => await Dispatcher.BeginInvoke(action); + private async void Window_Loaded(object sender, RoutedEventArgs e) { ModList.ItemsSource = Mod.AvailableMods; @@ -76,6 +80,7 @@ namespace ZuneModdingHelper if (mod.OptionsUI != null) { var optionsDialog = new AbstractUIGroupDialog(mod.OptionsUI); + optionsDialog.Title = optionsDialog.Title + " | " + mod.Title; bool? optionsResult = optionsDialog.ShowDialog(); if (!(optionsResult.HasValue && optionsResult.Value)) {