Fix potential incorrect null checks in FlaxEngine.Objects

The null-conditional operator checks for reference equality of the
Object, but doesn't check the validity of the unmanaged pointer. This
check is corrected in cases where the object was not immediately
returned from the bindings layer and may have been destroyed earlier.
This commit is contained in:
2023-09-25 23:06:14 +03:00
parent ea201b6173
commit 58445f04c4
18 changed files with 42 additions and 35 deletions

View File

@@ -576,8 +576,8 @@ namespace FlaxEditor.CustomEditors.Dedicated
return; return;
for (int j = 0; j < e.Length; j++) for (int j = 0; j < e.Length; j++)
{ {
var t1 = scripts[j]?.TypeName; var t1 = scripts[j] != null ? scripts[j].TypeName : null;
var t2 = e[j]?.TypeName; var t2 = e[j] != null ? e[j].TypeName : null;
if (t1 != t2) if (t1 != t2)
return; return;
} }

View File

@@ -36,7 +36,9 @@ namespace FlaxEditor
public static void OnEditorOptionsChanged(Options.EditorOptions options) public static void OnEditorOptionsChanged(Options.EditorOptions options)
{ {
var param = _highlightMaterial?.GetParameter("Color"); if (!_highlightMaterial)
return;
var param = _highlightMaterial.GetParameter("Color");
if (param != null) if (param != null)
param.Value = options.Visual.HighlightColor; param.Value = options.Visual.HighlightColor;
} }

View File

