Add pap requested event, some cleanup.

This commit is contained in:
Ottermandias 2024-07-21 22:58:24 +02:00
parent 0db70c89b1
commit ee5a21f7a2
2 changed files with 44 additions and 25 deletions

View file

@ -16,10 +16,10 @@ public unsafe class ResourceLoader : IDisposable, IService
private readonly ResourceService _resources;
private readonly FileReadService _fileReadService;
private readonly TexMdlService _texMdlService;
private readonly PapHandler _papHandler;
private readonly PapHandler _papHandler;
private ResolveData _resolvedData = ResolveData.Invalid;
private ResolveData _resolvedData = ResolveData.Invalid;
public event Action<Utf8GamePath>? PapRequested;
public ResourceLoader(ResourceService resources, FileReadService fileReadService, TexMdlService texMdlService)
{
@ -36,24 +36,28 @@ public unsafe class ResourceLoader : IDisposable, IService
_papHandler = new PapHandler(PapResourceHandler);
_papHandler.Enable();
}
private int PapResourceHandler(void* self, byte* path, int length)
{
Utf8GamePath.FromPointer(path, out var gamePath);
if (!Utf8GamePath.FromPointer(path, out var gamePath))
return length;
var (resolvedPath, _) = _incMode.Value
? (null, ResolveData.Invalid)
: _resolvedData.Valid
? (_resolvedData.ModCollection.ResolvePath(gamePath), _resolvedData)
: ResolvePath(gamePath, ResourceCategory.Chara, ResourceType.Pap);
if (!resolvedPath.HasValue || !Utf8GamePath.FromString(resolvedPath.Value.FullName, out var utf8ResolvedPath))
if (!resolvedPath.HasValue || !Utf8GamePath.FromByteString(resolvedPath.Value.InternalName, out var utf8ResolvedPath))
{
PapRequested?.Invoke(gamePath, gamePath, _resolvedData);
return length;
}
NativeMemory.Copy(utf8ResolvedPath.Path.Path, path, (nuint)utf8ResolvedPath.Length);
path[utf8ResolvedPath.Length] = 0;
PapRequested?.Invoke(gamePath, utf8ResolvedPath, _resolvedData);
return utf8ResolvedPath.Length;
}

View file

@ -36,31 +36,47 @@ public sealed class ResourceWatcher : IDisposable, ITab, IUiService
private Regex? _logRegex;
private int _newMaxEntries;
public unsafe ResourceWatcher(ActorManager actors, Configuration config, ResourceService resources, ResourceLoader loader, ResourceHandleDestructor destructor)
public unsafe ResourceWatcher(ActorManager actors, Configuration config, ResourceService resources, ResourceLoader loader,
ResourceHandleDestructor destructor)
{
_actors = actors;
_config = config;
_ephemeral = config.Ephemeral;
_resources = resources;
_destructor = destructor;
_loader = loader;
_table = new ResourceWatcherTable(config.Ephemeral, _records);
_resources.ResourceRequested += OnResourceRequested;
_actors = actors;
_config = config;
_ephemeral = config.Ephemeral;
_resources = resources;
_destructor = destructor;
_loader = loader;
_table = new ResourceWatcherTable(config.Ephemeral, _records);
_resources.ResourceRequested += OnResourceRequested;
_destructor.Subscribe(OnResourceDestroyed, ResourceHandleDestructor.Priority.ResourceWatcher);
_loader.ResourceLoaded += OnResourceLoaded;
_loader.FileLoaded += OnFileLoaded;
_loader.ResourceLoaded += OnResourceLoaded;
_loader.FileLoaded += OnFileLoaded;
_loader.PapRequested += OnPapRequested;
UpdateFilter(_ephemeral.ResourceLoggingFilter, false);
_newMaxEntries = _config.MaxResourceWatcherRecords;
}
private void OnPapRequested(Utf8GamePath original)
{
if (_ephemeral.EnableResourceLogging && FilterMatch(original.Path, out var match))
Penumbra.Log.Information($"[ResourceLoader] [REQ] {match} was requested asynchronously.");
if (!_ephemeral.EnableResourceWatcher)
return;
var record = Record.CreateRequest(original.Path, false);
if (!_ephemeral.OnlyAddMatchingResources || _table.WouldBeVisible(record))
_newRecords.Enqueue(record);
}
public unsafe void Dispose()
{
Clear();
_records.TrimExcess();
_resources.ResourceRequested -= OnResourceRequested;
_resources.ResourceRequested -= OnResourceRequested;
_destructor.Unsubscribe(OnResourceDestroyed);
_loader.ResourceLoaded -= OnResourceLoaded;
_loader.FileLoaded -= OnFileLoaded;
_loader.ResourceLoaded -= OnResourceLoaded;
_loader.FileLoaded -= OnFileLoaded;
_loader.PapRequested -= OnPapRequested;
}
private void Clear()
@ -200,8 +216,7 @@ public sealed class ResourceWatcher : IDisposable, ITab, IUiService
private unsafe void OnResourceRequested(ref ResourceCategory category, ref ResourceType type, ref int hash, ref Utf8GamePath path,
Utf8GamePath original,
GetResourceParameters* parameters, ref bool sync, ref ResourceHandle* returnValue)
Utf8GamePath original, GetResourceParameters* parameters, ref bool sync, ref ResourceHandle* returnValue)
{
if (_ephemeral.EnableResourceLogging && FilterMatch(original.Path, out var match))
Penumbra.Log.Information($"[ResourceLoader] [REQ] {match} was requested {(sync ? "synchronously." : "asynchronously.")}");