100 lines
4.8 KiB
C#
100 lines
4.8 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.Windows.Input;
|
|
using System.Windows.Threading;
|
|
using JD.Guinness.WPF.Module.Model;
|
|
using JD.Guinness.WPF.Module.Model.Ps4;
|
|
using JD.Guinness.WPF.Module.Model.Ps5;
|
|
using JD.Guinness.WPF.Module.Model.Stadia;
|
|
using JD.Guinness.WPF.Module.Model.XBoxOne;
|
|
using JD.Guinness.WPF.Module.Model.XBoxSeriesX;
|
|
using JD.Guinness.WPF.Module.ViewModel;
|
|
using Plume.Icons;
|
|
using Plume.Perforce.Service.Api;
|
|
|
|
namespace JD.Guinness.WPF.Module.PlumeAction
|
|
{
|
|
public sealed class RefreshResourcesAction : Plume.Interfaces.WPF.Actions.Action
|
|
{
|
|
private readonly GameViewModel _gameViewModel;
|
|
private readonly IPerforceService _perforceService;
|
|
|
|
public RefreshResourcesAction(GameViewModel gameViewModel, IPerforceService perforceService)
|
|
: base("Refresh resources", "Refresh resources", StandardIcon.Refresh, new KeyGesture(Key.R, ModifierKeys.Control))
|
|
{
|
|
_gameViewModel = gameViewModel;
|
|
_perforceService = perforceService;
|
|
}
|
|
|
|
private static void RefreshImage(Func<string> getter, Action<string> setter)
|
|
{
|
|
string imagePath = getter();
|
|
if (!string.IsNullOrEmpty(imagePath))
|
|
{
|
|
setter(null);
|
|
// wait for ui display
|
|
Application.Current.Dispatcher.Invoke(new Action(() => { }), DispatcherPriority.ContextIdle, null);
|
|
setter(imagePath);
|
|
}
|
|
}
|
|
|
|
public override bool CanExecute() => !_gameViewModel.IsWorking && _gameViewModel.HasWorkspace;
|
|
|
|
public override async void TryExecute() => await _gameViewModel.StartProcess(Task.Run(() =>
|
|
{
|
|
if (!_perforceService.Connect(_gameViewModel.Settings.PerforceServer, Environment.UserName, _gameViewModel.Settings.WorkspaceName))
|
|
{
|
|
throw new Exception($"Unable to connect on the server '{_gameViewModel.Settings.PerforceServer}' with user '{Environment.UserName}' on client '{_gameViewModel.Settings.WorkspaceName}'.");
|
|
}
|
|
|
|
try
|
|
{
|
|
_perforceService.Connection.Sync(Path.Combine(_gameViewModel.Settings.WorkspacePath, _gameViewModel.Settings.Ps4ImagesFolderPath) + "\\...");
|
|
_perforceService.Connection.Sync(Path.Combine(_gameViewModel.Settings.WorkspacePath, _gameViewModel.Settings.Ps5ImagesFolderPath) + "\\...");
|
|
_perforceService.Connection.Sync(Path.Combine(_gameViewModel.Settings.WorkspacePath, _gameViewModel.Settings.XBoxOneImagesFolderPath) + "\\...");
|
|
_perforceService.Connection.Sync(Path.Combine(_gameViewModel.Settings.WorkspacePath, _gameViewModel.Settings.XBoxSeriesXImagesFolderPath) + "\\...");
|
|
_perforceService.Connection.Sync(Path.Combine(_gameViewModel.Settings.WorkspacePath, _gameViewModel.Settings.StadiaImagesFolderPath) + "\\...");
|
|
}
|
|
finally
|
|
{
|
|
_perforceService.Disconnect();
|
|
}
|
|
|
|
foreach (APlatformChallenge challenge in _gameViewModel.Challenges.SelectMany(c => c.PlatformChallenges))
|
|
{
|
|
switch (challenge)
|
|
{
|
|
case Ps4Trophy ps4:
|
|
RefreshImage(() => ps4.ImagePath, i => ps4.ImagePath = i);
|
|
break;
|
|
|
|
case Ps5Trophy ps5:
|
|
RefreshImage(() => ps5.ImagePath, i => ps5.ImagePath = i);
|
|
break;
|
|
|
|
case XBoxOneAchievement xBoxOne:
|
|
RefreshImage(() => xBoxOne.ImagePath, i => xBoxOne.ImagePath = i);
|
|
break;
|
|
|
|
case XBoxSeriesXAchievement xBoxSeriesX:
|
|
RefreshImage(() => xBoxSeriesX.ImagePath, i => xBoxSeriesX.ImagePath = i);
|
|
break;
|
|
|
|
case StadiaAchievement stadia:
|
|
RefreshImage(() => stadia.LockedImagePath, i => stadia.LockedImagePath = i);
|
|
RefreshImage(() => stadia.UnlockedImagePath, i => stadia.UnlockedImagePath = i);
|
|
break;
|
|
}
|
|
}
|
|
|
|
RefreshImage(() => _gameViewModel.Ps4Configuration.TitleImagePath, i => _gameViewModel.Ps4Configuration.TitleImagePath = i);
|
|
RefreshImage(() => _gameViewModel.Ps5Configuration.TitleImagePath, i => _gameViewModel.Ps5Configuration.TitleImagePath = i);
|
|
}),
|
|
"Refreshing resources",
|
|
"Refreshed resources",
|
|
"Failed to refresh resources").ConfigureAwait(true);
|
|
}
|
|
}
|