@@ -33,7 +33,7 @@ namespace FlaxEditor.GUI.Timeline
/// </summary> /// </summary>
public SceneAnimationPlayer Player public SceneAnimationPlayer Player
{ {
get => _player; get => _player != null ? _player : null;
set set
{ {
if (_player == value) if (_player == value)
@@ -268,7 +268,7 @@ namespace FlaxEditor.GUI.Timeline
/// <inheritdoc /> /// <inheritdoc />
public override void OnSeek(int frame) public override void OnSeek(int frame)
{ {
if (_player?.Animation) if (_player != null && _player.Animation)
{ {
_player.Animation.WaitForLoaded(); _player.Animation.WaitForLoaded();
_player.Time = frame / _player.Animation.FramesPerSecond; _player.Time = frame / _player.Animation.FramesPerSecond;

View File

@@ -375,7 +375,7 @@ namespace FlaxEditor.Tools.Foliage
private void OnModified() private void OnModified()
{ {
Editor.Instance.Scene.MarkSceneEdited(_proxy.Foliage?.Scene); Editor.Instance.Scene.MarkSceneEdited(_proxy.Foliage != null ? _proxy.Foliage.Scene : null);
} }
private void OnSelectedFoliageChanged() private void OnSelectedFoliageChanged()

View File

@@ -218,7 +218,7 @@ namespace FlaxEditor.Tools.Foliage
private void OnModified() private void OnModified()
{ {
Editor.Instance.Scene.MarkSceneEdited(_proxy.Foliage?.Scene); Editor.Instance.Scene.MarkSceneEdited(_proxy.Foliage != null ? _proxy.Foliage.Scene : null);
} }
private void OnSelectedFoliageChanged() private void OnSelectedFoliageChanged()

View File

@@ -544,7 +544,7 @@ namespace FlaxEditor.Tools
public override bool IsControllingMouse => IsPainting; public override bool IsControllingMouse => IsPainting;
/// <inheritdoc /> /// <inheritdoc />
public override BoundingSphere FocusBounds => _selectedModel?.Sphere ?? base.FocusBounds; public override BoundingSphere FocusBounds => _selectedModel != null ? _selectedModel.Sphere : base.FocusBounds;
/// <inheritdoc /> /// <inheritdoc />
public override void Update(float dt) public override void Update(float dt)

View File

@@ -144,7 +144,7 @@ namespace FlaxEditor.Actions
{ {
var node = nodeParents[i]; var node = nodeParents[i];
var actor = node.Actor; var actor = node.Actor;
var parent = actor?.Parent; var parent = actor != null ? actor.Parent : null;
if (parent != null) if (parent != null)
{ {
bool IsNameValid(string name) bool IsNameValid(string name)

View File

@@ -68,7 +68,7 @@ namespace FlaxEditor.Viewport.Previews
/// </summary> /// </summary>
public bool ShowBounds public bool ShowBounds
{ {
get => _boundsModel?.IsActive ?? false; get => _boundsModel != null ? _boundsModel.IsActive : false;
set set
{ {
if (value == ShowBounds) if (value == ShowBounds)
@@ -110,7 +110,7 @@ namespace FlaxEditor.Viewport.Previews
/// </summary> /// </summary>
public bool ShowOrigin public bool ShowOrigin
{ {
get => _originModel?.IsActive ?? false; get => _originModel != null ? _originModel.IsActive : false;
set set
{ {
if (value == ShowOrigin) if (value == ShowOrigin)

View File

@@ -500,7 +500,7 @@ namespace FlaxEditor.Viewport.Previews
/// <inheritdoc /> /// <inheritdoc />
protected override void CalculateTextureRect(out Rectangle rect) protected override void CalculateTextureRect(out Rectangle rect)
{ {
CalculateTextureRect(_asset?.Size ?? new Float2(100), Size, out rect); CalculateTextureRect(_asset != null ? _asset.Size : new Float2(100), Size, out rect);
} }
/// <inheritdoc /> /// <inheritdoc />
@@ -549,7 +549,7 @@ namespace FlaxEditor.Viewport.Previews
/// <inheritdoc /> /// <inheritdoc />
protected override void CalculateTextureRect(out Rectangle rect) protected override void CalculateTextureRect(out Rectangle rect)
{ {
CalculateTextureRect(_asset?.Size ?? new Float2(100), Size, out rect); CalculateTextureRect(_asset != null ? _asset.Size : new Float2(100), Size, out rect);
} }
/// <inheritdoc /> /// <inheritdoc />
@@ -604,7 +604,7 @@ namespace FlaxEditor.Viewport.Previews
/// <inheritdoc /> /// <inheritdoc />
protected override void CalculateTextureRect(out Rectangle rect) protected override void CalculateTextureRect(out Rectangle rect)
{ {
CalculateTextureRect(_asset?.Size ?? new Float2(100), Size, out rect); CalculateTextureRect(_asset != null ? _asset.Size : new Float2(100), Size, out rect);
} }
/// <inheritdoc /> /// <inheritdoc />
@@ -659,7 +659,7 @@ namespace FlaxEditor.Viewport.Previews
/// <inheritdoc /> /// <inheritdoc />
protected override void CalculateTextureRect(out Rectangle rect) protected override void CalculateTextureRect(out Rectangle rect)
{ {
CalculateTextureRect(_asset?.Size ?? new Float2(100), Size, out rect); CalculateTextureRect(_asset != null ? _asset.Size : new Float2(100), Size, out rect);
} }
/// <inheritdoc /> /// <inheritdoc />

View File

@@ -69,7 +69,7 @@ namespace FlaxEditor.Windows.Assets
[EditorDisplay("General"), Tooltip("The base material used to override it's properties")] [EditorDisplay("General"), Tooltip("The base material used to override it's properties")]
public MaterialBase BaseMaterial public MaterialBase BaseMaterial
{ {
get => Window?.Asset?.BaseMaterial; get => Window?.Asset != null ? Window?.Asset.BaseMaterial : null;
set set
{ {
var asset = Window?.Asset; var asset = Window?.Asset;
@@ -101,10 +101,12 @@ namespace FlaxEditor.Windows.Assets
[HideInEditor] [HideInEditor]
public object[] Values public object[] Values
{ {
get => Window?.Asset?.Parameters.Select(x => x.Value).ToArray(); get => Window?.Asset != null ? Window?.Asset.Parameters.Select(x => x.Value).ToArray() : null;
set set
{ {
var parameters = Window?.Asset?.Parameters; if (Window?.Asset == null)
return;
var parameters = Window?.Asset.Parameters;
if (value != null && parameters != null) if (value != null && parameters != null)
{ {
if (value.Length != parameters.Length) if (value.Length != parameters.Length)
@@ -131,9 +133,11 @@ namespace FlaxEditor.Windows.Assets
[HideInEditor] [HideInEditor]
public FlaxEngine.Object[] ValuesRef public FlaxEngine.Object[] ValuesRef
{ {
get => Window?.Asset?.Parameters.Select(x => x.Value as FlaxEngine.Object).ToArray(); get => Window?.Asset != null ? Window?.Asset.Parameters.Select(x => x.Value as FlaxEngine.Object).ToArray() : null;
set set
{ {
if (Window?.Asset == null)
return;
var parameters = Window?.Asset?.Parameters; var parameters = Window?.Asset?.Parameters;
if (value != null && parameters != null) if (value != null && parameters != null)
{ {
@@ -293,7 +297,7 @@ namespace FlaxEditor.Windows.Assets
var p = (MaterialParameter)e.Tag; var p = (MaterialParameter)e.Tag;
// Try to get default value (from the base material) // Try to get default value (from the base material)
var pBase = baseMaterial?.GetParameter(p.Name); var pBase = baseMaterial != null ? baseMaterial.GetParameter(p.Name) : null;
if (pBase != null && pBase.ParameterType == p.ParameterType) if (pBase != null && pBase.ParameterType == p.ParameterType)
{ {
valueContainer.SetDefaultValue(pBase.Value); valueContainer.SetDefaultValue(pBase.Value);

View File

@@ -134,7 +134,7 @@ namespace FlaxEditor.Windows.Assets
if (Window._skipEffectsGuiEvents) if (Window._skipEffectsGuiEvents)
return; return;
Window._isolateIndex = mesh?.MaterialSlotIndex ?? -1; Window._isolateIndex = mesh != null ? mesh.MaterialSlotIndex : -1;
Window.UpdateEffectsOnAsset(); Window.UpdateEffectsOnAsset();
UpdateEffectsOnUI(); UpdateEffectsOnUI();
} }
@@ -144,7 +144,7 @@ namespace FlaxEditor.Windows.Assets
if (Window._skipEffectsGuiEvents) if (Window._skipEffectsGuiEvents)
return; return;
Window._highlightIndex = mesh?.MaterialSlotIndex ?? -1; Window._highlightIndex = mesh != null ? mesh.MaterialSlotIndex : -1;
Window.UpdateEffectsOnAsset(); Window.UpdateEffectsOnAsset();
UpdateEffectsOnUI(); UpdateEffectsOnUI();
} }
@@ -326,7 +326,7 @@ namespace FlaxEditor.Windows.Assets
[EditorOrder(10), EditorDisplay("Materials", EditorDisplayAttribute.InlineStyle)] [EditorOrder(10), EditorDisplay("Materials", EditorDisplayAttribute.InlineStyle)]
public MaterialSlot[] MaterialSlots public MaterialSlot[] MaterialSlots
{ {
get => Asset?.MaterialSlots; get => Asset != null ? Asset.MaterialSlots : null;
set set
{ {
if (Asset != null) if (Asset != null)

View File

@@ -187,7 +187,7 @@ namespace FlaxEditor.Windows.Assets
base.Initialize(layout); base.Initialize(layout);
var emitterTrack = Values[0] as EmitterTrackProxy; var emitterTrack = Values[0] as EmitterTrackProxy;
if (emitterTrack?._effect?.Parameters == null) if (emitterTrack?._effect == null || emitterTrack?._effect.Parameters == null)
return; return;
var group = layout.Group("Parameters"); var group = layout.Group("Parameters");

View File

@@ -792,7 +792,7 @@ namespace FlaxEditor.Windows.Assets
{ {
if (_previewButton.Checked) if (_previewButton.Checked)
return; return;
_previewPlayerPicker.Value = _timeline.Player; _previewPlayerPicker.Value = _timeline.Player != null ? _timeline.Player : null;
_cachedPlayerId = _timeline.Player?.ID ?? Guid.Empty; _cachedPlayerId = _timeline.Player?.ID ?? Guid.Empty;
} }

View File

@@ -151,7 +151,7 @@ namespace FlaxEditor.Windows.Assets
if (Window._skipEffectsGuiEvents) if (Window._skipEffectsGuiEvents)
return; return;
Window._isolateIndex = mesh?.MaterialSlotIndex ?? -1; Window._isolateIndex = mesh != null ? mesh.MaterialSlotIndex : -1;
Window.UpdateEffectsOnAsset(); Window.UpdateEffectsOnAsset();
UpdateEffectsOnUI(); UpdateEffectsOnUI();
} }
@@ -165,7 +165,7 @@ namespace FlaxEditor.Windows.Assets
if (Window._skipEffectsGuiEvents) if (Window._skipEffectsGuiEvents)
return; return;
Window._highlightIndex = mesh?.MaterialSlotIndex ?? -1; Window._highlightIndex = mesh != null ? mesh.MaterialSlotIndex : -1;
Window.UpdateEffectsOnAsset(); Window.UpdateEffectsOnAsset();
UpdateEffectsOnUI(); UpdateEffectsOnUI();
} }
@@ -415,7 +415,7 @@ namespace FlaxEditor.Windows.Assets
[EditorOrder(10), EditorDisplay("Materials", EditorDisplayAttribute.InlineStyle)] [EditorOrder(10), EditorDisplay("Materials", EditorDisplayAttribute.InlineStyle)]
public MaterialSlot[] MaterialSlots public MaterialSlot[] MaterialSlots
{ {
get => Asset?.MaterialSlots; get => Asset != null ? Asset.MaterialSlots : null;
set set
{ {
if (Asset != null) if (Asset != null)

View File

@@ -37,7 +37,7 @@ namespace FlaxEngine.GUI
} }
/// <inheritdoc /> /// <inheritdoc />
public Float2 Size => Texture?.Size ?? Float2.Zero; public Float2 Size => Texture != null ? Texture.Size : Float2.Zero;
/// <inheritdoc /> /// <inheritdoc />
public void Draw(Rectangle rect, Color color) public void Draw(Rectangle rect, Color color)

View File

@@ -104,7 +104,7 @@ namespace FlaxEngine.GUI
} }
/// <inheritdoc /> /// <inheritdoc />
public Float2 Size => Texture?.Size ?? Float2.Zero; public Float2 Size => Texture != null ? Texture.Size : Float2.Zero;
/// <inheritdoc /> /// <inheritdoc />
public unsafe void Draw(Rectangle rect, Color color) public unsafe void Draw(Rectangle rect, Color color)

View File

@@ -493,7 +493,8 @@ namespace FlaxEngine
if (_renderer) if (_renderer)
{ {
#if FLAX_EDITOR #if FLAX_EDITOR
_editorTask?.RemoveCustomPostFx(_renderer); if (_editorTask != null)
_editorTask.RemoveCustomPostFx(_renderer);
#endif #endif
SceneRenderTask.RemoveGlobalCustomPostFx(_renderer); SceneRenderTask.RemoveGlobalCustomPostFx(_renderer);
_renderer.Canvas = null; _renderer.Canvas = null;

View File

@@ -204,7 +204,7 @@ namespace FlaxEngine
up = value; up = value;
Internal_SetNavTargets(__unmanagedPtr, GetUnmanagedPtr(up), GetUnmanagedPtr(down), GetUnmanagedPtr(left), GetUnmanagedPtr(right)); Internal_SetNavTargets(__unmanagedPtr, GetUnmanagedPtr(up), GetUnmanagedPtr(down), GetUnmanagedPtr(left), GetUnmanagedPtr(right));
if (_control != null) if (_control != null)
_control.NavTargetUp = value?.Control; _control.NavTargetUp = value != null ? value.Control : null;
} }
} }
@@ -228,7 +228,7 @@ namespace FlaxEngine
down = value; down = value;
Internal_SetNavTargets(__unmanagedPtr, GetUnmanagedPtr(up), GetUnmanagedPtr(down), GetUnmanagedPtr(left), GetUnmanagedPtr(right)); Internal_SetNavTargets(__unmanagedPtr, GetUnmanagedPtr(up), GetUnmanagedPtr(down), GetUnmanagedPtr(left), GetUnmanagedPtr(right));
if (_control != null) if (_control != null)
_control.NavTargetDown = value?.Control; _control.NavTargetDown = value != null ? value.Control : null;
} }
} }
@@ -252,7 +252,7 @@ namespace FlaxEngine
left = value; left = value;
Internal_SetNavTargets(__unmanagedPtr, GetUnmanagedPtr(up), GetUnmanagedPtr(down), GetUnmanagedPtr(left), GetUnmanagedPtr(right)); Internal_SetNavTargets(__unmanagedPtr, GetUnmanagedPtr(up), GetUnmanagedPtr(down), GetUnmanagedPtr(left), GetUnmanagedPtr(right));
if (_control != null) if (_control != null)
_control.NavTargetLeft = value?.Control; _control.NavTargetLeft = value != null ? value.Control : null;
} }
} }
@@ -276,7 +276,7 @@ namespace FlaxEngine
right = value; right = value;
Internal_SetNavTargets(__unmanagedPtr, GetUnmanagedPtr(up), GetUnmanagedPtr(down), GetUnmanagedPtr(left), GetUnmanagedPtr(right)); Internal_SetNavTargets(__unmanagedPtr, GetUnmanagedPtr(up), GetUnmanagedPtr(down), GetUnmanagedPtr(left), GetUnmanagedPtr(right));
if (_control != null) if (_control != null)
_control.NavTargetRight = value?.Control; _control.NavTargetRight = value != null ? value.Control : null;
} }
} }