source code checkin

This commit is contained in:
Brett Hewitson 2021-01-07 16:23:23 +10:00
parent 5f8fb2c825
commit 7e57b72e35
675 changed files with 168433 additions and 0 deletions

View file

@ -0,0 +1,19 @@
using System;
using System.Windows.Data;
using System.Globalization;
using System.Linq;
namespace ServerManagerTool.Common.Converters
{
public class AllTrueMultiValueConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return !values.Any(v => !(v is bool) || !(bool)v);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotSupportedException("AllTrueConverter is a OneWay converter.");
}
}
}

View file

@ -0,0 +1,30 @@
using System;
using System.Globalization;
using System.Windows.Data;
namespace ServerManagerTool.Common.Converters
{
public class CommaDelimitedStringCountConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if(value == null)
{
return "0";
}
var strValue = value as string;
if(string.IsNullOrWhiteSpace(strValue))
{
return "0";
}
return strValue.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Length.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new InvalidOperationException("CommaDelimitedStringCountConverter can only be used OneWay.");
}
}
}

View file

@ -0,0 +1,56 @@
using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Markup;
namespace ServerManagerTool.Common.Converters
{
public class DoubleRangeValueConverter : MarkupExtension, IValueConverter
{
protected double MinValue { get; set; }
protected double MaxValue { get; set; }
public DoubleRangeValueConverter()
{
MinValue = int.MinValue;
MaxValue = int.MaxValue;
}
public DoubleRangeValueConverter(double minValue)
{
MinValue = minValue;
MaxValue = int.MaxValue;
}
public DoubleRangeValueConverter(double minValue, double maxValue)
{
MinValue = minValue;
MaxValue = maxValue;
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
double scaledValue = System.Convert.ToDouble(value);
var sliderValue = scaledValue;
sliderValue = Math.Max(MinValue, sliderValue);
sliderValue = Math.Min(MaxValue, sliderValue);
return sliderValue;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
var sliderValue = System.Convert.ToDouble(value);
sliderValue = Math.Max(MinValue, sliderValue);
sliderValue = Math.Min(MaxValue, sliderValue);
var scaledValue = sliderValue;
return scaledValue;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
}

View file

@ -0,0 +1,32 @@
using System;
using System.Globalization;
using System.Windows.Data;
namespace ServerManagerTool.Common.Converters
{
public class EnumConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null || parameter == null)
return false;
string checkValue = value.ToString();
string targetValue = parameter.ToString();
return checkValue.Equals(targetValue, StringComparison.InvariantCultureIgnoreCase);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null || parameter == null)
return null;
bool useValue = (bool)value;
string targetValue = parameter.ToString();
if (useValue)
return Enum.Parse(targetType, targetValue);
return null;
}
}
}

View file

@ -0,0 +1,27 @@
using System;
using System.Globalization;
using System.Windows.Data;
namespace ServerManagerTool.Common.Converters
{
public class EnumFlagsConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
if (value == null || parameter == null)
return false;
var flagsValue = (int)value;
var targetFlagValue = (int)Enum.Parse(value.GetType(), parameter.ToString());
return (flagsValue & targetFlagValue) == targetFlagValue;
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
throw new NotSupportedException("Cannot convert flags value back");
}
}
}

View file

@ -0,0 +1,25 @@
using System;
using System.Globalization;
using System.Windows.Data;
namespace ServerManagerTool.Common.Converters
{
public class FlagsEnumToBooleanConverter : IValueConverter
{
private int targetValue;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var mask = System.Convert.ToInt32(parameter);
this.targetValue = System.Convert.ToInt32(value);
return ((mask & this.targetValue) != 0);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
this.targetValue ^= System.Convert.ToInt32(parameter);
return Enum.Parse(targetType, this.targetValue.ToString());
}
}
}

View file

@ -0,0 +1,42 @@
using System;
using System.Globalization;
using System.Windows.Data;
namespace ServerManagerTool.Common.Converters
{
public class FloatToPercentageConverter : IValueConverter
{
public const int MIN_VALUE = 0;
public const int MAX_VALUE = 100;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var scaledValue = System.Convert.ToSingle(value);
var sliderValue = scaledValue * 100;
sliderValue = Math.Max(MIN_VALUE, sliderValue);
//sliderValue = Math.Min(MAX_VALUE, sliderValue);
return $"{sliderValue}%";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value.GetType() == typeof(string))
{
var stringValue = (string)value;
if (stringValue.EndsWith("%"))
{
stringValue = stringValue.Replace("%", "");
value = stringValue;
}
}
var sliderValue = System.Convert.ToSingle(value);
sliderValue = Math.Max(MIN_VALUE, sliderValue);
//sliderValue = Math.Min(MAX_VALUE, sliderValue);
var scaledValue = sliderValue / 100;
return scaledValue;
}
}
}

