mirror of
https://github.com/gwdevhub/Daybreak.git
synced 2026-09-17 13:55:02 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
54ae742899 | ||
|
|
4c862463d7 |
@@ -37,7 +37,5 @@ namespace Daybreak.Configuration
|
||||
public bool PlaceShortcut { get; set; }
|
||||
[JsonProperty("AutoCheckUpdate")]
|
||||
public bool AutoCheckUpdate { get; set; } = true;
|
||||
[JsonProperty("KeepLocalIconCache")]
|
||||
public bool KeepLocalIconCache { get; set; } = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,8 @@ namespace Daybreak.Configuration
|
||||
public bool LaunchGuildwarsAsCurrentUser { get; set; } = true;
|
||||
[JsonProperty("CanInterceptKeys")]
|
||||
public bool CanInterceptKeys { get; set; }
|
||||
[JsonProperty("DownloadIcons")]
|
||||
public bool DownloadIcons { get; set; }
|
||||
[JsonProperty("Macros")]
|
||||
public List<KeyMacro> Macros { get; set; } = new();
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ using LiteDB;
|
||||
using Daybreak.Services.Options;
|
||||
using Daybreak.Models;
|
||||
using Microsoft.CorrelationVector;
|
||||
using Services.IconRetrieve;
|
||||
|
||||
namespace Daybreak.Configuration
|
||||
{
|
||||
@@ -50,6 +51,7 @@ namespace Daybreak.Configuration
|
||||
serviceProducer.RegisterSingleton<ILiteDatabase, LiteDatabase>(sp => new LiteDatabase("Daybreak.db"));
|
||||
serviceProducer.RegisterSingleton<IMutexHandler, MutexHandler>();
|
||||
serviceProducer.RegisterSingleton<IShortcutManager, ShortcutManager>();
|
||||
serviceProducer.RegisterSingleton<IIconBrowser, IconBrowser>();
|
||||
serviceProducer.RegisterScoped<ICredentialManager, CredentialManager>();
|
||||
serviceProducer.RegisterScoped<IApplicationLauncher, ApplicationLauncher>();
|
||||
serviceProducer.RegisterScoped<IScreenshotProvider, ScreenshotProvider>();
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
xmlns:converters="clr-namespace:Daybreak.Converters"
|
||||
mc:Ignorable="d"
|
||||
x:Name="_this"
|
||||
Unloaded="BuildTemplate_Unloaded"
|
||||
d:DesignHeight="450" d:DesignWidth="800">
|
||||
<Grid Background="Transparent" MouseLeftButtonDown="Grid_MouseLeftButtonDown">
|
||||
<Grid.RowDefinitions>
|
||||
|
||||
@@ -3,12 +3,15 @@ using Daybreak.Models.Builds;
|
||||
using Daybreak.Services.BuildTemplates;
|
||||
using Daybreak.Services.IconRetrieve;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Services.IconRetrieve;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Configuration;
|
||||
using System.Core.Extensions;
|
||||
using System.Extensions;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Extensions;
|
||||
@@ -27,8 +30,10 @@ namespace Daybreak.Controls
|
||||
|
||||
private bool suppressBuildChanged = false;
|
||||
private bool loadedProperties = false;
|
||||
private IIconBrowser iconBrowser;
|
||||
private BuildEntry loadedBuild;
|
||||
private SkillTemplate selectingSkillTemplate;
|
||||
private CancellationTokenSource cancellationTokenSource = new();
|
||||
|
||||
public event EventHandler BuildChanged;
|
||||
|
||||
@@ -65,11 +70,14 @@ namespace Daybreak.Controls
|
||||
|
||||
public void InitializeTemplate(
|
||||
IIconRetriever iconRetriever,
|
||||
IIconBrowser iconBrowser,
|
||||
ILiveOptions<ApplicationConfiguration> liveOptions,
|
||||
IBuildTemplateManager buildTemplateManager,
|
||||
ILogger<ChromiumBrowserWrapper> logger)
|
||||
{
|
||||
this.iconBrowser = iconBrowser.ThrowIfNull();
|
||||
this.SkillBrowser.InitializeBrowser(liveOptions, buildTemplateManager, logger);
|
||||
this.iconBrowser.InitializeWebView(this.SkillBrowser.WebBrowser, this.cancellationTokenSource.Token);
|
||||
this.SkillTemplate0.InitializeSkillTemplate(iconRetriever);
|
||||
this.SkillTemplate1.InitializeSkillTemplate(iconRetriever);
|
||||
this.SkillTemplate2.InitializeSkillTemplate(iconRetriever);
|
||||
@@ -131,6 +139,11 @@ namespace Daybreak.Controls
|
||||
}
|
||||
}
|
||||
|
||||
private void BuildTemplate_Unloaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
this.cancellationTokenSource.Cancel();
|
||||
}
|
||||
|
||||
private void InitializeProperties()
|
||||
{
|
||||
this.PrimaryProfession = Profession.None;
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
<UserControl x:Class="Daybreak.Controls.SkillTemplate"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:webview="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
|
||||
xmlns:local="clr-namespace:Daybreak.Controls"
|
||||
mc:Ignorable="d"
|
||||
x:Name="_this"
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
using Daybreak.Launch;
|
||||
using Daybreak.Models.Builds;
|
||||
using Daybreak.Models.Builds;
|
||||
using Daybreak.Services.IconRetrieve;
|
||||
using System;
|
||||
using System.Extensions;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Extensions;
|
||||
@@ -41,19 +37,17 @@ namespace Daybreak.Controls
|
||||
this.iconRetriever = iconRetriever;
|
||||
}
|
||||
|
||||
private void SkillTemplate_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
|
||||
private async void SkillTemplate_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
if (e.NewValue is Skill skill)
|
||||
{
|
||||
if (skill != Skill.NoSkill)
|
||||
{
|
||||
Task.Run(() => this.GetImageStream(skill)).ContinueWith((previousTask) =>
|
||||
var maybeUri = await this.iconRetriever.GetIconUri(skill).ConfigureAwait(true);
|
||||
if (maybeUri.ExtractValue() is Uri uri)
|
||||
{
|
||||
this.Dispatcher.Invoke(() =>
|
||||
{
|
||||
this.ImageSource = this.GetImageSource(previousTask.Result);
|
||||
});
|
||||
});
|
||||
this.ImageSource = new BitmapImage(uri);
|
||||
}
|
||||
}
|
||||
else if (this.ImageSource is not null)
|
||||
{
|
||||
@@ -90,32 +84,5 @@ namespace Daybreak.Controls
|
||||
|
||||
return false;
|
||||
}
|
||||
private async Task<Stream> GetImageStream(Skill skill)
|
||||
{
|
||||
if (this.iconRetriever is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var maybeStream = await this.iconRetriever.GetIcon(skill);
|
||||
return maybeStream.ExtractValue();
|
||||
}
|
||||
private ImageSource GetImageSource(Stream stream)
|
||||
{
|
||||
if (stream is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return this.Dispatcher.Invoke(() =>
|
||||
{
|
||||
var bitmapImage = new BitmapImage();
|
||||
bitmapImage.BeginInit();
|
||||
bitmapImage.StreamSource = stream;
|
||||
bitmapImage.CacheOption = BitmapCacheOption.OnDemand;
|
||||
bitmapImage.EndInit();
|
||||
return bitmapImage;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,11 +10,10 @@
|
||||
<LangVersion>preview</LangVersion>
|
||||
<ApplicationIcon>Daybreak.ico</ApplicationIcon>
|
||||
<IncludePackageReferencesDuringMarkupCompilation>true</IncludePackageReferencesDuringMarkupCompilation>
|
||||
<Version>0.9.3.2</Version>
|
||||
<Version>0.9.3.3</Version>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="HtmlAgilityPack" Version="1.11.43" />
|
||||
<PackageReference Include="LiteDB" Version="5.0.12" />
|
||||
<PackageReference Include="Microsoft.CorrelationVector" Version="1.0.42" />
|
||||
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
|
||||
@@ -25,6 +24,7 @@
|
||||
<PackageReference Include="NReco.Logging.File" Version="1.1.5" />
|
||||
<PackageReference Include="securifybv.ShellLink" Version="0.1.0" />
|
||||
<PackageReference Include="Slim" Version="1.7.3" />
|
||||
<PackageReference Include="SystemExtensions.NetCore" Version="1.0.1" />
|
||||
<PackageReference Include="SystemExtensions.NetStandard" Version="1.5.0" />
|
||||
<PackageReference Include="WCL" Version="1.0.2" />
|
||||
<PackageReference Include="WpfExtended" Version="0.6.2" />
|
||||
|
||||
@@ -8,6 +8,7 @@ namespace Daybreak.Models.Browser
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
public enum PayloadKeys
|
||||
{
|
||||
None,
|
||||
ContextMenu
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Models
|
||||
{
|
||||
public sealed class IconPayload
|
||||
{
|
||||
public string SkillUrl { get; set; }
|
||||
public string SkillImage { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using Daybreak.Models.Builds;
|
||||
|
||||
namespace Models
|
||||
{
|
||||
public sealed class IconRequest
|
||||
{
|
||||
public Skill Skill { get; set; }
|
||||
public string IconBase64 { get; set; }
|
||||
public bool Finished { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using Microsoft.Web.WebView2.Wpf;
|
||||
using Models;
|
||||
using System.Threading;
|
||||
|
||||
namespace Services.IconRetrieve
|
||||
{
|
||||
public interface IIconBrowser
|
||||
{
|
||||
void InitializeWebView(WebView2 webView2, CancellationToken cancellationToken);
|
||||
/// <summary>
|
||||
/// Queue an icon request. The browser will attempt to download the icon. Monitor the <see cref="IconRequest.Finished"/> to be notified when the request has been served.
|
||||
/// </summary>
|
||||
/// <param name="iconRequest">Request model.</param>
|
||||
void QueueIconRequest(IconRequest iconRequest);
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,12 @@
|
||||
using Daybreak.Models.Builds;
|
||||
using System;
|
||||
using System.Extensions;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Daybreak.Services.IconRetrieve
|
||||
{
|
||||
public interface IIconRetriever
|
||||
{
|
||||
Task<Optional<Stream>> GetIcon(Skill skill);
|
||||
Task<Optional<Uri>> GetIconUri(Skill skill);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
using Daybreak.Utils;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Web.WebView2.Wpf;
|
||||
using Models;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Core.Extensions;
|
||||
using System.Extensions;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Threading;
|
||||
|
||||
namespace Services.IconRetrieve
|
||||
{
|
||||
public sealed class IconBrowser : IIconBrowser
|
||||
{
|
||||
// Sometimes due to browser issues, retrieved base64 is just a blank jpeg. This is the base64 of the image.
|
||||
private const string FaultyBase64 = "/9j/4AAQSkZJRgABAQAAAQABAAD/4gIoSUNDX1BST0ZJTEUAAQEAAAIYAAAAAAQwAABtbnRyUkdCIFhZWiAAAAAAAAAAAAAAAABhY3NwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAA9tYAAQAAAADTLQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAlkZXNjAAAA8AAAAHRyWFlaAAABZAAAABRnWFlaAAABeAAAABRiWFlaAAABjAAAABRyVFJDAAABoAAAAChnVFJDAAABoAAAAChiVFJDAAABoAAAACh3dHB0AAAByAAAABRjcHJ0AAAB3AAAADxtbHVjAAAAAAAAAAEAAAAMZW5VUwAAAFgAAAAcAHMAUgBHAEIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFhZWiAAAAAAAABvogAAOPUAAAOQWFlaIAAAAAAAAGKZAAC3hQAAGNpYWVogAAAAAAAAJKAAAA+EAAC2z3BhcmEAAAAAAAQAAAACZmYAAPKnAAANWQAAE9AAAApbAAAAAAAAAABYWVogAAAAAAAA9tYAAQAAAADTLW1sdWMAAAAAAAAAAQAAAAxlblVTAAAAIAAAABwARwBvAG8AZwBsAGUAIABJAG4AYwAuACAAMgAwADEANv/bAEMAAwICAgICAwICAgMDAwMEBgQEBAQECAYGBQYJCAoKCQgJCQoMDwwKCw4LCQkNEQ0ODxAQERAKDBITEhATDxAQEP/bAEMBAwMDBAMECAQECBALCQsQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEP/AABEIAEAAQAMBIgACEQEDEQH/xAAVAAEBAAAAAAAAAAAAAAAAAAAACf/EABQQAQAAAAAAAAAAAAAAAAAAAAD/xAAUAQEAAAAAAAAAAAAAAAAAAAAA/8QAFBEBAAAAAAAAAAAAAAAAAAAAAP/aAAwDAQACEQMRAD8AlUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD//2Q==";
|
||||
private const string LargeFaultyBase64 = "/9j/4AAQSkZJRgABAQAAAQABAAD/4gIoSUNDX1BST0ZJTEUAAQEAAAIYAAAAAAQwAABtbnRyUkdCIFhZWiAAAAAAAAAAAAAAAABhY3NwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAA9tYAAQAAAADTLQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAlkZXNjAAAA8AAAAHRyWFlaAAABZAAAABRnWFlaAAABeAAAABRiWFlaAAABjAAAABRyVFJDAAABoAAAAChnVFJDAAABoAAAAChiVFJDAAABoAAAACh3dHB0AAAByAAAABRjcHJ0AAAB3AAAADxtbHVjAAAAAAAAAAEAAAAMZW5VUwAAAFgAAAAcAHMAUgBHAEIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFhZWiAAAAAAAABvogAAOPUAAAOQWFlaIAAAAAAAAGKZAAC3hQAAGNpYWVogAAAAAAAAJKAAAA+EAAC2z3BhcmEAAAAAAAQAAAACZmYAAPKnAAANWQAAE9AAAApbAAAAAAAAAABYWVogAAAAAAAA9tYAAQAAAADTLW1sdWMAAAAAAAAAAQAAAAxlblVTAAAAIAAAABwARwBvAG8AZwBsAGUAIABJAG4AYwAuACAAMgAwADEANv/bAEMAAwICAgICAwICAgMDAwMEBgQEBAQECAYGBQYJCAoKCQgJCQoMDwwKCw4LCQkNEQ0ODxAQERAKDBITEhATDxAQEP/bAEMBAwMDBAMECAQECBALCQsQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEP/AABEIAIAAQAMBIgACEQEDEQH/xAAVAAEBAAAAAAAAAAAAAAAAAAAACf/EABQQAQAAAAAAAAAAAAAAAAAAAAD/xAAUAQEAAAAAAAAAAAAAAAAAAAAA/8QAFBEBAAAAAAAAAAAAAAAAAAAAAP/aAAwDAQACEQMRAD8AlUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD//2Q==";
|
||||
private const string BaseUrl = "https://wiki.guildwars.com";
|
||||
private const string QueryUrl = $"wiki/File:{NamePlaceholder}.jpg";
|
||||
private const string NamePlaceholder = "[SKILLNAME]";
|
||||
|
||||
private readonly ConcurrentQueue<IconRequest> iconRequests = new();
|
||||
private readonly ILogger<IconBrowser> logger;
|
||||
private WebView2 webView2;
|
||||
private CancellationToken cancellationToken;
|
||||
|
||||
public IconBrowser(
|
||||
ILogger<IconBrowser> logger)
|
||||
{
|
||||
this.logger = logger.ThrowIfNull();
|
||||
}
|
||||
|
||||
public void InitializeWebView(WebView2 webView2, CancellationToken cancellationToken)
|
||||
{
|
||||
this.webView2 = webView2.ThrowIfNull();
|
||||
this.cancellationToken = cancellationToken;
|
||||
Task.Run(this.PeriodicallyServeRequests, cancellationToken);
|
||||
}
|
||||
|
||||
public void QueueIconRequest(IconRequest iconRequest)
|
||||
{
|
||||
this.iconRequests.Enqueue(iconRequest);
|
||||
}
|
||||
|
||||
private async Task PeriodicallyServeRequests()
|
||||
{
|
||||
while(true)
|
||||
{
|
||||
if (this.cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.iconRequests.TryDequeue(out var request) is false)
|
||||
{
|
||||
await Task.Delay(1000);
|
||||
continue;
|
||||
}
|
||||
|
||||
this.logger.LogInformation($"Retrieving {request.Skill.Name} icon");
|
||||
while(this.webView2 is null)
|
||||
{
|
||||
if (this.cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
this.logger.LogInformation($"Browser is not yet initialized. Waiting");
|
||||
await Task.Delay(1000);
|
||||
}
|
||||
|
||||
await Application.Current.Dispatcher.InvokeAsync(async () =>
|
||||
{
|
||||
await this.webView2.EnsureCoreWebView2Async();
|
||||
}, DispatcherPriority.Render);
|
||||
|
||||
var curedSkillName = request.Skill.Name.Replace(" ", "_");
|
||||
var skillIconUrl = $"{BaseUrl}/{QueryUrl.Replace(NamePlaceholder, curedSkillName)}";
|
||||
this.logger.LogInformation($"Looking for icon at {skillIconUrl}");
|
||||
|
||||
Application.Current.Dispatcher.Invoke(() =>
|
||||
{
|
||||
this.webView2.Source = new Uri(skillIconUrl);
|
||||
});
|
||||
for(var i = 0; i < 5; i++)
|
||||
{
|
||||
if (this.cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
this.logger.LogInformation("Executing extraction script");
|
||||
var responseTask = await Application.Current.Dispatcher.InvokeAsync(async () =>
|
||||
{
|
||||
return await this.webView2.ExecuteScriptAsync(Scripts.GetHrefFromSkillPage);
|
||||
});
|
||||
var response = await responseTask;
|
||||
this.logger.LogInformation("Parsing response");
|
||||
var iconPayload = JsonConvert.DeserializeObject<IconPayload>(response);
|
||||
if (iconPayload is null)
|
||||
{
|
||||
this.logger.LogInformation("Bad response");
|
||||
await Task.Delay(1000);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (iconPayload.SkillUrl != skillIconUrl.Replace("\"", "%22"))
|
||||
{
|
||||
this.logger.LogInformation("Retrieved icon doesn't match");
|
||||
await Task.Delay(1000);
|
||||
continue;
|
||||
}
|
||||
|
||||
var potentialBase64 = iconPayload.SkillImage.Split(',').Skip(1).FirstOrDefault();
|
||||
if (potentialBase64 == FaultyBase64 ||
|
||||
potentialBase64 == LargeFaultyBase64)
|
||||
{
|
||||
this.logger.LogInformation("Faulty base64 retrieved");
|
||||
continue;
|
||||
}
|
||||
|
||||
request.IconBase64 = potentialBase64;
|
||||
request.Finished = true;
|
||||
break;
|
||||
}
|
||||
|
||||
this.logger.LogError($"Failed to retrieve icon for {request.Skill.Name}");
|
||||
request.Finished = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,13 @@
|
||||
using Daybreak.Configuration;
|
||||
using Daybreak.Models.Builds;
|
||||
using HtmlAgilityPack;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Models;
|
||||
using Services.IconRetrieve;
|
||||
using System;
|
||||
using System.Configuration;
|
||||
using System.Core.Extensions;
|
||||
using System.Extensions;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Daybreak.Services.IconRetrieve
|
||||
@@ -15,91 +15,58 @@ namespace Daybreak.Services.IconRetrieve
|
||||
public sealed class IconRetriever : IIconRetriever
|
||||
{
|
||||
private const string NamePlaceholder = "[SKILLNAME]";
|
||||
private const string BaseUrl = "https://wiki.guildwars.com";
|
||||
private const string QueryUrl = $"wiki/File:{NamePlaceholder}.jpg";
|
||||
private const string IconsDirectoryName = "Icons";
|
||||
private const string IconsLocation = $"{IconsDirectoryName}/{NamePlaceholder}.jpg";
|
||||
|
||||
private readonly IHttpClient<IconRetriever> httpClient;
|
||||
private readonly IIconBrowser iconBrowser;
|
||||
private readonly ILogger<IconRetriever> logger;
|
||||
private readonly ILiveOptions<ApplicationConfiguration> liveOptions;
|
||||
|
||||
public IconRetriever(
|
||||
ILogger<IconRetriever> logger,
|
||||
IHttpClient<IconRetriever> httpClient,
|
||||
IIconBrowser iconBrowser,
|
||||
ILiveOptions<ApplicationConfiguration> liveOptions)
|
||||
{
|
||||
this.logger = logger.ThrowIfNull(nameof(logger));
|
||||
this.httpClient = httpClient.ThrowIfNull(nameof(httpClient));
|
||||
this.liveOptions = liveOptions.ThrowIfNull(nameof(liveOptions));
|
||||
this.httpClient.BaseAddress = new Uri(BaseUrl);
|
||||
this.iconBrowser = iconBrowser.ThrowIfNull();
|
||||
this.logger = logger.ThrowIfNull();
|
||||
this.liveOptions = liveOptions.ThrowIfNull();
|
||||
if (Directory.Exists(IconsDirectoryName) is false)
|
||||
{
|
||||
Directory.CreateDirectory(IconsDirectoryName);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Optional<Stream>> GetIcon(Skill skill)
|
||||
public async Task<Optional<Uri>> GetIconUri(Skill skill)
|
||||
{
|
||||
if (this.liveOptions.Value.KeepLocalIconCache)
|
||||
var maybeIconUri = this.GetLocalIcon(skill);
|
||||
if (maybeIconUri.ExtractValue() is Uri uri)
|
||||
{
|
||||
this.logger.LogInformation($"{nameof(IconRetriever)} configured to look first in cache before downloading icons");
|
||||
var maybeIcon = await this.GetLocalIcon(skill);
|
||||
if (maybeIcon.ExtractValue() is Stream stream)
|
||||
{
|
||||
return stream;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
this.logger.LogInformation($"{nameof(IconRetriever)} configured to skip local cache. Downloading icon");
|
||||
return uri;
|
||||
}
|
||||
|
||||
return await this.DownloadIcon(skill);
|
||||
if (this.liveOptions.Value.ExperimentalFeatures.DownloadIcons)
|
||||
{
|
||||
this.logger.LogInformation("Application is configured to download icons");
|
||||
return await this.DownloadIcon(skill);
|
||||
}
|
||||
|
||||
return Optional.None<Uri>();
|
||||
}
|
||||
|
||||
private async Task<Optional<Stream>> DownloadIcon(Skill skill)
|
||||
private async Task<Optional<Uri>> DownloadIcon(Skill skill)
|
||||
{
|
||||
var curedSkillName = skill.Name
|
||||
.Replace(" ", "_");
|
||||
var skillIconUrl = QueryUrl.Replace(NamePlaceholder, curedSkillName);
|
||||
this.logger.LogInformation($"Looking up icon for skill '{skill.Name}' at url {skillIconUrl}");
|
||||
using var response = await this.httpClient.GetAsync(skillIconUrl).ConfigureAwait(false);
|
||||
if (response.IsSuccessStatusCode is false)
|
||||
var maybeBase64 = await this.GetBase64ForSkill(skill);
|
||||
if (maybeBase64.ExtractValue() is not string base64)
|
||||
{
|
||||
this.logger.LogError($"Client returned status code {response.StatusCode}");
|
||||
return Optional.None<Stream>();
|
||||
return Optional.None<Uri>();
|
||||
}
|
||||
|
||||
this.logger.LogInformation("Crawling through response for href to latest icon url");
|
||||
var doc = new HtmlDocument();
|
||||
doc.LoadHtml(await response.Content.ReadAsStringAsync());
|
||||
var url = GetHref(doc);
|
||||
if (url is null)
|
||||
{
|
||||
this.logger.LogError("Failed to find latest icon url");
|
||||
return Optional.None<Stream>();
|
||||
}
|
||||
|
||||
this.logger.LogInformation($"Found latest icon url at {BaseUrl + "/" + url}. Requesting stream");
|
||||
using var iconResponse = await this.httpClient.GetAsync(url);
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
this.logger.LogInformation("Retrieved latest icon stream");
|
||||
var iconData = await iconResponse.Content.ReadAsByteArrayAsync();
|
||||
if (this.liveOptions.Value.KeepLocalIconCache)
|
||||
{
|
||||
await SaveIconLocally(skill, iconData);
|
||||
}
|
||||
|
||||
return new MemoryStream(iconData);
|
||||
}
|
||||
|
||||
this.logger.LogError($"Failed to retrieve icon from {BaseUrl + "/" + url}");
|
||||
return Optional.None<Stream>();
|
||||
var bytes = Convert.FromBase64String(base64);
|
||||
var uri = await SaveIconLocally(skill, bytes);
|
||||
return new Uri(AppDomain.CurrentDomain.BaseDirectory + "/" + IconsLocation.Replace(NamePlaceholder, uri), UriKind.Absolute);
|
||||
}
|
||||
|
||||
private async Task<Optional<Stream>> GetLocalIcon(Skill skill)
|
||||
private Optional<Uri> GetLocalIcon(Skill skill)
|
||||
{
|
||||
var curedSkillName = skill.Name
|
||||
.Replace(" ", "_")
|
||||
@@ -108,33 +75,39 @@ namespace Daybreak.Services.IconRetrieve
|
||||
if (File.Exists(IconsLocation.Replace(NamePlaceholder, curedSkillName)))
|
||||
{
|
||||
this.logger.LogInformation("Local icon cache found. Retrieving icon");
|
||||
return new MemoryStream(await File.ReadAllBytesAsync(IconsLocation.Replace(NamePlaceholder, curedSkillName)));
|
||||
return new Uri(AppDomain.CurrentDomain.BaseDirectory + "/" + IconsLocation.Replace(NamePlaceholder, curedSkillName), UriKind.Absolute);
|
||||
}
|
||||
|
||||
this.logger.LogWarning("No local icon cache found");
|
||||
return Optional.None<Stream>();
|
||||
return Optional.None<Uri>();
|
||||
}
|
||||
|
||||
private static async Task SaveIconLocally(Skill skill, byte[] data)
|
||||
private async Task<Optional<string>> GetBase64ForSkill(Skill skill)
|
||||
{
|
||||
var request = new IconRequest { Skill = skill };
|
||||
this.iconBrowser.QueueIconRequest(request);
|
||||
|
||||
// Wait for the request to be served
|
||||
for (var i = 0; i < 20; i++)
|
||||
{
|
||||
if (request.Finished)
|
||||
{
|
||||
return request.IconBase64;
|
||||
}
|
||||
|
||||
await Task.Delay(1000);
|
||||
}
|
||||
|
||||
return Optional.None<string>();
|
||||
}
|
||||
|
||||
private static async Task<string> SaveIconLocally(Skill skill, byte[] data)
|
||||
{
|
||||
var curedSkillName = skill.Name
|
||||
.Replace(" ", "_")
|
||||
.Replace("\"", "");
|
||||
await File.WriteAllBytesAsync(IconsLocation.Replace(NamePlaceholder, curedSkillName), data);
|
||||
}
|
||||
|
||||
private static string GetHref(HtmlDocument doc)
|
||||
{
|
||||
foreach (var child in doc.DocumentNode.Descendants("a"))
|
||||
{
|
||||
var targetAttribute = child.Attributes.Where(a => a.Name == "href" && a.Value.Contains("images")).FirstOrDefault();
|
||||
if (targetAttribute is not null)
|
||||
{
|
||||
return targetAttribute.Value;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
return curedSkillName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,5 +31,32 @@
|
||||
};
|
||||
window.chrome.webview.postMessage(jsonObject);
|
||||
});";
|
||||
|
||||
public const string GetHrefFromSkillPage = @"
|
||||
new function(){
|
||||
var img = document.getElementsByClassName('fullImageLink')[0].childNodes[0].childNodes[0];
|
||||
function getDataUrl(img) {
|
||||
// Create canvas
|
||||
const canvas = document.createElement('canvas');
|
||||
const ctx = canvas.getContext('2d');
|
||||
// Set width and height
|
||||
canvas.width = img.width;
|
||||
canvas.height = img.height;
|
||||
// Draw the image
|
||||
ctx.drawImage(img, 0, 0);
|
||||
return canvas.toDataURL('image/jpeg');
|
||||
}
|
||||
|
||||
console.log(img.src);
|
||||
var imageBase64 = getDataUrl(img);
|
||||
console.log(imageBase64);
|
||||
let jsonObject =
|
||||
{
|
||||
skillUrl: document.URL,
|
||||
skillImage: imageBase64
|
||||
}
|
||||
window.chrome.webview.postMessage(jsonObject);
|
||||
return jsonObject;
|
||||
}";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ using Daybreak.Services.BuildTemplates;
|
||||
using Daybreak.Services.IconRetrieve;
|
||||
using Daybreak.Services.ViewManagement;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Services.IconRetrieve;
|
||||
using System;
|
||||
using System.Configuration;
|
||||
using System.Extensions;
|
||||
@@ -39,6 +40,7 @@ namespace Daybreak.Views
|
||||
IViewManager viewManager,
|
||||
IBuildTemplateManager buildTemplateManager,
|
||||
IIconRetriever iconRetriever,
|
||||
IIconBrowser iconBrowser,
|
||||
ILiveOptions<ApplicationConfiguration> liveOptions,
|
||||
ILogger<ChromiumBrowserWrapper> chromiumLogger,
|
||||
ILogger<BuildTemplateView> logger)
|
||||
@@ -47,7 +49,7 @@ namespace Daybreak.Views
|
||||
this.logger = logger.ThrowIfNull(nameof(logger));
|
||||
this.viewManager = viewManager.ThrowIfNull(nameof(viewManager));
|
||||
this.InitializeComponent();
|
||||
this.BuildTemplate.InitializeTemplate(iconRetriever, liveOptions, buildTemplateManager, chromiumLogger);
|
||||
this.BuildTemplate.InitializeTemplate(iconRetriever, iconBrowser, liveOptions, buildTemplateManager, chromiumLogger);
|
||||
this.DataContextChanged += (sender, contextArgs) =>
|
||||
{
|
||||
if (contextArgs.NewValue is BuildEntry)
|
||||
|
||||
@@ -92,6 +92,7 @@
|
||||
<TextBlock Text="GWToolbox startup delay (in ms)" Foreground="White" FontSize="22" Height="30"></TextBlock>
|
||||
<TextBlock Text="Detect build templates (in browser)" Foreground="White" FontSize="22" Height="30"></TextBlock>
|
||||
<TextBlock Text="Launch gw as current user" Foreground="White" FontSize="22" Height="30"></TextBlock>
|
||||
<TextBlock Text="Download icons" Foreground="White" FontSize="22" Height="30"></TextBlock>
|
||||
</StackPanel>
|
||||
<StackPanel Grid.Column="1">
|
||||
<ToggleButton IsChecked="{Binding ElementName=_this, Path=MultiLaunch, Mode=TwoWay}" Style="{StaticResource AnimatedSwitch}"
|
||||
@@ -103,6 +104,8 @@
|
||||
Height="30" Width="60"></ToggleButton>
|
||||
<ToggleButton IsChecked="{Binding ElementName=_this, Path=LaunchAsCurrentUser, Mode=TwoWay}" Style="{StaticResource AnimatedSwitch}"
|
||||
Height="30" Width="60"></ToggleButton>
|
||||
<ToggleButton IsChecked="{Binding ElementName=_this, Path=DownloadIcons, Mode=TwoWay}" Style="{StaticResource AnimatedSwitch}"
|
||||
Height="30" Width="60"></ToggleButton>
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</Grid>
|
||||
|
||||
@@ -16,46 +16,22 @@ namespace Daybreak.Views
|
||||
/// </summary>
|
||||
public partial class ExperimentalSettingsView : UserControl
|
||||
{
|
||||
public static readonly DependencyProperty MultiLaunchProperty =
|
||||
DependencyPropertyExtensions.Register<ExperimentalSettingsView, bool>(nameof(MultiLaunch));
|
||||
public static readonly DependencyProperty GWToolboxLaunchDelayProperty =
|
||||
DependencyPropertyExtensions.Register<ExperimentalSettingsView, string>(nameof(GWToolboxLaunchDelay));
|
||||
public static readonly DependencyProperty DynamicBuildLoadingProperty =
|
||||
DependencyPropertyExtensions.Register<ExperimentalSettingsView, bool>(nameof(DynamicBuildLoading));
|
||||
public static readonly DependencyProperty LaunchAsCurrentUserProperty =
|
||||
DependencyPropertyExtensions.Register<ExperimentalSettingsView, bool>(nameof(LaunchAsCurrentUser));
|
||||
public static readonly DependencyProperty MacrosEnabledProperty =
|
||||
DependencyPropertyExtensions.Register<ExperimentalSettingsView, bool>(nameof(MacrosEnabled));
|
||||
[GenerateDependencyProperty]
|
||||
private bool launchAsCurrentUser;
|
||||
[GenerateDependencyProperty]
|
||||
private bool multiLaunch;
|
||||
[GenerateDependencyProperty]
|
||||
private bool dynamicBuildLoading;
|
||||
[GenerateDependencyProperty]
|
||||
private bool macrosEnabled;
|
||||
[GenerateDependencyProperty]
|
||||
public string gWToolboxLaunchDelay;
|
||||
[GenerateDependencyProperty]
|
||||
public bool downloadIcons;
|
||||
|
||||
private readonly IViewManager viewManager;
|
||||
private readonly ILiveUpdateableOptions<ApplicationConfiguration> liveUpdateableOptions;
|
||||
|
||||
public bool LaunchAsCurrentUser
|
||||
{
|
||||
get => this.GetTypedValue<bool>(LaunchAsCurrentUserProperty);
|
||||
set => this.SetValue(LaunchAsCurrentUserProperty, value);
|
||||
}
|
||||
public bool MultiLaunch
|
||||
{
|
||||
get => this.GetTypedValue<bool>(MultiLaunchProperty);
|
||||
set => this.SetValue(MultiLaunchProperty, value);
|
||||
}
|
||||
public string GWToolboxLaunchDelay
|
||||
{
|
||||
get => this.GetTypedValue<string>(GWToolboxLaunchDelayProperty);
|
||||
set => this.SetValue(GWToolboxLaunchDelayProperty, value);
|
||||
}
|
||||
public bool DynamicBuildLoading
|
||||
{
|
||||
get => this.GetTypedValue<bool>(DynamicBuildLoadingProperty);
|
||||
set => this.SetValue(DynamicBuildLoadingProperty, value);
|
||||
}
|
||||
public bool MacrosEnabled
|
||||
{
|
||||
get => this.GetTypedValue<bool>(MacrosEnabledProperty);
|
||||
set => this.SetValue(MacrosEnabledProperty, value);
|
||||
}
|
||||
|
||||
public ExperimentalSettingsView(
|
||||
IViewManager viewManager,
|
||||
ILiveUpdateableOptions<ApplicationConfiguration> liveUpdateableOptions)
|
||||
@@ -74,6 +50,7 @@ namespace Daybreak.Views
|
||||
this.DynamicBuildLoading = config.ExperimentalFeatures.DynamicBuildLoading;
|
||||
this.LaunchAsCurrentUser = config.ExperimentalFeatures.LaunchGuildwarsAsCurrentUser;
|
||||
this.MacrosEnabled = config.ExperimentalFeatures.CanInterceptKeys;
|
||||
this.DownloadIcons = config.ExperimentalFeatures.DownloadIcons;
|
||||
}
|
||||
|
||||
private void SaveExperimentalSettings()
|
||||
@@ -83,6 +60,7 @@ namespace Daybreak.Views
|
||||
config.ExperimentalFeatures.DynamicBuildLoading = this.DynamicBuildLoading;
|
||||
config.ExperimentalFeatures.LaunchGuildwarsAsCurrentUser = this.LaunchAsCurrentUser;
|
||||
config.ExperimentalFeatures.CanInterceptKeys = this.MacrosEnabled;
|
||||
config.ExperimentalFeatures.DownloadIcons = this.DownloadIcons;
|
||||
if (int.TryParse(this.GWToolboxLaunchDelay, out var gwToolboxLaunchDelay))
|
||||
{
|
||||
config.ExperimentalFeatures.ToolboxAutoLaunchDelay = gwToolboxLaunchDelay;
|
||||
|
||||
@@ -94,7 +94,6 @@
|
||||
<TextBlock Text="Desired screen id: " FontSize="22" Foreground="White" Height="30"></TextBlock>
|
||||
<TextBlock Text="Place Shortcut: " FontSize="22" Foreground="White" Height="30"></TextBlock>
|
||||
<TextBlock Text="Shortcut folder: " FontSize="22" Foreground="White" Height="30"></TextBlock>
|
||||
<TextBlock Text="Keep local cache of icons: " FontSize="22" Foreground="White" Height="30"></TextBlock>
|
||||
</StackPanel>
|
||||
<StackPanel Orientation="Vertical" Grid.Row="1" Grid.Column="1">
|
||||
<ToggleButton HorizontalAlignment="Center" HorizontalContentAlignment="Stretch" Style="{StaticResource AnimatedSwitch}" Height="30" Width="40"
|
||||
@@ -149,8 +148,6 @@
|
||||
Foreground="White" BorderBrush="Gray" BorderThickness="0"
|
||||
Clicked="ShortcutFolderPickerGlyph_Clicked"></controls:FilePickerGlyph>
|
||||
</Grid>
|
||||
<ToggleButton HorizontalAlignment="Center" HorizontalContentAlignment="Stretch" Style="{StaticResource AnimatedSwitch}" Height="30" Width="40"
|
||||
FontSize="22" Foreground="White" IsChecked="{Binding ElementName=_this, Path=KeepLocalIconCache, Mode=TwoWay}"></ToggleButton>
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</ScrollViewer>
|
||||
|
||||
@@ -42,8 +42,6 @@ namespace Daybreak.Views
|
||||
private bool shortcutPlaced;
|
||||
[GenerateDependencyProperty]
|
||||
private bool autoCheckUpdate;
|
||||
[GenerateDependencyProperty]
|
||||
private bool keepLocalIconCache;
|
||||
|
||||
public SettingsView(
|
||||
ILiveUpdateableOptions<ApplicationConfiguration> liveUpdateableOptions,
|
||||
@@ -70,7 +68,6 @@ namespace Daybreak.Views
|
||||
this.ShortcutFolder = config.ShortcutLocation;
|
||||
this.ShortcutPlaced = config.PlaceShortcut;
|
||||
this.AutoCheckUpdate = config.AutoCheckUpdate;
|
||||
this.KeepLocalIconCache = config.KeepLocalIconCache;
|
||||
}
|
||||
|
||||
private void SaveButton_Clicked(object sender, EventArgs e)
|
||||
@@ -88,7 +85,6 @@ namespace Daybreak.Views
|
||||
currentConfig.ShortcutLocation = this.ShortcutFolder;
|
||||
currentConfig.PlaceShortcut = this.ShortcutPlaced;
|
||||
currentConfig.AutoCheckUpdate = this.AutoCheckUpdate;
|
||||
currentConfig.KeepLocalIconCache = this.KeepLocalIconCache;
|
||||
this.liveUpdateableOptions.UpdateOption();
|
||||
this.viewManager.ShowView<SettingsCategoryView>();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user