From 114ab2f4e1517e75026b73c6d08e1a9febcb4dcb Mon Sep 17 00:00:00 2001 From: Stanley Dimant Date: Mon, 9 Sep 2024 21:06:08 +0200 Subject: [PATCH] rework transient files to work on a per job/global basis --- .../FileCache/TransientResourceManager.cs | 171 ++++++++---------- .../Configurations/TransientConfig.cs | 64 ++++++- .../PlayerData/Factories/PlayerDataFactory.cs | 8 +- MareSynchronos/Services/DalamudUtilService.cs | 10 +- 4 files changed, 154 insertions(+), 99 deletions(-) diff --git a/MareSynchronos/FileCache/TransientResourceManager.cs b/MareSynchronos/FileCache/TransientResourceManager.cs index d71eab6..6de366f 100644 --- a/MareSynchronos/FileCache/TransientResourceManager.cs +++ b/MareSynchronos/FileCache/TransientResourceManager.cs @@ -1,5 +1,6 @@ using MareSynchronos.API.Data.Enum; using MareSynchronos.MareConfiguration; +using MareSynchronos.MareConfiguration.Configurations; using MareSynchronos.PlayerData.Data; using MareSynchronos.PlayerData.Handlers; using MareSynchronos.Services; @@ -11,16 +12,18 @@ namespace MareSynchronos.FileCache; public sealed class TransientResourceManager : DisposableMediatorSubscriberBase { + private readonly object _cacheAdditionLock = new(); private readonly HashSet _cachedHandledPaths = new(StringComparer.Ordinal); private readonly TransientConfigService _configurationService; private readonly DalamudUtilService _dalamudUtil; private readonly string[] _fileTypesToHandle = ["tmb", "pap", "avfx", "atex", "sklb", "eid", "phyb", "scd", "skp", "shpk"]; private readonly HashSet _playerRelatedPointers = []; - private HashSet _cachedFrameAddresses = []; - private readonly object _cacheAdditionLock = new(); + private Dictionary _cachedFrameAddresses = []; + private ConcurrentDictionary>? _semiTransientResources = null; + private uint _lastClassJobId = uint.MaxValue; public TransientResourceManager(ILogger logger, TransientConfigService configurationService, - DalamudUtilService dalamudUtil, MareMediator mediator) : base(logger, mediator) + DalamudUtilService dalamudUtil, MareMediator mediator) : base(logger, mediator) { _configurationService = configurationService; _dalamudUtil = dalamudUtil; @@ -28,13 +31,6 @@ public sealed class TransientResourceManager : DisposableMediatorSubscriberBase Mediator.Subscribe(this, Manager_PenumbraResourceLoadEvent); Mediator.Subscribe(this, (_) => Manager_PenumbraModSettingChanged()); Mediator.Subscribe(this, (_) => DalamudUtil_FrameworkUpdate()); - Mediator.Subscribe(this, (msg) => - { - if (_playerRelatedPointers.Contains(msg.GameObjectHandler)) - { - DalamudUtil_ClassJobChanged(); - } - }); Mediator.Subscribe(this, (msg) => { if (!msg.OwnedObject) return; @@ -47,8 +43,20 @@ public sealed class TransientResourceManager : DisposableMediatorSubscriberBase }); } + private TransientConfig.TransientPlayerConfig PlayerConfig + { + get + { + if (!_configurationService.Current.TransientConfigs.TryGetValue(PlayerPersistentDataKey, out var transientConfig)) + { + _configurationService.Current.TransientConfigs[PlayerPersistentDataKey] = transientConfig = new(); + } + + return transientConfig; + } + } + private string PlayerPersistentDataKey => _dalamudUtil.GetPlayerNameAsync().GetAwaiter().GetResult() + "_" + _dalamudUtil.GetHomeWorldIdAsync().GetAwaiter().GetResult(); - private ConcurrentDictionary>? _semiTransientResources = null; private ConcurrentDictionary> SemiTransientResources { get @@ -56,33 +64,14 @@ public sealed class TransientResourceManager : DisposableMediatorSubscriberBase if (_semiTransientResources == null) { _semiTransientResources = new(); - _semiTransientResources.TryAdd(ObjectKind.Player, new HashSet(StringComparer.Ordinal)); - if (_configurationService.Current.PlayerPersistentTransientCache.TryGetValue(PlayerPersistentDataKey, out var gamePaths)) - { - int restored = 0; - foreach (var gamePath in gamePaths) - { - if (string.IsNullOrEmpty(gamePath)) continue; - - try - { - Logger.LogDebug("Loaded persistent transient resource {path}", gamePath); - SemiTransientResources[ObjectKind.Player].Add(gamePath); - restored++; - } - catch (Exception ex) - { - Logger.LogWarning(ex, "Error during loading persistent transient resource {path}", gamePath); - } - } - Logger.LogDebug("Restored {restored}/{total} semi persistent resources", restored, gamePaths.Count); - } + PlayerConfig.JobSpecificCache.TryGetValue(_dalamudUtil.ClassJobId, out var jobSpecificData); + _semiTransientResources[ObjectKind.Player] = PlayerConfig.GlobalPersistentCache.Concat(jobSpecificData ?? []).ToHashSet(StringComparer.Ordinal); } return _semiTransientResources; } } - private ConcurrentDictionary> TransientResources { get; } = new(); + private ConcurrentDictionary> TransientResources { get; } = new(); public void CleanUpSemiTransientResources(ObjectKind objectKind, List? fileReplacement = null) { @@ -103,50 +92,55 @@ public sealed class TransientResourceManager : DisposableMediatorSubscriberBase public HashSet GetSemiTransientResources(ObjectKind objectKind) { - if (SemiTransientResources.TryGetValue(objectKind, out var result)) - { - return result ?? new HashSet(StringComparer.Ordinal); - } + SemiTransientResources.TryGetValue(objectKind, out var result); - return new HashSet(StringComparer.Ordinal); + return result ?? new HashSet(StringComparer.Ordinal); } - public List GetTransientResources(IntPtr gameObject) + public void PersistTransientResources(ObjectKind objectKind) { - if (TransientResources.TryGetValue(gameObject, out var result)) + if (!SemiTransientResources.TryGetValue(objectKind, out HashSet? semiTransientResources)) { - return [.. result]; + SemiTransientResources[objectKind] = semiTransientResources = new(StringComparer.Ordinal); } - return []; - } - - public void PersistTransientResources(IntPtr gameObject, ObjectKind objectKind) - { - if (!SemiTransientResources.TryGetValue(objectKind, out HashSet? value)) - { - value = new HashSet(StringComparer.Ordinal); - SemiTransientResources[objectKind] = value; - } - - if (!TransientResources.TryGetValue(gameObject, out var resources)) + if (!TransientResources.TryGetValue(objectKind, out var resources)) { return; } var transientResources = resources.ToList(); Logger.LogDebug("Persisting {count} transient resources", transientResources.Count); + List newlyAddedGamePaths = resources.Except(semiTransientResources, StringComparer.Ordinal).ToList(); foreach (var gamePath in transientResources) { - value.Add(gamePath); + semiTransientResources.Add(gamePath); } - if (objectKind == ObjectKind.Player && SemiTransientResources.TryGetValue(ObjectKind.Player, out var fileReplacements)) + if (objectKind == ObjectKind.Player && newlyAddedGamePaths.Any()) { - _configurationService.Current.PlayerPersistentTransientCache[PlayerPersistentDataKey] = fileReplacements.Where(f => !string.IsNullOrEmpty(f)).ToHashSet(StringComparer.Ordinal); + foreach (var item in newlyAddedGamePaths.Where(f => !string.IsNullOrEmpty(f))) + { + PlayerConfig.AddOrElevate(_dalamudUtil.ClassJobId, item); + } + _configurationService.Save(); } - TransientResources[gameObject].Clear(); + + TransientResources[objectKind].Clear(); + } + + public void RemoveTransientResource(ObjectKind objectKind, string path) + { + if (SemiTransientResources.TryGetValue(objectKind, out var resources)) + { + resources.RemoveWhere(f => string.Equals(path, f, StringComparison.Ordinal)); + if (objectKind == ObjectKind.Player) + { + PlayerConfig.RemovePath(path); + _configurationService.Save(); + } + } } internal void AddSemiTransientResource(ObjectKind objectKind, string item) @@ -160,9 +154,9 @@ public sealed class TransientResourceManager : DisposableMediatorSubscriberBase value.Add(item.ToLowerInvariant()); } - internal void ClearTransientPaths(IntPtr ptr, List list) + internal void ClearTransientPaths(ObjectKind objectKind, List list) { - if (TransientResources.TryGetValue(ptr, out var set)) + if (TransientResources.TryGetValue(objectKind, out var set)) { set.RemoveWhere(p => list.Contains(p, StringComparer.OrdinalIgnoreCase)); } @@ -174,32 +168,35 @@ public sealed class TransientResourceManager : DisposableMediatorSubscriberBase TransientResources.Clear(); SemiTransientResources.Clear(); - if (SemiTransientResources.TryGetValue(ObjectKind.Player, out HashSet? value)) - { - _configurationService.Current.PlayerPersistentTransientCache[PlayerPersistentDataKey] = value; - _configurationService.Save(); - } - } - - private void DalamudUtil_ClassJobChanged() - { - if (SemiTransientResources.TryGetValue(ObjectKind.Pet, out HashSet? value)) - { - value?.Clear(); - } } private void DalamudUtil_FrameworkUpdate() { - _cachedFrameAddresses = _playerRelatedPointers.Select(c => c.CurrentAddress()).ToHashSet(); + _cachedFrameAddresses = _playerRelatedPointers.Where(k => k.Address != nint.Zero).ToDictionary(c => c.CurrentAddress(), c => c.ObjectKind); lock (_cacheAdditionLock) { _cachedHandledPaths.Clear(); } - foreach (var item in TransientResources.Where(item => !_dalamudUtil.IsGameObjectPresent(item.Key)).Select(i => i.Key).ToList()) + + if (_lastClassJobId != _dalamudUtil.ClassJobId) { - Logger.LogDebug("Object not present anymore: {addr}", item.ToString("X")); - TransientResources.TryRemove(item, out _); + _lastClassJobId = _dalamudUtil.ClassJobId; + if (SemiTransientResources.TryGetValue(ObjectKind.Pet, out HashSet? value)) + { + value?.Clear(); + } + + // reload config for current new classjob + PlayerConfig.JobSpecificCache.TryGetValue(_dalamudUtil.ClassJobId, out var jobSpecificData); + SemiTransientResources[ObjectKind.Player] = PlayerConfig.GlobalPersistentCache.Concat(jobSpecificData ?? []).ToHashSet(StringComparer.OrdinalIgnoreCase); + } + + foreach (var kind in Enum.GetValues(typeof(ObjectKind))) + { + if (!_cachedFrameAddresses.Any(k => k.Value == (ObjectKind)kind) && TransientResources.Remove((ObjectKind)kind, out _)) + { + Logger.LogDebug("Object not present anymore: {kind}", kind.ToString()); + } } } @@ -252,7 +249,7 @@ public sealed class TransientResourceManager : DisposableMediatorSubscriberBase } // ignore files not belonging to anything player related - if (!_cachedFrameAddresses.Contains(gameObject)) + if (!_cachedFrameAddresses.TryGetValue(gameObject, out var objectKind)) { lock (_cacheAdditionLock) { @@ -261,14 +258,16 @@ public sealed class TransientResourceManager : DisposableMediatorSubscriberBase return; } - if (!TransientResources.TryGetValue(gameObject, out HashSet? value)) + // ^ all of the code above is just to sanitize the data + + if (!TransientResources.TryGetValue(objectKind, out HashSet? value)) { value = new(StringComparer.OrdinalIgnoreCase); - TransientResources[gameObject] = value; + TransientResources[objectKind] = value; } - if (value.Contains(replacedGamePath) || - SemiTransientResources.SelectMany(k => k.Value).Any(f => string.Equals(f, gamePath, StringComparison.OrdinalIgnoreCase))) + if (value.Contains(replacedGamePath) + || SemiTransientResources.SelectMany(k => k.Value).Any(f => string.Equals(f, gamePath, StringComparison.OrdinalIgnoreCase))) { Logger.LogTrace("Not adding {replacedPath} : {filePath}", replacedGamePath, filePath); } @@ -279,14 +278,4 @@ public sealed class TransientResourceManager : DisposableMediatorSubscriberBase Mediator.Publish(new TransientResourceChangedMessage(gameObject)); } } - - internal void RemoveTransientResource(ObjectKind objectKind, string path) - { - if (SemiTransientResources.TryGetValue(objectKind, out var resources)) - { - resources.RemoveWhere(f => string.Equals(path, f, StringComparison.OrdinalIgnoreCase)); - _configurationService.Current.PlayerPersistentTransientCache[PlayerPersistentDataKey] = resources; - _configurationService.Save(); - } - } } \ No newline at end of file diff --git a/MareSynchronos/MareConfiguration/Configurations/TransientConfig.cs b/MareSynchronos/MareConfiguration/Configurations/TransientConfig.cs index 668dc2b..d04c54c 100644 --- a/MareSynchronos/MareConfiguration/Configurations/TransientConfig.cs +++ b/MareSynchronos/MareConfiguration/Configurations/TransientConfig.cs @@ -2,6 +2,68 @@ public class TransientConfig : IMareConfiguration { - public Dictionary> PlayerPersistentTransientCache { get; set; } = new(StringComparer.Ordinal); + public Dictionary TransientConfigs { get; set; } = []; public int Version { get; set; } = 0; + + public class TransientPlayerConfig + { + public List GlobalPersistentCache { get; set; } = []; + public Dictionary> JobSpecificCache { get; set; } = []; + + public TransientPlayerConfig() + { + + } + + private bool ElevateIfNeeded(uint jobId, string gamePath) + { + // check if it's in the job cache of other jobs and elevate if needed + foreach (var kvp in JobSpecificCache) + { + if (kvp.Key == jobId) continue; + + // elevate if the gamepath is included somewhere else + if (kvp.Value.Contains(gamePath, StringComparer.Ordinal)) + { + JobSpecificCache[kvp.Key].Remove(gamePath); + GlobalPersistentCache.Add(gamePath); + return true; + } + } + + return false; + } + + public void RemovePath(string gamePath) + { + GlobalPersistentCache.Remove(gamePath); + foreach (var kvp in JobSpecificCache) + { + kvp.Value.Remove(gamePath); + } + } + + public void AddOrElevate(uint jobId, string gamePath) + { + // check if it's in the global cache, if yes, do nothing + if (GlobalPersistentCache.Contains(gamePath, StringComparer.Ordinal)) + { + return; + } + + if (ElevateIfNeeded(jobId, gamePath)) return; + + // check if the jobid is already in the cache to start + if (!JobSpecificCache.TryGetValue(jobId, out var jobCache)) + { + JobSpecificCache[jobId] = jobCache = new(); + } + + // check if the path is already in the job specific cache + if (!jobCache.Contains(gamePath, StringComparer.Ordinal)) + { + jobCache.Add(gamePath); + } + } + } } diff --git a/MareSynchronos/PlayerData/Factories/PlayerDataFactory.cs b/MareSynchronos/PlayerData/Factories/PlayerDataFactory.cs index 36703b6..f8f8659 100644 --- a/MareSynchronos/PlayerData/Factories/PlayerDataFactory.cs +++ b/MareSynchronos/PlayerData/Factories/PlayerDataFactory.cs @@ -177,10 +177,10 @@ public class PlayerDataFactory _logger.LogDebug("Handling transient update for {obj}", playerRelatedObject); // remove all potentially gathered paths from the transient resource manager that are resolved through static resolving - _transientResourceManager.ClearTransientPaths(charaPointer, previousData.FileReplacements[objectKind].SelectMany(c => c.GamePaths).ToList()); + _transientResourceManager.ClearTransientPaths(objectKind, previousData.FileReplacements[objectKind].SelectMany(c => c.GamePaths).ToList()); // get all remaining paths and resolve them - var transientPaths = ManageSemiTransientData(objectKind, charaPointer); + var transientPaths = ManageSemiTransientData(objectKind); var resolvedTransientPaths = await GetFileReplacementsFromPaths(transientPaths, new HashSet(StringComparer.Ordinal)).ConfigureAwait(false); _logger.LogDebug("== Transient Replacements =="); @@ -348,9 +348,9 @@ public class PlayerDataFactory return resolvedPaths.ToDictionary(k => k.Key, k => k.Value.ToArray(), StringComparer.OrdinalIgnoreCase).AsReadOnly(); } - private HashSet ManageSemiTransientData(ObjectKind objectKind, IntPtr charaPointer) + private HashSet ManageSemiTransientData(ObjectKind objectKind) { - _transientResourceManager.PersistTransientResources(charaPointer, objectKind); + _transientResourceManager.PersistTransientResources(objectKind); HashSet pathsToResolve = new(StringComparer.Ordinal); foreach (var path in _transientResourceManager.GetSemiTransientResources(objectKind).Where(path => !string.IsNullOrEmpty(path))) diff --git a/MareSynchronos/Services/DalamudUtilService.cs b/MareSynchronos/Services/DalamudUtilService.cs index 5d1d47b..1f9cbd5 100644 --- a/MareSynchronos/Services/DalamudUtilService.cs +++ b/MareSynchronos/Services/DalamudUtilService.cs @@ -82,7 +82,7 @@ public class DalamudUtilService : IHostedService, IMediatorSubscriber public bool IsZoning => _condition[ConditionFlag.BetweenAreas] || _condition[ConditionFlag.BetweenAreas51]; public bool IsInCombatOrPerforming { get; private set; } = false; public bool HasModifiedGameFiles => _gameData.HasModifiedGameDataFiles; - + public uint ClassJobId => _classJobId!.Value; public Lazy> WorldData { get; private set; } public MareMediator Mediator { get; } @@ -555,6 +555,12 @@ public class DalamudUtilService : IHostedService, IMediatorSubscriber Mediator.Publish(new ResumeScanMessage(nameof(ConditionFlag.BetweenAreas))); } + var localPlayer = _clientState.LocalPlayer; + if (localPlayer != null) + { + _classJobId = localPlayer.ClassJob.Id; + } + if (!IsInCombatOrPerforming) Mediator.Publish(new FrameworkUpdateMessage()); @@ -563,8 +569,6 @@ public class DalamudUtilService : IHostedService, IMediatorSubscriber if (isNormalFrameworkUpdate) return; - var localPlayer = _clientState.LocalPlayer; - if (localPlayer != null && !IsLoggedIn) { _logger.LogDebug("Logged in");