Files
RobustToolbox/Robust.UnitTesting/Server/RobustServerSimulation.cs
T
c17c8d7a11 Physics (#1605)
* Physics worlds

* Paul's a good boy

* Build working

* Ingame and not lagging to hell

* Why didn't you commit ahhhhh

* Hard collisions working

* Solver parity

* Decent broadphase work done

* BroadPhase outline done

* BroadPhase working

* waiting for pvs

* Fix static PVS AABB

* Stop static bodies from awakening

* Optimise a bunch of stuff

* Even more broadphase stuff

* I'm fucking stupid

* Optimise fixture updates

* Collision solver start

* Building

* A is for Argumentative

* Fix contact caching island flags

* Circle shapes actually workeded

* Damping

* DS2 consumables only

* Slightly more stable

* Even slightlier more stablier

* VV your heart out

* Initial joint support

* 90% of joints I just wanted to push as I'd scream if I lost progress

* JOINT PURGATORY

* Joints barely functional lmao

* Okay these joints slightly more functional

* Remove station FrictionJoint

* Also that

* Some Box2D ports

* Cleanup mass

* Edge shape

* Active contacts

* Fix active contacts

* Optimise active contacts even more

* Boxes be stacking

* I would die for smug oh my fucking god

* In which everything is fixed

* Distance joints working LETS GO

* Remove frequency on distancejoint

* Fix some stuff and break joints

* Crashing fixed mehbeh

* ICollideSpecial and more resilience

* auto-clear

* showbb vera

* Slap that TODO in there

* Fix restartround crash

* Random fixes

* Fix fixture networking

* Add intersection method for broadphase

* Fix contacts

* Licenses done

* Optimisations

* Fix wall clips

* Config caching for island

* allocations optimisations

* Optimise casts

* Optimise events queue for physics

* Contact manager optimisations

* Optimise controllers

* Sloth joint or something idk

* Controller graph

* Remove content cvar

* Random cleanup

* Finally remove VirtualController

* Manifold structs again

* Optimise this absolute retardation

* Optimise

* fix license

* Cleanup physics interface

* AHHHHHHHHHHHHH

* Fix collisions again

* snivybus

* Fix potential nasty manifold bug

* Tests go snivy

* Disable prediction for now

* Spans

* Fix ShapeTypes

* fixes

* ch ch changeesss

* Kinematic idea

* Prevent static bodies from waking

* Pass WorldAABB to MoveEvent

* Fix collisions

* manifold structs fucking WOOORRKKKINNGGG

* Better pushing

* Fix merge ickies

* Optimise MoveEvents

* Use event for collisions performance

* Fix content tests

* Do not research tests

* Fix most conflicts

* Paul's trying to kill me

* Maybe collisions work idk

* Make us whole again

* Smug is also trying to kill me

* nani

* shitty collisions

* Settling

* Do not research collisions

* SHIP IT

* Fix joints

* PVS moment

* Fix other assert

* Fix locker collisions

* serializable sleeptime

* Aether2D contacts

* Physics is no longer crashing (and burning)

* Add to the TODO list

Co-authored-by: Metal Gear Sloth <metalgearsloth@gmail.com>
2021-03-08 03:19:01 +11:00

245 lines
9.7 KiB
C#

using JetBrains.Annotations;
using Moq;
using Robust.Server.GameObjects;
using Robust.Shared.Asynchronous;
using Robust.Shared.Configuration;
using Robust.Shared.ContentPack;
using Robust.Shared.Exceptions;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Log;
using Robust.Shared.Map;
using Robust.Shared.Network;
using Robust.Shared.Physics;
using Robust.Shared.Prototypes;
using Robust.Shared.Reflection;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Serialization.TypeSerializers.Implementations;
using Robust.Shared.Timing;
namespace Robust.UnitTesting.Server
{
[PublicAPI]
internal interface ISimulationFactory
{
ISimulationFactory RegisterComponents(CompRegistrationDelegate factory);
ISimulationFactory RegisterDependencies(DiContainerDelegate factory);
ISimulationFactory RegisterEntitySystems(EntitySystemRegistrationDelegate factory);
ISimulationFactory RegisterPrototypes(PrototypeRegistrationDelegate factory);
ISimulation InitializeInstance();
}
[PublicAPI]
internal interface ISimulation
{
IDependencyCollection Collection { get; }
/// <summary>
/// Resolves a dependency directly out of IoC collection.
/// </summary>
T Resolve<T>();
/// <summary>
/// Adds a new map directly to the map manager.
/// </summary>
EntityUid AddMap(int mapId);
EntityUid AddMap(MapId mapId);
IEntity SpawnEntity(string? protoId, EntityCoordinates coordinates);
IEntity SpawnEntity(string? protoId, MapCoordinates coordinates);
}
internal delegate void DiContainerDelegate(IDependencyCollection diContainer);
internal delegate void CompRegistrationDelegate(IComponentFactory factory);
internal delegate void EntitySystemRegistrationDelegate(IEntitySystemManager systemMan);
internal delegate void PrototypeRegistrationDelegate(IPrototypeManager protoMan);
internal class RobustServerSimulation : ISimulation, ISimulationFactory
{
private DiContainerDelegate? _diFactory;
private CompRegistrationDelegate? _regDelegate;
private EntitySystemRegistrationDelegate? _systemDelegate;
private PrototypeRegistrationDelegate? _protoDelegate;
public IDependencyCollection Collection { get; private set; } = default!;
public T Resolve<T>()
{
return Collection.Resolve<T>();
}
public EntityUid AddMap(int mapId)
{
var mapMan = Collection.Resolve<IMapManager>();
mapMan.CreateMap(new MapId(mapId));
return mapMan.GetMapEntityId(new MapId(mapId));
}
public EntityUid AddMap(MapId mapId)
{
var mapMan = Collection.Resolve<IMapManager>();
mapMan.CreateMap(mapId);
return mapMan.GetMapEntityId(mapId);
}
public IEntity SpawnEntity(string? protoId, EntityCoordinates coordinates)
{
var entMan = Collection.Resolve<IEntityManager>();
return entMan.SpawnEntity(protoId, coordinates);
}
public IEntity SpawnEntity(string? protoId, MapCoordinates coordinates)
{
var entMan = Collection.Resolve<IEntityManager>();
return entMan.SpawnEntity(protoId, coordinates);
}
private RobustServerSimulation() { }
public ISimulationFactory RegisterDependencies(DiContainerDelegate factory)
{
_diFactory += factory;
return this;
}
public ISimulationFactory RegisterComponents(CompRegistrationDelegate factory)
{
_regDelegate += factory;
return this;
}
public ISimulationFactory RegisterEntitySystems(EntitySystemRegistrationDelegate factory)
{
_systemDelegate += factory;
return this;
}
public ISimulationFactory RegisterPrototypes(PrototypeRegistrationDelegate factory)
{
_protoDelegate += factory;
return this;
}
public ISimulation InitializeInstance()
{
var container = new DependencyCollection();
Collection = container;
IoCManager.InitThread(container, true);
//TODO: This is a long term project that should eventually have parity with the actual server/client/SP IoC registration.
// The goal is to be able to pull out all networking and frontend dependencies, and only have a core simulation running.
// This does NOT replace the full RobustIntegrationTest, or regular unit testing. This simulation sits in the middle
// and allows you to run integration testing only on the simulation.
//Tier 1: System
container.Register<ILogManager, LogManager>();
container.Register<IRuntimeLog, RuntimeLog>();
container.Register<IConfigurationManager, ConfigurationManager>();
container.Register<IDynamicTypeFactory, DynamicTypeFactory>();
container.Register<IDynamicTypeFactoryInternal, DynamicTypeFactory>();
container.Register<ILocalizationManager, LocalizationManager>();
container.Register<IModLoader, TestingModLoader>();
container.Register<IModLoaderInternal, TestingModLoader>();
container.RegisterInstance<ITaskManager>(new Mock<ITaskManager>().Object);
var reflectionManager = new Mock<IReflectionManager>();
reflectionManager
.Setup(x => x.FindTypesWithAttribute<MeansDataDefinition>())
.Returns(() => new[]
{
typeof(DataDefinition)
});
reflectionManager
.Setup(x => x.FindTypesWithAttribute(typeof(DataDefinition)))
.Returns(() => new[]
{
typeof(EntityPrototype),
typeof(TransformComponent),
typeof(MetaDataComponent)
});
reflectionManager
.Setup(x => x.FindTypesWithAttribute<TypeSerializerAttribute>())
.Returns(() => new[] {typeof(StringSerializer)});
container.RegisterInstance<IReflectionManager>(reflectionManager.Object); // tests should not be searching for types
container.RegisterInstance<IRobustSerializer>(new Mock<IRobustSerializer>().Object);
container.RegisterInstance<IResourceManager>(new Mock<IResourceManager>().Object); // no disk access for tests
container.RegisterInstance<IGameTiming>(new Mock<IGameTiming>().Object); // TODO: get timing working similar to RobustIntegrationTest
//Tier 2: Simulation
container.Register<IServerEntityManager, ServerEntityManager>();
container.Register<IEntityManager, ServerEntityManager>();
container.Register<ISerializationManager, SerializationManager>();
container.Register<IComponentManager, ComponentManager>();
container.Register<IMapManager, MapManager>();
container.Register<IPrototypeManager, PrototypeManager>();
container.Register<IComponentFactory, ComponentFactory>();
container.Register<IComponentDependencyManager, ComponentDependencyManager>();
container.Register<IEntitySystemManager, EntitySystemManager>();
container.Register<IPhysicsManager, PhysicsManager>();
container.RegisterInstance<IPauseManager>(new Mock<IPauseManager>().Object); // TODO: get timing working similar to RobustIntegrationTest
//Tier 3: Networking
//TODO: Try to remove these
container.RegisterInstance<IEntityNetworkManager>(new Mock<IEntityNetworkManager>().Object);
container.RegisterInstance<INetManager>(new Mock<INetManager>().Object);
_diFactory?.Invoke(container);
container.BuildGraph();
var logMan = container.Resolve<ILogManager>();
logMan.RootSawmill.AddHandler(new TestLogHandler("SIM"));
var compFactory = container.Resolve<IComponentFactory>();
compFactory.Register<MetaDataComponent>();
compFactory.RegisterReference<MetaDataComponent, IMetaDataComponent>();
compFactory.Register<TransformComponent>();
compFactory.RegisterReference<TransformComponent, ITransformComponent>();
compFactory.Register<MapComponent>();
compFactory.RegisterReference<MapComponent, IMapComponent>();
compFactory.Register<MapGridComponent>();
compFactory.RegisterReference<MapGridComponent, IMapGridComponent>();
compFactory.Register<PhysicsComponent>();
compFactory.RegisterReference<PhysicsComponent, IPhysBody>();
_regDelegate?.Invoke(compFactory);
var entityMan = container.Resolve<IEntityManager>();
entityMan.Initialize();
_systemDelegate?.Invoke(container.Resolve<IEntitySystemManager>());
entityMan.Startup();
var mapManager = container.Resolve<IMapManager>();
mapManager.Initialize();
mapManager.Startup();
container.Resolve<ISerializationManager>().Initialize();
var protoMan = container.Resolve<IPrototypeManager>();
protoMan.RegisterType(typeof(EntityPrototype));
_protoDelegate?.Invoke(protoMan);
protoMan.Resync();
return this;
}
public static ISimulationFactory NewSimulation()
{
return new RobustServerSimulation();
}
}
}