Skip to content

Commit b3883c1

Browse files
committed
Add handling for cached TMBs.
1 parent f679e0c commit b3883c1

File tree

9 files changed

+415
-40
lines changed

9 files changed

+415
-40
lines changed

Penumbra.GameData

Penumbra/Communication/ResolvedFileChanged.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using OtterGui.Classes;
22
using Penumbra.Collections;
3-
using Penumbra.Mods;
43
using Penumbra.Mods.Editor;
54
using Penumbra.String.Classes;
65

@@ -33,5 +32,8 @@ public enum Priority
3332
{
3433
/// <seealso cref="Api.DalamudSubstitutionProvider.OnResolvedFileChange"/>
3534
DalamudSubstitutionProvider = 0,
35+
36+
/// <seealso cref="Interop.Services.SchedulerResourceManagementService.OnResolvedFileChange"/>
37+
SchedulerResourceManagementService = 0,
3638
}
3739
}

Penumbra/Interop/GameState.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ public ResolveData SetAnimationData(ResolveData data)
4949
public void RestoreAnimationData(ResolveData old)
5050
=> _animationLoadData.Value = old;
5151

52+
public readonly ThreadLocal<bool> InLoadActionTmb = new(() => false);
53+
public readonly ThreadLocal<bool> SkipTmbCache = new(() => false);
54+
5255
#endregion
5356

5457
#region Sound Data
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using FFXIVClientStructs.FFXIV.Client.System.Scheduler.Resource;
2+
using JetBrains.Annotations;
3+
using OtterGui.Services;
4+
using Penumbra.GameData;
5+
using Penumbra.Interop.Structs;
6+
using Penumbra.String;
7+
8+
namespace Penumbra.Interop.Hooks.Animation;
9+
10+
/// <summary> Load a cached TMB resource from SchedulerResourceManagement. </summary>
11+
public sealed unsafe class GetCachedScheduleResource : FastHook<GetCachedScheduleResource.Delegate>
12+
{
13+
private readonly GameState _state;
14+
15+
public GetCachedScheduleResource(HookManager hooks, GameState state)
16+
{
17+
_state = state;
18+
Task = hooks.CreateHook<Delegate>("Get Cached Schedule Resource", Sigs.GetCachedScheduleResource, Detour,
19+
!HookOverrides.Instance.Animation.GetCachedScheduleResource);
20+
}
21+
22+
public delegate SchedulerResource* Delegate(SchedulerResourceManagement* a, ScheduleResourceLoadData* b, byte useMap);
23+
24+
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
25+
private SchedulerResource* Detour(SchedulerResourceManagement* a, ScheduleResourceLoadData* b, byte c)
26+
{
27+
if (_state.SkipTmbCache.Value)
28+
{
29+
Penumbra.Log.Verbose(
30+
$"[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.");
31+
return null;
32+
}
33+
34+
var ret = Task.Result.Original(a, b, c);
35+
Penumbra.Log.Excessive(
36+
$"[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")}).");
37+
return ret;
38+
}
39+
40+
public struct ScheduleResourceLoadData
41+
{
42+
[UsedImplicitly]
43+
public byte* Path;
44+
45+
[UsedImplicitly]
46+
public uint Id;
47+
}
48+
49+
50+
// #TODO: remove when fixed in CS.
51+
public static ResourceHandle* Resource(SchedulerResource* r)
52+
=> ((ResourceHandle**)r)[3];
53+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using FFXIVClientStructs.FFXIV.Client.System.Scheduler.Resource;
2+
using OtterGui.Services;
3+
using Penumbra.GameData;
4+
using Penumbra.Interop.Services;
5+
using Penumbra.String;
6+
7+
namespace Penumbra.Interop.Hooks.Animation;
8+
9+
/// <summary> Load a Action TMB. </summary>
10+
public sealed unsafe class LoadActionTmb : FastHook<LoadActionTmb.Delegate>
11+
{
12+
private readonly GameState _state;
13+
private readonly SchedulerResourceManagementService _scheduler;
14+
15+
public LoadActionTmb(HookManager hooks, GameState state, SchedulerResourceManagementService scheduler)
16+
{
17+
_state = state;
18+
_scheduler = scheduler;
19+
Task = hooks.CreateHook<Delegate>("Load Action TMB", Sigs.LoadActionTmb, Detour, !HookOverrides.Instance.Animation.LoadActionTmb);
20+
}
21+
22+
public delegate SchedulerResource* Delegate(SchedulerResourceManagement* scheduler,
23+
GetCachedScheduleResource.ScheduleResourceLoadData* loadData, nint b, byte c, byte d, byte e);
24+
25+
26+
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
27+
private SchedulerResource* Detour(SchedulerResourceManagement* scheduler, GetCachedScheduleResource.ScheduleResourceLoadData* loadData,
28+
nint b, byte c, byte d, byte e)
29+
{
30+
_state.InLoadActionTmb.Value = true;
31+
SchedulerResource* ret;
32+
if (ShouldSkipCache(loadData))
33+
{
34+
_state.SkipTmbCache.Value = true;
35+
ret = Task.Result.Original(scheduler, loadData, b, c, d, 1);
36+
Penumbra.Log.Verbose(
37+
$"[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")}).");
38+
_state.SkipTmbCache.Value = false;
39+
}
40+
else
41+
{
42+
ret = Task.Result.Original(scheduler, loadData, b, c, d, e);
43+
Penumbra.Log.Excessive(
44+
$"[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")}).");
45+
}
46+
47+
_state.InLoadActionTmb.Value = false;
48+
49+
return ret;
50+
}
51+
52+
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
53+
private bool ShouldSkipCache(GetCachedScheduleResource.ScheduleResourceLoadData* loadData)
54+
=> _scheduler.Contains(loadData->Id);
55+
}

Penumbra/Interop/Hooks/HookSettings.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public struct AnimationHooks
4343
public bool SomeMountAnimation;
4444
public bool SomePapLoad;
4545
public bool SomeParasolAnimation;
46+
public bool GetCachedScheduleResource;
47+
public bool LoadActionTmb;
4648
}
4749

4850
public struct MetaHooks
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using System.Collections.Frozen;
2+
using Dalamud.Plugin.Services;
3+
using Dalamud.Utility.Signatures;
4+
using FFXIVClientStructs.FFXIV.Client.System.Scheduler.Resource;
5+
using Lumina.Excel.Sheets;
6+
using OtterGui.Services;
7+
using Penumbra.Collections;
8+
using Penumbra.Communication;
9+
using Penumbra.GameData;
10+
using Penumbra.Mods.Editor;
11+
using Penumbra.Services;
12+
using Penumbra.String;
13+
using Penumbra.String.Classes;
14+
15+
namespace Penumbra.Interop.Services;
16+
17+
public unsafe class SchedulerResourceManagementService : IService, IDisposable
18+
{
19+
private static readonly CiByteString TmbExtension = new(".tmb"u8, MetaDataComputation.All);
20+
private static readonly CiByteString FolderPrefix = new("chara/action/"u8, MetaDataComputation.All);
21+
22+
private readonly CommunicatorService _communicator;
23+
private readonly FrozenDictionary<CiByteString, uint> _actionTmbs;
24+
25+
private readonly ConcurrentDictionary<uint, CiByteString> _listedTmbIds = [];
26+
27+
public bool Contains(uint tmbId)
28+
=> _listedTmbIds.ContainsKey(tmbId);
29+
30+
public IReadOnlyDictionary<uint, CiByteString> ListedTmbs
31+
=> _listedTmbIds;
32+
33+
public IReadOnlyDictionary<CiByteString, uint> ActionTmbs
34+
=> _actionTmbs;
35+
36+
public SchedulerResourceManagementService(IGameInteropProvider interop, CommunicatorService communicator, IDataManager dataManager)
37+
{
38+
_communicator = communicator;
39+
_actionTmbs = CreateActionTmbs(dataManager);
40+
_communicator.ResolvedFileChanged.Subscribe(OnResolvedFileChange, ResolvedFileChanged.Priority.SchedulerResourceManagementService);
41+
interop.InitializeFromAttributes(this);
42+
}
43+
44+
private void OnResolvedFileChange(ModCollection collection, ResolvedFileChanged.Type type, Utf8GamePath gamePath, FullPath oldPath,
45+
FullPath newPath, IMod? mod)
46+
{
47+
switch (type)
48+
{
49+
case ResolvedFileChanged.Type.Added:
50+
CheckFile(gamePath);
51+
return;
52+
case ResolvedFileChanged.Type.FullRecomputeFinished:
53+
foreach (var path in collection.ResolvedFiles.Keys)
54+
CheckFile(path);
55+
return;
56+
}
57+
}
58+
59+
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
60+
private void CheckFile(Utf8GamePath gamePath)
61+
{
62+
if (!gamePath.Extension().Equals(TmbExtension))
63+
return;
64+
65+
if (!gamePath.Path.StartsWith(FolderPrefix))
66+
return;
67+
68+
var tmb = gamePath.Path.Substring(FolderPrefix.Length, gamePath.Length - FolderPrefix.Length - TmbExtension.Length).Clone();
69+
if (_actionTmbs.TryGetValue(tmb, out var rowId))
70+
_listedTmbIds[rowId] = tmb;
71+
else
72+
Penumbra.Log.Debug($"Action TMB {gamePath} encountered with no corresponding row ID.");
73+
}
74+
75+
[Signature(Sigs.SchedulerResourceManagementInstance, ScanType = ScanType.StaticAddress)]
76+
public readonly SchedulerResourceManagement** Address = null;
77+
78+
public SchedulerResourceManagement* Scheduler
79+
=> *Address;
80+
81+
public void Dispose()
82+
{
83+
_listedTmbIds.Clear();
84+
_communicator.ResolvedFileChanged.Unsubscribe(OnResolvedFileChange);
85+
}
86+
87+
private static FrozenDictionary<CiByteString, uint> CreateActionTmbs(IDataManager dataManager)
88+
{
89+
var sheet = dataManager.GetExcelSheet<ActionTimeline>();
90+
return sheet.Where(row => !row.Key.IsEmpty).DistinctBy(row => row.Key).ToFrozenDictionary(row => new CiByteString(row.Key, MetaDataComputation.All).Clone(), row => row.RowId);
91+
}
92+
}

Penumbra/UI/Tabs/Debug/DebugTab.cs

Lines changed: 75 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -69,38 +69,39 @@ public void DrawDiagnostics()
6969

7070
public class DebugTab : Window, ITab, IUiService
7171
{
72-
private readonly PerformanceTracker _performance;
73-
private readonly Configuration _config;
74-
private readonly CollectionManager _collectionManager;
75-
private readonly ModManager _modManager;
76-
private readonly ValidityChecker _validityChecker;
77-
private readonly HttpApi _httpApi;
78-
private readonly ActorManager _actors;
79-
private readonly StainService _stains;
80-
private readonly GlobalVariablesDrawer _globalVariablesDrawer;
81-
private readonly ResourceManagerService _resourceManager;
82-
private readonly CollectionResolver _collectionResolver;
83-
private readonly DrawObjectState _drawObjectState;
84-
private readonly PathState _pathState;
85-
private readonly SubfileHelper _subfileHelper;
86-
private readonly IdentifiedCollectionCache _identifiedCollectionCache;
87-
private readonly CutsceneService _cutsceneService;
88-
private readonly ModImportManager _modImporter;
89-
private readonly ImportPopup _importPopup;
90-
private readonly FrameworkManager _framework;
91-
private readonly TextureManager _textureManager;
92-
private readonly ShaderReplacementFixer _shaderReplacementFixer;
93-
private readonly RedrawService _redraws;
94-
private readonly DictEmote _emotes;
95-
private readonly Diagnostics _diagnostics;
96-
private readonly ObjectManager _objects;
97-
private readonly IClientState _clientState;
98-
private readonly IDataManager _dataManager;
99-
private readonly IpcTester _ipcTester;
100-
private readonly CrashHandlerPanel _crashHandlerPanel;
101-
private readonly TexHeaderDrawer _texHeaderDrawer;
102-
private readonly HookOverrideDrawer _hookOverrides;
103-
private readonly RsfService _rsfService;
72+
private readonly PerformanceTracker _performance;
73+
private readonly Configuration _config;
74+
private readonly CollectionManager _collectionManager;
75+
private readonly ModManager _modManager;
76+
private readonly ValidityChecker _validityChecker;
77+
private readonly HttpApi _httpApi;
78+
private readonly ActorManager _actors;
79+
private readonly StainService _stains;
80+
private readonly GlobalVariablesDrawer _globalVariablesDrawer;
81+
private readonly ResourceManagerService _resourceManager;
82+
private readonly CollectionResolver _collectionResolver;
83+
private readonly DrawObjectState _drawObjectState;
84+
private readonly PathState _pathState;
85+
private readonly SubfileHelper _subfileHelper;
86+
private readonly IdentifiedCollectionCache _identifiedCollectionCache;
87+
private readonly CutsceneService _cutsceneService;
88+
private readonly ModImportManager _modImporter;
89+
private readonly ImportPopup _importPopup;
90+
private readonly FrameworkManager _framework;
91+
private readonly TextureManager _textureManager;
92+
private readonly ShaderReplacementFixer _shaderReplacementFixer;
93+
private readonly RedrawService _redraws;
94+
private readonly DictEmote _emotes;
95+
private readonly Diagnostics _diagnostics;
96+
private readonly ObjectManager _objects;
97+
private readonly IClientState _clientState;
98+
private readonly IDataManager _dataManager;
99+
private readonly IpcTester _ipcTester;
100+
private readonly CrashHandlerPanel _crashHandlerPanel;
101+
private readonly TexHeaderDrawer _texHeaderDrawer;
102+
private readonly HookOverrideDrawer _hookOverrides;
103+
private readonly RsfService _rsfService;
104+
private readonly SchedulerResourceManagementService _schedulerService;
104105

105106
public DebugTab(PerformanceTracker performance, Configuration config, CollectionManager collectionManager, ObjectManager objects,
106107
IClientState clientState, IDataManager dataManager,
@@ -110,7 +111,8 @@ public DebugTab(PerformanceTracker performance, Configuration config, Collection
110111
CutsceneService cutsceneService, ModImportManager modImporter, ImportPopup importPopup, FrameworkManager framework,
111112
TextureManager textureManager, ShaderReplacementFixer shaderReplacementFixer, RedrawService redraws, DictEmote emotes,
112113
Diagnostics diagnostics, IpcTester ipcTester, CrashHandlerPanel crashHandlerPanel, TexHeaderDrawer texHeaderDrawer,
113-
HookOverrideDrawer hookOverrides, RsfService rsfService, GlobalVariablesDrawer globalVariablesDrawer)
114+
HookOverrideDrawer hookOverrides, RsfService rsfService, GlobalVariablesDrawer globalVariablesDrawer,
115+
SchedulerResourceManagementService schedulerService)
114116
: base("Penumbra Debug Window", ImGuiWindowFlags.NoCollapse)
115117
{
116118
IsOpen = true;
@@ -148,6 +150,7 @@ public DebugTab(PerformanceTracker performance, Configuration config, Collection
148150
_hookOverrides = hookOverrides;
149151
_rsfService = rsfService;
150152
_globalVariablesDrawer = globalVariablesDrawer;
153+
_schedulerService = schedulerService;
151154
_objects = objects;
152155
_clientState = clientState;
153156
_dataManager = dataManager;
@@ -672,6 +675,22 @@ private unsafe void DrawPathResolverDebug()
672675
}
673676
}
674677
}
678+
679+
using (var tmbCache = TreeNode("TMB Cache"))
680+
{
681+
if (tmbCache)
682+
{
683+
using var table = Table("###TmbTable", 2, ImGuiTableFlags.SizingFixedFit);
684+
if (table)
685+
{
686+
foreach (var (id, name) in _schedulerService.ListedTmbs.OrderBy(kvp => kvp.Key))
687+
{
688+
ImUtf8.DrawTableColumn($"{id:D6}");
689+
ImUtf8.DrawTableColumn(name.Span);
690+
}
691+
}
692+
}
693+
}
675694
}
676695

677696
private void DrawData()
@@ -680,6 +699,7 @@ private void DrawData()
680699
return;
681700

682701
DrawEmotes();
702+
DrawActionTmbs();
683703
DrawStainTemplates();
684704
DrawAtch();
685705
}
@@ -739,6 +759,27 @@ private void DrawEmotes()
739759
ImGuiClip.DrawEndDummy(dummy, ImGui.GetTextLineHeightWithSpacing());
740760
}
741761

762+
private void DrawActionTmbs()
763+
{
764+
using var mainTree = TreeNode("Action TMBs");
765+
if (!mainTree)
766+
return;
767+
768+
using var table = Table("##table", 2, ImGuiTableFlags.RowBg | ImGuiTableFlags.ScrollY | ImGuiTableFlags.SizingFixedFit,
769+
new Vector2(-1, 12 * ImGui.GetTextLineHeightWithSpacing()));
770+
if (!table)
771+
return;
772+
773+
var skips = ImGuiClip.GetNecessarySkips(ImGui.GetTextLineHeightWithSpacing());
774+
var dummy = ImGuiClip.ClippedDraw(_schedulerService.ActionTmbs.OrderBy(r => r.Value), skips,
775+
p =>
776+
{
777+
ImUtf8.DrawTableColumn($"{p.Value}");
778+
ImUtf8.DrawTableColumn(p.Key.Span);
779+
});
780+
ImGuiClip.DrawEndDummy(dummy, ImGui.GetTextLineHeightWithSpacing());
781+
}
782+
742783
private void DrawStainTemplates()
743784
{
744785
using var mainTree = TreeNode("Staining Templates");
@@ -1061,7 +1102,7 @@ public static unsafe void DrawCopyableAddress(ReadOnlySpan<byte> label, void* ad
10611102
}
10621103

10631104
ImUtf8.HoverTooltip("Click to copy address to clipboard."u8);
1064-
}
1105+
}
10651106

10661107
public static unsafe void DrawCopyableAddress(ReadOnlySpan<byte> label, nint address)
10671108
=> DrawCopyableAddress(label, (void*)address);

0 commit comments

Comments
 (0)