Skip to content

Commit

Permalink
Add doc, Fix bug, code simplification (#2335)
Browse files Browse the repository at this point in the history
* Add doc, Fix bug, code simplification

* TypeCastObject & TypeCastMono rewrite Cast
  • Loading branch information
warquys authored Dec 20, 2023
1 parent 9a52272 commit bfbe175
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Exiled.API/Features/Core/EObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public static Type GetObjectTypeFromRegisteredTypes(Type type, string name)
public static EObject CreateDefaultSubobject(Type type, params object[] parameters)
{
BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance;
EObject @object = Activator.CreateInstance(type, flags, null, parameters, null) is not EObject outer ? null : outer;
EObject @object = Activator.CreateInstance(type, flags, null, parameters, null) as EObject;

// Do not use implicit bool conversion as @object may be null
if (@object != null)
Expand Down
2 changes: 1 addition & 1 deletion Exiled.API/Features/Core/Generic/EnumClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public static TObject Parse(string obj)
/// Converts the <see cref="EnumClass{TSource, TObject}"/> instance to a human-readable <see cref="string"/> representation.
/// </summary>
/// <returns>A human-readable <see cref="string"/> representation of the <see cref="EnumClass{TSource, TObject}"/> instance.</returns>
public override string ToString() => name;
public override string ToString() => Name;

/// <summary>
/// Determines whether the specified object is equal to the current object.
Expand Down
2 changes: 1 addition & 1 deletion Exiled.API/Features/Core/Generic/UnmanagedEnumClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public static TObject Parse(string obj)
/// Converts the <see cref="UnmanagedEnumClass{TSource, TObject}"/> instance to a human-readable <see cref="string"/> representation.
/// </summary>
/// <returns>A human-readable <see cref="string"/> representation of the <see cref="UnmanagedEnumClass{TSource, TObject}"/> instance.</returns>
public override string ToString() => name;
public override string ToString() => Name;

/// <summary>
/// Determines whether the specified object is equal to the current object.
Expand Down
9 changes: 7 additions & 2 deletions Exiled.API/Features/Core/StaticActor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public abstract class StaticActor : EActor
/// </summary>
public bool IsDestroyed { get; private set; }

/// <inheritdoc cref="CreateNewInstance"/>
/// <typeparam name="T"><inheritdoc path="/param[@name='type']" cref="CreateNewInstance"/></typeparam>
public static StaticActor CreateNewInstance<T>()
where T : new() => CreateNewInstance(typeof(T));

/// <summary>
/// Creates a new instance of the <see cref="StaticActor"/>.
/// </summary>
Expand All @@ -60,7 +65,7 @@ public static StaticActor CreateNewInstance(Type type)
/// <typeparam name="T">The type of the <see cref="StaticActor"/> to look for.</typeparam>
/// <returns>The corresponding <see cref="StaticActor"/>, or <see langword="null"/> if not found.</returns>
public static T Get<T>()
where T : StaticActor
where T : StaticActor, new()
{
foreach (StaticActor actor in FindActiveObjectsOfType<StaticActor>())
{
Expand All @@ -70,7 +75,7 @@ public static T Get<T>()
return actor.Cast<T>();
}

return CreateNewInstance(typeof(T)).Cast<T>();
return CreateNewInstance<T>().Cast<T>();
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Exiled.API/Features/Core/TickComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Exiled.API.Features.Core
public sealed class TickComponent : EObject
{
/// <summary>
/// The default fixed tick rate.
/// The default fixed tick rate (60 per second).
/// </summary>
public const float DefaultFixedTickRate = 0.016f;

Expand Down
9 changes: 5 additions & 4 deletions Exiled.API/Features/Core/TypeCastMono.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ protected TypeCastMono()

/// <inheritdoc/>
public TObject Cast<TObject>()
where TObject : class, T => this as T as TObject;
where TObject : class, T => this as TObject;

/// <inheritdoc/>
public bool Cast<TObject>(out TObject param)
where TObject : class, T
{
param = default;

if (this as TObject is not TObject cast)
if (this is not TObject cast)
{
param = default;
return false;
}

param = cast;
return true;
Expand Down
9 changes: 5 additions & 4 deletions Exiled.API/Features/Core/TypeCastObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@ protected TypeCastObject()

/// <inheritdoc/>
public TObject Cast<TObject>()
where TObject : class, T => this as T as TObject;
where TObject : class, T => this as TObject;

/// <inheritdoc/>
public bool Cast<TObject>(out TObject param)
where TObject : class, T
{
param = default;

if (this as TObject is not TObject cast)
if (this is not TObject cast)
{
param = default;
return false;
}

param = cast;
return true;
Expand Down

0 comments on commit bfbe175

Please sign in to comment.