This commit is contained in:
Давид Чернопятов
2024-07-08 16:20:28 +03:00
parent 6a1b03163c
commit c8aa7ba8e7
5 changed files with 101 additions and 129 deletions
+37 -1
View File
@@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using AchiesUtilities.Collections; using AchiesUtilities.Collections;
using AchiesUtilities.Web.Proxy; using AchiesUtilities.Web.Proxy;
@@ -69,6 +70,41 @@ public static class ProxyStorage
Save(); Save();
} }
public static void SetProxies(IEnumerable<KeyValuePair<int?, ProxyData>> proxies)
{
foreach (var (key, proxyData) in proxies)
{
var id = key;
if (id == null)
{
if (Proxies.Count == 0)
{
id = 0;
}
else
{
id = Proxies.Keys.Max() + 1;
}
}
Proxies[id] = proxyData;
}
Save();
}
public static void OrderCollection() //RETHINK: maybe there is better way to handle it
{
var proxies = Proxies.OrderBy(p => p.Key)
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
Proxies.Clear();
foreach (var kvp in proxies)
{
Proxies.Add(kvp.Key, kvp.Value);
}
}
public static void RemoveProxy(int id) public static void RemoveProxy(int id)
{ {
Proxies.Remove(id); Proxies.Remove(id);
@@ -76,7 +112,7 @@ public static class ProxyStorage
} }
public static bool CompareProxy(ProxyData proxyData1, ProxyData proxyData2) public static bool CompareProxy(ProxyData proxyData1, ProxyData proxyData2)
{ {
return proxyData1.Address == proxyData2.Address && proxyData1.Port == proxyData2.Port; return proxyData1.Equals(proxyData2);
} }
-58
View File
@@ -1,58 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net7.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<UseWindowsForms>true</UseWindowsForms>
<LangVersion>latest</LangVersion>
<SatelliteResourceLanguages>ru</SatelliteResourceLanguages>
<ApplicationIcon>Theme\nebula lock.ico</ApplicationIcon>
<SupportedOSPlatformVersion>7.0</SupportedOSPlatformVersion>
<AssemblyVersion>1.4.0</AssemblyVersion>
</PropertyGroup>
<ItemGroup>
<None Remove="Theme\Background.jpg" />
<None Remove="Theme\nebula lock.ico" />
<None Remove="Theme\nebula.ico" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.1.0" />
<PackageReference Include="MaterialDesignColors" Version="2.1.4" />
<PackageReference Include="MaterialDesignExtensions" Version="3.3.0" />
<PackageReference Include="MaterialDesignThemes" Version="4.9.0" />
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.39" />
<PackageReference Include="NLog" Version="5.1.2" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.2.2" />
</ItemGroup>
<ItemGroup>
<Resource Include="Theme\Background.jpg">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Resource>
<Resource Include="Theme\nebula lock.ico" />
<Resource Include="Theme\nebula.ico" />
</ItemGroup>
<ItemGroup>
<Folder Include="Model\Utility\" />
<Folder Include="Model\Exceptions\" />
<Folder Include="Theme\Fonts\Новая папка\" />
<Folder Include="ViewModel\Other\" />
<Folder Include="Utility\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\SteamLib\SteamLib\SteamLib.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="NLog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
+2 -2
View File
@@ -76,7 +76,7 @@ public partial class MainVM
var selectedId = SelectedProxy.Id; var selectedId = SelectedProxy.Id;
ProxyExist = ProxyStorage.Proxies.TryGetValue(selectedId, out var existedProxy) ProxyExist = ProxyStorage.Proxies.TryGetValue(selectedId, out var existedProxy)
&& ProxyStorage.CompareProxy(SelectedProxy.Data, existedProxy); && SelectedProxy.Data.Equals(existedProxy); //Id is not important in 'Equals()' as we extract it from the dictionary
} }
[RelayCommand] [RelayCommand]
@@ -98,7 +98,7 @@ public partial class MainVM
[RelayCommand] [RelayCommand]
private void RemoveProxy() private void RemoveProxy()
{ {
if(SelectedProxy == null) return; if (SelectedProxy == null) return;
if (SelectedMafile == null) return; if (SelectedMafile == null) return;
if (!ValidateCanSaveAndWarn(SelectedMafile)) return; if (!ValidateCanSaveAndWarn(SelectedMafile)) return;
SelectedMafile.Proxy = null; SelectedMafile.Proxy = null;
+50 -58
View File
@@ -21,7 +21,7 @@ public partial class ProxyManagerVM : ObservableObject
[ObservableProperty] private KeyValuePair<int, ProxyData>? _defaultProxy; [ObservableProperty] private KeyValuePair<int, ProxyData>? _defaultProxy;
public ObservableDictionary<int, ProxyData> Proxies => ProxyStorage.Proxies; public ObservableDictionary<int, ProxyData> Proxies => ProxyStorage.Proxies;
private static readonly Regex IdRegex = new(@"(?:\{(\d+)\})"); private static readonly Regex IdRegex = new(@"\{(\d+)\}$");
public ProxyManagerVM() public ProxyManagerVM()
@@ -33,79 +33,71 @@ public partial class ProxyManagerVM : ObservableObject
[RelayCommand] [RelayCommand]
private void AddProxy() private void AddProxy()
{ {
if (string.IsNullOrEmpty(AddProxyField)) return; var input = AddProxyField;
if (AddProxyField.Contains(Environment.NewLine)) if (string.IsNullOrEmpty(input)) return;
{
var split = AddProxyField.Split(Environment.NewLine);
var idPresent = (bool?)null; var split = input
var proxies = new List<KeyValuePair<int?, ProxyData>>(); .Split(Environment.NewLine)
var i = 0; .Where(s => string.IsNullOrWhiteSpace(s) == false)
foreach (var s in split.Where(s => string.IsNullOrWhiteSpace(s) == false)) .ToArray();
{
i++; if (split.Length == 0) return;
var str = s;
int? id = null; bool? idPresent = null;
var match = IdRegex.Match(str); var proxies = new List<KeyValuePair<int?, ProxyData>>();
if (match.Success) var i = 0;
{
id = int.Parse(match.Groups[1].Value);
str = IdRegex.Replace(str, ""); foreach (var s in split.Where(s => string.IsNullOrWhiteSpace(s) == false))
}
idPresent ??= match.Success;
if (idPresent.Value != match.Success)
{
SnackbarController.SendSnackbar(GetLocalizationOrDefault("WrongFormatSomeIdsMissing"));
return;
}
if (ProxyStorage.DefaultScheme.TryParse(str, out var proxy))
{
if (id != null && proxies.Any(kvp => kvp.Key == id))
{
SnackbarController.SendSnackbar(string.Format(GetLocalizationOrDefault("DuplicateId"), id));
return;
}
proxies.Add(new KeyValuePair<int?, ProxyData>(id, proxy));
}
else
{
SnackbarController.SendSnackbar(string.Format(GetLocalizationOrDefault("WrongFormatOnLine"), i));
return;
}
}
foreach (var kvp in proxies)
{
ProxyStorage.SetProxy(kvp.Key, kvp.Value);
}
}
else
{ {
i++;
var str = s;
int? id = null; int? id = null;
var input = AddProxyField; var idMatch = IdRegex.Match(str);
if (IdRegex.IsMatch(AddProxyField)) if (idMatch.Success)
{ {
id = int.Parse(IdRegex.Match(AddProxyField).Groups[1].Value); id = int.Parse(idMatch.Groups[1].Value);
input = IdRegex.Replace(input, ""); str = IdRegex.Replace(str, "");
} }
if (ProxyStorage.DefaultScheme.TryParse(input, out var data)) idPresent ??= idMatch.Success;
if (idPresent.Value != idMatch.Success)
{ {
ProxyStorage.SetProxy(id, data); SnackbarController.SendSnackbar(GetLocalizationOrDefault("WrongFormatSomeIdsMissing"));
return;
}
if (ProxyStorage.DefaultScheme.TryParse(str, out var proxy))
{
if (id != null && proxies.Any(kvp => kvp.Key == id))
{
SnackbarController.SendSnackbar(string.Format(GetLocalizationOrDefault("DuplicateId"), id));
return;
}
proxies.Add(new KeyValuePair<int?, ProxyData>(id, proxy));
} }
else else
{ {
SnackbarController.SendSnackbar(GetLocalizationOrDefault("WrongFormat")); if (split.Length == 1)
{
SnackbarController.SendSnackbar(GetLocalizationOrDefault("WrongFormat"));
return;
}
SnackbarController.SendSnackbar(string.Format(GetLocalizationOrDefault("WrongFormatOnLine"), i));
return; return;
} }
} }
ProxyStorage.SetProxies(proxies);
ProxyStorage.OrderCollection();
AddProxyField = string.Empty; AddProxyField = string.Empty;
CheckIfDefaultProxyStay(); CheckIfDefaultProxyStay();
} }
private void CheckIfDefaultProxyStay() private void CheckIfDefaultProxyStay()
{ {
if (!DefaultProxy.HasValue || Proxies.Any(kvp => kvp.Equals(DefaultProxy.Value))) return; if (!DefaultProxy.HasValue || Proxies.Any(kvp => kvp.Equals(DefaultProxy.Value))) return;
+10 -8
View File
@@ -63,16 +63,18 @@
</head> </head>
<body> <body>
<div class="changelog-container"> <div class="changelog-container">
<!-- Changelog entry --> <!-- Changelog entry -->
<div class="change"> <div class="change">
<div class="version">Version 1.5.1</div> <div class="version">Version 1.5.1</div>
<div class="date">DATE</div> <div class="date">DATE</div>
<div class="description"> <div class="description">
- FIX: Fixed proxy problem when {id} was parsed as a part of password when using 'ip:port:username:password{id}' proxy format - FIX: Fixed proxy error (407) when {id} was parsed as a part of password when using 'ip:port:username:password{id}' proxy format <br />
- IMPROVEMENT: Now proxies always ordered by ID <br />
- FIX: Small fix in the proxy comparison method (sometimes proxy red indicator was not shown)
</div>
</div> </div>
</div> </div>
</div>
</body> </body>
</html> </html>