Fixed Webservices Mod availability tester

This commit is contained in:
Yoshi Askharoun
2022-01-02 21:30:16 -06:00
parent 129a0ad283
commit d82e85a90e
3 changed files with 73 additions and 42 deletions
+65 -39
View File
@@ -3,6 +3,7 @@ using OwlCore.AbstractUI.Models;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Net;
using System.Threading.Tasks; using System.Threading.Tasks;
using static ZuneModCore.Mods.FeaturesOverrideMod; using static ZuneModCore.Mods.FeaturesOverrideMod;
@@ -16,6 +17,9 @@ namespace ZuneModCore.Mods
private const string WEBSERVICE_ICON_SUCCESS = "\uE73E"; private const string WEBSERVICE_ICON_SUCCESS = "\uE73E";
private const string WEBSERVICE_ICON_UNREACHABLE = "\uF384"; private const string WEBSERVICE_ICON_UNREACHABLE = "\uF384";
private const string WEBSERVICE_ICON_UNAVAILABLE = "\uE894"; 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); public override string Id => nameof(WebservicesMod);
@@ -33,7 +37,7 @@ namespace ZuneModCore.Mods
Title = string.Empty, Title = string.Empty,
Items = new List<AbstractUIElement> Items = new List<AbstractUIElement>
{ {
new AbstractTextBox("hostBox", "zunes.me", "zune.net") new AbstractTextBox("hostBox", string.Empty, "zune.net")
{ {
Title = "Host", Title = "Host",
TooltipText = "The host where the replacement servers are located. Must be the same length as \"zune.net\"." 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() public override Task Init()
{ {
Threading.SetPrimarySynchronizationContext(System.Threading.SynchronizationContext.Current!);
AbstractTextBox newHostBox = (AbstractTextBox)OptionsUI!.Items[0]; 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.ValueChanged += OnHostChanged;
newHostBox.Value = "zunes.me";
return Task.CompletedTask; return Task.CompletedTask;
} }
@@ -255,5 +222,64 @@ namespace ZuneModCore.Mods
return ex.Message; 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<string?> 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;
}
}
} }
} }
+3 -3
View File
@@ -1,13 +1,13 @@
Microsoft Visual Studio Solution File, Format Version 12.00 Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16 # Visual Studio Version 17
VisualStudioVersion = 16.0.31205.134 VisualStudioVersion = 17.0.31919.166
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ZuneModdingHelper", "ZuneModdingHelper\ZuneModdingHelper.csproj", "{A4AE840D-BC01-4E60-B106-289218D9479F}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ZuneModdingHelper", "ZuneModdingHelper\ZuneModdingHelper.csproj", "{A4AE840D-BC01-4E60-B106-289218D9479F}"
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ZuneModCore", "ZuneModCore\ZuneModCore.csproj", "{4B1587B5-0DE2-4E94-87FC-239D5E7FC4E7}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ZuneModCore", "ZuneModCore\ZuneModCore.csproj", "{4B1587B5-0DE2-4E94-87FC-239D5E7FC4E7}"
EndProject 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 EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
+5
View File
@@ -34,11 +34,15 @@ namespace ZuneModdingHelper
public MainWindow() public MainWindow()
{ {
InitializeComponent(); InitializeComponent();
OwlCore.Threading.SetPrimarySynchronizationContext(System.Threading.SynchronizationContext.Current!);
OwlCore.Threading.SetPrimaryThreadInvokeHandler(RunOnUI);
ThemeManager.Current.ThemeSyncMode = ThemeSyncMode.SyncWithAppMode; ThemeManager.Current.ThemeSyncMode = ThemeSyncMode.SyncWithAppMode;
ThemeManager.Current.SyncTheme(); 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) private async void Window_Loaded(object sender, RoutedEventArgs e)
{ {
ModList.ItemsSource = Mod.AvailableMods; ModList.ItemsSource = Mod.AvailableMods;
@@ -76,6 +80,7 @@ namespace ZuneModdingHelper
if (mod.OptionsUI != null) if (mod.OptionsUI != null)
{ {
var optionsDialog = new AbstractUIGroupDialog(mod.OptionsUI); var optionsDialog = new AbstractUIGroupDialog(mod.OptionsUI);
optionsDialog.Title = optionsDialog.Title + " | " + mod.Title;
bool? optionsResult = optionsDialog.ShowDialog(); bool? optionsResult = optionsDialog.ShowDialog();
if (!(optionsResult.HasValue && optionsResult.Value)) if (!(optionsResult.HasValue && optionsResult.Value))
{ {