Skip to content

Commit

Permalink
重写了头像加载与皮肤缓存服务
Browse files Browse the repository at this point in the history
  • Loading branch information
natsurainko committed Jul 16, 2024
1 parent 5d5d6e5 commit 7c9aa93
Show file tree
Hide file tree
Showing 24 changed files with 565 additions and 325 deletions.
Binary file added Natsurainko.FluentLauncher/Assets/skin_steve.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:behaviors="using:Natsurainko.FluentLauncher.XamlHelpers.Behaviors"
xmlns:i="using:Microsoft.Xaml.Interactivity"
xmlns:local="using:Natsurainko.FluentLauncher.Views">
xmlns:userControls="using:Natsurainko.FluentLauncher.UserControls">
<DataTemplate x:Key="NotifyPresenterTemplate">
<Border
MinWidth="100"
Expand Down Expand Up @@ -157,16 +155,12 @@
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>

<Border
<userControls:AccountAvatar
Grid.RowSpan="2"
Grid.Column="0"
Width="32"
Height="32"
CornerRadius="4">
<i:Interaction.Behaviors>
<behaviors:SkinHeadControlBehavior Account="{Binding}" />
</i:Interaction.Behaviors>
</Border>
Account="{Binding}" />
<TextBlock
Grid.Row="0"
Grid.Column="2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<None Remove="Assets\MinecraftTen.ttf" />
<None Remove="Assets\Rig_alex.obj" />
<None Remove="Assets\Rig_steve.obj" />
<None Remove="Assets\skin_steve.png" />
<Content Remove="Assets\Libs\authlib-injector-1.2.5.jar" />
<Content Remove="Assets\PackageIcons\SplashScreen.scale-100.scale-100.png" />
<Content Remove="Assets\PackageIcons\SplashScreen.scale-100.scale-125.png" />
Expand Down
4 changes: 2 additions & 2 deletions Natsurainko.FluentLauncher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
pages.WithPage<Views.Settings.DownloadPage, ViewModels.Settings.DownloadViewModel>("Settings/Download");
pages.WithPage<Views.Settings.AppearancePage, ViewModels.Settings.AppearanceViewModel>("Settings/Appearance");
pages.WithPage<Views.Settings.AboutPage, ViewModels.Settings.AboutViewModel>("Settings/About");
pages.WithPage<Views.Settings.SkinPage>("Settings/Account/Skin");
pages.WithPage<Views.Settings.SkinPage, ViewModels.Settings.SkinViewModel>("Settings/Account/Skin");

#endregion

Expand All @@ -97,7 +97,7 @@
services.AddSingleton<AuthenticationService>();
services.AddSingleton<NotificationService>();
services.AddSingleton<AppearanceService>();
services.AddSingleton<SkinCacheService>();
services.AddSingleton<CacheSkinService>();
services.AddSingleton<InterfaceCacheService>();
services.AddSingleton<JumpListService>();

Expand Down
11 changes: 5 additions & 6 deletions Natsurainko.FluentLauncher/Services/Accounts/AccountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ internal class AccountService
private readonly LocalStorageService _storageService;
private readonly SettingsService _settingsService;
private readonly AuthenticationService _authService;
private readonly SkinCacheService _skinCacheService;

public readonly string AccountsJsonPath = Path.Combine("settings", "accounts.json");

Expand Down Expand Up @@ -60,13 +59,11 @@ public Account? ActiveAccount
public AccountService(
SettingsService settingsService,
LocalStorageService storageService,
AuthenticationService authService,
SkinCacheService skinCacheService)
AuthenticationService authService)
{
_settingsService = settingsService;
_storageService = storageService;
_authService = authService;
_skinCacheService = skinCacheService;

_accounts = new ObservableCollection<Account>(InitializeAccountCollection());
Accounts = new ReadOnlyObservableCollection<Account>(_accounts);
Expand Down Expand Up @@ -188,8 +185,10 @@ public async Task RefreshAccount(Account account)
if (isActiveAccount)
ActivateAccount(refreshedAccount);

// Cache skin
await Task.Run(() => _skinCacheService.TryCacheSkin(refreshedAccount));
App.GetService<CacheSkinService>().CacheSkinOfAccount(refreshedAccount).ContinueWith(task =>
{
});
}

