check nulls remove redundant catches.
This commit is contained in:
@@ -11,6 +11,8 @@ using LightlessSync.Services.Mediator;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.ExceptionServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace LightlessSync.PlayerData.Factories;
|
||||
|
||||
@@ -123,8 +125,10 @@ public class PlayerDataFactory
|
||||
{
|
||||
if (playerPointer == IntPtr.Zero)
|
||||
return true;
|
||||
try
|
||||
{
|
||||
|
||||
if (!IsPointerValid(playerPointer))
|
||||
return true;
|
||||
|
||||
var character = (Character*)playerPointer;
|
||||
if (character == null)
|
||||
return true;
|
||||
@@ -133,12 +137,26 @@ public class PlayerDataFactory
|
||||
if (gameObject == null)
|
||||
return true;
|
||||
|
||||
if (!IsPointerValid((IntPtr)gameObject))
|
||||
return true;
|
||||
|
||||
return gameObject->DrawObject == null;
|
||||
}
|
||||
catch (AccessViolationException)
|
||||
|
||||
private static bool IsPointerValid(IntPtr ptr)
|
||||
{
|
||||
if (ptr == IntPtr.Zero)
|
||||
return false;
|
||||
|
||||
try
|
||||
{
|
||||
_ = Marshal.ReadByte(ptr);
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static bool IsCacheFresh(CacheEntry entry)
|
||||
|
||||
@@ -881,7 +881,7 @@ public class DalamudUtilService : IHostedService, IMediatorSubscriber
|
||||
{
|
||||
// ignore
|
||||
}
|
||||
catch (AccessViolationException ex)
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogWarning(ex, "Error accessing {handler}, object does not exist anymore?", handler);
|
||||
}
|
||||
@@ -953,13 +953,39 @@ public class DalamudUtilService : IHostedService, IMediatorSubscriber
|
||||
});
|
||||
}
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
private static extern bool IsBadReadPtr(IntPtr ptr, UIntPtr size);
|
||||
|
||||
private static bool IsValidPointer(nint ptr, int size = 8)
|
||||
{
|
||||
if (ptr == nint.Zero)
|
||||
return false;
|
||||
|
||||
try
|
||||
{
|
||||
if (!Util.IsWine())
|
||||
{
|
||||
return !IsBadReadPtr(ptr, (UIntPtr)size);
|
||||
}
|
||||
return ptr != nint.Zero && (ptr % IntPtr.Size) == 0;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private unsafe void CheckCharacterForDrawing(nint address, string characterName)
|
||||
{
|
||||
if (address == nint.Zero)
|
||||
return;
|
||||
|
||||
try
|
||||
if (!IsValidPointer(address))
|
||||
{
|
||||
_logger.LogDebug("Invalid pointer for character {name} at {addr}", characterName, address.ToString("X"));
|
||||
return;
|
||||
}
|
||||
|
||||
var gameObj = (GameObject*)address;
|
||||
|
||||
if (gameObj == null)
|
||||
@@ -978,23 +1004,14 @@ public class DalamudUtilService : IHostedService, IMediatorSubscriber
|
||||
bool isDrawing = false;
|
||||
bool isDrawingChanged = false;
|
||||
|
||||
if ((nint)drawObj != IntPtr.Zero)
|
||||
{
|
||||
try
|
||||
if ((nint)drawObj != IntPtr.Zero && IsValidPointer((nint)drawObj))
|
||||
{
|
||||
isDrawing = gameObj->RenderFlags == (VisibilityFlags)0b100000000000;
|
||||
}
|
||||
catch (AccessViolationException)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isDrawing)
|
||||
{
|
||||
try
|
||||
{
|
||||
var charBase = (CharacterBase*)drawObj;
|
||||
if (charBase != null)
|
||||
if (charBase != null && IsValidPointer((nint)charBase))
|
||||
{
|
||||
isDrawing = charBase->HasModelInSlotLoaded != 0;
|
||||
if (!isDrawing)
|
||||
@@ -1020,11 +1037,6 @@ public class DalamudUtilService : IHostedService, IMediatorSubscriber
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (AccessViolationException)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!string.Equals(_lastGlobalBlockPlayer, characterName, StringComparison.Ordinal)
|
||||
@@ -1044,15 +1056,6 @@ public class DalamudUtilService : IHostedService, IMediatorSubscriber
|
||||
|
||||
IsAnythingDrawing |= isDrawing;
|
||||
}
|
||||
catch (AccessViolationException ex)
|
||||
{
|
||||
_logger.LogDebug(ex, "Memory access violation checking character {name} at {addr}", characterName, address.ToString("X"));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Unexpected error checking character {name} at {addr}", characterName, address.ToString("X"));
|
||||
}
|
||||
}
|
||||
|
||||
private void FrameworkOnUpdate(IFramework framework)
|
||||
{
|
||||
@@ -1061,6 +1064,11 @@ public class DalamudUtilService : IHostedService, IMediatorSubscriber
|
||||
|
||||
private unsafe void FrameworkOnUpdateInternal()
|
||||
{
|
||||
if (!_clientState.IsLoggedIn || _objectTable.LocalPlayer == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if ((_objectTable.LocalPlayer?.IsDead ?? false) && _condition[ConditionFlag.BoundByDuty])
|
||||
{
|
||||
return;
|
||||
@@ -1083,8 +1091,6 @@ public class DalamudUtilService : IHostedService, IMediatorSubscriber
|
||||
var descriptorCount = playerDescriptors.Count;
|
||||
|
||||
for (var i = 0; i < descriptorCount; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (i >= playerDescriptors.Count)
|
||||
break;
|
||||
@@ -1092,7 +1098,7 @@ public class DalamudUtilService : IHostedService, IMediatorSubscriber
|
||||
var actor = playerDescriptors[i];
|
||||
|
||||
var playerAddress = actor.Address;
|
||||
if (playerAddress == nint.Zero)
|
||||
if (playerAddress == nint.Zero || !IsValidPointer(playerAddress))
|
||||
continue;
|
||||
|
||||
if (actor.ObjectIndex >= 200)
|
||||
@@ -1106,46 +1112,16 @@ public class DalamudUtilService : IHostedService, IMediatorSubscriber
|
||||
|
||||
if (!IsAnythingDrawing)
|
||||
{
|
||||
try
|
||||
{
|
||||
var gameObj = (GameObject*)playerAddress;
|
||||
|
||||
if (gameObj == null || gameObj->ObjectKind == 0)
|
||||
if (!_objectTable.Any(o => o?.Address == playerAddress))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
string currentName;
|
||||
try
|
||||
{
|
||||
currentName = gameObj->NameString ?? string.Empty;
|
||||
}
|
||||
catch (AccessViolationException)
|
||||
{
|
||||
currentName = string.Empty;
|
||||
}
|
||||
|
||||
var charaName = string.IsNullOrEmpty(currentName) ? actor.Name : currentName;
|
||||
|
||||
CheckCharacterForDrawing(playerAddress, charaName);
|
||||
CheckCharacterForDrawing(playerAddress, actor.Name);
|
||||
|
||||
if (IsAnythingDrawing)
|
||||
break;
|
||||
}
|
||||
catch (AccessViolationException ex)
|
||||
{
|
||||
_logger.LogDebug(ex, "Access violation on GameObject pointer for actor {index} at {addr}", i, playerAddress.ToString("X"));
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (AccessViolationException ex)
|
||||
{
|
||||
_logger.LogDebug(ex, "Access violation processing actor {index} - object likely destroyed", i);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Unexpected error processing actor {index}", i);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user