Skip to content

Commit

Permalink
Add handling for cached TMBs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ottermandias committed Dec 25, 2024
1 parent f679e0c commit b3883c1
Show file tree
Hide file tree
Showing 9 changed files with 415 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Penumbra.GameData
4 changes: 3 additions & 1 deletion Penumbra/Communication/ResolvedFileChanged.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using OtterGui.Classes;
using Penumbra.Collections;
using Penumbra.Mods;
using Penumbra.Mods.Editor;
using Penumbra.String.Classes;

Expand Down Expand Up @@ -33,5 +32,8 @@ public enum Priority
{
/// <seealso cref="Api.DalamudSubstitutionProvider.OnResolvedFileChange"/>
DalamudSubstitutionProvider = 0,

/// <seealso cref="Interop.Services.SchedulerResourceManagementService.OnResolvedFileChange"/>
SchedulerResourceManagementService = 0,
}
}
3 changes: 3 additions & 0 deletions Penumbra/Interop/GameState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public ResolveData SetAnimationData(ResolveData data)
public void RestoreAnimationData(ResolveData old)
=> _animationLoadData.Value = old;

public readonly ThreadLocal<bool> InLoadActionTmb = new(() => false);
public readonly ThreadLocal<bool> SkipTmbCache = new(() => false);

#endregion

#region Sound Data
Expand Down
53 changes: 53 additions & 0 deletions Penumbra/Interop/Hooks/Animation/GetCachedScheduleResource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using FFXIVClientStructs.FFXIV.Client.System.Scheduler.Resource;
using JetBrains.Annotations;
using OtterGui.Services;
using Penumbra.GameData;
using Penumbra.Interop.Structs;
using Penumbra.String;

namespace Penumbra.Interop.Hooks.Animation;

