Add Resolves to IoCManager and EntitySystemManager (#2231)

This commit is contained in:
Vera Aguilera Puerto
2021-11-14 19:41:34 +01:00
committed by GitHub
parent ad9bda2efe
commit 6cf5021efa
6 changed files with 184 additions and 8 deletions

View File

@@ -3,12 +3,15 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.CompilerServices;
using Prometheus;
using Robust.Shared.IoC;
using Robust.Shared.IoC.Exceptions;
using Robust.Shared.Log;
using Robust.Shared.Reflection;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
using Dependency = Robust.Shared.IoC.DependencyAttribute;
#if EXCEPTION_TOLERANCE
using Robust.Shared.Exceptions;
#endif
@@ -53,13 +56,47 @@ namespace Robust.Shared.GameObjects
/// <inheritdoc />
public event EventHandler<SystemChangedArgs>? SystemUnloaded;
/// <exception cref="InvalidEntitySystemException">Thrown if the provided type is not registered.</exception>
/// <exception cref="UnregisteredTypeException">Thrown if the provided type is not registered.</exception>
public T GetEntitySystem<T>()
where T : IEntitySystem
{
return _systemDependencyCollection.Resolve<T>();
}
[MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)]
public void Resolve<T>([NotNull] ref T? instance)
where T : IEntitySystem
{
_systemDependencyCollection.Resolve(ref instance);
}
[MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)]
public void Resolve<T1, T2>([NotNull] ref T1? instance1, [NotNull] ref T2? instance2)
where T1 : IEntitySystem
where T2 : IEntitySystem
{
_systemDependencyCollection.Resolve(ref instance1, ref instance2);
}
[MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)]
public void Resolve<T1, T2, T3>([NotNull] ref T1? instance1, [NotNull] ref T2? instance2, [NotNull] ref T3? instance3)
where T1 : IEntitySystem
where T2 : IEntitySystem
where T3 : IEntitySystem
{
_systemDependencyCollection.Resolve(ref instance1, ref instance2, ref instance3);
}
[MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)]
public void Resolve<T1, T2, T3, T4>([NotNull] ref T1? instance1, [NotNull] ref T2? instance2, [NotNull] ref T3? instance3, [NotNull] ref T4? instance4)
where T1 : IEntitySystem
where T2 : IEntitySystem
where T3 : IEntitySystem
where T4 : IEntitySystem
{
_systemDependencyCollection.Resolve(ref instance1, ref instance2, ref instance3, ref instance4);
}
/// <inheritdoc />
public bool TryGetEntitySystem<T>([NotNullWhen(true)] out T? entitySystem)
where T : IEntitySystem

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Robust.Shared.IoC.Exceptions;
namespace Robust.Shared.GameObjects
{
@@ -39,6 +40,44 @@ namespace Robust.Shared.GameObjects
/// <returns>The <see cref="IEntitySystem"/> instance matching the specified type.</returns>
T GetEntitySystem<T>() where T : IEntitySystem;
/// <summary>
/// Resolves an entity system.
/// </summary>
/// <exception cref="UnregisteredTypeException">Thrown if the provided type is not registered.</exception>
/// <exception cref="InvalidOperationException">
/// Thrown if the resolved type hasn't been created yet
/// because the dependency collection object graph still needs to be constructed for it.
/// </exception>
void Resolve<T>([NotNull] ref T? instance)
where T : IEntitySystem;
/// <inheritdoc cref="Resolve{T}(ref T?)"/>
/// <summary>
/// Resolve two entity systems.
/// </summary>
void Resolve<T1, T2>([NotNull] ref T1? instance1, [NotNull] ref T2? instance2)
where T1 : IEntitySystem
where T2 : IEntitySystem;
/// <inheritdoc cref="Resolve{T1, T2}(ref T1?, ref T2?)"/>
/// <summary>
/// Resolve three entity systems.
/// </summary>
void Resolve<T1, T2, T3>([NotNull] ref T1? instance1, [NotNull] ref T2? instance2, [NotNull] ref T3? instance3)
where T1 : IEntitySystem
where T2 : IEntitySystem
where T3 : IEntitySystem;
/// <inheritdoc cref="Resolve{T1, T2, T3}(ref T1?, ref T2?, ref T3?)"/>
/// <summary>
/// Resolve four entity systems.
/// </summary>
void Resolve<T1, T2, T3, T4>([NotNull] ref T1? instance1, [NotNull] ref T2? instance2, [NotNull] ref T3? instance3, [NotNull] ref T4? instance4)
where T1 : IEntitySystem
where T2 : IEntitySystem
where T3 : IEntitySystem
where T4 : IEntitySystem;
/// <summary>
/// Tries to get an entity system of the specified type.
/// </summary>

View File

@@ -4,9 +4,11 @@ using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using JetBrains.Annotations;
using Robust.Shared.IoC.Exceptions;
using Robust.Shared.Utility;
using NotNull = System.Diagnostics.CodeAnalysis.NotNullAttribute;
namespace Robust.Shared.IoC
{
@@ -242,6 +244,34 @@ namespace Robust.Shared.IoC
return (T) ResolveType(typeof(T));
}
[MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)]
public void Resolve<T>([NotNull] ref T? instance)
{
// Resolve<T>() will either throw or return a concrete instance, therefore we suppress the nullable warning.
instance ??= Resolve<T>()!;
}
[MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)]
public void Resolve<T1, T2>([NotNull] ref T1? instance1, [NotNull] ref T2? instance2)
{
Resolve(ref instance1);
Resolve(ref instance2);
}
[MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)]
public void Resolve<T1, T2, T3>([NotNull] ref T1? instance1, [NotNull] ref T2? instance2, [NotNull] ref T3? instance3)
{
Resolve(ref instance1, ref instance2);
Resolve(ref instance3);
}
[MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)]
public void Resolve<T1, T2, T3, T4>([NotNull] ref T1? instance1, [NotNull] ref T2? instance2, [NotNull] ref T3? instance3, [NotNull] ref T4? instance4)
{
Resolve(ref instance1, ref instance2);
Resolve(ref instance3, ref instance4);
}
/// <inheritdoc />
[System.Diagnostics.Contracts.Pure]
public object ResolveType(Type type)