View file

@ -0,0 +1,32 @@
using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Markup;
namespace ServerManagerTool.Common.Converters
{
public class GreaterThanIntValueConverter : MarkupExtension, IValueConverter
{
protected int Operand { get; set; }
public GreaterThanIntValueConverter(int operand)
{
Operand = operand;
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return System.Convert.ToInt32(value) > Operand;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
}

View file

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
namespace ServerManagerTool.Common.Converters
{
public class HasStringValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if(value == null)
{
return false;
}
var strValue = value as string;
if(String.IsNullOrWhiteSpace(strValue))
{
return false;
}
return true;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new InvalidOperationException("IsNullConverter can only be used OneWay.");
}
}
}

View file

@ -0,0 +1,26 @@
using System;
using System.Globalization;
using System.Windows.Data;
namespace ServerManagerTool.Common.Converters
{
public class InstalledVersionConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var version = value as string;
if (string.IsNullOrWhiteSpace(version))
return "0.0";
if (version.Equals("0.0"))
return "0.0";
return version;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}

View file

@ -0,0 +1,56 @@
using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Markup;
namespace ServerManagerTool.Common.Converters
{
public class IntRangeValueConverter : MarkupExtension, IValueConverter
{
protected int MinValue { get; set; }
protected int MaxValue { get; set; }
public IntRangeValueConverter()
{
MinValue = int.MinValue;
MaxValue = int.MaxValue;
}
public IntRangeValueConverter(int minValue)
{
MinValue = minValue;
MaxValue = int.MaxValue;
}
public IntRangeValueConverter(int minValue, int maxValue)
{
MinValue = minValue;
MaxValue = maxValue;
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
double scaledValue = System.Convert.ToInt32(value);
var sliderValue = scaledValue;
sliderValue = Math.Max(MinValue, sliderValue);
sliderValue = Math.Min(MaxValue, sliderValue);
return sliderValue;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
var sliderValue = System.Convert.ToInt32(value);
sliderValue = Math.Max(MinValue, sliderValue);
sliderValue = Math.Min(MaxValue, sliderValue);
var scaledValue = sliderValue;
return scaledValue;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
}

View file

@ -0,0 +1,19 @@
using System;
using System.Globalization;
using System.Windows.Data;
namespace ServerManagerTool.Common.Converters
{
public class InvertBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return !(bool)value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return !(bool)value;
}
}
}

View file

@ -0,0 +1,32 @@
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
namespace ServerManagerTool.Common.Converters
{
public class InvertBooleanToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool flag = false;
if (value is bool)
{
flag = (bool)value;
}
else if (value is bool?)
{
bool? nullable = (bool?)value;
flag = nullable.HasValue && nullable.Value;
}
return flag ? Visibility.Collapsed : Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is Visibility)
return (Visibility)value == Visibility.Collapsed;
return false;
}
}
}

View file

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
namespace ServerManagerTool.Common.Converters
{
public class IsNullOrWhiteSpaceValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if(value == null)
{
return true;
}
var strValue = value as string;
if(String.IsNullOrWhiteSpace(strValue))
{
return true;
}
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new InvalidOperationException("IsNullConverter can only be used OneWay.");
}
}
}

View file

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
namespace ServerManagerTool.Common.Converters
{
public class IsNullValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value == null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new InvalidOperationException("IsNullConverter can only be used OneWay.");
}
}
}

View file

@ -0,0 +1,39 @@
using System;
using System.Globalization;
using System.Windows.Data;
namespace ServerManagerTool.Common.Converters
{
public class MinutesToTimeValueConverter : IValueConverter
{
public const int MAX_VALUE_HOURS = 24 * 365;
public const int MAX_VALUE_MINUTES = 59;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
// Value is seconds since midnight.
var totalMinutes = (int)value;
var hours = Math.Min(Math.Max(totalMinutes / 60, 0), MAX_VALUE_HOURS);
var minutes = Math.Min(Math.Max(totalMinutes % 60, 0), MAX_VALUE_MINUTES);
return String.Format("{0:00}:{1:00}", hours, minutes);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
var strTime = (string)value;
var split = strTime.Split(':');
if(split.Length != 2)
{
return 0;
}
int hours;
Int32.TryParse(split[0], out hours);
int minutes;
Int32.TryParse(split[1], out minutes);
return hours * 60 + minutes;
}
}
}

View file

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
namespace ServerManagerTool.Common.Converters
{
public class NullValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value ?? parameter;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new InvalidOperationException("IsNullConverter can only be used OneWay.");
}
}
}

