Fix exception in ReagentPrototype caused by IMetabolizable (#451)

* Fix exception in ReagentPrototype

Due to client trying to access concrete implementations of IMetabolizable that are in Content.Server. This fix checks to see if the prototype is being loaded by the client through reflection. I don't want to move the prototype completely into Content.Server since it's useful for the client to view descriptions and values of reagents without needing to send that data from the server.

* Make fix slightly more explicit

Now it will only load the metabolizable when in Robust.Server, instead of it being anything that's not Robust.Client.

* Add IModuleManager and ModuleManager

Provide simple way to check if shared code is being run by the server or the client

* Change ModuleManager implementations to not require assembly name comparison

Now just has ClientModuleManager registered to client, and ServerModuleManager registered to server.

* Change IModuleManager functions to properties

* Fix failing tests.

This was failing because the tests weren't initializing IoC. Simply using RobustUnitTest wasn't enough because that doesn't initialize content either. I did some cleaning up so now content IoC is registered via ContentUnitTest.
This commit is contained in:
moneyl
2019-11-23 15:55:31 -05:00
committed by Pieter-Jan Briers
parent 4198b6dc4e
commit b89615342e
11 changed files with 162 additions and 28 deletions

View File

@@ -0,0 +1,30 @@
using Content.Client.Chat;
using Content.Client.GameTicking;
using Content.Client.Interfaces;
using Content.Client.Interfaces.Chat;
using Content.Client.Interfaces.Parallax;
using Content.Client.Parallax;
using Content.Client.Sandbox;
using Content.Client.UserInterface;
using Content.Client.Utility;
using Content.Shared.Interfaces;
using Robust.Shared.IoC;
namespace Content.Client
{
internal static class ClientContentIoC
{
public static void Register()
{
IoCManager.Register<IGameHud, GameHud>();
IoCManager.Register<IClientNotifyManager, ClientNotifyManager>();
IoCManager.Register<ISharedNotifyManager, ClientNotifyManager>();
IoCManager.Register<IClientGameTicker, ClientGameTicker>();
IoCManager.Register<IParallaxManager, ParallaxManager>();
IoCManager.Register<IChatManager, ChatManager>();
IoCManager.Register<IEscapeMenuOwner, EscapeMenuOwner>();
IoCManager.Register<ISandboxManager, SandboxManager>();
IoCManager.Register<IModuleManager, ClientModuleManager>();
}
}
}

View File

@@ -1,7 +1,5 @@
using System;
using Content.Client.Chat;
using Content.Client.GameObjects.Components.Actor;
using Content.Client.GameTicking;
using Content.Client.Input;
using Content.Client.Interfaces;
using Content.Client.Interfaces.Chat;
@@ -15,7 +13,6 @@ using Content.Shared.GameObjects.Components.Chemistry;
using Content.Shared.GameObjects.Components.Markers;
using Content.Shared.GameObjects.Components.Research;
using Content.Shared.GameObjects.Components.VendingMachines;
using Content.Shared.Interfaces;
using Robust.Client.Interfaces;
using Robust.Client.Interfaces.Graphics.Overlays;
using Robust.Client.Interfaces.Input;
@@ -143,14 +140,7 @@ namespace Content.Client
prototypes.RegisterIgnore("material");
prototypes.RegisterIgnore("reaction"); //Chemical reactions only needed by server. Reactions checks are server-side.
IoCManager.Register<IGameHud, GameHud>();
IoCManager.Register<IClientNotifyManager, ClientNotifyManager>();
IoCManager.Register<ISharedNotifyManager, ClientNotifyManager>();
IoCManager.Register<IClientGameTicker, ClientGameTicker>();
IoCManager.Register<IParallaxManager, ParallaxManager>();
IoCManager.Register<IChatManager, ChatManager>();
IoCManager.Register<IEscapeMenuOwner, EscapeMenuOwner>();
IoCManager.Register<ISandboxManager, SandboxManager>();
ClientContentIoC.Register();
if (TestingCallbacks != null)
{

View File

@@ -0,0 +1,15 @@
using Content.Shared.Interfaces;
namespace Content.Client.Utility
{
/// <summary>
/// Client implementation of IModuleManager.
/// Provides simple way for shared code to check if it's being run by
/// the client of the server.
/// </summary>
public class ClientModuleManager : IModuleManager
{
bool IModuleManager.IsClientModule => true;
bool IModuleManager.IsServerModule => false;
}
}

View File

@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("Content.Tests")]

View File

@@ -1,11 +1,8 @@
using Content.Server.Cargo;
using Content.Server.Chat;
using Content.Server.GameTicking;
using Content.Server.Interfaces;
using Content.Server.Interfaces.Chat;
using Content.Server.Interfaces.GameTicking;
using Content.Server.Sandbox;
using Content.Shared.Interfaces;
using Robust.Server.Interfaces.Player;
using Robust.Shared.ContentPack;
using Robust.Shared.Interfaces.GameObjects;
@@ -45,14 +42,8 @@ namespace Content.Server
factory.RegisterIgnore(ignoreName);
}
IoCManager.Register<ISharedNotifyManager, ServerNotifyManager>();
IoCManager.Register<IServerNotifyManager, ServerNotifyManager>();
IoCManager.Register<IGameTicker, GameTicker>();
IoCManager.Register<IChatManager, ChatManager>();
IoCManager.Register<IMoMMILink, MoMMILink>();
IoCManager.Register<ISandboxManager, SandboxManager>();
IoCManager.Register<IGalacticBankManager, GalacticBankManager>();
IoCManager.Register<ICargoOrderDataManager, CargoOrderDataManager>();
ServerContentIoC.Register();
if (TestingCallbacks != null)
{
var cast = (ServerModuleTestingCallbacks) TestingCallbacks;

View File

@@ -0,0 +1,29 @@
using Content.Server.Cargo;
using Content.Server.Chat;
using Content.Server.GameTicking;
using Content.Server.Interfaces;
using Content.Server.Interfaces.Chat;
using Content.Server.Interfaces.GameTicking;
using Content.Server.Sandbox;
using Content.Server.Utility;
using Content.Shared.Interfaces;
using Robust.Shared.IoC;
namespace Content.Server
{
internal static class ServerContentIoC
{
public static void Register()
{
IoCManager.Register<ISharedNotifyManager, ServerNotifyManager>();
IoCManager.Register<IServerNotifyManager, ServerNotifyManager>();
IoCManager.Register<IGameTicker, GameTicker>();
IoCManager.Register<IChatManager, ChatManager>();
IoCManager.Register<IMoMMILink, MoMMILink>();
IoCManager.Register<ISandboxManager, SandboxManager>();
IoCManager.Register<IGalacticBankManager, GalacticBankManager>();
IoCManager.Register<ICargoOrderDataManager, CargoOrderDataManager>();
IoCManager.Register<IModuleManager, ServerModuleManager>();
}
}
}

View File

@@ -0,0 +1,15 @@
using Content.Shared.Interfaces;
namespace Content.Server.Utility
{
/// <summary>
/// Server implementation of IModuleManager.
/// Provides simple way for shared code to check if it's being run by
/// the client of the server.
/// </summary>
public class ServerModuleManager : IModuleManager
{
bool IModuleManager.IsClientModule => false;
bool IModuleManager.IsServerModule => true;
}
}

View File

@@ -1,10 +1,10 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using Content.Shared.Interfaces;
using Content.Shared.Interfaces.Chemistry;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
using Robust.Shared.Utility;
using YamlDotNet.RepresentationModel;
namespace Content.Shared.Chemistry
@@ -12,6 +12,10 @@ namespace Content.Shared.Chemistry
[Prototype("reagent")]
public class ReagentPrototype : IPrototype, IIndexedPrototype
{
#pragma warning disable 649
[Dependency] private readonly IModuleManager _moduleManager;
#pragma warning restore 649
private string _id;
private string _name;
private string _description;
@@ -25,6 +29,11 @@ namespace Content.Shared.Chemistry
//List of metabolism effects this reagent has, should really only be used server-side.
public List<IMetabolizable> Metabolism => _metabolism;
public ReagentPrototype()
{
IoCManager.InjectDependencies(this);
}
public void LoadFrom(YamlMappingNode mapping)
{
var serializer = YamlObjectSerializer.NewReader(mapping);
@@ -33,7 +42,11 @@ namespace Content.Shared.Chemistry
serializer.DataField(ref _name, "name", string.Empty);
serializer.DataField(ref _description, "desc", string.Empty);
serializer.DataField(ref _substanceColor, "color", Color.White);
serializer.DataField(ref _metabolism, "metabolism", new List<IMetabolizable>{new DefaultMetabolizable()});
if (_moduleManager.IsServerModule)
serializer.DataField(ref _metabolism, "metabolism", new List<IMetabolizable> {new DefaultMetabolizable()});
else
_metabolism = new List<IMetabolizable> { new DefaultMetabolizable() };
}
}
}

