diff --git a/PotatoWallClient.xaml b/App.xaml similarity index 71% rename from PotatoWallClient.xaml rename to App.xaml index a32e62b..a7c1dad 100644 --- a/PotatoWallClient.xaml +++ b/App.xaml @@ -1,20 +1,12 @@ - + - - - - - + diff --git a/App.xaml.cs b/App.xaml.cs new file mode 100644 index 0000000..17f6e27 --- /dev/null +++ b/App.xaml.cs @@ -0,0 +1,44 @@ +/* + * This file is part of PotatoWall distribution (https://github.com/poqdavid/PotatoWall or http://poqdavid.github.io/PotatoWall/). + * Copyright (c) 2023 POQDavid + * Copyright (c) contributors + * + * PotatoWall is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * PotatoWall is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with PotatoWall. If not, see . + */ + +namespace PotatoWall; + +using System.Windows; + +/// +/// Copyright (c) POQDavid. All rights reserved. +/// +/// POQDavid +/// This is the App class. +public partial class App : Application +{ + protected override void OnStartup(StartupEventArgs e) + { + PotatoWallClient.InitializeComponent(); + + base.OnStartup(e); + + PotatoWallClient.Run(); + } + + private void Application_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e) + { + PotatoWallClient.Logger.Information("I see loaded!!!"); + } +} \ No newline at end of file diff --git a/Converters/ComboBoxSelectedValueConverter.cs b/Converters/ComboBoxSelectedValueConverter.cs index f3bd623..f281b75 100644 --- a/Converters/ComboBoxSelectedValueConverter.cs +++ b/Converters/ComboBoxSelectedValueConverter.cs @@ -1,6 +1,6 @@ /* * This file is part of PotatoWall distribution (https://github.com/poqdavid/PotatoWall or http://poqdavid.github.io/PotatoWall/). - * Copyright (c) 2021 POQDavid + * Copyright (c) 2023 POQDavid * Copyright (c) contributors * * PotatoWall is free software: you can redistribute it and/or modify diff --git a/Converters/DialogHostStackPanelHeightConverter.cs b/Converters/DialogHostStackPanelHeightConverter.cs index 86b0338..d8e9690 100644 --- a/Converters/DialogHostStackPanelHeightConverter.cs +++ b/Converters/DialogHostStackPanelHeightConverter.cs @@ -1,6 +1,6 @@ /* * This file is part of PotatoWall distribution (https://github.com/poqdavid/PotatoWall or http://poqdavid.github.io/PotatoWall/). - * Copyright (c) 2021 POQDavid + * Copyright (c) 2023 POQDavid * Copyright (c) contributors * * PotatoWall is free software: you can redistribute it and/or modify @@ -29,7 +29,7 @@ public class DialogHostStackPanelHeightConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { - return (double)value - double.Parse((string)parameter, Thread.CurrentThread.CurrentCulture); + return value.CastTo() - double.Parse(parameter.CastTo(), Thread.CurrentThread.CurrentCulture); } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) diff --git a/Converters/DialogHostStackPanelWidthConverter.cs b/Converters/DialogHostStackPanelWidthConverter.cs index 36c447a..7f67f6c 100644 --- a/Converters/DialogHostStackPanelWidthConverter.cs +++ b/Converters/DialogHostStackPanelWidthConverter.cs @@ -1,6 +1,6 @@ /* * This file is part of PotatoWall distribution (https://github.com/poqdavid/PotatoWall or http://poqdavid.github.io/PotatoWall/). - * Copyright (c) 2021 POQDavid + * Copyright (c) 2023 POQDavid * Copyright (c) contributors * * PotatoWall is free software: you can redistribute it and/or modify @@ -29,7 +29,7 @@ public class DialogHostStackPanelWidthConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { - return (double)value - double.Parse((string)parameter, Thread.CurrentThread.CurrentCulture); + return value.CastTo() - double.Parse(parameter.CastTo(), Thread.CurrentThread.CurrentCulture); } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) diff --git a/Converters/IntArrayConverter.cs b/Converters/IntArrayConverter.cs new file mode 100644 index 0000000..ae0a342 --- /dev/null +++ b/Converters/IntArrayConverter.cs @@ -0,0 +1,49 @@ +/* + * This file is part of PotatoWall distribution (https://github.com/poqdavid/PotatoWall or http://poqdavid.github.io/PotatoWall/). + * Copyright (c) 2023 POQDavid + * Copyright (c) contributors + * + * PotatoWall is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * PotatoWall is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with PotatoWall. If not, see . + */ + +namespace PotatoWall.Converters; + +// +// Copyright (c) POQDavid. All rights reserved. +// +// POQDavid +// This is the IntArrayConverter class. + +public class IntArrayConverter : IValueConverter +{ + public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) + { + return (targetType.FullName, value.GetType().FullName) switch + { + ("System.String", "System.Int32[]") => value.CastTo().ToCSV(), + ("System.String", "System.UInt32[]") => value.CastTo().ToCSV(), + _ => throw new NotImplementedException("Not implemented."), + }; + } + + public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) + { + return (targetType.FullName, value.GetType().FullName) switch + { + ("System.Int32[]", "System.String") => value.CastTo().CSVToArray(true).Select(x => int.Parse(x)).ToArray(), + ("System.UInt32[]", "System.String") => value.CastTo().CSVToArray(true).Select(x => uint.Parse(x)).ToArray(), + _ => throw new NotImplementedException("Not implemented."), + }; + } +} \ No newline at end of file diff --git a/Converters/MultiConverter.cs b/Converters/MultiConverter.cs index 9ab1e28..2a22d90 100644 --- a/Converters/MultiConverter.cs +++ b/Converters/MultiConverter.cs @@ -1,6 +1,6 @@ /* * This file is part of PotatoWall distribution (https://github.com/poqdavid/PotatoWall or http://poqdavid.github.io/PotatoWall/). - * Copyright (c) 2021 POQDavid + * Copyright (c) 2023 POQDavid * Copyright (c) contributors * * PotatoWall is free software: you can redistribute it and/or modify diff --git a/Extensions/ArrayExtensions.cs b/Extensions/ArrayExtensions.cs new file mode 100644 index 0000000..6897da4 --- /dev/null +++ b/Extensions/ArrayExtensions.cs @@ -0,0 +1,64 @@ +/* + * This file is part of PotatoWall distribution (https://github.com/poqdavid/PotatoWall or http://poqdavid.github.io/PotatoWall/). + * Copyright (c) 2023 POQDavid + * Copyright (c) contributors + * + * PotatoWall is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * PotatoWall is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with PotatoWall. If not, see . + */ + +namespace PotatoWall.Extensions; + +// +// Copyright (c) POQDavid. All rights reserved. +// +// POQDavid +// This is the ArrayExtensions class. + +public static class ArrayExtensions +{ + /// + /// Returns a CSV string of the int array + /// + /// + /// CSV string + public static string ToCSV(this int[] array) + { + return String.Join(", ", array); + } + + /// + /// Returns a CSV string of the uint array + /// + /// + /// CSV string + public static string ToCSV(this uint[] array) + { + return String.Join(", ", array); + } + + /// + /// Returns a CSV string of the int array + /// + /// + /// CSV without whitespace + /// CSV string + public static string ToCSV(this int[] array, bool withoutspace) + { + return withoutspace switch + { + true => String.Join(",", array).RemoveWhitespace(), + false => String.Join(", ", array), + }; + } +} \ No newline at end of file diff --git a/Extensions/DictionaryExtensions.cs b/Extensions/DictionaryExtensions.cs new file mode 100644 index 0000000..e4962fc --- /dev/null +++ b/Extensions/DictionaryExtensions.cs @@ -0,0 +1,17 @@ +namespace PotatoWall.Extensions; + +public static class DictionaryExtensions +{ + public static TValue Pop(this Dictionary dictionary, TKey key) + { + if (dictionary.TryGetValue(key, out TValue temp)) + { + dictionary.Remove(key); + return temp; + } + else + { + throw new KeyNotFoundException($"Key {key} not found in dictionary."); + } + } +} \ No newline at end of file diff --git a/Extensions/DispatcherExtensions.cs b/Extensions/DispatcherExtensions.cs index 86efe35..d573217 100644 --- a/Extensions/DispatcherExtensions.cs +++ b/Extensions/DispatcherExtensions.cs @@ -1,6 +1,6 @@ /* * This file is part of PotatoWall distribution (https://github.com/poqdavid/PotatoWall or http://poqdavid.github.io/PotatoWall/). - * Copyright (c) 2021 POQDavid + * Copyright (c) 2023 POQDavid * Copyright (c) contributors * * PotatoWall is free software: you can redistribute it and/or modify diff --git a/Extensions/FlowDocumentExtensions.cs b/Extensions/FlowDocumentExtensions.cs index 4ac6f3d..0aa8b3a 100644 --- a/Extensions/FlowDocumentExtensions.cs +++ b/Extensions/FlowDocumentExtensions.cs @@ -1,6 +1,6 @@ /* * This file is part of PotatoWall distribution (https://github.com/poqdavid/PotatoWall or http://poqdavid.github.io/PotatoWall/). - * Copyright (c) 2021 POQDavid + * Copyright (c) 2023 POQDavid * Copyright (c) contributors * * PotatoWall is free software: you can redistribute it and/or modify diff --git a/Extensions/FrameworkElementExtensions.cs b/Extensions/FrameworkElementExtensions.cs index aae0249..a87ff1e 100644 --- a/Extensions/FrameworkElementExtensions.cs +++ b/Extensions/FrameworkElementExtensions.cs @@ -1,6 +1,6 @@ /* * This file is part of PotatoWall distribution (https://github.com/poqdavid/PotatoWall or http://poqdavid.github.io/PotatoWall/). - * Copyright (c) 2021 POQDavid + * Copyright (c) 2023 POQDavid * Copyright (c) contributors * * PotatoWall is free software: you can redistribute it and/or modify @@ -25,7 +25,6 @@ namespace PotatoWall.Extensions; // POQDavid // This is the FrameworkElementExtensions class. -[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0004:Cast is redundant", Justification = "The cast is needed")] public static class FrameworkElementExtensions { public static T GetTemplatedParent(this FrameworkElement o) where T : DependencyObject diff --git a/Extensions/IntegralTypeExtensions.cs b/Extensions/IntegralTypeExtensions.cs index ceb2bcc..77e6b72 100644 --- a/Extensions/IntegralTypeExtensions.cs +++ b/Extensions/IntegralTypeExtensions.cs @@ -1,6 +1,6 @@ /* * This file is part of PotatoWall distribution (https://github.com/poqdavid/PotatoWall or http://poqdavid.github.io/PotatoWall/). - * Copyright (c) 2021 POQDavid + * Copyright (c) 2023 POQDavid * Copyright (c) contributors * * PotatoWall is free software: you can redistribute it and/or modify diff --git a/Extensions/ListBoxExtensions.cs b/Extensions/ListBoxExtensions.cs index 519d19b..8c352bd 100644 --- a/Extensions/ListBoxExtensions.cs +++ b/Extensions/ListBoxExtensions.cs @@ -1,6 +1,6 @@ /* * This file is part of PotatoWall distribution (https://github.com/poqdavid/PotatoWall or http://poqdavid.github.io/PotatoWall/). - * Copyright (c) 2021 POQDavid + * Copyright (c) 2023 POQDavid * Copyright (c) contributors * * PotatoWall is free software: you can redistribute it and/or modify diff --git a/Extensions/ObjectExtensions.cs b/Extensions/ObjectExtensions.cs new file mode 100644 index 0000000..c91400e --- /dev/null +++ b/Extensions/ObjectExtensions.cs @@ -0,0 +1,69 @@ +/* + * This file is part of PotatoWall distribution (https://github.com/poqdavid/PotatoWall or http://poqdavid.github.io/PotatoWall/). + * Copyright (c) 2023 POQDavid + * Copyright (c) contributors + * + * PotatoWall is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * PotatoWall is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with PotatoWall. If not, see . + */ + +namespace PotatoWall.Extensions; + +// +// Copyright (c) POQDavid. All rights reserved. +// +// POQDavid +// This is the ObjectExtensions class. + +public static class ObjectExtensions +{ + /// + /// Cast's a System.Object to T + /// + /// Type to cast to + /// + /// + /// + public static T CastTo(this object value) + { + if (value == null) throw new ArgumentNullException(nameof(value)); + + if (value is T t) return t; + + throw new Exception($"Casting from {value.GetType().FullName} to {typeof(T).FullName} wasn't successful."); + } + + /// + /// Cast's a System.Object to T + /// + /// Type to cast to + /// + /// + /// + public static T TryCastTo(this object value, out bool success) + { + success = true; + + if (value == null) + { + success = false; + return default; + } + + if (value is T t) + return t; + + success = false; + return default; + } +} \ No newline at end of file diff --git a/Extensions/PaletteHelperExtensions.cs b/Extensions/PaletteHelperExtensions.cs index 14a8af2..c366e75 100644 --- a/Extensions/PaletteHelperExtensions.cs +++ b/Extensions/PaletteHelperExtensions.cs @@ -119,7 +119,7 @@ public static void ReplaceAccentColor(this PaletteHelper paletteHelper, string n /// The root dictionary to start searching at. Null means using Application.Current.Resources private static void ReplaceEntry(object entryName, object newValue, ResourceDictionary parentDictionary = null) { - if (parentDictionary == null) { parentDictionary = Application.Current.Resources; } + parentDictionary ??= Application.Current.Resources; if (parentDictionary.Contains(entryName)) { diff --git a/Extensions/ParagraphExtentions.cs b/Extensions/ParagraphExtentions.cs index e8fd8bd..9369b03 100644 --- a/Extensions/ParagraphExtentions.cs +++ b/Extensions/ParagraphExtentions.cs @@ -1,6 +1,6 @@ /* * This file is part of PotatoWall distribution (https://github.com/poqdavid/PotatoWall or http://poqdavid.github.io/PotatoWall/). - * Copyright (c) 2021 POQDavid + * Copyright (c) 2023 POQDavid * Copyright (c) contributors * * PotatoWall is free software: you can redistribute it and/or modify diff --git a/Extensions/RichTextBoxExtensions.cs b/Extensions/RichTextBoxExtensions.cs index 0dd4aa4..20db00d 100644 --- a/Extensions/RichTextBoxExtensions.cs +++ b/Extensions/RichTextBoxExtensions.cs @@ -1,6 +1,6 @@ /* * This file is part of PotatoWall distribution (https://github.com/poqdavid/PotatoWall or http://poqdavid.github.io/PotatoWall/). - * Copyright (c) 2021 POQDavid + * Copyright (c) 2023 POQDavid * Copyright (c) contributors * * PotatoWall is free software: you can redistribute it and/or modify @@ -29,7 +29,6 @@ public static class RichTextBoxExtensions { public static void CheckAppendText(this RichTextBox richtextBox, Paragraph msg, bool waitUntilReturn = false) { - //Action append = () => Action append = () => { richtextBox.Document.CheckAppendText(msg); @@ -51,7 +50,6 @@ public static void CheckAppendText(this RichTextBox richtextBox, Paragraph msg, public static void SetText(this RichTextBox richtextBox, Paragraph msg, bool waitUntilReturn = false) { - //Action append = () => Action append = () => { richtextBox.Document.SetText(msg); diff --git a/Extensions/StringExtensions.cs b/Extensions/StringExtensions.cs new file mode 100644 index 0000000..dd55ba8 --- /dev/null +++ b/Extensions/StringExtensions.cs @@ -0,0 +1,95 @@ +/* + * This file is part of PotatoWall distribution (https://github.com/poqdavid/PotatoWall or http://poqdavid.github.io/PotatoWall/). + * Copyright (c) 2023 POQDavid + * Copyright (c) contributors + * + * PotatoWall is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * PotatoWall is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with PotatoWall. If not, see . + */ + +namespace PotatoWall.Extensions; + +// +// Copyright (c) POQDavid. All rights reserved. +// +// POQDavid +// This is the StringExtensions class. + +public static class StringExtensions +{ + /// + /// Returns a new string with whitespaces removed. + /// + /// + /// + public static string RemoveWhitespace(this string value) + { + return value.Replace(" ", string.Empty); + } + + /// + /// Returns a new string with whitespaces removed. + /// + /// + /// Converts string to lowercase + /// + public static string RemoveWhitespace(this string value, bool lowercase) + { + return lowercase switch + { + true => value.Replace(" ", string.Empty).ToLower(), + false => value.Replace(" ", string.Empty), + }; + } + + /// + /// Converts CSV to String Array + /// + /// + /// An string Array converted from CSV + public static string[] CSVToArray(this string value) + { + return value.Split(','); + } + + /// + /// Converts CSV to String Array + /// + /// + /// Removes whitespace + /// An string Array converted from CSV + public static string[] CSVToArray(this string value, bool removewhitespace) + { + return removewhitespace switch + { + true => value.RemoveWhitespace().Split(','), + false => value.Split(','), + }; + } + + /// + /// Converts CSV to String Array + /// + /// + /// Removes whitespace + /// Converts string to lowercase + /// An string Array converted from CSV + public static string[] CSVToArray(this string value, bool removewhitespace, bool lowercase) + { + return lowercase switch + { + true => value.ToLower().CSVToArray(removewhitespace), + false => value.CSVToArray(removewhitespace), + }; + } +} \ No newline at end of file diff --git a/Extensions/TextBlockExtensions.cs b/Extensions/TextBlockExtensions.cs index 1e69d81..3745ac7 100644 --- a/Extensions/TextBlockExtensions.cs +++ b/Extensions/TextBlockExtensions.cs @@ -1,6 +1,6 @@ /* * This file is part of PotatoWall distribution (https://github.com/poqdavid/PotatoWall or http://poqdavid.github.io/PotatoWall/). - * Copyright (c) 2021 POQDavid + * Copyright (c) 2023 POQDavid * Copyright (c) contributors * * PotatoWall is free software: you can redistribute it and/or modify diff --git a/Extensions/TextBoxExtensions.cs b/Extensions/TextBoxExtensions.cs index a8a9dc1..da16a50 100644 --- a/Extensions/TextBoxExtensions.cs +++ b/Extensions/TextBoxExtensions.cs @@ -1,6 +1,6 @@ /* * This file is part of PotatoWall distribution (https://github.com/poqdavid/PotatoWall or http://poqdavid.github.io/PotatoWall/). - * Copyright (c) 2021 POQDavid + * Copyright (c) 2023 POQDavid * Copyright (c) contributors * * PotatoWall is free software: you can redistribute it and/or modify diff --git a/Extensions/WindowExtensions.cs b/Extensions/WindowExtensions.cs index bf54afb..1b357c3 100644 --- a/Extensions/WindowExtensions.cs +++ b/Extensions/WindowExtensions.cs @@ -1,6 +1,6 @@ /* * This file is part of PotatoWall distribution (https://github.com/poqdavid/PotatoWall or http://poqdavid.github.io/PotatoWall/). - * Copyright (c) 2021 POQDavid + * Copyright (c) 2023 POQDavid * Copyright (c) contributors * * PotatoWall is free software: you can redistribute it and/or modify diff --git a/GlobalUsings.cs b/GlobalUsings.cs index 1b78395..652830d 100644 --- a/GlobalUsings.cs +++ b/GlobalUsings.cs @@ -4,8 +4,6 @@ global using MaxMind.Db; global using MaxMind.GeoIP2.Responses; global using Microsoft.Extensions.Configuration; -global using Newtonsoft.Json; -global using Newtonsoft.Json.Linq; global using PotatoWall.Extensions; global using PotatoWall.MVVM.ViewModel; global using PotatoWall.Utils; @@ -17,6 +15,7 @@ global using System.Collections.ObjectModel; global using System.ComponentModel; global using System.Diagnostics; +global using System.Formats.Tar; global using System.Globalization; global using System.IO; global using System.IO.Compression; @@ -28,8 +27,12 @@ global using System.Reflection; global using System.Runtime.CompilerServices; global using System.Runtime.InteropServices; +global using System.Security.Cryptography; global using System.ServiceProcess; global using System.Text; +global using System.Text.Json; +global using System.Text.Json.Nodes; +global using System.Text.Json.Serialization; global using System.Text.RegularExpressions; global using System.Threading; global using System.Threading.Tasks; diff --git a/Libs/WinDivertSharp b/Libs/WinDivertSharp index 74cd73b..9b3af8b 160000 --- a/Libs/WinDivertSharp +++ b/Libs/WinDivertSharp @@ -1 +1 @@ -Subproject commit 74cd73b21e0981f427744dfce5e81a2affcd2919 +Subproject commit 9b3af8b0011da0f2d6f8a41471d55d7223e9a19b diff --git a/Utils/ColorData.cs b/MVVM/Model/ColorData.cs similarity index 76% rename from Utils/ColorData.cs rename to MVVM/Model/ColorData.cs index 4b2c94a..86b4b6e 100644 --- a/Utils/ColorData.cs +++ b/MVVM/Model/ColorData.cs @@ -1,6 +1,6 @@ /* * This file is part of PotatoWall distribution (https://github.com/poqdavid/PotatoWall or http://poqdavid.github.io/PotatoWall/). - * Copyright (c) 2021 POQDavid + * Copyright (c) 2023 POQDavid * Copyright (c) contributors * * PotatoWall is free software: you can redistribute it and/or modify @@ -17,7 +17,7 @@ * along with PotatoWall. If not, see . */ -namespace PotatoWall.Utils; +namespace PotatoWall.MVVM.Model; // // Copyright (c) POQDavid. All rights reserved. @@ -26,6 +26,13 @@ namespace PotatoWall.Utils; // This is the ColorData class. public class ColorDataList { + public ColorDataList() + { + ColorName = string.Empty; + ColorMetadata = string.Empty; + ColorEnabled = true; + } + public ColorDataList(string colorname, string colormetadata, bool colorenabled) { ColorName = colorname; @@ -33,13 +40,13 @@ public ColorDataList(string colorname, string colormetadata, bool colorenabled) ColorEnabled = colorenabled; } - [JsonProperty("ColorName")] - public string ColorName { get; set; } = ""; + [JsonPropertyName("ColorName")] + public string ColorName { get; set; } = string.Empty; - [JsonProperty("ColorMetadata")] - public string ColorMetadata { get; set; } = ""; + [JsonPropertyName("ColorMetadata")] + public string ColorMetadata { get; set; } = string.Empty; - [JsonProperty("ColorEnabled")] + [JsonPropertyName("ColorEnabled")] public bool ColorEnabled { get; set; } = true; } @@ -49,7 +56,7 @@ public class ColorData : INotifyPropertyChanged private List defaultColorDataList = new() { }; - [JsonProperty("ColorDataList")] + [JsonPropertyName("ColorDataList")] public List ColorDataList { get => defaultColorDataList; set { defaultColorDataList = value; OnPropertyChanged(); } } @@ -63,9 +70,9 @@ public static void Save() _ = Directory.CreateDirectory(PotatoWallClient.AppDataPath); } - JsonSerializerSettings s = new() + JsonSerializerOptions options = new() { - ObjectCreationHandling = ObjectCreationHandling.Replace // without this, you end up with duplicates. + WriteIndented = true }; SwatchesProvider swatchesProvider = new(); @@ -83,12 +90,12 @@ public static void Save() cd.ColorDataList.Add(new ColorDataList("------------Others------------", "SEP", false)); foreach (PropertyInfo c in typeof(Colors).GetProperties()) { - Color cx = (Color)c.GetValue(null); + Color cx = c.GetValue(null).CastTo(); cd.ColorDataList.Add(new ColorDataList(c.Name, cx.ToString(Thread.CurrentThread.CurrentCulture), true)); } cd.ColorDataList.Add(new ColorDataList("------------Others------------", "SEP", false)); - File.WriteAllText(PotatoWallClient.ColorDataPath, JsonConvert.SerializeObject(cd, Formatting.Indented, s)); + File.WriteAllText(PotatoWallClient.ColorDataPath, JsonSerializer.Serialize(cd, options)); } public void Load() @@ -98,13 +105,12 @@ public void Load() string json_string = File.ReadAllText(PotatoWallClient.ColorDataPath); if (Json.IsValid(json_string)) { - JsonSerializerSettings s = new() + JsonSerializerOptions options = new() { - NullValueHandling = NullValueHandling.Ignore, - ObjectCreationHandling = ObjectCreationHandling.Replace // without this, you end up with duplicates. + WriteIndented = true }; - ColorDataList = JsonConvert.DeserializeObject(json_string, s).ColorDataList; + ColorDataList = JsonSerializer.Deserialize(json_string, options).ColorDataList; } else { diff --git a/Utils/IPList.cs b/MVVM/Model/IPList.cs similarity index 72% rename from Utils/IPList.cs rename to MVVM/Model/IPList.cs index d56d94c..e3be5cb 100644 --- a/Utils/IPList.cs +++ b/MVVM/Model/IPList.cs @@ -1,6 +1,6 @@ /* * This file is part of PotatoWall distribution (https://github.com/poqdavid/PotatoWall or http://poqdavid.github.io/PotatoWall/). - * Copyright (c) 2021 POQDavid + * Copyright (c) 2023 POQDavid * Copyright (c) contributors * * PotatoWall is free software: you can redistribute it and/or modify @@ -17,7 +17,7 @@ * along with PotatoWall. If not, see . */ -namespace PotatoWall.Utils; +namespace PotatoWall.MVVM.Model; // // Copyright (c) POQDavid. All rights reserved. @@ -82,7 +82,7 @@ public SrcIPData(string srcip, string srcport, string dstip, string dstport, str DstIPData.Port = dstport; } - [JsonProperty("IP")] + [JsonPropertyName("IP")] public string IP { get => defaultIP; @@ -95,7 +95,7 @@ public string IP } } - [JsonProperty("Port")] + [JsonPropertyName("Port")] public string Port { get => defaultPort; @@ -121,7 +121,7 @@ public string Port } } - [JsonProperty("Direction")] + [JsonPropertyName("Direction")] public string Direction { get => defaultDirection; @@ -135,18 +135,18 @@ public string Direction } } - [JsonProperty("IPPort")] + [JsonPropertyName("IPPort")] public string IPPort => $"{IP}:{Port}"; - [JsonProperty("IPPorts")] + [JsonPropertyName("IPPorts")] public Dictionary> IPPorts { get => defaultIPPorts; set { defaultIPPorts = value; OnPropertyChanged(); } } - [JsonProperty("DstIPData")] + [JsonPropertyName("DstIPData")] public DstIPData DstIPData { get => defaultDstIPData; set { defaultDstIPData = value; OnPropertyChanged(); } } - [JsonProperty("ImageSource")] + [JsonPropertyName("ImageSource")] public Uri ImageSource { get @@ -156,18 +156,18 @@ public Uri ImageSource } } - [JsonProperty("Country")] + [JsonPropertyName("Country")] public string Country => GetCountry(); - [JsonProperty("ASN")] + [JsonPropertyName("ASN")] public string ASN => GetASN(); - [JsonProperty("LastActivity")] + [JsonPropertyName("LastActivity")] public DateTime LastActivity { get; private set; } = DateTime.Now; public override bool Equals(object obj) { - return obj.GetType() == typeof(string) ? IP == ((string)obj) : obj is SrcIPData iPData && IP.Equals(iPData.IP, global::System.StringComparison.Ordinal); + return obj.GetType() == typeof(string) ? IP == obj.CastTo() : obj is SrcIPData iPData && IP.Equals(iPData.IP, global::System.StringComparison.Ordinal); } public IPAddress ToIPAddress() @@ -278,7 +278,7 @@ public DstIPData(string ip, string port) Port = port; } - [JsonProperty("IP")] + [JsonPropertyName("IP")] public string IP { get => defaultIP; @@ -290,7 +290,7 @@ public string IP } } - [JsonProperty("Port")] + [JsonPropertyName("Port")] public string Port { get => defaultPort; @@ -316,14 +316,14 @@ public string Port } } - [JsonProperty("IPPort")] + [JsonPropertyName("IPPort")] public string IPPort => $"{IP}:{Port}"; - [JsonProperty("IPPorts")] + [JsonPropertyName("IPPorts")] public Dictionary> IPPorts { get => defaultIPPorts; set { defaultIPPorts = value; OnPropertyChanged(); } } - [JsonProperty("ImageSource")] + [JsonPropertyName("ImageSource")] public Uri ImageSource { get @@ -333,16 +333,16 @@ public Uri ImageSource } } - [JsonProperty("Country")] + [JsonPropertyName("Country")] public string Country => GetCountry(); - [JsonProperty("ASN")] + [JsonPropertyName("ASN")] public string ASN => GetASN(); public override bool Equals(object obj) { return obj.GetType() == typeof(string) - ? IP == ((string)obj) + ? IP == obj.CastTo() : obj is SrcIPData iPData && IP.Equals(iPData.IP, StringComparison.Ordinal); } @@ -432,178 +432,79 @@ private void OnPropertyChanged([CallerMemberName] string propertyName = "") } } -public class IPList : ObservableCollection +public class IPList : ThreadSafeObservableCollection { - private delegate void SetItemCallback(int index, SrcIPData item); - - private delegate void RemoveItemCallback(int index); - - private delegate void ClearItemsCallback(); - - private delegate void InsertItemCallback(int index, SrcIPData item); - - private delegate void MoveItemCallback(int oldIndex, int newIndex); - - private static volatile bool ModifyIPList = true; - - public Dispatcher Dispatcher { get; } - - public IPList(Dispatcher dispatcher) + public IPList(Dispatcher dispatcher) : base(dispatcher) { Dispatcher = dispatcher; } - public IPList() + public IPList() : base() { Dispatcher = Dispatcher.CurrentDispatcher; } - protected override void SetItem(int index, SrcIPData item) - { - try - { - ModifyIPList = false; - if (Dispatcher.CheckAccess()) - { - base.SetItem(index, item); - } - else - { - _ = Dispatcher.Invoke(DispatcherPriority.Send, new SetItemCallback(SetItem), index, new object[] { item }); - } - } - finally - { - ModifyIPList = true; - } - } - - protected override void InsertItem(int index, SrcIPData item) + public void AddOrUpdate(SrcIPData iPData) { - try + if (AddContains(iPData)) { - ModifyIPList = false; - if (Dispatcher == null) + try { - base.InsertItem(index, item); + ModifyIPList = false; + SrcIPData item = this.FirstOrDefault(i => i.IP == iPData.IP); + if (item != null) + { + item = iPData; + } } - else if (Dispatcher.CheckAccess()) + catch (Exception ex) { - base.InsertItem(index, item); + PotatoWallClient.Logger.Debug(ex, "AddOrUpdate: "); } - else + finally { - _ = Dispatcher.Invoke(DispatcherPriority.Send, new InsertItemCallback(InsertItem), index, new object[] { item }); + ModifyIPList = true; } } - catch (Exception ex) - { - PotatoWallClient.Logger.Debug(ex, "InsertItem: "); - } - finally - { - ModifyIPList = true; - } } - protected override void MoveItem(int oldIndex, int newIndex) - { - if (Dispatcher.CheckAccess()) - { - base.MoveItem(oldIndex, newIndex); - } - else - { - _ = Dispatcher.Invoke(DispatcherPriority.Send, new MoveItemCallback(MoveItem), oldIndex, new object[] { newIndex }); - } - } - - protected override void RemoveItem(int index) + public bool Contains(string ip) { - if (ModifyIPList) + for (int i = 0; i < Count; i++) { - if (Dispatcher.CheckAccess()) - { - base.RemoveItem(index); - } - else + if (Items[i].Equals(ip)) { - _ = Dispatcher.Invoke(DispatcherPriority.Send, new RemoveItemCallback(RemoveItem), index); + return true; } } + return false; } +} - protected override void ClearItems() +public class IPList : ThreadSafeObservableDictionary +{ + public IPList() : base() { - if (ModifyIPList) - { - if (Dispatcher.CheckAccess()) - { - base.ClearItems(); - } - else - { - _ = Dispatcher.Invoke(DispatcherPriority.Send, new ClearItemsCallback(ClearItems)); - } - } + _dispatcher = Dispatcher.CurrentDispatcher; } - public bool Contains(string ip) + public IPList(Dispatcher dispatcher) : base(dispatcher) { - for (int i = 0; i < Count; i++) - { - if (Items[i].Equals(ip)) - { - return true; - } - } - return false; + _dispatcher = dispatcher; } - public new bool Contains(SrcIPData iPData) + public bool Remove(SrcIPData iPData) { - for (int i = 0; i < Count; i++) - { - if (Items[i].Equals(iPData)) - { - return true; - } - } - return false; + return base.Remove(iPData.IP); } public bool AddContains(SrcIPData iPData) { - if (!base.Contains(iPData)) - { - Add(iPData); - return false; - } - - return true; + return AddContains(iPData.IP, iPData); } public void AddOrUpdate(SrcIPData iPData) { - if (AddContains(iPData)) - { - try - { - ModifyIPList = false; - SrcIPData item = this.FirstOrDefault(i => i.IP == iPData.IP); - if (item != null) - { - item = iPData; - } - } - catch (Exception ex) - { - PotatoWallClient.Logger.Debug(ex, "AddOrUpdate: "); - } - finally - { - ModifyIPList = true; - } - } + AddOrUpdate(iPData.IP, iPData); } } \ No newline at end of file diff --git a/Utils/IPRule.cs b/MVVM/Model/IPRule.cs similarity index 62% rename from Utils/IPRule.cs rename to MVVM/Model/IPRule.cs index 3bb3dd0..0c88d18 100644 --- a/Utils/IPRule.cs +++ b/MVVM/Model/IPRule.cs @@ -1,6 +1,6 @@ /* * This file is part of PotatoWall distribution (https://github.com/poqdavid/PotatoWall or http://poqdavid.github.io/PotatoWall/). - * Copyright (c) 2021 POQDavid + * Copyright (c) 2023 POQDavid * Copyright (c) contributors * * PotatoWall is free software: you can redistribute it and/or modify @@ -17,14 +17,15 @@ * along with PotatoWall. If not, see . */ -namespace PotatoWall.Utils; +namespace PotatoWall.MVVM.Model; -public class IPRule : ValidationRule +public partial class IPRule : ValidationRule { - private readonly Regex RegEX = new(@"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"); + [GeneratedRegex("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$")] + private static partial Regex RegEX(); - public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo) + public override ValidationResult Validate(object value, CultureInfo cultureInfo) { - return RegEX.IsMatch((string)value) ? new ValidationResult(true, null) : new ValidationResult(false, "Invalid IP Address"); + return RegEX().IsMatch(value.CastTo()) ? new ValidationResult(true, null) : new ValidationResult(false, "Invalid IP Address"); } } \ No newline at end of file diff --git a/Utils/PotatoParagraph.cs b/MVVM/Model/PotatoParagraph.cs similarity index 80% rename from Utils/PotatoParagraph.cs rename to MVVM/Model/PotatoParagraph.cs index 8e9d699..189c219 100644 --- a/Utils/PotatoParagraph.cs +++ b/MVVM/Model/PotatoParagraph.cs @@ -1,6 +1,6 @@ /* * This file is part of PotatoWall distribution (https://github.com/poqdavid/PotatoWall or http://poqdavid.github.io/PotatoWall/). - * Copyright (c) 2021 POQDavid + * Copyright (c) 2023 POQDavid * Copyright (c) contributors * * PotatoWall is free software: you can redistribute it and/or modify @@ -17,7 +17,7 @@ * along with PotatoWall. If not, see . */ -namespace PotatoWall.Utils; +namespace PotatoWall.MVVM.Model; // // Copyright (c) POQDavid. All rights reserved. @@ -26,24 +26,34 @@ namespace PotatoWall.Utils; // This is the PotatoParagraph class. public class PotatoParagraph { - [JsonProperty("Text")] - public string Text { get; set; } = ""; + [JsonPropertyName("Text")] + public string Text { get; set; } = string.Empty; - [JsonProperty("Background")] + [JsonPropertyName("Background")] public Brush Background { get; set; } = null; - [JsonProperty("Foreground")] + [JsonPropertyName("Foreground")] public Brush Foreground { get; set; } = null; - [JsonProperty("Bold")] + [JsonPropertyName("Bold")] public bool Bold { get; set; } = false; - [JsonProperty("Italic")] + [JsonPropertyName("Italic")] public bool Italic { get; set; } = false; - [JsonProperty("Underline")] + [JsonPropertyName("Underline")] public bool Underline { get; set; } = false; + public PotatoParagraph() + { + this.Text = string.Empty; + this.Background = null; + this.Foreground = null; + this.Bold = false; + this.Italic = false; + this.Underline = false; + } + public PotatoParagraph(string Text, Brush Background, Brush Foreground, bool Bold, bool Italic, bool Underline) { this.Text = Text; diff --git a/MVVM/View/AddIPControl.xaml b/MVVM/View/AddIPControl.xaml index 46aa62b..a94602f 100644 --- a/MVVM/View/AddIPControl.xaml +++ b/MVVM/View/AddIPControl.xaml @@ -6,8 +6,8 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:localConverters="clr-namespace:PotatoWall.Converters" + xmlns:model="clr-namespace:PotatoWall.MVVM.Model" xmlns:viewmodel="clr-namespace:PotatoWall.MVVM.ViewModel" - xmlns:localutils="clr-namespace:PotatoWall.Utils" MinWidth="270" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" @@ -28,8 +28,6 @@ - - @@ -71,7 +69,7 @@ Path="IP" UpdateSourceTrigger="PropertyChanged"> - + diff --git a/MVVM/View/SettingsControl.xaml b/MVVM/View/SettingsControl.xaml index ebc65de..a1f6e50 100644 --- a/MVVM/View/SettingsControl.xaml +++ b/MVVM/View/SettingsControl.xaml @@ -9,6 +9,7 @@ xmlns:local="clr-namespace:PotatoWall" xmlns:localConverters="clr-namespace:PotatoWall.Converters" xmlns:viewmodel="clr-namespace:PotatoWall.MVVM.ViewModel" + xmlns:localValidationRules="clr-namespace:PotatoWall.ValidationRules" MinWidth="270" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" @@ -29,80 +30,84 @@ - - + + - + - - - - - - - - - - - - - - - - - - + + - + + + + + - + - + + + + - + + - + + - - - + + + + + + + + - - UI options. - - - - - - - - + UI options. + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - FriesWall options. - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + GeoIP options. + + + + + + + + + + + + - + + + + + + + + + + FriesWall options. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + Text="{Binding ISettings.Firewall.RegEX, BindsDirectlyToSource=True, Mode=TwoWay, NotifyOnSourceUpdated=True, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:PotatoWallUI}}, UpdateSourceTrigger=PropertyChanged}" /> - - + Text="{Binding ISettings.Firewall.IPActivityTime, BindsDirectlyToSource=True, Mode=TwoWay, NotifyOnSourceUpdated=True, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:PotatoWallUI}}, UpdateSourceTrigger=PropertyChanged}" /> - - + + + + + + + + + + + + - - - - + + + + + + + + + + + + + + + + + + + + + + + + + - - - + + + - - - + + + - + - + - + - + - + - - - - - WinDivert options. - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + WinDivert options. + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - WinDivert function. - - - - - - - - + + + + + WinDivert function. + + + + + + + + - - - - + + + - - + +