Temporary fix for E4S crashes.

This commit is contained in:
Ottermandias 2022-01-03 11:14:41 +01:00
parent 0823423eda
commit 59fa4c4fe4
2 changed files with 343 additions and 298 deletions

View file

@ -5,6 +5,7 @@ using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Dalamud.Hooking; using Dalamud.Hooking;
using Dalamud.Logging; using Dalamud.Logging;
using Lumina.Excel.GeneratedSheets;
using Penumbra.GameData.Util; using Penumbra.GameData.Util;
using Penumbra.Mods; using Penumbra.Mods;
using Penumbra.Structs; using Penumbra.Structs;
@ -18,6 +19,7 @@ public class ResourceLoader : IDisposable
public Penumbra Penumbra { get; set; } public Penumbra Penumbra { get; set; }
public bool IsEnabled { get; set; } public bool IsEnabled { get; set; }
public bool HacksEnabled { get; set; }
public Crc32 Crc32 { get; } public Crc32 Crc32 { get; }
@ -126,6 +128,29 @@ public class ResourceLoader : IDisposable
LoadMdlFileExternHook = new Hook< LoadMdlFileExternPrototype >( loadMdlFileExternAddress, LoadMdlFileExternDetour ); LoadMdlFileExternHook = new Hook< LoadMdlFileExternPrototype >( loadMdlFileExternAddress, LoadMdlFileExternDetour );
} }
private bool CheckForTerritory()
{
var territory = Dalamud.GameData.GetExcelSheet< TerritoryType >()?.GetRow( Dalamud.ClientState.TerritoryType );
var bad = territory?.Unknown40 ?? false;
switch( bad )
{
case true when HacksEnabled:
CheckFileStateHook?.Disable();
LoadTexFileExternHook?.Disable();
LoadMdlFileExternHook?.Disable();
HacksEnabled = false;
return bad;
case false when Penumbra.Config.IsEnabled && !HacksEnabled:
CheckFileStateHook?.Enable();
LoadTexFileExternHook?.Enable();
LoadMdlFileExternHook?.Enable();
HacksEnabled = true;
break;
}
return bad;
}
private static bool CheckFileStateDetour( IntPtr _, ulong _2 ) private static bool CheckFileStateDetour( IntPtr _, ulong _2 )
=> true; => true;
@ -198,6 +223,11 @@ public class ResourceLoader : IDisposable
bool isUnknown bool isUnknown
) )
{ {
if( CheckForTerritory() )
{
return CallOriginalHandler( isSync, pFileManager, pCategoryId, pResourceType, pResourceHash, pPath, pUnknown, isUnknown );
}
string file; string file;
var modManager = Service< ModManager >.Get(); var modManager = Service< ModManager >.Get();
@ -247,6 +277,11 @@ public class ResourceLoader : IDisposable
private unsafe byte ReadSqpackHandler( IntPtr pFileHandler, SeFileDescriptor* pFileDesc, int priority, bool isSync ) private unsafe byte ReadSqpackHandler( IntPtr pFileHandler, SeFileDescriptor* pFileDesc, int priority, bool isSync )
{ {
if( CheckForTerritory() )
{
return ReadSqpackHook?.Original( pFileHandler, pFileDesc, priority, isSync ) ?? 0;
}
if( ReadFile == null || pFileDesc == null || pFileDesc->ResourceHandle == null ) if( ReadFile == null || pFileDesc == null || pFileDesc->ResourceHandle == null )
{ {
PluginLog.Error( "THIS SHOULD NOT HAPPEN" ); PluginLog.Error( "THIS SHOULD NOT HAPPEN" );
@ -286,7 +321,12 @@ public class ResourceLoader : IDisposable
return; return;
} }
if( ReadSqpackHook == null || GetResourceSyncHook == null || GetResourceAsyncHook == null || CheckFileStateHook == null || LoadTexFileExternHook == null || LoadMdlFileExternHook == null) if( ReadSqpackHook == null
|| GetResourceSyncHook == null
|| GetResourceAsyncHook == null
|| CheckFileStateHook == null
|| LoadTexFileExternHook == null
|| LoadMdlFileExternHook == null )
{ {
PluginLog.Error( "[GetResourceHandler] Could not activate hooks because at least one was not set." ); PluginLog.Error( "[GetResourceHandler] Could not activate hooks because at least one was not set." );
return; return;
@ -300,6 +340,7 @@ public class ResourceLoader : IDisposable
LoadMdlFileExternHook.Enable(); LoadMdlFileExternHook.Enable();
IsEnabled = true; IsEnabled = true;
HacksEnabled = true;
} }
public void Disable() public void Disable()
@ -316,6 +357,7 @@ public class ResourceLoader : IDisposable
LoadTexFileExternHook?.Disable(); LoadTexFileExternHook?.Disable();
LoadMdlFileExternHook?.Disable(); LoadMdlFileExternHook?.Disable();
IsEnabled = false; IsEnabled = false;
HacksEnabled = false;
} }
public void Dispose() public void Dispose()

View file

@ -17,10 +17,10 @@ using Penumbra.Mods;
using Penumbra.UI.Custom; using Penumbra.UI.Custom;
using Penumbra.Util; using Penumbra.Util;
namespace Penumbra.UI namespace Penumbra.UI;
public partial class SettingsInterface
{ {
public partial class SettingsInterface
{
private static void DrawDebugTabPlayers() private static void DrawDebugTabPlayers()
{ {
if( !ImGui.CollapsingHeader( "Players##Debug" ) ) if( !ImGui.CollapsingHeader( "Players##Debug" ) )
@ -29,7 +29,7 @@ namespace Penumbra.UI
} }
var players = Penumbra.PlayerWatcher.WatchedPlayers().ToArray(); var players = Penumbra.PlayerWatcher.WatchedPlayers().ToArray();
var count = players.Sum( s => Math.Max(1, s.Item2.Length) ); var count = players.Sum( s => Math.Max( 1, s.Item2.Length ) );
if( count == 0 ) if( count == 0 )
{ {
return; return;
@ -128,7 +128,7 @@ namespace Penumbra.UI
ImGui.Text( value ); ImGui.Text( value );
} }
private static void DrawDebugTabGeneral() private void DrawDebugTabGeneral()
{ {
if( !ImGui.CollapsingHeader( "General##Debug" ) ) if( !ImGui.CollapsingHeader( "General##Debug" ) )
{ {
@ -145,7 +145,7 @@ namespace Penumbra.UI
var manager = Service< ModManager >.Get(); var manager = Service< ModManager >.Get();
PrintValue( "Active Collection", manager.Collections.ActiveCollection.Name ); PrintValue( "Active Collection", manager.Collections.ActiveCollection.Name );
PrintValue( " has Cache", (manager.Collections.ActiveCollection.Cache != null).ToString() ); PrintValue( " has Cache", ( manager.Collections.ActiveCollection.Cache != null ).ToString() );
PrintValue( "Current Collection", manager.Collections.CurrentCollection.Name ); PrintValue( "Current Collection", manager.Collections.CurrentCollection.Name );
PrintValue( " has Cache", ( manager.Collections.CurrentCollection.Cache != null ).ToString() ); PrintValue( " has Cache", ( manager.Collections.CurrentCollection.Cache != null ).ToString() );
PrintValue( "Default Collection", manager.Collections.DefaultCollection.Name ); PrintValue( "Default Collection", manager.Collections.DefaultCollection.Name );
@ -155,13 +155,17 @@ namespace Penumbra.UI
PrintValue( "Mod Manager BasePath", manager.BasePath?.Name ?? "NULL" ); PrintValue( "Mod Manager BasePath", manager.BasePath?.Name ?? "NULL" );
PrintValue( "Mod Manager BasePath-Full", manager.BasePath?.FullName ?? "NULL" ); PrintValue( "Mod Manager BasePath-Full", manager.BasePath?.FullName ?? "NULL" );
PrintValue( "Mod Manager BasePath IsRooted", Path.IsPathRooted( Penumbra.Config.ModDirectory ).ToString() ); PrintValue( "Mod Manager BasePath IsRooted", Path.IsPathRooted( Penumbra.Config.ModDirectory ).ToString() );
PrintValue( "Mod Manager BasePath Exists", manager.BasePath != null ? Directory.Exists( manager.BasePath.FullName ).ToString() : false.ToString() ); PrintValue( "Mod Manager BasePath Exists",
manager.BasePath != null ? Directory.Exists( manager.BasePath.FullName ).ToString() : false.ToString() );
PrintValue( "Mod Manager Valid", manager.Valid.ToString() ); PrintValue( "Mod Manager Valid", manager.Valid.ToString() );
PrintValue( "Mod Manager Temp Path", manager.TempPath?.FullName ?? "NULL" ); PrintValue( "Mod Manager Temp Path", manager.TempPath?.FullName ?? "NULL" );
PrintValue( "Mod Manager Temp Path IsRooted", PrintValue( "Mod Manager Temp Path IsRooted",
( !Penumbra.Config.TempDirectory.Any() || Path.IsPathRooted( Penumbra.Config.TempDirectory ) ).ToString() ); ( !Penumbra.Config.TempDirectory.Any() || Path.IsPathRooted( Penumbra.Config.TempDirectory ) ).ToString() );
PrintValue( "Mod Manager Temp Path Exists", manager.TempPath != null ? Directory.Exists( manager.TempPath.FullName ).ToString() : false.ToString() ); PrintValue( "Mod Manager Temp Path Exists",
manager.TempPath != null ? Directory.Exists( manager.TempPath.FullName ).ToString() : false.ToString() );
PrintValue( "Mod Manager Temp Path IsWritable", manager.TempWritable.ToString() ); PrintValue( "Mod Manager Temp Path IsWritable", manager.TempWritable.ToString() );
PrintValue( "Resource Loader Enabled", _penumbra.ResourceLoader.IsEnabled.ToString() );
PrintValue( "Resource Loader Hacks Enabled", _penumbra.ResourceLoader.HacksEnabled.ToString() );
} }
private void DrawDebugTabRedraw() private void DrawDebugTabRedraw()
@ -405,5 +409,4 @@ namespace Penumbra.UI
DrawDebugTabIpc(); DrawDebugTabIpc();
ImGui.NewLine(); ImGui.NewLine();
} }
}
} }