JD2022-TU1/main/tools/modules/JD.Guinness.WPF.Module/ViewModel/Ps4ViewModel.cs

140 lines
7 KiB
C#

using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using JD.Guinness.WPF.Module.Extension;
using JD.Guinness.WPF.Module.Model;
using JD.Guinness.WPF.Module.Model.Ps4;
using JD.Guinness.WPF.Module.Model.Ps5;
using JD.SonyTrophy.Service;
namespace JD.Guinness.WPF.Module.ViewModel
{
public sealed class Ps4ViewModel : APlatformChallengeViewModel<Ps4Trophy>
{
private readonly ISonyTrophyService _sonyTrophyService;
public DelegateCommand SetTitleImageCommand { get; }
public DelegateCommand<Ps4Trophy> SetImageCommand { get; }
public DelegateCommand SetImageFromIdCommand { get; }
public DelegateCommand AddZoneCommand { get; }
public DelegateCommand<Ps4Zone> RemoveZoneCommand { get; }
public DelegateCommand<Ps4Zone> SelectTrpFileCommand { get; }
public AsyncDelegateCommand SetSameAsPs5Command { get; }
public Ps4Configuration Configuration => Game.Ps4Configuration;
public int TotalPoints => PlatformChallenges.Sum(t => t.Grade.ToPoints());
public Ps4ViewModel(GameViewModel gameViewModel, ISonyTrophyService sonyTrophyService)
: base(gameViewModel)
{
_sonyTrophyService = sonyTrophyService ?? throw new ArgumentNullException(nameof(sonyTrophyService));
SetTitleImageCommand = new DelegateCommand(OnSetTitleImage, () => Game.IsAvailable);
SetImageCommand = new DelegateCommand<Ps4Trophy>(OnSetImage, _ => Game.IsAvailable);
SetImageFromIdCommand = new DelegateCommand(OnSetImageFromId, () => Game.IsAvailable);
AddZoneCommand = new DelegateCommand(OnAddZone, () => Game.IsAvailable);
RemoveZoneCommand = new DelegateCommand<Ps4Zone>(OnRemoveZone, _ => Game.IsAvailable);
SelectTrpFileCommand = new DelegateCommand<Ps4Zone>(OnSelectTrpFile, _ => Game.IsAvailable);
SetSameAsPs5Command = new AsyncDelegateCommand(OnSetSameAsPs5, () => Game.IsAvailable);
}
private void OnSetTitleImage() => SelectFile("Select the title image", Path.Combine(Game.Settings.WorkspacePath, Game.Settings.Ps4ImagesFolderPath), "png|*.png", file => Configuration.TitleImagePath = file);
private void OnSetImage(Ps4Trophy trophy) => SelectFile("Select the trophy image", Path.Combine(Game.Settings.WorkspacePath, Game.Settings.Ps4ImagesFolderPath), "png|*.png", file => trophy.ImagePath = file);
private void OnSetImageFromId() => SetImageFromId(Path.Combine(Game.Settings.WorkspacePath, Game.Settings.Ps4ImagesFolderPath), (trophy, file) => trophy.ImagePath = file);
private void OnAddZone() => Configuration.Zones.Add(new Ps4Zone(Game.Languages));
private void OnRemoveZone(Ps4Zone zone)
{
if (MessageBox.Show("Are you sure you want to remove this PS4 zone?", "Remove PS4 zone", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
{
Configuration.Zones.Remove(zone);
}
}
private void OnSelectTrpFile(Ps4Zone zone) => SaveFile("Select where to generate the trp file", "trp|*.trp", file => zone.TrpFilePath = file);
private async Task OnSetSameAsPs5()
{
await Game.StartProcess(
Task.Run(() =>
{
foreach (Ps4Trophy achievement in PlatformChallenges.ToList())
{
achievement.Remove();
achievement.Challenge.NotifyPlatformChallenges();
}
Configuration.TitleName = Game.Ps4Configuration.TitleName;
Configuration.TitleDescription = Game.Ps4Configuration.TitleDescription;
foreach (Challenge challenge in Game.Challenges)
{
Ps5Trophy ps5 = challenge.GetPlatformChallenge<Ps5Trophy>();
if (ps5 != null)
{
Ps4Trophy ps4 = challenge.GetOrAddPlatformChallenge<Ps4Trophy>();
ps4.CustomData.Add(ps5.CustomData.Values);
ps4.Id = ps5.Id;
ps4.ImagePath = ps5.ImagePath.FromDifferentDirectory(Path.Combine(Game.Settings.WorkspacePath, Game.Settings.Ps4ImagesFolderPath));
ps4.Grade = ps5.Grade;
ps4.IsRequiredForPlatinium = ps5.IsRequiredForPlatinium;
ps4.IsSecret = ps5.IsSecret;
challenge.NotifyPlatformChallenges();
}
}
}),
"PS4 - Replicating PS5 achievements",
"PS4 - Replicated PS5 achievements",
"PS4 - Failed to replicate PS5 achievements").ConfigureAwait(true);
}
protected override Ps4Trophy DuplicatePlatFormChallengeInto(Ps4Trophy platformChallenge, Challenge challenge) => platformChallenge.DuplicateInto(challenge);
protected override void OnPlatformChallengeChanged(object target, PropertyChangedEventArgs args)
{
if (target is null
|| (target is Ps4Trophy && args.PropertyName == nameof(Ps4Trophy.Grade)))
{
OnPropertyChanged(nameof(TotalPoints));
}
}
protected override async Task OnGenerate()
{
foreach (Ps4Zone zone in Configuration.Zones)
{
await Game.OpenForEditOrAdd(zone.TrpFilePath).ConfigureAwait(false);
await Game.StartProcess(
_sonyTrophyService.GenerateTrophyPackFileAsync(
new OldTrophySet(
Platform.PS4,
zone.NpCommId,
Configuration.Version,
new Text(Configuration.TitleName.GetMasterText(), Configuration.TitleDescription.GetMasterText()),
zone.Languages.Where(s => s.IsSelected && s.Value.Ps4Id >= 0).ToDictionary(
s => s.Value.Ps4Id,
s => new Text(Configuration.TitleName.GetTranslation(s.Value.Language), Configuration.TitleDescription.GetTranslation(s.Value.Language))),
Configuration.TitleImagePath,
PlatformChallenges
.Select(t => t.ToServiceTrophy(zone.Languages.Where(s => s.IsSelected && s.Value.Ps4Id >= 0).Select(s => s.Value)))
.ToArray()),
Game.Settings.SonyPublishingToolFilePath,
zone.TrpFilePath),
$"PS4 - Generating trp file \"{zone.TrpFilePath}\" for \"{zone.Name}\"",
$"PS4 - Generated trp file \"{zone.TrpFilePath}\" for \"{zone.Name}\"",
$"PS4 - Failed to generate trp file \"{zone.TrpFilePath}\" for \"{zone.Name}\"").ConfigureAwait(true);
}
}
}
}