public async Task RefreshActiveAccount()
Expand Down
85 changes: 85 additions & 0 deletions Natsurainko.FluentLauncher/Services/Storage/CacheSkinService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using CommunityToolkit.Mvvm.Messaging;
using Natsurainko.FluentLauncher.Services.UI.Messaging;
using Nrk.FluentCore.Authentication;
using Nrk.FluentCore.Management.Downloader.Data;
using Nrk.FluentCore.Utils;
using System;
using System.IO;
using System.Linq;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Windows.Storage;

namespace Natsurainko.FluentLauncher.Services.Storage;

internal class CacheSkinService
{
private readonly LocalStorageService _localStorageService;

public CacheSkinService(LocalStorageService localStorageService)
{
_localStorageService = localStorageService;
}

public string GetSkinFilePath(Account account)
{
if (account.Type == AccountType.Offline)
return "ms-appx:///Assets/Icons/steve.png";

return _localStorageService.GetFile($"cache-skins\\{account.Type}\\{account.Uuid}.png").FullName;
}

public async Task<bool> CacheSkinOfAccount(Account account)
{
if (account == null || account.Type.Equals(AccountType.Offline))
return false;

var authorization = new Tuple<string, string>("Bearer", account.AccessToken);
var skinUrl = string.Empty;

if (account is YggdrasilAccount yggdrasil)
{
using var responseMessage = HttpUtils.HttpGet(
yggdrasil.YggdrasilServerUrl +
"/sessionserver/session/minecraft/profile/" +
yggdrasil.Uuid.ToString("N").ToLower()
, authorization);

responseMessage.EnsureSuccessStatusCode();

var jsonBase64 = JsonNode.Parse(responseMessage.Content.ReadAsString())!["properties"]![0]!["value"]!;
var json = JsonNode.Parse(jsonBase64.GetValue<string>().ConvertFromBase64());

skinUrl = json!["textures"]?["SKIN"]?["url"]?.GetValue<string>();
}

if (account is MicrosoftAccount microsoft)
{
using var responseMessage = HttpUtils.HttpGet("https://api.minecraftservices.com/minecraft/profile", authorization);
responseMessage.EnsureSuccessStatusCode();

var json = JsonNode.Parse(responseMessage.Content.ReadAsString())!["skins"]!
.AsArray().Where(item => (item!["state"]?.GetValue<string>().Equals("ACTIVE")).GetValueOrDefault()).FirstOrDefault();

skinUrl = json!["url"]!.GetValue<string>();
}

var skinFilePath = GetSkinFilePath(account);
if (!string.IsNullOrEmpty(skinUrl))
{
var downloadResult = await HttpUtils.DownloadElementAsync(new DownloadElement
{
AbsolutePath = skinFilePath,
Url = skinUrl,
});

if (downloadResult.IsFaulted)
return false;
}
else File.Copy((await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/skin_steve.png"))).Path, skinFilePath, true);

WeakReferenceMessenger.Default.Send(new AccountSkinCacheUpdatedMessage(account));

return true;
}
}
188 changes: 0 additions & 188 deletions Natsurainko.FluentLauncher/Services/Storage/SkinCacheService.cs

This file was deleted.

5 changes: 5 additions & 0 deletions Natsurainko.FluentLauncher/Services/UI/Messaging/Messages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ public SettingsStringValueChangedMessage(string value, string propertyName) : ba
{
PropertyName = propertyName;
}
}

class AccountSkinCacheUpdatedMessage : ValueChangedMessage<Account>
{
public AccountSkinCacheUpdatedMessage(Account value) : base(value) { }
}
Loading

0 comments on commit 7c9aa93

Please sign in to comment.