From baff29362ac63aa64bf9b50361d6da9215397c49 Mon Sep 17 00:00:00 2001 From: Pieter-Jan Briers Date: Sun, 25 Nov 2018 16:38:40 +0100 Subject: [PATCH] Allow VVing IoC services. --- .../ViewVariables/ViewVariablesCommand.cs | 22 +++++++++++++++- .../ViewVariables/ViewVariablesHost.cs | 12 +++++++++ .../Reflection/IReflectionManager.cs | 2 ++ SS14.Shared/IoC/IoCManager.cs | 26 ++++++++++++------- SS14.Shared/Reflection/ReflectionManager.cs | 20 +++++++++++--- SS14.Shared/Serialization/SS14Serializer.cs | 2 +- .../ViewVariablesObjectSelector.cs | 11 ++++++++ 7 files changed, 79 insertions(+), 16 deletions(-) diff --git a/SS14.Client/ViewVariables/ViewVariablesCommand.cs b/SS14.Client/ViewVariables/ViewVariablesCommand.cs index ef49e4811..deedbe933 100644 --- a/SS14.Client/ViewVariables/ViewVariablesCommand.cs +++ b/SS14.Client/ViewVariables/ViewVariablesCommand.cs @@ -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 "; + public string Help => "Usage: vv "; 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().LooseGetType(valArg); + var obj = IoCManager.ResolveType(type); + vvm.OpenVV(obj); + return false; + } + var entityManager = IoCManager.Resolve(); var uid = EntityUid.Parse(args[0]); vvm.OpenVV(entityManager.GetEntity(uid)); } + return false; } diff --git a/SS14.Server/ViewVariables/ViewVariablesHost.cs b/SS14.Server/ViewVariables/ViewVariablesHost.cs index e3a86b582..86b6548aa 100644 --- a/SS14.Server/ViewVariables/ViewVariablesHost.cs +++ b/SS14.Server/ViewVariables/ViewVariablesHost.cs @@ -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(); + if (!reflectionManager.TryLooseGetType(ioCSelector.TypeName, out var type)) + { + Deny(DenyReason.InvalidRequest); + return; + } + theObject = IoCManager.ResolveType(type); + break; + default: Deny(DenyReason.InvalidRequest); return; diff --git a/SS14.Shared/Interfaces/Reflection/IReflectionManager.cs b/SS14.Shared/Interfaces/Reflection/IReflectionManager.cs index 14ca62c18..d569663a6 100644 --- a/SS14.Shared/Interfaces/Reflection/IReflectionManager.cs +++ b/SS14.Shared/Interfaces/Reflection/IReflectionManager.cs @@ -54,6 +54,8 @@ namespace SS14.Shared.Interfaces.Reflection Type LooseGetType(string name); + bool TryLooseGetType(string name, out Type type); + /// /// Finds all Types in all Assemblies that have a specific Attribute. /// diff --git a/SS14.Shared/IoC/IoCManager.cs b/SS14.Shared/IoC/IoCManager.cs index 7db7d8a90..3efa0f9fe 100644 --- a/SS14.Shared/IoC/IoCManager.cs +++ b/SS14.Shared/IoC/IoCManager.cs @@ -111,19 +111,25 @@ namespace SS14.Shared.IoC [Pure] public static T Resolve() { - 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); + } /// diff --git a/SS14.Shared/Reflection/ReflectionManager.cs b/SS14.Shared/Reflection/ReflectionManager.cs index faaad8f6a..bf2e5b914 100644 --- a/SS14.Shared/Reflection/ReflectionManager.cs +++ b/SS14.Shared/Reflection/ReflectionManager.cs @@ -81,18 +81,30 @@ namespace SS14.Shared.Reflection /// 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; } /// diff --git a/SS14.Shared/Serialization/SS14Serializer.cs b/SS14.Shared/Serialization/SS14Serializer.cs index 201dc89e6..997c0eb31 100644 --- a/SS14.Shared/Serialization/SS14Serializer.cs +++ b/SS14.Shared/Serialization/SS14Serializer.cs @@ -19,7 +19,7 @@ namespace SS14.Shared.Serialization public void Initialize() { - var types = reflectionManager.FindTypesWithAttribute(); + var types = reflectionManager.FindTypesWithAttribute().ToList(); var settings = new Settings() { CustomTypeSerializers = new ITypeSerializer[] { new OpenTKTypeSerializer() } diff --git a/SS14.Shared/ViewVariables/ViewVariablesObjectSelector.cs b/SS14.Shared/ViewVariables/ViewVariablesObjectSelector.cs index 7a09d3781..a1e46405f 100644 --- a/SS14.Shared/ViewVariables/ViewVariablesObjectSelector.cs +++ b/SS14.Shared/ViewVariables/ViewVariablesObjectSelector.cs @@ -73,4 +73,15 @@ namespace SS14.Shared.ViewVariables /// public object[] PropertyIndex { get; } } + + [Serializable, NetSerializable] + public class ViewVariablesIoCSelector : ViewVariablesObjectSelector + { + public ViewVariablesIoCSelector(string typeName) + { + TypeName = typeName; + } + + public string TypeName { get; } + } }