Change handling of tex and shpk files to be simpler.

This commit is contained in:
Ottermandias 2022-03-17 16:43:31 +01:00
parent e6752ade04
commit 0ba0c6d057
2 changed files with 28 additions and 17 deletions

View file

@ -4,6 +4,7 @@ using Dalamud.Hooking;
using Dalamud.Utility.Signatures; using Dalamud.Utility.Signatures;
using Penumbra.GameData.ByteString; using Penumbra.GameData.ByteString;
using Penumbra.Interop.Structs; using Penumbra.Interop.Structs;
using Penumbra.Mods;
namespace Penumbra.Interop.Resolver; namespace Penumbra.Interop.Resolver;
@ -20,7 +21,9 @@ public unsafe partial class PathResolver
private byte LoadMtrlTexDetour( IntPtr mtrlResourceHandle ) private byte LoadMtrlTexDetour( IntPtr mtrlResourceHandle )
{ {
LoadMtrlTexHelper( mtrlResourceHandle ); LoadMtrlTexHelper( mtrlResourceHandle );
return LoadMtrlTexHook!.Original( mtrlResourceHandle ); var ret = LoadMtrlTexHook!.Original( mtrlResourceHandle );
_mtrlCollection = null;
return ret;
} }
[Signature( "48 89 5C 24 ?? 57 48 81 EC ?? ?? ?? ?? 48 8B 05 ?? ?? ?? ?? 48 33 C4 48 89 84 24 ?? ?? ?? ?? 44 0F B7 89", [Signature( "48 89 5C 24 ?? 57 48 81 EC ?? ?? ?? ?? 48 8B 05 ?? ?? ?? ?? 48 33 C4 48 89 84 24 ?? ?? ?? ?? 44 0F B7 89",
@ -30,11 +33,12 @@ public unsafe partial class PathResolver
private byte LoadMtrlShpkDetour( IntPtr mtrlResourceHandle ) private byte LoadMtrlShpkDetour( IntPtr mtrlResourceHandle )
{ {
LoadMtrlShpkHelper( mtrlResourceHandle ); LoadMtrlShpkHelper( mtrlResourceHandle );
return LoadMtrlShpkHook!.Original( mtrlResourceHandle ); var ret = LoadMtrlShpkHook!.Original( mtrlResourceHandle );
_mtrlCollection = null;
return ret;
} }
// Taken from the actual hooked function. The Shader string is just concatenated to this base directory. private ModCollection? _mtrlCollection;
private static readonly Utf8String ShaderBase = Utf8String.FromStringUnsafe( "shader/sm5/shpk", true );
private void LoadMtrlShpkHelper( IntPtr mtrlResourceHandle ) private void LoadMtrlShpkHelper( IntPtr mtrlResourceHandle )
{ {
@ -43,11 +47,9 @@ public unsafe partial class PathResolver
return; return;
} }
var mtrl = ( MtrlResource* )mtrlResourceHandle; var mtrl = ( MtrlResource* )mtrlResourceHandle;
var shpkPath = Utf8String.Join( ( byte )'/', ShaderBase, new Utf8String( mtrl->ShpkString ).AsciiToLower() ); var mtrlPath = Utf8String.FromSpanUnsafe( mtrl->Handle.FileNameSpan(), true, null, true );
var mtrlPath = Utf8String.FromSpanUnsafe( mtrl->Handle.FileNameSpan(), true, null, true ); _mtrlCollection = PathCollections.TryGetValue( mtrlPath, out var c ) ? c : null;
var collection = PathCollections.TryGetValue( mtrlPath, out var c ) ? c : null;
SetCollection( shpkPath, collection );
} }
private void LoadMtrlTexHelper( IntPtr mtrlResourceHandle ) private void LoadMtrlTexHelper( IntPtr mtrlResourceHandle )
@ -63,14 +65,21 @@ public unsafe partial class PathResolver
return; return;
} }
var mtrlPath = Utf8String.FromSpanUnsafe( mtrl->Handle.FileNameSpan(), true, null, true ); var mtrlPath = Utf8String.FromSpanUnsafe( mtrl->Handle.FileNameSpan(), true, null, true );
var collection = PathCollections.TryGetValue( mtrlPath, out var c ) ? c : null; _mtrlCollection = PathCollections.TryGetValue( mtrlPath, out var c ) ? c : null;
var x = PathCollections.ToList(); }
for( var i = 0; i < mtrl->NumTex; ++i )
// Check specifically for shpk and tex files whether we are currently in a material load.
private bool HandleMaterialSubFiles( Utf8GamePath gamePath, out ModCollection? collection )
{
if( _mtrlCollection != null && ( gamePath.Path.EndsWith( 't', 'e', 'x' ) || gamePath.Path.EndsWith( 's', 'h', 'p', 'k' ) ) )
{ {
var texString = new Utf8String( mtrl->TexString( i ) ); collection = _mtrlCollection;
SetCollection( texString, collection ); return true;
} }
collection = null;
return false;
} }
private void EnableMtrlHooks() private void EnableMtrlHooks()

View file

@ -29,8 +29,10 @@ public partial class PathResolver : IDisposable
// The modified resolver that handles game path resolving. // The modified resolver that handles game path resolving.
private (FullPath?, object?) CharacterResolver( Utf8GamePath gamePath ) private (FullPath?, object?) CharacterResolver( Utf8GamePath gamePath )
{ {
// Check if the path was marked for a specific collection, if not use the default collection. // Check if the path was marked for a specific collection,
var nonDefault = PathCollections.TryGetValue( gamePath.Path, out var collection ); // or if it is a file loaded by a material, and if we are currently in a material load.
// If not use the default collection.
var nonDefault = HandleMaterialSubFiles( gamePath, out var collection ) || PathCollections.TryGetValue( gamePath.Path, out collection );
if( !nonDefault ) if( !nonDefault )
{ {
collection = Penumbra.ModManager.Collections.DefaultCollection; collection = Penumbra.ModManager.Collections.DefaultCollection;