View file

@ -0,0 +1,34 @@
using System;
using System.Globalization;
using System.Windows.Data;
namespace ServerManagerTool.Common.Converters
{
public class NullableHasValueConverter : IValueConverter
{
public object convertValue = null;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
convertValue = value;
return value != null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool && (bool)value)
{
if (targetType == typeof(bool) || targetType == typeof(bool?))
return default(bool);
else if (targetType == typeof(int) || targetType == typeof(int?))
return default(int);
else if (targetType == typeof(float) || targetType == typeof(float?))
return default(float);
return string.Empty;
}
return null;
}
}
}

View file

@ -0,0 +1,49 @@
using ServerManagerTool.Common.Utils;
using System;
using System.Globalization;
using System.Numerics;
using System.Windows.Data;
namespace ServerManagerTool.Common.Converters
{
public class ProcessorAffinityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (!BigInteger.TryParse(value.ToString(), out BigInteger affinity))
return "Invalid";
if (!ProcessUtils.IsProcessorAffinityValid(affinity))
return "Invalid";
if (affinity == BigInteger.Zero)
return "All";
var result = string.Empty;
var delimiter = string.Empty;
var index = 0;
while (true)
{
var cpuValue = (BigInteger)Math.Pow(2, index);
if (cpuValue > affinity)
break;
if ((affinity & cpuValue) == cpuValue)
{
result = $"{result}{delimiter}{index}";
delimiter = ", ";
}
index++;
}
return result;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new InvalidOperationException("ProcessorAffinityConverter can only be used OneWay.");
}
}
}

View file

@ -0,0 +1,32 @@
using System;
using System.Globalization;
using System.Windows.Data;
namespace ServerManagerTool.Common.Converters
{
public class SecondsToHoursConverter : IValueConverter
{
public const int MIN_VALUE = 0;
public const int MAX_VALUE = int.MaxValue;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
double scaledValue = System.Convert.ToInt32(value);
var sliderValue = (int)TimeSpan.FromSeconds(scaledValue).TotalHours;
sliderValue = Math.Max(MIN_VALUE, sliderValue);
sliderValue = Math.Min(MAX_VALUE, sliderValue);
return sliderValue;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
var sliderValue = System.Convert.ToInt32(value);
sliderValue = Math.Max(MIN_VALUE, sliderValue);
sliderValue = Math.Min(MAX_VALUE, sliderValue);
var scaledValue = (int)TimeSpan.FromHours(sliderValue).TotalSeconds;
return scaledValue;
}
}
}

View file

@ -0,0 +1,32 @@
using System;
using System.Globalization;
using System.Windows.Data;
namespace ServerManagerTool.Common.Converters
{
public class SecondsToMinutesConverter : IValueConverter
{
public const int MIN_VALUE = 0;
public const int MAX_VALUE = int.MaxValue;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
double scaledValue = System.Convert.ToInt32(value);
var sliderValue = (int)TimeSpan.FromSeconds(scaledValue).TotalMinutes;
sliderValue = Math.Max(MIN_VALUE, sliderValue);
sliderValue = Math.Min(MAX_VALUE, sliderValue);
return sliderValue;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
var sliderValue = System.Convert.ToInt32(value);
sliderValue = Math.Max(MIN_VALUE, sliderValue);
sliderValue = Math.Min(MAX_VALUE, sliderValue);
var scaledValue = (int)TimeSpan.FromMinutes(sliderValue).TotalSeconds;
return scaledValue;
}
}
}

View file

@ -0,0 +1,35 @@
using System;
using System.Windows.Data;
namespace ServerManagerTool.Common.Converters
{
public class SecondsToTimeValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// Value is seconds since midnight.
var seconds = (int)value;
var hours = Math.Min(Math.Max(seconds / 3600, 0), 23);
var minutes = Math.Min(Math.Max((seconds % 3600) / 60, 0), 59);
return String.Format("{0:00}:{1:00}", hours, minutes);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var strTime = (string)value;
var split = strTime.Split(':');
if(split.Length != 2)
{
return 0;
}
int hours;
Int32.TryParse(split[0], out hours);
int minutes;
Int32.TryParse(split[1], out minutes);
return hours * 3600 + minutes * 60;
}
}
}

View file

@ -0,0 +1,21 @@
using ServerManagerTool.Common.Utils;
using System;
using System.Globalization;
using System.Windows.Data;
namespace ServerManagerTool.Common.Converters
{
public class UnixTimeToDateTimeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
int unixTimestamp = (int)value;
return DateTimeUtils.UnixTimeStampToDateTime(unixTimestamp).ToString(culture);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return 0;
}
}
}