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