Make SubFiles threadlocal.

This commit is contained in:
Ottermandias 2023-01-14 19:59:13 +01:00
parent ff2b9de93e
commit 27fed7860d

View file

@ -2,6 +2,7 @@ using System;
using System.Collections; using System.Collections;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading;
using Dalamud.Hooking; using Dalamud.Hooking;
using Dalamud.Utility.Signatures; using Dalamud.Utility.Signatures;
using FFXIVClientStructs.FFXIV.Client.System.Resource; using FFXIVClientStructs.FFXIV.Client.System.Resource;
@ -25,8 +26,8 @@ public unsafe partial class PathResolver
{ {
private readonly ResourceLoader _loader; private readonly ResourceLoader _loader;
private ResolveData _mtrlData = ResolveData.Invalid; private readonly ThreadLocal< ResolveData > _mtrlData = new(() => ResolveData.Invalid);
private ResolveData _avfxData = ResolveData.Invalid; private readonly ThreadLocal< ResolveData > _avfxData = new(() => ResolveData.Invalid);
private readonly ConcurrentDictionary< IntPtr, ResolveData > _subFileCollection = new(); private readonly ConcurrentDictionary< IntPtr, ResolveData > _subFileCollection = new();
@ -44,15 +45,15 @@ public unsafe partial class PathResolver
{ {
case ResourceType.Tex: case ResourceType.Tex:
case ResourceType.Shpk: case ResourceType.Shpk:
if( _mtrlData.Valid ) if( _mtrlData.Value.Valid )
{ {
collection = _mtrlData; collection = _mtrlData.Value;
return true; return true;
} }
break; break;
case ResourceType.Atex when _avfxData.Valid: case ResourceType.Atex when _avfxData.Value.Valid:
collection = _avfxData; collection = _avfxData.Value;
return true; return true;
} }
@ -166,9 +167,10 @@ public unsafe partial class PathResolver
private byte LoadMtrlTexDetour( IntPtr mtrlResourceHandle ) private byte LoadMtrlTexDetour( IntPtr mtrlResourceHandle )
{ {
using var performance = Penumbra.Performance.Measure( PerformanceType.LoadTextures ); using var performance = Penumbra.Performance.Measure( PerformanceType.LoadTextures );
_mtrlData = LoadFileHelper( mtrlResourceHandle ); var old = _mtrlData.Value;
_mtrlData.Value = LoadFileHelper( mtrlResourceHandle );
var ret = _loadMtrlTexHook.Original( mtrlResourceHandle ); var ret = _loadMtrlTexHook.Original( mtrlResourceHandle );
_mtrlData = ResolveData.Invalid; _mtrlData.Value = old;
return ret; return ret;
} }
@ -178,9 +180,10 @@ public unsafe partial class PathResolver
private byte LoadMtrlShpkDetour( IntPtr mtrlResourceHandle ) private byte LoadMtrlShpkDetour( IntPtr mtrlResourceHandle )
{ {
using var performance = Penumbra.Performance.Measure( PerformanceType.LoadShaders ); using var performance = Penumbra.Performance.Measure( PerformanceType.LoadShaders );
_mtrlData = LoadFileHelper( mtrlResourceHandle ); var old = _mtrlData.Value;
_mtrlData.Value = LoadFileHelper( mtrlResourceHandle );
var ret = _loadMtrlShpkHook.Original( mtrlResourceHandle ); var ret = _loadMtrlShpkHook.Original( mtrlResourceHandle );
_mtrlData = ResolveData.Invalid; _mtrlData.Value = old;
return ret; return ret;
} }
@ -204,9 +207,10 @@ public unsafe partial class PathResolver
private byte ApricotResourceLoadDetour( IntPtr handle, IntPtr unk1, byte unk2 ) private byte ApricotResourceLoadDetour( IntPtr handle, IntPtr unk1, byte unk2 )
{ {
using var performance = Penumbra.Performance.Measure( PerformanceType.LoadApricotResources ); using var performance = Penumbra.Performance.Measure( PerformanceType.LoadApricotResources );
_avfxData = LoadFileHelper( handle ); var old = _avfxData.Value;
_avfxData.Value = LoadFileHelper( handle );
var ret = _apricotResourceLoadHook.Original( handle, unk1, unk2 ); var ret = _apricotResourceLoadHook.Original( handle, unk1, unk2 );
_avfxData = ResolveData.Invalid; _avfxData.Value = old;
return ret; return ret;
} }
@ -220,9 +224,9 @@ public unsafe partial class PathResolver
=> _subFileCollection.Count; => _subFileCollection.Count;
internal ResolveData MtrlData internal ResolveData MtrlData
=> _mtrlData; => _mtrlData.Value;
internal ResolveData AvfxData internal ResolveData AvfxData
=> _avfxData; => _avfxData.Value;
} }
} }