View File

@@ -3,6 +3,7 @@ using System.Diagnostics.CodeAnalysis;
using JetBrains.Annotations;
using Robust.Shared.IoC.Exceptions;
using Robust.Shared.Reflection;
using NotNull = System.Diagnostics.CodeAnalysis.NotNullAttribute;
namespace Robust.Shared.IoC
{
@@ -134,6 +135,27 @@ namespace Robust.Shared.IoC
[System.Diagnostics.Contracts.Pure]
T Resolve<T>();
/// <inheritdoc cref="Resolve{T}()"/>
void Resolve<T>([NotNull] ref T? instance);
/// <inheritdoc cref="Resolve{T}(ref T?)"/>
/// <summary>
/// Resolve two dependencies manually.
/// </summary>
void Resolve<T1, T2>([NotNull] ref T1? instance1, [NotNull] ref T2? instance2);
/// <inheritdoc cref="Resolve{T1, T2}(ref T1?, ref T2?)"/>
/// <summary>
/// Resolve three dependencies manually.
/// </summary>
void Resolve<T1, T2, T3>([NotNull] ref T1? instance1, [NotNull] ref T2? instance2, [NotNull] ref T3? instance3);
/// <inheritdoc cref="Resolve{T1, T2, T3}(ref T1?, ref T2?, ref T3?)"/>
/// <summary>
/// Resolve four dependencies manually.
/// </summary>
void Resolve<T1, T2, T3, T4>([NotNull] ref T1? instance1, [NotNull] ref T2? instance2, [NotNull] ref T3? instance3, [NotNull] ref T4? instance4);
/// <summary>
/// Resolve a dependency manually.
/// </summary>

View File

@@ -1,10 +1,13 @@
using Robust.Shared.IoC.Exceptions;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Robust.Shared.Reflection;
using Robust.Shared.Utility;
using NotNull = System.Diagnostics.CodeAnalysis.NotNullAttribute;
namespace Robust.Shared.IoC
{
@@ -190,6 +193,51 @@ namespace Robust.Shared.IoC
return _container.Value!.Resolve<T>();
}
/// <inheritdoc cref="Resolve{T}()"/>
[MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)]
public static void Resolve<T>([NotNull] ref T? instance)
{
DebugTools.Assert(_container.IsValueCreated, NoContextAssert);
_container.Value!.Resolve(ref instance);
}
/// <inheritdoc cref="Resolve{T}(ref T?)"/>
/// <summary>
/// Resolve two dependencies manually.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)]
public static void Resolve<T1, T2>([NotNull] ref T1? instance1, [NotNull] ref T2? instance2)
{
DebugTools.Assert(_container.IsValueCreated, NoContextAssert);
_container.Value!.Resolve(ref instance1, ref instance2);
}
/// <inheritdoc cref="Resolve{T1, T2}(ref T1?, ref T2?)"/>
/// <summary>
/// Resolve three dependencies manually.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)]
public static void Resolve<T1, T2, T3>([NotNull] ref T1? instance1, [NotNull] ref T2? instance2, [NotNull] ref T3? instance3)
{
DebugTools.Assert(_container.IsValueCreated, NoContextAssert);
_container.Value!.Resolve(ref instance1, ref instance2, ref instance3);
}
/// <inheritdoc cref="Resolve{T1, T2, T3}(ref T1?, ref T2?, ref T3?)"/>
/// <summary>
/// Resolve four dependencies manually.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)]
public static void Resolve<T1, T2, T3, T4>([NotNull] ref T1? instance1, [NotNull] ref T2? instance2, [NotNull] ref T3? instance3, [NotNull] ref T4? instance4)
{
DebugTools.Assert(_container.IsValueCreated, NoContextAssert);
_container.Value!.Resolve(ref instance1, ref instance2, ref instance3, ref instance4);
}
/// <summary>
/// Resolve a dependency manually.
/// </summary>

