Skip to content

Commit

Permalink
Circumvent rsf not existing.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ottermandias committed Nov 21, 2024
1 parent 9e72432 commit f2bdaf1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
1 change: 1 addition & 0 deletions Penumbra/Interop/Hooks/HookSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public struct ResourceLoadingHooks
public bool CheckFileState;
public bool TexResourceHandleOnLoad;
public bool LoadMdlFileExtern;
public bool SoundOnLoad;
}

public struct ResourceHooks
Expand Down
27 changes: 23 additions & 4 deletions Penumbra/Interop/Hooks/ResourceLoading/TexMdlService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Penumbra.Interop.Structs;
using Penumbra.String.Classes;
using ResourceHandle = FFXIVClientStructs.FFXIV.Client.System.Resource.Handle.ResourceHandle;
using TextureResourceHandle = Penumbra.Interop.Structs.TextureResourceHandle;

namespace Penumbra.Interop.Hooks.ResourceLoading;

Expand Down Expand Up @@ -52,7 +53,8 @@ public TexMdlScdService(IGameInteropProvider interop)
_loadMdlFileExternHook.Enable();
if (!HookOverrides.Instance.ResourceLoading.TexResourceHandleOnLoad)
_textureOnLoadHook.Enable();
_soundOnLoadHook.Enable();
if (!HookOverrides.Instance.ResourceLoading.SoundOnLoad)
_soundOnLoadHook.Enable();
}

/// <summary> Add CRC64 if the given file is a model or texture file and has an associated path. </summary>
Expand Down Expand Up @@ -80,6 +82,7 @@ public void Dispose()
/// i.e. CRC32 of filename in the lower bytes, CRC32 of parent path in the upper bytes.
/// </summary>
private readonly Dictionary<ulong, ResourceType> _customFileCrc = [];

public IReadOnlyDictionary<ulong, ResourceType> CustomCache
=> _customFileCrc;

Expand All @@ -98,15 +101,31 @@ public IReadOnlyDictionary<ulong, ResourceType> CustomCache

private delegate byte SoundOnLoadDelegate(ResourceHandle* handle, SeFileDescriptor* descriptor, byte unk);

[Signature("48 89 5C 24 ?? 48 89 74 24 ?? 57 48 83 EC 30 8B 79 ?? 48 8B DA 8B D7")]
[Signature(Sigs.LoadScdFileLocal)]
private readonly delegate* unmanaged<ResourceHandle*, SeFileDescriptor*, byte, byte> _loadScdFileLocal = null!;

[Signature("40 56 57 41 54 48 81 EC 90 00 00 00 80 3A 0B 45 0F B6 E0 48 8B F2", DetourName = nameof(OnScdLoadDetour))]
[Signature(Sigs.SoundOnLoad, DetourName = nameof(OnScdLoadDetour))]
private readonly Hook<SoundOnLoadDelegate> _soundOnLoadHook = null!;

[Signature(Sigs.RsfServiceAddress, ScanType = ScanType.StaticAddress)]
private readonly nint* _rsfService = null;

private byte OnScdLoadDetour(ResourceHandle* handle, SeFileDescriptor* descriptor, byte unk)
{
var ret = _soundOnLoadHook.Original(handle, descriptor, unk);
byte ret;
if (*_rsfService == nint.Zero)
{
Penumbra.Log.Debug(
$"Resource load of {handle->FileName} before FFXIV RSF-service was instantiated, workaround by setting pointer.");
*_rsfService = 1;
ret = _soundOnLoadHook.Original(handle, descriptor, unk);
*_rsfService = nint.Zero;
}
else
{
ret = _soundOnLoadHook.Original(handle, descriptor, unk);
}

if (!_scdReturnData.Value)
return ret;

Expand Down

0 comments on commit f2bdaf1

Please sign in to comment.