View File

@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Content.Shared.Interfaces
{
/// <summary>
/// Provides a simple way to check whether calling code is being run by
/// Robust.Client, or Robust.Server. Useful for code in Content.Shared
/// that wants different behavior depending on if client or server is using it.
/// </summary>
public interface IModuleManager
{
/// <summary>
/// Returns true if the code is being run by the client, returns false otherwise.
/// </summary>
bool IsClientModule { get; }
/// <summary>
/// Returns true if the code is being run by the server, returns false otherwise.
/// </summary>
bool IsServerModule { get; }
}
}

View File

@@ -0,0 +1,23 @@
using Content.Client;
using Content.Server;
using Robust.UnitTesting;
namespace Content.Tests
{
public class ContentUnitTest : RobustUnitTest
{
protected override void OverrideIoC()
{
base.OverrideIoC();
if (Project == UnitTestProject.Server)
{
ServerContentIoC.Register();
}
else if (Project == UnitTestProject.Client)
{
ClientContentIoC.Register();
}
}
}
}

View File

@@ -7,8 +7,8 @@ using YamlDotNet.RepresentationModel;
namespace Content.Tests.Shared.Chemistry
{
[TestFixture, Parallelizable, TestOf(typeof(ReagentPrototype))]
public class ReagentPrototype_Tests
[TestFixture, TestOf(typeof(ReagentPrototype))]
public class ReagentPrototype_Tests : ContentUnitTest
{
[Test]
public void DeserializeReagentPrototype()