cleanup and fix application issues
This commit is contained in:
@@ -1,10 +1,133 @@
|
||||
using Dalamud.Game.ClientState.Objects.Types;
|
||||
using MareSynchronos.API.Data;
|
||||
using MareSynchronos.API.Data.Enum;
|
||||
using MareSynchronos.PlayerData.Handlers;
|
||||
using MareSynchronos.PlayerData.Pairs;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace MareSynchronos.Utils;
|
||||
|
||||
public static class VariousExtensions
|
||||
{
|
||||
public static void CancelDispose(this CancellationTokenSource? cts)
|
||||
{
|
||||
try
|
||||
{
|
||||
cts?.Cancel();
|
||||
cts?.Dispose();
|
||||
}
|
||||
catch (ObjectDisposedException)
|
||||
{
|
||||
// swallow it
|
||||
}
|
||||
}
|
||||
|
||||
public static CancellationTokenSource CancelRecreate(this CancellationTokenSource? cts)
|
||||
{
|
||||
cts.CancelDispose();
|
||||
return new CancellationTokenSource();
|
||||
}
|
||||
|
||||
public static Dictionary<ObjectKind, HashSet<PlayerChanges>> CheckUpdatedData(this CharacterData newData, CharacterData? oldData, ILogger logger, PairHandler cachedPlayer, bool forced, bool applyLastOnVisible)
|
||||
{
|
||||
oldData ??= new();
|
||||
var charaDataToUpdate = new Dictionary<ObjectKind, HashSet<PlayerChanges>>();
|
||||
foreach (ObjectKind objectKind in Enum.GetValues<ObjectKind>())
|
||||
{
|
||||
charaDataToUpdate[objectKind] = new();
|
||||
oldData.FileReplacements.TryGetValue(objectKind, out var existingFileReplacements);
|
||||
newData.FileReplacements.TryGetValue(objectKind, out var newFileReplacements);
|
||||
oldData.GlamourerData.TryGetValue(objectKind, out var existingGlamourerData);
|
||||
newData.GlamourerData.TryGetValue(objectKind, out var newGlamourerData);
|
||||
|
||||
bool hasNewButNotOldFileReplacements = newFileReplacements != null && existingFileReplacements == null;
|
||||
bool hasOldButNotNewFileReplacements = existingFileReplacements != null && newFileReplacements == null;
|
||||
|
||||
bool hasNewButNotOldGlamourerData = newGlamourerData != null && existingGlamourerData == null;
|
||||
bool hasOldButNotNewGlamourerData = existingGlamourerData != null && newGlamourerData == null;
|
||||
|
||||
bool hasNewAndOldFileReplacements = newFileReplacements != null && existingFileReplacements != null;
|
||||
bool hasNewAndOldGlamourerData = newGlamourerData != null && existingGlamourerData != null;
|
||||
|
||||
if (hasNewButNotOldFileReplacements || hasOldButNotNewFileReplacements || hasNewButNotOldGlamourerData || hasOldButNotNewGlamourerData)
|
||||
{
|
||||
logger.LogDebug("Updating {object}/{kind} (Some new data arrived: NewButNotOldFiles:{hasNewButNotOldFileReplacements}," +
|
||||
" OldButNotNewFiles:{hasOldButNotNewFileReplacements}, NewButNotOldGlam:{hasNewButNotOldGlamourerData}, OldButNotNewGlam:{hasOldButNotNewGlamourerData}) => {change}, {change2}",
|
||||
cachedPlayer, objectKind, hasNewButNotOldFileReplacements, hasOldButNotNewFileReplacements, hasNewButNotOldGlamourerData, hasOldButNotNewGlamourerData, PlayerChanges.ModFiles, PlayerChanges.Glamourer);
|
||||
charaDataToUpdate[objectKind].Add(PlayerChanges.ModFiles);
|
||||
charaDataToUpdate[objectKind].Add(PlayerChanges.Glamourer);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (hasNewAndOldFileReplacements)
|
||||
{
|
||||
bool listsAreEqual = oldData.FileReplacements[objectKind].SequenceEqual(newData.FileReplacements[objectKind], PlayerData.Data.FileReplacementDataComparer.Instance);
|
||||
if (!listsAreEqual || applyLastOnVisible)
|
||||
{
|
||||
logger.LogDebug("Updating {object}/{kind} (FileReplacements not equal) => {change}", cachedPlayer, objectKind, PlayerChanges.ModFiles);
|
||||
charaDataToUpdate[objectKind].Add(PlayerChanges.ModFiles);
|
||||
}
|
||||
}
|
||||
|
||||
if (hasNewAndOldGlamourerData)
|
||||
{
|
||||
bool glamourerDataDifferent = !string.Equals(oldData.GlamourerData[objectKind], newData.GlamourerData[objectKind], StringComparison.Ordinal);
|
||||
if (glamourerDataDifferent || forced)
|
||||
{
|
||||
logger.LogDebug("Updating {object}/{kind} (Glamourer different) => {change}", cachedPlayer, objectKind, PlayerChanges.Glamourer);
|
||||
charaDataToUpdate[objectKind].Add(PlayerChanges.Glamourer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (objectKind != ObjectKind.Player) continue;
|
||||
|
||||
bool manipDataDifferent = !string.Equals(oldData.ManipulationData, newData.ManipulationData, StringComparison.Ordinal);
|
||||
if (manipDataDifferent || applyLastOnVisible)
|
||||
{
|
||||
logger.LogDebug("Updating {object}/{kind} (Diff manip data) => {change}", cachedPlayer, objectKind, PlayerChanges.ModManip);
|
||||
charaDataToUpdate[objectKind].Add(PlayerChanges.ModManip);
|
||||
}
|
||||
|
||||
bool heelsOffsetDifferent = oldData.HeelsOffset != newData.HeelsOffset;
|
||||
if (heelsOffsetDifferent || (forced && newData.HeelsOffset != 0))
|
||||
{
|
||||
logger.LogDebug("Updating {object}/{kind} (Diff heels data) => {change}", cachedPlayer, objectKind, PlayerChanges.Heels);
|
||||
charaDataToUpdate[objectKind].Add(PlayerChanges.Heels);
|
||||
}
|
||||
|
||||
bool customizeDataDifferent = !string.Equals(oldData.CustomizePlusData, newData.CustomizePlusData, StringComparison.Ordinal);
|
||||
if (customizeDataDifferent || (forced && !string.IsNullOrEmpty(newData.CustomizePlusData)))
|
||||
{
|
||||
logger.LogDebug("Updating {object}/{kind} (Diff customize data) => {change}", cachedPlayer, objectKind, PlayerChanges.Customize);
|
||||
charaDataToUpdate[objectKind].Add(PlayerChanges.Customize);
|
||||
}
|
||||
|
||||
bool palettePlusDataDifferent = !string.Equals(oldData.PalettePlusData, newData.PalettePlusData, StringComparison.Ordinal);
|
||||
if (palettePlusDataDifferent || (forced && !string.IsNullOrEmpty(newData.PalettePlusData)))
|
||||
{
|
||||
logger.LogDebug("Updating {object}/{kind} (Diff palette data) => {change}", cachedPlayer, objectKind, PlayerChanges.Palette);
|
||||
charaDataToUpdate[objectKind].Add(PlayerChanges.Palette);
|
||||
}
|
||||
|
||||
bool honorificDataDifferent = !string.Equals(oldData.HonorificData, newData.HonorificData, StringComparison.Ordinal);
|
||||
if (honorificDataDifferent || (forced && !string.IsNullOrEmpty(newData.HonorificData)))
|
||||
{
|
||||
logger.LogDebug("Updating {object}/{kind} (Diff honorific data) => {change}", cachedPlayer, objectKind, PlayerChanges.Honorific);
|
||||
charaDataToUpdate[objectKind].Add(PlayerChanges.Honorific);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (KeyValuePair<ObjectKind, HashSet<PlayerChanges>> data in charaDataToUpdate.ToList())
|
||||
{
|
||||
if (!data.Value.Any()) charaDataToUpdate.Remove(data.Key);
|
||||
else charaDataToUpdate[data.Key] = data.Value.OrderByDescending(p => (int)p).ToHashSet();
|
||||
}
|
||||
|
||||
return charaDataToUpdate;
|
||||
}
|
||||
|
||||
public static T DeepClone<T>(this T obj)
|
||||
{
|
||||
return JsonSerializer.Deserialize<T>(JsonSerializer.Serialize(obj))!;
|
||||
@@ -19,23 +142,4 @@ public static class VariousExtensions
|
||||
|
||||
return ((FFXIVClientStructs.FFXIV.Client.Game.Object.GameObject*)gameObject.Address)->ObjectIndex;
|
||||
}
|
||||
|
||||
public static void CancelDispose(this CancellationTokenSource? cts)
|
||||
{
|
||||
try
|
||||
{
|
||||
cts?.Cancel();
|
||||
cts?.Dispose();
|
||||
}
|
||||
catch(ObjectDisposedException)
|
||||
{
|
||||
// swallow it
|
||||
}
|
||||
}
|
||||
|
||||
public static CancellationTokenSource CancelRecreate(this CancellationTokenSource? cts)
|
||||
{
|
||||
cts.CancelDispose();
|
||||
return new CancellationTokenSource();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user