mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-14 19:29:36 +01:00
Allow VVing IoC services.
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using JetBrains.Annotations;
|
||||
using SS14.Client.Interfaces.Console;
|
||||
using SS14.Shared.GameObjects;
|
||||
using SS14.Shared.Interfaces.GameObjects;
|
||||
using SS14.Shared.Interfaces.Reflection;
|
||||
using SS14.Shared.IoC;
|
||||
using SS14.Shared.ViewVariables;
|
||||
|
||||
@@ -14,7 +16,7 @@ namespace SS14.Client.ViewVariables
|
||||
{
|
||||
public string Command => "vv";
|
||||
public string Description => "Opens View Variables.";
|
||||
public string Help => "Usage: vv <entity ID>";
|
||||
public string Help => "Usage: vv <entity ID|IoC interface name|SIoC interface name>";
|
||||
|
||||
public bool Execute(IDebugConsole console, params string[] args)
|
||||
{
|
||||
@@ -27,10 +29,28 @@ namespace SS14.Client.ViewVariables
|
||||
}
|
||||
else
|
||||
{
|
||||
var valArg = args[0];
|
||||
if (valArg.StartsWith("SI"))
|
||||
{
|
||||
var selector = new ViewVariablesIoCSelector(valArg.Substring(1));
|
||||
vvm.OpenVV(selector);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (valArg.StartsWith("I"))
|
||||
{
|
||||
// IoC name.
|
||||
var type = IoCManager.Resolve<IReflectionManager>().LooseGetType(valArg);
|
||||
var obj = IoCManager.ResolveType(type);
|
||||
vvm.OpenVV(obj);
|
||||
return false;
|
||||
}
|
||||
|
||||
var entityManager = IoCManager.Resolve<IEntityManager>();
|
||||
var uid = EntityUid.Parse(args[0]);
|
||||
vvm.OpenVV(entityManager.GetEntity(uid));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ using SS14.Server.Interfaces.Player;
|
||||
using SS14.Shared.Enums;
|
||||
using SS14.Shared.Interfaces.GameObjects;
|
||||
using SS14.Shared.Interfaces.Network;
|
||||
using SS14.Shared.Interfaces.Reflection;
|
||||
using SS14.Shared.IoC;
|
||||
using SS14.Shared.Log;
|
||||
using SS14.Shared.Network.Messages;
|
||||
@@ -168,6 +169,17 @@ namespace SS14.Server.ViewVariables
|
||||
|
||||
theObject = value;
|
||||
break;
|
||||
|
||||
case ViewVariablesIoCSelector ioCSelector:
|
||||
var reflectionManager = IoCManager.Resolve<IReflectionManager>();
|
||||
if (!reflectionManager.TryLooseGetType(ioCSelector.TypeName, out var type))
|
||||
{
|
||||
Deny(DenyReason.InvalidRequest);
|
||||
return;
|
||||
}
|
||||
theObject = IoCManager.ResolveType(type);
|
||||
break;
|
||||
|
||||
default:
|
||||
Deny(DenyReason.InvalidRequest);
|
||||
return;
|
||||
|
||||
@@ -54,6 +54,8 @@ namespace SS14.Shared.Interfaces.Reflection
|
||||
|
||||
Type LooseGetType(string name);
|
||||
|
||||
bool TryLooseGetType(string name, out Type type);
|
||||
|
||||
/// <summary>
|
||||
/// Finds all Types in all Assemblies that have a specific Attribute.
|
||||
/// </summary>
|
||||
|
||||
@@ -111,19 +111,25 @@ namespace SS14.Shared.IoC
|
||||
[Pure]
|
||||
public static T Resolve<T>()
|
||||
{
|
||||
Type type = typeof(T);
|
||||
if (!Services.ContainsKey(type))
|
||||
{
|
||||
if (ResolveTypes.ContainsKey(type))
|
||||
{
|
||||
// If we have the type registered but not created that means we haven't been told to initialize the graph yet.
|
||||
throw new InvalidOperationException($"Attempted to resolve type {type} before the object graph for it has been populated.");
|
||||
}
|
||||
return (T)ResolveType(typeof(T));
|
||||
}
|
||||
|
||||
throw new UnregisteredTypeException(type);
|
||||
[Pure]
|
||||
public static object ResolveType(Type type)
|
||||
{
|
||||
if (Services.TryGetValue(type, out var value))
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
return (T)Services[type];
|
||||
if (ResolveTypes.ContainsKey(type))
|
||||
{
|
||||
// If we have the type registered but not created that means we haven't been told to initialize the graph yet.
|
||||
throw new InvalidOperationException($"Attempted to resolve type {type} before the object graph for it has been populated.");
|
||||
}
|
||||
|
||||
throw new UnregisteredTypeException(type);
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -81,18 +81,30 @@ namespace SS14.Shared.Reflection
|
||||
|
||||
/// <inheritdoc />
|
||||
public Type LooseGetType(string name)
|
||||
{
|
||||
if (TryLooseGetType(name, out var ret))
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
throw new ArgumentException("Unable to find type.");
|
||||
}
|
||||
|
||||
public bool TryLooseGetType(string name, out Type type)
|
||||
{
|
||||
foreach (var assembly in assemblies)
|
||||
{
|
||||
foreach (var type in assembly.DefinedTypes)
|
||||
foreach (var tryType in assembly.DefinedTypes)
|
||||
{
|
||||
if (type.FullName.EndsWith(name))
|
||||
if (tryType.FullName.EndsWith(name))
|
||||
{
|
||||
return type;
|
||||
type = tryType;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new ArgumentException("Unable to find type.");
|
||||
|
||||
type = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace SS14.Shared.Serialization
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
var types = reflectionManager.FindTypesWithAttribute<NetSerializableAttribute>();
|
||||
var types = reflectionManager.FindTypesWithAttribute<NetSerializableAttribute>().ToList();
|
||||
var settings = new Settings()
|
||||
{
|
||||
CustomTypeSerializers = new ITypeSerializer[] { new OpenTKTypeSerializer() }
|
||||
|
||||
@@ -73,4 +73,15 @@ namespace SS14.Shared.ViewVariables
|
||||
/// </remarks>
|
||||
public object[] PropertyIndex { get; }
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public class ViewVariablesIoCSelector : ViewVariablesObjectSelector
|
||||
{
|
||||
public ViewVariablesIoCSelector(string typeName)
|
||||
{
|
||||
TypeName = typeName;
|
||||
}
|
||||
|
||||
public string TypeName { get; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user