View File

@@ -41,7 +41,7 @@ namespace Robust.Shared.Player
/// </summary>
public Filter AddPlayersByPvs(EntityUid origin, float rangeMultiplier = 2f, IEntityManager? entityManager = null)
{
entityManager ??= IoCManager.Resolve<IEntityManager>();
IoCManager.Resolve(ref entityManager);
var transform = entityManager.GetComponent<TransformComponent>(origin);
return AddPlayersByPvs(transform.MapPosition, rangeMultiplier);
}
@@ -70,7 +70,7 @@ namespace Robust.Shared.Player
/// </summary>
public Filter AddPlayersByPvs(EntityCoordinates origin, float rangeMultiplier = 2f, IEntityManager? entityMan = null, ISharedPlayerManager? playerMan = null)
{
entityMan ??= IoCManager.Resolve<IEntityManager>();
IoCManager.Resolve(ref entityMan, ref playerMan);
return AddPlayersByPvs(origin.ToMap(entityMan), rangeMultiplier, playerMan);
}
@@ -78,9 +78,9 @@ namespace Robust.Shared.Player
/// Adds all players inside an entity's PVS.
/// The current PVS range will be multiplied by <see cref="rangeMultiplier"/>.
/// </summary>
public Filter AddPlayersByPvs(MapCoordinates origin, float rangeMultiplier = 2f, ISharedPlayerManager? playerMan = null)
public Filter AddPlayersByPvs(MapCoordinates origin, float rangeMultiplier = 2f, ISharedPlayerManager? playerMan = null, IConfigurationManager? cfgMan = null)
{
var cfgMan = IoCManager.Resolve<IConfigurationManager>();
IoCManager.Resolve(ref playerMan, ref cfgMan);
// If PVS is disabled, we simply return all players.
if (!cfgMan.GetCVar(CVars.NetPVS))
@@ -107,9 +107,9 @@ namespace Robust.Shared.Player
/// <summary>
/// Adds all players to the filter.
/// </summary>
public Filter AddAllPlayers()
public Filter AddAllPlayers(ISharedPlayerManager? playerMan = null)
{
var playerMan = IoCManager.Resolve<ISharedPlayerManager>();
IoCManager.Resolve(ref playerMan);
_recipients = new HashSet<ICommonSession>(playerMan.NetworkedSessions);
@@ -121,7 +121,7 @@ namespace Robust.Shared.Player
/// </summary>
public Filter AddWhere(Predicate<ICommonSession> predicate, ISharedPlayerManager? playerMan = null)
{
playerMan ??= IoCManager.Resolve<ISharedPlayerManager>();
IoCManager.Resolve(ref playerMan);
foreach (var player in playerMan.NetworkedSessions)
{
if (predicate(player))