Revert "Obsolete static IoC methods (#6232)" (#6296)

This reverts commit 14439784dd.
This commit is contained in:
metalgearsloth
2025-11-15 11:26:52 +11:00
committed by GitHub
parent 64baee0a22
commit 663b83821a
8 changed files with 16 additions and 30 deletions

View File

@@ -7,6 +7,7 @@ using System.Runtime.CompilerServices;
using Robust.Client.Audio;
using Robust.Shared.ContentPack;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Utility;
namespace Robust.Client.ResourceManagement;
@@ -16,7 +17,6 @@ namespace Robust.Client.ResourceManagement;
/// </summary>
internal sealed partial class ResourceCache : ResourceManager, IResourceCacheInternal, IDisposable
{
[Shared.IoC.Dependency] private readonly IDependencyCollection _deps = default!;
private readonly Dictionary<Type, TypeData> _cachedResources = new();
private readonly Dictionary<Type, BaseResource> _fallbacks = new();
@@ -36,7 +36,8 @@ internal sealed partial class ResourceCache : ResourceManager, IResourceCacheInt
var resource = new T();
try
{
resource.Load(_deps, path);
var dependencies = IoCManager.Instance!;
resource.Load(dependencies, path);
cache.Resources[path] = resource;
return resource;
}
@@ -80,7 +81,8 @@ internal sealed partial class ResourceCache : ResourceManager, IResourceCacheInt
var _resource = new T();
try
{
_resource.Load(_deps, path);
var dependencies = IoCManager.Instance!;
_resource.Load(dependencies, path);
resource = _resource;
cache.Resources[path] = resource;
return true;
@@ -121,7 +123,8 @@ internal sealed partial class ResourceCache : ResourceManager, IResourceCacheInt
try
{
res.Reload(_deps, path);
var dependencies = IoCManager.Instance!;
res.Reload(dependencies, path);
}
catch (Exception e)
{

View File

@@ -50,7 +50,6 @@ namespace Robust.Shared.IoC
/// <remarks>
/// This property will be null if <see cref="InitThread()"/> has not been called on this thread yet.
/// </remarks>
[Obsolete("Resolve the IDependencyCollection directly without static methods (e.g., with a [Dependency] IDependencyCollection field)")]
public static IDependencyCollection? Instance => _container.IsValueCreated ? _container.Value : null;
/// <summary>
@@ -103,7 +102,6 @@ namespace Robust.Shared.IoC
/// Thrown if <paramref name="overwrite"/> is false and <typeparamref name="TInterface"/> has been registered before,
/// or if an already instantiated interface (by <see cref="BuildGraph"/>) is attempting to be overwritten.
/// </exception>
[Obsolete("Use an IDependencyCollection instance instead of static methods")]
public static void Register<TInterface, [MeansImplicitUse] TImplementation>(bool overwrite = false)
where TImplementation : class, TInterface
where TInterface : class
@@ -125,7 +123,6 @@ namespace Robust.Shared.IoC
/// Thrown if <paramref name="overwrite"/> is false and <typeparamref name="T"/> has been registered before,
/// or if an already instantiated interface (by <see cref="BuildGraph"/>) is attempting to be overwritten.
/// </exception>
[Obsolete("Use an IDependencyCollection instance instead of static methods")]
public static void Register<[MeansImplicitUse] T>(bool overwrite = false) where T : class
{
Register<T, T>(overwrite);
@@ -146,7 +143,6 @@ namespace Robust.Shared.IoC
/// Thrown if <paramref name="overwrite"/> is false and <typeparamref name="TInterface"/> has been registered before,
/// or if an already instantiated interface (by <see cref="BuildGraph"/>) is attempting to be overwritten.
/// </exception>
[Obsolete("Use an IDependencyCollection instance instead of static methods")]
public static void Register<TInterface, TImplementation>(DependencyFactoryDelegate<TImplementation> factory, bool overwrite = false)
where TImplementation : class, TInterface
where TInterface : class
@@ -169,7 +165,6 @@ namespace Robust.Shared.IoC
/// If true, do not throw an <see cref="InvalidOperationException"/> if an interface is already registered,
/// replace the current implementation instead.
/// </param>
[Obsolete("Use an IDependencyCollection instance instead of static methods")]
public static void RegisterInstance<TInterface>(object implementation, bool overwrite = false)
where TInterface : class
{
@@ -198,7 +193,6 @@ namespace Robust.Shared.IoC
/// because the object graph still needs to be constructed for it.
/// </exception>
[System.Diagnostics.Contracts.Pure]
[Obsolete("Use dependency injection or an IDependencyCollection instance instead of static methods")]
public static T Resolve<T>()
{
DebugTools.Assert(_container.IsValueCreated, NoContextAssert);
@@ -208,7 +202,6 @@ namespace Robust.Shared.IoC
/// <inheritdoc cref="Resolve{T}()"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Obsolete("Use dependency injection or an IDependencyCollection instance instead of static methods")]
public static void Resolve<T>([NotNull] ref T? instance)
{
// Do not call into IDependencyCollection immediately for this,
@@ -221,7 +214,6 @@ namespace Robust.Shared.IoC
/// Resolve two dependencies manually.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Obsolete("Use dependency injection or an IDependencyCollection instance instead of static methods")]
public static void Resolve<T1, T2>([NotNull] ref T1? instance1, [NotNull] ref T2? instance2)
{
DebugTools.Assert(_container.IsValueCreated, NoContextAssert);
@@ -234,7 +226,6 @@ namespace Robust.Shared.IoC
/// Resolve three dependencies manually.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Obsolete("Use dependency injection or an IDependencyCollection instance instead of static methods")]
public static void Resolve<T1, T2, T3>([NotNull] ref T1? instance1, [NotNull] ref T2? instance2, [NotNull] ref T3? instance3)
{
DebugTools.Assert(_container.IsValueCreated, NoContextAssert);
@@ -247,7 +238,6 @@ namespace Robust.Shared.IoC
/// Resolve four dependencies manually.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Obsolete("Use dependency injection or an IDependencyCollection instance instead of static methods")]
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);
@@ -264,7 +254,6 @@ namespace Robust.Shared.IoC
/// because the object graph still needs to be constructed for it.
/// </exception>
[System.Diagnostics.Contracts.Pure]
[Obsolete("Use dependency injection or an IDependencyCollection instance instead of static methods")]
public static object ResolveType(Type type)
{
DebugTools.Assert(_container.IsValueCreated, NoContextAssert);
@@ -295,7 +284,6 @@ namespace Robust.Shared.IoC
/// Thrown if a dependency field on the object is not registered.
/// </exception>
/// <seealso cref="BuildGraph"/>
[Obsolete("Use an IDependencyCollection instance instead of static methods")]
public static T InjectDependencies<T>(T obj) where T : notnull
{
DebugTools.Assert(_container.IsValueCreated, NoContextAssert);

View File

@@ -7,10 +7,9 @@ namespace Robust.Shared.Toolshed.Commands.Misc;
[ToolshedCommand]
internal sealed class IoCCommand : ToolshedCommand
{
[Dependency] private readonly IDependencyCollection _deps = default!;
[CommandImplementation("registered")]
public IEnumerable<Type> Registered() => _deps.GetRegisteredTypes();
public IEnumerable<Type> Registered() => IoCManager.Instance!.GetRegisteredTypes();
[CommandImplementation("get")]
public object? Get([PipedArgument] Type t) => _deps.ResolveType(t);
public object? Get([PipedArgument] Type t) => IoCManager.ResolveType(t);
}

View File

@@ -43,9 +43,9 @@ internal abstract partial class ViewVariablesManager
private (ViewVariablesPath? Path, string[] Segments) ResolveIoCObject(string path)
{
var empty = (new ViewVariablesInstancePath(_deps), Array.Empty<string>());
var empty = (new ViewVariablesInstancePath(IoCManager.Instance), Array.Empty<string>());
if (string.IsNullOrEmpty(path))
if (string.IsNullOrEmpty(path) || IoCManager.Instance == null)
return empty;
var segments = path.Split('/');
@@ -58,24 +58,24 @@ internal abstract partial class ViewVariablesManager
if (!_reflectionMan.TryLooseGetType(service, out var type))
return EmptyResolve;
return _deps.TryResolveType(type, out var obj)
return IoCManager.Instance.TryResolveType(type, out var obj)
? (new ViewVariablesInstancePath(obj), segments[1..])
: EmptyResolve;
}
private IEnumerable<string>? ListIoCPaths(string[] segments)
{
if (segments.Length > 1)
if (segments.Length > 1 || IoCManager.Instance is not {} deps)
return null;
if (segments.Length == 1
&& _reflectionMan.TryLooseGetType(segments[0], out var type)
&& _deps.TryResolveType(type, out _))
&& deps.TryResolveType(type, out _))
{
return null;
}
return _deps.GetRegisteredTypes()
return deps.GetRegisteredTypes()
.Select(t => t.Name);
}
@@ -161,7 +161,7 @@ internal abstract partial class ViewVariablesManager
{
var empty = (new ViewVariablesInstancePath(_protoMan), Array.Empty<string>());
if (string.IsNullOrEmpty(path))
if (string.IsNullOrEmpty(path) || IoCManager.Instance == null)
return empty;
var segments = path.Split('/');

View File

@@ -22,7 +22,6 @@ internal abstract partial class ViewVariablesManager : IViewVariablesManager, IP
[Dependency] private readonly IReflectionManager _reflectionMan = default!;
[Dependency] private readonly INetManager _netMan = default!;
[Dependency] private readonly ILogManager _logMan = default!;
[Dependency] private readonly IDependencyCollection _deps = default!;
private readonly Dictionary<Type, HashSet<object>> _cachedTraits = new();

View File

@@ -4,7 +4,6 @@ using NUnit.Framework;
using Robust.Shared.Analyzers;
using Robust.Shared.IoC;
using Robust.Shared.IoC.Exceptions;
#pragma warning disable CS0618 // Type or member is obsolete
namespace Robust.UnitTesting.Shared.IoC
{

View File

@@ -6,7 +6,6 @@ using Robust.Shared.ContentPack;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Log;
#pragma warning disable CS0618 // Type or member is obsolete
namespace Robust.UnitTesting.Shared.Localization;

View File

@@ -3,7 +3,6 @@ using Robust.Shared.IoC;
using Robust.Shared.Reflection;
using System.Collections.Generic;
using JetBrains.Annotations;
#pragma warning disable CS0618 // Type or member is obsolete
namespace Robust.UnitTesting.Shared.Reflection
{