/// <summary> Load a cached TMB resource from SchedulerResourceManagement. </summary>
public sealed unsafe class GetCachedScheduleResource : FastHook<GetCachedScheduleResource.Delegate>
{
private readonly GameState _state;

public GetCachedScheduleResource(HookManager hooks, GameState state)
{
_state = state;
Task = hooks.CreateHook<Delegate>("Get Cached Schedule Resource", Sigs.GetCachedScheduleResource, Detour,
!HookOverrides.Instance.Animation.GetCachedScheduleResource);
}

public delegate SchedulerResource* Delegate(SchedulerResourceManagement* a, ScheduleResourceLoadData* b, byte useMap);

[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
private SchedulerResource* Detour(SchedulerResourceManagement* a, ScheduleResourceLoadData* b, byte c)
{
if (_state.SkipTmbCache.Value)
{
Penumbra.Log.Verbose(
$"[GetCachedScheduleResource] Called with 0x{(ulong)a:X}, {b->Id}, {new CiByteString(b->Path, MetaDataComputation.None)}, {c} from LoadActionTmb with forced skipping of cache, returning NULL.");
return null;
}

var ret = Task.Result.Original(a, b, c);
Penumbra.Log.Excessive(
$"[GetCachedScheduleResource] Called with 0x{(ulong)a:X}, {b->Id}, {new CiByteString(b->Path, MetaDataComputation.None)}, {c}, returning 0x{(ulong)ret:X} ({(ret != null && Resource(ret) != null ? Resource(ret)->FileName().ToString() : "No Path")}).");
return ret;
}

public struct ScheduleResourceLoadData
{
[UsedImplicitly]
public byte* Path;

[UsedImplicitly]
public uint Id;
}


// #TODO: remove when fixed in CS.
public static ResourceHandle* Resource(SchedulerResource* r)
=> ((ResourceHandle**)r)[3];
}
55 changes: 55 additions & 0 deletions Penumbra/Interop/Hooks/Animation/LoadActionTmb.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using FFXIVClientStructs.FFXIV.Client.System.Scheduler.Resource;
using OtterGui.Services;
using Penumbra.GameData;
using Penumbra.Interop.Services;
using Penumbra.String;

namespace Penumbra.Interop.Hooks.Animation;

/// <summary> Load a Action TMB. </summary>
public sealed unsafe class LoadActionTmb : FastHook<LoadActionTmb.Delegate>
{
private readonly GameState _state;
private readonly SchedulerResourceManagementService _scheduler;

public LoadActionTmb(HookManager hooks, GameState state, SchedulerResourceManagementService scheduler)
{
_state = state;
_scheduler = scheduler;
Task = hooks.CreateHook<Delegate>("Load Action TMB", Sigs.LoadActionTmb, Detour, !HookOverrides.Instance.Animation.LoadActionTmb);
}

public delegate SchedulerResource* Delegate(SchedulerResourceManagement* scheduler,
GetCachedScheduleResource.ScheduleResourceLoadData* loadData, nint b, byte c, byte d, byte e);


[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
private SchedulerResource* Detour(SchedulerResourceManagement* scheduler, GetCachedScheduleResource.ScheduleResourceLoadData* loadData,
nint b, byte c, byte d, byte e)
{
_state.InLoadActionTmb.Value = true;
SchedulerResource* ret;
if (ShouldSkipCache(loadData))
{
_state.SkipTmbCache.Value = true;
ret = Task.Result.Original(scheduler, loadData, b, c, d, 1);
Penumbra.Log.Verbose(
$"[LoadActionTMB] Called with 0x{(ulong)scheduler:X}, {loadData->Id}, {new CiByteString(loadData->Path, MetaDataComputation.None)}, 0x{b:X}, {c}, {d}, {e}, forced no-cache use, returned 0x{(ulong)ret:X} ({(ret != null && GetCachedScheduleResource.Resource(ret) != null ? GetCachedScheduleResource.Resource(ret)->FileName().ToString() : "No Path")}).");
_state.SkipTmbCache.Value = false;
}
else
{
ret = Task.Result.Original(scheduler, loadData, b, c, d, e);
Penumbra.Log.Excessive(
$"[LoadActionTMB] Called with 0x{(ulong)scheduler:X}, {loadData->Id}, {new CiByteString(loadData->Path)}, 0x{b:X}, {c}, {d}, {e}, returned 0x{(ulong)ret:X} ({(ret != null && GetCachedScheduleResource.Resource(ret) != null ? GetCachedScheduleResource.Resource(ret)->FileName().ToString() : "No Path")}).");
}

_state.InLoadActionTmb.Value = false;

return ret;
}

[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
private bool ShouldSkipCache(GetCachedScheduleResource.ScheduleResourceLoadData* loadData)
=> _scheduler.Contains(loadData->Id);
}
2 changes: 2 additions & 0 deletions Penumbra/Interop/Hooks/HookSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public struct AnimationHooks
public bool SomeMountAnimation;
public bool SomePapLoad;
public bool SomeParasolAnimation;
public bool GetCachedScheduleResource;
public bool LoadActionTmb;
}

public struct MetaHooks
Expand Down
92 changes: 92 additions & 0 deletions Penumbra/Interop/Services/SchedulerResourceManagementService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System.Collections.Frozen;
using Dalamud.Plugin.Services;
using Dalamud.Utility.Signatures;
using FFXIVClientStructs.FFXIV.Client.System.Scheduler.Resource;
using Lumina.Excel.Sheets;
using OtterGui.Services;
using Penumbra.Collections;
using Penumbra.Communication;
using Penumbra.GameData;
using Penumbra.Mods.Editor;
using Penumbra.Services;
using Penumbra.String;
using Penumbra.String.Classes;

namespace Penumbra.Interop.Services;

public unsafe class SchedulerResourceManagementService : IService, IDisposable
{
private static readonly CiByteString TmbExtension = new(".tmb"u8, MetaDataComputation.All);
private static readonly CiByteString FolderPrefix = new("chara/action/"u8, MetaDataComputation.All);

private readonly CommunicatorService _communicator;
private readonly FrozenDictionary<CiByteString, uint> _actionTmbs;

private readonly ConcurrentDictionary<uint, CiByteString> _listedTmbIds = [];

public bool Contains(uint tmbId)
=> _listedTmbIds.ContainsKey(tmbId);

public IReadOnlyDictionary<uint, CiByteString> ListedTmbs
=> _listedTmbIds;

public IReadOnlyDictionary<CiByteString, uint> ActionTmbs
=> _actionTmbs;

public SchedulerResourceManagementService(IGameInteropProvider interop, CommunicatorService communicator, IDataManager dataManager)
{
_communicator = communicator;
_actionTmbs = CreateActionTmbs(dataManager);
_communicator.ResolvedFileChanged.Subscribe(OnResolvedFileChange, ResolvedFileChanged.Priority.SchedulerResourceManagementService);
interop.InitializeFromAttributes(this);
}

private void OnResolvedFileChange(ModCollection collection, ResolvedFileChanged.Type type, Utf8GamePath gamePath, FullPath oldPath,
FullPath newPath, IMod? mod)
{
switch (type)
{
case ResolvedFileChanged.Type.Added:
CheckFile(gamePath);
return;
case ResolvedFileChanged.Type.FullRecomputeFinished:
foreach (var path in collection.ResolvedFiles.Keys)
CheckFile(path);
return;
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
private void CheckFile(Utf8GamePath gamePath)
{
if (!gamePath.Extension().Equals(TmbExtension))
return;

if (!gamePath.Path.StartsWith(FolderPrefix))
return;

var tmb = gamePath.Path.Substring(FolderPrefix.Length, gamePath.Length - FolderPrefix.Length - TmbExtension.Length).Clone();
if (_actionTmbs.TryGetValue(tmb, out var rowId))
_listedTmbIds[rowId] = tmb;
else
Penumbra.Log.Debug($"Action TMB {gamePath} encountered with no corresponding row ID.");
}

[Signature(Sigs.SchedulerResourceManagementInstance, ScanType = ScanType.StaticAddress)]
public readonly SchedulerResourceManagement** Address = null;

public SchedulerResourceManagement* Scheduler
=> *Address;

public void Dispose()
{
_listedTmbIds.Clear();
_communicator.ResolvedFileChanged.Unsubscribe(OnResolvedFileChange);
}

private static FrozenDictionary<CiByteString, uint> CreateActionTmbs(IDataManager dataManager)
{
var sheet = dataManager.GetExcelSheet<ActionTimeline>();
return sheet.Where(row => !row.Key.IsEmpty).DistinctBy(row => row.Key).ToFrozenDictionary(row => new CiByteString(row.Key, MetaDataComputation.All).Clone(), row => row.RowId);
}
}
109 changes: 75 additions & 34 deletions Penumbra/UI/Tabs/Debug/DebugTab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,38 +69,39 @@ public void DrawDiagnostics()

public class DebugTab : Window, ITab, IUiService
{
private readonly PerformanceTracker _performance;
private readonly Configuration _config;
private readonly CollectionManager _collectionManager;
private readonly ModManager _modManager;
private readonly ValidityChecker _validityChecker;
private readonly HttpApi _httpApi;
private readonly ActorManager _actors;
private readonly StainService _stains;
private readonly GlobalVariablesDrawer _globalVariablesDrawer;
private readonly ResourceManagerService _resourceManager;
private readonly CollectionResolver _collectionResolver;
private readonly DrawObjectState _drawObjectState;
private readonly PathState _pathState;
private readonly SubfileHelper _subfileHelper;
private readonly IdentifiedCollectionCache _identifiedCollectionCache;
private readonly CutsceneService _cutsceneService;
private readonly ModImportManager _modImporter;
private readonly ImportPopup _importPopup;
private readonly FrameworkManager _framework;
private readonly TextureManager _textureManager;
private readonly ShaderReplacementFixer _shaderReplacementFixer;
private readonly RedrawService _redraws;
private readonly DictEmote _emotes;
private readonly Diagnostics _diagnostics;
private readonly ObjectManager _objects;
private readonly IClientState _clientState;
private readonly IDataManager _dataManager;
private readonly IpcTester _ipcTester;
private readonly CrashHandlerPanel _crashHandlerPanel;
private readonly TexHeaderDrawer _texHeaderDrawer;
private readonly HookOverrideDrawer _hookOverrides;
private readonly RsfService _rsfService;
private readonly PerformanceTracker _performance;
private readonly Configuration _config;
private readonly CollectionManager _collectionManager;
private readonly ModManager _modManager;
private readonly ValidityChecker _validityChecker;
private readonly HttpApi _httpApi;
private readonly ActorManager _actors;
private readonly StainService _stains;
private readonly GlobalVariablesDrawer _globalVariablesDrawer;
private readonly ResourceManagerService _resourceManager;
private readonly CollectionResolver _collectionResolver;
private readonly DrawObjectState _drawObjectState;
private readonly PathState _pathState;
private readonly SubfileHelper _subfileHelper;
private readonly IdentifiedCollectionCache _identifiedCollectionCache;
private readonly CutsceneService _cutsceneService;
private readonly ModImportManager _modImporter;
private readonly ImportPopup _importPopup;
private readonly FrameworkManager _framework;
private readonly TextureManager _textureManager;
private readonly ShaderReplacementFixer _shaderReplacementFixer;
private readonly RedrawService _redraws;
private readonly DictEmote _emotes;
private readonly Diagnostics _diagnostics;
private readonly ObjectManager _objects;
private readonly IClientState _clientState;
private readonly IDataManager _dataManager;
private readonly IpcTester _ipcTester;
private readonly CrashHandlerPanel _crashHandlerPanel;
private readonly TexHeaderDrawer _texHeaderDrawer;
private readonly HookOverrideDrawer _hookOverrides;
private readonly RsfService _rsfService;
private readonly SchedulerResourceManagementService _schedulerService;

public DebugTab(PerformanceTracker performance, Configuration config, CollectionManager collectionManager, ObjectManager objects,
IClientState clientState, IDataManager dataManager,
Expand All @@ -110,7 +111,8 @@ public DebugTab(PerformanceTracker performance, Configuration config, Collection
CutsceneService cutsceneService, ModImportManager modImporter, ImportPopup importPopup, FrameworkManager framework,
TextureManager textureManager, ShaderReplacementFixer shaderReplacementFixer, RedrawService redraws, DictEmote emotes,
Diagnostics diagnostics, IpcTester ipcTester, CrashHandlerPanel crashHandlerPanel, TexHeaderDrawer texHeaderDrawer,
HookOverrideDrawer hookOverrides, RsfService rsfService, GlobalVariablesDrawer globalVariablesDrawer)
HookOverrideDrawer hookOverrides, RsfService rsfService, GlobalVariablesDrawer globalVariablesDrawer,
SchedulerResourceManagementService schedulerService)
: base("Penumbra Debug Window", ImGuiWindowFlags.NoCollapse)
{
IsOpen = true;
Expand Down Expand Up @@ -148,6 +150,7 @@ public DebugTab(PerformanceTracker performance, Configuration config, Collection
_hookOverrides = hookOverrides;
_rsfService = rsfService;
_globalVariablesDrawer = globalVariablesDrawer;
_schedulerService = schedulerService;
_objects = objects;
_clientState = clientState;
_dataManager = dataManager;
Expand Down Expand Up @@ -672,6 +675,22 @@ private unsafe void DrawPathResolverDebug()
}
}
}

using (var tmbCache = TreeNode("TMB Cache"))
{
if (tmbCache)
{
using var table = Table("###TmbTable", 2, ImGuiTableFlags.SizingFixedFit);
if (table)
{
foreach (var (id, name) in _schedulerService.ListedTmbs.OrderBy(kvp => kvp.Key))
{
ImUtf8.DrawTableColumn($"{id:D6}");
ImUtf8.DrawTableColumn(name.Span);
}
}
}
}
}

private void DrawData()
Expand All @@ -680,6 +699,7 @@ private void DrawData()
return;

DrawEmotes();
DrawActionTmbs();
DrawStainTemplates();
DrawAtch();
}
Expand Down Expand Up @@ -739,6 +759,27 @@ private void DrawEmotes()
ImGuiClip.DrawEndDummy(dummy, ImGui.GetTextLineHeightWithSpacing());
}

private void DrawActionTmbs()
{
using var mainTree = TreeNode("Action TMBs");
if (!mainTree)
return;

using var table = Table("##table", 2, ImGuiTableFlags.RowBg | ImGuiTableFlags.ScrollY | ImGuiTableFlags.SizingFixedFit,
new Vector2(-1, 12 * ImGui.GetTextLineHeightWithSpacing()));
if (!table)
return;

var skips = ImGuiClip.GetNecessarySkips(ImGui.GetTextLineHeightWithSpacing());
var dummy = ImGuiClip.ClippedDraw(_schedulerService.ActionTmbs.OrderBy(r => r.Value), skips,
p =>
{
ImUtf8.DrawTableColumn($"{p.Value}");
ImUtf8.DrawTableColumn(p.Key.Span);
});
ImGuiClip.DrawEndDummy(dummy, ImGui.GetTextLineHeightWithSpacing());
}

private void DrawStainTemplates()
{
using var mainTree = TreeNode("Staining Templates");
Expand Down Expand Up @@ -1061,7 +1102,7 @@ public static unsafe void DrawCopyableAddress(ReadOnlySpan<byte> label, void* ad
}

ImUtf8.HoverTooltip("Click to copy address to clipboard."u8);
}
}

public static unsafe void DrawCopyableAddress(ReadOnlySpan<byte> label, nint address)
=> DrawCopyableAddress(label, (void*)address);
Expand Down
Loading

0 comments on commit b3883c1

Please sign in to comment.