mirror of
https://github.com/Tribufu/ServerManagers
synced 2026-08-08 20:21:06 +00:00
source code checkin
This commit is contained in:
parent
5f8fb2c825
commit
7e57b72e35
675 changed files with 168433 additions and 0 deletions
90
src/Plugin.Discord/Utils/NetworkUtils.cs
Normal file
90
src/Plugin.Discord/Utils/NetworkUtils.cs
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ServerManagerTool.Plugin.Discord
|
||||
{
|
||||
internal static class NetworkUtils
|
||||
{
|
||||
public static async Task<IPAddress> DiscoverPublicIPAsync()
|
||||
{
|
||||
using (var webClient = new WebClient())
|
||||
{
|
||||
try
|
||||
{
|
||||
var publicIP = await webClient.DownloadStringTaskAsync(Config.Default.PublicIPCheckUrl);
|
||||
|
||||
if (IPAddress.TryParse(publicIP, out IPAddress address))
|
||||
return address;
|
||||
|
||||
return IPAddress.None;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"ERROR: {nameof(DiscoverPublicIPAsync)}\r\n{ex.Message}");
|
||||
return IPAddress.None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<Version> CheckLatestVersionAsync(bool betaEnabled)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var webClient = new WebClient())
|
||||
{
|
||||
string latestVersion = null;
|
||||
|
||||
if (betaEnabled)
|
||||
latestVersion = await webClient.DownloadStringTaskAsync(Config.Default.LatestBetaVersionUrl);
|
||||
else
|
||||
latestVersion = await webClient.DownloadStringTaskAsync(Config.Default.LatestVersionUrl);
|
||||
|
||||
if (Version.TryParse(latestVersion, out Version version))
|
||||
return version;
|
||||
|
||||
return new Version();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"ERROR: {nameof(CheckLatestVersionAsync)}\r\n{ex.Message}");
|
||||
return new Version();
|
||||
}
|
||||
}
|
||||
|
||||
public static bool DownloadLatestVersion(string sourceUrl, string destinationFile)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var client = new WebClient())
|
||||
{
|
||||
client.DownloadFile(sourceUrl, destinationFile);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"ERROR: {nameof(DownloadLatestVersion)}\r\n{ex.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task PerformCallToAPIAsync(string pluginCode, IPAddress ipAddress)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var client = new WebClient())
|
||||
{
|
||||
var url = string.Format(Config.Default.PluginCallUrlFormat, pluginCode, ipAddress);
|
||||
await client.DownloadStringTaskAsync(url);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"ERROR: {nameof(PerformCallToAPIAsync)} - {pluginCode}; {ipAddress}\r\n{ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
15
src/Plugin.Discord/Utils/TaskUtils.cs
Normal file
15
src/Plugin.Discord/Utils/TaskUtils.cs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ServerManagerTool.Plugin.Discord
|
||||
{
|
||||
internal static class TaskUtils
|
||||
{
|
||||
public static readonly Task FinishedTask = Task.FromResult(true);
|
||||
|
||||
public static void DoNotWait(this Task task)
|
||||
{
|
||||
// Do nothing, let the task continue. Eliminates compiler warning about non-awaited tasks in an async method.
|
||||
}
|
||||
}
|
||||
}
|
||||
53
src/Plugin.Discord/Utils/VersionFeedUtils.cs
Normal file
53
src/Plugin.Discord/Utils/VersionFeedUtils.cs
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
using System;
|
||||
using System.ServiceModel.Syndication;
|
||||
using System.Xml;
|
||||
|
||||
namespace ServerManagerTool.Plugin.Discord
|
||||
{
|
||||
public static class VersionFeedUtils
|
||||
{
|
||||
public static VersionFeed LoadVersionFeed(string inputUri, string currentVersion)
|
||||
{
|
||||
try
|
||||
{
|
||||
var reader = XmlReader.Create(inputUri);
|
||||
var feed = SyndicationFeed.Load(reader);
|
||||
|
||||
var versionFeed = new VersionFeed
|
||||
{
|
||||
Id = feed.Id,
|
||||
Title = feed.Title?.Text,
|
||||
SubTitle = feed.Description?.Text,
|
||||
Link = feed.Links?[0].Uri,
|
||||
Updated = feed.LastUpdatedTime.ToLocalTime(),
|
||||
};
|
||||
|
||||
//Loop through all items in the SyndicationFeed
|
||||
foreach (var item in feed.Items)
|
||||
{
|
||||
var textContent = item.Content as TextSyndicationContent;
|
||||
|
||||
var versionFeedEntry = new VersionFeedEntry
|
||||
{
|
||||
Id = item.Id,
|
||||
Title = item.Title?.Text,
|
||||
Summary = item.Summary?.Text,
|
||||
Link = item.Links?[0].Uri,
|
||||
Updated = item.LastUpdatedTime.ToLocalTime(),
|
||||
Content = textContent?.Text,
|
||||
Author = item.Authors?[0].Name,
|
||||
|
||||
IsCurrent = (item.Summary?.Text ?? string.Empty).Equals(currentVersion),
|
||||
};
|
||||
versionFeed.Entries.Add(versionFeedEntry);
|
||||
}
|
||||
|
||||
return versionFeed;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return new VersionFeed();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
122
src/Plugin.Discord/Utils/WindowUtils.cs
Normal file
122
src/Plugin.Discord/Utils/WindowUtils.cs
Normal file
|
|
@ -0,0 +1,122 @@
|
|||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace ServerManagerTool.Plugin.Discord
|
||||
{
|
||||
public static class WindowUtils
|
||||
{
|
||||
public static void RemoveDefaultResourceDictionary(Window window, string defaultDictionary)
|
||||
{
|
||||
if (window == null)
|
||||
return;
|
||||
|
||||
var dictToRemove = window.Resources.MergedDictionaries.FirstOrDefault(d => d.Source.OriginalString.Contains(defaultDictionary));
|
||||
if (dictToRemove != null)
|
||||
{
|
||||
window.Resources.MergedDictionaries.Remove(dictToRemove);
|
||||
}
|
||||
}
|
||||
|
||||
public static void RemoveDefaultResourceDictionary(UserControl control, string defaultDictionary)
|
||||
{
|
||||
if (control == null)
|
||||
return;
|
||||
|
||||
var dictToRemove = control.Resources.MergedDictionaries.FirstOrDefault(d => d.Source.OriginalString.Contains(defaultDictionary));
|
||||
if (dictToRemove != null)
|
||||
{
|
||||
control.Resources.MergedDictionaries.Remove(dictToRemove);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finds a parent of a given item on the visual tree.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the queried item.</typeparam>
|
||||
/// <param name="child">A direct or indirect child of the queried item.</param>
|
||||
/// <returns>The first parent item that matches the submitted type parameter. If not matching item can be found, a null reference is being returned.</returns>
|
||||
public static T TryFindParent<T>(DependencyObject child)
|
||||
where T : DependencyObject
|
||||
{
|
||||
//get parent item
|
||||
DependencyObject parentObject = GetParentObject(child);
|
||||
|
||||
//we've reached the end of the tree
|
||||
if (parentObject == null) return null;
|
||||
|
||||
//check if the parent matches the type we're looking for
|
||||
T parent = parentObject as T;
|
||||
if (parent != null)
|
||||
return parent;
|
||||
|
||||
//use recursion to proceed with next level
|
||||
return TryFindParent<T>(parentObject);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This method is an alternative to WPF's <see cref="VisualTreeHelper.GetParent"/> method, which also supports content elements.
|
||||
/// Do note, that for content element, this method falls back to the logical tree of the element.
|
||||
/// </summary>
|
||||
/// <param name="child">The item to be processed.</param>
|
||||
/// <returns>The submitted item's parent, if available. Otherwise null.</returns>
|
||||
public static DependencyObject GetParentObject(DependencyObject child)
|
||||
{
|
||||
if (child == null) return null;
|
||||
var contentElement = child as ContentElement;
|
||||
|
||||
if (contentElement != null)
|
||||
{
|
||||
var parent = ContentOperations.GetParent(contentElement);
|
||||
if (parent != null) return parent;
|
||||
|
||||
var fce = contentElement as FrameworkContentElement;
|
||||
return fce?.Parent;
|
||||
}
|
||||
|
||||
//if it's not a ContentElement, rely on VisualTreeHelper
|
||||
return VisualTreeHelper.GetParent(child);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Recursively processes a given dependency object and all its children, and updates sources of all objects that use a binding expression on a given property.
|
||||
/// </summary>
|
||||
/// <param name="obj">The dependency object that marks a starting point. This could be a dialog window or a panel control that hosts bound controls.</param>
|
||||
/// <param name="properties">The properties to be updated if
|
||||
/// <paramref name="obj"/> or one of its childs provide it along with a binding expression.</param>
|
||||
public static void UpdateBindingSources(DependencyObject obj, params DependencyProperty[] properties)
|
||||
{
|
||||
foreach (var depProperty in properties)
|
||||
{
|
||||
//check whether the submitted object provides a bound property that matches the property parameters
|
||||
var be = BindingOperations.GetBindingExpression(obj, depProperty);
|
||||
be?.UpdateSource();
|
||||
}
|
||||
|
||||
int count = VisualTreeHelper.GetChildrenCount(obj);
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
//process child items recursively
|
||||
var childObject = VisualTreeHelper.GetChild(obj, i);
|
||||
UpdateBindingSources(childObject, properties);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to locate a given item within the visual tree, starting with the dependency object at a given position.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the element to be found on the visual tree of the element at the given location.</typeparam>
|
||||
/// <param name="reference">The main element which is used to perform hit testing.</param>
|
||||
/// <param name="point">The position to be evaluated on the origin.</param>
|
||||
public static T TryFindFromPoint<T>(UIElement reference, Point point)
|
||||
where T : DependencyObject
|
||||
{
|
||||
var element = reference.InputHitTest(point) as DependencyObject;
|
||||
if (element == null) return null;
|
||||
if (element is T) return (T)element;
|
||||
return TryFindParent<T>(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue