-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f679e0c
commit b3883c1
Showing
9 changed files
with
415 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
Penumbra/Interop/Hooks/Animation/GetCachedScheduleResource.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
Penumbra/Interop/Services/SchedulerResourceManagementService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.