add semi transient resource handling, wait max 3s for pets to change

This commit is contained in:
Stanley Dimant
2022-09-03 22:06:28 +02:00
parent ce6764cbf8
commit bd947d8f2a
7 changed files with 95 additions and 22 deletions

View File

@@ -1,4 +1,5 @@
using MareSynchronos.Models;
using MareSynchronos.API;
using MareSynchronos.Models;
using MareSynchronos.Utils;
using System;
using System.Collections.Generic;
@@ -17,6 +18,7 @@ namespace MareSynchronos.Managers
public event TransientResourceLoadedEvent? TransientResourceLoaded;
private Dictionary<IntPtr, HashSet<string>> TransientResources { get; } = new();
private Dictionary<ObjectKind, HashSet<string>> SemiTransientResources { get; } = new();
public TransientResourceManager(IpcManager manager, DalamudUtil dalamudUtil)
{
manager.PenumbraResourceLoadEvent += Manager_PenumbraResourceLoadEvent;
@@ -37,6 +39,14 @@ namespace MareSynchronos.Managers
}
}
public void CleanSemiTransientResources(ObjectKind objectKind)
{
if (SemiTransientResources.ContainsKey(objectKind))
{
SemiTransientResources[objectKind].Clear();
}
}
public List<string> GetTransientResources(IntPtr gameObject)
{
if (TransientResources.TryGetValue(gameObject, out var result))
@@ -47,6 +57,16 @@ namespace MareSynchronos.Managers
return new List<string>();
}
public List<string> GetSemiTransientResources(ObjectKind objectKind)
{
if (SemiTransientResources.TryGetValue(objectKind, out var result))
{
return result.ToList();
}
return new List<string>();
}
private void Manager_PenumbraResourceLoadEvent(IntPtr gameObject, string gamePath, string filePath)
{
if (!TransientResources.ContainsKey(gameObject))
@@ -61,7 +81,7 @@ namespace MareSynchronos.Managers
var newPath = filePath.ToLowerInvariant().Replace("\\", "/");
if (filePath != gamePath && !TransientResources[gameObject].Contains(newPath))
if (filePath != gamePath && !TransientResources[gameObject].Contains(newPath) && !SemiTransientResources.Any(r => r.Value.Contains(newPath)))
{
TransientResources[gameObject].Add(newPath);
Logger.Debug($"Adding {filePath.ToLowerInvariant().Replace("\\", "/")} for {gameObject}");
@@ -69,14 +89,36 @@ namespace MareSynchronos.Managers
}
}
public void RemoveTransientResource(IntPtr drawObject, FileReplacement fileReplacement)
public void RemoveTransientResource(IntPtr gameObject, FileReplacement fileReplacement)
{
if (TransientResources.ContainsKey(drawObject))
if (TransientResources.ContainsKey(gameObject))
{
TransientResources[drawObject].RemoveWhere(f => fileReplacement.ResolvedPath == f);
TransientResources[gameObject].RemoveWhere(f => fileReplacement.ResolvedPath == f);
}
}
public void PersistTransientResources(IntPtr gameObject, ObjectKind objectKind)
{
if (!SemiTransientResources.ContainsKey(objectKind))
{
SemiTransientResources[objectKind] = new HashSet<string>();
}
if (!TransientResources.TryGetValue(gameObject, out var resources))
{
return;
}
var transientResources = resources.ToList();
Logger.Debug("Persisting " + transientResources.Count + " transient resources");
foreach (var item in transientResources)
{
SemiTransientResources[objectKind].Add(item);
}
TransientResources[gameObject].Clear();
}
public void Dispose()
{
dalamudUtil.FrameworkUpdate -= DalamudUtil_FrameworkUpdate;