Skip to content

Commit

Permalink
Core: Refactor following changes introduced in PR stride3d#2599
Browse files Browse the repository at this point in the history
  • Loading branch information
Eideren committed Jan 21, 2025
1 parent e830c34 commit 6ff9f04
Show file tree
Hide file tree
Showing 17 changed files with 74 additions and 120 deletions.
7 changes: 3 additions & 4 deletions sources/editor/Stride.Editor/Thumbnails/ThumbnailGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,13 @@ public ThumbnailGenerator(EffectCompilerBase effectCompiler)
GraphicsDeviceService = new GraphicsDeviceServiceLocal(Services, GraphicsDevice);
Services.AddService(GraphicsDeviceService);

var uiSystem = new UISystem(Services);
Services.AddService(uiSystem);

var physicsSystem = new Bullet2PhysicsSystem(Services);
Services.AddService<IPhysicsSystem>(physicsSystem);

gameSystems = new GameSystemCollection(Services) { fontSystem, uiSystem, physicsSystem };
gameSystems = new GameSystemCollection(Services) { fontSystem, physicsSystem };
Services.AddService<IGameSystemCollection>(gameSystems);

Services.GetOrCreate<UISystem>();
Simulation.DisableSimulation = true; //make sure we do not simulate physics within the editor

// initialize base services
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected override void OnSystemAdd()
{
SinglePassWireframeRenderFeature wireframeRenderFeature;

ServicesHelper.LoadBepuServices(Services, out _, out _shapeCacheSystem, out _);
_shapeCacheSystem = Services.GetOrCreate<ShapeCacheSystem>();
_game = Services.GetSafeServiceAs<IGame>();
_sceneSystem = Services.GetSafeServiceAs<SceneSystem>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,50 @@
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.

using Stride.BepuPhysics.Systems;
using Stride.Core;
using Stride.Core.Diagnostics;
using Stride.Data;
using Stride.Engine.Design;
using Stride.Games;

namespace Stride.BepuPhysics;
[DataContract]
[Display("Bepu Configuration")]
public class BepuConfiguration : Configuration
public class BepuConfiguration : Configuration, IService
{
public List<BepuSimulation> BepuSimulations = new();

private static readonly Logger _logger = GlobalLogger.GetLogger("BepuService");

public static IService NewInstance(IServiceRegistry services)
{
BepuConfiguration config;
if (services.GetService<IGameSettingsService>() is { } settings)
{
config = settings.Settings.Configurations.Get<BepuConfiguration>();
if (settings.Settings.Configurations.Configurations.Any(x => x.Configuration is BepuConfiguration) == false)
_logger.Warning("Creating a default configuration for Bepu as none were set up in your game's settings.");
}
else
config = new BepuConfiguration { BepuSimulations = [new BepuSimulation()] };

if (config.BepuSimulations.Count == 0)
{
_logger.Warning("No simulations configured for Bepu, please add one in your game's configuration.");
config.BepuSimulations.Add(new BepuSimulation());
}

var systems = services.GetSafeServiceAs<IGameSystemCollection>();
PhysicsGameSystem? physicsGameSystem = null;
foreach (var system in systems)
{
if (system is PhysicsGameSystem pgs)
physicsGameSystem = pgs;
}
if (physicsGameSystem == null)
systems.Add(new PhysicsGameSystem(config, services));

return config;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ public static class BepuSimulationExtensions
public static BepuSimulation GetSimulation(this Entity entity)
{
var services = entity.EntityManager.Services;
var config = services.GetService<BepuConfiguration>();
if (config == null)
ServicesHelper.LoadBepuServices(services, out config, out _, out _);

var config = services.GetOrCreate<BepuConfiguration>();
return SceneBasedSimulationSelector.Shared.Pick(config, entity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ public void SystemAdded(IServiceRegistry registryParam)
{
_services = registryParam;
registryParam.AddService(this);
ServicesHelper.LoadBepuServices(registryParam, out _config, out _, out _);
_config = registryParam.GetOrCreate<BepuConfiguration>();
}

public void SystemRemoved()
{
_services!.RemoveService<SimUpdateProcessor>();
_services!.RemoveService(this);
}

public void RebindSimulation(ISimulationUpdate item)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,7 @@ public void Initialize(IServiceRegistry services, Scene editorScene)

private void PrepareModels()
{
var bepuShapeCacheSys = _services.GetService<ShapeCacheSystem>();
if (bepuShapeCacheSys == null)
{
bepuShapeCacheSys = new ShapeCacheSystem(_services);
_services.AddService(bepuShapeCacheSys);
}
var bepuShapeCacheSys = _services.GetOrCreate<ShapeCacheSystem>();
var graphicsDevice = _services.GetSafeServiceAs<IGraphicsDeviceService>().GraphicsDevice;

if (_component.Collider is MeshCollider meshCollider && (meshCollider.Model.Meshes.Count == 0 || meshCollider.Model == null!/*May be null in editor*/))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ public CollidableProcessor()

protected override void OnSystemAdd()
{
ServicesHelper.LoadBepuServices(Services, out var config, out var shapes, out _);
BepuConfiguration = config;
ShapeCache = shapes;
BepuConfiguration = Services.GetOrCreate<BepuConfiguration>();
ShapeCache = Services.GetOrCreate<ShapeCacheSystem>();
}

public override unsafe void Draw(RenderContext context) // While this is not related to drawing, we're doing this in draw as it runs after the TransformProcessor updates WorldMatrix
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public ConstraintProcessor()

protected override void OnSystemAdd()
{
ServicesHelper.LoadBepuServices(Services, out _bepuConfiguration, out _, out _);
_bepuConfiguration = Services.GetOrCreate<BepuConfiguration>();
}

protected override void OnEntityComponentAdding(Entity entity, ConstraintComponentBase component, ConstraintComponentBase data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ internal class PhysicsGameSystem : GameSystemBase
{
private BepuConfiguration _bepuConfiguration;

public PhysicsGameSystem(IServiceRegistry registry) : base(registry)
public PhysicsGameSystem(BepuConfiguration configuration, IServiceRegistry registry) : base(registry)
{
_bepuConfiguration = registry.GetService<BepuConfiguration>();
_bepuConfiguration = configuration;
UpdateOrder = SystemsOrderHelper.ORDER_OF_GAME_SYSTEM;
Enabled = true; //enabled by default

Expand All @@ -35,4 +35,4 @@ public override void Update(GameTime time)
bepuSim.Update(elapsed);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

namespace Stride.BepuPhysics.Systems;

internal class ShapeCacheSystem : IDisposable
internal class ShapeCacheSystem : IDisposable, IService
{
internal readonly BasicMeshBuffers _boxShapeData;
internal readonly BasicMeshBuffers _cylinderShapeData;
Expand Down Expand Up @@ -424,4 +424,6 @@ public void GetBuffers(out VertexPosition3[] vertices, out int[] indices)
// /!\ THIS MAY RUN OUTSIDE OF THE MAIN THREAD /!\
}
}

public static IService NewInstance(IServiceRegistry services) => new ShapeCacheSystem(services);
}
8 changes: 1 addition & 7 deletions sources/engine/Stride.UI.Tests/Regression/UITestGameBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,7 @@ protected override async Task LoadContent()
UIComponent.ResolutionStretch = ResolutionStretch.FixedWidthFixedHeight;
Scene.Entities.Add(UIRoot);

UI = Services.GetService<UISystem>();
if (UI == null)
{
UI = new UISystem(Services);
Services.AddService(UI);
GameSystems.Add(UI);
}
UI = Services.GetOrCreate<UISystem>();

Camera = new Entity("Scene camera") { new CameraComponent { Slot = SceneSystem.GraphicsCompositor.Cameras[0].ToSlotId() } };
Camera.Transform.Position = new Vector3(0, 0, 1000);
Expand Down
9 changes: 1 addition & 8 deletions sources/engine/Stride.UI/Renderers/ElementRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,7 @@ public ElementRenderer(IServiceRegistry services)
Content = services.GetSafeServiceAs<IContentManager>();
GraphicsDeviceService = services.GetSafeServiceAs<IGraphicsDeviceService>();

UI = services.GetService<UISystem>();
if (UI == null)
{
UI = new UISystem(services);
services.AddService(UI);
var gameSystems = services.GetService<IGameSystemCollection>();
gameSystems?.Add(UI);
}
UI = services.GetOrCreate<UISystem>();
}

/// <summary>
Expand Down
10 changes: 1 addition & 9 deletions sources/engine/Stride.UI/Rendering/UI/UIRenderFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,9 @@ protected override void InitializeCore()
Name = "UIComponentRenderer";
game = RenderSystem.Services.GetService<IGame>();
input = RenderSystem.Services.GetService<InputManager>();
uiSystem = RenderSystem.Services.GetService<UISystem>();
uiSystem = RenderSystem.Services.GetOrCreate<UISystem>();
graphicsDeviceService = RenderSystem.Services.GetSafeServiceAs<IGraphicsDeviceService>();

if (uiSystem == null)
{
var gameSytems = RenderSystem.Services.GetSafeServiceAs<IGameSystemCollection>();
uiSystem = new UISystem(RenderSystem.Services);
RenderSystem.Services.AddService(uiSystem);
gameSytems.Add(uiSystem);
}

rendererManager = new RendererManager(new DefaultRenderersFactory(RenderSystem.Services));

batch = uiSystem.Batch;
Expand Down
8 changes: 6 additions & 2 deletions sources/engine/Stride.UI/UISystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Stride.UI
/// <summary>
/// Interface of the UI system.
/// </summary>
public class UISystem : GameSystemBase
public class UISystem : GameSystemBase, IService
{
internal UIBatch Batch { get; private set; }

Expand All @@ -29,6 +29,8 @@ public class UISystem : GameSystemBase
public UISystem(IServiceRegistry registry)
: base(registry)
{
var gameSystems = registry.GetService<IGameSystemCollection>();
gameSystems?.Add(this);
}

public override void Initialize()
Expand Down Expand Up @@ -166,5 +168,7 @@ private void UpdateKeyEvents()
UIElement.FocusedElement?.RaiseKeyDownEvent(new KeyEventArgs { Key = key, Input = input });
}
}

public static IService NewInstance(IServiceRegistry services) => new UISystem(services);
}
}
}
9 changes: 1 addition & 8 deletions sources/engine/Stride.Video/VideoProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,7 @@ protected override void OnEntityComponentRemoved(Entity entity, VideoComponent c
/// <inheritdoc />
protected override void OnSystemAdd()
{
var videoSystem = Services.GetService<VideoSystem>();
if (videoSystem == null)
{
videoSystem = new VideoSystem(Services);
Services.AddService(videoSystem);
var gameSystems = Services.GetSafeServiceAs<IGameSystemCollection>();
gameSystems.Add(videoSystem);
}
Services.GetOrCreate<VideoSystem>();
}
}
}
10 changes: 9 additions & 1 deletion sources/engine/Stride.Video/VideoSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@

namespace Stride.Video
{
public partial class VideoSystem : GameSystemBase
public partial class VideoSystem : GameSystemBase, IService
{
public VideoSystem([NotNull] IServiceRegistry registry)
: base(registry)
{
}

public static IService NewInstance(IServiceRegistry services)
{
var instance = new VideoSystem(services);
var gameSystems = services.GetSafeServiceAs<IGameSystemCollection>();
gameSystems.Add(instance);
return instance;
}
}
}

0 comments on commit 6ff9f04

Please sign in to comment.