Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Modules] Fix null #2671

Merged
merged 6 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions Exiled.API/Features/Core/EObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ namespace Exiled.API.Features.Core
using System.Reflection;

using Exiled.API.Features.Core.Attributes;

using Exiled.API.Features.Core.Generic.Pools;

using MEC;
using UnityEngine;

/// <summary>
Expand All @@ -25,6 +24,8 @@ public abstract class EObject : TypeCastObject<EObject>
{
private static readonly Dictionary<Type, List<string>> RegisteredTypesValue = new();
private bool destroyedValue;
private bool searchForHostObjectIfNull;
private CoroutineHandle addHostObjectInternalHandle;

/// <summary>
/// Initializes a new instance of the <see cref="EObject"/> class.
Expand Down Expand Up @@ -77,6 +78,31 @@ protected EObject(GameObject gameObject = null)
/// </summary>
public bool IsDestroying { get; private set; }

/// <summary>
/// Gets or sets a value indicating whether to search for the <see cref="Server.Host"/> object
/// when both <see cref="Server.Host"/> and <see cref="Base"/> are null.
/// <br/>
/// If found, assigns it to <see cref="Base"/>.
/// </summary>
public bool SearchForHostObjectIfNull
{
get => searchForHostObjectIfNull;
set
{
if (value == searchForHostObjectIfNull)
return;

if (addHostObjectInternalHandle.IsRunning)
Timing.KillCoroutines(addHostObjectInternalHandle);

searchForHostObjectIfNull = value;
if (!searchForHostObjectIfNull)
return;

addHostObjectInternalHandle = Timing.RunCoroutine(AddHostObject_Internal());
}
}

/// <summary>
/// Gets all the active <see cref="EObject"/> instances.
/// </summary>
Expand Down Expand Up @@ -783,6 +809,17 @@ public override int GetHashCode()
/// <inheritdoc/>
public override bool Equals(object other) => other is not null && other is EObject && other == this;

/// <summary>
/// Waits until the server host object is available and then sets the base game object.
/// </summary>
/// <returns>An IEnumerator representing the asynchronous operation.</returns>
protected internal IEnumerator<float> AddHostObject_Internal()
{
yield return Timing.WaitUntilTrue(() => Server.Host != null);

Base = Server.Host.GameObject;
}

/// <inheritdoc cref="Destroy()"/>
protected virtual void Destroy(bool destroying)
{
Expand Down Expand Up @@ -852,4 +889,4 @@ private static int LevenshteinDistance(string source, string target)
return distance[currentRow, m];
}
}
}
}
10 changes: 4 additions & 6 deletions Exiled.API/Features/Core/Generic/StaticActor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@ public static T CreateNewInstance()
{
EObject @object = CreateDefaultSubobject<T>();
@object.Name = "__" + typeof(T).Name + " (StaticActor)";

if (Server.Host.GameObject)
@object.Base = Server.Host.GameObject;

@object.SearchForHostObjectIfNull = true;
return @object.Cast<T>();
}

Expand All @@ -79,7 +76,8 @@ protected override void PostInitialize()
{
base.PostInitialize();

if (FindExistingInstance() && Get() != this)
T instance = FindExistingInstance();
if (instance is not null && instance != this)
{
Log.Warn($"Found a duplicated instance of a StaticActor with type {GetType().Name} in the Actor {Name} that will be ignored");
NotifyInstanceRepeated();
Expand Down Expand Up @@ -164,4 +162,4 @@ protected virtual void NotifyInstanceRepeated()
comp.Destroy();
}
}
}
}
8 changes: 2 additions & 6 deletions Exiled.API/Features/Core/StaticActor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,8 @@ public static StaticActor CreateNewInstance<T>()
public static StaticActor CreateNewInstance(Type type)
{
EObject @object = CreateDefaultSubobject<StaticActor>(type);

@object.Name = "__" + type.Name + " (StaticActor)";

if (Server.Host.GameObject)
@object.Base = Server.Host.GameObject;

@object.SearchForHostObjectIfNull = true;
return @object.Cast<StaticActor>();
}

Expand Down Expand Up @@ -207,4 +203,4 @@ protected virtual void EndPlay_Static()
/// </remarks>
protected virtual void NotifyInstanceRepeated() => Destroy(GetComponent<StaticActor>());
}
}
}
Loading