232 lines
		
	
	
		
			9.4 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			232 lines
		
	
	
		
			9.4 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using Dalamud.ContextMenu;
 | |
| using Dalamud.Game.Text.SeStringHandling;
 | |
| using MareSynchronos.API.Data;
 | |
| using MareSynchronos.API.Data.Enum;
 | |
| using MareSynchronos.API.Data.Extensions;
 | |
| using MareSynchronos.API.Dto.User;
 | |
| using MareSynchronos.PlayerData.Factories;
 | |
| using MareSynchronos.PlayerData.Handlers;
 | |
| using MareSynchronos.Services.Mediator;
 | |
| using MareSynchronos.Services.ServerConfiguration;
 | |
| using MareSynchronos.Utils;
 | |
| using Microsoft.Extensions.Logging;
 | |
| 
 | |
| namespace MareSynchronos.PlayerData.Pairs;
 | |
| 
 | |
| public class Pair
 | |
| {
 | |
|     private readonly PairHandlerFactory _cachedPlayerFactory;
 | |
|     private readonly SemaphoreSlim _creationSemaphore = new(1);
 | |
|     private readonly ILogger<Pair> _logger;
 | |
|     private readonly MareMediator _mediator;
 | |
|     private readonly ServerConfigurationManager _serverConfigurationManager;
 | |
|     private CancellationTokenSource _applicationCts = new CancellationTokenSource();
 | |
|     private OnlineUserIdentDto? _onlineUserIdentDto = null;
 | |
| 
 | |
|     public Pair(ILogger<Pair> logger, UserFullPairDto userPair, PairHandlerFactory cachedPlayerFactory,
 | |
|         MareMediator mediator, ServerConfigurationManager serverConfigurationManager)
 | |
|     {
 | |
|         _logger = logger;
 | |
|         UserPair = userPair;
 | |
|         _cachedPlayerFactory = cachedPlayerFactory;
 | |
|         _mediator = mediator;
 | |
|         _serverConfigurationManager = serverConfigurationManager;
 | |
|     }
 | |
| 
 | |
|     public bool HasCachedPlayer => CachedPlayer != null && !string.IsNullOrEmpty(CachedPlayer.PlayerName) && _onlineUserIdentDto != null;
 | |
|     public IndividualPairStatus IndividualPairStatus => UserPair.IndividualPairStatus;
 | |
|     public bool IsDirectlyPaired => IndividualPairStatus != IndividualPairStatus.None;
 | |
|     public bool IsOneSidedPair => IndividualPairStatus == IndividualPairStatus.OneSided;
 | |
|     public bool IsOnline => CachedPlayer != null;
 | |
| 
 | |
|     public bool IsPaired => IndividualPairStatus == IndividualPairStatus.Bidirectional || UserPair.Groups.Any();
 | |
|     public bool IsPaused => UserPair.OtherPermissions.IsPaused() || UserPair.OwnPermissions.IsPaused();
 | |
|     public bool IsVisible => CachedPlayer?.IsVisible ?? false;
 | |
|     public CharacterData? LastReceivedCharacterData { get; set; }
 | |
|     public string? PlayerName => CachedPlayer?.PlayerName ?? string.Empty;
 | |
| 
 | |
|     public UserData UserData => UserPair.User;
 | |
| 
 | |
|     public UserFullPairDto UserPair { get; set; }
 | |
|     private PairHandler? CachedPlayer { get; set; }
 | |
| 
 | |
|     public void AddContextMenu(GameObjectContextMenuOpenArgs args)
 | |
|     {
 | |
|         if (CachedPlayer == null || args.ObjectId != CachedPlayer.PlayerCharacterId || IsPaused) return;
 | |
| 
 | |
|         SeStringBuilder seStringBuilder = new();
 | |
|         SeStringBuilder seStringBuilder2 = new();
 | |
|         SeStringBuilder seStringBuilder3 = new();
 | |
|         SeStringBuilder seStringBuilder4 = new();
 | |
|         var openProfileSeString = seStringBuilder.AddUiForeground(526).AddText(" ").AddUiForegroundOff().AddText("Open Profile").Build();
 | |
|         var reapplyDataSeString = seStringBuilder2.AddUiForeground(526).AddText(" ").AddUiForegroundOff().AddText("Reapply last data").Build();
 | |
|         var cyclePauseState = seStringBuilder3.AddUiForeground(526).AddText(" ").AddUiForegroundOff().AddText("Cycle pause state").Build();
 | |
|         var changePermissions = seStringBuilder4.AddUiForeground(526).AddText(" ").AddUiForegroundOff().AddText("Change Permissions").Build();
 | |
|         args.AddCustomItem(new GameObjectContextMenuItem(openProfileSeString, (a) =>
 | |
|         {
 | |
|             _mediator.Publish(new ProfileOpenStandaloneMessage(this));
 | |
|         }));
 | |
|         args.AddCustomItem(new GameObjectContextMenuItem(reapplyDataSeString, (a) =>
 | |
|         {
 | |
|             ApplyLastReceivedData(forced: true);
 | |
|         }, useDalamudIndicator: false));
 | |
|         args.AddCustomItem(new GameObjectContextMenuItem(changePermissions, (a) =>
 | |
|         {
 | |
|             _mediator.Publish(new OpenPermissionWindow(this));
 | |
|         }, useDalamudIndicator: false));
 | |
|         args.AddCustomItem(new GameObjectContextMenuItem(cyclePauseState, (a) =>
 | |
|         {
 | |
|             _mediator.Publish(new CyclePauseMessage(UserData));
 | |
|         }, useDalamudIndicator: false));
 | |
|     }
 | |
| 
 | |
|     public void ApplyData(OnlineUserCharaDataDto data)
 | |
|     {
 | |
|         _applicationCts = _applicationCts.CancelRecreate();
 | |
|         LastReceivedCharacterData = data.CharaData;
 | |
| 
 | |
|         if (CachedPlayer == null)
 | |
|         {
 | |
|             _logger.LogDebug("Received Data for {uid} but CachedPlayer does not exist, waiting", data.User.UID);
 | |
|             _ = Task.Run(async () =>
 | |
|             {
 | |
|                 using var timeoutCts = new CancellationTokenSource();
 | |
|                 timeoutCts.CancelAfter(TimeSpan.FromSeconds(120));
 | |
|                 var appToken = _applicationCts.Token;
 | |
|                 using var combined = CancellationTokenSource.CreateLinkedTokenSource(timeoutCts.Token, appToken);
 | |
|                 while (CachedPlayer == null && !combined.Token.IsCancellationRequested)
 | |
|                 {
 | |
|                     await Task.Delay(250, combined.Token).ConfigureAwait(false);
 | |
|                 }
 | |
| 
 | |
|                 if (!combined.IsCancellationRequested)
 | |
|                 {
 | |
|                     _logger.LogDebug("Applying delayed data for {uid}", data.User.UID);
 | |
|                     ApplyLastReceivedData();
 | |
|                 }
 | |
|             });
 | |
|             return;
 | |
|         }
 | |
| 
 | |
|         ApplyLastReceivedData();
 | |
|     }
 | |
| 
 | |
|     public void ApplyLastReceivedData(bool forced = false)
 | |
|     {
 | |
|         if (CachedPlayer == null) return;
 | |
|         if (LastReceivedCharacterData == null) return;
 | |
| 
 | |
|         CachedPlayer.ApplyCharacterData(Guid.NewGuid(), RemoveNotSyncedFiles(LastReceivedCharacterData.DeepClone())!, forced);
 | |
|     }
 | |
| 
 | |
|     public void CreateCachedPlayer(OnlineUserIdentDto? dto = null)
 | |
|     {
 | |
|         try
 | |
|         {
 | |
|             _creationSemaphore.Wait();
 | |
| 
 | |
|             if (CachedPlayer != null) return;
 | |
| 
 | |
|             if (dto == null && _onlineUserIdentDto == null)
 | |
|             {
 | |
|                 CachedPlayer?.Dispose();
 | |
|                 CachedPlayer = null;
 | |
|                 return;
 | |
|             }
 | |
|             if (dto != null)
 | |
|             {
 | |
|                 _onlineUserIdentDto = dto;
 | |
|             }
 | |
| 
 | |
|             CachedPlayer?.Dispose();
 | |
|             CachedPlayer = _cachedPlayerFactory.Create(_onlineUserIdentDto!);
 | |
|         }
 | |
|         finally
 | |
|         {
 | |
|             _creationSemaphore.Release();
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     public string? GetNote()
 | |
|     {
 | |
|         return _serverConfigurationManager.GetNoteForUid(UserData.UID);
 | |
|     }
 | |
| 
 | |
|     public string GetPlayerNameHash()
 | |
|     {
 | |
|         return CachedPlayer?.PlayerNameHash ?? string.Empty;
 | |
|     }
 | |
| 
 | |
|     public bool HasAnyConnection()
 | |
|     {
 | |
|         return UserPair.Groups.Any() || UserPair.IndividualPairStatus != IndividualPairStatus.None;
 | |
|     }
 | |
| 
 | |
|     public void MarkOffline()
 | |
|     {
 | |
|         try
 | |
|         {
 | |
|             _creationSemaphore.Wait();
 | |
|             _onlineUserIdentDto = null;
 | |
|             LastReceivedCharacterData = null;
 | |
|             var player = CachedPlayer;
 | |
|             CachedPlayer = null;
 | |
|             player?.Dispose();
 | |
|         }
 | |
|         finally
 | |
|         {
 | |
|             _creationSemaphore.Release();
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     public void SetNote(string note)
 | |
|     {
 | |
|         _serverConfigurationManager.SetNoteForUid(UserData.UID, note);
 | |
|     }
 | |
| 
 | |
|     internal void SetIsUploading()
 | |
|     {
 | |
|         CachedPlayer?.SetUploading();
 | |
|     }
 | |
| 
 | |
|     private CharacterData? RemoveNotSyncedFiles(CharacterData? data)
 | |
|     {
 | |
|         _logger.LogTrace("Removing not synced files");
 | |
|         if (data == null)
 | |
|         {
 | |
|             _logger.LogTrace("Nothing to remove");
 | |
|             return data;
 | |
|         }
 | |
| 
 | |
|         bool disableIndividualAnimations = (UserPair.OtherPermissions.IsDisableAnimations() || UserPair.OwnPermissions.IsDisableAnimations());
 | |
|         bool disableIndividualVFX = (UserPair.OtherPermissions.IsDisableVFX() || UserPair.OwnPermissions.IsDisableVFX());
 | |
|         bool disableIndividualSounds = (UserPair.OtherPermissions.IsDisableSounds() || UserPair.OwnPermissions.IsDisableSounds());
 | |
| 
 | |
|         _logger.LogTrace("Disable: Sounds: {disableIndividualSounds}, Anims: {disableIndividualAnims}; " +
 | |
|             "VFX: {disableGroupSounds}",
 | |
|             disableIndividualSounds, disableIndividualAnimations, disableIndividualVFX);
 | |
| 
 | |
|         if (disableIndividualAnimations || disableIndividualSounds || disableIndividualVFX)
 | |
|         {
 | |
|             _logger.LogTrace("Data cleaned up: Animations disabled: {disableAnimations}, Sounds disabled: {disableSounds}, VFX disabled: {disableVFX}",
 | |
|                 disableIndividualAnimations, disableIndividualSounds, disableIndividualVFX);
 | |
|             foreach (var objectKind in data.FileReplacements.Select(k => k.Key))
 | |
|             {
 | |
|                 if (disableIndividualSounds)
 | |
|                     data.FileReplacements[objectKind] = data.FileReplacements[objectKind]
 | |
|                         .Where(f => !f.GamePaths.Any(p => p.EndsWith("scd", StringComparison.OrdinalIgnoreCase)))
 | |
|                         .ToList();
 | |
|                 if (disableIndividualAnimations)
 | |
|                     data.FileReplacements[objectKind] = data.FileReplacements[objectKind]
 | |
|                         .Where(f => !f.GamePaths.Any(p => p.EndsWith("tmb", StringComparison.OrdinalIgnoreCase) || p.EndsWith("pap", StringComparison.OrdinalIgnoreCase)))
 | |
|                         .ToList();
 | |
|                 if (disableIndividualVFX)
 | |
|                     data.FileReplacements[objectKind] = data.FileReplacements[objectKind]
 | |
|                         .Where(f => !f.GamePaths.Any(p => p.EndsWith("atex", StringComparison.OrdinalIgnoreCase) || p.EndsWith("avfx", StringComparison.OrdinalIgnoreCase)))
 | |
|                         .ToList();
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         return data;
 | |
|     }
 | |
| } | 
